Bug 13320: Move price calculation from tt to pl
[koha.git] / acqui / orderreceive.pl
blob96a6a024bde9fd5d5e68053ba89ca28be624c78d
1 #!/usr/bin/perl
4 #script to recieve orders
5 #written by chris@katipo.co.nz 24/2/2000
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
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.
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.
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.
24 =head1 NAME
26 orderreceive.pl
28 =head1 DESCRIPTION
30 This script shows all order already receive and all pendings orders.
31 It permit to write a new order as 'received'.
33 =head1 CGI PARAMETERS
35 =over 4
37 =item booksellerid
39 to know on what supplier this script has to display receive order.
41 =item invoiceid
43 the id of this invoice.
45 =item freight
47 =item biblio
49 The biblionumber of this order.
51 =item datereceived
53 =item catview
55 =item gst
57 =back
59 =cut
61 use strict;
62 use warnings;
64 use CGI qw ( -utf8 );
65 use C4::Context;
66 use C4::Koha; # GetKohaAuthorisedValues GetItemTypes
67 use C4::Acquisition;
68 use C4::Auth;
69 use C4::Output;
70 use C4::Dates qw/format_date/;
71 use C4::Budgets qw/ GetBudget GetBudgetHierarchy CanUserUseBudget GetBudgetPeriods /;
72 use C4::Members;
73 use C4::Branch; # GetBranches
74 use C4::Items;
75 use C4::Biblio;
76 use C4::Suggestions;
78 use Koha::Acquisition::Bookseller;
81 my $input = new CGI;
83 my $dbh = C4::Context->dbh;
84 my $invoiceid = $input->param('invoiceid');
85 my $invoice = GetInvoice($invoiceid);
86 my $booksellerid = $invoice->{booksellerid};
87 my $freight = $invoice->{shipmentcost};
88 my $datereceived = $invoice->{shipmentdate};
89 my $ordernumber = $input->param('ordernumber');
91 $datereceived = $datereceived ? C4::Dates->new($datereceived, 'iso') : C4::Dates->new();
93 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
94 my $results;
95 $results = SearchOrders({
96 ordernumber => $ordernumber
97 }) if $ordernumber;
99 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
101 template_name => "acqui/orderreceive.tt",
102 query => $input,
103 type => "intranet",
104 authnotrequired => 0,
105 flagsrequired => {acquisition => 'order_receive'},
106 debug => 1,
110 unless ( $results and @$results) {
111 output_html_with_http_headers $input, $cookie, $template->output;
112 exit;
115 # prepare the form for receiving
116 my $order = $results->[0];
118 # Check if ACQ framework exists
119 my $acq_fw = GetMarcStructure(1, 'ACQ');
120 unless($acq_fw) {
121 $template->param('NoACQframework' => 1);
124 my $AcqCreateItem = C4::Context->preference('AcqCreateItem');
125 if ($AcqCreateItem eq 'receiving') {
126 $template->param(
127 AcqCreateItemReceiving => 1,
128 UniqueItemFields => C4::Context->preference('UniqueItemFields'),
130 } elsif ($AcqCreateItem eq 'ordering') {
131 my $fw = ($acq_fw) ? 'ACQ' : '';
132 my @itemnumbers = GetItemnumbersFromOrder($order->{ordernumber});
133 my @items;
134 foreach (@itemnumbers) {
135 my $item = GetItem($_);
136 if($item->{homebranch}) {
137 $item->{homebranchname} = GetBranchName($item->{homebranch});
139 if($item->{holdingbranch}) {
140 $item->{holdingbranchname} = GetBranchName($item->{holdingbranch});
142 if(my $code = GetAuthValCode("items.notforloan", $fw)) {
143 $item->{notforloan} = GetKohaAuthorisedValueLib($code, $item->{notforloan});
145 if(my $code = GetAuthValCode("items.restricted", $fw)) {
146 $item->{restricted} = GetKohaAuthorisedValueLib($code, $item->{restricted});
148 if(my $code = GetAuthValCode("items.location", $fw)) {
149 $item->{location} = GetKohaAuthorisedValueLib($code, $item->{location});
151 if(my $code = GetAuthValCode("items.ccode", $fw)) {
152 $item->{collection} = GetKohaAuthorisedValueLib($code, $item->{ccode});
154 if(my $code = GetAuthValCode("items.materials", $fw)) {
155 $item->{materials} = GetKohaAuthorisedValueLib($code, $item->{materials});
157 my $itemtype = getitemtypeinfo($item->{itype});
158 $item->{itemtype} = $itemtype->{description};
159 push @items, $item;
161 $template->param(items => \@items);
164 $order->{quantityreceived} = '' if $order->{quantityreceived} == 0;
165 $order->{unitprice} = '' if $order->{unitprice} == 0;
167 my $rrp;
168 my $ecost;
169 my $unitprice;
170 if ( $bookseller->{listincgst} ) {
171 if ( $bookseller->{invoiceincgst} ) {
172 $rrp = $order->{rrp};
173 $ecost = $order->{ecost};
174 $unitprice = $order->{unitprice};
175 } else {
176 $rrp = $order->{rrp} / ( 1 + $order->{gstrate} );
177 $ecost = $order->{ecost} / ( 1 + $order->{gstrate} );
178 $unitprice = $order->{unitprice} / ( 1 + $order->{gstrate} );
180 } else {
181 if ( $bookseller->{invoiceincgst} ) {
182 $rrp = $order->{rrp} * ( 1 + $order->{gstrate} );
183 $ecost = $order->{ecost} * ( 1 + $order->{gstrate} );
184 $unitprice = $order->{unitprice} * ( 1 + $order->{gstrate} );
185 } else {
186 $rrp = $order->{rrp};
187 $ecost = $order->{ecost};
188 $unitprice = $order->{unitprice};
192 my $suggestion = GetSuggestionInfoFromBiblionumber($order->{biblionumber});
194 my $authorisedby = $order->{authorisedby};
195 my $member = GetMember( borrowernumber => $authorisedby );
197 my $budget = GetBudget( $order->{budget_id} );
199 $template->param(
200 AcqCreateItem => $AcqCreateItem,
201 count => 1,
202 biblionumber => $order->{'biblionumber'},
203 ordernumber => $order->{'ordernumber'},
204 subscriptionid => $order->{subscriptionid},
205 booksellerid => $order->{'booksellerid'},
206 freight => $freight,
207 name => $bookseller->{'name'},
208 date => format_date($order->{entrydate}),
209 title => $order->{'title'},
210 author => $order->{'author'},
211 copyrightdate => $order->{'copyrightdate'},
212 isbn => $order->{'isbn'},
213 seriestitle => $order->{'seriestitle'},
214 bookfund => $budget->{budget_name},
215 quantity => $order->{'quantity'},
216 quantityreceivedplus1 => $order->{'quantityreceived'} + 1,
217 quantityreceived => $order->{'quantityreceived'},
218 rrp => sprintf( "%.2f", $rrp ),
219 ecost => sprintf( "%.2f", $ecost ),
220 memberfirstname => $member->{firstname} || "",
221 membersurname => $member->{surname} || "",
222 invoiceid => $invoice->{invoiceid},
223 invoice => $invoice->{invoicenumber},
224 datereceived => $datereceived->output(),
225 datereceived_iso => $datereceived->output('iso'),
226 order_internalnote => $order->{order_internalnote},
227 order_vendornote => $order->{order_vendornote},
228 suggestionid => $suggestion->{suggestionid},
229 surnamesuggestedby => $suggestion->{surnamesuggestedby},
230 firstnamesuggestedby => $suggestion->{firstnamesuggestedby},
233 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
234 my @budget_loop;
235 my $periods = GetBudgetPeriods( );
236 foreach my $period (@$periods) {
237 if ($period->{'budget_period_id'} == $budget->{'budget_period_id'}) {
238 $template->{'VARS'}->{'budget_period_description'} = $period->{'budget_period_description'};
240 next if $period->{'budget_period_locked'} || !$period->{'budget_period_description'};
241 my $budget_hierarchy = GetBudgetHierarchy( $period->{'budget_period_id'} );
242 my @funds;
243 foreach my $r ( @{$budget_hierarchy} ) {
244 next unless ( CanUserUseBudget( $borrower, $r, $userflags ) );
245 if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
246 next;
248 push @funds,
250 b_id => $r->{budget_id},
251 b_txt => $r->{budget_name},
252 b_sel => ( $r->{budget_id} == $order->{budget_id} ) ? 1 : 0,
256 @funds = sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @funds;
258 push @budget_loop,
260 'id' => $period->{'budget_period_id'},
261 'description' => $period->{'budget_period_description'},
262 'funds' => \@funds
266 $template->{'VARS'}->{'budget_loop'} = \@budget_loop;
268 # regardless of the content of $unitprice e.g 0 or '' or any string will return in these cases 0.00
269 # and the 'IF' in the .tt will show 0.00 and not 'ecost' (see BZ 7129)
270 # So if $unitprice == 0 we don't create unitprice
271 if ( $unitprice != 0) {
272 $template->param(
273 unitprice => sprintf( "%.2f", $unitprice),
277 my $op = $input->param('op');
278 if ($op and $op eq 'edit'){
279 $template->param(edit => 1);
281 output_html_with_http_headers $input, $cookie, $template->output;