Bug 23376: Clean up order receipt page code
[koha.git] / acqui / orderreceive.pl
1 #!/usr/bin/perl
2
3
4 #script to receive orders
5 #written by chris@katipo.co.nz 24/2/2000
6
7 # Copyright 2000-2002 Katipo Communications
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 orderreceive.pl
27
28 =head1 DESCRIPTION
29
30 This script shows all order already receive and all pendings orders.
31 It permit to write a new order as 'received'.
32
33 =head1 CGI PARAMETERS
34
35 =over 4
36
37 =item booksellerid
38
39 to know on what supplier this script has to display receive order.
40
41 =item invoiceid
42
43 the id of this invoice.
44
45 =item freight
46
47 =item biblio
48
49 The biblionumber of this order.
50
51 =item datereceived
52
53 =item catview
54
55 =item gst
56
57 =back
58
59 =cut
60
61 use Modern::Perl;
62
63 use CGI qw ( -utf8 );
64 use C4::Context;
65 use C4::Acquisition;
66 use C4::Auth;
67 use C4::Output;
68 use C4::Budgets qw/ GetBudget GetBudgetHierarchy CanUserUseBudget GetBudgetPeriods /;
69 use C4::Members;
70 use C4::Items;
71 use C4::Biblio;
72 use C4::Suggestions;
73 use C4::Koha;
74
75 use Koha::Acquisition::Booksellers;
76 use Koha::Acquisition::Currencies;
77 use Koha::Acquisition::Orders;
78 use Koha::DateUtils qw( dt_from_string );
79 use Koha::ItemTypes;
80 use Koha::Patrons;
81
82 my $input      = new CGI;
83
84 my $dbh          = C4::Context->dbh;
85 my $invoiceid    = $input->param('invoiceid');
86 my $invoice      = GetInvoice($invoiceid);
87 my $booksellerid   = $invoice->{booksellerid};
88 my $freight      = $invoice->{shipmentcost};
89 my $ordernumber  = $input->param('ordernumber');
90
91 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
92 my $order = Koha::Acquisition::Orders->find( $ordernumber );
93
94 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
95     {
96         template_name   => "acqui/orderreceive.tt",
97         query           => $input,
98         type            => "intranet",
99         authnotrequired => 0,
100         flagsrequired   => {acquisition => 'order_receive'},
101         debug           => 1,
102     }
103 );
104
105 unless ( $order ) {
106     output_html_with_http_headers $input, $cookie, $template->output;
107     exit;
108 }
109
110 # prepare the form for receiving
111 my $basket = $order->basket;
112 my $currencies = Koha::Acquisition::Currencies->search;
113 my $active_currency = $currencies->get_active;
114
115 # Check if ACQ framework exists
116 my $acq_fw = GetMarcStructure( 1, 'ACQ', { unsafe => 1 } );
117 unless($acq_fw) {
118     $template->param('NoACQframework' => 1);
119 }
120
121 my $AcqCreateItem = $basket->effective_create_items;
122 if ($AcqCreateItem eq 'receiving') {
123     $template->param(
124         AcqCreateItemReceiving => 1,
125         UniqueItemFields => C4::Context->preference('UniqueItemFields'),
126     );
127 } elsif ($AcqCreateItem eq 'ordering') {
128     my $fw = ($acq_fw) ? 'ACQ' : '';
129     my $items = $order->items;
130     $template->param(items => $items);
131 }
132
133 $order->quantityreceived = '' if $order->quantityreceived == 0;
134
135 my $unitprice = $order->unitprice;
136 my ( $rrp, $ecost );
137 if ( $bookseller->invoiceincgst ) {
138     $rrp = $order->rrp_tax_included;
139     $ecost = $order->ecost_tax_included;
140     unless ( $unitprice != 0 and defined $unitprice) {
141         $unitprice = $order->ecost_tax_included;
142     }
143 } else {
144     $rrp = $order->rrp_tax_excluded;
145     $ecost = $order->ecost_tax_excluded;
146     unless ( $unitprice != 0 and defined $unitprice) {
147         $unitprice = $order->ecost_tax_excluded;
148     }
149 }
150
151 my $tax_rate;
152 if( defined $order->tax_rate_on_receiving ) {
153     $tax_rate = $order->tax_rate_on_receiving + 0.0;
154 } else {
155     $tax_rate = $order->tax_rate_on_ordering + 0.0;
156 }
157
158 my $creator = Koha::Patrons->find( $order->created_by );
159
160 my $budget = GetBudget( $order->budget_id );
161
162 my $datereceived = $order->datereceived ? dt_from_string( $order->datereceived ) : dt_from_string;
163
164 # get option values for gist syspref
165 my @gst_values = map {
166     option => $_ + 0.0
167 }, split( '\|', C4::Context->preference("gist") );
168
169 my $order_internalnote = $order->order_internalnote;
170 my $order_vendornote   = $order->order_vendornote;
171 if ( $order->subscriptionid ) {
172     # Order from a subscription, we will display an history of what has been received
173     my $orders = Koha::Acquisition::Orders->search(
174         {
175             subscriptionid     => $order->subscriptionid,
176             parent_ordernumber => $order->ordernumber,
177             ordernumber        => { '!=' => $order->ordernumber }
178         }
179     );
180     if ( $order->parent_ordernumber != $order->ordernumber ) {
181         my $parent_order = Koha::Acquisition::Orders->find($order->parent_ordernumber);
182         $order_internalnote = $parent_order->order_internalnote;
183         $order_vendornote   = $parent_order->order_vendornote;
184     }
185     $template->param(
186         orders => $orders,
187     );
188 }
189
190 $template->param(
191     AcqCreateItem         => $AcqCreateItem,
192     count                 => 1,
193     order                 => $order,
194     freight               => $freight,
195     name                  => $bookseller->name,
196     active_currency       => $active_currency,
197     currencies            => scalar $currencies->search({ rate => { '!=' => 1 } }),
198     invoiceincgst         => $bookseller->invoiceincgst,
199     bookfund              => $budget->{budget_name},
200     rrp                   => $rrp,
201     ecost                 => $ecost,
202     creator               => $creator,
203     invoiceid             => $invoice->{invoiceid},
204     invoice               => $invoice->{invoicenumber},
205     datereceived          => $datereceived,
206     order_internalnote    => $order_internalnote,
207     order_vendornote      => $order_vendornote,
208     gst_values            => \@gst_values,
209 );
210
211 my $suggestion = GetSuggestionInfoFromBiblionumber($order->{biblionumber});
212 if ( $suggestion ) {
213     $template->param( suggestion => $suggestion );
214 }
215
216 my $patron = Koha::Patrons->find( $loggedinuser )->unblessed;
217 my @budget_loop;
218 my $periods = GetBudgetPeriods( );
219 foreach my $period (@$periods) {
220     if ($period->{'budget_period_id'} == $budget->{'budget_period_id'}) {
221         $template->{'VARS'}->{'budget_period_description'} = $period->{'budget_period_description'};
222     }
223     next if $period->{'budget_period_locked'} || !$period->{'budget_period_description'};
224     my $budget_hierarchy = GetBudgetHierarchy( $period->{'budget_period_id'} );
225     my @funds;
226     foreach my $r ( @{$budget_hierarchy} ) {
227         next unless ( CanUserUseBudget( $patron, $r, $userflags ) );
228         push @funds,
229           {
230             b_id  => $r->{budget_id},
231             b_txt => $r->{budget_name},
232             b_sel => ( $r->{budget_id} == $order->{budget_id} ) ? 1 : 0,
233           };
234     }
235
236     @funds = sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @funds;
237
238     push @budget_loop,
239       {
240         'id'          => $period->{'budget_period_id'},
241         'description' => $period->{'budget_period_description'},
242         'funds'       => \@funds
243       };
244 }
245
246 $template->{'VARS'}->{'budget_loop'} = \@budget_loop;
247
248 my $op = $input->param('op');
249 if ($op and $op eq 'edit'){
250     $template->param(edit   =>   1);
251 }
252 output_html_with_http_headers $input, $cookie, $template->output;