Bug 26384: Fix executable flags
[koha-ffzg.git] / t / db_dependent / Accounts.t
old mode 100644 (file)
new mode 100755 (executable)
index 1e214fa..12e11f5
@@ -26,12 +26,15 @@ use t::lib::TestBuilder;
 use t::lib::Mocks;
 
 use Koha::Account;
+use Koha::Account::DebitTypes;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
 use Koha::Notice::Messages;
 use Koha::Notice::Templates;
 use Koha::DateUtils qw( dt_from_string );
 
+use C4::Circulation qw( MarkIssueReturned );
+
 BEGIN {
     use_ok('C4::Accounts');
     use_ok('Koha::Object');
@@ -58,7 +61,6 @@ $dbh->do(q|DELETE FROM issues|);
 $dbh->do(q|DELETE FROM borrowers|);
 
 my $branchcode = $library->{branchcode};
-my $borrower_number;
 
 my $context = new Test::MockModule('C4::Context');
 $context->mock( 'userenv', sub {
@@ -73,15 +75,15 @@ my $userenv_branchcode = $branchcode;
 
 # Test manualinvoice
 my $itemtype = $builder->build( { source => 'Itemtype' } );
-my $item   = $builder->build( { source => 'Item', value => { itype => $itemtype->{itemtype} } } );
+my $item   = $builder->build_sample_item( { itype => $itemtype->{itemtype} } );
 my $patron = $builder->build( { source => 'Borrower' } );
-my $amount = '5.000000';
+my $amount = 5;
 my $description = "Test fee!";
 my $type = 'LOST';
 my $note = 'Test note!';
 warning_like {
     C4::Accounts::manualinvoice( $patron->{borrowernumber},
-        $item->{itemnumber}, $description, $type, $amount, $note )
+        $item->itemnumber, $description, $type, $amount, $note )
 }
 qr/C4::Accounts::manualinvoice is deprecated in favor of Koha::Account->add_debit/,
   "deprecation warning received for manualinvoice";
@@ -90,8 +92,8 @@ my ($accountline) = Koha::Account::Lines->search(
         borrowernumber => $patron->{borrowernumber}
     }
 );
-is( $accountline->accounttype, $type, 'Accountline type set correctly for manualinvoice' );
-is( $accountline->amount, $amount, 'Accountline amount set correctly for manualinvoice' );
+is( $accountline->debit_type_code, $type, 'Debit type set correctly for manualinvoice' );
+is( $accountline->amount+0, $amount, 'Accountline amount set correctly for manualinvoice' );
 ok( $accountline->description =~ /^$description/, 'Accountline description set correctly for manualinvoice' );
 is( $accountline->note, $note, 'Accountline note set correctly for manualinvoice' );
 is( $accountline->branchcode, $branchcode, 'Accountline branchcode set correctly for manualinvoice' );
@@ -111,31 +113,41 @@ my $sth = $dbh->prepare(
          amountoutstanding,
          date,
          description,
-         interface
+         interface,
+         credit_type_code,
+         debit_type_code
      )
-     VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ?, ? )"
+     VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ?, ?, ?, ? )"
 );
 
 my $days = 5;
 
 my @test_data = (
-    { amount => 0     , days_ago => 0         , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today'                     , delete => 0 } ,
-    { amount => 0     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day'      , delete => 0 } ,
-    { amount => 0     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day'          , delete => 0 } ,
-    { amount => 0     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day'           , delete => 1 } ,
-    { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day'        , delete => 1 } ,
-    { amount => 5     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day'  , delete => 0 } ,
-    { amount => 5     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day'      , delete => 0 } ,
-    { amount => 5     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day'   , delete => 0 } ,
-    { amount => -5    , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
-    { amount => -5    , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0 } ,
-    { amount => -5    , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0 }
+    { amount => 0     , days_ago => 0         , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today'                     , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
+    { amount => 0     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day'      , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
+    { amount => 0     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day'          , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
+    { amount => 0     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day'           , delete => 1, credit_type => undef, debit_type => 'OVERDUE' } ,
+    { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day'        , delete => 1, credit_type => undef, debit_type => 'OVERDUE' } ,
+    { amount => 5     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
+    { amount => 5     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day'     , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
+    { amount => 5     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day'  , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
+    { amount => -5    , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0, credit_type => 'PAYMENT', debit_type => undef } ,
+    { amount => -5    , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0, credit_type => 'PAYMENT', debit_type => undef } ,
+    { amount => -5    , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0, credit_type => 'PAYMENT', debit_type => undef }
 );
 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
 
 for my $data ( @test_data ) {
-    $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description}, 'commandline');
+    $sth->execute(
+        $borrower->borrowernumber,
+        $data->{amount},
+        $data->{days_ago},
+        $data->{description},
+        'commandline',
+        $data->{credit_type},
+        $data->{debit_type}
+    );
 }
 
 purge_zero_balance_fees( $days );
@@ -180,8 +192,8 @@ subtest "Koha::Account::pay tests" => sub {
 
     my $account = Koha::Account->new({ patron_id => $borrower->id });
 
-    my $line1 = $account->add_debit({ type => 'account', amount => 100, interface => 'commandline' });
-    my $line2 = $account->add_debit({ type => 'account', amount => 200, interface => 'commandline' });
+    my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 100, interface => 'commandline' });
+    my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 200, interface => 'commandline' });
 
     $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
     $sth->execute;
@@ -201,7 +213,7 @@ subtest "Koha::Account::pay tests" => sub {
     my $borrowernumber = $borrower->borrowernumber;
     my $data = '20.00';
     my $payment_note = '$20.00 payment note';
-    my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } );
+    my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } )->{payment_id};
 
     my $accountline = Koha::Account::Lines->find( $id );
     is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
@@ -280,12 +292,12 @@ subtest "Koha::Account::pay tests" => sub {
     $note = $sth->fetchrow_array;
     is($note,'$200.00 payment note', '$200.00 payment note is registered');
 
-    my $line3 = $account->add_debit({ type => 'account', amount => 42, interface => 'commandline' });
-    my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
+    my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
+    my $payment_id = $account->pay( { lines => [$line3], amount => 42 } )->{payment_id};
     my $payment = Koha::Account::Lines->find( $payment_id );
-    is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
+    is( $payment->amount()+0, -42, "Payment paid the specified fine" );
     $line3 = Koha::Account::Lines->find( $line3->id );
-    is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
+    is( $line3->amountoutstanding+0, 0, "Specified fine is paid" );
     is( $payment->branchcode, undef, 'branchcode passed, then undef' );
 };
 
@@ -308,10 +320,10 @@ subtest "Koha::Account::pay particular line tests" => sub {
 
     my $account = Koha::Account->new({ patron_id => $borrower->id });
 
-    my $line1 = $account->add_debit({ type => 'account', amount => 1, interface => 'commandline' });
-    my $line2 = $account->add_debit({ type => 'account', amount => 2, interface => 'commandline' });
-    my $line3 = $account->add_debit({ type => 'account', amount => 3, interface => 'commandline' });
-    my $line4 = $account->add_debit({ type => 'account', amount => 4, interface => 'commandline' });
+    my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 1, interface => 'commandline' });
+    my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 2, interface => 'commandline' });
+    my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 3, interface => 'commandline' });
+    my $line4 = $account->add_debit({ type => 'ACCOUNT', amount => 4, interface => 'commandline' });
 
     is( $account->balance(), 10, "Account balance is 10" );
 
@@ -325,13 +337,13 @@ subtest "Koha::Account::pay particular line tests" => sub {
     $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
 
     # Line1 is not paid at all, as it was not passed in the lines param
-    is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
+    is( $line1->amountoutstanding+0, 1, "Line 1 was not paid" );
     # Line2 was paid in full, as it was the first in the lines list
-    is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
+    is( $line2->amountoutstanding+0, 0, "Line 2 was paid in full" );
     # Line3 was paid partially, as the remaining balance did not cover it entirely
-    is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
+    is( $line3->amountoutstanding+0, 1, "Line 3 was paid to 1.00" );
     # Line4 was not paid at all, as the payment was all used up by that point
-    is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
+    is( $line4->amountoutstanding+0, 4, "Line 4 was not paid" );
 };
 
 subtest "Koha::Account::pay writeoff tests" => sub {
@@ -353,7 +365,7 @@ subtest "Koha::Account::pay writeoff tests" => sub {
 
     my $account = Koha::Account->new({ patron_id => $borrower->id });
 
-    my $line = $account->add_debit({ type => 'account', amount => 42, interface => 'commandline' });
+    my $line = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
 
     is( $account->balance(), 42, "Account balance is 42" );
 
@@ -361,19 +373,19 @@ subtest "Koha::Account::pay writeoff tests" => sub {
         {
             lines  => [$line],
             amount => 42,
-            type   => 'writeoff',
+            type   => 'WRITEOFF',
         }
-    );
+    )->{payment_id};
 
     $line->_result->discard_changes();
 
-    is( $line->amountoutstanding, "0.000000", "Line was written off" );
+    is( $line->amountoutstanding+0, 0, "Line was written off" );
 
     my $writeoff = Koha::Account::Lines->find( $id );
 
-    is( $writeoff->accounttype, 'W', 'Type is correct' );
+    is( $writeoff->credit_type_code, 'WRITEOFF', 'Type is correct for WRITEOFF' );
     is( $writeoff->description, 'Writeoff', 'Description is correct' );
-    is( $writeoff->amount, '-42.000000', 'Amount is correct' );
+    is( $writeoff->amount+0, -42, 'Amount is correct' );
 };
 
 subtest "More Koha::Account::pay tests" => sub {
@@ -391,11 +403,17 @@ subtest "More Koha::Account::pay tests" => sub {
     })->{ borrowernumber };
 
     my $amount = 100;
-    my $accountline = $builder->build({ source => 'Accountline',
-        value  => { borrowernumber => $borrowernumber,
-                    amount => $amount,
-                    amountoutstanding => $amount }
-    });
+    my $accountline = $builder->build(
+        {
+            source => 'Accountline',
+            value  => {
+                borrowernumber    => $borrowernumber,
+                amount            => $amount,
+                amountoutstanding => $amount,
+                credit_type_code  => undef,
+            }
+        }
+    );
 
     my $rs = $schema->resultset('Accountline')->search({
         borrowernumber => $borrowernumber
@@ -409,12 +427,12 @@ subtest "More Koha::Account::pay tests" => sub {
     $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
 
     my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
-    is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
+    is( $offset->amount+0, -100, 'Offset amount is -100.00' );
     is( $offset->type(), 'Payment', 'Offset type is Payment' );
 
     my $stat = $schema->resultset('Statistic')->search({
         branch  => $branch,
-        type    => 'payment'
+        type    => 'PAYMENT'
     }, { order_by => { -desc => 'datetime' } })->next();
 
     ok( defined $stat, "There's a payment log that matches the branch" );
@@ -445,11 +463,17 @@ subtest "Even more Koha::Account::pay tests" => sub {
 
     my $amount = 100;
     my $partialamount = 60;
-    my $accountline = $builder->build({ source => 'Accountline',
-        value  => { borrowernumber => $borrowernumber,
-                    amount => $amount,
-                    amountoutstanding => $amount }
-    });
+    my $accountline = $builder->build(
+        {
+            source => 'Accountline',
+            value  => {
+                borrowernumber    => $borrowernumber,
+                amount            => $amount,
+                amountoutstanding => $amount,
+                credit_type_code  => undef,
+            }
+        }
+    );
 
     my $rs = $schema->resultset('Accountline')->search({
         borrowernumber => $borrowernumber
@@ -463,12 +487,12 @@ subtest "Even more Koha::Account::pay tests" => sub {
     $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
 
     my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
-    is( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
+    is( $offset->amount+0, -60, 'Offset amount is -60.00' );
     is( $offset->type, 'Payment', 'Offset type is payment' );
 
     my $stat = $schema->resultset('Statistic')->search({
         branch  => $branch,
-        type    => 'payment'
+        type    => 'PAYMENT'
     }, { order_by => { -desc => 'datetime' } })->next();
 
     ok( defined $stat, "There's a payment log that matches the branch" );
@@ -497,7 +521,8 @@ subtest 'balance' => sub {
             value  => {
                 borrowernumber    => $patron->borrowernumber,
                 amount            => 42,
-                amountoutstanding => 42
+                amountoutstanding => 42,
+                credit_type_code  => undef,
             }
         }
     );
@@ -507,7 +532,8 @@ subtest 'balance' => sub {
             value  => {
                 borrowernumber    => $patron->borrowernumber,
                 amount            => -13,
-                amountoutstanding => -13
+                amountoutstanding => -13,
+                debit_type_code   => undef,
             }
         }
     );
@@ -560,59 +586,60 @@ subtest "C4::Accounts::chargelostitem tests" => sub {
             processfee => 2.04,
     }});
     my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
-    my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
-    my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
-    my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
-    my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
+    my $cli_itemnumber1 = $builder->build_sample_item({ itype => $itype_no_replace_no_fee->{itemtype} })->itemnumber;
+    my $cli_itemnumber2 = $builder->build_sample_item({ itype => $itype_replace_no_fee->{itemtype} })->itemnumber;
+    my $cli_itemnumber3 = $builder->build_sample_item({ itype => $itype_no_replace_fee->{itemtype} })->itemnumber;
+    my $cli_itemnumber4 = $builder->build_sample_item({ itype => $itype_replace_fee->{itemtype} })->itemnumber;
 
     my $cli_issue_id_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1 } })->{issue_id};
     my $cli_issue_id_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2 } })->{issue_id};
     my $cli_issue_id_3 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3 } })->{issue_id};
     my $cli_issue_id_4 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
