use Memoize because Memcache::Memoize is slow for me
[koha.git] / acqui / parcel.pl
index 675b07d..c256c60 100755 (executable)
@@ -26,6 +26,7 @@
 parcel.pl
 
 =head1 DESCRIPTION
+
 This script shows all orders receipt or pending for a given supplier.
 It allows to write an order as 'received' when he arrives.
 
@@ -34,9 +35,11 @@ It allows to write an order as 'received' when he arrives.
 =over 4
 
 =item supplierid
+
 To know the supplier this script has to show orders.
 
 =item code
+
 is the bookseller invoice number.
 
 =item freight
@@ -46,6 +49,7 @@ is the bookseller invoice number.
 
 
 =item datereceived
+
 To filter the results list on this given date.
 
 =back
@@ -57,7 +61,7 @@ use strict;
 use C4::Auth;
 use C4::Acquisition;
 use C4::Budgets;
-use C4::Bookseller;
+use C4::Bookseller qw/ GetBookSellerFromId /;
 use C4::Biblio;
 use C4::Items;
 use CGI;
@@ -71,7 +75,8 @@ my $bookseller=GetBookSellerFromId($supplierid);
 
 my $invoice=$input->param('invoice') || '';
 my $freight=$input->param('freight');
-my $gst= $input->param('gst') || $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
+my $input_gst = ($input->param('gst') eq '' ? undef : $input->param('gst'));
+my $gst= $input_gst // $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
 my $datereceived =  ($input->param('op') eq 'new') ? C4::Dates->new($input->param('datereceived')) 
                                        :  C4::Dates->new($input->param('datereceived'), 'iso')   ;
 $datereceived = C4::Dates->new() unless $datereceived;
@@ -135,44 +140,6 @@ my ($template, $loggedinuser, $cookie)
                  debug => 1,
 });
 
-my $action = $input->param('action');
-my $ordernumber = $input->param('ordernumber');
-my $biblionumber = $input->param('biblionumber');
-
-# If canceling an order
-if ($action eq "cancelorder") {
-
-    my $error_delitem;
-    my $error_delbiblio;
-
-    # We delete the order
-    DelOrder($biblionumber, $ordernumber);
-
-    # We delete all the items related to this order
-    my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
-    foreach (@itemnumbers) {
-       my $delcheck = DelItemCheck(C4::Context->dbh, $biblionumber, $_);
-       # (should always success, as no issue should exist on item on order)
-       if ($delcheck != 1) { $error_delitem = 1; }
-    }
-
-    # We get the number of remaining items
-    my $itemcount = GetItemsCount($biblionumber);
-    
-    # If there are no items left,
-    if ($itemcount eq 0) {
-       # We delete the record
-       $error_delbiblio = DelBiblio($biblionumber);    
-    }
-
-    if ($error_delitem || $error_delbiblio) {
-       if ($error_delitem)   { $template->param(error_delitem => 1); }
-       if ($error_delbiblio) { $template->param(error_delbiblio => 1); }
-    } else {
-       $template->param(success_delorder => 1);
-    }
-}
-
 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
 if( scalar(@rcv_err) ) {
        my $cnt=0;
@@ -232,6 +199,7 @@ my @loop_orders = ();
 for (my $i = 0 ; $i < $countpendings ; $i++) {
     my %line;
     %line = %{$pendingorders->[$i]};
+   
     $line{quantity}+=0;
     $line{quantityreceived}+=0;
     $line{unitprice}+=0;
@@ -247,6 +215,36 @@ for (my $i = 0 ; $i < $countpendings ; $i++) {
     $line{total} = $total;
     $line{supplierid} = $supplierid;
     $ordergrandtotal += $line{ecost} * $line{quantity};
+    
+    my $biblionumber = $line{'biblionumber'};
+    my $countbiblio = CountBiblioInOrders($biblionumber);
+    my $ordernumber = $line{'ordernumber'};
+    my @subscriptions = GetSubscriptionsId ($biblionumber);
+    my $itemcount = GetItemsCount($biblionumber);
+    my $holds  = GetHolds ($biblionumber);
+    my @items = GetItemnumbersFromOrder( $ordernumber );
+    my $itemholds;
+    foreach my $item (@items){
+        my $nb = GetItemHolds($biblionumber, $item);
+        if ($nb){
+            $itemholds += $nb;
+        }
+    }
+    
+    # if the biblio is not in other orders and if there is no items elsewhere and no subscriptions and no holds we can then show the link "Delete order and Biblio" see bug 5680
+    $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
+    $line{items}                = ($itemcount) - (scalar @items);
+    $line{left_item}            = 1 if $line{items} >= 1;
+    $line{left_biblio}          = 1 if $countbiblio > 1;
+    $line{biblios}              = $countbiblio - 1;
+    $line{left_subscription}    = 1 if scalar @subscriptions >= 1;
+    $line{subscriptions}        = scalar @subscriptions;
+    $line{left_holds}           = 1 if $holds >= 1;
+    $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
+    $line{holds}                = $holds;
+    $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
+    
+    
     push @loop_orders, \%line if ($i >= $startfrom and $i < $startfrom + $resultsperpage);
 }
 $freight = $totalfreight unless $freight;
@@ -315,3 +313,4 @@ $template->param(
     resultsperpage        => $resultsperpage,
 );
 output_html_with_http_headers $input, $cookie, $template->output;