Bug 24329: Add a test
[srvgit] / C4 / Accounts.pm
index 39b2bda..92365ea 100644 (file)
@@ -18,8 +18,7 @@ package C4::Accounts;
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 
-use strict;
-#use warnings; FIXME - Bug 2505
+use Modern::Perl;
 use C4::Context;
 use C4::Stats;
 use C4::Members;
@@ -71,7 +70,7 @@ FIXME : if no replacement price, borrower just doesn't get charged?
 
 =cut
 
-sub chargelostitem{
+sub chargelostitem {
     my $dbh = C4::Context->dbh();
     my ($borrowernumber, $itemnumber, $amount, $description) = @_;
     my $itype = Koha::ItemTypes->find({ itemtype => Koha::Items->find($itemnumber)->effective_itemtype() });
@@ -83,22 +82,21 @@ sub chargelostitem{
     if ($usedefaultreplacementcost && $amount == 0 && $defaultreplacecost){
         $replacementprice = $defaultreplacecost;
     }
+    my $checkout = Koha::Checkouts->find({ itemnumber => $itemnumber });
+    my $issue_id = $checkout ? $checkout->issue_id : undef;
 
     my $account = Koha::Account->new({ patron_id => $borrowernumber });
-    # first make sure the borrower hasn't already been charged for this item
-    # FIXME this should be more exact
-    #       there is no reason a user can't lose an item, find and return it, and lost it again
+    # first make sure the borrower hasn't already been charged for this item (for this issuance)
     my $existing_charges = $account->lines->search(
         {
-            itemnumber     => $itemnumber,
-            accounttype    => 'L',
+            itemnumber      => $itemnumber,
+            debit_type_code => 'LOST',
+            issue_id        => $issue_id
         }
     )->count();
 
     # OK, they haven't
     unless ($existing_charges) {
-        my $checkout = Koha::Checkouts->find({ itemnumber => $itemnumber });
-        my $issue_id = $checkout ? $checkout->issue_id : undef;
         #add processing fee
         if ($processfee && $processfee > 0){
             my $accountline = $account->add_debit(
@@ -107,8 +105,9 @@ sub chargelostitem{
                     description => $description,
                     note        => $processingfeenote,
                     user_id     => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                    interface   => C4::Context->interface,
                     library_id  => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
-                    type        => 'processing',
+                    type        => 'PROCESSING',
                     item_id     => $itemnumber,
                     issue_id    => $issue_id,
                 }
@@ -122,8 +121,9 @@ sub chargelostitem{
                     description => $description,
                     note        => undef,
                     user_id     => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                    interface   => C4::Context->interface,
                     library_id  => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
-                    type        => 'lost_item',
+                    type        => 'LOST',
                     item_id     => $itemnumber,
                     issue_id    => $issue_id,
                 }
@@ -153,17 +153,33 @@ sub manualinvoice {
 
     my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
 
+    my $issue_id;
+    if ( $type eq 'LOST' && $itemnum ) {
+        my $checkouts = Koha::Checkouts->search(
+            { itemnumber => $itemnum, borrowernumber => $borrowernumber } );
+        my $checkout =
+            $checkouts->count
+          ? $checkouts->next
+          : Koha::Old::Checkouts->search(
+            { itemnumber => $itemnum, borrowernumber => $borrowernumber },
+            { order_by   => { -desc => 'returndate' }, rows => 1 }
+        )->next;
+        $issue_id = $checkout ? $checkout->issue_id : undef;
+    }
+
     my $accountline = Koha::Account::Line->new(
         {
             borrowernumber    => $borrowernumber,
             date              => \'NOW()',
             amount            => $amount,
             description       => $desc,
-            accounttype       => $type,
+            debit_type_code   => $type,
             amountoutstanding => $amountleft,
             itemnumber        => $itemnum || undef,
+            issue_id          => $issue_id,
             note              => $note,
             manager_id        => $manager_id,
+            interface         => C4::Context->interface,
             branchcode        => $branchcode,
         }
     )->store();
@@ -182,7 +198,7 @@ sub manualinvoice {
             borrowernumber    => $borrowernumber,
             amount            => $amount,
             description       => $desc,
-            accounttype       => $type,
+            debit_type_code   => $type,
             amountoutstanding => $amountleft,
             note              => $note,
             itemnumber        => $itemnum,