+    my $cli_issue_id_4X = undef;
 
     my $lostfine;
     my $procfee;
 
     subtest "fee application tests" => sub {
-        plan tests => 40;
+        plan tests => 44;
 
         t::lib::Mocks::mock_preference('item-level_itypes', '1');
         t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
 
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
         ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
         ok( !$procfee,  "No processing fee if no processing fee");
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
         ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
         ok( !$procfee,  "No processing fee if no processing fee");
         $lostfine->delete();
 
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
         ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
         ok( !$procfee,  "No processing fee if no processing fee");
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
         ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
         ok( !$procfee,  "No processing fee if no processing fee");
         $lostfine->delete();
 
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
         ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
         ok( $procfee->amount == 8.16,  "Processing fee if processing fee");
         is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
         $procfee->delete();
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
         ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
         ok( $procfee->amount == 8.16,  "Processing fee if processing fee");
         is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
@@ -620,15 +647,15 @@ subtest "C4::Accounts::chargelostitem tests" => sub {
         $procfee->delete();
 
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
         ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
         ok( $procfee->amount == 2.04,  "Processing fee if processing fee");
         is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
         $procfee->delete();
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
         ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
         ok( $procfee->amount == 2.04,  "Processing fee if processing fee");
         is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
@@ -638,58 +665,70 @@ subtest "C4::Accounts::chargelostitem tests" => sub {
         t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
 
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
         ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
         ok( !$procfee,  "No processing fee if no processing fee");
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
         is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
         ok( !$procfee,  "No processing fee if no processing fee");
 
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
         is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
         ok( !$procfee,  "No processing fee if no processing fee");
         $lostfine->delete();
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
         is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
         ok( !$procfee,  "No processing fee if no processing fee");
 
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
         ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
         is( $procfee->amount, "8.160000",  "Processing fee if processing fee");
         is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
         $procfee->delete();
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
         is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
         is( $procfee->amount, "8.160000",  "Processing fee if processing fee");
         is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
 
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
         is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
         is( $procfee->amount, "2.040000",  "Processing fee if processing fee");
         is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
         $lostfine->delete();
         $procfee->delete();
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
         is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
         is( $procfee->amount, "2.040000",  "Processing fee if processing fee");
         is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
-        $lostfine->delete();
-        $procfee->delete();
+        C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
+        my $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
+        my $procfees  = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
+        ok( $lostfines->count == 1 , "Lost fine cannot be double charged for the same issue_id");
+        ok( $procfees->count == 1,  "Processing fee cannot be double charged for the same issue_id");
+        MarkIssueReturned($cli_borrowernumber, $cli_itemnumber4);
+        $cli_issue_id_4X = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
+        C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
+        $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
+        $procfees  = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
+        ok( $lostfines->count == 2 , "Lost fine can be charged twice for the same item if they are distinct issue_id's");
+        ok( $procfees->count == 2,  "Processing fee can be charged twice for the same item if they are distinct issue_id's");
+        $lostfines->delete();
+        $procfees->delete();
     };
 
     subtest "basic fields tests" => sub {
@@ -699,19 +738,19 @@ subtest "C4::Accounts::chargelostitem tests" => sub {
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, '1.99', "Perdedor");
 
         # Lost Item Fee
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
         ok($lostfine, "Lost fine created");
         is($lostfine->manager_id, $staff_id, "Lost fine manager_id set correctly");
-        is($lostfine->issue_id, $cli_issue_id_4, "Lost fine issue_id set correctly");
+        is($lostfine->issue_id, $cli_issue_id_4X, "Lost fine issue_id set correctly");
         is($lostfine->description, "Perdedor", "Lost fine issue_id set correctly");
         is($lostfine->note, '', "Lost fine does not contain a note");
         is($lostfine->branchcode, $branchcode, "Lost fine branchcode set correctly");
 
         # Processing Fee
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
         ok($procfee, "Processing fee created");
         is($procfee->manager_id, $staff_id, "Processing fee manager_id set correctly");
-        is($procfee->issue_id, $cli_issue_id_4, "Processing fee issue_id set correctly");
+        is($procfee->issue_id, $cli_issue_id_4X, "Processing fee issue_id set correctly");
         is($procfee->description, "Perdedor", "Processing fee issue_id set correctly");
         is($procfee->note, C4::Context->preference("ProcessingFeeNote"), "Processing fee contains note matching ProcessingFeeNote");
         is($procfee->branchcode, $branchcode, "Processing fee branchcode set correctly");
@@ -726,16 +765,16 @@ subtest "C4::Accounts::chargelostitem tests" => sub {
 
         t::lib::Mocks::mock_preference( 'FinesLog', 0 );
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
         is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No logs were added' );
         $lostfine->delete();
         $procfee->delete();
 
         t::lib::Mocks::mock_preference( 'FinesLog', 1 );
         C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
-        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'LOST' });
-        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
         is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Logs were added' );
         $lostfine->delete();
         $procfee->delete();
@@ -751,14 +790,13 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
     my $account = $patron->account;
 
-    my $today  = dt_from_string;
     my $res    = 3;
     my $rent   = 5;
     my $manual = 7;
     $account->add_debit(
         {
             description => 'a Res fee',
-            type        => 'reserve',
+            type        => 'RESERVE',
             amount      => $res,
             interface   => 'commandline'
         }
@@ -766,28 +804,27 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     $account->add_debit(
         {
             description => 'a Rental fee',
-            type        => 'rent',
+            type        => 'RENT',
             amount      => $rent,
             interface   => 'commandline'
         }
     );
+    Koha::Account::DebitTypes->find_or_create(
+        {
+            code        => 'Copie',
+            description => 'Fee for copie',
+            is_system   => 0
+        }
+    )->store;
     Koha::Account::Line->new(
         {
             borrowernumber    => $patron->borrowernumber,
-            date              => $today,
             description       => 'a Manual invoice fee',
-            accounttype       => 'Copie',
+            debit_type_code   => 'Copie',
             amountoutstanding => $manual,
             interface         => 'commandline'
         }
     )->store;
-    Koha::AuthorisedValue->new(
-        {
-            category         => 'MANUAL_INV',
-            authorised_value => 'Copie',
-            lib              => 'Fee for copie',
-        }
-    )->store;
 
 
     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   0 );
@@ -926,9 +963,32 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
         }
     );
 
