Bug 19458: Self-check module highlighting
[koha.git] / opac / sco / sco-main.pl
index 80ac47d..49018da 100755 (executable)
@@ -67,45 +67,42 @@ if (C4::Context->preference('AutoSelfCheckAllowed'))
     $query->param(-name=>'koha_login_context',-values=>['sco']);
 }
 $query->param(-name=>'sco_user_login',-values=>[1]);
-my ($template, $loggedinuser, $cookie) = get_template_and_user({
-    template_name   => "sco/sco-main.tt",
-    authnotrequired => 0,
-    flagsrequired => { circulate => "self_checkout" },
-    query => $query,
-    type  => "opac",
-    debug => 1,
-});
 
-if (C4::Context->preference('SelfCheckoutByLogin'))
-{
-    $template->param(authbylogin  => 1);
-}
+my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
+    {
+        template_name   => "sco/sco-main.tt",
+        authnotrequired => 0,
+        flagsrequired   => { self_check => "self_checkout_module" },
+        query           => $query,
+        type            => "opac",
+        debug           => 1,
+    }
+);
 
 # Get the self checkout timeout preference, or use 120 seconds as a default
 my $selfchecktimeout = 120000;
 if (C4::Context->preference('SelfCheckTimeout')) { 
     $selfchecktimeout = C4::Context->preference('SelfCheckTimeout') * 1000;
 }
-$template->param(SelfCheckTimeout => $selfchecktimeout);
+$template->param( SelfCheckTimeout => $selfchecktimeout );
 
 # Checks policy laid out by AllowSelfCheckReturns, defaults to 'on' if preference is undefined
 my $allowselfcheckreturns = 1;
 if (defined C4::Context->preference('AllowSelfCheckReturns')) {
     $allowselfcheckreturns = C4::Context->preference('AllowSelfCheckReturns');
 }
-$template->param(AllowSelfCheckReturns => $allowselfcheckreturns);
-
 
 my $issuerid = $loggedinuser;
