Bug 7751: Decrease Loans for Items in Demand for Holds
[koha.git] / acqui / parcel.pl
index ad95944..1c1736d 100755 (executable)
@@ -34,7 +34,7 @@ It allows to write an order as 'received' when he arrives.
 
 =over 4
 
-=item supplierid
+=item booksellerid
 
 To know the supplier this script has to show orders.
 
@@ -67,70 +67,30 @@ use C4::Items;
 use CGI;
 use C4::Output;
 use C4::Dates qw/format_date format_date_in_iso/;
+use C4::Suggestions;
 use JSON;
 
 my $input=new CGI;
-my $supplierid=$input->param('supplierid');
-my $bookseller=GetBookSellerFromId($supplierid);
+my $booksellerid=$input->param('booksellerid');
+my $bookseller=GetBookSellerFromId($booksellerid);
 
 my $invoice=$input->param('invoice') || '';
 my $freight=$input->param('freight');
 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')   ;
+my $op = $input->param('op') // '';
+my $datereceived = ( $op eq ('new' or 'search' ) )
+    ? C4::Dates->new($input->param('datereceived'))
+    :  C4::Dates->new($input->param('datereceived'), 'iso');
 $datereceived = C4::Dates->new() unless $datereceived;
 my $code            = $input->param('code');
 my @rcv_err         = $input->param('error');
 my @rcv_err_barcode = $input->param('error_bc');
-
 my $startfrom=$input->param('startfrom');
 my $resultsperpage = $input->param('resultsperpage');
 $resultsperpage = 20 unless ($resultsperpage);
 $startfrom=0 unless ($startfrom);
 
-if($input->param('format') eq "json"){
-    my ($template, $loggedinuser, $cookie)
-        = get_template_and_user({template_name => "acqui/ajax.tmpl",
-                 query => $input,
-                                type => "intranet",
-                 authnotrequired => 0,
-                 flagsrequired => {acquisition => 'order_receive'},
-                 debug => 1,
-    });
-       
-    my @datas;
-    my $search   = $input->param('search') || '';
-    my $supplier = $input->param('supplierid') || '';
-    my $basketno = $input->param('basketno') || '';
-    my $orderno  = $input->param('orderno') || '';
-
-    my $orders = SearchOrder($orderno, $search, $supplier, $basketno);
-    foreach my $order (@$orders){
-        if($order->{quantityreceived} < $order->{quantity}){
-            my $data = {};
-            
-            $data->{basketno} = $order->{basketno};
-            $data->{ordernumber} = $order->{ordernumber};
-            $data->{title} = $order->{title};
-            $data->{author} = $order->{author};
-            $data->{isbn} = $order->{isbn};
-            $data->{booksellerid} = $order->{booksellerid};
-            $data->{biblionumber} = $order->{biblionumber};
-            $data->{freight} = $order->{freight};
-            $data->{quantity} = $order->{quantity};
-            $data->{ecost} = $order->{ecost};
-            $data->{ordertotal} = sprintf("%.2f",$order->{ecost}*$order->{quantity});
-            push @datas, $data;
-        }
-    }
-    
-    my $json_text = to_json(\@datas);
-    $template->param(return => $json_text);
-    output_html_with_http_headers $input, $cookie, $template->output;
-    exit;
-}
-
 my ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "acqui/parcel.tmpl",
                  query => $input,
@@ -140,41 +100,11 @@ 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($op eq 'cancelreceipt') {
+    my $ordernumber = $input->param('ordernumber');
+    my $parent_ordernumber = CancelReceipt($ordernumber);
+    unless($parent_ordernumber) {
+        $template->param(error_cancelling_receipt => 1);
     }
 }
 
@@ -192,7 +122,7 @@ if( scalar(@rcv_err) ) {
 }
 
 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
-my @parcelitems   = GetParcel($supplierid, $invoice, $datereceived->output('iso'));
+my @parcelitems   = GetParcel($booksellerid, $invoice, $datereceived->output('iso'));
 my $countlines    = scalar @parcelitems;
 my $totalprice    = 0;
 my $totalfreight  = 0;
@@ -211,11 +141,24 @@ for (my $i = 0 ; $i < $countlines ; $i++) {
     $line{invoice} = $invoice;
     $line{gst}     = $gst;
     $line{total} = sprintf($cfstr, $total);
-    $line{supplierid} = $supplierid;
-    push @loop_received, \%line;
+    $line{booksellerid} = $booksellerid;
     $totalprice += $parcelitems[$i]->{'unitprice'};
     $line{unitprice} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
 
+    my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
+    $line{suggestionid}         = $suggestion->{suggestionid};
+    $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
+    $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
+
+    if ( $line{parent_ordernumber} != $line{ordernumber} ) {
+        if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
+            @parcelitems )
+        {
+            $line{cannot_cancel} = 1;
+        }
+    }
+
+    push @loop_received, \%line;
     #double FIXME - totalfreight is redefined later.
 
 # FIXME - each order in a  parcel holds the freight for the whole parcel. This means if you receive a parcel with items from multiple budgets, you'll see the freight charge in each budget..
@@ -227,7 +170,19 @@ for (my $i = 0 ; $i < $countlines ; $i++) {
     $tototal       += $total;
 }
 
-my $pendingorders = GetPendingOrders($supplierid);
+# We get the pending orders either all or filtered
+my $pendingorders;
+if($input->param('op') eq "search"){
+    my $search   = $input->param('summaryfilter') || '';
+    my $ean      = $input->param('eanfilter') || '';
+    my $basketno = $input->param('basketfilter') || '';
+    my $orderno  = $input->param('orderfilter') || '';
+    my $grouped;
+    my $owner;
+    $pendingorders = GetPendingOrders($booksellerid,$grouped,$owner,$basketno,$orderno,$search,$ean);
+}else{
+    $pendingorders = GetPendingOrders($booksellerid);
+}
 my $countpendings = scalar @$pendingorders;
 
 # pending orders totals
@@ -237,6 +192,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;
@@ -250,12 +206,46 @@ for (my $i = 0 ; $i < $countpendings ; $i++) {
     $line{invoice} = $invoice;
     $line{gst} = $gst;
     $line{total} = $total;
-    $line{supplierid} = $supplierid;
+    $line{booksellerid} = $booksellerid;
     $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;
+        }
+    }
+
+    my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
+    $line{suggestionid}         = $suggestion->{suggestionid};
+    $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
+    $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
+
+    # 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;
-
 my $count = $countpendings;
 
 if ($count>$resultsperpage){
@@ -298,7 +288,7 @@ $template->param(
     invoicedatereceived   => $datereceived->output('iso'),
     formatteddatereceived => $datereceived->output(),
     name                  => $bookseller->{'name'},
-    supplierid            => $supplierid,
+    booksellerid            => $booksellerid,
     gst                   => $gst,
     freight               => $freight,
     invoice               => $invoice,
@@ -318,5 +308,7 @@ $template->param(
     totalPqtyrcvd         => $totalPqtyrcvd,
     totalPecost           => sprintf("%.2f", $totalPecost),
     resultsperpage        => $resultsperpage,
+    (uc(C4::Context->preference("marcflavour"))) => 1
 );
 output_html_with_http_headers $input, $cookie, $template->output;