Merge commit 'koha-biblibre/master' into bl-acq
[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::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;
65
66 use strict;
67
68 my $input=new CGI;
69 my $supplierid=$input->param('supplierid');
70 my $bookseller=GetBookSellerFromId($supplierid);
71
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');
81
82 my $startfrom=$input->param('startfrom');
83 my $resultsperpage = $input->param('resultsperpage');
84 $resultsperpage = 20 unless ($resultsperpage);
85 $startfrom=0 unless ($startfrom);
86
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     });
96        
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') || '';
102
103     my $orders = SearchOrder($orderno, $search, $supplier, $basketno);
104     foreach my $order (@$orders){
105         if($order->{quantityreceived} < $order->{quantity}){
106             my $data = {};
107             
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;
119         }
120     }
121     
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;
126 }
127
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,
135 });
136
137 my $action = $input->param('action');
138 my $ordernumber = $input->param('ordernumber');
139 my $biblionumber = $input->param('biblionumber');
140
141 # If canceling an order
142 if ($action eq "cancelorder") {
143
144     my $error_delitem;
145     my $error_delbiblio;
146
147     # We delete the order
148     DelOrder($biblionumber, $ordernumber);
149
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; }
156     }
157
158     # We get the number of remaining items
159     my $itemcount = GetItemsCount($biblionumber);
160     
161     # If there are no items left,
162     if ($itemcount eq 0) {
163         # We delete the record
164         $error_delbiblio = DelBiblio($biblionumber);    
165     }
166
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);
174     }
175 }
176
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++;
184         }
185         $template->param( receive_error => 1 ,
186                                                 error_loop => $error_loop,
187                                         );
188 }
189
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 = ();
199
200 for (my $i = 0 ; $i < $countlines ; $i++) {
201
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'});
214
215     #double FIXME - totalfreight is redefined later.
216
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!!";
220     }
221     $totalfreight = $parcelitems[$i]->{'freight'};
222     $totalquantity += $parcelitems[$i]->{'quantityreceived'};
223     $tototal       += $total;
224 }
225
226 my $pendingorders = GetPendingOrders($supplierid);
227 my $countpendings = scalar @$pendingorders;
228
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);
253 }
254 $freight = $totalfreight unless $freight;
255
256 my $count = $countpendings;
257
258 if ($count>$resultsperpage){
259     my $displaynext=0;
260     my $displayprev=$startfrom;
261     if(($count - ($startfrom+$resultsperpage)) > 0 ) {
262         $displaynext = 1;
263     }
264
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};
272     }
273
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);
280     }
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)
286                     );
287 }
288
289 #$totalfreight=$freight;
290 $tototal = $tototal + $freight;
291
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,
318 );
319 output_html_with_http_headers $input, $cookie, $template->output;