fix references to $session in circ/circulation.pl
[koha_gimpoz] / circ / circulation.pl
index aec1b29..49dc338 100755 (executable)
@@ -1,7 +1,5 @@
 #!/usr/bin/perl
 
-# Please use 8-character tabs for this file (indents are every 4 characters)
-
 # written 8/5/2002 by Finlay
 # script to execute issuing of books
 
@@ -23,6 +21,7 @@
 # Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
+# use warnings;  # FIXME
 use CGI;
 use C4::Output;
 use C4::Print;
@@ -50,14 +49,15 @@ use Date::Calc qw(
 #
 my $query = new CGI;
 
+my $sessionID = $query->cookie("CGISESSID") ;
+my $session = get_session($sessionID);
+
 # new op dev the branch and the printer are now defined by the userenv
 # but first we have to check if someone has tried to change them
 
 my $branch = $query->param('branch');
 if ($branch){
     # update our session so the userenv is updated
-    my $sessionID = $query->cookie("CGISESSID") ;
-    my $session = get_session($sessionID);
     $session->param('branch',$branch);
     my $branchname = GetBranchName($branch);
     $session->param('branchname',$branchname);
@@ -66,14 +66,10 @@ if ($branch){
 my $printer = $query->param('printer');
 if ($printer){
     # update our session so the userenv is updated
-  my $sessionID = $query->cookie("CGISESSID") ;
-  my $session = get_session($sessionID);
   $session->param('branchprinter',$printer);
 
 }
 if (!C4::Context->userenv && !$branch){
-  my $sessionID = $query->cookie("CGISESSID") ;
-  my $session = get_session($sessionID);
   if ($session->param('branch') eq 'NO_LIBRARY_SET'){
     # no branch set we can't issue
     print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
@@ -87,7 +83,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
         query           => $query,
         type            => "intranet",
         authnotrequired => 0,
-        flagsrequired   => { circulate => 1 },
+        flagsrequired   => { circulate => 'circulate_remaining_permissions' },
     }
 );
 
@@ -108,18 +104,16 @@ $printer = C4::Context->userenv->{'branchprinter'};
 
 
 # If Autolocated is not activated, we show the Circulation Parameters to chage settings of librarian
-    if (C4::Context->preference("AutoLocation") ne 1)
-        {
-            $template->param(
-            ManualLocation => 1,
-            );
-        }
+if (C4::Context->preference("AutoLocation") ne 1) { # FIXME: string comparison to number
+    $template->param(ManualLocation => 1);
+}
 
 my $barcode        = $query->param('barcode') || '';
+$barcode =~  s/^\s*|\s*$//g; # remove leading/trailing whitespace
 
 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
-my $stickyduedate  = $query->param('stickyduedate');
-my $duedatespec    = $query->param('duedatespec');
+my $stickyduedate  = $query->param('stickyduedate') || $session->param( 'stickyduedate' );
+my $duedatespec    = $query->param('duedatespec') || $session->param( 'stickyduedate' );
 my $issueconfirmed = $query->param('issueconfirmed');
 my $cancelreserve  = $query->param('cancelreserve');
 my $organisation   = $query->param('organisations');
@@ -127,6 +121,16 @@ my $print          = $query->param('print');
 my $newexpiry      = $query->param('dateexpiry');
 my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
 
+# Check if stickyduedate is turned off
+if ( $barcode ) {
+    # was stickyduedate loaded from session?
+    if ( $stickyduedate && ! $query->param("stickyduedate") ) {
+        $session->clear( 'stickyduedate' );
+        $stickyduedate  = $query->param('stickyduedate');
+        $duedatespec    = $query->param('duedatespec');
+    }
+}
+
 #set up cookie.....
 # my $branchcookie;
 # my $printercookie;
@@ -136,21 +140,37 @@ my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt
 # }
 #
 
-my ($datedue,$invalidduedate);
-if ($duedatespec) {
-       if ($duedatespec =~ C4::Dates->regexp('syspref')) {
-               my $tempdate = C4::Dates->new($duedatespec);
-               if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
-                       # i.e., it has to be later than today/now
-                       $datedue = $tempdate;
-               } else {
-                       $invalidduedate = 1;
-                       $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
-               }
-       } else {
-               $invalidduedate = 1;
-               $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
-       }
+my ($datedue,$invalidduedate,$globalduedate);
+
+if(C4::Context->preference('globalDueDate') && (C4::Context->preference('globalDueDate') =~ C4::Dates->regexp('syspref'))){
+        $globalduedate = C4::Dates->new(C4::Context->preference('globalDueDate'));
+}
+my $duedatespec_allow = C4::Context->preference('SpecifyDueDate');
+if($duedatespec_allow){
+    if ($duedatespec) {
+       if ($duedatespec =~ C4::Dates->regexp('syspref')) {
+               my $tempdate = C4::Dates->new($duedatespec);
+               if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
+                       # i.e., it has to be later than today/now
+                       $datedue = $tempdate;
+               } else {
+                       $invalidduedate = 1;
+                       $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
+               }
+       } else {
+               $invalidduedate = 1;
+               $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
+       }
+    } else {
+        # pass global due date to tmpl if specifyduedate is true 
+        # and we have no barcode (loading circ page but not checking out)
+        if($globalduedate &&  ! $barcode ){
+            $duedatespec = $globalduedate->output();
+            $stickyduedate = 1;
+        }
+    }
+} else {
+    $datedue = $globalduedate if($globalduedate);
 }
 
 my $todaysdate = C4::Dates->new->output('iso');
@@ -161,7 +181,6 @@ if ( $barcode eq '' && $print eq 'maybe' ) {
 }
 
 my $inprocess = ($barcode eq '') ? '' : $query->param('inprocess');
-
 if ( $barcode eq '' && $query->param('charges') eq 'yes' ) {
     $template->param(
         PAYCHARGES     => 'yes',
@@ -185,6 +204,14 @@ if ($findborrower) {
     my ( $count, $borrowers ) =
       SearchMember($findborrower, 'cardnumber', 'web' );
     my @borrowers = @$borrowers;
+        $template->param(
+                "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
+        );
+        if (C4::Context->preference("AddPatronLists")=~/code/){
+                my $categories=GetBorrowercategoryList;
+                $categories->[0]->{'first'}=1;
+                $template->param(categories=>$categories);
+        }
     if ( $#borrowers == -1 ) {
         $query->param( 'findborrower', '' );
         $message = "'$findborrower'";
@@ -256,7 +283,7 @@ if ($barcode) {
   # always check for blockers on issuing
   my ( $error, $question ) =
     CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
-  my $noerror = $invalidduedate ? 0 : 1;
+  my $blocker = $invalidduedate ? 1 : 0;
 
   delete $question->{'DEBT'} if ($debt_confirmed);
   foreach my $impossible ( keys %$error ) {
@@ -264,51 +291,38 @@ if ($barcode) {
                 $impossible => $$error{$impossible},
                 IMPOSSIBLE  => 1
             );
-            $noerror = 0;
+            $blocker = 1;
         }
-    
-  if ($issueconfirmed && $noerror) {
-    # we have no blockers for issuing and any issues needing confirmation have been resolved
-        AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
-        $inprocess = 1;
-    }
-  elsif ($issueconfirmed){     # FIXME: Do something? Or is this to *intentionally* do nothing?
-  }
-  else {
-        my $noquestion = 1;
-#         Get the item title for more information
-       my $getmessageiteminfo  = GetBiblioFromItemNumber(undef,$barcode);
-               if ($noerror) {
-                       # only pass needsconfirmation to template if issuing is possible 
-               foreach my $needsconfirmation ( keys %$question ) {
-                   $template->param(
-                       $needsconfirmation => $$question{$needsconfirmation},
-                       getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
-                       NEEDSCONFIRMATION  => 1
-                   );
-                   $noquestion = 0;
-               }
-                       # Because of the weird conditional structure (empty elsif block),
-                       # if we reached here, $issueconfirmed must be false.
-                       # Also, since we moved inside the if ($noerror) conditional,
-                       # this old chunky conditional can be simplified:
-                   # if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
-                       if ($noquestion) {
-                               AddIssue( $borrower, $barcode, $datedue );
-                               $inprocess = 1;
-                       }
-           }
-               $template->param(
-                        itemhomebranch => $getmessageiteminfo->{'homebranch'} ,                     
-                        duedatespec => $duedatespec,
-        );
+    if( !$blocker ){
+        my $confirm_required = 0;
+       unless($issueconfirmed){
+            #  Get the item title for more information
+            my $getmessageiteminfo  = GetBiblioFromItemNumber(undef,$barcode);
+                   $template->param( itemhomebranch => $getmessageiteminfo->{'homebranch'} );
+
+                   # pass needsconfirmation to template if issuing is possible and user hasn't yet confirmed.
+                   foreach my $needsconfirmation ( keys %$question ) {
+                       $template->param(
+                           $needsconfirmation => $$question{$needsconfirmation},
+                           getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
+                           NEEDSCONFIRMATION  => 1
+                       );
+                       $confirm_required = 1;
+                   }
+               }
+        unless($confirm_required) {
+            AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
+                       $inprocess = 1;
+            if($globalduedate && ! $stickyduedate && $duedatespec_allow ){
+                $duedatespec = $globalduedate->output();
+                $stickyduedate = 1;
+            }
+               }
     }
     
-# FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue 
-        my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
-        $template->param(
-        issuecount   => $issue,
-        );
+    # FIXME If the issue is confirmed, we launch another time GetMemberIssuesAndFines, now display the issue count after issue 
+    my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrowernumber );
+    $template->param( issuecount   => $issue );
 }
 
 # reload the borrower info for the sake of reseting the flags.....
@@ -424,7 +438,7 @@ my @issued_itemtypes_count_loop;
 
 if ($borrower) {
 # get each issue of the borrower & separate them in todayissues & previous issues
-    my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
+    my ($issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
 
     # split in 2 arrays for today & previous
     foreach my $it ( @$issueslist ) {
@@ -484,7 +498,7 @@ FROM issuingrules
   WHERE categorycode=?
 " );
 #my @issued_itemtypes_count;  # huh?
-$issueqty_sth->execute("*");   # FIXME: Why have a WHERE clause at all with a hardcoded "*"?
+$issueqty_sth->execute("*");   # This is a literal asterisk, not a wildcard.
 
 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
 
@@ -527,6 +541,7 @@ if ($borrowerslist) {
         -id       => 'borrowernumber',
         -values   => \@values,
         -labels   => \%labels,
+       -onclick  => "window.location = '/cgi-bin/koha/circ/circulation.pl?borrowernumber=' + this.value;",
         -size     => 7,
         -tabindex => '',
         -multiple => 0
@@ -674,6 +689,7 @@ $template->param(
     amountold         => $amountold,
     barcode           => $barcode,
     stickyduedate     => $stickyduedate,
+    duedatespec       => $duedatespec,
     message           => $message,
     CGIselectborrower => $CGIselectborrower,
     todayissues       => \@todaysissues,
@@ -685,11 +701,9 @@ $template->param(
     circview => 1,
 );
 
-# set return date if stickyduedate
+# save stickyduedate to session
 if ($stickyduedate) {
-    $template->param(
-        duedatespec => $duedatespec,
-    );
+    $session->param( 'stickyduedate', $duedatespec );
 }
 
 #if ($branchcookie) {
@@ -702,7 +716,7 @@ $template->param( picture => 1 ) if $picture;
 
 $template->param(
     debt_confirmed            => $debt_confirmed,
-    SpecifyDueDate            => C4::Context->preference("SpecifyDueDate"),
+    SpecifyDueDate            => $duedatespec_allow,
     CircAutocompl             => C4::Context->preference("CircAutocompl"),
        AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
     dateformat                => C4::Context->preference("dateformat"),