Bug 11141: retain pending order filters during receiving.
[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
46 =item gst
47
48
49 =item datereceived
50
51 To filter the results list on this given date.
52
53 =back
54
55 =cut
56
57 use strict;
58 use warnings;
59
60 use C4::Auth;
61 use C4::Acquisition;
62 use C4::Budgets;
63 use C4::Bookseller qw/ GetBookSellerFromId /;
64 use C4::Biblio;
65 use C4::Items;
66 use CGI;
67 use C4::Output;
68 use C4::Dates qw/format_date format_date_in_iso/;
69 use C4::Suggestions;
70 use C4::Reserves qw/GetReservesFromBiblionumber/;
71 use JSON;
72
73 my $input=new CGI;
74 my $sticky_filters = $input->param('sticky_filters') || 0;
75
76 sub get_value_with_gst_params {
77     my $value = shift;
78     my $gstrate = shift;
79     my $bookseller = shift;
80     if ( $bookseller->{listincgst} ) {
81         if ( $bookseller->{invoiceincgst} ) {
82             return $value;
83         } else {
84             return $value / ( 1 + $gstrate );
85         }
86     } else {
87         if ( $bookseller->{invoiceincgst} ) {
88             return $value * ( 1 + $gstrate );
89         } else {
90             return $value;
91         }
92     }
93 }
94
95 sub get_gste {
96     my $value = shift;
97     my $gstrate = shift;
98     my $bookseller = shift;
99     return $bookseller->{invoiceincgst}
100         ? $value / ( 1 + $gstrate )
101         : $value;
102 }
103
104 sub get_gst {
105     my $value = shift;
106     my $gstrate = shift;
107     my $bookseller = shift;
108     return $bookseller->{invoiceincgst}
109         ? $value / ( 1 + $gstrate ) * $gstrate
110         : $value * ( 1 + $gstrate ) - $value;
111 }
112
113 my ($template, $loggedinuser, $cookie)
114     = get_template_and_user({template_name => "acqui/parcel.tmpl",
115                  query => $input,
116                  type => "intranet",
117                  authnotrequired => 0,
118                  flagsrequired => {acquisition => 'order_receive'},
119                  debug => 1,
120 });
121
122 my $op = $input->param('op') // '';
123
124 # process cancellation first so that list of
125 # orders to display is calculated after
126 if ($op eq 'cancelreceipt') {
127     my $ordernumber = $input->param('ordernumber');
128     my $parent_ordernumber = CancelReceipt($ordernumber);
129     unless($parent_ordernumber) {
130         $template->param(error_cancelling_receipt => 1);
131     }
132 }
133
134 my $invoiceid = $input->param('invoiceid');
135 my $invoice;
136 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
137
138 unless( $invoiceid and $invoice->{invoiceid} ) {
139     $template->param(
140         error_invoice_not_known => 1,
141         no_orders_to_display    => 1
142     );
143     output_html_with_http_headers $input, $cookie, $template->output;
144     exit;
145 }
146
147 my $booksellerid = $invoice->{booksellerid};
148 my $bookseller = GetBookSellerFromId($booksellerid);
149 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
150 my $datereceived = C4::Dates->new();
151
152 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
153 my @orders        = @{ $invoice->{orders} };
154 my $countlines    = scalar @orders;
155 my $totalprice    = 0;
156 my $totalquantity = 0;
157 my $total;
158 my @loop_received = ();
159 my @book_foot_loop;
160 my %foot;
161 my $total_quantity = 0;
162 my $total_gste = 0;
163 my $total_gsti = 0;
164
165 for my $order ( @orders ) {
166     $order->{unitprice} = get_value_with_gst_params( $order->{unitprice}, $order->{gstrate}, $bookseller );
167     $total = ( $order->{unitprice} ) * $order->{quantityreceived};
168     $order->{'unitprice'} += 0;
169     my %line = %{ $order };
170     my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
171     $line{ecost} = sprintf( "%.2f", $ecost );
172     $line{invoice} = $invoice->{invoicenumber};
173     $line{total} = sprintf($cfstr, $total);
174     $line{booksellerid} = $invoice->{booksellerid};
175     $line{holds} = 0;
176     my @itemnumbers = GetItemnumbersFromOrder( $order->{ordernumber} );
177     for my $itemnumber ( @itemnumbers ) {
178         my ( $count ) = &GetReservesFromBiblionumber($line{biblionumber}, undef, $itemnumber);
179         $line{holds} += $count;
180     }
181     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
182     $totalprice += $order->{unitprice};
183     $line{unitprice} = sprintf( $cfstr, $order->{unitprice} );
184     my $gste = get_gste( $line{total}, $line{gstrate}, $bookseller );
185     my $gst = get_gst( $line{total}, $line{gstrate}, $bookseller );
186     $foot{$line{gstrate}}{gstrate} = $line{gstrate};
187     $foot{$line{gstrate}}{value} += sprintf( "%.2f", $gst );
188     $total_quantity += $line{quantity};
189     $total_gste += $gste;
190     $total_gsti += $gste + $gst;
191
192     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
193     $line{suggestionid}         = $suggestion->{suggestionid};
194     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
195     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
196
197     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
198         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
199             @orders
200             )
201         {
202             $line{cannot_cancel} = 1;
203         }
204     }
205
206     my $budget = GetBudget( $line{budget_id} );
207     $line{budget_name} = $budget->{'budget_name'};
208
209     push @loop_received, \%line;
210     $totalquantity += $order->{quantityreceived};
211
212 }
213 push @book_foot_loop, map { $_ } values %foot;
214
215 my @loop_orders = ();
216 unless( defined $invoice->{closedate} ) {
217     my $pendingorders;
218     if ( $op eq "search" or $sticky_filters ) {
219         my ( $search, $ean, $basketname, $orderno, $basketgroupname );
220         if ( $sticky_filters ) {
221             $search = $input->cookie("filter_parcel_summary");
222             $ean = $input->cookie("filter_parcel_ean");
223             $basketname = $input->cookie("filter_parcel_basketname");
224             $orderno = $input->cookie("filter_parcel_orderno");
225             $basketgroupname = $input->cookie("filter_parcel_basketgroupname");
226         } else {
227             $search   = $input->param('summaryfilter') || '';
228             $ean      = $input->param('eanfilter') || '';
229             $basketname = $input->param('basketfilter') || '';
230             $orderno  = $input->param('orderfilter') || '';
231             $basketgroupname = $input->param('basketgroupnamefilter') || '';
232         }
233         $pendingorders = SearchOrders({
234             booksellerid => $booksellerid,
235             basketname => $basketname,
236             ordernumber => $orderno,
237             search => $search,
238             ean => $ean,
239             basketgroupname => $basketgroupname,
240             pending => 1,
241         });
242         $template->param(
243             summaryfilter => $search,
244             eanfilter => $ean,
245             basketfilter => $basketname,
246             orderfilter => $orderno,
247             basketgroupnamefilter => $basketgroupname,
248         );
249     }else{
250         $pendingorders = SearchOrders({
251             booksellerid => $booksellerid,
252             pending => 1
253         });
254     }
255     my $countpendings = scalar @$pendingorders;
256
257     for (my $i = 0 ; $i < $countpendings ; $i++) {
258         my %line;
259         %line = %{$pendingorders->[$i]};
260
261         my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
262         $line{unitprice} = get_value_with_gst_params( $line{unitprice}, $line{gstrate}, $bookseller );
263         $line{quantity} += 0;
264         $line{quantityreceived} += 0;
265         $line{unitprice}+=0;
266         $line{ecost} = sprintf( "%.2f", $ecost );
267         $line{ordertotal} = sprintf( "%.2f", $ecost * $line{quantity} );
268         $line{unitprice} = sprintf("%.2f",$line{unitprice});
269         $line{invoice} = $invoice;
270         $line{booksellerid} = $booksellerid;
271
272
273
274         my $biblionumber = $line{'biblionumber'};
275         my $countbiblio = CountBiblioInOrders($biblionumber);
276         my $ordernumber = $line{'ordernumber'};
277         my @subscriptions = GetSubscriptionsId ($biblionumber);
278         my $itemcount = GetItemsCount($biblionumber);
279         my $holds  = GetHolds ($biblionumber);
280         my @items = GetItemnumbersFromOrder( $ordernumber );
281         my $itemholds;
282         foreach my $item (@items){
283             my $nb = GetItemHolds($biblionumber, $item);
284             if ($nb){
285                 $itemholds += $nb;
286             }
287         }
288
289         my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
290         $line{suggestionid}         = $suggestion->{suggestionid};
291         $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
292         $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
293
294         # 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
295         $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
296         $line{items}                = ($itemcount) - (scalar @items);
297         $line{left_item}            = 1 if $line{items} >= 1;
298         $line{left_biblio}          = 1 if $countbiblio > 1;
299         $line{biblios}              = $countbiblio - 1;
300         $line{left_subscription}    = 1 if scalar @subscriptions >= 1;
301         $line{subscriptions}        = scalar @subscriptions;
302         $line{left_holds}           = ($holds >= 1) ? 1 : 0;
303         $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
304         $line{holds}                = $holds;
305         $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
306
307         my $budget = GetBudget( $line{budget_id} );
308         $line{budget_name} = $budget->{'budget_name'};
309
310         push @loop_orders, \%line;
311     }
312
313     $template->param(
314         loop_orders  => \@loop_orders,
315     );
316 }
317
318 $template->param(
319     invoiceid             => $invoice->{invoiceid},
320     invoice               => $invoice->{invoicenumber},
321     invoiceclosedate      => $invoice->{closedate},
322     datereceived          => $datereceived->output('iso'),
323     invoicedatereceived   => $datereceived->output('iso'),
324     formatteddatereceived => $datereceived->output(),
325     name                  => $bookseller->{'name'},
326     booksellerid          => $bookseller->{id},
327     countreceived         => $countlines,
328     loop_received         => \@loop_received,
329     loop_orders           => \@loop_orders,
330     book_foot_loop        => \@book_foot_loop,
331     totalprice            => sprintf($cfstr, $totalprice),
332     totalquantity         => $totalquantity,
333     (uc(C4::Context->preference("marcflavour"))) => 1,
334     total_quantity       => $total_quantity,
335     total_gste           => sprintf( "%.2f", $total_gste ),
336     total_gsti           => sprintf( "%.2f", $total_gsti ),
337     sticky_filters       => $sticky_filters,
338 );
339 output_html_with_http_headers $input, $cookie, $template->output;