Bug 10723: Merge GetPendingOrders and SearchOrders routines
[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 JSON;
71
72 my $input=new CGI;
73
74 sub get_value_with_gst_params {
75     my $value = shift;
76     my $gstrate = shift;
77     my $bookseller = shift;
78     if ( $bookseller->{listincgst} ) {
79         if ( $bookseller->{invoiceincgst} ) {
80             return $value;
81         } else {
82             return $value / ( 1 + $gstrate );
83         }
84     } else {
85         if ( $bookseller->{invoiceincgst} ) {
86             return $value * ( 1 + $gstrate );
87         } else {
88             return $value;
89         }
90     }
91 }
92
93 sub get_gste {
94     my $value = shift;
95     my $gstrate = shift;
96     my $bookseller = shift;
97     return $bookseller->{invoiceincgst}
98         ? $value / ( 1 + $gstrate )
99         : $value;
100 }
101
102 sub get_gst {
103     my $value = shift;
104     my $gstrate = shift;
105     my $bookseller = shift;
106     return $bookseller->{invoiceincgst}
107         ? $value / ( 1 + $gstrate ) * $gstrate
108         : $value * ( 1 + $gstrate ) - $value;
109 }
110
111 my ($template, $loggedinuser, $cookie)
112     = get_template_and_user({template_name => "acqui/parcel.tmpl",
113                  query => $input,
114                  type => "intranet",
115                  authnotrequired => 0,
116                  flagsrequired => {acquisition => 'order_receive'},
117                  debug => 1,
118 });
119
120 my $invoiceid = $input->param('invoiceid');
121 my $invoice;
122 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
123
124 unless( $invoiceid and $invoice->{invoiceid} ) {
125     $template->param(
126         error_invoice_not_known => 1,
127         no_orders_to_display    => 1
128     );
129     output_html_with_http_headers $input, $cookie, $template->output;
130     exit;
131 }
132
133 my $op = $input->param('op') // '';
134
135 if ($op eq 'cancelreceipt') {
136     my $ordernumber = $input->param('ordernumber');
137     my $parent_ordernumber = CancelReceipt($ordernumber);
138     unless($parent_ordernumber) {
139         $template->param(error_cancelling_receipt => 1);
140     }
141 }
142
143 my $booksellerid = $invoice->{booksellerid};
144 my $bookseller = GetBookSellerFromId($booksellerid);
145 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
146 my $datereceived = C4::Dates->new();
147
148 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
149 my @parcelitems   = @{ $invoice->{orders} };
150 my $countlines    = scalar @parcelitems;
151 my $totalprice    = 0;
152 my $totalquantity = 0;
153 my $total;
154 my @loop_received = ();
155 my @book_foot_loop;
156 my %foot;
157 my $total_quantity = 0;
158 my $total_gste = 0;
159 my $total_gsti = 0;
160
161 for my $item ( @parcelitems ) {
162     $item->{unitprice} = get_value_with_gst_params( $item->{unitprice}, $item->{gstrate}, $bookseller );
163     $total = ( $item->{'unitprice'} ) * $item->{'quantityreceived'};
164     $item->{'unitprice'} += 0;
165     my %line = %{ $item };
166     my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
167     $line{ecost} = sprintf( "%.2f", $ecost );
168     $line{invoice} = $invoice->{invoicenumber};
169     $line{total} = sprintf($cfstr, $total);
170     $line{booksellerid} = $invoice->{booksellerid};
171     $totalprice += $item->{'unitprice'};
172     $line{unitprice} = sprintf( $cfstr, $item->{'unitprice'} );
173     my $gste = get_gste( $line{total}, $line{gstrate}, $bookseller );
174     my $gst = get_gst( $line{total}, $line{gstrate}, $bookseller );
175     $foot{$line{gstrate}}{gstrate} = $line{gstrate};
176     $foot{$line{gstrate}}{value} += sprintf( "%.2f", $gst );
177     $total_quantity += $line{quantity};
178     $total_gste += $gste;
179     $total_gsti += $gste + $gst;
180
181     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
182     $line{suggestionid}         = $suggestion->{suggestionid};
183     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
184     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
185
186     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
187         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
188             @parcelitems )
189         {
190             $line{cannot_cancel} = 1;
191         }
192     }
193
194     my $budget = GetBudget( $line{budget_id} );
195     $line{budget_name} = $budget->{'budget_name'};
196
197     push @loop_received, \%line;
198     $totalquantity += $item->{'quantityreceived'};
199
200 }
201 push @book_foot_loop, map { $_ } values %foot;
202
203 my @loop_orders = ();
204 if(!defined $invoice->{closedate}) {
205     my $pendingorders;
206     if($input->param('op') eq "search"){
207         my $search   = $input->param('summaryfilter') || '';
208         my $ean      = $input->param('eanfilter') || '';
209         my $basketname = $input->param('basketfilter') || '';
210         my $orderno  = $input->param('orderfilter') || '';
211         $pendingorders = SearchOrders({
212             booksellerid => $booksellerid,
213             basketname => $basketname,
214             ordernumber => $orderno,
215             search => $search,
216             ean => $ean,
217             pending => 1,
218         });
219         $template->param(
220             summaryfilter => $search,
221             eanfilter => $ean,
222             basketfilter => $basketname,
223             orderfilter => $orderno,
224         );
225     }else{
226         $pendingorders = SearchOrders({
227             booksellerid => $booksellerid,
228             pending => 1
229         });
230     }
231     my $countpendings = scalar @$pendingorders;
232
233     for (my $i = 0 ; $i < $countpendings ; $i++) {
234         my %line;
235         %line = %{$pendingorders->[$i]};
236
237         my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
238         $line{unitprice} = get_value_with_gst_params( $line{unitprice}, $line{gstrate}, $bookseller );
239         $line{quantity} += 0;
240         $line{quantityreceived} += 0;
241         $line{unitprice}+=0;
242         $line{ecost} = sprintf( "%.2f", $ecost );
243         $line{ordertotal} = sprintf( "%.2f", $ecost * $line{quantity} );
244         $line{unitprice} = sprintf("%.2f",$line{unitprice});
245         $line{invoice} = $invoice;
246         $line{booksellerid} = $booksellerid;
247
248
249
250         my $biblionumber = $line{'biblionumber'};
251         my $countbiblio = CountBiblioInOrders($biblionumber);
252         my $ordernumber = $line{'ordernumber'};
253         my @subscriptions = GetSubscriptionsId ($biblionumber);
254         my $itemcount = GetItemsCount($biblionumber);
255         my $holds  = GetHolds ($biblionumber);
256         my @items = GetItemnumbersFromOrder( $ordernumber );
257         my $itemholds;
258         foreach my $item (@items){
259             my $nb = GetItemHolds($biblionumber, $item);
260             if ($nb){
261                 $itemholds += $nb;
262             }
263         }
264
265         my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
266         $line{suggestionid}         = $suggestion->{suggestionid};
267         $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
268         $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
269
270         # 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
271         $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
272         $line{items}                = ($itemcount) - (scalar @items);
273         $line{left_item}            = 1 if $line{items} >= 1;
274         $line{left_biblio}          = 1 if $countbiblio > 1;
275         $line{biblios}              = $countbiblio - 1;
276         $line{left_subscription}    = 1 if scalar @subscriptions >= 1;
277         $line{subscriptions}        = scalar @subscriptions;
278         $line{left_holds}           = ($holds >= 1) ? 1 : 0;
279         $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
280         $line{holds}                = $holds;
281         $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
282
283         my $budget = GetBudget( $line{budget_id} );
284         $line{budget_name} = $budget->{'budget_name'};
285
286         push @loop_orders, \%line;
287     }
288
289     $template->param(
290         loop_orders  => \@loop_orders,
291     );
292 }
293
294 $template->param(
295     invoiceid             => $invoice->{invoiceid},
296     invoice               => $invoice->{invoicenumber},
297     invoiceclosedate      => $invoice->{closedate},
298     datereceived          => $datereceived->output('iso'),
299     invoicedatereceived   => $datereceived->output('iso'),
300     formatteddatereceived => $datereceived->output(),
301     name                  => $bookseller->{'name'},
302     booksellerid          => $bookseller->{id},
303     countreceived         => $countlines,
304     loop_received         => \@loop_received,
305     loop_orders           => \@loop_orders,
306     book_foot_loop        => \@book_foot_loop,
307     totalprice            => sprintf($cfstr, $totalprice),
308     totalquantity         => $totalquantity,
309     (uc(C4::Context->preference("marcflavour"))) => 1,
310     total_quantity       => $total_quantity,
311     total_gste           => sprintf( "%.2f", $total_gste ),
312     total_gsti           => sprintf( "%.2f", $total_gsti ),
313 );
314 output_html_with_http_headers $input, $cookie, $template->output;