Bug 10694: (follow-up) fix various issues
[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;
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::Bookseller qw/ GetBookSellerFromId /;
72 use C4::Budgets qw/ GetBudget GetBudgetHierarchy CanUserUseBudget GetBudgetPeriods /;
73 use C4::Members;
74 use C4::Branch;    # GetBranches
75 use C4::Items;
76 use C4::Biblio;
77 use C4::Suggestions;
78
79
80 my $input      = new CGI;
81
82 my $dbh          = C4::Context->dbh;
83 my $invoiceid    = $input->param('invoiceid');
84 my $invoice      = GetInvoice($invoiceid);
85 my $booksellerid   = $invoice->{booksellerid};
86 my $freight      = $invoice->{shipmentcost};
87 my $datereceived = $invoice->{shipmentdate};
88 my $ordernumber  = $input->param('ordernumber');
89
90 $datereceived = $datereceived ? C4::Dates->new($datereceived, 'iso') : C4::Dates->new();
91
92 my $bookseller = GetBookSellerFromId($booksellerid);
93 my $results;
94 $results = SearchOrders({
95     ordernumber => $ordernumber
96 }) if $ordernumber;
97
98 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
99     {
100         template_name   => "acqui/orderreceive.tmpl",
101         query           => $input,
102         type            => "intranet",
103         authnotrequired => 0,
104         flagsrequired   => {acquisition => 'order_receive'},
105         debug           => 1,
106     }
107 );
108
109 unless ( $results and @$results) {
110     output_html_with_http_headers $input, $cookie, $template->output;
111     exit;
112 }
113
114 # prepare the form for receiving
115 my $order = $results->[0];
116
117 # Check if ACQ framework exists
118 my $acq_fw = GetMarcStructure(1, 'ACQ');
119 unless($acq_fw) {
120     $template->param('NoACQframework' => 1);
121 }
122
123 my $AcqCreateItem = C4::Context->preference('AcqCreateItem');
124 if ($AcqCreateItem eq 'receiving') {
125     $template->param(
126         AcqCreateItemReceiving => 1,
127         UniqueItemFields => C4::Context->preference('UniqueItemFields'),
128     );
129 } elsif ($AcqCreateItem eq 'ordering') {
130     my $fw = ($acq_fw) ? 'ACQ' : '';
131     my @itemnumbers = GetItemnumbersFromOrder($order->{ordernumber});
132     my @items;
133     foreach (@itemnumbers) {
134         my $item = GetItem($_);
135         if($item->{homebranch}) {
136             $item->{homebranchname} = GetBranchName($item->{homebranch});
137         }
138         if($item->{holdingbranch}) {
139             $item->{holdingbranchname} = GetBranchName($item->{holdingbranch});
140         }
141         if(my $code = GetAuthValCode("items.notforloan", $fw)) {
142             $item->{notforloan} = GetKohaAuthorisedValueLib($code, $item->{notforloan});
143         }
144         if(my $code = GetAuthValCode("items.restricted", $fw)) {
145             $item->{restricted} = GetKohaAuthorisedValueLib($code, $item->{restricted});
146         }
147         if(my $code = GetAuthValCode("items.location", $fw)) {
148             $item->{location} = GetKohaAuthorisedValueLib($code, $item->{location});
149         }
150         if(my $code = GetAuthValCode("items.ccode", $fw)) {
151             $item->{collection} = GetKohaAuthorisedValueLib($code, $item->{ccode});
152         }
153         if(my $code = GetAuthValCode("items.materials", $fw)) {
154             $item->{materials} = GetKohaAuthorisedValueLib($code, $item->{materials});
155         }
156         my $itemtype = getitemtypeinfo($item->{itype});
157         $item->{itemtype} = $itemtype->{description};
158         push @items, $item;
159     }
160     $template->param(items => \@items);
161 }
162
163 $order->{quantityreceived} = '' if $order->{quantityreceived} == 0;
164 $order->{unitprice} = '' if $order->{unitprice} == 0;
165
166 my $rrp;
167 my $ecost;
168 my $unitprice;
169 if ( $bookseller->{listincgst} ) {
170     if ( $bookseller->{invoiceincgst} ) {
171         $rrp = $order->{rrp};
172         $ecost = $order->{ecost};
173         $unitprice = $order->{unitprice};
174     } else {
175         $rrp = $order->{rrp} / ( 1 + $order->{gstrate} );
176         $ecost = $order->{ecost} / ( 1 + $order->{gstrate} );
177         $unitprice = $order->{unitprice} / ( 1 + $order->{gstrate} );
178     }
179 } else {
180     if ( $bookseller->{invoiceincgst} ) {
181         $rrp = $order->{rrp} * ( 1 + $order->{gstrate} );
182         $ecost = $order->{ecost} * ( 1 + $order->{gstrate} );
183         $unitprice = $order->{unitprice} * ( 1 + $order->{gstrate} );
184     } else {
185         $rrp = $order->{rrp};
186         $ecost = $order->{ecost};
187         $unitprice = $order->{unitprice};
188     }
189  }
190
191 my $suggestion = GetSuggestionInfoFromBiblionumber($order->{biblionumber});
192
193 my $authorisedby = $order->{authorisedby};
194 my $member = GetMember( borrowernumber => $authorisedby );
195
196 my $budget = GetBudget( $order->{budget_id} );
197
198 $template->param(
199     AcqCreateItem         => $AcqCreateItem,
200     count                 => 1,
201     biblionumber          => $order->{'biblionumber'},
202     ordernumber           => $order->{'ordernumber'},
203     subscriptionid        => $order->{subscriptionid},
204     booksellerid          => $order->{'booksellerid'},
205     freight               => $freight,
206     name                  => $bookseller->{'name'},
207     date                  => format_date($order->{entrydate}),
208     title                 => $order->{'title'},
209     author                => $order->{'author'},
210     copyrightdate         => $order->{'copyrightdate'},
211     isbn                  => $order->{'isbn'},
212     seriestitle           => $order->{'seriestitle'},
213     bookfund              => $budget->{budget_name},
214     quantity              => $order->{'quantity'},
215     quantityreceivedplus1 => $order->{'quantityreceived'} + 1,
216     quantityreceived      => $order->{'quantityreceived'},
217     rrp                   => sprintf( "%.2f", $rrp ),
218     ecost                 => sprintf( "%.2f", $ecost ),
219     memberfirstname       => $member->{firstname} || "",
220     membersurname         => $member->{surname} || "",
221     invoiceid             => $invoice->{invoiceid},
222     invoice               => $invoice->{invoicenumber},
223     datereceived          => $datereceived->output(),
224     datereceived_iso      => $datereceived->output('iso'),
225     order_internalnote    => $order->{order_internalnote},
226     order_vendornote      => $order->{order_vendornote},
227     suggestionid          => $suggestion->{suggestionid},
228     surnamesuggestedby    => $suggestion->{surnamesuggestedby},
229     firstnamesuggestedby  => $suggestion->{firstnamesuggestedby},
230 );
231
232 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
233 my @budget_loop;
234 my $periods = GetBudgetPeriods( );
235 foreach my $period (@$periods) {
236     if ($period->{'budget_period_id'} == $budget->{'budget_period_id'}) {
237         $template->{'VARS'}->{'budget_period_description'} = $period->{'budget_period_description'};
238     }
239     next if $period->{'budget_period_locked'} || !$period->{'budget_period_description'};
240     my $budget_hierarchy = GetBudgetHierarchy( $period->{'budget_period_id'} );
241     my @funds;
242     foreach my $r ( @{$budget_hierarchy} ) {
243         next unless ( CanUserUseBudget( $borrower, $r, $userflags ) );
244         if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
245             next;
246         }
247         push @funds,
248           {
249             b_id  => $r->{budget_id},
250             b_txt => $r->{budget_name},
251             b_sel => ( $r->{budget_id} == $order->{budget_id} ) ? 1 : 0,
252           };
253     }
254
255     @funds = sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @funds;
256
257     push @budget_loop,
258       {
259         'id'          => $period->{'budget_period_id'},
260         'description' => $period->{'budget_period_description'},
261         'funds'       => \@funds
262       };
263 }
264
265 $template->{'VARS'}->{'budget_loop'} = \@budget_loop;
266
267 # regardless of the content of $unitprice e.g 0 or '' or any string will return in these cases 0.00
268 # and the 'IF' in the .tt will show 0.00 and not 'ecost' (see BZ 7129)
269 # So if $unitprice == 0 we don't create unitprice
270 if ( $unitprice != 0) {
271     $template->param(
272         unitprice             => sprintf( "%.2f", $unitprice),
273     );
274 }
275
276 my $op = $input->param('op');
277 if ($op and $op eq 'edit'){
278     $template->param(edit   =>   1);
279 }
280 output_html_with_http_headers $input, $cookie, $template->output;