-    my $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
-    my $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => -5, interface => 'commandline' })->store();
-    my $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
+    my $debit = Koha::Account::Line->new(
+        {
+            borrowernumber    => $patron->id,
+            date              => '1970-01-01 00:00:01',
+            amountoutstanding => 0,
+            interface         => 'commandline',
+            debit_type_code   => 'LOST'
+        }
+    )->store();
+    my $credit = Koha::Account::Line->new(
+        {
+            borrowernumber    => $patron->id,
+            date              => '1970-01-01 00:00:01',
+            amountoutstanding => -5,
+            interface         => 'commandline',
+            credit_type_code  => 'PAYMENT'
+        }
+    )->store();
+    my $offset = Koha::Account::Offset->new(
+        {
+            credit_id => $credit->id,
+            debit_id  => $debit->id,
+            type      => 'Payment',
+            amount    => 0
+        }
+    )->store();
     purge_zero_balance_fees( 1 );
     my $debit_2 = Koha::Account::Lines->find( $debit->id );
     my $credit_2 = Koha::Account::Lines->find( $credit->id );
@@ -936,9 +996,32 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
     is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
 
-    $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 5, interface => 'commanline' })->store();
-    $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
-    $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
+    $debit = Koha::Account::Line->new(
+        {
+            borrowernumber    => $patron->id,
+            date              => '1970-01-01 00:00:01',
+            amountoutstanding => 5,
+            interface         => 'commanline',
+            debit_type_code   => 'LOST'
+        }
+    )->store();
+    $credit = Koha::Account::Line->new(
+        {
+            borrowernumber    => $patron->id,
+            date              => '1970-01-01 00:00:01',
+            amountoutstanding => 0,
+            interface         => 'commandline',
+            credit_type_code  => 'PAYMENT'
+        }
+    )->store();
+    $offset = Koha::Account::Offset->new(
+        {
+            credit_id => $credit->id,
+            debit_id  => $debit->id,
+            type      => 'Payment',
+            amount    => 0
+        }
+    )->store();
     purge_zero_balance_fees( 1 );
     $debit_2 = $credit_2 = undef;
     $debit_2 = Koha::Account::Lines->find( $debit->id );
