bug 2201 - autoBarcode code in acq is old. Modified to only work with 'increment...
[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 with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 =head1 NAME
25
26 orderreceive.pl
27
28 =head1 DESCRIPTION
29 This script shows all order already receive and all pendings orders.
30 It permit to write a new order as 'received'.
31
32 =head1 CGI PARAMETERS
33
34 =over 4
35
36 =item supplierid
37 to know on what supplier this script has to display receive order.
38
39 =item receive
40
41 =item invoice
42 the number of this invoice.
43
44 =item freight
45
46 =item biblio
47 The biblionumber of this order.
48
49 =item datereceived
50
51 =item catview
52
53 =item gst
54
55 =back
56
57 =cut
58
59 use strict;
60 use CGI;
61 use C4::Context;
62 use C4::Koha;   # GetKohaAuthorisedValues GetItemTypes
63 use C4::Acquisition;
64 use C4::Auth;
65 use C4::Output;
66 use C4::Dates qw/format_date/;
67 use C4::Bookseller;
68 use C4::Members;
69 use C4::Branch;    # GetBranches
70
71 my $input      = new CGI;
72 my $supplierid = $input->param('supplierid');
73 my $dbh        = C4::Context->dbh;
74
75 my $search       = $input->param('receive');
76 my $invoice      = $input->param('invoice');
77 my $freight      = $input->param('freight');
78 my $biblionumber       = $input->param('biblionumber');
79 my $datereceived = C4::Dates->new($input->param('datereceived'),'iso') || C4::Dates->new();
80 my $catview      = $input->param('catview');
81 my $gst          = $input->param('gst');
82
83 my @results = SearchOrder( $search, $supplierid, $biblionumber, $catview );
84 my $count   = scalar @results;
85
86 my $bookseller = GetBookSellerFromId( $results[0]->{'booksellerid'} );
87
88 my $date = $results[0]->{'entrydate'};
89
90 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
91     {
92         template_name   => "acqui/orderreceive.tmpl",
93         query           => $input,
94         type            => "intranet",
95         authnotrequired => 0,
96         flagsrequired   => { acquisition => 1 },
97         debug           => 1,
98     }
99 );
100 $template->param($count);
101
102 if ( $count == 1 ) {
103
104     my (@itemtypesloop,@locationloop,@ccodeloop);
105     my $itemtypes = GetItemTypes;
106     foreach my $thisitemtype (sort keys %$itemtypes) {
107                 my %row = (
108                     value => $thisitemtype,
109                     description => $itemtypes->{$thisitemtype}->{'description'},
110                                         selected => ($thisitemtype eq $results[0]->{itemtype}),  # ifdef itemtype @ bibliolevel, use it as default for item level. 
111                   );
112         push @itemtypesloop, \%row;
113     }
114
115     my $locs = GetKohaAuthorisedValues( 'items.location' );
116     foreach my $thisloc (sort keys %$locs) {
117                 my $row = {
118                     value => $thisloc,
119                     description => $locs->{$thisloc},
120                   };
121         push @locationloop, $row;
122     }
123     my $ccodes= GetKohaAuthorisedValues( 'items.ccode' );
124         foreach my $thisccode (sort keys %$ccodes) {
125         push @ccodeloop,  {
126                     value => $thisccode,
127                     description => $ccodes->{$thisccode},
128                   };
129     }
130     $template->param(itemtypeloop => \@itemtypesloop ,
131                                         locationloop => \@locationloop,
132                                         ccodeloop => \@ccodeloop,
133                                         itype => C4::Context->preference('item-level_itypes'),
134                                         );
135     
136         my $onlymine=C4::Context->preference('IndependantBranches') && 
137                 C4::Context->userenv && 
138                 C4::Context->userenv->{flags} !=1  && 
139                 C4::Context->userenv->{branch};
140     my $branches = GetBranches($onlymine);
141     my @branchloop;
142     foreach my $thisbranch ( sort keys %$branches ) {
143         my %row = (
144             value      => $thisbranch,
145             description => $branches->{$thisbranch}->{'branchname'},
146         );
147         push @branchloop, \%row;
148     }
149
150     my $barcode;
151     # See whether barcodes should be automatically allocated.
152         # FIXME : only incremental is implemented here, and it creates a race condition.
153         #
154     if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
155         my $sth = $dbh->prepare("Select max(barcode) from items");
156         $sth->execute;
157         my $data = $sth->fetchrow_hashref;
158         $barcode = $results[0]->{'barcode'} + 1;
159         $sth->finish;
160     }
161
162     if ( $results[0]->{'quantityreceived'} == 0 ) {
163         $results[0]->{'quantityreceived'} = '';
164     }
165     if ( $results[0]->{'unitprice'} == 0 ) {
166         $results[0]->{'unitprice'} = '';
167     }
168 #    $results[0]->{'copyrightdate'} = format_date( $results[0]->{'copyrightdate'} );  # this usu fails.
169     $template->param(
170         branchloop            => \@branchloop,
171         count                 => 1,
172         biblionumber          => $results[0]->{'biblionumber'},
173         ordernumber           => $results[0]->{'ordernumber'},
174         biblioitemnumber      => $results[0]->{'biblioitemnumber'},
175         supplierid            => $results[0]->{'booksellerid'},
176         freight               => $freight,
177         gst                   => $gst,
178         catview               => ( $catview ne 'yes' ? 1 : 0 ),
179         name                  => $bookseller->{'name'},
180         date                  => format_date($date),
181         title                 => $results[0]->{'title'},
182         author                => $results[0]->{'author'},
183         copyrightdate         => $results[0]->{'copyrightdate'},
184         itemtype              => $results[0]->{'itemtype'},
185         isbn                  => $results[0]->{'isbn'},
186         seriestitle           => $results[0]->{'seriestitle'},
187         barcode               => $barcode,
188         bookfund              => $results[0]->{'bookfundid'},
189         quantity              => $results[0]->{'quantity'},
190         quantityreceivedplus1 => $results[0]->{'quantityreceived'} + 1,
191         quantityreceived      => $results[0]->{'quantityreceived'},
192         rrp                   => $results[0]->{'rrp'},
193         ecost                 => $results[0]->{'ecost'},
194         unitprice             => $results[0]->{'unitprice'},
195         invoice               => $invoice,
196         datereceived          => $datereceived->output(),
197         datereceived_iso          => $datereceived->output('iso'),
198     );
199 }
200 else {
201     my @loop;
202     for ( my $i = 0 ; $i < $count ; $i++ ) {
203         my %line = %{ $results[$i] };
204
205         $line{invoice}      = $invoice;
206         $line{datereceived} = $datereceived->output();
207         $line{freight}      = $freight;
208         $line{gst}          = $gst;
209         $line{title}        = $results[$i]->{'title'};
210         $line{author}       = $results[$i]->{'author'};
211         $line{supplierid}   = $supplierid;
212         push @loop, \%line;
213     }
214     $template->param(
215         loop                    => \@loop,
216         date                    => format_date($date),
217         datereceived            => $datereceived->output(),
218         name                    => $bookseller->{'name'},
219         supplierid              => $supplierid,
220         invoice                 => $invoice,
221     );
222
223 }
224 output_html_with_http_headers $input, $cookie, $template->output;