Merge branch 'bug_9923' into 3.12-master
[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 /;
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 = SearchOrder($ordernumber) if $ordernumber;
95
96 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
97     {
98         template_name   => "acqui/orderreceive.tmpl",
99         query           => $input,
100         type            => "intranet",
101         authnotrequired => 0,
102         flagsrequired   => {acquisition => 'order_receive'},
103         debug           => 1,
104     }
105 );
106
107 unless ( $results and @$results) {
108     output_html_with_http_headers $input, $cookie, $template->output;
109     exit;
110 }
111
112 # prepare the form for receiving
113 my $order = $results->[0];
114
115 # Check if ACQ framework exists
116 my $acq_fw = GetMarcStructure(1, 'ACQ');
117 unless($acq_fw) {
118     $template->param('NoACQframework' => 1);
119 }
120
121 my $AcqCreateItem = C4::Context->preference('AcqCreateItem');
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 @itemnumbers = GetItemnumbersFromOrder($order->{ordernumber});
130     my @items;
131     foreach (@itemnumbers) {
132         my $item = GetItem($_);
133         if($item->{homebranch}) {
134             $item->{homebranchname} = GetBranchName($item->{homebranch});
135         }
136         if($item->{holdingbranch}) {
137             $item->{holdingbranchname} = GetBranchName($item->{holdingbranch});
138         }
139         if(my $code = GetAuthValCode("items.notforloan", $fw)) {
140             $item->{notforloan} = GetKohaAuthorisedValueLib($code, $item->{notforloan});
141         }
142         if(my $code = GetAuthValCode("items.restricted", $fw)) {
143             $item->{restricted} = GetKohaAuthorisedValueLib($code, $item->{restricted});
144         }
145         if(my $code = GetAuthValCode("items.location", $fw)) {
146             $item->{location} = GetKohaAuthorisedValueLib($code, $item->{location});
147         }
148         if(my $code = GetAuthValCode("items.ccode", $fw)) {
149             $item->{collection} = GetKohaAuthorisedValueLib($code, $item->{ccode});
150         }
151         if(my $code = GetAuthValCode("items.materials", $fw)) {
152             $item->{materials} = GetKohaAuthorisedValueLib($code, $item->{materials});
153         }
154         my $itemtype = getitemtypeinfo($item->{itype});
155         $item->{itemtype} = $itemtype->{description};
156         push @items, $item;
157     }
158     $template->param(items => \@items);
159 }
160
161 $order->{quantityreceived} = '' if $order->{quantityreceived} == 0;
162 $order->{unitprice} = '' if $order->{unitprice} == 0;
163
164 my $rrp;
165 my $ecost;
166 my $unitprice;
167 if ( $bookseller->{listincgst} ) {
168     if ( $bookseller->{invoiceincgst} ) {
169         $rrp = $order->{rrp};
170         $ecost = $order->{ecost};
171         $unitprice = $order->{unitprice};
172     } else {
173         $rrp = $order->{rrp} / ( 1 + $order->{gstrate} );
174         $ecost = $order->{ecost} / ( 1 + $order->{gstrate} );
175         $unitprice = $order->{unitprice} / ( 1 + $order->{gstrate} );
176     }
177 } else {
178     if ( $bookseller->{invoiceincgst} ) {
179         $rrp = $order->{rrp} * ( 1 + $order->{gstrate} );
180         $ecost = $order->{ecost} * ( 1 + $order->{gstrate} );
181         $unitprice = $order->{unitprice} * ( 1 + $order->{gstrate} );
182     } else {
183         $rrp = $order->{rrp};
184         $ecost = $order->{ecost};
185         $unitprice = $order->{unitprice};
186     }
187  }
188
189 my $suggestion = GetSuggestionInfoFromBiblionumber($order->{biblionumber});
190
191 my $authorisedby = $order->{authorisedby};
192 my $member = GetMember( borrowernumber => $authorisedby );
193
194 my $budget = GetBudget( $order->{budget_id} );
195
196 $template->param(
197     AcqCreateItem         => $AcqCreateItem,
198     count                 => 1,
199     biblionumber          => $order->{'biblionumber'},
200     ordernumber           => $order->{'ordernumber'},
201     biblioitemnumber      => $order->{'biblioitemnumber'},
202     subscriptionid        => $order->{subscriptionid},
203     booksellerid          => $order->{'booksellerid'},
204     freight               => $freight,
205     name                  => $bookseller->{'name'},
206     date                  => format_date($order->{entrydate}),
207     title                 => $order->{'title'},
208     author                => $order->{'author'},
209     copyrightdate         => $order->{'copyrightdate'},
210     isbn                  => $order->{'isbn'},
211     seriestitle           => $order->{'seriestitle'},
212     bookfund              => $budget->{budget_name},
213     quantity              => $order->{'quantity'},
214     quantityreceivedplus1 => $order->{'quantityreceived'} + 1,
215     quantityreceived      => $order->{'quantityreceived'},
216     rrp                   => sprintf( "%.2f", $rrp ),
217     ecost                 => sprintf( "%.2f", $ecost ),
218     memberfirstname       => $member->{firstname} || "",
219     membersurname         => $member->{surname} || "",
220     invoiceid             => $invoice->{invoiceid},
221     invoice               => $invoice->{invoicenumber},
222     datereceived          => $datereceived->output(),
223     datereceived_iso      => $datereceived->output('iso'),
224     notes                 => $order->{notes},
225     suggestionid          => $suggestion->{suggestionid},
226     surnamesuggestedby    => $suggestion->{surnamesuggestedby},
227     firstnamesuggestedby  => $suggestion->{firstnamesuggestedby},
228 );
229
230 # regardless of the content of $unitprice e.g 0 or '' or any string will return in these cases 0.00
231 # and the 'IF' in the .tt will show 0.00 and not 'ecost' (see BZ 7129)
232 # So if $unitprice == 0 we don't create unitprice
233 if ( $unitprice != 0) {
234     $template->param(
235         unitprice             => sprintf( "%.2f", $unitprice),
236     );
237 }
238
239 my $op = $input->param('op');
240 if ($op and $op eq 'edit'){
241     $template->param(edit   =>   1);
242 }
243 output_html_with_http_headers $input, $cookie, $template->output;