Changes to prevent javascript errors in Internet Explorer
[koha.git] / circ / circulation.pl
index add047d..70e7fd2 100755 (executable)
@@ -39,7 +39,6 @@ use CGI::Session;
 
 use Date::Calc qw(
   Today
-  Today_and_Now
   Add_Delta_YM
   Add_Delta_Days
   Date_to_Days
@@ -93,6 +92,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
         flagsrequired   => { circulate => 1 },
     }
 );
+
 my $branches = GetBranches();
 
 my $printers = GetPrinters();
@@ -121,16 +121,14 @@ $printer = C4::Context->userenv->{'branchprinter'};
 my $barcode        = $query->param('barcode') || '';
 
 $barcode = barcodedecode($barcode) if( $barcode && C4::Context->preference('itemBarcodeInputFilter'));
-my $year           = $query->param('year');
-my $month          = $query->param('month');
-my $day            = $query->param('day');
 my $stickyduedate  = $query->param('stickyduedate');
 my $duedatespec    = $query->param('duedatespec');
 my $issueconfirmed = $query->param('issueconfirmed');
 my $cancelreserve  = $query->param('cancelreserve');
 my $organisation   = $query->param('organisations');
 my $print          = $query->param('print');
-my $newexpiry = $query->param('dateexpiry');
+my $newexpiry      = $query->param('dateexpiry');
+my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
 
 #set up cookie.....
 # my $branchcookie;
@@ -143,17 +141,22 @@ my $newexpiry = $query->param('dateexpiry');
 
 my ($datedue,$invalidduedate);
 if ($duedatespec) {
-  $datedue =  C4::Dates->new($duedatespec);
-  $invalidduedate=1 unless $datedue;
+       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);
+       }
 }
 
-#if (defined($year)) {
-#        $duedatespec = "$year-$month-$day";
-#} else {
-#        ($year, $month, $day) = ($duedatespec) ? split /-/, $duedatespec : (0,0,0);
-#}
-
-my $todaysdate     = sprintf("%-04.4d%-02.2d%-02.2d", Today());
+my $todaysdate = C4::Dates->new->output('iso');
 
 # check and see if we should print
 if ( $barcode eq '' && $print eq 'maybe' ) {
@@ -256,7 +259,9 @@ if ($barcode) {
   # always check for blockers on issuing
   my ( $error, $question ) =
     CanBookBeIssued( $borrower, $barcode, $datedue , $inprocess );
-  my $noerror    = 1;
+  my $noerror = $invalidduedate ? 0 : 1;
+
+  delete $question->{'DEBT'} if ($debt_confirmed);
   foreach my $impossible ( keys %$error ) {
             $template->param(
                 $impossible => $$error{$impossible},
@@ -270,9 +275,9 @@ if ($barcode) {
         AddIssue( $borrower, $barcode, $datedue, $cancelreserve );
         $inprocess = 1;
     }
-  elsif ($issueconfirmed){
+  elsif ($issueconfirmed){     # FIXME: Do something? Or is this to *intentionally* do nothing?
   }
-    else {
+  else {
         my $noquestion = 1;
 #         Get the item title for more information
        my $getmessageiteminfo  = GetBiblioFromItemNumber(undef,$barcode);
@@ -286,16 +291,20 @@ if ($barcode) {
                    );
                    $noquestion = 0;
                }
-        }
-               # only pass needsconfirmation to template if issuing is possible 
+                       # 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 ( $noerror && ( $noquestion || $issueconfirmed ) ) {
-            AddIssue( $borrower, $barcode, $datedue );
-            $inprocess = 1;
-        }
     }
     
 # FIXME If the issue is confirmed, we launch another time borrdata2, now display the issue count after issue 
@@ -378,12 +387,12 @@ if ($borrowernumber) {
             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );  # fixme - we should have item-level reserves here ?
             $getreserv{color}           = 'inwait';
             $getreserv{title}           = $getbibinfo->{'title'};
-            $getreserv{waitingposition} = $num_res->{'priority'};
             $getreserv{nottransfered}   = 0;
             $getreserv{itemtype}        = $getbibtype->{'description'};
             $getreserv{author}          = $getbibinfo->{'author'};
           $getreserv{biblionumber}    = $num_res->{'biblionumber'};
         }
+        $getreserv{waitingposition} = $num_res->{'priority'};
         push( @reservloop, \%getreserv );
 
 #         if we have a reserve waiting, initiate waitingreserveloop
@@ -399,9 +408,7 @@ if ($borrowernumber) {
         reservloop  => \@reservloop ,
         WaitingReserveLoop  => \@WaitingReserveLoop,
     );
-    $borrowercategory = GetBorrowercategory( $borrower->{'categorycode'} );
-    $category_type = $borrowercategory->{'category_type'};
-    ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
+    $template->param( adultborrower => 1 ) if ( $borrower->{'category_type'} eq 'A' );
 }
 
 # make the issued books table.
@@ -419,64 +426,58 @@ my $issued_itemtypes_flags;            #hashref that stores flags
 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'});
 
     # split in 2 arrays for today & previous
     foreach my $it ( @$issueslist ) {
         # set itemtype per item-level_itype syspref - FIXME this is an ugly hack
-        $it->{'itemtype'} = (C4::Context->preference('item-level_itypes')) ? $it->{'itype'} : $it->{'itemtype'};
+        $it->{'itemtype'} = ( C4::Context->preference( 'item-level_itypes' ) ) ? $it->{'itype'} : $it->{'itemtype'};
 
-        my $issuedate = $it->{'issuedate'};
-        $issuedate =~ s/-//g;
-        $issuedate = substr( $issuedate, 0, 8 );
-    ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
+        ($it->{'charge'}, $it->{'itemtype_charge'}) = GetIssuingCharges(
             $it->{'itemnumber'}, $borrower->{'borrowernumber'}
-    );
-    $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
+        );
+        $it->{'charge'} = sprintf("%.2f", $it->{'charge'});
         my $can_renew_error;
         ($it->{'can_renew'}, $can_renew_error) = CanBookBeRenewed( 
             $borrower->{'borrowernumber'},$it->{'itemnumber'}
-    );
+        );
         $it->{"renew_error_${can_renew_error}"} = 1 if defined $can_renew_error;
