Changes to the appearance and behavior of the cart in the staff client (see Bug 3871)
[koha.git] / acqui / parcel.pl
blob7b93a158f111dc913ad0d701adc946994e900217
1 #!/usr/bin/perl
3 #script to recieve orders
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2008-2009 BibLibre SARL
9 # This file is part of Koha.
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.
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.
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
24 =head1 NAME
26 parcel.pl
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.
32 =head1 CGI PARAMETERS
34 =over 4
36 =item supplierid
37 To know the supplier this script has to show orders.
39 =item code
40 is the bookseller invoice number.
42 =item freight
45 =item gst
48 =item datereceived
49 To filter the results list on this given date.
51 =back
53 =cut
55 use C4::Auth;
56 use C4::Acquisition;
57 use C4::Budgets;
58 use C4::Bookseller;
59 use C4::Biblio;
60 use C4::Items;
61 use CGI;
62 use C4::Output;
63 use C4::Dates qw/format_date format_date_in_iso/;
64 use JSON;
66 use strict;
68 my $input=new CGI;
69 my $supplierid=$input->param('supplierid');
70 my $bookseller=GetBookSellerFromId($supplierid);
72 my $invoice=$input->param('invoice') || '';
73 my $freight=$input->param('freight');
74 my $gst= $input->param('gst') || $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
75 my $datereceived = ($input->param('op') eq 'new') ? C4::Dates->new($input->param('datereceived'))
76 : C4::Dates->new($input->param('datereceived'), 'iso') ;
77 $datereceived = C4::Dates->new() unless $datereceived;
78 my $code = $input->param('code');
79 my @rcv_err = $input->param('error');
80 my @rcv_err_barcode = $input->param('error_bc');
82 my $startfrom=$input->param('startfrom');
83 my $resultsperpage = $input->param('resultsperpage');
84 $resultsperpage = 20 unless ($resultsperpage);
85 $startfrom=0 unless ($startfrom);
87 if($input->param('format') eq "json"){
88 my ($template, $loggedinuser, $cookie)
89 = get_template_and_user({template_name => "acqui/ajax.tmpl",
90 query => $input,
91 type => "intranet",
92 authnotrequired => 0,
93 flagsrequired => {acquisition => 'order_receive'},
94 debug => 1,
95 });
97 my @datas;
98 my $search = $input->param('search') || '';
99 my $supplier = $input->param('supplierid') || '';
100 my $basketno = $input->param('basketno') || '';
101 my $orderno = $input->param('orderno') || '';
103 my $orders = SearchOrder($orderno, $search, $supplier, $basketno);
104 foreach my $order (@$orders){
105 if($order->{quantityreceived} < $order->{quantity}){
106 my $data = {};
108 $data->{basketno} = $order->{basketno};
109 $data->{ordernumber} = $order->{ordernumber};
110 $data->{title} = $order->{title};
111 $data->{author} = $order->{author};
112 $data->{biblionumber} = $order->{biblionumber};
113 $data->{freight} = $order->{freight};
114 $data->{quantrem} = $order->{quantity} - $order->{quantityreceived};
115 $data->{quantity} = $order->{quantity};
116 $data->{ecost} = $order->{ecost};
117 $data->{ordertotal} = sprintf("%.2f",$order->{ecost}*$order->{quantity});
118 push @datas, $data;
122 my $json_text = to_json(\@datas);
123 $template->param(return => $json_text);
124 output_html_with_http_headers $input, $cookie, $template->output;
125 exit;
128 my ($template, $loggedinuser, $cookie)
129 = get_template_and_user({template_name => "acqui/parcel.tmpl",
130 query => $input,
131 type => "intranet",
132 authnotrequired => 0,
133 flagsrequired => {acquisition => 'order_receive'},
134 debug => 1,
137 my $action = $input->param('action');
138 my $ordernumber = $input->param('ordernumber');
139 my $biblionumber = $input->param('biblionumber');
141 # If canceling an order
142 if ($action eq "cancelorder") {
144 my $error_delitem;
145 my $error_delbiblio;
147 # We delete the order
148 DelOrder($biblionumber, $ordernumber);
150 # We delete all the items related to this order
151 my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
152 foreach (@itemnumbers) {
153 my $delcheck = DelItemCheck(C4::Context->dbh, $biblionumber, $_);
154 # (should always success, as no issue should exist on item on order)
155 if ($delcheck != 1) { $error_delitem = 1; }
158 # We get the number of remaining items
159 my $itemcount = GetItemsCount($biblionumber);
161 # If there are no items left,
162 if ($itemcount eq 0) {
163 # We delete the record
164 $error_delbiblio = DelBiblio($biblionumber);
167 if ($error_delitem || $error_delbiblio) {
168 warn $error_delitem;
169 warn $error_delbiblio;
170 if ($error_delitem) { $template->param(error_delitem => 1); }
171 if ($error_delbiblio) { $template->param(error_delbiblio => 1); }
172 } else {
173 $template->param(success_delorder => 1);
177 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
178 if( scalar(@rcv_err) ) {
179 my $cnt=0;
180 my $error_loop;
181 for my $err (@rcv_err) {
182 push @$error_loop, { "error_$err" => 1 , barcode => $rcv_err_barcode[$cnt] };
183 $cnt++;
185 $template->param( receive_error => 1 ,
186 error_loop => $error_loop,
190 my $cfstr = "%.2f"; # currency format string -- could get this from currency table.
191 my @parcelitems = GetParcel($supplierid, $invoice, $datereceived->output('iso'));
192 my $countlines = scalar @parcelitems;
193 my $totalprice = 0;
194 my $totalfreight = 0;
195 my $totalquantity = 0;
196 my $total;
197 my $tototal;
198 my @loop_received = ();
200 for (my $i = 0 ; $i < $countlines ; $i++) {
202 #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'}; #weird, are the freight fees counted by book? (pierre)
203 $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'}; #weird, are the freight fees counted by book? (pierre)
204 $parcelitems[$i]->{'unitprice'} += 0;
205 my %line;
206 %line = %{ $parcelitems[$i] };
207 $line{invoice} = $invoice;
208 $line{gst} = $gst;
209 $line{total} = sprintf($cfstr, $total);
210 $line{supplierid} = $supplierid;
211 push @loop_received, \%line;
212 $totalprice += $parcelitems[$i]->{'unitprice'};
213 $line{unitprice} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
215 #double FIXME - totalfreight is redefined later.
217 # 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..
218 if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
219 warn "FREIGHT CHARGE MISMATCH!!";
221 $totalfreight = $parcelitems[$i]->{'freight'};
222 $totalquantity += $parcelitems[$i]->{'quantityreceived'};
223 $tototal += $total;
226 my $pendingorders = GetPendingOrders($supplierid);
227 my $countpendings = scalar @$pendingorders;
229 # pending orders totals
230 my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
231 my $ordergrandtotal;
232 my @loop_orders = ();
233 for (my $i = 0 ; $i < $countpendings ; $i++) {
234 my %line;
235 %line = %{$pendingorders->[$i]};
236 $line{quantity}+=0;
237 $line{quantrem} = $line{quantity} - $line{quantityreceived};
238 $line{quantityreceived}+=0;
239 $line{unitprice}+=0;
240 $totalPunitprice += $line{unitprice};
241 $totalPquantity +=$line{quantity};
242 $totalPqtyrcvd +=$line{quantityreceived};
243 $totalPecost += $line{ecost};
244 $line{ecost} = sprintf("%.2f",$line{ecost});
245 $line{ordertotal} = sprintf("%.2f",$line{ecost}*$line{quantity});
246 $line{unitprice} = sprintf("%.2f",$line{unitprice});
247 $line{invoice} = $invoice;
248 $line{gst} = $gst;
249 $line{total} = $total;
250 $line{supplierid} = $supplierid;
251 $ordergrandtotal += $line{ecost} * $line{quantity};
252 push @loop_orders, \%line if ($i >= $startfrom and $i < $startfrom + $resultsperpage);
254 $freight = $totalfreight unless $freight;
256 my $count = $countpendings;
258 if ($count>$resultsperpage){
259 my $displaynext=0;
260 my $displayprev=$startfrom;
261 if(($count - ($startfrom+$resultsperpage)) > 0 ) {
262 $displaynext = 1;
265 my @numbers = ();
266 for (my $i=1; $i<$count/$resultsperpage+1; $i++) {
267 my $highlight=0;
268 ($startfrom/$resultsperpage==($i-1)) && ($highlight=1);
269 push @numbers, { number => $i,
270 highlight => $highlight ,
271 startfrom => ($i-1)*$resultsperpage};
274 my $from = $startfrom*$resultsperpage+1;
275 my $to;
276 if($count < (($startfrom+1)*$resultsperpage)){
277 $to = $count;
278 } else {
279 $to = (($startfrom+1)*$resultsperpage);
281 $template->param(numbers=>\@numbers,
282 displaynext=>$displaynext,
283 displayprev=>$displayprev,
284 nextstartfrom=>(($startfrom+$resultsperpage<$count)?$startfrom+$resultsperpage:$count),
285 prevstartfrom=>(($startfrom-$resultsperpage>0)?$startfrom-$resultsperpage:0)
289 #$totalfreight=$freight;
290 $tototal = $tototal + $freight;
292 $template->param(
293 invoice => $invoice,
294 datereceived => $datereceived->output('iso'),
295 invoicedatereceived => $datereceived->output('iso'),
296 formatteddatereceived => $datereceived->output(),
297 name => $bookseller->{'name'},
298 supplierid => $supplierid,
299 gst => $gst,
300 freight => $freight,
301 invoice => $invoice,
302 countreceived => $countlines,
303 loop_received => \@loop_received,
304 countpending => $countpendings,
305 loop_orders => \@loop_orders,
306 totalprice => sprintf($cfstr, $totalprice),
307 totalfreight => $totalfreight,
308 totalquantity => $totalquantity,
309 tototal => sprintf($cfstr, $tototal),
310 ordergrandtotal => sprintf($cfstr, $ordergrandtotal),
311 gst => $gst,
312 grandtot => sprintf($cfstr, $tototal + $gst),
313 totalPunitprice => sprintf("%.2f", $totalPunitprice),
314 totalPquantity => $totalPquantity,
315 totalPqtyrcvd => $totalPqtyrcvd,
316 totalPecost => sprintf("%.2f", $totalPecost),
317 resultsperpage => $resultsperpage,
319 output_html_with_http_headers $input, $cookie, $template->output;