improvement/bugfix : search budget
[koha_fer] / acqui / parcel.pl
old mode 100644 (file)
new mode 100755 (executable)
index c0533cf..15a08e7
@@ -1,12 +1,10 @@
 #!/usr/bin/perl
 
-# $Id$
-
 #script to recieve orders
-#written by chris@katipo.co.nz 24/2/2000
 
 
 # Copyright 2000-2002 Katipo Communications
+# Copyright 2008-2009 BibLibre SARL
 #
 # This file is part of Koha.
 #
@@ -23,7 +21,6 @@
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
-
 =head1 NAME
 
 parcel.pl
@@ -54,134 +51,153 @@ To filter the results list on this given date.
 =back
 
 =cut
-use strict;
+
 use C4::Auth;
 use C4::Acquisition;
 use C4::Bookseller;
-use C4::Bookfund;
 use C4::Biblio;
 use CGI;
-use C4::Interface::CGI::Output;
-use C4::Date;
-use Time::localtime;
+use C4::Output;
+use C4::Dates qw/format_date format_date_in_iso/;
 
+use strict;
 
 my $input=new CGI;
 my $supplierid=$input->param('supplierid');
-my $basketno=$input->param('basketno');
-my @booksellers=GetBookSeller($supplierid);
-my $count = scalar @booksellers;
+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 $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;
+my $code            = $input->param('code');
+my @rcv_err         = $input->param('error');
+my @rcv_err_barcode = $input->param('error_bc');
 
-my @datetoday = localtime();
-my $date = (1900+$datetoday[5])."-".($datetoday[4]+1)."-". $datetoday[3];
 my ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "acqui/parcel.tmpl",
                  query => $input,
-                 type => "intranet",
+                                type => "intranet",
                  authnotrequired => 0,
-                 flagsrequired => {acquisition => 1},
+                 flagsrequired => {acquisition => 'order_receive'},
                  debug => 1,
 });
 