-    my ($restype, $reserves) = CheckReserves($it->{'itemnumber'});
-    ($restype) and $it->{'can_renew'} = 0;
+        my ( $restype, $reserves ) = CheckReserves( $it->{'itemnumber'} );
+        ( $restype ) and $it->{'can_renew'} = 0;
 
-    $it->{'dd'} = format_date($it->{'date_due'});
-        my $datedue = $it->{'date_due'};
-        $datedue =~ s/-//g;
-    $it->{'od'} = ($datedue < $todaysdate) ? 1 : 0 ;
+        $it->{'dd'} = format_date($it->{'date_due'});
+        $it->{'od'} = ( $it->{'date_due'} lt $todaysdate ) ? 1 : 0 ;
         ($it->{'author'} eq '') and $it->{'author'} = ' ';
         $it->{'renew_failed'} = $renew_failed[$it->{'itemnumber'}];
         # ADDED BY JF: NEW ITEMTYPE COUNT DISPLAY
         $issued_itemtypes_count->{ $it->{'itemtype'} }++;
 
-        if ( $todaysdate == $issuedate ) {
+        if ( $todaysdate eq $it->{'issuedate'} or $todaysdate eq $it->{'lastreneweddate'} ) {
             push @todaysissues, $it;
         } else {
             push @previousissues, $it;
         }
     }
-  if (C4::Context->preference("todaysIssuesDefaultSortOrder") eq 'asc'){
-    @todaysissues   = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
-  }
-  else {
-    @todaysissues   = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
-  }
-  if (C4::Context->preference("previousIssuesDefaultSortOrder") eq 'asc'){
-    @previousissues = sort { $a->{'date_due' } cmp $b->{'date_due' } } @previousissues;
-  }
-  else {
-    @previousissues = sort { $b->{'date_due' } cmp $a->{'date_due' } } @previousissues;
-  }
+    if ( C4::Context->preference( "todaysIssuesDefaultSortOrder" ) eq 'asc' ) {
+        @todaysissues   = sort { $a->{'timestamp'} cmp $b->{'timestamp'} } @todaysissues;
+    }
+    else {
+        @todaysissues   = sort { $b->{'timestamp'} cmp $a->{'timestamp'} } @todaysissues;
+    }
+    if ( C4::Context->preference( "previousIssuesDefaultSortOrder" ) eq 'asc' ){
+        @previousissues = sort { $a->{'date_due'} cmp $b->{'date_due'} } @previousissues;
+    }
+    else {
+        @previousissues = sort { $b->{'date_due'} cmp $a->{'date_due'} } @previousissues;
+    }
     my $i = 1;
