Merge remote-tracking branch 'origin/new/bug_8293'
[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
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 =head1 NAME
25
26 parcel.pl
27
28 =head1 DESCRIPTION
29
30 This script shows all orders receipt or pending for a given supplier.
31 It allows to write an order as 'received' when he arrives.
32
33 =head1 CGI PARAMETERS
34
35 =over 4
36
37 =item booksellerid
38
39 To know the supplier this script has to show orders.
40
41 =item code
42
43 is the bookseller invoice number.
44
45 =item freight
46
47
48 =item gst
49
50
51 =item datereceived
52
53 To filter the results list on this given date.
54
55 =back
56
57 =cut
58
59 use strict;
60 #use warnings; FIXME - Bug 2505
61 use C4::Auth;
62 use C4::Acquisition;
63 use C4::Budgets;
64 use C4::Bookseller qw/ GetBookSellerFromId /;
65 use C4::Biblio;
66 use C4::Items;
67 use CGI;
68 use C4::Output;
69 use C4::Dates qw/format_date format_date_in_iso/;
70 use C4::Suggestions;
71 use JSON;
72
73 my $input=new CGI;
74 my $booksellerid=$input->param('booksellerid');
75 my $bookseller=GetBookSellerFromId($booksellerid);
76
77 my $invoice=$input->param('invoice') || '';
78 my $freight=$input->param('freight');
79 my $input_gst = ($input->param('gst') eq '' ? undef : $input->param('gst'));
80 my $gst= $input_gst // $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
81 my $datereceived =  ($input->param('op') eq ('new' or "search")) ? C4::Dates->new($input->param('datereceived'))
82                     :  C4::Dates->new($input->param('datereceived'), 'iso');
83 $datereceived = C4::Dates->new() unless $datereceived;
84 my $code            = $input->param('code');
85 my @rcv_err         = $input->param('error');
86 my @rcv_err_barcode = $input->param('error_bc');
87 my $startfrom=$input->param('startfrom');
88 my $resultsperpage = $input->param('resultsperpage');
89 $resultsperpage = 20 unless ($resultsperpage);
90 $startfrom=0 unless ($startfrom);
91
92 my ($template, $loggedinuser, $cookie)
93     = get_template_and_user({template_name => "acqui/parcel.tmpl",
94                  query => $input,
95                                  type => "intranet",
96                  authnotrequired => 0,
97                  flagsrequired => {acquisition => 'order_receive'},
98                  debug => 1,
99 });
100
101 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
102 if( scalar(@rcv_err) ) {
103         my $cnt=0;
104         my $error_loop;
105         for my $err (@rcv_err) {
106                 push @$error_loop, { "error_$err" => 1 , barcode => $rcv_err_barcode[$cnt] };
107                 $cnt++;
108         }
109         $template->param( receive_error => 1 ,
110                                                 error_loop => $error_loop,
111                                         );
112 }
113
114 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
115 my @parcelitems   = GetParcel($booksellerid, $invoice, $datereceived->output('iso'));
116 my $countlines    = scalar @parcelitems;
117 my $totalprice    = 0;
118 my $totalfreight  = 0;
119 my $totalquantity = 0;
120 my $total;
121 my $tototal;
122 my @loop_received = ();
123
124 for (my $i = 0 ; $i < $countlines ; $i++) {
125
126     #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'};   #weird, are the freight fees counted by book? (pierre)
127     $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'};    #weird, are the freight fees counted by book? (pierre)
128     $parcelitems[$i]->{'unitprice'} += 0;
129     my %line;
130     %line          = %{ $parcelitems[$i] };
131     $line{invoice} = $invoice;
132     $line{gst}     = $gst;
133     $line{total} = sprintf($cfstr, $total);
134     $line{booksellerid} = $booksellerid;
135     push @loop_received, \%line;
136     $totalprice += $parcelitems[$i]->{'unitprice'};
137     $line{unitprice} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
138
139     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
140     $line{suggestionid}         = $suggestion->{suggestionid};
141     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
142     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
143
144     #double FIXME - totalfreight is redefined later.
145
146 # 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..
147     if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
148         warn "FREIGHT CHARGE MISMATCH!!";
149     }
150     $totalfreight = $parcelitems[$i]->{'freight'};
151     $totalquantity += $parcelitems[$i]->{'quantityreceived'};
152     $tototal       += $total;
153 }
154
155 # We get the pending orders either all or filtered
156 my $pendingorders;
157 if($input->param('op') eq "search"){
158     my $search   = $input->param('summaryfilter') || '';
159     my $ean      = $input->param('eanfilter') || '';
160     my $basketno = $input->param('basketfilter') || '';
161     my $orderno  = $input->param('orderfilter') || '';
162     my $grouped;
163     my $owner;
164     $pendingorders = GetPendingOrders($booksellerid,$grouped,$owner,$basketno,$orderno,$search,$ean);
165 }else{
166     $pendingorders = GetPendingOrders($booksellerid);
167 }
168 my $countpendings = scalar @$pendingorders;
169
170 # pending orders totals
171 my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
172 my $ordergrandtotal;
173 my @loop_orders = ();
174 for (my $i = 0 ; $i < $countpendings ; $i++) {
175     my %line;
176     %line = %{$pendingorders->[$i]};
177    
178     $line{quantity}+=0;
179     $line{quantityreceived}+=0;
180     $line{unitprice}+=0;
181     $totalPunitprice += $line{unitprice};
182     $totalPquantity +=$line{quantity};
183     $totalPqtyrcvd +=$line{quantityreceived};
184     $totalPecost += $line{ecost};
185     $line{ecost} = sprintf("%.2f",$line{ecost});
186     $line{ordertotal} = sprintf("%.2f",$line{ecost}*$line{quantity});
187     $line{unitprice} = sprintf("%.2f",$line{unitprice});
188     $line{invoice} = $invoice;
189     $line{gst} = $gst;
190     $line{total} = $total;
191     $line{booksellerid} = $booksellerid;
192     $ordergrandtotal += $line{ecost} * $line{quantity};
193     
194     my $biblionumber = $line{'biblionumber'};
195     my $countbiblio = CountBiblioInOrders($biblionumber);
196     my $ordernumber = $line{'ordernumber'};
197     my @subscriptions = GetSubscriptionsId ($biblionumber);
198     my $itemcount = GetItemsCount($biblionumber);
199     my $holds  = GetHolds ($biblionumber);
200     my @items = GetItemnumbersFromOrder( $ordernumber );
201     my $itemholds;
202     foreach my $item (@items){
203         my $nb = GetItemHolds($biblionumber, $item);
204         if ($nb){
205             $itemholds += $nb;
206         }
207     }
208
209     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
210     $line{suggestionid}         = $suggestion->{suggestionid};
211     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
212     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
213
214     # if the biblio is not in other orders and if there is no items elsewhere and no subscriptions and no holds we can then show the link "Delete order and Biblio" see bug 5680
215     $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
216     $line{items}                = ($itemcount) - (scalar @items);
217     $line{left_item}            = 1 if $line{items} >= 1;
218     $line{left_biblio}          = 1 if $countbiblio > 1;
219     $line{biblios}              = $countbiblio - 1;
220     $line{left_subscription}    = 1 if scalar @subscriptions >= 1;
221     $line{subscriptions}        = scalar @subscriptions;
222     $line{left_holds}           = 1 if $holds >= 1;
223     $line{left_holds_on_order}  = 1 if $line{left_holds} == 1 && ($line{items} == 0 || $itemholds );
224     $line{holds}                = $holds;
225     $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
226     
227     
228     push @loop_orders, \%line if ($i >= $startfrom and $i < $startfrom + $resultsperpage);
229 }
230 $freight = $totalfreight unless $freight;
231 my $count = $countpendings;
232
233 if ($count>$resultsperpage){
234     my $displaynext=0;
235     my $displayprev=$startfrom;
236     if(($count - ($startfrom+$resultsperpage)) > 0 ) {
237         $displaynext = 1;
238     }
239
240     my @numbers = ();
241     for (my $i=1; $i<$count/$resultsperpage+1; $i++) {
242             my $highlight=0;
243             ($startfrom/$resultsperpage==($i-1)) && ($highlight=1);
244             push @numbers, { number => $i,
245                 highlight => $highlight ,
246                 startfrom => ($i-1)*$resultsperpage};
247     }
248
249     my $from = $startfrom*$resultsperpage+1;
250     my $to;
251     if($count < (($startfrom+1)*$resultsperpage)){
252         $to = $count;
253     } else {
254         $to = (($startfrom+1)*$resultsperpage);
255     }
256     $template->param(numbers=>\@numbers,
257                      displaynext=>$displaynext,
258                      displayprev=>$displayprev,
259                      nextstartfrom=>(($startfrom+$resultsperpage<$count)?$startfrom+$resultsperpage:$count),
260                      prevstartfrom=>(($startfrom-$resultsperpage>0)?$startfrom-$resultsperpage:0)
261                     );
262 }
263
264 #$totalfreight=$freight;
265 $tototal = $tototal + $freight;
266
267 $template->param(
268     invoice               => $invoice,
269     datereceived          => $datereceived->output('iso'),
270     invoicedatereceived   => $datereceived->output('iso'),
271     formatteddatereceived => $datereceived->output(),
272     name                  => $bookseller->{'name'},
273     booksellerid            => $booksellerid,
274     gst                   => $gst,
275     freight               => $freight,
276     invoice               => $invoice,
277     countreceived         => $countlines,
278     loop_received         => \@loop_received,
279     countpending          => $countpendings,
280     loop_orders           => \@loop_orders,
281     totalprice            => sprintf($cfstr, $totalprice),
282     totalfreight          => $totalfreight,
283     totalquantity         => $totalquantity,
284     tototal               => sprintf($cfstr, $tototal),
285     ordergrandtotal       => sprintf($cfstr, $ordergrandtotal),
286     gst                   => $gst,
287     grandtot              => sprintf($cfstr, $tototal + $gst),
288     totalPunitprice       => sprintf("%.2f", $totalPunitprice),
289     totalPquantity        => $totalPquantity,
290     totalPqtyrcvd         => $totalPqtyrcvd,
291     totalPecost           => sprintf("%.2f", $totalPecost),
292     resultsperpage        => $resultsperpage,
293     (uc(C4::Context->preference("marcflavour"))) => 1
294 );
295 output_html_with_http_headers $input, $cookie, $template->output;
296