this file has been changed by parcels.pl
[koha.git] / acqui / acquire.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 #script to recieve orders
6 #written by chris@katipo.co.nz 24/2/2000
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24 use strict;
25 use CGI;
26 use C4::Context;
27 use C4::Acquisition;
28 use C4::Biblio;
29 use C4::Output;
30 use C4::Search;
31 use C4::Auth;
32 use C4::Interface::CGI::Output;
33 use C4::Database;
34 use HTML::Template;
35 use C4::Date;
36
37 use Data::Dumper;
38
39 my $input      = new CGI;
40 my $supplierid = $input->param('supplierid');
41 my $dbh        = C4::Context->dbh;
42
43 my $search      = $input->param('recieve');
44 my $invoice     = $input->param('invoice');
45 my $freight     = $input->param('freight');
46 my $biblio      = $input->param('biblio');
47 my $biblioitem  = $input->param('bibitem');       # added my mason 20060324
48 my $catview     = $input->param('catview');
49 my $gst         = $input->param('gst');
50 my $noitems     = $input->param('items');
51 my $set_barcode = $input->param('set_barcode');
52 my $library_name = C4::Context->preference("LibraryName");
53
54 my ( $count, @results ) =
55   ordersearch( $search, $supplierid, $biblio, $catview );
56
57 if ( $library_name eq "Horowhenua Library Trust" && $count > 1 ) {
58     ( $count, @results ) = ordersearch( $search, $biblio, $catview );
59
60 }
61
62 #warn "COUNT = $count";
63 #warn "C:$count for ordersearch($search,$supplierid,$biblio,$catview);";
64
65 my ( $count2, @booksellers );
66 if ( $count == 1 ) {
67     ( $count2, @booksellers ) = bookseller( $results[0]->{'booksellerid'} );
68 }
69 else {
70     ( $count2, @booksellers ) = bookseller($supplierid);
71 }
72
73 #warn Dumper @results;
74
75 my $date     = $results[0]->{'entrydate'};
76 my $exchange = getcurrency( $booksellers[0]->{'listprice'} );
77
78 my $no_multi = $input->param('no_multi');
79
80 #-------------------------
81
82 # bugzilla: http://bugzilla.katipo.co.nz/show_bug.cgi?id=3916 , mason.
83 # ok lets do a lookup to see how many orders exist for a bibitem, if there are >1,
84 # then we need to display them  to the user so they can choose, because the system cant
85 # work it out, as there are no itemnumbers stored in the aqorders records :(
86
87 my @results2;
88
89 #warn "MASON BIBITEM=  $biblioitem";
90 my $query2 = " select * from aqorders where biblioitemnumber =?";
91 my $sth2   = $dbh->prepare($query2);
92 $sth2->execute($biblioitem);
93 while ( my $data2 = $sth2->fetchrow_hashref ) {
94
95     #warn $data2;
96
97     #warn Dumper "DATA2:", $data2->{'basketno'};
98     my $query3 = " select * from aqbasket where basketno =?";
99     my $sth3   = $dbh->prepare($query3);
100     $sth3->execute( $data2->{'basketno'} );
101     my $data3 = $sth3->fetchrow_hashref;
102
103     #warn Dumper $data3;
104     $data2->{'booksellerid'} = $data3->{'booksellerid'};
105     push( @results2, $data2 );
106 }
107 $sth2->finish;
108
109 #warn Dumper @results2;
110
111 my @loop;
112 my $result_count = scalar(@results2);
113
114 #warn "MULTI REESULT $result_count";
115 #warn "NO_MULTI =  $no_multi";
116 my  ( $template, $loggedinuser, $cookie );
117
118 if ( $result_count > 1 && $no_multi != 1 ) {
119
120     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
121         {
122             template_name   => "acqui/acquire-multi-order.tmpl",
123             query           => $input,
124             type            => "intranet",
125             authnotrequired => 0,
126             flagsrequired   => { acquisition => 1 },
127             debug           => 1,
128         }
129     );
130
131     #warn "IN MULTI IF \n";
132
133     # cool, now lets shove the results into a loop
134     for ( my $i = 0 ; $i < $result_count ; $i++ ) {
135         my %line;
136
137         $line{booksellerid}            = $results2[$i]->{'booksellerid'};
138         $line{biblionumber}            = $results2[$i]->{'biblionumber'};
139         $line{biblioitemnumber}        = $results2[$i]->{'biblioitemnumber'};
140         $line{ordernumber}             = $results2[$i]->{'ordernumber'};
141         $line{title}                   = $results2[$i]->{'title'};
142         $line{booksellerinvoicenumber} =
143           $results[$i]->{'booksellerinvoicenumber'};
144         $line{datereceived}        = $results2[$i]->{'datereceived'};
145         $line{entrydate}           = $results2[$i]->{'entrydate'};
146         $line{quantity}            = $results2[$i]->{'quantity'};
147         $line{listprice}           = $results2[$i]->{'listprice'};
148         $line{freight}             = $results2[$i]->{'freight'};
149         $line{unitprice}           = $results2[$i]->{'unitprice'};
150         $line{quantityreceived}    = $results2[$i]->{'quantityreceived'};
151         $line{supplierreference}   = $results2[$i]->{'supplierreference'};
152         $line{purchaseordernumber} = $results2[$i]->{'purchaseordernumber'};
153         $line{basketno}            = $results2[$i]->{'basketno'};
154         $line{timestamp}           = $results2[$i]->{'timestamp'};
155         $line{rrp}                 = $results2[$i]->{'rrp'};
156         $line{budgetdate}          = $results2[$i]->{'budgetdate'};
157         push @loop, \%line;
158
159         #warn "LOOPING", $results2[$i]->{'ordernumber'};
160     }
161
162     $template->param(
163         loop       => \@loop,
164         multi      => 1,
165         biblio     => $biblio,
166         biblioitem => $biblioitem,
167     );
168
169 }
170 elsif ( $count == 1 ) {
171
172     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
173         {
174
175                     template_name   => "acqui/acquire.tmpl",
176             query           => $input,
177             type            => "intranet",
178             authnotrequired => 0,
179             flagsrequired   => { acquisition => 1 },
180             debug           => 1,
181         }
182     );
183
184     #warn "NO MULTI\n";
185
186     my $itemtype2 = $results[0]->{'itemtype'};
187     my $freightperitem;
188     if ( $results[0]->{'freight'} > 0 ) {
189         $freightperitem = $results[0]->{'freight'};
190     }
191     else {
192         if ( $noitems > 0 ) {
193             $freightperitem = $freight / $noitems;
194         }
195     }
196     $freightperitem = sprintf( "%.2f", $freightperitem );
197     my $sth;
198     my $sth =
199       $dbh->prepare(
200         "Select itemtype,description from itemtypes order by description");
201     $sth->execute;
202     my @itemtype;
203     my %itemtypes;
204     push @itemtype, "";
205     $itemtypes{''} = "Please choose";
206
207     while ( my ( $value, $lib ) = $sth->fetchrow_array ) {
208         push @itemtype, $value;
209         $itemtypes{$value} = $lib;
210     }
211
212     my $CGIitemtype = CGI::scrolling_list(
213         -name     => 'format',
214         -values   => \@itemtype,
215         -default  => $results[0]->{'itemtype'},
216         -labels   => \%itemtypes,
217         -size     => 1,
218         -multiple => 0
219     );
220     $sth->finish;
221
222     my @branches;
223     my @select_branch;
224     my %select_branches;
225     my ( $count2, @branches ) = branches();
226     for ( my $i = 0 ; $i < $count2 ; $i++ ) {
227         push @select_branch, $branches[$i]->{'branchcode'};    #
228         $select_branches{ $branches[$i]->{'branchcode'} } =
229           $branches[$i]->{'branchname'};
230     }
231     my $CGIbranch = CGI::scrolling_list(
232         -name     => 'branch',
233         -values   => \@select_branch,
234         -default  => $results[0]->{'branchcode'},
235         -labels   => \%select_branches,
236         -size     => 1,
237         -multiple => 0
238     );
239
240     my $auto_barcode = C4::Context->boolean_preference("autoBarcode") || 0;
241     #warn "ACQIRE AUTO BARCODE = $auto_barcode";
242
243     # See whether barcodes should be automatically allocated.
244     # Defaults to 0, meaning "no".
245     my $barcode;
246     if ( $auto_barcode eq '1' ) {
247         $sth = $dbh->prepare("Select max(barcode) barcode from items");
248         $sth->execute;
249         my @data_results;
250         while ( my $data = $sth->fetchrow_hashref ) {
251             push( @data_results, $data );
252         }
253         $barcode = @data_results[0]->{'barcode'} + 1;
254         #warn 'moo', @data_results[0]->{'barcode'};
255         #warn "auto Barcode = $barcode";
256
257         #$barcode = sprintf( "%.0f", $barcode );
258         #warn "auto Barcode = $barcode";
259         $sth->finish;
260
261         my $moo = 'TEST777';
262         $moo = $moo + 1;
263         #warn $moo;
264     }
265
266     my @bookfund;
267     my @select_bookfund;
268     my %select_bookfunds;
269     ( $count2, @bookfund ) = bookfunds();
270     for ( my $i = 0 ; $i < $count2 ; $i++ ) {
271         push @select_bookfund, $bookfund[$i]->{'bookfundid'};
272         $select_bookfunds{ $bookfund[$i]->{'bookfundid'} } =
273           $bookfund[$i]->{'bookfundname'};
274     }
275     my $CGIbookfund = CGI::scrolling_list(
276         -name     => 'bookfund',
277         -values   => \@select_bookfund,
278         -default  => $results[0]->{'bookfundid'},
279         -labels   => \%select_bookfunds,
280         -size     => 1,
281         -multiple => 0
282     );
283
284     if ( $results[0]->{'quantityreceived'} == 0 ) {
285         $results[0]->{'quantityreceived'} = '';
286     }
287     if ( $results[0]->{'unitprice'} == 0 ) {
288         $results[0]->{'unitprice'} = '';
289     }
290
291     #warn Dumper( $results[0] );
292     $template->param(
293         count            => 1,
294         biblionumber     => $results[0]->{'biblionumber'},
295         ordernumber      => $results[0]->{'ordernumber'},
296         biblioitemnumber => $results[0]->{'biblioitemnumber'},
297         supplierid       => $results[0]->{'booksellerid'},
298         freight          => $freight,
299         gst              => $gst,
300         noitems          => $noitems,
301
302         #               catview => ($catview ne 'yes'?1:0),
303         catview       => $catview,
304         name          => $booksellers[0]->{'name'},
305         date          => format_date($date),
306         title         => $results[0]->{'title'},
307         author        => $results[0]->{'author'},
308         copyrightdate => $results[0]->{'copyrightdate'},
309
310         #               copyrightdate => format_date($results[0]->{'copyrightdate'}),
311         #               itemtype => $results[0]->{'itemtype'},
312         CGIbranch   => $CGIbranch,
313         CGIbookfund => $CGIbookfund,
314         CGIitemtype => $CGIitemtype,
315         isbn        => $results[0]->{'isbn'},
316         seriestitle => $results[0]->{'seriestitle'},
317         volinf      => $results[0]->{'volumeddesc'},
318         barcode     => $barcode,
319         set_barcode => $set_barcode,
320
321         #               bookfund => $results[0]->{'bookfundid'},
322         quantity         => $results[0]->{'quantity'},
323         quantityreceived => $results[0]->{'quantityreceived'},
324         rrp              => $results[0]->{'rrp'},
325         ecost            => $results[0]->{'ecost'},
326         unitprice        => $results[0]->{'unitprice'},
327         invoice          => $invoice,
328         notes            => $results[0]->{'notes'},
329         freightperitem   => $freightperitem,
330         nocalc           => $booksellers[0]->{'nocalc'},
331         invoicedisc      => $booksellers[0]->{'invoicedisc'},
332         invoiceinc       => $booksellers[0]->{'invoiceincgst'},
333         applygst         => $booksellers[0]->{'gstreg'},
334         discount         => $booksellers[0]->{'discount'},
335
336         supplierid => $booksellers[0]->{'id'},
337
338         currency                => $exchange->{'rate'},
339         basketno                => $results[0]->{'basketno'},
340         booksellerinvoicenumber => $results[0]->{'booksellerinvoicenumber'},
341         itemtype2 => $itemtype2,    #added by mason BGZLA:3823
342
343     );
344
345     #warn Dumper $booksellers[0];
346     #warn Dumper $results[0];
347
348 # MASON: this is a little fix, to ensure that the 'create new biblio group' checkbox        # ONLY appears in acquire.tmpl for biblioitems that already have 1 OR MORE items
349 # attached to them.
350     my $biblioitemnumber = $results[0]->{'biblioitemnumber'};
351     my $error            = &countitems($biblioitemnumber);
352
353     #warn "MASON: number of items for $biblioitemnumber = $error";
354     if ( $error > 0 ) {
355         $template->param( createbibitem => 'YES' );
356     }
357
358 }
359 else {    # whats this loop for ??? mason
360           # why this loop when acqui
361
362     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
363         {
364
365             template_name   => "acqui/searchresult.tmpl",
366             query           => $input,
367             type            => "intranet",
368             authnotrequired => 0,
369             flagsrequired   => { acquisition => 1 },
370             debug           => 1,
371         }
372     );
373
374     my @loop;
375     for ( my $i = 0 ; $i < $count ; $i++ ) {
376         my %line;
377         $line{isbn}             = $results[$i]->{'isbn'};
378         $line{basketno}         = $results[$i]->{'basketno'};
379         $line{quantity}         = $results[$i]->{'quantity'};
380         $line{quantityrecieved} = $results[$i]->{'quantityreceived'};
381         $line{ordernumber}      = $results[$i]->{'ordernumber'};
382         $line{biblionumber}     = $results[$i]->{'biblionumber'};
383         $line{invoice}          = $invoice;
384         $line{freight}          = $freight;
385         $line{gst}              = $gst;
386         $line{title}            = $results[$i]->{'title'};
387         $line{author}           = $results[$i]->{'author'};
388         $line{vol}              = $results[$i]->{'volume'};
389         $line{volinf}           = $results[$i]->{'volumeddesc'};
390         $line{supplierid}       = $supplierid;
391         $line{noitems}          = $noitems;
392         push @loop, \%line;
393     }
394     $template->param(
395         loop       => \@loop,
396         date       => format_date($date),
397         name       => $booksellers[0]->{'name'},
398         supplierid => $supplierid,
399         invoice    => $invoice,
400         search     => $search
401     );
402     warn "MASON: search= $search";
403
404 }
405
406 output_html_with_http_headers $input, $cookie, $template->output;Chris