Merge remote-tracking branch 'origin/new/bug_8597'
[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 receive
42
43 =item invoiceid
44
45 the id of this invoice.
46
47 =item freight
48
49 =item biblio
50
51 The biblionumber of this order.
52
53 =item datereceived
54
55 =item catview
56
57 =item gst
58
59 =back
60
61 =cut
62
63 use strict;
64 use warnings;
65
66 use CGI;
67 use C4::Context;
68 use C4::Koha;   # GetKohaAuthorisedValues GetItemTypes
69 use C4::Acquisition;
70 use C4::Auth;
71 use C4::Output;
72 use C4::Dates qw/format_date/;
73 use C4::Bookseller qw/ GetBookSellerFromId /;
74 use C4::Budgets qw/ GetBudget /;
75 use C4::Members;
76 use C4::Branch;    # GetBranches
77 use C4::Items;
78 use C4::Biblio;
79 use C4::Suggestions;
80
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 $datereceived = $invoice->{shipmentdate};
90 my $ordernumber  = $input->param('ordernumber');
91 my $search       = $input->param('receive');
92
93 $datereceived = $datereceived ? C4::Dates->new($datereceived, 'iso') : C4::Dates->new();
94
95 my $bookseller = GetBookSellerFromId($booksellerid);
96 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
97 my $results = SearchOrder($ordernumber,$search);
98
99 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
100     {
101         template_name   => "acqui/orderreceive.tmpl",
102         query           => $input,
103         type            => "intranet",
104         authnotrequired => 0,
105         flagsrequired   => {acquisition => 'order_receive'},
106         debug           => 1,
107     }
108 );
109
110 my $count = scalar @$results;
111 # prepare the form for receiving
112 if ( $count == 1 ) {
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     if ( $order->{'unitprice'} == 0 ) {
162         $order->{'unitprice'} = '';
163     }
164
165     my $suggestion   = GetSuggestionInfoFromBiblionumber($order->{'biblionumber'});
166
167     my $authorisedby = $order->{'authorisedby'};
168     my $member = GetMember( borrowernumber => $authorisedby );
169
170     my $budget = GetBudget( $order->{'budget_id'} );
171
172     $template->param(
173         AcqCreateItem         => $AcqCreateItem,
174         count                 => 1,
175         biblionumber          => $order->{'biblionumber'},
176         ordernumber           => $order->{'ordernumber'},
177         biblioitemnumber      => $order->{'biblioitemnumber'},
178         booksellerid          => $order->{'booksellerid'},
179         freight               => $freight,
180         gst                   => $gst,
181         name                  => $bookseller->{'name'},
182         date                  => format_date($order->{entrydate}),
183         title                 => $order->{'title'},
184         author                => $order->{'author'},
185         copyrightdate         => $order->{'copyrightdate'},
186         isbn                  => $order->{'isbn'},
187         seriestitle           => $order->{'seriestitle'},
188         bookfund              => $budget->{budget_name},
189         quantity              => $order->{'quantity'},
190         quantityreceivedplus1 => $order->{'quantityreceived'} + 1,
191         quantityreceived      => $order->{'quantityreceived'},
192         rrp                   => $order->{'rrp'},
193         ecost                 => sprintf( "%.2f",$order->{'ecost'}),
194         unitprice             => sprintf( "%.2f",$order->{'unitprice'}),
195         memberfirstname       => $member->{firstname} || "",
196         membersurname         => $member->{surname} || "",
197         invoiceid             => $invoice->{invoiceid},
198         invoice               => $invoice->{invoicenumber},
199         datereceived          => $datereceived->output(),
200         datereceived_iso      => $datereceived->output('iso'),
201         notes                 => $order->{notes},
202         suggestionid          => $suggestion->{suggestionid},
203         surnamesuggestedby    => $suggestion->{surnamesuggestedby},
204         firstnamesuggestedby  => $suggestion->{firstnamesuggestedby},
205     );
206 }
207 else {
208     my @loop;
209     for ( my $i = 0 ; $i < $count ; $i++ ) {
210         my %line = %{ @$results[$i] };
211
212         $line{invoice}      = $invoice->{invoicenumber};
213         $line{datereceived} = $datereceived->output();
214         $line{freight}      = $freight;
215         $line{gst}          = $gst;
216         $line{title}        = @$results[$i]->{'title'};
217         $line{author}       = @$results[$i]->{'author'};
218         $line{booksellerid}   = $booksellerid;
219         push @loop, \%line;
220     }
221
222     $template->param(
223         loop         => \@loop,
224         booksellerid => $booksellerid,
225         invoiceid    => $invoice->{invoiceid},
226     );
227 }
228 my $op = $input->param('op');
229 if ($op and $op eq 'edit'){
230     $template->param(edit   =>   1);
231 }
232 output_html_with_http_headers $input, $cookie, $template->output;