(mantis 1475) uncertains price, don't set if we find a price in MARC
[koha.git] / acqui / parcel.pl
1 #!/usr/bin/perl
2
3 #script to recieve orders
4
5
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2008-2009 BibLibre SARL
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 =head1 NAME
25
26 parcel.pl
27
28 =head1 DESCRIPTION
29 This script shows all orders receipt or pending for a given supplier.
30 It allows to write an order as 'received' when he arrives.
31
32 =head1 CGI PARAMETERS
33
34 =over 4
35
36 =item supplierid
37 To know the supplier this script has to show orders.
38
39 =item code
40 is the bookseller invoice number.
41
42 =item freight
43
44
45 =item gst
46
47
48 =item datereceived
49 To filter the results list on this given date.
50
51 =back
52
53 =cut
54
55 use C4::Auth;
56 use C4::Acquisition;
57 use C4::Bookseller;
58 use C4::Biblio;
59 use CGI;
60 use C4::Output;
61 use C4::Dates qw/format_date format_date_in_iso/;
62
63 use strict;
64
65 my $input=new CGI;
66 my $supplierid=$input->param('supplierid');
67 my $bookseller=GetBookSellerFromId($supplierid);
68
69 my $invoice=$input->param('invoice') || '';
70 my $freight=$input->param('freight');
71 my $gst= $input->param('gst') || $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
72 my $datereceived =  ($input->param('op') eq 'new') ? C4::Dates->new($input->param('datereceived')) 
73                                         :  C4::Dates->new($input->param('datereceived'), 'iso')   ;
74 $datereceived = C4::Dates->new() unless $datereceived;
75 my $code            = $input->param('code');
76 my @rcv_err         = $input->param('error');
77 my @rcv_err_barcode = $input->param('error_bc');
78
79 my ($template, $loggedinuser, $cookie)
80     = get_template_and_user({template_name => "acqui/parcel.tmpl",
81                  query => $input,
82                                  type => "intranet",
83                  authnotrequired => 0,
84                  flagsrequired => {acquisition => 'order_receive'},
85                  debug => 1,
86 });
87
88 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
89 if( scalar(@rcv_err) ) {
90         my $cnt=0;
91         my $error_loop;
92         for my $err (@rcv_err) {
93                 push @$error_loop, { "error_$err" => 1 , barcode => $rcv_err_barcode[$cnt] };
94                 $cnt++;
95         }
96         $template->param( receive_error => 1 ,
97                                                 error_loop => $error_loop,
98                                         );
99 }
100
101 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
102 my @parcelitems   = GetParcel($supplierid, $invoice, $datereceived->output('iso'));
103 my $countlines    = scalar @parcelitems;
104 my $totalprice    = 0;
105 my $totalfreight  = 0;
106 my $totalquantity = 0;
107 my $total;
108 my $tototal;
109 my @loop_received = ();
110
111 for (my $i = 0 ; $i < $countlines ; $i++) {
112
113     #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'};   #weird, are the freight fees counted by book? (pierre)
114     $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'};    #weird, are the freight fees counted by book? (pierre)
115     $parcelitems[$i]->{'unitprice'} += 0;
116     my %line;
117     %line          = %{ $parcelitems[$i] };
118     $line{invoice} = $invoice;
119     $line{gst}     = $gst;
120     $line{total} = sprintf($cfstr, $total);
121     $line{supplierid} = $supplierid;
122     push @loop_received, \%line;
123     $totalprice += $parcelitems[$i]->{'unitprice'};
124     $line{unitprice} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
125
126     #double FIXME - totalfreight is redefined later.
127
128 # 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..
129     if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
130         warn "FREIGHT CHARGE MISMATCH!!";
131     }
132     $totalfreight = $parcelitems[$i]->{'freight'};
133     $totalquantity += $parcelitems[$i]->{'quantityreceived'};
134     $tototal       += $total;
135 }
136
137 my $pendingorders = GetPendingOrders($supplierid);
138 my $countpendings = scalar @$pendingorders;
139
140 # pending orders totals
141 my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
142 my $ordergrandtotal;
143 my @loop_orders = ();
144 for (my $i = 0 ; $i < $countpendings ; $i++) {
145     my %line;
146     if ($toggle==0){
147         $line{color}='#EEEEEE';
148         $toggle=1;
149     } else {
150             $line{color}='white';
151             $toggle=0;
152     }
153     %line = %{$pendingorders->[$i]};
154         $line{quantity}+=0;
155         $line{quantrem} = $line{quantity} - $line{quantityreceived};
156         $line{quantityreceived}+=0;
157         $line{unitprice}+=0;
158     $totalPunitprice += $line{unitprice};
159     $totalPquantity +=$line{quantity};
160     $totalPqtyrcvd +=$line{quantityreceived};
161     $totalPecost += $line{ecost};
162     $line{ecost} = sprintf("%.2f",$line{ecost});
163     $line{ordertotal} = sprintf("%.2f",$line{ecost}*$line{quantity});
164     $line{unitprice} = sprintf("%.2f",$line{unitprice});
165     $line{invoice} = $invoice;
166     $line{gst} = $gst;
167     $line{total} = $total;
168     $line{supplierid} = $supplierid;
169     $ordergrandtotal += $line{ecost} * $line{quantity};
170     push @loop_orders, \%line;
171 }
172 $freight = $totalfreight unless $freight;
173
174 #$totalfreight=$freight;
175 $tototal = $tototal + $freight;
176
177 $template->param(
178     invoice               => $invoice,
179     datereceived          => $datereceived->output('iso'),
180     invoicedatereceived   => $datereceived->output('iso'),
181     formatteddatereceived => $datereceived->output(),
182     name                  => $bookseller->{'name'},
183     supplierid            => $supplierid,
184     gst                   => $gst,
185     freight               => $freight,
186     invoice               => $invoice,
187     countreceived         => $countlines,
188     loop_received         => \@loop_received,
189     countpending          => $countpendings,
190     loop_orders           => \@loop_orders,
191     totalprice            => sprintf($cfstr, $totalprice),
192     totalfreight          => $totalfreight,
193     totalquantity         => $totalquantity,
194     tototal               => sprintf($cfstr, $tototal),
195     ordergrandtotal       => sprintf($cfstr, $ordergrandtotal),
196     gst                   => $gst,
197     grandtot              => sprintf($cfstr, $tototal + $gst),
198     totalPunitprice       => sprintf("%.2f", $totalPunitprice),
199     totalPquantity        => $totalPquantity,
200     totalPqtyrcvd         => $totalPqtyrcvd,
201     totalPecost           => sprintf("%.2f", $totalPecost),
202 );
203 output_html_with_http_headers $input, $cookie, $template->output;