Bug 16844: (follow-up of 15656) Remove export of GetMemberRelatives from C4::Members
[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 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::Suggestions;
68 use C4::Reserves qw/GetReservesFromBiblionumber/;
69
70 use Koha::Acquisition::Bookseller;
71 use Koha::DateUtils;
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
116 my @orders        = @{ $invoice->{orders} };
117 my $countlines    = scalar @orders;
118 my @loop_received = ();
119 my @book_foot_loop;
120 my %foot;
121 my $total_gste = 0;
122 my $total_gsti = 0;
123
124 my $subtotal_for_funds;
125 for my $order ( @orders ) {
126     $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $bookseller->{id}, receiving => 1, ordering => 1 });
127     $order->{'unitprice'} += 0;
128
129     if ( $bookseller->{listincgst} and not $bookseller->{invoiceincgst} ) {
130         $order->{ecost}     = $order->{ecostgste};
131         $order->{unitprice} = $order->{unitpricegste};
132     }
133     elsif ( not $bookseller->{listinct} and $bookseller->{invoiceincgst} ) {
134         $order->{ecost}     = $order->{ecostgsti};
135         $order->{unitprice} = $order->{unitpricegsti};
136     }
137     $order->{total} = $order->{unitprice} * $order->{quantity};
138
139     my %line = %{ $order };
140     $line{invoice} = $invoice->{invoicenumber};
141     $line{holds} = 0;
142     my @itemnumbers = GetItemnumbersFromOrder( $order->{ordernumber} );
143     for my $itemnumber ( @itemnumbers ) {
144         my $holds = GetReservesFromBiblionumber({ biblionumber => $line{biblionumber}, itemnumber => $itemnumber });
145         $line{holds} += scalar( @$holds );
146     }
147     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
148     $foot{$line{gstrate}}{gstrate} = $line{gstrate};
149     $foot{$line{gstrate}}{gstvalue} += $line{gstvalue};
150     $total_gste += $line{totalgste};
151     $total_gsti += $line{totalgsti};
152
153     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
154     $line{suggestionid}         = $suggestion->{suggestionid};
155     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
156     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
157
158     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
159         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
160             @orders
161             )
162         {
163             $line{cannot_cancel} = 1;
164         }
165     }
166
167     my $budget_name = GetBudgetName( $line{budget_id} );
168     $line{budget_name} = $budget_name;
169
170     $subtotal_for_funds->{ $line{budget_name} }{ecost} += $order->{ecost} * $order->{quantity};
171     $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
172
173     push @loop_received, \%line;
174 }
175 push @book_foot_loop, map { $_ } values %foot;
176
177 my @loop_orders = ();
178 unless( defined $invoice->{closedate} ) {
179     my $pendingorders;
180     if ( $op eq "search" or $sticky_filters ) {
181         my ( $search, $ean, $basketname, $orderno, $basketgroupname );
182         if ( $sticky_filters ) {
183             $search = $input->cookie("filter_parcel_summary");
184             $ean = $input->cookie("filter_parcel_ean");
185             $basketname = $input->cookie("filter_parcel_basketname");
186             $orderno = $input->cookie("filter_parcel_orderno");
187             $basketgroupname = $input->cookie("filter_parcel_basketgroupname");
188         } else {
189             $search   = $input->param('summaryfilter') || '';
190             $ean      = $input->param('eanfilter') || '';
191             $basketname = $input->param('basketfilter') || '';
192             $orderno  = $input->param('orderfilter') || '';
193             $basketgroupname = $input->param('basketgroupnamefilter') || '';
194         }
195         $pendingorders = SearchOrders({
196             booksellerid => $booksellerid,
197             basketname => $basketname,
198             ordernumber => $orderno,
199             search => $search,
200             ean => $ean,
201             basketgroupname => $basketgroupname,
202             pending => 1,
203             ordered => 1,
204         });
205         $template->param(
206             summaryfilter => $search,
207             eanfilter => $ean,
208             basketfilter => $basketname,
209             orderfilter => $orderno,
210             basketgroupnamefilter => $basketgroupname,
211         );
212     }else{
213         $pendingorders = SearchOrders({
214             booksellerid => $booksellerid,
215             ordered => 1
216         });
217     }
218     my $countpendings = scalar @$pendingorders;
219
220     for (my $i = 0 ; $i < $countpendings ; $i++) {
221         my $order = $pendingorders->[$i];
222         $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $bookseller->{id}, receiving => 1, ordering => 1 });
223
224         if ( $bookseller->{listincgst} and not $bookseller->{invoiceincgst} ) {
225             $order->{ecost} = $order->{ecostgste};
226         } elsif ( not $bookseller->{listinct} and $bookseller->{invoiceincgst} ) {
227             $order->{ecost} = $order->{ecostgsti};
228         }
229         $order->{total} = $order->{ecost} * $order->{quantity};
230
231         my %line = %$order;
232
233         $line{invoice} = $invoice;
234         $line{booksellerid} = $booksellerid;
235
236         my $biblionumber = $line{'biblionumber'};
237         my $countbiblio = CountBiblioInOrders($biblionumber);
238         my $ordernumber = $line{'ordernumber'};
239         my @subscriptions = GetSubscriptionsId ($biblionumber);
240         my $itemcount = GetItemsCount($biblionumber);
241         my $holds  = GetHolds ($biblionumber);
242         my @items = GetItemnumbersFromOrder( $ordernumber );
243         my $itemholds;
244         foreach my $item (@items){
245             my $nb = GetItemHolds($biblionumber, $item);
246             if ($nb){
247                 $itemholds += $nb;
248             }
249         }
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 && !(@subscriptions) && !($holds);
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 scalar @subscriptions >= 1;
263         $line{subscriptions}        = scalar @subscriptions;
264         $line{left_holds}           = ($holds >= 1) ? 1 : 0;
265         $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
266         $line{holds}                = $holds;
267         $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
268
269         my $budget_name = GetBudgetName( $line{budget_id} );
270         $line{budget_name} = $budget_name;
271
272         push @loop_orders, \%line;
273     }
274
275     $template->param(
276         loop_orders  => \@loop_orders,
277     );
278 }
279
280 $template->param(
281     invoiceid             => $invoice->{invoiceid},
282     invoice               => $invoice->{invoicenumber},
283     invoiceclosedate      => $invoice->{closedate},
284     datereceived          => dt_from_string,
285     name                  => $bookseller->{'name'},
286     booksellerid          => $bookseller->{id},
287     loop_received         => \@loop_received,
288     loop_orders           => \@loop_orders,
289     book_foot_loop        => \@book_foot_loop,
290     (uc(C4::Context->preference("marcflavour"))) => 1,
291     total_gste           => $total_gste,
292     total_gsti           => $total_gsti,
293     subtotal_for_funds    => $subtotal_for_funds,
294     sticky_filters       => $sticky_filters,
295 );
296 output_html_with_http_headers $input, $cookie, $template->output;