@@ -947,9 +1030,32 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
     is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
 
-    $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
-    $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
-    $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
+    $debit = Koha::Account::Line->new(
+        {
+            borrowernumber    => $patron->id,
+            date              => '1970-01-01 00:00:01',
+            amountoutstanding => 0,
+            interface         => 'commandline',
+            debit_type_code   => 'LOST'
+        }
+    )->store();
+    $credit = Koha::Account::Line->new(
+        {
+            borrowernumber    => $patron->id,
+            date              => '1970-01-01 00:00:01',
+            amountoutstanding => 0,
+            interface         => 'commandline',
+            credit_type_code  => 'PAYMENT'
+        }
+    )->store();
+    $offset = Koha::Account::Offset->new(
+        {
+            credit_id => $credit->id,
+            debit_id  => $debit->id,
+            type      => 'Payment',
+            amount    => 0
+        }
+    )->store();
     purge_zero_balance_fees( 1 );
     $debit_2 = Koha::Account::Lines->find( $debit->id );
     $credit_2 = Koha::Account::Lines->find( $credit->id );
@@ -978,15 +1084,31 @@ subtest "Koha::Account::Offset credit & debit tests" => sub {
 
     my $account = Koha::Account->new({ patron_id => $borrower->id });
 
-    my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10, interface => 'commandline' })->store();
-    my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20, interface => 'commandline' })->store();
+    my $line1 = Koha::Account::Line->new(
+        {
+            borrowernumber    => $borrower->borrowernumber,
+            amount            => 10,
+            amountoutstanding => 10,
+            interface         => 'commandline',
+            debit_type_code   => 'LOST'
+        }
+    )->store();
+    my $line2 = Koha::Account::Line->new(
+        {
+            borrowernumber    => $borrower->borrowernumber,
+            amount            => 20,
+            amountoutstanding => 20,
+            interface         => 'commandline',
+            debit_type_code   => 'LOST'
+        }
+    )->store();
 
     my $id = $account->pay(
         {
             lines  => [$line1, $line2],
             amount => 30,
         }
-    );
+    )->{payment_id};
 
     # Test debit and credit methods for Koha::Account::Offset
     my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
