Bug 28591: Don't pass debug to get_template_and_user
[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 it 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 Modern::Perl;
58
59 use C4::Auth;
60 use C4::Acquisition;
61 use C4::Budgets;
62 use C4::Biblio;
63 use C4::Items;
64 use CGI qw ( -utf8 );
65 use C4::Output;
66 use C4::Suggestions;
67
68 use Koha::Acquisition::Baskets;
69 use Koha::Acquisition::Bookseller;
70 use Koha::Acquisition::Orders;
71 use Koha::Biblios;
72 use Koha::DateUtils;
73 use Koha::Biblios;
74
75 use JSON;
76
77 my $input = CGI->new;
78
79 my ($template, $loggedinuser, $cookie)
80     = get_template_and_user({template_name => "acqui/parcel.tt",
81                  query => $input,
82                  type => "intranet",
83                  flagsrequired => {acquisition => 'order_receive'},
84 });
85
86 my $op = $input->param('op') // '';
87
88 # process cancellation first so that list of
89 # orders to display is calculated after
90 if ($op eq 'cancelreceipt') {
91     my $ordernumber = $input->param('ordernumber');
92     my $parent_ordernumber = CancelReceipt($ordernumber);
93     unless($parent_ordernumber) {
94         $template->param(error_cancelling_receipt => 1);
95     }
96 }
97
98 my $invoiceid = $input->param('invoiceid');
99 my $invoice;
100 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
101
102 unless( $invoiceid and $invoice->{invoiceid} ) {
103     $template->param(
104         error_invoice_not_known => 1,
105         no_orders_to_display    => 1
106     );
107     output_html_with_http_headers $input, $cookie, $template->output;
108     exit;
109 }
110
111 my $booksellerid = $invoice->{booksellerid};
112 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
113
114 my @orders        = @{ $invoice->{orders} };
115 my $countlines    = scalar @orders;
116 my @loop_received = ();
117 my @book_foot_loop;
118 my %foot;
119 my $total_tax_excluded = 0;
120 my $total_tax_included = 0;
121
122 my $subtotal_for_funds;
123 for my $order ( @orders ) {
124     $order->{'unitprice'} += 0;
125
126     my $order_object = Koha::Acquisition::Orders->find($order->{ordernumber});
127     if ( $bookseller->invoiceincgst ) {
128         $order->{ecost}     = $order->{ecost_tax_included};
129         $order->{unitprice} = $order->{unitprice_tax_included};
130     }
131     else {
132         $order->{ecost}     = $order->{ecost_tax_excluded};
133         $order->{unitprice} = $order->{unitprice_tax_excluded};
134     }
135
136     $order->{total} = get_rounded_price($order->{unitprice}) * $order->{quantity};
137
138     my %line = %{ $order };
139     $line{invoice} = $invoice->{invoicenumber};
140     my @itemnumbers = $order_object->items->get_column('itemnumber');
141     my $biblio = Koha::Biblios->find( $line{biblionumber} );
142     $line{total_holds} = $biblio ? $biblio->holds->count : 0;
143     $line{item_holds} = $biblio ? $biblio->current_holds->search(
144         {
145             itemnumber => { -in => \@itemnumbers },
146         }
147     )->count : 0;
148     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
149
150     $line{tax_value} = $line{tax_value_on_receiving};
151     $line{tax_rate} = $line{tax_rate_on_receiving};
152     $foot{$line{tax_rate}}{tax_rate} = $line{tax_rate};
153     $foot{$line{tax_rate}}{tax_value} += $line{tax_value};
154     $total_tax_excluded += get_rounded_price($line{unitprice_tax_excluded}) * $line{quantity};
155     $total_tax_included += get_rounded_price($line{unitprice_tax_included}) * $line{quantity};
156
157     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
158     $line{suggestionid}         = $suggestion->{suggestionid};
159     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
160     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
161
162     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
163         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
164             @orders
165             )
166         {
167             $line{cannot_cancel} = 1;
168         }
169     }
170
171     my $budget_name = GetBudgetName( $line{budget_id} );
172     $line{budget_name} = $budget_name;
173
174     $subtotal_for_funds->{ $line{budget_name} }{ecost} += get_rounded_price($order->{ecost}) * $order->{quantity};
175     $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
176
177     push @loop_received, \%line;
178 }
179 push @book_foot_loop, map { $_ } values %foot;
180
181 $template->param(
182     invoiceid             => $invoice->{invoiceid},
183     invoice               => $invoice->{invoicenumber},
184     invoiceclosedate      => $invoice->{closedate},
185     shipmentdate         => $invoice->{shipmentdate},
186     name                  => $bookseller->name,
187     booksellerid          => $bookseller->id,
188     loop_received         => \@loop_received,
189     book_foot_loop        => \@book_foot_loop,
190     (uc(C4::Context->preference("marcflavour"))) => 1,
191     total_tax_excluded    => $total_tax_excluded,
192     total_tax_included    => $total_tax_included,
193     subtotal_for_funds    => $subtotal_for_funds,
194 );
195 output_html_with_http_headers $input, $cookie, $template->output;