@@ -, +, @@ to your logged in branch --- C4/Accounts.pm | 55 +++++++++++++++++++------------ C4/Circulation.pm | 83 ++++++++++++++++++++++++++++++++--------------- C4/Reserves.pm | 25 ++++++++++---- Koha/Account.pm | 6 ++-- t/db_dependent/Accounts.t | 51 +++++++++++++++++++++++++++-- 5 files changed, 163 insertions(+), 57 deletions(-) --- a/C4/Accounts.pm +++ a/C4/Accounts.pm @@ -27,6 +27,7 @@ use C4::Circulation qw(ReturnLostItem); use C4::Log qw(logaction); use Koha::Account; use Koha::Account::Lines; +use Koha::DateUtils qw(dt_from_string); use Data::Dumper qw(Dumper); @@ -136,15 +137,26 @@ sub chargelostitem{ unless ($existing_charge_hashref) { my $manager_id = 0; $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; + my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef; + # This item is on issue ... add replacement cost to the borrower's record and mark it returned # Note that we add this to the account even if there's no replacement price, allowing some other # process (or person) to update it, since we don't handle any defaults for replacement prices. my $accountno = getnextacctno($borrowernumber); - my $sth2=$dbh->prepare("INSERT INTO accountlines - (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber,manager_id) - VALUES (?,?,now(),?,?,'L',?,?,?)"); - $sth2->execute($borrowernumber,$accountno,$amount, - $description,$amount,$itemnumber,$manager_id); + Koha::Account::Line->new( + { + borrowernumber => $borrowernumber, + accountno => $accountno, + date => dt_from_string(), + amount => $amount, + description => $description, + accounttype => 'L', + amountoutstanding => $amount, + itemnumber => $itemnumber, + manager_id => $manager_id, + branchcode => $branchcode, + } + )->store(); if ( C4::Context->preference("FinesLog") ) { logaction("FINES", 'CREATE', $borrowernumber, Dumper({ @@ -208,21 +220,24 @@ sub manualinvoice { $notifyid = 1; } - if ( $itemnum ) { - $desc .= ' ' . $itemnum; - my $sth = $dbh->prepare( - 'INSERT INTO accountlines - (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id) - VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)'); - $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr; - } else { - my $sth=$dbh->prepare("INSERT INTO accountlines - (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id, note, manager_id) - VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)" - ); - $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type, - $amountleft, $notifyid, $note, $manager_id ); - } + my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef; + + Koha::Account::Line->new( + { + borrowernumber => $borrowernumber, + accountno => $accountno, + date => dt_from_string(), + amount => $amount, + description => $desc, + accounttype => $type, + amountoutstanding => $amountleft, + itemnumber => $itemnum, + notify_id => $notifyid, + note => $note, + manager_id => $manager_id, + branchcode => $branchcode, + } + )->store(); if ( C4::Context->preference("FinesLog") ) { logaction("FINES", 'CREATE',$borrowernumber,Dumper({ --- a/C4/Circulation.pm +++ a/C4/Circulation.pm @@ -50,6 +50,7 @@ use Koha::Patrons; use Koha::Patron::Debarments; use Koha::Database; use Koha::Libraries; +use Koha::Account::Lines; use Koha::Holds; use Koha::RefundLostItemFeeRule; use Koha::RefundLostItemFeeRules; @@ -2395,7 +2396,7 @@ sub _FixAccountForLostAndReturned { WHERE (accountlines_id = ?)"); $usth->execute($data->{'accountlines_id'}); # We might be adjusting an account for some OTHER borrowernumber now. Not the one we passed in. #check if any credit is left if so writeoff other accounts - my $nextaccntno = getnextacctno($data->{'borrowernumber'}); + my $nextaccntno = C4::Accounts::getnextacctno($data->{'borrowernumber'}); $amountleft *= -1 if ($amountleft < 0); if ($amountleft > 0) { my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?) @@ -2425,11 +2426,24 @@ sub _FixAccountForLostAndReturned { } } $amountleft *= -1 if ($amountleft > 0); + + my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef; + my $desc = "Item Returned " . $item_id; - $usth = $dbh->prepare("INSERT INTO accountlines - (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding) - VALUES (?,?,now(),?,?,'CR',?)"); - $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft); + + Koha::Account::Line->new( + { + borrowernumber => $data->{borrowernumber}, + accountno => $nextaccntno, + date => dt_from_string(), + amount => 0 - $amount, + description => $desc, + accounttype => 'CR', + amountoutstanding => $amountleft, + branchcode => $branchcode, + } + )->store(); + if ($borrowernumber) { # FIXME: same as query above. use 1 sth for both $usth = $dbh->prepare("INSERT INTO accountoffsets @@ -2858,18 +2872,26 @@ sub AddRenewal { # Charge a new rental fee, if applicable? my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber ); if ( $charge > 0 ) { - my $accountno = getnextacctno( $borrowernumber ); + my $accountno = C4::Accounts::getnextacctno( $borrowernumber ); my $manager_id = 0; $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; - $sth = $dbh->prepare( - "INSERT INTO accountlines - (date, borrowernumber, accountno, amount, manager_id, - description,accounttype, amountoutstanding, itemnumber) - VALUES (now(),?,?,?,?,?,?,?,?)" - ); - $sth->execute( $borrowernumber, $accountno, $charge, $manager_id, - "Renewal of Rental Item " . $biblio->title . " $item->{'barcode'}", - 'Rent', $charge, $itemnumber ); + my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef; + Koha::Account::Line->new( + { + date => dt_from_string(), + borrowernumber => $borrowernumber, + accountno => $accountno, + amount => $charge, + manager_id => $manager_id, + accounttype => 'Rent', + amountoutstanding => $charge, + itemnumber => $itemnumber, + branchcode => $branchcode, + description => 'Renewal of Rental Item ' + . $biblio->title + . " $item->{'barcode'}", + } + )->store(); } # Send a renewal slip according to checkout alert preferencei @@ -3194,19 +3216,28 @@ sub _get_discount_from_rule { sub AddIssuingCharge { my ( $itemnumber, $borrowernumber, $charge ) = @_; - my $dbh = C4::Context->dbh; - my $nextaccntno = getnextacctno( $borrowernumber ); + + my $nextaccntno = getnextacctno($borrowernumber); + my $manager_id = 0; $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; - my $query =" - INSERT INTO accountlines - (borrowernumber, itemnumber, accountno, - date, amount, description, accounttype, - amountoutstanding, manager_id) - VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?,?) - "; - my $sth = $dbh->prepare($query); - $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge, $manager_id ); + + my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef; + + Koha::Account::Line->new( + { + date => dt_from_string(), + borrowernumber => $borrowernumber, + accountno => $nextaccntno, + amount => $charge, + manager_id => $manager_id, + accounttype => 'Rent', + amountoutstanding => $charge, + itemnumber => $itemnumber, + branchcode => $branchcode, + description => 'Rental', + } + )->store(); } =head2 GetTransfers --- a/C4/Reserves.pm +++ a/C4/Reserves.pm @@ -48,6 +48,7 @@ use Koha::IssuingRules; use Koha::Items; use Koha::ItemTypes; use Koha::Patrons; +use Koha::Account::Lines; use List::MoreUtils qw( firstidx any ); use Carp; @@ -559,13 +560,23 @@ sub GetOtherReserves { sub ChargeReserveFee { my ( $borrowernumber, $fee, $title ) = @_; - return if !$fee || $fee==0; # the last test is needed to include 0.00 - my $accquery = qq{ -INSERT INTO accountlines ( borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding ) VALUES (?, ?, NOW(), ?, ?, 'Res', ?) - }; - my $dbh = C4::Context->dbh; - my $nextacctno = &getnextacctno( $borrowernumber ); - $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) ); + + return if !$fee || $fee == 0; # the last test is needed to include 0.00 + + my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef; + my $nextacctno = C4::Accounts::getnextacctno($borrowernumber); + + Koha::Account::Line->new( + { + borrowernumber => $borrowernumber, + accountno => $nextacctno, + date => dt_from_string(), + amount => $fee, + description => "Reserve Charge - $title", + accounttype => 'Res', + amountoutstanding => $fee, + } + )->store(); } =head2 GetReserveFee --- a/Koha/Account.pm +++ a/Koha/Account.pm @@ -68,6 +68,9 @@ sub pay { my $library_id = $params->{library_id}; my $lines = $params->{lines}; my $type = $params->{type} || 'payment'; + my $branchcode = $params->{branchcode}; + + $library_id ||= C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef; my $userenv = C4::Context->userenv; @@ -191,12 +194,11 @@ sub pay { accounttype => $account_type, amountoutstanding => 0 - $balance_remaining, manager_id => $manager_id, + branchcode => $library_id, note => $note, } )->store(); - $library_id ||= $userenv ? $userenv->{'branch'} : undef; - UpdateStats( { branch => $library_id, --- a/t/db_dependent/Accounts.t +++ a/t/db_dependent/Accounts.t @@ -18,7 +18,7 @@ use Modern::Perl; -use Test::More tests => 22; +use Test::More tests => 35; use Test::MockModule; use Test::Warn; @@ -70,6 +70,52 @@ $context->mock( 'userenv', sub { branch => $branchcode, }; }); +my $userenv_branchcode = $branchcode; + +# Test chargelostitem +my $item = $builder->build( { source => 'Item' } ); +my $patron = $builder->build( { source => 'Borrower' } ); +my $amount = '5.000000'; +my $description = "Test fee!"; +chargelostitem( $patron->{borrowernumber}, $item->{itemnumber}, $amount, $description ); +my ($accountline) = Koha::Account::Lines->search( + { + borrowernumber => $patron->{borrowernumber} + } +); +is( $accountline->amount, $amount, 'Accountline amount set correctly for chargelostitem' ); +is( $accountline->description, $description, 'Accountline description set correctly for chargelostitem' ); +is( $accountline->branchcode, $branchcode, 'Accountline branchcode set correctly for chargelostitem' ); +$dbh->do(q|DELETE FROM accountlines|); + +# Test manualinvoice, reuse some of the vars from testing chargelostitem +my $type = 'L'; +my $note = 'Test note!'; +manualinvoice( $patron->{borrowernumber}, $item->{itemnumber}, $description, $type, $amount, $note ); +($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' ); +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' ); + +# Test _FixAccountForLostAndReturned, use the accountline from the manualinvoice to test +C4::Circulation::_FixAccountForLostAndReturned( $item->{itemnumber} ); +my ( $accountline_fee, $accountline_payment ) = Koha::Account::Lines->search( + { + borrowernumber => $patron->{borrowernumber} + } +); +is( $accountline_fee->accounttype, 'LR', 'Lost item fee account type updated to LR' ); +is( $accountline_fee->amountoutstanding, '0.000000', 'Lost item fee amount outstanding updated to 0' ); +is( $accountline_payment->accounttype, 'CR', 'Lost item fee account type is CR' ); +is( $accountline_payment->amount, "-$amount", 'Lost item refund amount is correct' ); +is( $accountline_payment->branchcode, $branchcode, 'Lost item refund branchcode is set correctly' ); +$dbh->do(q|DELETE FROM accountlines|); # Testing purge_zero_balance_fees @@ -135,7 +181,7 @@ $dbh->do(q|DELETE FROM accountlines|); subtest "Koha::Account::pay tests" => sub { - plan tests => 12; + plan tests => 13; # Create a borrower my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; @@ -255,6 +301,7 @@ subtest "Koha::Account::pay tests" => sub { is( $payment->amount(), '-42.000000', "Payment paid the specified fine" ); $line3 = Koha::Account::Lines->find( $line3->id ); is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" ); + is( $payment->branchcode, $userenv_branchcode, 'Branchcode set correctly' ); }; subtest "Koha::Account::pay particular line tests" => sub { --