Bug 19596: Don't try to get hold or items count for non-existent biblios
[koha.git] / acqui / parcel.pl
1 #!/usr/bin/perl
2
3 #script to receive 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
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 it 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::Biblio;
64 use C4::Items;
65 use CGI qw ( -utf8 );
66 use C4::Output;
67 use C4::Suggestions;
68
69 use Koha::Acquisition::Baskets;
70 use Koha::Acquisition::Bookseller;
71 use Koha::Biblios;
72 use Koha::DateUtils;
73 use Koha::Biblios;
74
75 use JSON;
76
77 my $input=new CGI;
78 my $sticky_filters = $input->param('sticky_filters') || 0;
79
80 my ($template, $loggedinuser, $cookie)
81     = get_template_and_user({template_name => "acqui/parcel.tt",
82                  query => $input,
83                  type => "intranet",
84                  authnotrequired => 0,
85                  flagsrequired => {acquisition => 'order_receive'},
86                  debug => 1,
87 });
88
89 my $op = $input->param('op') // '';
90
91 # process cancellation first so that list of
92 # orders to display is calculated after
93 if ($op eq 'cancelreceipt') {
94     my $ordernumber = $input->param('ordernumber');
95     my $parent_ordernumber = CancelReceipt($ordernumber);
96     unless($parent_ordernumber) {
97         $template->param(error_cancelling_receipt => 1);
98     }
99 }
100
101 my $invoiceid = $input->param('invoiceid');
102 my $invoice;
103 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
104
105 unless( $invoiceid and $invoice->{invoiceid} ) {
106     $template->param(
107         error_invoice_not_known => 1,
108         no_orders_to_display    => 1
109     );
110     output_html_with_http_headers $input, $cookie, $template->output;
111     exit;
112 }
113
114 my $booksellerid = $invoice->{booksellerid};
115 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
116
117 my @orders        = @{ $invoice->{orders} };
118 my $countlines    = scalar @orders;
119 my @loop_received = ();
120 my @book_foot_loop;
121 my %foot;
122 my $total_tax_excluded = 0;
123 my $total_tax_included = 0;
124
125 my $subtotal_for_funds;
126 for my $order ( @orders ) {
127     $order->{'unitprice'} += 0;
128
129     if ( $bookseller->invoiceincgst ) {
130         $order->{ecost}     = $order->{ecost_tax_included};
131         $order->{unitprice} = $order->{unitprice_tax_included};
132     }
133     else {
134         $order->{ecost}     = $order->{ecost_tax_excluded};
135         $order->{unitprice} = $order->{unitprice_tax_excluded};
136     }
137
138     $order->{total} = $order->{unitprice} * $order->{quantity};
139
140     my %line = %{ $order };
141     $line{invoice} = $invoice->{invoicenumber};
142     $line{holds} = 0;
143     my @itemnumbers = GetItemnumbersFromOrder( $order->{ordernumber} );
144     my $biblio = Koha::Biblios->find( $line{biblionumber} );
145     $line{holds} = $biblio ? $biblio->current_holds->search(
146         {
147             itemnumber => { -in => \@itemnumbers },
148         }
149     )->count : 0;
150     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
151
152     $line{tax_value} = $line{tax_value_on_receiving};
153     $line{tax_rate} = $line{tax_rate_on_receiving};
154     $foot{$line{tax_rate}}{tax_rate} = $line{tax_rate};
155     $foot{$line{tax_rate}}{tax_value} += $line{tax_value};
156     $total_tax_excluded += $line{unitprice_tax_excluded} * $line{quantity};
157     $total_tax_included += $line{unitprice_tax_included} * $line{quantity};
158
159     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
160     $line{suggestionid}         = $suggestion->{suggestionid};
161     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
162     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
163
164     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
165         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
166             @orders
167             )
168         {
169             $line{cannot_cancel} = 1;
170         }
171     }
172
173     my $budget_name = GetBudgetName( $line{budget_id} );
174     $line{budget_name} = $budget_name;
175
176     $subtotal_for_funds->{ $line{budget_name} }{ecost} += $order->{ecost} * $order->{quantity};
177     $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
178
179     push @loop_received, \%line;
180 }
181 push @book_foot_loop, map { $_ } values %foot;
182
183 my @loop_orders = ();
184 unless( defined $invoice->{closedate} ) {
185     my $pendingorders;
186     if ( $op eq "search" or $sticky_filters ) {
187         my ( $search, $ean, $basketname, $orderno, $basketgroupname );
188         if ( $sticky_filters ) {
189             $search = $input->cookie("filter_parcel_summary");
190             $ean = $input->cookie("filter_parcel_ean");
191             $basketname = $input->cookie("filter_parcel_basketname");
192             $orderno = $input->cookie("filter_parcel_orderno");
193             $basketgroupname = $input->cookie("filter_parcel_basketgroupname");
194         } else {
195             $search   = $input->param('summaryfilter') || '';
196             $ean      = $input->param('eanfilter') || '';
197             $basketname = $input->param('basketfilter') || '';
198             $orderno  = $input->param('orderfilter') || '';
199             $basketgroupname = $input->param('basketgroupnamefilter') || '';
200         }
201         $pendingorders = SearchOrders({
202             booksellerid => $booksellerid,
203             basketname => $basketname,
204             ordernumber => $orderno,
205             search => $search,
206             ean => $ean,
207             basketgroupname => $basketgroupname,
208             pending => 1,
209             ordered => 1,
210         });
211         $template->param(
212             summaryfilter => $search,
213             eanfilter => $ean,
214             basketfilter => $basketname,
215             orderfilter => $orderno,
216             basketgroupnamefilter => $basketgroupname,
217         );
218     }else{
219         $pendingorders = SearchOrders({
220             booksellerid => $booksellerid,
221             ordered => 1
222         });
223     }
224     my $countpendings = scalar @$pendingorders;
225
226     for (my $i = 0 ; $i < $countpendings ; $i++) {
227         my $order = $pendingorders->[$i];
228
229         if ( $bookseller->invoiceincgst ) {
230             $order->{ecost} = $order->{ecost_tax_included};
231         } else {
232             $order->{ecost} = $order->{ecost_tax_excluded};
233         }
234         $order->{total} = $order->{ecost} * $order->{quantity};
235
236         my %line = %$order;
237
238         $line{invoice} = $invoice;
239         $line{booksellerid} = $booksellerid;
240
241         my $biblionumber = $line{'biblionumber'};
242         my $biblio = Koha::Biblios->find( $biblionumber );
243         my $countbiblio = CountBiblioInOrders($biblionumber);
244         my $ordernumber = $line{'ordernumber'};
245         my $cnt_subscriptions = $biblio ? $biblio->subscriptions->count: 0;
246         my $itemcount   = $biblio ? $biblio->items->count : 0;
247         my $holds_count = $biblio ? $biblio->holds->count : 0;
248         my @items = GetItemnumbersFromOrder( $ordernumber );
249         my $itemholds = $biblio ? $biblio->holds->search({ itemnumber => { -in => \@items } })->count : 0;
250
251         my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
252         $line{suggestionid}         = $suggestion->{suggestionid};
253         $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
254         $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
255
256         # 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
257         $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !($cnt_subscriptions) && !($holds_count);
258         $line{items}                = ($itemcount) - (scalar @items);
259         $line{left_item}            = 1 if $line{items} >= 1;
260         $line{left_biblio}          = 1 if $countbiblio > 1;
261         $line{biblios}              = $countbiblio - 1;
262         $line{left_subscription}    = 1 if $cnt_subscriptions;
263         $line{subscriptions}        = $cnt_subscriptions;
264         $line{left_holds}           = ($holds_count >= 1) ? 1 : 0;
265         $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
266         $line{holds}                = $holds_count;
267         $line{holds_on_order}       = $itemholds?$itemholds:$holds_count if $line{left_holds_on_order};
268         $line{basket}               = Koha::Acquisition::Baskets->find( $line{basketno} );
269
270         my $budget_name = GetBudgetName( $line{budget_id} );
271         $line{budget_name} = $budget_name;
272
273         push @loop_orders, \%line;
274     }
275
276     $template->param(
277         loop_orders  => \@loop_orders,
278     );
279 }
280
281 $template->param(
282     invoiceid             => $invoice->{invoiceid},
283     invoice               => $invoice->{invoicenumber},
284     invoiceclosedate      => $invoice->{closedate},
285     datereceived          => dt_from_string,
286     name                  => $bookseller->name,
287     booksellerid          => $bookseller->id,
288     loop_received         => \@loop_received,
289     loop_orders           => \@loop_orders,
290     book_foot_loop        => \@book_foot_loop,
291     (uc(C4::Context->preference("marcflavour"))) => 1,
292     total_tax_excluded    => $total_tax_excluded,
293     total_tax_included    => $total_tax_included,
294     subtotal_for_funds    => $subtotal_for_funds,
295     sticky_filters       => $sticky_filters,
296 );
297 output_html_with_http_headers $input, $cookie, $template->output;