Bug 12648: Hide the result search on load
[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::Biblio;
64 use C4::Items;
65 use CGI qw ( -utf8 );
66 use C4::Output;
67 use C4::Dates qw/format_date format_date_in_iso/;
68 use C4::Suggestions;
69 use C4::Reserves qw/GetReservesFromBiblionumber/;
70
71 use Koha::Acquisition::Bookseller;
72
73 use JSON;
74
75 my $input=new CGI;
76 my $sticky_filters = $input->param('sticky_filters') || 0;
77
78 my ($template, $loggedinuser, $cookie)
79     = get_template_and_user({template_name => "acqui/parcel.tt",
80                  query => $input,
81                  type => "intranet",
82                  authnotrequired => 0,
83                  flagsrequired => {acquisition => 'order_receive'},
84                  debug => 1,
85 });
86
87 my $op = $input->param('op') // '';
88
89 # process cancellation first so that list of
90 # orders to display is calculated after
91 if ($op eq 'cancelreceipt') {
92     my $ordernumber = $input->param('ordernumber');
93     my $parent_ordernumber = CancelReceipt($ordernumber);
94     unless($parent_ordernumber) {
95         $template->param(error_cancelling_receipt => 1);
96     }
97 }
98
99 my $invoiceid = $input->param('invoiceid');
100 my $invoice;
101 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
102
103 unless( $invoiceid and $invoice->{invoiceid} ) {
104     $template->param(
105         error_invoice_not_known => 1,
106         no_orders_to_display    => 1
107     );
108     output_html_with_http_headers $input, $cookie, $template->output;
109     exit;
110 }
111
112 my $booksellerid = $invoice->{booksellerid};
113 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
114 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
115 my $datereceived = C4::Dates->new();
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_gste = 0;
123 my $total_gsti = 0;
124
125 my $subtotal_for_funds;
126 for my $order ( @orders ) {
127     $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $bookseller->{id}, receiving => 1, ordering => 1 });
128     $order->{'unitprice'} += 0;
129
130     if ( $bookseller->{listincgst} and not $bookseller->{invoiceincgst} ) {
131         $order->{ecost}     = $order->{ecostgste};
132         $order->{unitprice} = $order->{unitpricegste};
133     }
134     elsif ( not $bookseller->{listinct} and $bookseller->{invoiceincgst} ) {
135         $order->{ecost}     = $order->{ecostgsti};
136         $order->{unitprice} = $order->{unitpricegsti};
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     for my $itemnumber ( @itemnumbers ) {
145         my $holds = GetReservesFromBiblionumber({ biblionumber => $line{biblionumber}, itemnumber => $itemnumber });
146         $line{holds} += scalar( @$holds );
147     }
148     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
149     $foot{$line{gstrate}}{gstrate} = $line{gstrate};
150     $foot{$line{gstrate}}{gstvalue} += $line{gstvalue};
151     $total_gste += $line{totalgste};
152     $total_gsti += $line{totalgsti};
153
154     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
155     $line{suggestionid}         = $suggestion->{suggestionid};
156     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
157     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
158
159     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
160         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
161             @orders
162             )
163         {
164             $line{cannot_cancel} = 1;
165         }
166     }
167
168     my $budget_name = GetBudgetName( $line{budget_id} );
169     $line{budget_name} = $budget_name;
170
171     $subtotal_for_funds->{ $line{budget_name} }{ecost} += $order->{ecost} * $order->{quantity};
172     $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
173
174     push @loop_received, \%line;
175 }
176 push @book_foot_loop, map { $_ } values %foot;
177
178 my @loop_orders = ();
179 unless( defined $invoice->{closedate} ) {
180     my $pendingorders;
181     if ( $op eq "search" or $sticky_filters ) {
182         my ( $search, $ean, $basketname, $orderno, $basketgroupname );
183         if ( $sticky_filters ) {
184             $search = $input->cookie("filter_parcel_summary");
185             $ean = $input->cookie("filter_parcel_ean");
186             $basketname = $input->cookie("filter_parcel_basketname");
187             $orderno = $input->cookie("filter_parcel_orderno");
188             $basketgroupname = $input->cookie("filter_parcel_basketgroupname");
189         } else {
190             $search   = $input->param('summaryfilter') || '';
191             $ean      = $input->param('eanfilter') || '';
192             $basketname = $input->param('basketfilter') || '';
193             $orderno  = $input->param('orderfilter') || '';
194             $basketgroupname = $input->param('basketgroupnamefilter') || '';
195         }
196         $pendingorders = SearchOrders({
197             booksellerid => $booksellerid,
198             basketname => $basketname,
199             ordernumber => $orderno,
200             search => $search,
201             ean => $ean,
202             basketgroupname => $basketgroupname,
203             pending => 1,
204             ordered => 1,
205         });
206         $template->param(
207             summaryfilter => $search,
208             eanfilter => $ean,
209             basketfilter => $basketname,
210             orderfilter => $orderno,
211             basketgroupnamefilter => $basketgroupname,
212         );
213     }else{
214         $pendingorders = SearchOrders({
215             booksellerid => $booksellerid,
216             ordered => 1
217         });
218     }
219     my $countpendings = scalar @$pendingorders;
220
221     for (my $i = 0 ; $i < $countpendings ; $i++) {
222         my $order = $pendingorders->[$i];
223         $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $bookseller->{id}, receiving => 1, ordering => 1 });
224
225         if ( $bookseller->{listincgst} and not $bookseller->{invoiceincgst} ) {
226             $order->{ecost} = $order->{ecostgste};
227         } elsif ( not $bookseller->{listinct} and $bookseller->{invoiceincgst} ) {
228             $order->{ecost} = $order->{ecostgsti};
229         }
230         $order->{total} = $order->{ecost} * $order->{quantity};
231
232         my %line = %$order;
233
234         $line{invoice} = $invoice;
235         $line{booksellerid} = $booksellerid;
236
237         my $biblionumber = $line{'biblionumber'};
238         my $countbiblio = CountBiblioInOrders($biblionumber);
239         my $ordernumber = $line{'ordernumber'};
240         my @subscriptions = GetSubscriptionsId ($biblionumber);
241         my $itemcount = GetItemsCount($biblionumber);
242         my $holds  = GetHolds ($biblionumber);
243         my @items = GetItemnumbersFromOrder( $ordernumber );
244         my $itemholds;
245         foreach my $item (@items){
246             my $nb = GetItemHolds($biblionumber, $item);
247             if ($nb){
248                 $itemholds += $nb;
249             }
250         }
251
252         my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
253         $line{suggestionid}         = $suggestion->{suggestionid};
254         $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
255         $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
256
257         # 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
258         $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
259         $line{items}                = ($itemcount) - (scalar @items);
260         $line{left_item}            = 1 if $line{items} >= 1;
261         $line{left_biblio}          = 1 if $countbiblio > 1;
262         $line{biblios}              = $countbiblio - 1;
263         $line{left_subscription}    = 1 if scalar @subscriptions >= 1;
264         $line{subscriptions}        = scalar @subscriptions;
265         $line{left_holds}           = ($holds >= 1) ? 1 : 0;
266         $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
267         $line{holds}                = $holds;
268         $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
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          => $datereceived->output('iso'),
286     invoicedatereceived   => $datereceived->output('iso'),
287     formatteddatereceived => $datereceived->output(),
288     name                  => $bookseller->{'name'},
289     booksellerid          => $bookseller->{id},
290     loop_received         => \@loop_received,
291     loop_orders           => \@loop_orders,
292     book_foot_loop        => \@book_foot_loop,
293     (uc(C4::Context->preference("marcflavour"))) => 1,
294     total_gste           => $total_gste,
295     total_gsti           => $total_gsti,
296     subtotal_for_funds    => $subtotal_for_funds,
297     sticky_filters       => $sticky_filters,
298 );
299 output_html_with_http_headers $input, $cookie, $template->output;