-  foreach my $book (@todaysissues) {
-        $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
+    foreach my $book ( @todaysissues ) {
+        $book->{'togglecolor'} = ( ++$i % 2 ) ? 0 : 1 ;
     }
     $i = 1;
-  foreach my $book (@previousissues) {
-        $book->{'togglecolor'} = (++$i % 2) ? 0 : 1 ;
+    foreach my $book ( @previousissues ) {
+        $book->{'togglecolor'} = ( ++$i % 2 ) ? 0 : 1 ;
     }
 }
 
@@ -493,7 +494,7 @@ FROM issuingrules
   WHERE categorycode=?
 " );
 #my @issued_itemtypes_count;  # huh?
-$issueqty_sth->execute("*");
+$issueqty_sth->execute("*");   # FIXME: Why have a WHERE clause at all with a hardcoded "*"?
 
 while ( my $data = $issueqty_sth->fetchrow_hashref() ) {
 
@@ -522,10 +523,7 @@ my %labels;
 my $CGIselectborrower;
 if ($borrowerslist) {
     foreach (
-        sort {
-                lc $a->{'surname'}
-              . lc $a->{'firstname'} cmp lc $b->{'surname'}
-              . lc $b->{'firstname'}
+        sort {(lc $a->{'surname'} cmp lc $b->{'surname'} ?lc $a->{'surname'} cmp lc $b->{'surname'}:lc $a->{'firstname'} cmp lc $b->{'firstname'})
         } @$borrowerslist
       )
     {
@@ -551,7 +549,7 @@ my $flag;
 
 foreach $flag ( sort keys %$flags ) {
     $template->param( flagged=> 1);
-    $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
+    $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
     if ( $flags->{$flag}->{'noissues'} ) {
         $template->param(
             flagged  => 1,
@@ -569,7 +567,8 @@ foreach $flag ( sort keys %$flags ) {
         if ( $flag eq 'CHARGES' ) {
             $template->param(
                 charges    => 'true',
-                chargesmsg => $flags->{'CHARGES'}->{'message'}
+                chargesmsg => $flags->{'CHARGES'}->{'message'},
+                charges_is_blocker => 1
             );
         }
         if ( $flag eq 'CREDITS' ) {
@@ -627,6 +626,13 @@ foreach $flag ( sort keys %$flags ) {
 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
 my @temp = split( /\$/, $amountold );
 
+    if ( $borrower->{'category_type'} eq 'C') {
+        my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
+        my $cnt = scalar(@$catcodes);
+        $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
+        $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
+    }
+
 my $CGIorganisations;
 my $member_of_institution;
 if ( C4::Context->preference("memberofinstitution") ) {
@@ -664,13 +670,14 @@ $template->param(
     dateexpiry        => format_date($newexpiry),
     expiry            => format_date($borrower->{'dateexpiry'}),
     categorycode      => $borrower->{'categorycode'},
-    categoryname      => $borrowercategory->{description},
+    categoryname      => $borrower->{description},
     address           => $borrower->{'address'},
     address2          => $borrower->{'address2'},
     email             => $borrower->{'email'},
     emailpro          => $borrower->{'emailpro'},
     borrowernotes     => $borrower->{'borrowernotes'},
     city              => $borrower->{'city'},
+    zipcode          => $borrower->{'zipcode'},
     phone             => $borrower->{'phone'} || $borrower->{'mobile'},
     cardnumber        => $borrower->{'cardnumber'},
     amountold         => $amountold,
@@ -683,6 +690,7 @@ $template->param(
     inprocess         => $inprocess,
     memberofinstution => $member_of_institution,
     CGIorganisations  => $CGIorganisations,
+       is_child        => ($borrower->{'category_type'} eq 'C'),
   circview => 1,
   
 );
@@ -703,8 +711,10 @@ $template->param( picture => 1 ) if $picture;
 
 
 $template->param(
+    debt_confirmed           => $debt_confirmed,
     SpecifyDueDate           => C4::Context->preference("SpecifyDueDate"),
-    CircAutocompl            => C4::Context->preference("CircAutocompl") ,
+    CircAutocompl            => C4::Context->preference("CircAutocompl"),
+    dateformat               => C4::Context->preference("dateformat"),
     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
 );
 output_html_with_http_headers $query, $cookie, $template->output;