Bug 7175: Allow to choose which items to receive
[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 invoice
44
45 the number 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 $booksellerid = $input->param('booksellerid');
86 my $ordernumber  = $input->param('ordernumber');
87 my $search       = $input->param('receive');
88 my $invoice      = $input->param('invoice');
89 my $freight      = $input->param('freight');
90 my $datereceived = $input->param('datereceived');
91
92
93 $datereceived = $datereceived ? C4::Dates->new($datereceived, 'iso') : C4::Dates->new();
94
95 my $bookseller = GetBookSellerFromId($booksellerid);
96 my $input_gst = ($input->param('gst') eq '' ? undef : $input->param('gst'));
97 my $gst= $input_gst // $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
98 my $results = SearchOrder($ordernumber,$search);
99
100 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
101     {
102         template_name   => "acqui/orderreceive.tmpl",
103         query           => $input,
104         type            => "intranet",
105         authnotrequired => 0,
106         flagsrequired   => {acquisition => 'order_receive'},
107         debug           => 1,
108     }
109 );
110
111 my $count = scalar @$results;
112 # prepare the form for receiving
113 if ( $count == 1 ) {
114     my $order = $results->[0];
115
116     # Check if ACQ framework exists
117     my $acq_fw = GetMarcStructure(1, 'ACQ');
118     unless($acq_fw) {
119         $template->param('NoACQframework' => 1);
120     }
121
122     my $AcqCreateItem = C4::Context->preference('AcqCreateItem');
123     if ($AcqCreateItem eq 'receiving') {
124         $template->param(
125             AcqCreateItemReceiving => 1,
126             UniqueItemFields => C4::Context->preference('UniqueItemFields'),
127         );
128     } elsif ($AcqCreateItem eq 'ordering') {
129         my $fw = ($acq_fw) ? 'ACQ' : '';
130         my @itemnumbers = GetItemnumbersFromOrder($order->{ordernumber});
131         my @items;
132         foreach (@itemnumbers) {
133             my $item = GetItem($_);
134             if($item->{homebranch}) {
135                 $item->{homebranchname} = GetBranchName($item->{homebranch});
136             }
137             if($item->{holdingbranch}) {
138                 $item->{holdingbranchname} = GetBranchName($item->{holdingbranch});
139             }
140             if(my $code = GetAuthValCode("items.notforloan", $fw)) {
141                 $item->{notforloan} = GetKohaAuthorisedValueLib($code, $item->{notforloan});
142             }
143             if(my $code = GetAuthValCode("items.restricted", $fw)) {
144                 $item->{restricted} = GetKohaAuthorisedValueLib($code, $item->{restricted});
145             }
146             if(my $code = GetAuthValCode("items.location", $fw)) {
147                 $item->{location} = GetKohaAuthorisedValueLib($code, $item->{location});
148             }
149             if(my $code = GetAuthValCode("items.ccode", $fw)) {
150                 $item->{collection} = GetKohaAuthorisedValueLib($code, $item->{ccode});
151             }
152             if(my $code = GetAuthValCode("items.materials", $fw)) {
153                 $item->{materials} = GetKohaAuthorisedValueLib($code, $item->{materials});
154             }
155             my $itemtype = getitemtypeinfo($item->{itype});
156             $item->{itemtype} = $itemtype->{description};
157             push @items, $item;
158         }
159         $template->param(items => \@items);
160     }
161
162     if ( $order->{'unitprice'} == 0 ) {
163         $order->{'unitprice'} = '';
164     }
165
166     my $suggestion   = GetSuggestionInfoFromBiblionumber($order->{'biblionumber'});
167
168     my $authorisedby = $order->{'authorisedby'};
169     my $member = GetMember( borrowernumber => $authorisedby );
170
171     my $budget = GetBudget( $order->{'budget_id'} );
172
173     $template->param(
174         AcqCreateItem         => $AcqCreateItem,
175         count                 => 1,
176         biblionumber          => $order->{'biblionumber'},
177         ordernumber           => $order->{'ordernumber'},
178         biblioitemnumber      => $order->{'biblioitemnumber'},
179         booksellerid          => $order->{'booksellerid'},
180         freight               => $freight,
181         gst                   => $gst,
182         name                  => $bookseller->{'name'},
183         date                  => format_date($order->{entrydate}),
184         title                 => $order->{'title'},
185         author                => $order->{'author'},
186         copyrightdate         => $order->{'copyrightdate'},
187         isbn                  => $order->{'isbn'},
188         seriestitle           => $order->{'seriestitle'},
189         bookfund              => $budget->{budget_name},
190         quantity              => $order->{'quantity'},
191         quantityreceivedplus1 => $order->{'quantityreceived'} + 1,
192         quantityreceived      => $order->{'quantityreceived'},
193         rrp                   => $order->{'rrp'},
194         ecost                 => $order->{'ecost'},
195         unitprice             => $order->{'unitprice'},
196         memberfirstname       => $member->{firstname} || "",
197         membersurname         => $member->{surname} || "",
198         invoice               => $invoice,
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;
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     );
226 }
227 my $op = $input->param('op');
228 if ($op and $op eq 'edit'){
229     $template->param(edit   =>   1);
230 }
231 output_html_with_http_headers $input, $cookie, $template->output;