Bug 27272: Remove GetItemsInfo, GetItemsLocationInfo and GetHostItemsInfo
[koha-ffzg.git] / t / db_dependent / Accounts.t
old mode 100644 (file)
new mode 100755 (executable)
index 5ae8c20..b35c659
@@ -18,8 +18,9 @@
 
 use Modern::Perl;
 
-use Test::More tests => 33;
+use Test::More tests => 27;
 use Test::MockModule;
+use Test::Exception;
 use Test::Warn;
 
 use t::lib::TestBuilder;
@@ -36,7 +37,7 @@ use Koha::DateUtils qw( dt_from_string );
 use C4::Circulation qw( MarkIssueReturned );
 
 BEGIN {
-    use_ok('C4::Accounts');
+    use_ok('C4::Accounts', qw( chargelostitem purge_zero_balance_fees ));
     use_ok('Koha::Object');
     use_ok('Koha::Patron');
     use_ok('Data::Dumper');
@@ -45,7 +46,6 @@ BEGIN {
 can_ok( 'C4::Accounts',
     qw(
         chargelostitem
-        manualinvoice
         purge_zero_balance_fees )
 );
 
@@ -56,14 +56,9 @@ my $dbh = C4::Context->dbh;
 my $builder = t::lib::TestBuilder->new;
 my $library = $builder->build( { source => 'Branch' } );
 
-$dbh->do(q|DELETE FROM accountlines|);
-$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');
+my $context = Test::MockModule->new('C4::Context');
 $context->mock( 'userenv', sub {
     return {
         flags  => 1,
@@ -74,33 +69,6 @@ $context->mock( 'userenv', sub {
 $context->mock( 'interface', sub { return "commandline" } );
 my $userenv_branchcode = $branchcode;
 
-# Test manualinvoice
-my $itemtype = $builder->build( { source => 'Itemtype' } );
-my $item   = $builder->build( { source => 'Item', value => { itype => $itemtype->{itemtype} } } );
-my $patron = $builder->build( { source => 'Borrower' } );
-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 )
-}
-qr/C4::Accounts::manualinvoice is deprecated in favor of Koha::Account->add_debit/,
-  "deprecation warning received for manualinvoice";
-my ($accountline) = Koha::Account::Lines->search(
-    {
-        borrowernumber => $patron->{borrowernumber}
-    }
-);
-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' );
-
-$dbh->do(q|DELETE FROM accountlines|);
-
 # Testing purge_zero_balance_fees
 
 # The 3rd value in the insert is 'days ago' --
@@ -174,9 +142,9 @@ for my $data  (@test_data) {
 
 $dbh->do(q|DELETE FROM accountlines|);
 
-subtest "Koha::Account::pay tests" => sub {
+subtest "Koha::Account::pay - always AutoReconcile + notes tests" => sub {
 
-    plan tests => 14;
+    plan tests => 17;
 
     # Create a borrower
     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
@@ -203,103 +171,73 @@ subtest "Koha::Account::pay tests" => sub {
 
     # There is $100 in the account
     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    my $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, 300, 'The account has 300$ as expected' );
+    my $outstanding_debt = $account->outstanding_debits->total_outstanding;
+    is($outstanding_debt, 300, 'The account has $300 outstanding debt as expected' );
+    my $outstanding_credit = $account->outstanding_credits->total_outstanding;
+    is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
 
     # We make a $20 payment
     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" );
 
     # There is now $280 in the account
-    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, 280, 'The account has $280 as expected' );
+    $outstanding_debt = $account->outstanding_debits->total_outstanding;
+    is($outstanding_debt, 280, 'The account has $280 outstanding debt as expected' );
+    $outstanding_credit = $account->outstanding_credits->total_outstanding;
+    is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
 
     # Is the payment note well registered
-    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
-    $sth->execute($borrower->borrowernumber);
-    my $note = $sth->fetchrow_array;
-    is($note,'$20.00 payment note', '$20.00 payment note is registered');
+    is($accountline->note,'$20.00 payment note', '$20.00 payment note is registered');
 
-    # We make a -$30 payment (a NEGATIVE payment)
+    # We attempt to make a -$30 payment (a NEGATIVE payment)
     $data = '-30.00';
     $payment_note = '-$30.00 payment note';
-    $account->pay( { amount => $data, note => $payment_note } );
-
-    # There is now $310 in the account
-    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, 310, 'The account has $310 as expected' );
-    # Is the payment note well registered
-    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
-    $sth->execute($borrower->borrowernumber);
-    $note = $sth->fetchrow_array;
-    is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
+    throws_ok { $account->pay( { amount => $data, note => $payment_note } ) }
+    'Koha::Exceptions::Account::AmountNotPositive',
+      'Croaked on call to pay with negative amount';
 
     #We make a $150 payment ( > 1stLine )
     $data = '150.00';
     $payment_note = '$150.00 payment note';
-    $account->pay( { amount => $data, note => $payment_note } );
+    $id = $account->pay( { amount => $data, note => $payment_note } )->{payment_id};
 
-    # There is now $160 in the account
-    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, 160, 'The account has $160 as expected' );
+    # There is now $130 in the account
+    $outstanding_debt = $account->outstanding_debits->total_outstanding;
+    is($outstanding_debt, 130, 'The account has $130 outstanding debt as expected' );
+    $outstanding_credit = $account->outstanding_credits->total_outstanding;
+    is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
 
     # Is the payment note well registered
-    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
-    $sth->execute($borrower->borrowernumber);
-    $note = $sth->fetchrow_array;
-    is($note,'$150.00 payment note', '$150.00 payment note is registered');
+    $accountline = Koha::Account::Lines->find( $id );
+    is($accountline->note,'$150.00 payment note', '$150.00 payment note is registered');
 
     #We make a $200 payment ( > amountleft )
     $data = '200.00';
     $payment_note = '$200.00 payment note';
-    $account->pay( { amount => $data, note => $payment_note } );
+    $id = $account->pay( { amount => $data, note => $payment_note } )->{payment_id};
 
-    # There is now -$40 in the account
-    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
+    # There is now -$70 in the account
+    $outstanding_debt = $account->outstanding_debits->total_outstanding;
+    is($outstanding_debt, 0, 'The account has $0 outstanding debt as expected' );
+    $outstanding_credit = $account->outstanding_credits->total_outstanding;
+    is($outstanding_credit, -70, 'The account has -$70 outstanding credit as expected' );
 
     # Is the payment note well registered
-    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
-    $sth->execute($borrower->borrowernumber);
-    $note = $sth->fetchrow_array;
-    is($note,'$200.00 payment note', '$200.00 payment note is registered');
+    $accountline = Koha::Account::Lines->find( $id );
+    is($accountline->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 $payment = Koha::Account::Lines->find( $payment_id );
-    is( $payment->amount()+0, -42, "Payment paid the specified fine" );
+    $id = $account->pay( { lines => [$line3], amount => 42 } )->{payment_id};
+    $accountline = Koha::Account::Lines->find( $id );
+    is( $accountline->amount()+0, -42, "Payment paid the specified fine" );
     $line3 = Koha::Account::Lines->find( $line3->id );
     is( $line3->amountoutstanding+0, 0, "Specified fine is paid" );
-    is( $payment->branchcode, undef, 'branchcode passed, then undef' );
+    is( $accountline->branchcode, undef, 'branchcode passed, then undef' );
 };
 
 subtest "Koha::Account::pay particular line tests" => sub {
@@ -376,7 +314,7 @@ subtest "Koha::Account::pay writeoff tests" => sub {
             amount => 42,
             type   => 'WRITEOFF',
         }
-    );
+    )->{payment_id};
 
     $line->_result->discard_changes();
 
@@ -385,13 +323,13 @@ subtest "Koha::Account::pay writeoff tests" => sub {
     my $writeoff = Koha::Account::Lines->find( $id );
 
     is( $writeoff->credit_type_code, 'WRITEOFF', 'Type is correct for WRITEOFF' );
-    is( $writeoff->description, 'Writeoff', 'Description is correct' );
+    is( $writeoff->description, '', 'Description is correct' );
     is( $writeoff->amount+0, -42, 'Amount is correct' );
 };
 
 subtest "More Koha::Account::pay tests" => sub {
 
-    plan tests => 8;
+    plan tests => 12;
 
     # Create a borrower
     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
@@ -425,11 +363,24 @@ subtest "More Koha::Account::pay tests" => sub {
     my $account = Koha::Account->new( { patron_id => $borrowernumber } );
     my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
     # make the full payment
-    $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
+    my $payment = $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+0, -100, 'Offset amount is -100.00' );
-    is( $offset->type(), 'Payment', 'Offset type is Payment' );
+    my $offsets = Koha::Account::Offsets->search({ credit_id => $payment->{payment_id} });
+    is( $offsets->count, 2, 'Two offsets recorded');
+    my $offset = $offsets->next;
+    is( $offset->type(), 'CREATE', 'First offset type is CREATE' );
+    is( $offset->amount+0, 100, 'First offset amount is 100.00' );
+    $offset = $offsets->next;
+    is( $offset->type(), 'APPLY', 'Second offset type is APPLY' );
+    is( $offset->amount+0, -100, 'Second offset amount is -100.00' );
+    is( $offset->debit_id, $accountline->{accountlines_id}, 'Second offset is against the right accountline');
 
     my $stat = $schema->resultset('Statistic')->search({
         branch  => $branch,
@@ -444,13 +395,13 @@ subtest "More Koha::Account::pay tests" => sub {
         is( $stat->type, 'payment', "Correct statistic type" );
         is( $stat->branch, $branch, "Correct branch logged to statistics" );
         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
-        is( $stat->value+0, $amount, "Correct amount logged to statistics" );
+        is( $stat->value+0, -$amount, "Correct amount logged to statistics" );
     }
 };
 
 subtest "Even more Koha::Account::pay tests" => sub {
 
-    plan tests => 8;
+    plan tests => 12;
 
     # Create a borrower
     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
@@ -485,11 +436,24 @@ subtest "Even more Koha::Account::pay tests" => sub {
     my $account = Koha::Account->new( { patron_id => $borrowernumber } );
     my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
     # make the full payment
-    $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
+    my $payment = $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+0, -60, 'Offset amount is -60.00' );
-    is( $offset->type, 'Payment', 'Offset type is payment' );
+    my $offsets = Koha::Account::Offsets->search({ credit_id => $payment->{payment_id} });
+    is( $offsets->count, 2, 'Two offsets recorded');
+    my $offset = $offsets->next;
+    is( $offset->type(), 'CREATE', 'First offset type is CREATE' );
+    is( $offset->amount+0, 60, 'First offset amount is 60.00' );
+    $offset = $offsets->next;
+    is( $offset->type(), 'APPLY', 'Second offset type is APPLY' );
+    is( $offset->amount+0, -60, 'Second offset amount is -60.00' );
+    is( $offset->debit_id, $accountline->{accountlines_id}, 'Second offset is against the right accountline');
 
     my $stat = $schema->resultset('Statistic')->search({
         branch  => $branch,
@@ -504,7 +468,7 @@ subtest "Even more Koha::Account::pay tests" => sub {
         is( $stat->type, 'payment', "Correct statistic type" );
         is( $stat->branch, $branch, "Correct branch logged to statistics" );
         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
-        is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
+        is( $stat->value+0, -$partialamount, "Correct amount logged to statistics" );
     }
 };
 
@@ -587,22 +551,26 @@ 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_itemnumber5 = $builder->build_sample_item({ itype => $itype_replace_fee->{itemtype} })->itemnumber;
+    my $cli_bundle1     = $builder->build_sample_item({ itype => $itype_no_replace_no_fee->{itemtype} })->itemnumber;
+    $schema->resultset('ItemBundle')->create( { host => $cli_bundle1, item => $cli_itemnumber5 } );
 
     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 $cli_bundle_issue = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_bundle1 } })->{issue_id};
 
     my $lostfine;
     my $procfee;
 
     subtest "fee application tests" => sub {
-        plan tests => 44;
+        plan tests => 48;
 
         t::lib::Mocks::mock_preference('item-level_itypes', '1');
         t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
@@ -730,6 +698,16 @@ subtest "C4::Accounts::chargelostitem tests" => sub {
         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();
+
+        C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber5, 6.12, "Bundle");
+        $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber5, debit_type_code => 'LOST' });
+        $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber5, debit_type_code => 'PROCESSING' });
+        is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set (Bundle)");
+        is( $procfee->amount, "2.040000",  "Processing fee if processing fee (Bundle)");
+        is( $lostfine->issue_id, $cli_bundle_issue, "Lost fine issue id matched to bundle issue");
+        is( $procfee->issue_id, $cli_bundle_issue, "Processing fee issue id matched to bundle issue");
+        $lostfine->delete();
+        $procfee->delete();
     };
 
     subtest "basic fields tests" => sub {
@@ -791,7 +769,6 @@ 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;
@@ -821,7 +798,6 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     Koha::Account::Line->new(
         {
             borrowernumber    => $patron->borrowernumber,
-            date              => $today,
             description       => 'a Manual invoice fee',
             debit_type_code   => 'Copie',
             amountoutstanding => $manual,
@@ -969,7 +945,7 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     my $debit = Koha::Account::Line->new(
         {
             borrowernumber    => $patron->id,
-            date              => '1900-01-01',
+            date              => '1970-01-01 00:00:01',
             amountoutstanding => 0,
             interface         => 'commandline',
             debit_type_code   => 'LOST'
@@ -978,7 +954,7 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     my $credit = Koha::Account::Line->new(
         {
             borrowernumber    => $patron->id,
-            date              => '1900-01-01',
+            date              => '1970-01-01 00:00:01',
             amountoutstanding => -5,
             interface         => 'commandline',
             credit_type_code  => 'PAYMENT'
@@ -988,7 +964,7 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
         {
             credit_id => $credit->id,
             debit_id  => $debit->id,
-            type      => 'Payment',
+            type      => 'APPLY',
             amount    => 0
         }
     )->store();
@@ -1002,7 +978,7 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     $debit = Koha::Account::Line->new(
         {
             borrowernumber    => $patron->id,
-            date              => '1900-01-01',
+            date              => '1970-01-01 00:00:01',
             amountoutstanding => 5,
             interface         => 'commanline',
             debit_type_code   => 'LOST'
@@ -1011,7 +987,7 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     $credit = Koha::Account::Line->new(
         {
             borrowernumber    => $patron->id,
-            date              => '1900-01-01',
+            date              => '1970-01-01 00:00:01',
             amountoutstanding => 0,
             interface         => 'commandline',
             credit_type_code  => 'PAYMENT'
@@ -1021,7 +997,7 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
         {
             credit_id => $credit->id,
             debit_id  => $debit->id,
-            type      => 'Payment',
+            type      => 'APPLY',
             amount    => 0
         }
     )->store();
@@ -1036,7 +1012,7 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     $debit = Koha::Account::Line->new(
         {
             borrowernumber    => $patron->id,
-            date              => '1900-01-01',
+            date              => '1970-01-01 00:00:01',
             amountoutstanding => 0,
             interface         => 'commandline',
             debit_type_code   => 'LOST'
@@ -1045,7 +1021,7 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
     $credit = Koha::Account::Line->new(
         {
             borrowernumber    => $patron->id,
-            date              => '1900-01-01',
+            date              => '1970-01-01 00:00:01',
             amountoutstanding => 0,
             interface         => 'commandline',
             credit_type_code  => 'PAYMENT'
@@ -1055,7 +1031,7 @@ subtest "Koha::Account::non_issues_charges tests" => sub {
         {
             credit_id => $credit->id,
             debit_id  => $debit->id,
-            type      => 'Payment',
+            type      => 'APPLY',
             amount    => 0
         }
     )->store();
@@ -1111,7 +1087,7 @@ subtest "Koha::Account::Offset credit & debit tests" => sub {
             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 } );
@@ -1122,7 +1098,7 @@ subtest "Koha::Account::Offset credit & debit tests" => sub {
         {
             credit_id => undef,
             debit_id  => undef,
-            type      => 'Payment',
+            type      => 'CREATE',
             amount    => 0,
         }
     )->store();
@@ -1135,37 +1111,23 @@ subtest "Payment notice tests" => sub {
 
     plan tests => 8;
 
-    Koha::Account::Lines->delete();
-    Koha::Patrons->delete();
     Koha::Notice::Messages->delete();
-    # Create a borrower
-    my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
-    my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
-
-    my $borrower = Koha::Patron->new(
-        {
-            cardnumber   => 'chelseahall',
-            surname      => 'Hall',
-            firstname    => 'Chelsea',
-            email        => 'chelsea@example.com',
-            categorycode => $categorycode,
-            branchcode   => $branchcode,
-        }
-    )->store();
+    # Create a patron
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
 
     my $manager = $builder->build_object({ class => "Koha::Patrons" });
-    my $context = new Test::MockModule('C4::Context');
+    my $context = Test::MockModule->new('C4::Context');
     $context->mock( 'userenv', sub {
         return {
             number     => $manager->borrowernumber,
             branch     => $manager->branchcode,
         };
     });
-    my $account = Koha::Account->new({ patron_id => $borrower->id });
+    my $account = Koha::Account->new({ patron_id => $patron->borrowernumber });
 
     my $line = Koha::Account::Line->new(
         {
-            borrowernumber    => $borrower->borrowernumber,
+            borrowernumber    => $patron->borrowernumber,
             amountoutstanding => 27,
             interface         => 'commandline',
             debit_type_code   => 'LOST'
@@ -1177,15 +1139,15 @@ subtest "Payment notice tests" => sub {
     $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, library_id => $branchcode } );
+    $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' );
@@ -1196,7 +1158,7 @@ subtest "Payment notice tests" => sub {
     $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', library_id => $branchcode  } );
+    $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' );