@@ -1038,37 +1160,44 @@ subtest "Payment notice tests" => sub {
     });
     my $account = Koha::Account->new({ patron_id => $borrower->id });
 
-    my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 27, interface => 'commandline' })->store();
+    my $line = Koha::Account::Line->new(
+        {
+            borrowernumber    => $borrower->borrowernumber,
+            amountoutstanding => 27,
+            interface         => 'commandline',
+            debit_type_code   => 'LOST'
+        }
+    )->store();
 
     my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
-    $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account.');
+    $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account. Your [% branch.branchcode %]');
     $letter->store();
 
     t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
-    my $id = $account->pay( { amount => 1 } );
+    my $id = $account->pay( { amount => 1 } )->{payment_id};
     is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
 
-    $id = $account->pay( { amount => 1, type => 'writeoff' } );
+    $id = $account->pay( { amount => 1, type => 'WRITEOFF' } )->{payment_id};
     is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
 
     t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
 
-    $id = $account->pay( { amount => 12 } );
+    $id = $account->pay( { amount => 12, library_id => $branchcode } )->{payment_id};
     my $notice = Koha::Notice::Messages->search()->next();
     is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
     is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
-    is( $notice->content, 'A payment of 12.00 has been applied to your account.', 'Notice content is correct for payment' );
+    is( $notice->content, "A payment of 12.00 has been applied to your account. Your $branchcode", 'Notice content is correct for payment' );
     $notice->delete();
 
     $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
-    $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.');
+    $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account. Your [% branch.branchcode %]');
     $letter->store();
 
-    $id = $account->pay( { amount => 13, type => 'writeoff' } );
+    $id = $account->pay( { amount => 13, type => 'WRITEOFF', library_id => $branchcode  } )->{payment_id};
     $notice = Koha::Notice::Messages->search()->next();
     is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
     is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
-    is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );
+    is( $notice->content, "A writeoff of 13.00 has been applied to your account. Your $branchcode", 'Notice content is correct for writeoff' );
 };
 
 1;