Bug 7583 follow-up: Prevent users to cancel receipt if they can't
[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 $op = $input->param('op') // '';
82 my $datereceived = ( $op eq ('new' or 'search' ) )
83     ? C4::Dates->new($input->param('datereceived'))
84     :  C4::Dates->new($input->param('datereceived'), 'iso');
85 $datereceived = C4::Dates->new() unless $datereceived;
86 my $code            = $input->param('code');
87 my @rcv_err         = $input->param('error');
88 my @rcv_err_barcode = $input->param('error_bc');
89 my $startfrom=$input->param('startfrom');
90 my $resultsperpage = $input->param('resultsperpage');
91 $resultsperpage = 20 unless ($resultsperpage);
92 $startfrom=0 unless ($startfrom);
93
94 my ($template, $loggedinuser, $cookie)
95     = get_template_and_user({template_name => "acqui/parcel.tmpl",
96                  query => $input,
97                                  type => "intranet",
98                  authnotrequired => 0,
99                  flagsrequired => {acquisition => 'order_receive'},
100                  debug => 1,
101 });
102
103 if($op eq 'cancelreceipt') {
104     my $ordernumber = $input->param('ordernumber');
105     my $parent_ordernumber = CancelReceipt($ordernumber);
106     unless($parent_ordernumber) {
107         $template->param(error_cancelling_receipt => 1);
108     }
109 }
110
111 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
112 if( scalar(@rcv_err) ) {
113         my $cnt=0;
114         my $error_loop;
115         for my $err (@rcv_err) {
116                 push @$error_loop, { "error_$err" => 1 , barcode => $rcv_err_barcode[$cnt] };
117                 $cnt++;
118         }
119         $template->param( receive_error => 1 ,
120                                                 error_loop => $error_loop,
121                                         );
122 }
123
124 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
125 my @parcelitems   = GetParcel($booksellerid, $invoice, $datereceived->output('iso'));
126 my $countlines    = scalar @parcelitems;
127 my $totalprice    = 0;
128 my $totalfreight  = 0;
129 my $totalquantity = 0;
130 my $total;
131 my $tototal;
132 my @loop_received = ();
133
134 for (my $i = 0 ; $i < $countlines ; $i++) {
135
136     #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'};   #weird, are the freight fees counted by book? (pierre)
137     $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'};    #weird, are the freight fees counted by book? (pierre)
138     $parcelitems[$i]->{'unitprice'} += 0;
139     my %line;
140     %line          = %{ $parcelitems[$i] };
141     $line{invoice} = $invoice;
142     $line{gst}     = $gst;
143     $line{total} = sprintf($cfstr, $total);
144     $line{booksellerid} = $booksellerid;
145     $totalprice += $parcelitems[$i]->{'unitprice'};
146     $line{unitprice} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
147
148     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
149     $line{suggestionid}         = $suggestion->{suggestionid};
150     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
151     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
152
153     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
154         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
155             @parcelitems )
156         {
157             $line{cannot_cancel} = 1;
158         }
159     }
160
161     push @loop_received, \%line;
162     #double FIXME - totalfreight is redefined later.
163
164 # 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..
165     if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
166         warn "FREIGHT CHARGE MISMATCH!!";
167     }
168     $totalfreight = $parcelitems[$i]->{'freight'};
169     $totalquantity += $parcelitems[$i]->{'quantityreceived'};
170     $tototal       += $total;
171 }
172
173 # We get the pending orders either all or filtered
174 my $pendingorders;
175 if($input->param('op') eq "search"){
176     my $search   = $input->param('summaryfilter') || '';
177     my $ean      = $input->param('eanfilter') || '';
178     my $basketno = $input->param('basketfilter') || '';
179     my $orderno  = $input->param('orderfilter') || '';
180     my $grouped;
181     my $owner;
182     $pendingorders = GetPendingOrders($booksellerid,$grouped,$owner,$basketno,$orderno,$search,$ean);
183 }else{
184     $pendingorders = GetPendingOrders($booksellerid);
185 }
186 my $countpendings = scalar @$pendingorders;
187
188 # pending orders totals
189 my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
190 my $ordergrandtotal;
191 my @loop_orders = ();
192 for (my $i = 0 ; $i < $countpendings ; $i++) {
193     my %line;
194     %line = %{$pendingorders->[$i]};
195    
196     $line{quantity}+=0;
197     $line{quantityreceived}+=0;
198     $line{unitprice}+=0;
199     $totalPunitprice += $line{unitprice};
200     $totalPquantity +=$line{quantity};
201     $totalPqtyrcvd +=$line{quantityreceived};
202     $totalPecost += $line{ecost};
203     $line{ecost} = sprintf("%.2f",$line{ecost});
204     $line{ordertotal} = sprintf("%.2f",$line{ecost}*$line{quantity});
205     $line{unitprice} = sprintf("%.2f",$line{unitprice});
206     $line{invoice} = $invoice;
207     $line{gst} = $gst;
208     $line{total} = $total;
209     $line{booksellerid} = $booksellerid;
210     $ordergrandtotal += $line{ecost} * $line{quantity};
211     
212     my $biblionumber = $line{'biblionumber'};
213     my $countbiblio = CountBiblioInOrders($biblionumber);
214     my $ordernumber = $line{'ordernumber'};
215     my @subscriptions = GetSubscriptionsId ($biblionumber);
216     my $itemcount = GetItemsCount($biblionumber);
217     my $holds  = GetHolds ($biblionumber);
218     my @items = GetItemnumbersFromOrder( $ordernumber );
219     my $itemholds;
220     foreach my $item (@items){
221         my $nb = GetItemHolds($biblionumber, $item);
222         if ($nb){
223             $itemholds += $nb;
224         }
225     }
226
227     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
228     $line{suggestionid}         = $suggestion->{suggestionid};
229     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
230     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
231
232     # 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
233     $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
234     $line{items}                = ($itemcount) - (scalar @items);
235     $line{left_item}            = 1 if $line{items} >= 1;
236     $line{left_biblio}          = 1 if $countbiblio > 1;
237     $line{biblios}              = $countbiblio - 1;
238     $line{left_subscription}    = 1 if scalar @subscriptions >= 1;
239     $line{subscriptions}        = scalar @subscriptions;
240     $line{left_holds}           = 1 if $holds >= 1;
241     $line{left_holds_on_order}  = 1 if $line{left_holds} == 1 && ($line{items} == 0 || $itemholds );
242     $line{holds}                = $holds;
243     $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
244     
245     
246     push @loop_orders, \%line if ($i >= $startfrom and $i < $startfrom + $resultsperpage);
247 }
248 $freight = $totalfreight unless $freight;
249 my $count = $countpendings;
250
251 if ($count>$resultsperpage){
252     my $displaynext=0;
253     my $displayprev=$startfrom;
254     if(($count - ($startfrom+$resultsperpage)) > 0 ) {
255         $displaynext = 1;
256     }
257
258     my @numbers = ();
259     for (my $i=1; $i<$count/$resultsperpage+1; $i++) {
260             my $highlight=0;
261             ($startfrom/$resultsperpage==($i-1)) && ($highlight=1);
262             push @numbers, { number => $i,
263                 highlight => $highlight ,
264                 startfrom => ($i-1)*$resultsperpage};
265     }
266
267     my $from = $startfrom*$resultsperpage+1;
268     my $to;
269     if($count < (($startfrom+1)*$resultsperpage)){
270         $to = $count;
271     } else {
272         $to = (($startfrom+1)*$resultsperpage);
273     }
274     $template->param(numbers=>\@numbers,
275                      displaynext=>$displaynext,
276                      displayprev=>$displayprev,
277                      nextstartfrom=>(($startfrom+$resultsperpage<$count)?$startfrom+$resultsperpage:$count),
278                      prevstartfrom=>(($startfrom-$resultsperpage>0)?$startfrom-$resultsperpage:0)
279                     );
280 }
281
282 #$totalfreight=$freight;
283 $tototal = $tototal + $freight;
284
285 $template->param(
286     invoice               => $invoice,
287     datereceived          => $datereceived->output('iso'),
288     invoicedatereceived   => $datereceived->output('iso'),
289     formatteddatereceived => $datereceived->output(),
290     name                  => $bookseller->{'name'},
291     booksellerid            => $booksellerid,
292     gst                   => $gst,
293     freight               => $freight,
294     invoice               => $invoice,
295     countreceived         => $countlines,
296     loop_received         => \@loop_received,
297     countpending          => $countpendings,
298     loop_orders           => \@loop_orders,
299     totalprice            => sprintf($cfstr, $totalprice),
300     totalfreight          => $totalfreight,
301     totalquantity         => $totalquantity,
302     tototal               => sprintf($cfstr, $tototal),
303     ordergrandtotal       => sprintf($cfstr, $ordergrandtotal),
304     gst                   => $gst,
305     grandtot              => sprintf($cfstr, $tototal + $gst),
306     totalPunitprice       => sprintf("%.2f", $totalPunitprice),
307     totalPquantity        => $totalPquantity,
308     totalPqtyrcvd         => $totalPqtyrcvd,
309     totalPecost           => sprintf("%.2f", $totalPecost),
310     resultsperpage        => $resultsperpage,
311     (uc(C4::Context->preference("marcflavour"))) => 1
312 );
313 output_html_with_http_headers $input, $cookie, $template->output;
314