-my @booksellers=GetBookSeller($supplierid);
-
-my $gstreg=$booksellers[0]->{gstreg};
-my $incgst=$booksellers[0]->{'invoiceincgst'};
-my $invcurrency=$booksellers[0]->{'invoiceprice'};
-my $discount=$booksellers[0]->{'discount'};
-my $currencyrate;
-# get currencies (for exchange rates calcs if needed)
-my @rates = GetCurrencies();
-my $count = scalar @rates;
-
-for ( my $i = 0 ; $i < $count ; $i++ ) {
- if ($rates[$i]->{'currency'} eq $invcurrency){
-    $currencyrate     = $rates[$i]->{'rate'};
-   }
+# If receiving error, report the error (coming from finishrecieve.pl(sic)).
+if( scalar(@rcv_err) ) {
+       my $cnt=0;
+       my $error_loop;
+       for my $err (@rcv_err) {
+               push @$error_loop, { "error_$err" => 1 , barcode => $rcv_err_barcode[$cnt] };
+               $cnt++;
+       }
+       $template->param( receive_error => 1 ,
+                                               error_loop => $error_loop,
+                                       );
 }
-my $me=C4::Context->userenv;
-my $user=$me->{'cardnumber'};
-my $totalprice=0;
-my $totalfreight=0;
-my $totalquantity=0;
-my $totaldiscount=0;
+
+my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
+my @parcelitems   = GetParcel($supplierid, $invoice, $datereceived->output('iso'));
+my $countlines    = scalar @parcelitems;
+my $totalprice    = 0;
+my $totalfreight  = 0;
+my $totalquantity = 0;
 my $total;
 my $tototal;
-my $toggle;
-my $totalgst;
-my $totaltoreceive;
-my $totaltoprice;
-my $totaltogst;
-my $totaltodiscount;
-my @loop_orders;
-my $countpendings;
-my $invoice;
-##Receiving a single basket or all baskets of a supplier
-unless($basketno){
-my $pendingorders = GetPendingOrders($supplierid);
-$countpendings = scalar @$pendingorders;
-foreach my $pendingorder (@$pendingorders){
- my @orders=GetOrders($pendingorder->{basketno});
-  foreach my $order(@orders){
-  $order->{toreceive}=$order->{quantity} - $order->{quantityreceived};
-  $totalquantity+=$order->{quantity};
-  $totaltoreceive+=$order->{toreceive};
-  $totalprice+=$order->{rrp}*$order->{quantity};
-  $totaltoprice+=$order->{rrp}*$order->{toreceive};
-  $totalgst+=(($order->{rrp}*$order->{quantity}) -($order->{rrp}*$order->{quantity}*$order->{discount}/100))* $order->{gst}/100;
-  $totaltogst+=(($order->{rrp}*$order->{toreceive}) -($order->{rrp}*$order->{toreceive}*$order->{discount}/100))* $order->{gst}/100;
-  $totaldiscount +=$order->{rrp}*$order->{quantity}*$order->{discount}/100;
-  $totaltodiscount +=$order->{rrp}*$order->{toreceive}*$order->{discount}/100;
-  $order->{actualrrp}=sprintf( "%.2f",$order->{rrp}/$currencyrate);
-       push @loop_orders, $order;
-  }    
+my @loop_received = ();
+
+for (my $i = 0 ; $i < $countlines ; $i++) {
+
+    #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'};   #weird, are the freight fees counted by book? (pierre)
+    $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'};    #weird, are the freight fees counted by book? (pierre)
+    $parcelitems[$i]->{'unitprice'} += 0;
+    my %line;
+    %line          = %{ $parcelitems[$i] };
+    $line{invoice} = $invoice;
+    $line{gst}     = $gst;
+    $line{total} = sprintf($cfstr, $total);
+    $line{supplierid} = $supplierid;
+    push @loop_received, \%line;
+    $totalprice += $parcelitems[$i]->{'unitprice'};
+    $line{unitprice} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
+
+    #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..
+    if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
+        warn "FREIGHT CHARGE MISMATCH!!";
+    }
+    $totalfreight = $parcelitems[$i]->{'freight'};
+    $totalquantity += $parcelitems[$i]->{'quantityreceived'};
+    $tototal       += $total;
 }
-  
-}else{
-## one basket
-$countpendings=1;
-
-my @orders=GetOrders($basketno);
-  foreach my $order(@orders){
-$invoice=$order->{booksellerinvoicenumber} unless $invoice;
-  $order->{toreceive}=$order->{quantity} - $order->{quantityreceived};
-  $totalquantity+=$order->{quantity};
-  $totaltoreceive+=$order->{toreceive};
-  $totalprice+=$order->{rrp}*$order->{quantity};
-  $totaltoprice+=$order->{rrp}*$order->{toreceive};
-  $totalgst+=(($order->{rrp}*$order->{quantity}) -($order->{rrp}*$order->{quantity}*$order->{discount}/100))* $order->{gst}/100;
-  $totaltogst+=(($order->{rrp}*$order->{toreceive}) -($order->{rrp}*$order->{toreceive}*$order->{discount}/100))* $order->{gst}/100;
-  $totaldiscount +=$order->{rrp}*$order->{quantity}*$order->{discount}/100;
-  $totaltodiscount +=$order->{rrp}*$order->{toreceive}*$order->{discount}/100;
-  $order->{actualrrp}=sprintf( "%.2f",$order->{rrp}/$currencyrate);
-       push @loop_orders, $order;
-  }    
+
+my $pendingorders = GetPendingOrders($supplierid);
+my $countpendings = scalar @$pendingorders;
+
+# pending orders totals
+my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
+my $ordergrandtotal;
+my @loop_orders = ();
+for (my $i = 0 ; $i < $countpendings ; $i++) {
+    my %line;
+    if ($toggle==0){
+        $line{color}='#EEEEEE';
+        $toggle=1;
+    } else {
+            $line{color}='white';
+            $toggle=0;
+    }
+    %line = %{$pendingorders->[$i]};
+       $line{quantity}+=0;
+       $line{quantrem} = $line{quantity} - $line{quantityreceived};
+       $line{quantityreceived}+=0;
+       $line{unitprice}+=0;
+    $totalPunitprice += $line{unitprice};
+    $totalPquantity +=$line{quantity};
+    $totalPqtyrcvd +=$line{quantityreceived};
+    $totalPecost += $line{ecost};
+    $line{ecost} = sprintf("%.2f",$line{ecost});
+    $line{ordertotal} = sprintf("%.2f",$line{ecost}*$line{quantity});
+    $line{unitprice} = sprintf("%.2f",$line{unitprice});
+    $line{invoice} = $invoice;
+    $line{gst} = $gst;
+    $line{total} = $total;
+    $line{supplierid} = $supplierid;
+    $ordergrandtotal += $line{ecost} * $line{quantity};
+    push @loop_orders, \%line;
 }
-undef $invcurrency if ($currencyrate ==1);
-
-$template->param( invoice=>$invoice,
-                        date => format_date($date),
-                        name => $booksellers[0]->{'name'},
-                        supplierid => $supplierid,
-                        countpending => $countpendings,
-                        loop_orders => \@loop_orders,
-                 user=>$user,
-                totalquantity=>$totalquantity,
-                totaltoreceive=>$totaltoreceive,
-                 totalprice=>sprintf( "%.2f",$totalprice),
-                totalactual =>sprintf( "%.2f",$totaltoprice/$currencyrate),
-                        totalgst=>sprintf( "%.2f",$totalgst),
-                        actualgst=>sprintf( "%.2f",$totaltogst/$currencyrate),
-               totaldiscount=>sprintf( "%.2f",$totaldiscount),
-               actualdiscount=>sprintf( "%.2f",$totaltodiscount/$currencyrate),        
-               total=>sprintf( "%.2f",$totalprice+$totalgst-$totaldiscount),
-               gstreg=>$gstreg,
-                            gstrate=>C4::Context->preference('gist')*100,
-               currencyrate=>$currencyrate,
-               incgst =>$incgst,
-               invcurrency=>$invcurrency ,
-                        intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
-        intranetstylesheet => C4::Context->preference("intranetstylesheet"),
-        IntranetNav => C4::Context->preference("IntranetNav"),
-                        );
+$freight = $totalfreight unless $freight;
+
+#$totalfreight=$freight;
+$tototal = $tototal + $freight;
+
+$template->param(
+    invoice               => $invoice,
+    datereceived          => $datereceived->output('iso'),
+    invoicedatereceived   => $datereceived->output('iso'),
+    formatteddatereceived => $datereceived->output(),
+    name                  => $bookseller->{'name'},
+    supplierid            => $supplierid,
+    gst                   => $gst,
+    freight               => $freight,
+    invoice               => $invoice,
+    countreceived         => $countlines,
+    loop_received         => \@loop_received,
+    countpending          => $countpendings,
+    loop_orders           => \@loop_orders,
+    totalprice            => sprintf($cfstr, $totalprice),
+    totalfreight          => $totalfreight,
+    totalquantity         => $totalquantity,
+    tototal               => sprintf($cfstr, $tototal),
+    ordergrandtotal       => sprintf($cfstr, $ordergrandtotal),
+    gst                   => $gst,
+    grandtot              => sprintf($cfstr, $tototal + $gst),
+    totalPunitprice       => sprintf("%.2f", $totalPunitprice),
+    totalPquantity        => $totalPquantity,
+    totalPqtyrcvd         => $totalPqtyrcvd,
+    totalPecost           => sprintf("%.2f", $totalPecost),
+);
 output_html_with_http_headers $input, $cookie, $template->output;