839c2a9944c71c6fc147534d42eb5291be9811bc
[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 $op = $input->param('op') // '';
121
122 # process cancellation first so that list of
123 # orders to display is calculated after
124 if ($op eq 'cancelreceipt') {
125     my $ordernumber = $input->param('ordernumber');
126     my $parent_ordernumber = CancelReceipt($ordernumber);
127     unless($parent_ordernumber) {
128         $template->param(error_cancelling_receipt => 1);
129     }
130 }
131
132 my $invoiceid = $input->param('invoiceid');
133 my $invoice;
134 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
135
136 unless( $invoiceid and $invoice->{invoiceid} ) {
137     $template->param(
138         error_invoice_not_known => 1,
139         no_orders_to_display    => 1
140     );
141     output_html_with_http_headers $input, $cookie, $template->output;
142     exit;
143 }
144
145 my $booksellerid = $invoice->{booksellerid};
146 my $bookseller = GetBookSellerFromId($booksellerid);
147 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
148 my $datereceived = C4::Dates->new();
149
150 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
151 my @parcelitems   = @{ $invoice->{orders} };
152 my $countlines    = scalar @parcelitems;
153 my $totalprice    = 0;
154 my $totalquantity = 0;
155 my $total;
156 my @loop_received = ();
157 my @book_foot_loop;
158 my %foot;
159 my $total_quantity = 0;
160 my $total_gste = 0;
161 my $total_gsti = 0;
162
163 for my $item ( @parcelitems ) {
164     $item->{unitprice} = get_value_with_gst_params( $item->{unitprice}, $item->{gstrate}, $bookseller );
165     $total = ( $item->{'unitprice'} ) * $item->{'quantityreceived'};
166     $item->{'unitprice'} += 0;
167     my %line = %{ $item };
168     my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
169     $line{ecost} = sprintf( "%.2f", $ecost );
170     $line{invoice} = $invoice->{invoicenumber};
171     $line{total} = sprintf($cfstr, $total);
172     $line{booksellerid} = $invoice->{booksellerid};
173     $totalprice += $item->{'unitprice'};
174     $line{unitprice} = sprintf( $cfstr, $item->{'unitprice'} );
175     my $gste = get_gste( $line{total}, $line{gstrate}, $bookseller );
176     my $gst = get_gst( $line{total}, $line{gstrate}, $bookseller );
177     $foot{$line{gstrate}}{gstrate} = $line{gstrate};
178     $foot{$line{gstrate}}{value} += sprintf( "%.2f", $gst );
179     $total_quantity += $line{quantity};
180     $total_gste += $gste;
181     $total_gsti += $gste + $gst;
182
183     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
184     $line{suggestionid}         = $suggestion->{suggestionid};
185     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
186     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
187
188     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
189         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
190             @parcelitems )
191         {
192             $line{cannot_cancel} = 1;
193         }
194     }
195
196     my $budget = GetBudget( $line{budget_id} );
197     $line{budget_name} = $budget->{'budget_name'};
198
199     push @loop_received, \%line;
200     $totalquantity += $item->{'quantityreceived'};
201
202 }
203 push @book_foot_loop, map { $_ } values %foot;
204
205 my @loop_orders = ();
206 unless( defined $invoice->{closedate} ) {
207     my $pendingorders;
208     if($op eq "search"){
209         my $search   = $input->param('summaryfilter') || '';
210         my $ean      = $input->param('eanfilter') || '';
211         my $basketname = $input->param('basketfilter') || '';
212         my $orderno  = $input->param('orderfilter') || '';
213         my $basketgroupname = $input->param('basketgroupnamefilter') || '';
214         $pendingorders = SearchOrders({
215             booksellerid => $booksellerid,
216             basketname => $basketname,
217             ordernumber => $orderno,
218             search => $search,
219             ean => $ean,
220             basketgroupname => $basketgroupname,
221             pending => 1,
222         });
223         $template->param(
224             summaryfilter => $search,
225             eanfilter => $ean,
226             basketfilter => $basketname,
227             orderfilter => $orderno,
228             basketgroupnamefilter => $basketgroupname,
229         );
230     }else{
231         $pendingorders = SearchOrders({
232             booksellerid => $booksellerid,
233             pending => 1
234         });
235     }
236     my $countpendings = scalar @$pendingorders;
237
238     for (my $i = 0 ; $i < $countpendings ; $i++) {
239         my %line;
240         %line = %{$pendingorders->[$i]};
241
242         my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
243         $line{unitprice} = get_value_with_gst_params( $line{unitprice}, $line{gstrate}, $bookseller );
244         $line{quantity} += 0;
245         $line{quantityreceived} += 0;
246         $line{unitprice}+=0;
247         $line{ecost} = sprintf( "%.2f", $ecost );
248         $line{ordertotal} = sprintf( "%.2f", $ecost * $line{quantity} );
249         $line{unitprice} = sprintf("%.2f",$line{unitprice});
250         $line{invoice} = $invoice;
251         $line{booksellerid} = $booksellerid;
252
253
254
255         my $biblionumber = $line{'biblionumber'};
256         my $countbiblio = CountBiblioInOrders($biblionumber);
257         my $ordernumber = $line{'ordernumber'};
258         my @subscriptions = GetSubscriptionsId ($biblionumber);
259         my $itemcount = GetItemsCount($biblionumber);
260         my $holds  = GetHolds ($biblionumber);
261         my @items = GetItemnumbersFromOrder( $ordernumber );
262         my $itemholds;
263         foreach my $item (@items){
264             my $nb = GetItemHolds($biblionumber, $item);
265             if ($nb){
266                 $itemholds += $nb;
267             }
268         }
269
270         my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
271         $line{suggestionid}         = $suggestion->{suggestionid};
272         $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
273         $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
274
275         # 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
276         $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
277         $line{items}                = ($itemcount) - (scalar @items);
278         $line{left_item}            = 1 if $line{items} >= 1;
279         $line{left_biblio}          = 1 if $countbiblio > 1;
280         $line{biblios}              = $countbiblio - 1;
281         $line{left_subscription}    = 1 if scalar @subscriptions >= 1;
282         $line{subscriptions}        = scalar @subscriptions;
283         $line{left_holds}           = ($holds >= 1) ? 1 : 0;
284         $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
285         $line{holds}                = $holds;
286         $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
287
288         my $budget = GetBudget( $line{budget_id} );
289         $line{budget_name} = $budget->{'budget_name'};
290
291         push @loop_orders, \%line;
292     }
293
294     $template->param(
295         loop_orders  => \@loop_orders,
296     );
297 }
298
299 $template->param(
300     invoiceid             => $invoice->{invoiceid},
301     invoice               => $invoice->{invoicenumber},
302     invoiceclosedate      => $invoice->{closedate},
303     datereceived          => $datereceived->output('iso'),
304     invoicedatereceived   => $datereceived->output('iso'),
305     formatteddatereceived => $datereceived->output(),
306     name                  => $bookseller->{'name'},
307     booksellerid          => $bookseller->{id},
308     countreceived         => $countlines,
309     loop_received         => \@loop_received,
310     loop_orders           => \@loop_orders,
311     book_foot_loop        => \@book_foot_loop,
312     totalprice            => sprintf($cfstr, $totalprice),
313     totalquantity         => $totalquantity,
314     (uc(C4::Context->preference("marcflavour"))) => 1,
315     total_quantity       => $total_quantity,
316     total_gste           => sprintf( "%.2f", $total_gste ),
317     total_gsti           => sprintf( "%.2f", $total_gsti ),
318 );
319 output_html_with_http_headers $input, $cookie, $template->output;