Bug 2553: Fixes drop-down alphabetization in acquisitions - New order and 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 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 warnings;  # FIXME
61 use CGI;
62 use C4::Context;
63 use C4::Koha;   # GetKohaAuthorisedValues GetItemTypes
64 use C4::Acquisition;
65 use C4::Auth;
66 use C4::Output;
67 use C4::Dates qw/format_date/;
68 use C4::Bookseller;
69 use C4::Members;
70 use C4::Branch;    # GetBranches
71
72 my $input      = new CGI;
73 my $supplierid = $input->param('supplierid');
74 my $dbh        = C4::Context->dbh;
75
76 my $search       = $input->param('receive');
77 my $invoice      = $input->param('invoice');
78 my $freight      = $input->param('freight');
79 my $biblionumber = $input->param('biblionumber');
80 my $datereceived = C4::Dates->new($input->param('datereceived'),'iso') || C4::Dates->new();
81 my $catview      = $input->param('catview');
82 my $gst          = $input->param('gst');
83
84 my @results = SearchOrder( $search, $supplierid, $biblionumber, $catview );
85 my $count   = scalar @results;
86 my $order       = GetOrder($search);
87
88 my $bookseller = GetBookSellerFromId( $results[0]->{'booksellerid'} );
89
90 my $date = $results[0]->{'entrydate'};
91
92 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
93     {
94         template_name   => "acqui/orderreceive.tmpl",
95         query           => $input,
96         type            => "intranet",
97         authnotrequired => 0,
98         flagsrequired   => { acquisition => 1 },
99         debug           => 1,
100     }
101 );
102
103 if ( $count == 1 ) {
104
105     my (@itemtypesloop,@locationloop,@ccodeloop);
106     my $itemtypes = GetItemTypes;
107     foreach my $thisitemtype (sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
108         push @itemtypesloop, {
109                   value => $thisitemtype,
110             description => $itemtypes->{$thisitemtype}->{'description'},
111                selected => ($thisitemtype eq $results[0]->{itemtype}),  # ifdef itemtype @ bibliolevel, use it as default for item level. 
112         };
113     }
114     my $locs = GetKohaAuthorisedValues( 'items.location' );
115     foreach my $thisloc (sort {$locs->{$a} cmp $locs->{$b}} keys %$locs) {
116             push @locationloop, {
117                   value => $thisloc,
118             description => $locs->{$thisloc},
119         };
120     }
121     my $ccodes = GetKohaAuthorisedValues( 'items.ccode' );
122         foreach my $thisccode (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
123         push @ccodeloop, {
124                   value => $thisccode,
125             description => $ccodes->{$thisccode},
126         };
127     }
128     $template->param(
129         itemtypeloop => \@itemtypesloop,
130         locationloop => \@locationloop,
131            ccodeloop => \@ccodeloop,
132           branchloop => GetBranchesLoop($order->{branchcode}),
133                itype => C4::Context->preference('item-level_itypes'),
134     );
135     
136     my $barcode;
137     # See whether barcodes should be automatically allocated.
138         # FIXME : only incremental is implemented here, and it creates a race condition.
139         # FIXME : Same problems as other autoBarcode: breaks if any unexpected data is encountered (like alphanumerical barcode)
140     # FIXME : Fails when >1 items are added (via js).  
141     if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
142         my $sth = $dbh->prepare("Select max(barcode) from items");
143         $sth->execute;
144         my $data = $sth->fetchrow_hashref;
145         $barcode = $results[0]->{'barcode'} + 1;
146     }
147
148     if ( $results[0]->{'quantityreceived'} == 0 ) {
149         $results[0]->{'quantityreceived'} = '';
150     }
151     if ( $results[0]->{'unitprice'} == 0 ) {
152         $results[0]->{'unitprice'} = '';
153     }
154 #    $results[0]->{'copyrightdate'} = format_date( $results[0]->{'copyrightdate'} );  # this usu fails.
155     $template->param(
156         count                 => 1,
157         biblionumber          => $results[0]->{'biblionumber'},
158         ordernumber           => $results[0]->{'ordernumber'},
159         biblioitemnumber      => $results[0]->{'biblioitemnumber'},
160         supplierid            => $results[0]->{'booksellerid'},
161         catview               => ( $catview ne 'yes' ? 1 : 0 ),
162         title                 => $results[0]->{'title'},
163         author                => $results[0]->{'author'},
164         copyrightdate         => $results[0]->{'copyrightdate'},
165         itemtype              => $results[0]->{'itemtype'},
166         isbn                  => $results[0]->{'isbn'},
167         seriestitle           => $results[0]->{'seriestitle'},
168         barcode               => $barcode,
169         bookfund              => $results[0]->{'bookfundid'},
170         quantity              => $results[0]->{'quantity'},
171         quantityreceivedplus1 => $results[0]->{'quantityreceived'} + 1,
172         quantityreceived      => $results[0]->{'quantityreceived'},
173         rrp                   => $results[0]->{'rrp'},
174         ecost                 => $results[0]->{'ecost'},
175         unitprice             => $results[0]->{'unitprice'},
176     );
177 }
178 else {
179     my @loop;
180     for ( my $i = 0 ; $i < $count ; $i++ ) {
181         my %line = %{ $results[$i] };
182         $line{title}        = $results[$i]->{'title'};
183         $line{author}       = $results[$i]->{'author'};
184         push @loop, \%line;
185     }
186     $template->param(
187         loop         => \@loop,
188         supplierid   => $supplierid,
189     );
190 }
191
192 $template->param(
193     date             => format_date($date),
194     datereceived     => $datereceived->output(),
195     datereceived_iso => $datereceived->output('iso'),
196     invoice          => $invoice,
197     name             => $bookseller->{'name'},
198     freight          => $freight,
199     gst              => $gst,
200 );
201
202 output_html_with_http_headers $input, $cookie, $template->output;