Bug 10694: (follow-up) fix various issues
[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 $holds = GetReservesFromBiblionumber({ biblionumber => $line{biblionumber}, itemnumber => $itemnumber });
179         $line{holds} += scalar( @$holds );
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             ordered => 1,
242         });
243         $template->param(
244             summaryfilter => $search,
245             eanfilter => $ean,
246             basketfilter => $basketname,
247             orderfilter => $orderno,
248             basketgroupnamefilter => $basketgroupname,
249         );
250     }else{
251         $pendingorders = SearchOrders({
252             booksellerid => $booksellerid,
253             ordered => 1
254         });
255     }
256     my $countpendings = scalar @$pendingorders;
257
258     for (my $i = 0 ; $i < $countpendings ; $i++) {
259         my %line;
260         %line = %{$pendingorders->[$i]};
261
262         my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
263         $line{unitprice} = get_value_with_gst_params( $line{unitprice}, $line{gstrate}, $bookseller );
264         $line{quantity} += 0;
265         $line{quantityreceived} += 0;
266         $line{unitprice}+=0;
267         $line{ecost} = sprintf( "%.2f", $ecost );
268         $line{ordertotal} = sprintf( "%.2f", $ecost * $line{quantity} );
269         $line{unitprice} = sprintf("%.2f",$line{unitprice});
270         $line{invoice} = $invoice;
271         $line{booksellerid} = $booksellerid;
272
273
274
275         my $biblionumber = $line{'biblionumber'};
276         my $countbiblio = CountBiblioInOrders($biblionumber);
277         my $ordernumber = $line{'ordernumber'};
278         my @subscriptions = GetSubscriptionsId ($biblionumber);
279         my $itemcount = GetItemsCount($biblionumber);
280         my $holds  = GetHolds ($biblionumber);
281         my @items = GetItemnumbersFromOrder( $ordernumber );
282         my $itemholds;
283         foreach my $item (@items){
284             my $nb = GetItemHolds($biblionumber, $item);
285             if ($nb){
286                 $itemholds += $nb;
287             }
288         }
289
290         my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
291         $line{suggestionid}         = $suggestion->{suggestionid};
292         $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
293         $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
294
295         # 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
296         $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
297         $line{items}                = ($itemcount) - (scalar @items);
298         $line{left_item}            = 1 if $line{items} >= 1;
299         $line{left_biblio}          = 1 if $countbiblio > 1;
300         $line{biblios}              = $countbiblio - 1;
301         $line{left_subscription}    = 1 if scalar @subscriptions >= 1;
302         $line{subscriptions}        = scalar @subscriptions;
303         $line{left_holds}           = ($holds >= 1) ? 1 : 0;
304         $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
305         $line{holds}                = $holds;
306         $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
307
308         my $budget = GetBudget( $line{budget_id} );
309         $line{budget_name} = $budget->{'budget_name'};
310
311         push @loop_orders, \%line;
312     }
313
314     $template->param(
315         loop_orders  => \@loop_orders,
316     );
317 }
318
319 $template->param(
320     invoiceid             => $invoice->{invoiceid},
321     invoice               => $invoice->{invoicenumber},
322     invoiceclosedate      => $invoice->{closedate},
323     datereceived          => $datereceived->output('iso'),
324     invoicedatereceived   => $datereceived->output('iso'),
325     formatteddatereceived => $datereceived->output(),
326     name                  => $bookseller->{'name'},
327     booksellerid          => $bookseller->{id},
328     countreceived         => $countlines,
329     loop_received         => \@loop_received,
330     loop_orders           => \@loop_orders,
331     book_foot_loop        => \@book_foot_loop,
332     totalprice            => sprintf($cfstr, $totalprice),
333     totalquantity         => $totalquantity,
334     (uc(C4::Context->preference("marcflavour"))) => 1,
335     total_quantity       => $total_quantity,
336     total_gste           => sprintf( "%.2f", $total_gste ),
337     total_gsti           => sprintf( "%.2f", $total_gsti ),
338     sticky_filters       => $sticky_filters,
339 );
340 output_html_with_http_headers $input, $cookie, $template->output;