-my ($op, $patronid, $patronlogin, $patronpw, $barcode, $confirmed) = (
+my ($op, $patronid, $patronlogin, $patronpw, $barcode, $confirmed, $newissues) = (
     $query->param("op")         || '',
     $query->param("patronid")   || '',
     $query->param("patronlogin")|| '',
     $query->param("patronpw")   || '',
     $query->param("barcode")    || '',
     $query->param("confirmed")  || '',
+    $query->param("newissues")  || '',
 );
-
+my @newissueslist = split /,/, $newissues;
 my $issuenoconfirm = 1; #don't need to confirm on issue.
 my $issuer   = Koha::Patrons->find( $issuerid )->unblessed;
 my $item     = GetItem(undef,$barcode);
@@ -114,11 +111,11 @@ if (C4::Context->preference('SelfCheckoutByLogin') && !$patronid) {
     my $resval;
     ($resval, $patronid) = checkpw($dbh, $patronlogin, $patronpw);
 }
-my $borrower = Koha::Patrons->find( { cardnumber => $patronid } )->unblessed;
 
-my $currencySymbol = "";
-if ( my $active_currency = Koha::Acquisition::Currencies->get_active ) {
-    $currencySymbol = $active_currency->symbol;
+my ( $borrower, $patron );
+if ( $patronid ) {
+    $patron = Koha::Patrons->find( { cardnumber => $patronid } );
+    $borrower = $patron->unblessed if $patron;
 }
 
 my $branch = $issuer->{branchcode};
@@ -131,14 +128,12 @@ if ($op eq "logout") {
 }
 elsif ( $op eq "returnbook" && $allowselfcheckreturns ) {
     my ($doreturn) = AddReturn( $barcode, $branch );
-    #warn "returnbook: " . $doreturn;
-    $borrower = Koha::Patrons->find( { cardnumber => $patronid } )->unblessed;
 }
-elsif ( $op eq "checkout" ) {
+elsif ( $patron and $op eq "checkout" ) {
     my $impossible  = {};
     my $needconfirm = {};
     ( $impossible, $needconfirm ) = CanBookBeIssued(
-        $borrower,
+        $patron,
         $barcode,
         undef,
         0,
@@ -167,7 +162,7 @@ elsif ( $op eq "checkout" ) {
             hide_main                 => 1,
         );
         if ($issue_error eq 'DEBT') {
-            $template->param(amount => $currencySymbol.$impossible->{DEBT});
+            $template->param(DEBT => $impossible->{DEBT});
         }
         #warn "issue_error: " . $issue_error ;
         if ( $issue_error eq "NO_MORE_RENEWALS" ) {
@@ -199,7 +194,7 @@ elsif ( $op eq "checkout" ) {
             hide_main                 => 1,
         );
         if ($issue_error eq 'DEBT') {
-            $template->param(amount => $currencySymbol.$needconfirm->{DEBT});
+            $template->param(DEBT => $needconfirm->{DEBT});
         }
     } else {
         if ( $confirmed || $issuenoconfirm ) {    # we'll want to call getpatroninfo again to get updated issues.
@@ -221,7 +216,9 @@ elsif ( $op eq "checkout" ) {
                 )->count;
             }
             AddIssue( $borrower, $barcode );
-
+            push @newissueslist, $barcode;
+            print 'Issues \n';
+            print join(',', @newissueslist);
             if ( $hold_existed ) {
                 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
                 $template->param(
@@ -250,28 +247,31 @@ elsif ( $op eq "checkout" ) {
     }
 } # $op
 
-if ($borrower->{cardnumber}) {
+if ($borrower) {
+    print 'borrower \n';
 #   warn "issuer's  branchcode: " .   $issuer->{branchcode};
 #   warn   "user's  branchcode: " . $borrower->{branchcode};
     my $borrowername = sprintf "%s %s", ($borrower->{firstname} || ''), ($borrower->{surname} || '');
-    my @issues;
-    my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
-    foreach my $it (@$issueslist) {
+    my $pending_checkouts = $patron->pending_checkouts;
+    my @checkouts;
+    while ( my $c = $pending_checkouts->next ) {
+        my $checkout = $c->unblessed_all_relateds;
         my ($can_be_renewed, $renew_error) = CanBookBeRenewed(
             $borrower->{borrowernumber},
-            $it->{itemnumber},
+            $checkout->{itemnumber},
         );
-        $it->{can_be_renewed} = $can_be_renewed;
-        $it->{renew_error} = $renew_error;
-        $it->{date_due}  = $it->{date_due_sql};
-        push @issues, $it;
+        $checkout->{can_be_renewed} = $can_be_renewed; # In the future this will be $checkout->can_be_renewed
+        $checkout->{renew_error} = $renew_error;
+        $checkout->{overdue} = $c->is_overdue;
+        push @checkouts, $checkout;
     }
 
     $template->param(
         validuser => 1,
         borrowername => $borrowername,
-        issues_count => scalar(@issues),
-        ISSUES => \@issues,
+        issues_count => scalar(@checkouts),
+        ISSUES => \@checkouts,
+        newissues => join(',',@newissueslist),
         patronid => $patronid,
         patronlogin => $patronlogin,
         patronpw => $patronpw,
@@ -301,7 +301,6 @@ if ($borrower->{cardnumber}) {
         my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
         $template->param(
             display_patron_image => 1,
-            cardnumber           => $borrower->{cardnumber},
             csrf_token           => Koha::Token->new->generate_csrf( { session_id => scalar $query->cookie('CGISESSID') . $borrower->{cardnumber}, id => $borrower->{userid}} ),
         ) if $patron_image;
     }
@@ -312,9 +311,4 @@ if ($borrower->{cardnumber}) {
     );
 }
 
-$template->param(
-    SCOUserJS  => C4::Context->preference('SCOUserJS'),
-    SCOUserCSS => C4::Context->preference('SCOUserCSS'),
-);
-
 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };