MT 1487 : Ability to cancel orders when receiving shipments
[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
65 use strict;
66
67 my $input=new CGI;
68 my $supplierid=$input->param('supplierid');
69 my $bookseller=GetBookSellerFromId($supplierid);
70
71 my $invoice=$input->param('invoice') || '';
72 my $freight=$input->param('freight');
73 my $gst= $input->param('gst') || $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
74 my $datereceived =  ($input->param('op') eq 'new') ? C4::Dates->new($input->param('datereceived')) 
75                                         :  C4::Dates->new($input->param('datereceived'), 'iso')   ;
76 $datereceived = C4::Dates->new() unless $datereceived;
77 my $code            = $input->param('code');
78 my @rcv_err         = $input->param('error');
79 my @rcv_err_barcode = $input->param('error_bc');
80
81 my $startfrom=$input->param('startfrom');
82 my $resultsperpage = $input->param('resultsperpage');
83 $resultsperpage = 20 unless ($resultsperpage);
84 $startfrom=0 unless ($startfrom);
85
86 my ($template, $loggedinuser, $cookie)
87     = get_template_and_user({template_name => "acqui/parcel.tmpl",
88                  query => $input,
89                                  type => "intranet",
90                  authnotrequired => 0,
91                  flagsrequired => {acquisition => 'order_receive'},
92                  debug => 1,
93 });
94
95 my $action = $input->param('action');
96 my $ordernumber = $input->param('ordernumber');
97 my $biblionumber = $input->param('biblionumber');
98
99 # If canceling an order
100 if ($action eq "cancelorder") {
101
102     my $error_delitem;
103     my $error_delbiblio;
104
105     # We delete the order
106     DelOrder($biblionumber, $ordernumber);
107
108     # We delete all the items related to this order
109     my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
110     foreach (@itemnumbers) {
111         my $delcheck = DelItemCheck(C4::Context->dbh, $biblionumber, $_);
112         # (should always success, as no issue should exist on item on order)
113         if ($delcheck != 1) { $error_delitem = 1; }
114     }
115
116     # We get the number of remaining items
117     my $itemcount = GetItemsCount($biblionumber);
118     
119     # If there are no items left,
120     if ($itemcount eq 0) {
121         # We delete the record
122         $error_delbiblio = DelBiblio($biblionumber);    
123     }
124
125     if ($error_delitem || $error_delbiblio) {
126         warn $error_delitem;
127         warn $error_delbiblio;
128         if ($error_delitem)   { $template->param(error_delitem => 1); }
129         if ($error_delbiblio) { $template->param(error_delbiblio => 1); }
130     } else {
131         $template->param(success_delorder => 1);
132     }
133 }
134
135 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
136 if( scalar(@rcv_err) ) {
137         my $cnt=0;
138         my $error_loop;
139         for my $err (@rcv_err) {
140                 push @$error_loop, { "error_$err" => 1 , barcode => $rcv_err_barcode[$cnt] };
141                 $cnt++;
142         }
143         $template->param( receive_error => 1 ,
144                                                 error_loop => $error_loop,
145                                         );
146 }
147
148 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
149 my @parcelitems   = GetParcel($supplierid, $invoice, $datereceived->output('iso'));
150 my $countlines    = scalar @parcelitems;
151 my $totalprice    = 0;
152 my $totalfreight  = 0;
153 my $totalquantity = 0;
154 my $total;
155 my $tototal;
156 my @loop_received = ();
157
158 for (my $i = 0 ; $i < $countlines ; $i++) {
159
160     #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'};   #weird, are the freight fees counted by book? (pierre)
161     $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'};    #weird, are the freight fees counted by book? (pierre)
162     $parcelitems[$i]->{'unitprice'} += 0;
163     my %line;
164     %line          = %{ $parcelitems[$i] };
165     $line{invoice} = $invoice;
166     $line{gst}     = $gst;
167     $line{total} = sprintf($cfstr, $total);
168     $line{supplierid} = $supplierid;
169     push @loop_received, \%line;
170     $totalprice += $parcelitems[$i]->{'unitprice'};
171     $line{unitprice} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
172
173     #double FIXME - totalfreight is redefined later.
174
175 # 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..
176     if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
177         warn "FREIGHT CHARGE MISMATCH!!";
178     }
179     $totalfreight = $parcelitems[$i]->{'freight'};
180     $totalquantity += $parcelitems[$i]->{'quantityreceived'};
181     $tototal       += $total;
182 }
183
184 my $pendingorders = GetPendingOrders($supplierid);
185 my $countpendings = scalar @$pendingorders;
186
187 # pending orders totals
188 my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
189 my $ordergrandtotal;
190 my @loop_orders = ();
191 for (my $i = 0 ; $i < $countpendings ; $i++) {
192     my %line;
193     %line = %{$pendingorders->[$i]};
194     $line{quantity}+=0;
195     $line{quantrem} = $line{quantity} - $line{quantityreceived};
196     $line{quantityreceived}+=0;
197     $line{unitprice}+=0;
198     $totalPunitprice += $line{unitprice};
199     $totalPquantity +=$line{quantity};
200     $totalPqtyrcvd +=$line{quantityreceived};
201     $totalPecost += $line{ecost};
202     $line{ecost} = sprintf("%.2f",$line{ecost});
203     $line{ordertotal} = sprintf("%.2f",$line{ecost}*$line{quantity});
204     $line{unitprice} = sprintf("%.2f",$line{unitprice});
205     $line{invoice} = $invoice;
206     $line{gst} = $gst;
207     $line{total} = $total;
208     $line{supplierid} = $supplierid;
209     $ordergrandtotal += $line{ecost} * $line{quantity};
210     push @loop_orders, \%line if ($i >= $startfrom and $i < $startfrom + $resultsperpage);
211 }
212 $freight = $totalfreight unless $freight;
213
214 my $count = $countpendings;
215
216 if ($count>$resultsperpage){
217     my $displaynext=0;
218     my $displayprev=$startfrom;
219     if(($count - ($startfrom+$resultsperpage)) > 0 ) {
220         $displaynext = 1;
221     }
222
223     my @numbers = ();
224     for (my $i=1; $i<$count/$resultsperpage+1; $i++) {
225             my $highlight=0;
226             ($startfrom/$resultsperpage==($i-1)) && ($highlight=1);
227             push @numbers, { number => $i,
228                 highlight => $highlight ,
229                 startfrom => ($i-1)*$resultsperpage};
230     }
231
232     my $from = $startfrom*$resultsperpage+1;
233     my $to;
234     if($count < (($startfrom+1)*$resultsperpage)){
235         $to = $count;
236     } else {
237         $to = (($startfrom+1)*$resultsperpage);
238     }
239     $template->param(numbers=>\@numbers,
240                      displaynext=>$displaynext,
241                      displayprev=>$displayprev,
242                      nextstartfrom=>(($startfrom+$resultsperpage<$count)?$startfrom+$resultsperpage:$count),
243                      prevstartfrom=>(($startfrom-$resultsperpage>0)?$startfrom-$resultsperpage:0)
244                     );
245 }
246
247 #$totalfreight=$freight;
248 $tototal = $tototal + $freight;
249
250 $template->param(
251     invoice               => $invoice,
252     datereceived          => $datereceived->output('iso'),
253     invoicedatereceived   => $datereceived->output('iso'),
254     formatteddatereceived => $datereceived->output(),
255     name                  => $bookseller->{'name'},
256     supplierid            => $supplierid,
257     gst                   => $gst,
258     freight               => $freight,
259     invoice               => $invoice,
260     countreceived         => $countlines,
261     loop_received         => \@loop_received,
262     countpending          => $countpendings,
263     loop_orders           => \@loop_orders,
264     totalprice            => sprintf($cfstr, $totalprice),
265     totalfreight          => $totalfreight,
266     totalquantity         => $totalquantity,
267     tototal               => sprintf($cfstr, $tototal),
268     ordergrandtotal       => sprintf($cfstr, $ordergrandtotal),
269     gst                   => $gst,
270     grandtot              => sprintf($cfstr, $tototal + $gst),
271     totalPunitprice       => sprintf("%.2f", $totalPunitprice),
272     totalPquantity        => $totalPquantity,
273     totalPqtyrcvd         => $totalPqtyrcvd,
274     totalPecost           => sprintf("%.2f", $totalPecost),
275     resultsperpage        => $resultsperpage,
276 );
277 output_html_with_http_headers $input, $cookie, $template->output;