Bug 12976: Use the centralize VAT and prices calculation - invoice.pl
[koha.git] / acqui / orderreceive.pl
1 #!/usr/bin/perl
2
3
4 #script to recieve 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 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 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 strict;
62 use warnings;
63
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;
77
78 use Koha::Acquisition::Bookseller;
79
80
81 my $input      = new CGI;
82
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');
90
91 $datereceived = $datereceived ? C4::Dates->new($datereceived, 'iso') : C4::Dates->new();
92
93 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
94 my $results;
95 $results = SearchOrders({
96     ordernumber => $ordernumber
97 }) if $ordernumber;
98
99 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
100     {
101         template_name   => "acqui/orderreceive.tt",
102         query           => $input,
103         type            => "intranet",
104         authnotrequired => 0,
105         flagsrequired   => {acquisition => 'order_receive'},
106         debug           => 1,
107     }
108 );
109
110 unless ( $results and @$results) {
111     output_html_with_http_headers $input, $cookie, $template->output;
112     exit;
113 }
114
115 # prepare the form for receiving
116 my $order = $results->[0];
117
118 # Check if ACQ framework exists
119 my $acq_fw = GetMarcStructure(1, 'ACQ');
120 unless($acq_fw) {
121     $template->param('NoACQframework' => 1);
122 }
123
124 my $AcqCreateItem = C4::Context->preference('AcqCreateItem');
125 if ($AcqCreateItem eq 'receiving') {
126     $template->param(
127         AcqCreateItemReceiving => 1,
128         UniqueItemFields => C4::Context->preference('UniqueItemFields'),
129     );
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});
138         }
139         if($item->{holdingbranch}) {
140             $item->{holdingbranchname} = GetBranchName($item->{holdingbranch});
141         }
142         if(my $code = GetAuthValCode("items.notforloan", $fw)) {
143             $item->{notforloan} = GetKohaAuthorisedValueLib($code, $item->{notforloan});
144         }
145         if(my $code = GetAuthValCode("items.restricted", $fw)) {
146             $item->{restricted} = GetKohaAuthorisedValueLib($code, $item->{restricted});
147         }
148         if(my $code = GetAuthValCode("items.location", $fw)) {
149             $item->{location} = GetKohaAuthorisedValueLib($code, $item->{location});
150         }
151         if(my $code = GetAuthValCode("items.ccode", $fw)) {
152             $item->{collection} = GetKohaAuthorisedValueLib($code, $item->{ccode});
153         }
154         if(my $code = GetAuthValCode("items.materials", $fw)) {
155             $item->{materials} = GetKohaAuthorisedValueLib($code, $item->{materials});
156         }
157         my $itemtype = getitemtypeinfo($item->{itype});
158         $item->{itemtype} = $itemtype->{description};
159         push @items, $item;
160     }
161     $template->param(items => \@items);
162 }
163
164 $order->{quantityreceived} = '' if $order->{quantityreceived} == 0;
165 $order->{unitprice} = '' if $order->{unitprice} == 0;
166
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} );
179     }
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};
189     }
190  }
191
192 my $suggestion = GetSuggestionInfoFromBiblionumber($order->{biblionumber});
193
194 my $authorisedby = $order->{authorisedby};
195 my $member = GetMember( borrowernumber => $authorisedby );
196
197 my $budget = GetBudget( $order->{budget_id} );
198
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},
231 );
232
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'};
239     }
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;
247         }
248         push @funds,
249           {
250             b_id  => $r->{budget_id},
251             b_txt => $r->{budget_name},
252             b_sel => ( $r->{budget_id} == $order->{budget_id} ) ? 1 : 0,
253           };
254     }
255
256     @funds = sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @funds;
257
258     push @budget_loop,
259       {
260         'id'          => $period->{'budget_period_id'},
261         'description' => $period->{'budget_period_description'},
262         'funds'       => \@funds
263       };
264 }
265
266 $template->{'VARS'}->{'budget_loop'} = \@budget_loop;
267
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),
274     );
275 }
276
277 my $op = $input->param('op');
278 if ($op and $op eq 'edit'){
279     $template->param(edit   =>   1);
280 }
281 output_html_with_http_headers $input, $cookie, $template->output;