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