Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / svc / checkouts
index dc559a2..b8e84f4 100755 (executable)
@@ -4,39 +4,42 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
 use CGI;
 use JSON qw(to_json);
 
-use C4::Auth qw(check_cookie_auth haspermission get_session);
-use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
+use C4::Auth qw(check_cookie_auth haspermission);
+use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount );
 use C4::Overdues qw(GetFine);
 use C4::Context;
 
 use Koha::AuthorisedValues;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Items;
 use Koha::ItemTypes;
 
-my $input = new CGI;
+my $input = CGI->new;
 
-my ( $auth_status, $sessionID ) =
-  check_cookie_auth( $input->cookie('CGISESSID'));
+my ( $auth_status, $session ) = check_cookie_auth( $input->cookie('CGISESSID'));
+if( $auth_status ne 'ok' ) {
+    print CGI::header( '-status' => '401' );
+    exit 0;
+}
 
-my $session   = get_session($sessionID);
 my $userid   = $session->param('id');
 
 unless (haspermission($userid, { circulate => 'circulate_remaining_permissions' })
@@ -85,6 +88,7 @@ my $sql = '
         items.itemnotes,
         items.itemnotes_nonpublic,
         items.itemcallnumber,
+        items.copynumber,
         items.replacementprice,
 
         issues.branchcode,
@@ -104,6 +108,7 @@ my $sql = '
         items.damaged,
         items.location,
         items.enumchron,
+        items.materials,
 
         DATEDIFF( issues.issuedate, CURRENT_DATE() ) AS not_issued_today,
 
@@ -139,6 +144,9 @@ $sth->execute(@parameters);
 
 my $item_level_itypes = C4::Context->preference('item-level_itypes');
 my $claims_returned_lost_value = C4::Context->preference('ClaimReturnedLostValue');
+my $confirm_parts_required = C4::Context->preference("CircConfirmItemParts");
+
+my $itemtypes = { map { $_->{itemtype} => $_->{translated_description} } @{ Koha::ItemTypes->search_with_localization->unblessed } };
 
 my @checkouts_today;
 my @checkouts_previous;
@@ -146,47 +154,88 @@ while ( my $c = $sth->fetchrow_hashref() ) {
     my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
     my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
 
-    my ( $can_renew, $can_renew_error ) =
+    my ( $can_renew, $can_renew_error, $info ) =
       CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
     my $can_renew_date =
       $can_renew_error && $can_renew_error eq 'too_soon'
       ? output_pref(
         {
-            dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
+            dt => $info->{soonest_renew_date},
             as_due_date => 1
         }
       )
       : undef;
 
-    my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
+    my (
+        $renewals_count,
+        $renewals_allowed,
+        $renewals_remaining,
+        $unseen_count,
+        $unseen_allowed,
+        $unseen_remaining
+    ) =
       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
 
-    my $type_for_stat = Koha::ItemTypes->find( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
-    my $itemtype = Koha::ItemTypes->find( $c->{itype} );
-    my $recordtype = Koha::ItemTypes->find( $c->{itemtype} );
+    my ( $itemtype, $recordtype, $type_for_stat );
+    $itemtype      = $itemtypes->{ $c->{itype} }    if $c->{itype};
+    $recordtype    = $itemtypes->{ $c->{itemtype} } if $c->{itemtype};
+    $type_for_stat = $item_level_itypes ? $itemtype : $recordtype;
+
     my $location;
     if ( $c->{location} ) {
-        my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $c->{location} });
-        $location = $av->count ? $av->next->lib : '';
+        my $av = Koha::AuthorisedValues->get_description_by_koha_field(
+            { kohafield => 'items.location', authorised_value => $c->{location} } );
+        $location = $av->{lib} ? $av->{lib} : '';
     }
     my $collection;
     if ( $c->{collection} ) {
-        my $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $c->{collection} });
-        $collection = $av->count ? $av->next->lib : '';
+        my $av = Koha::AuthorisedValues->get_description_by_koha_field(
+            { kohafield => 'items.ccode', authorised_value => $c->{collection} } );
+        $collection = $av->{lib} ? $av->{lib} : '';
     }
     my $lost;
     my $claims_returned;
     if ( $c->{itemlost} ) {
-        my $av = Koha::AuthorisedValues->search({ category => 'LOST', authorised_value => $c->{itemlost} });
-        $lost = $av->count ? $av->next->lib : '';
+        my $av = Koha::AuthorisedValues->get_description_by_koha_field(
+            { kohafield => 'items.itemlost', authorised_value => $c->{itemlost} } );
+        $lost            = $av->{lib} ? $av->{lib} : '';
         $claims_returned = $c->{itemlost} eq $claims_returned_lost_value;
     }
     my $damaged;
     if ( $c->{damaged} ) {
-        my $av = Koha::AuthorisedValues->search({ category => 'DAMAGED', authorised_value => $c->{damaged} });
-        $damaged = $av->count ? $av->next->lib : '';
+        my $av = Koha::AuthorisedValues->get_description_by_koha_field(
+            { kohafield => 'items.damaged', authorised_value => $c->{damaged} } );
+        $damaged = $av->{lib} ? $av->{lib} : '';
+    }
+    my $materials;
+    if ( $c->{materials} && $confirm_parts_required ) {
+        my $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => '', kohafield =>'items.materials', authorised_value => $c->{materials} });
+        $materials = $descriptions->{lib} // $c->{materials};
     }
     my @subtitles = split(/ \| /, $c->{'subtitle'} // '' );
+
+    my $item = Koha::Items->find( $c->{itemnumber} );
+    my $recalled = 0;
+    if ( C4::Context->preference('UseRecalls') ) {
+        my $recall = undef;
+        $recall = $item->check_recalls if $item->can_be_waiting_recall;
+        if ( defined $recall ) {
+            if ( $recall->item_level ) {
+                if ( $recall->item_id == $c->{itemnumber} ) {
+                    # item-level recall on this item
+                    $recalled = 1;
+                } else {
+                    $recalled = 0;
+                }
+            } else {
+                # biblio-level recall, but don't want to mark recalled if the recall has been allocated a different item
+                if ( !$recall->waiting ) {
+                    $recalled = 1;
+                }
+            }
+        }
+    }
+
     my $checkout = {
         DT_RowId             => $c->{itemnumber} . '-' . $c->{borrowernumber},
         title                => $c->{title},
@@ -196,9 +245,9 @@ while ( my $c = $sth->fetchrow_hashref() ) {
         part_name            => $c->{part_name} // '',
         author               => $c->{author},
         barcode              => $c->{barcode},
-        type_for_stat        => $type_for_stat ? $type_for_stat->translated_description : q{},
-        itemtype_description => $itemtype ? $itemtype->translated_description : q{},
-        recordtype_description => $recordtype ? $recordtype->translated_description : q{},
+        type_for_stat          => $type_for_stat || q{},
+        itemtype_description   => $itemtype || q{},
+        recordtype_description => $recordtype || q{},
         collection           => $collection,
         location             => $location,
         homebranch           => $c->{homebranch},
@@ -206,7 +255,8 @@ while ( my $c = $sth->fetchrow_hashref() ) {
         itemnotes_nonpublic  => $c->{itemnotes_nonpublic},
         branchcode           => $c->{branchcode},
         branchname           => $c->{branchname},
-        itemcallnumber => $c->{itemcallnumber}   || q{},
+        itemcallnumber       => $c->{itemcallnumber} || q{},
+        copynumber           => $c->{copynumber} || q{},
         charge         => $charge,
         fine           => $fine,
         price          => $c->{replacementprice} || q{},
@@ -223,8 +273,11 @@ while ( my $c = $sth->fetchrow_hashref() ) {
         onsite_checkout     => $c->{onsite_checkout},
         enumchron           => $c->{enumchron},
         renewals_count      => $renewals_count,
-        renewals_allowed    => $renewals_allowed,
+        renewals_allowed    => $renewals_allowed || 0,
         renewals_remaining  => $renewals_remaining,
+        unseen_count        => $unseen_count,
+        unseen_allowed      => $unseen_allowed,
+        unseen_remaining    => $unseen_remaining,
 
         return_claim_id         => $c->{return_claim_id},
         return_claim_notes      => $c->{return_claim_notes},
@@ -248,12 +301,14 @@ while ( my $c = $sth->fetchrow_hashref() ) {
         lost    => $lost,
         claims_returned => $claims_returned,
         damaged => $damaged,
+        materials => $materials,
         borrower => {
             surname    => $c->{surname},
             firstname  => $c->{firstname},
             cardnumber => $c->{cardnumber},
         },
         issued_today => !$c->{not_issued_today},
+        recalled => $recalled,
     };
 
     if ( $c->{not_issued_today} ) {
@@ -267,13 +322,13 @@ while ( my $c = $sth->fetchrow_hashref() ) {
 
 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;    # latest to earliest
 @checkouts_today = reverse(@checkouts_today)
-  unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );      # earliest to latest
+  if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );      # earliest to latest
 
 @checkouts_previous =
-  sort { $a->{date_due} eq $b->{date_due} ? $a->{timestamp} cmp $b->{timestamp} : $a->{date_due} cmp $b->{date_due} }
+  sort { $a->{date_due} cmp $b->{date_due} || $a->{timestamp} cmp $b->{timestamp} }
   @checkouts_previous;                                                               # latest to earliest
 @checkouts_previous = reverse(@checkouts_previous)
-  unless ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );    # earliest to latest
+  if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );    # earliest to latest
 
 my @checkouts = ( @checkouts_today, @checkouts_previous );