Bug 7295: More granular permissions for baskets
[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 @orders        = @{ $invoice->{orders} };
153 my $countlines    = scalar @orders;
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 $order ( @orders ) {
165     $order->{unitprice} = get_value_with_gst_params( $order->{unitprice}, $order->{gstrate}, $bookseller );
166     $total = ( $order->{unitprice} ) * $order->{quantityreceived};
167     $order->{'unitprice'} += 0;
168     my %line = %{ $order };
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     $line{holds} = 0;
175     my @itemnumbers = GetItemnumbersFromOrder( $order->{ordernumber} );
176     for my $itemnumber ( @itemnumbers ) {
177         my ( $count ) = &GetReservesFromBiblionumber($line{biblionumber}, undef, $itemnumber);
178         $line{holds} += $count;
179     }
180     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
181     $totalprice += $order->{unitprice};
182     $line{unitprice} = sprintf( $cfstr, $order->{unitprice} );
183     my $gste = get_gste( $line{total}, $line{gstrate}, $bookseller );
184     my $gst = get_gst( $line{total}, $line{gstrate}, $bookseller );
185     $foot{$line{gstrate}}{gstrate} = $line{gstrate};
186     $foot{$line{gstrate}}{value} += sprintf( "%.2f", $gst );
187     $total_quantity += $line{quantity};
188     $total_gste += $gste;
189     $total_gsti += $gste + $gst;
190
191     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
192     $line{suggestionid}         = $suggestion->{suggestionid};
193     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
194     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
195
196     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
197         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
198             @orders
199             )
200         {
201             $line{cannot_cancel} = 1;
202         }
203     }
204
205     my $budget = GetBudget( $line{budget_id} );
206     $line{budget_name} = $budget->{'budget_name'};
207
208     push @loop_received, \%line;
209     $totalquantity += $order->{quantityreceived};
210
211 }
212 push @book_foot_loop, map { $_ } values %foot;
213
214 my @loop_orders = ();
215 unless( defined $invoice->{closedate} ) {
216     my $pendingorders;
217     if($op eq "search"){
218         my $search   = $input->param('summaryfilter') || '';
219         my $ean      = $input->param('eanfilter') || '';
220         my $basketname = $input->param('basketfilter') || '';
221         my $orderno  = $input->param('orderfilter') || '';
222         my $basketgroupname = $input->param('basketgroupnamefilter') || '';
223         $pendingorders = SearchOrders({
224             booksellerid => $booksellerid,
225             basketname => $basketname,
226             ordernumber => $orderno,
227             search => $search,
228             ean => $ean,
229             basketgroupname => $basketgroupname,
230             pending => 1,
231         });
232         $template->param(
233             summaryfilter => $search,
234             eanfilter => $ean,
235             basketfilter => $basketname,
236             orderfilter => $orderno,
237             basketgroupnamefilter => $basketgroupname,
238         );
239     }else{
240         $pendingorders = SearchOrders({
241             booksellerid => $booksellerid,
242             pending => 1
243         });
244     }
245     my $countpendings = scalar @$pendingorders;
246
247     for (my $i = 0 ; $i < $countpendings ; $i++) {
248         my %line;
249         %line = %{$pendingorders->[$i]};
250
251         my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
252         $line{unitprice} = get_value_with_gst_params( $line{unitprice}, $line{gstrate}, $bookseller );
253         $line{quantity} += 0;
254         $line{quantityreceived} += 0;
255         $line{unitprice}+=0;
256         $line{ecost} = sprintf( "%.2f", $ecost );
257         $line{ordertotal} = sprintf( "%.2f", $ecost * $line{quantity} );
258         $line{unitprice} = sprintf("%.2f",$line{unitprice});
259         $line{invoice} = $invoice;
260         $line{booksellerid} = $booksellerid;
261
262
263
264         my $biblionumber = $line{'biblionumber'};
265         my $countbiblio = CountBiblioInOrders($biblionumber);
266         my $ordernumber = $line{'ordernumber'};
267         my @subscriptions = GetSubscriptionsId ($biblionumber);
268         my $itemcount = GetItemsCount($biblionumber);
269         my $holds  = GetHolds ($biblionumber);
270         my @items = GetItemnumbersFromOrder( $ordernumber );
271         my $itemholds;
272         foreach my $item (@items){
273             my $nb = GetItemHolds($biblionumber, $item);
274             if ($nb){
275                 $itemholds += $nb;
276             }
277         }
278
279         my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
280         $line{suggestionid}         = $suggestion->{suggestionid};
281         $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
282         $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
283
284         # 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
285         $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
286         $line{items}                = ($itemcount) - (scalar @items);
287         $line{left_item}            = 1 if $line{items} >= 1;
288         $line{left_biblio}          = 1 if $countbiblio > 1;
289         $line{biblios}              = $countbiblio - 1;
290         $line{left_subscription}    = 1 if scalar @subscriptions >= 1;
291         $line{subscriptions}        = scalar @subscriptions;
292         $line{left_holds}           = ($holds >= 1) ? 1 : 0;
293         $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
294         $line{holds}                = $holds;
295         $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
296
297         my $budget = GetBudget( $line{budget_id} );
298         $line{budget_name} = $budget->{'budget_name'};
299
300         push @loop_orders, \%line;
301     }
302
303     $template->param(
304         loop_orders  => \@loop_orders,
305     );
306 }
307
308 $template->param(
309     invoiceid             => $invoice->{invoiceid},
310     invoice               => $invoice->{invoicenumber},
311     invoiceclosedate      => $invoice->{closedate},
312     datereceived          => $datereceived->output('iso'),
313     invoicedatereceived   => $datereceived->output('iso'),
314     formatteddatereceived => $datereceived->output(),
315     name                  => $bookseller->{'name'},
316     booksellerid          => $bookseller->{id},
317     countreceived         => $countlines,
318     loop_received         => \@loop_received,
319     loop_orders           => \@loop_orders,
320     book_foot_loop        => \@book_foot_loop,
321     totalprice            => sprintf($cfstr, $totalprice),
322     totalquantity         => $totalquantity,
323     (uc(C4::Context->preference("marcflavour"))) => 1,
324     total_quantity       => $total_quantity,
325     total_gste           => sprintf( "%.2f", $total_gste ),
326     total_gsti           => sprintf( "%.2f", $total_gsti ),
327 );
328 output_html_with_http_headers $input, $cookie, $template->output;