Bug 21738: check items count in C4:ILSDI::HoldTitle
[koha.git] / C4 / Circulation.pm
index 23d0b5f..fa08210 100644 (file)
@@ -52,13 +52,15 @@ 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;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
+use Koha::Config::SysPrefs;
 use Carp;
-use List::MoreUtils qw( uniq );
+use List::MoreUtils qw( uniq any );
 use Scalar::Util qw( looks_like_number );
 use Date::Calc qw(
   Today
@@ -1354,17 +1356,27 @@ sub AddIssue {
             }
             $datedue->truncate( to => 'minute' );
 
-            $issue = Koha::Database->new()->schema()->resultset('Issue')->update_or_create(
-                {
-                    borrowernumber => $borrower->{'borrowernumber'},
-                    itemnumber     => $item->{'itemnumber'},
-                    issuedate      => $issuedate->strftime('%Y-%m-%d %H:%M:%S'),
-                    date_due       => $datedue->strftime('%Y-%m-%d %H:%M:%S'),
-                    branchcode     => C4::Context->userenv->{'branch'},
-                    onsite_checkout => $onsite_checkout,
-                    auto_renew      => $auto_renew ? 1 : 0
-                }
-              );
+            my $issue_attributes = {
+                borrowernumber  => $borrower->{'borrowernumber'},
+                issuedate       => $issuedate->strftime('%Y-%m-%d %H:%M:%S'),
+                date_due        => $datedue->strftime('%Y-%m-%d %H:%M:%S'),
+                branchcode      => C4::Context->userenv->{'branch'},
+                onsite_checkout => $onsite_checkout,
+                auto_renew      => $auto_renew ? 1 : 0,
+            };
+
+            $issue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
+            if ($issue) {
+                $issue->set($issue_attributes)->store;
+            }
+            else {
+                $issue = Koha::Checkout->new(
+                    {
+                        itemnumber => $item->{itemnumber},
+                        %$issue_attributes,
+                    }
+                )->store;
+            }
 
             if ( C4::Context->preference('ReturnToShelvingCart') ) {
                 # ReturnToShelvingCart is on, anything issued should be taken off the cart.
@@ -1894,13 +1906,7 @@ sub AddReturn {
         my $is_overdue;
         die "The item is not issed and cannot be returned" unless $issue; # Just in case...
         $patron or warn "AddReturn without current borrower";
-               my $circControlBranch;
         if ($dropbox) {
-            # define circControlBranch only if dropbox mode is set
-            # don't allow dropbox mode to create an invalid entry in issues (issuedate > today)
-            # FIXME: check issuedate > returndate, factoring in holidays
-
-            $circControlBranch = _GetCircControlBranch($item,$patron_unblessed);
             $is_overdue = $issue->is_overdue( $dropboxdate );
         } else {
             $is_overdue = $issue->is_overdue;
@@ -1908,8 +1914,14 @@ sub AddReturn {
 
         if ($patron) {
             eval {
-                MarkIssueReturned( $borrowernumber, $item->{'itemnumber'},
-                    $circControlBranch, $return_date, $patron->privacy );
+                if ( $dropbox ) {
+                    MarkIssueReturned( $borrowernumber, $item->{'itemnumber'},
+                        $dropboxdate, $patron->privacy );
+                }
+                else {
+                    MarkIssueReturned( $borrowernumber, $item->{'itemnumber'},
+                        $return_date, $patron->privacy );
+                }
             };
             unless ( $@ ) {
                 if ( ( C4::Context->preference('CalculateFinesOnReturn') && $is_overdue ) || $return_date ) {
@@ -2086,30 +2098,26 @@ sub AddReturn {
 
 =head2 MarkIssueReturned
 
-  MarkIssueReturned($borrowernumber, $itemnumber, $dropbox_branch, $returndate, $privacy);
+  MarkIssueReturned($borrowernumber, $itemnumber, $returndate, $privacy);
 
 Unconditionally marks an issue as being returned by
 moving the C<issues> row to C<old_issues> and
-setting C<returndate> to the current date, or
-the last non-holiday date of the branccode specified in
-C<dropbox_branch> .  Assumes you've already checked that 
-it's safe to do this, i.e. last non-holiday > issuedate.
+setting C<returndate> to the current date.
 
 if C<$returndate> is specified (in iso format), it is used as the date
-of the return. It is ignored when a dropbox_branch is passed in.
+of the return.
 
 C<$privacy> contains the privacy parameter. If the patron has set privacy to 2,
 the old_issue is immediately anonymised
 
 Ideally, this function would be internal to C<C4::Circulation>,
-not exported, but it is currently needed by one 
-routine in C<C4::Accounts>.
+not exported, but it is currently used in misc/cronjobs/longoverdue.pl
+and offline_circ/process_koc.pl.
 
 =cut
 
 sub MarkIssueReturned {
-    my ( $borrowernumber, $itemnumber, $dropbox_branch, $returndate, $privacy ) = @_;
-
+    my ( $borrowernumber, $itemnumber, $returndate, $privacy ) = @_;
 
     # Retrieve the issue
     my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } ) or return;
@@ -2125,41 +2133,26 @@ sub MarkIssueReturned {
         die "Fatal error: the patron ($borrowernumber) has requested their circulation history be anonymized on check-in, but the AnonymousPatron system preference is empty or not set correctly."
             unless Koha::Patrons->find( $anonymouspatron );
     }
-    my $database = Koha::Database->new();
-    my $schema   = $database->schema;
-    my $dbh   = C4::Context->dbh;
 
-    my $query = 'UPDATE issues SET returndate=';
-    my @bind;
-    if ($dropbox_branch) {
-        my $calendar = Koha::Calendar->new( branchcode => $dropbox_branch );
-        my $dropboxdate = $calendar->addDate( DateTime->now( time_zone => C4::Context->tz), -1 );
-        $query .= ' ? ';
-        push @bind, $dropboxdate->strftime('%Y-%m-%d %H:%M');
-    } elsif ($returndate) {
-        $query .= ' ? ';
-        push @bind, $returndate;
-    } else {
-        $query .= ' now() ';
-    }
-    $query .= ' WHERE issue_id = ?';
-    push @bind, $issue_id;
+    my $schema = Koha::Database->schema;
 
     # FIXME Improve the return value and handle it from callers
     $schema->txn_do(sub {
 
-        # Update the returndate
-        $dbh->do( $query, undef, @bind );
-
-        # We just updated the returndate, so we need to refetch $issue
-        $issue->discard_changes;
+        # Update the returndate value
+        if ( $returndate ) {
+            $issue->returndate( $returndate )->store->discard_changes; # update and refetch
+        }
+        else {
+            $issue->returndate( \'NOW()' )->store->discard_changes; # update and refetch
+        }
 
         # Create the old_issues entry
         my $old_checkout = Koha::Old::Checkout->new($issue->unblessed)->store;
 
         # anonymise patron checkout immediately if $privacy set to 2 and AnonymousPatron is set to a valid borrowernumber
         if ( $privacy == 2) {
-            $dbh->do(q|UPDATE old_issues SET borrowernumber=? WHERE issue_id = ?|, undef, $anonymouspatron, $old_checkout->issue_id);
+            $old_checkout->borrowernumber($anonymouspatron)->store;
         }
 
         # And finally delete the issue
@@ -2298,22 +2291,20 @@ C<$itm> itemnumber
 C<$exemptfine> BOOL -- remove overdue charge associated with this issue. 
 C<$dropboxmode> BOOL -- remove lastincrement on overdue charge associated with this issue.
 
-Internal function, called only by AddReturn
+Internal function
 
 =cut
 
 sub _FixOverduesOnReturn {
-    my ($borrowernumber, $item);
-    unless ($borrowernumber = shift) {
+    my ($borrowernumber, $item, $exemptfine, $dropbox ) = @_;
+    unless( $borrowernumber ) {
         warn "_FixOverduesOnReturn() not supplied valid borrowernumber";
         return;
     }
-    unless ($item = shift) {
+    unless( $item ) {
         warn "_FixOverduesOnReturn() not supplied valid itemnumber";
         return;
     }
-    my ($exemptfine, $dropbox) = @_;
-    my $dbh = C4::Context->dbh;
 
     # check for overdue fine
     my $accountline = Koha::Account::Lines->search(
@@ -2328,7 +2319,6 @@ sub _FixOverduesOnReturn {
     )->next();
     return 0 unless $accountline;    # no warning, there's just nothing to fix
 
-    my $uquery;
     if ($exemptfine) {
         my $amountoutstanding = $accountline->amountoutstanding;
 
@@ -2341,7 +2331,7 @@ sub _FixOverduesOnReturn {
                 type => 'Forgiven',
                 amount => $amountoutstanding * -1,
             }
-        );
+        )->store();
 
         if (C4::Context->preference("FinesLog")) {
             &logaction("FINES", 'MODIFY',$borrowernumber,"Overdue forgiven: item $item");
@@ -2356,7 +2346,7 @@ sub _FixOverduesOnReturn {
                 type => 'Dropbox',
                 amount => $accountline->lastincrement * -1,
             }
-        );
+        )->store();
 
         if ( C4::Context->preference("FinesLog") ) {
             &logaction( "FINES", 'MODIFY', $borrowernumber,
@@ -2406,12 +2396,14 @@ sub _FixAccountForLostAndReturned {
     );
 
     return unless $accountlines->count > 0;
-    my $accountline = $accountlines->next;
+    my $accountline     = $accountlines->next;
+    my $total_to_refund = 0;
+    my $account = Koha::Patrons->find( $accountline->borrowernumber )->account;
 
     # Use cases
     if ( $accountline->amount > $accountline->amountoutstanding ) {
         # some amount has been cancelled. collect the offsets that are not writeoffs
-        # this works because the only way to subtract from a debt is
+        # this works because the only way to subtract from this kind of a debt is
         # using the UI buttons 'Pay' and 'Write off'
         my $credits_offsets = Koha::Account::Offsets->search({
             debit_id  => $accountline->id,
@@ -2420,30 +2412,36 @@ sub _FixAccountForLostAndReturned {
             amount    => { '<'  => 0 } # credits are negative on the DB
         });
 
-        my $total_to_refund = ( $credits_offsets->count > 0 )
-                                ? $credits_offsets->total * -1 # credits are negative on the DB
-                                : 0;
+        $total_to_refund = ( $credits_offsets->count > 0 )
+                            ? $credits_offsets->total * -1 # credits are negative on the DB
+                            : 0;
+    }
 
-        if ( $total_to_refund > 0 ) {
-            my $account = Koha::Patrons->find( $accountline->borrowernumber )->account;
-            $credit = $account->add_credit(
-                {
-                    amount      => $total_to_refund,
-                    description => 'Item Returned ' . $item_id,
-                    type        => 'lost_item_return'
-                }
-            );
-        }
+    my $credit_total = $accountline->amountoutstanding + $total_to_refund;
+
+    if ( $credit_total > 0 ) {
+        my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
+        $credit = $account->add_credit(
+            {   amount      => $credit_total,
+                description => 'Item Returned ' . $item_id,
+                type        => 'lost_item_return',
+                library_id  => $branchcode
+            }
+        );
 
-        ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } );
+        # TODO: ->apply should just accept the accountline
+        $credit->apply( { debits => $accountlines->reset } );
     }
-    # else {
-        # $accountline->amount == $accountline->amountoutstanding
-    #}
 
-    $accountline->accounttype('LR');
-    $accountline->amountoutstanding(0);
-    $accountline->store();
+    # Manually set the accounttype
+    $accountline->discard_changes->accounttype('LR');
+    $accountline->store;
+
+    ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } );
+
+    if ( defined $account and C4::Context->preference('AccountAutoReconcile') ) {
+        $account->reconcile_balance;
+    }
 
     return ($credit) ? $credit->id : undef;
 }
@@ -2617,6 +2615,8 @@ sub CanBookBeRenewed {
     my $item      = GetItem($itemnumber)      or return ( 0, 'no_item' );
     my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } ) or return ( 0, 'no_checkout' );
     return ( 0, 'onsite_checkout' ) if $issue->onsite_checkout;
+    return ( 0, 'item_denied_renewal') if _item_denied_renewal({ item => $item });
+
 
     $borrowernumber ||= $issue->borrowernumber;
     my $patron = Koha::Patrons->find( $borrowernumber )
@@ -2876,15 +2876,23 @@ sub AddRenewal {
         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
@@ -3218,6 +3226,8 @@ sub AddIssuingCharge {
     my $manager_id  = 0;
     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
 
+    my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
+
     my $accountline = Koha::Account::Line->new(
         {
             borrowernumber    => $checkout->borrowernumber,
@@ -3227,6 +3237,7 @@ sub AddIssuingCharge {
             amount            => $charge,
             amountoutstanding => $charge,
             manager_id        => $manager_id,
+            branchcode        => $branchcode,
             description       => 'Rental',
             accounttype       => 'Rent',
             date              => \'NOW()',
@@ -3689,18 +3700,23 @@ sub LostItem{
     if ( my $borrowernumber = $issues->{borrowernumber} ){
         my $patron = Koha::Patrons->find( $borrowernumber );
 
-        if (C4::Context->preference('WhenLostForgiveFine')){
-            my $fix = _FixOverduesOnReturn($borrowernumber, $itemnumber, 1, 0); # 1, 0 = exemptfine, no-dropbox
-            defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $itemnumber...) failed!";  # zero is OK, check defined
-        }
+        my $fix = _FixOverduesOnReturn($borrowernumber, $itemnumber, C4::Context->preference('WhenLostForgiveFine'), 0); # 1, 0 = exemptfine, no-dropbox
+        defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $itemnumber...) failed!";  # zero is OK, check defined
+
         if (C4::Context->preference('WhenLostChargeReplacementFee')){
-            C4::Accounts::chargelostitem($borrowernumber, $itemnumber, $issues->{'replacementprice'}, "Lost Item $issues->{'title'} $issues->{'barcode'}");
+            C4::Accounts::chargelostitem($borrowernumber, $itemnumber, $issues->{'replacementprice'}, "Lost Item $issues->{'title'} $issues->{'barcode'} $issues->{'itemcallnumber'}");
             #FIXME : Should probably have a way to distinguish this from an item that really was returned.
             #warn " $issues->{'borrowernumber'}  /  $itemnumber ";
         }
 
-        MarkIssueReturned($borrowernumber,$itemnumber,undef,undef,$patron->privacy) if $mark_returned;
+        MarkIssueReturned($borrowernumber,$itemnumber,undef,$patron->privacy) if $mark_returned;
     }
+
+    #When item is marked lost automatically cancel its outstanding transfers and set items holdingbranch to the transfer source branch (frombranch)
+    if (my ( $datesent,$frombranch,$tobranch ) = GetTransfers($itemnumber)) {
+        ModItem({holdingbranch => $frombranch}, undef, $itemnumber);
+    }
+    my $transferdeleted = DeleteTransfer($itemnumber);
 }
 
 sub GetOfflineOperations {
@@ -3764,7 +3780,6 @@ sub ProcessOfflineReturn {
             MarkIssueReturned(
                 $issue->{borrowernumber},
                 $itemnumber,
-                undef,
                 $operation->{timestamp},
             );
             ModItem(
@@ -3799,7 +3814,6 @@ sub ProcessOfflineIssue {
             MarkIssueReturned(
                 $issue->{borrowernumber},
                 $itemnumber,
-                undef,
                 $operation->{timestamp},
             );
         }
@@ -3820,15 +3834,13 @@ sub ProcessOfflineIssue {
 sub ProcessOfflinePayment {
     my $operation = shift;
 
-    my $patron = Koha::Patrons->find( { cardnumber => $operation->{cardnumber} });
-    my $amount = $operation->{amount};
+    my $patron = Koha::Patrons->find({ cardnumber => $operation->{cardnumber} });
 
-    Koha::Account->new( { patron_id => $patron->id } )->pay( { amount => $amount } );
+    $patron->account->pay({ amount => $operation->{amount}, library_id => $operation->{branchcode} });
 
-    return "Success."
+    return "Success.";
 }
 
-
 =head2 TransferSlip
 
   TransferSlip($user_branch, $itemnumber, $barcode, $to_branch)
@@ -4089,11 +4101,9 @@ sub _CalculateAndUpdateFine {
 
     my $date_returned = $return_date ? dt_from_string($return_date) : dt_from_string();
 
-    my ( $amount, $type, $unitcounttotal ) =
+    my ( $amount, $unitcounttotal, $unitcount  ) =
       C4::Overdues::CalcFine( $item, $borrower->{categorycode}, $control_branchcode, $datedue, $date_returned );
 
-    $type ||= q{};
-
     if ( C4::Context->preference('finesMode') eq 'production' ) {
         if ( $amount > 0 ) {
             C4::Overdues::UpdateFine({
@@ -4101,7 +4111,6 @@ sub _CalculateAndUpdateFine {
                 itemnumber     => $issue->itemnumber,
                 borrowernumber => $issue->borrowernumber,
                 amount         => $amount,
-                type           => $type,
                 due            => output_pref($datedue),
             });
         }
@@ -4115,13 +4124,36 @@ sub _CalculateAndUpdateFine {
                 itemnumber     => $issue->itemnumber,
                 borrowernumber => $issue->borrowernumber,
                 amount         => 0,
-                type           => $type,
                 due            => output_pref($datedue),
             });
         }
     }
 }
 
+sub _item_denied_renewal {
+    my ($params) = @_;
+
+    my $item = $params->{item};
+    return unless $item;
+
+    my $denyingrules = Koha::Config::SysPrefs->find('ItemsDeniedRenewal')->get_yaml_pref_hash();
+    return unless $denyingrules;
+    foreach my $field (keys %$denyingrules) {
+        my $val = $item->{$field};
+        if( !defined $val) {
+            if ( any { !defined $_ }  @{$denyingrules->{$field}} ){
+                return 1;
+            }
+        } elsif (any { defined($_) && $val eq $_ } @{$denyingrules->{$field}}) {
+           # If the results matches the values in the syspref
+           # We return true if match found
+            return 1;
+        }
+    }
+    return 0;
+}
+
+
 1;
 
 __END__