Bug 14310 [QA Followup] - Deal with error conditions
[koha.git] / svc / checkouts
index d2eb8ee..93a1c95 100755 (executable)
@@ -23,7 +23,7 @@ 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::Koha qw(GetAuthorisedValueByCode);
@@ -35,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;
 }
 
@@ -76,6 +79,7 @@ my $sql = '
         itemnumber,
         barcode,
         itemnotes,
+        itemnotes_nonpublic,
         itemcallnumber,
         replacementprice,
 
@@ -83,9 +87,7 @@ my $sql = '
         branchname,
 
         items.itype,
-        itemtype_item.description AS itype_description,
         biblioitems.itemtype,
-        itemtype_bib.description AS itemtype_description,
 
         borrowernumber,
         surname,
@@ -103,8 +105,6 @@ my $sql = '
         LEFT JOIN biblioitems USING ( biblionumber )
         LEFT JOIN borrowers USING ( borrowernumber )
         LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
-        LEFT JOIN itemtypes itemtype_bib ON ( biblioitems.itemtype = itemtype_bib.itemtype )
-        LEFT JOIN itemtypes itemtype_item ON ( items.itype = itemtype_item.itemtype )
     WHERE borrowernumber
 ';
 
@@ -145,17 +145,19 @@ 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},
-        itemtype_description => $item_level_itypes ? $c->{itype_description} : $c->{itemtype_description},
-        location   => $c->{location} ? GetAuthorisedValueByCode( 'LOC', $c->{location} ) : q{},
-        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,
@@ -170,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},
-        onsite_checkout         => $c->{onsite_checkout},
+        onsite_checkout     => $c->{onsite_checkout},
         renewals_count      => $renewals_count,
         renewals_allowed    => $renewals_allowed,
         renewals_remaining  => $renewals_remaining,
@@ -186,13 +188,10 @@ while ( my $c = $sth->fetchrow_hashref() ) {
                 as_due_date => 1
             }
         ),
-        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,
+        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},
@@ -210,7 +209,7 @@ while ( my $c = $sth->fetchrow_hashref() ) {
 }
 
 
-@checkouts_today = sort { $a->{timstamp} cmp $b->{timestamp} } @checkouts_today;
+@checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
 @checkouts_today = reverse(@checkouts_today)
   unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );