Bug 14310 [QA Followup] - Deal with error conditions
[koha.git] / svc / checkouts
index 9e56f64..93a1c95 100755 (executable)
@@ -23,10 +23,11 @@ use warnings;
 use CGI;
 use JSON qw(to_json);
 
-use C4::Auth qw(check_cookie_auth);
+use C4::Auth qw(check_cookie_auth haspermission get_session);
 use C4::Biblio qw(GetMarcBiblio GetFrameworkCode GetRecordValue );
-use C4::Circulation
-  qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
+use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
+use C4::Koha qw(GetAuthorisedValueByCode);
+use C4::Overdues qw(GetFine);
 use C4::Context;
 
 use Koha::DateUtils;
@@ -34,10 +35,13 @@ use Koha::DateUtils;
 my $input = new CGI;
 
 my ( $auth_status, $sessionID ) =
-  check_cookie_auth( $input->cookie('CGISESSID'),
-    { circulate => 'circulate_remaining_permissions' } );
+  check_cookie_auth( $input->cookie('CGISESSID'));
 
-if ( $auth_status ne "ok" ) {
+my $session   = get_session($sessionID);
+my $userid   = $session->param('id');
+
+unless (haspermission($userid, { circulate => 'circulate_remaining_permissions' })
+    || haspermission($userid, { borrowers => '*' })) {
     exit 0;
 }
 
@@ -46,9 +50,12 @@ my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
 my @borrowernumber   = $input->param('borrowernumber');
 my $offset           = $input->param('iDisplayStart');
 my $results_per_page = $input->param('iDisplayLength') || -1;
-my $sorting_column   = $sort_columns[ $input->param('iSortCol_0') ]
-  || 'issuedate';
-my $sorting_direction = $input->param('sSortDir_0') eq 'asc' ? 'asc' : 'desc';
+
+my $sorting_column = $input->param('iSortCol_0') || q{};
+$sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
+
+my $sorting_direction = $input->param('sSortDir_0') || q{};
+$sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
 
 $results_per_page = undef if ( $results_per_page == -1 );
 
@@ -63,7 +70,7 @@ my $sql = '
         date_due < now() as date_due_overdue,
         issues.timestamp,
 
-        inhouse_use,
+        onsite_checkout,
 
         biblionumber,
         biblio.title,
@@ -72,20 +79,25 @@ my $sql = '
         itemnumber,
         barcode,
         itemnotes,
+        itemnotes_nonpublic,
         itemcallnumber,
         replacementprice,
 
         issues.branchcode,
         branchname,
 
-        itype,
-        itemtype,
+        items.itype,
+        biblioitems.itemtype,
 
         borrowernumber,
         surname,
         firstname,
         cardnumber,
 
+        itemlost,
+        damaged,
+        location,
+
         DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
     FROM issues
         LEFT JOIN items USING ( itemnumber )
@@ -116,11 +128,12 @@ my @checkouts_today;
 my @checkouts_previous;
 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 ) =
       CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
     my $can_renew_date =
-      $can_renew_error eq 'too_soon'
+      $can_renew_error && $can_renew_error eq 'too_soon'
       ? output_pref(
         {
             dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
@@ -132,17 +145,22 @@ while ( my $c = $sth->fetchrow_hashref() ) {
     my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
 
+    my $itemtype = C4::Koha::getitemtypeinfo( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
     my $checkout = {
-        DT_RowId   => $c->{itemnumber} . '-' . $c->{borrowernumber},
-        title      => $c->{title},
-        author     => $c->{author},
-        barcode    => $c->{barcode},
-        itemtype   => $item_level_itypes ? $c->{itype} : $c->{itemtype},
-        itemnotes  => $c->{itemnotes},
-        branchcode => $c->{branchcode},
-        branchname => $c->{branchname},
+        DT_RowId             => $c->{itemnumber} . '-' . $c->{borrowernumber},
+        title                => $c->{title},
+        author               => $c->{author},
+        barcode              => $c->{barcode},
+        itemtype             => $item_level_itypes ? $c->{itype} : $c->{itemtype},
+        itemtype_description => $itemtype->{translated_description},
+        location             => $c->{location} ? GetAuthorisedValueByCode( 'LOC', $c->{location} ) : q{},
+        itemnotes            => $c->{itemnotes},
+        itemnotes_nonpublic  => $c->{itemnotes_nonpublic},
+        branchcode           => $c->{branchcode},
+        branchname           => $c->{branchname},
         itemcallnumber => $c->{itemcallnumber}   || q{},
         charge         => $charge,
+        fine           => $fine,
         price          => $c->{replacementprice} || q{},
         can_renew      => $can_renew,
         can_renew_error     => $can_renew_error,
@@ -154,7 +172,7 @@ while ( my $c = $sth->fetchrow_hashref() ) {
         date_due            => $c->{date_due},
         date_due_overdue    => $c->{date_due_overdue} ? JSON::true : JSON::false,
         timestamp           => $c->{timestamp},
-        inhouse_use         => $c->{inhouse_use},
+        onsite_checkout     => $c->{onsite_checkout},
         renewals_count      => $renewals_count,
         renewals_allowed    => $renewals_allowed,
         renewals_remaining  => $renewals_remaining,
@@ -170,11 +188,10 @@ while ( my $c = $sth->fetchrow_hashref() ) {
                 as_due_date => 1
             }
         ),
-        subtitle => GetRecordValue(
-            'subtitle',
-            GetMarcBiblio( $c->{biblionumber} ),
-            GetFrameworkCode( $c->{biblionumber} )
-        ),
+        subtitle =>
+          GetRecordValue( 'subtitle', GetMarcBiblio( $c->{biblionumber} ), GetFrameworkCode( $c->{biblionumber} ) ),
+        lost    => $c->{itemlost} ? GetAuthorisedValueByCode( 'LOST',    $c->{itemlost} ) : undef,
+        damaged => $c->{damaged}  ? GetAuthorisedValueByCode( 'DAMAGED', $c->{damaged} )  : undef,
         borrower => {
             surname    => $c->{surname},
             firstname  => $c->{firstname},
@@ -191,13 +208,21 @@ while ( my $c = $sth->fetchrow_hashref() ) {
     }
 }
 
+
+@checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
 @checkouts_today = reverse(@checkouts_today)
-  if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
+  unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
+
+@checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
 @checkouts_previous = reverse(@checkouts_previous)
   if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
 
 my @checkouts = ( @checkouts_today, @checkouts_previous );
 
+my $i = 1;
+map { $_->{sort_order} = $i++ } @checkouts;
+
+
 my $data;
 $data->{'iTotalRecords'}        = scalar @checkouts;
 $data->{'iTotalDisplayRecords'} = scalar @checkouts;