Merging Katipo changes.
[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::Koha;
29 use C4::Auth;
30 use C4::Interface::CGI::Output;
31 use C4::Date;
32
33 my $input      = new CGI;
34 my $supplierid = $input->param('supplierid');
35 my $dbh        = C4::Context->dbh;
36
37 my $search  = $input->param('recieve');
38 my $invoice = $input->param('invoice');
39 my $freight = $input->param('freight');
40 my $biblio  = $input->param('biblio');
41 my $catview = $input->param('catview');
42 my $gst     = $input->param('gst');
43 my ( $count, @results ) =
44   ordersearch( $search, $supplierid, $biblio, $catview );
45
46 # warn "C:$count for ordersearch($search,$supplierid,$biblio,$catview);";
47 my ( $count2, @booksellers ) = bookseller( $results[0]->{'booksellerid'} );
48 my $date = $results[0]->{'entrydate'};
49
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
51     {
52         template_name   => "acqui/acquire.tmpl",
53         query           => $input,
54         type            => "intranet",
55         authnotrequired => 0,
56         flagsrequired   => { acquisition => 1 },
57         debug           => 1,
58     }
59 );
60 $template->param($count);
61 my $sthtemp =
62   $dbh->prepare(
63     "Select flags, branchcode from borrowers where borrowernumber = ?");
64 $sthtemp->execute($loggedinuser);
65 my ( $flags, $homebranch ) = $sthtemp->fetchrow;
66
67 if ( $count == 1 ) {
68     my $sth;
69
70     my $branches = getbranches;
71     my @branchloop;
72     foreach my $thisbranch ( sort keys %$branches ) {
73         my %row = (
74             value      => $thisbranch,
75             branchname => $branches->{$thisbranch}->{'branchname'},
76         );
77         push @branchloop, \%row;
78     }
79
80     my $auto_barcode = C4::Context->boolean_preference("autoBarcode") || 0;
81
82     # See whether barcodes should be automatically allocated.
83     # Defaults to 0, meaning "no".
84     my $barcode;
85     if ( $auto_barcode eq '1' ) {
86         $sth = $dbh->prepare("Select max(barcode) from items");
87         $sth->execute;
88         my $data = $sth->fetchrow_hashref;
89         $barcode = $results[0]->{'barcode'} + 1;
90         $sth->finish;
91     }
92
93     if ( $results[0]->{'quantityreceived'} == 0 ) {
94         $results[0]->{'quantityreceived'} = '';
95     }
96     if ( $results[0]->{'unitprice'} == 0 ) {
97         $results[0]->{'unitprice'} = '';
98     }
99     $template->param(
100         branchloop       => \@branchloop,
101         count            => 1,
102         biblionumber     => $results[0]->{'biblionumber'},
103         ordernumber      => $results[0]->{'ordernumber'},
104         biblioitemnumber => $results[0]->{'biblioitemnumber'},
105         supplierid       => $results[0]->{'booksellerid'},
106         freight          => $freight,
107         gst              => $gst,
108         catview          => ( $catview ne 'yes' ? 1 : 0 ),
109         name             => $booksellers[0]->{'name'},
110         date             => format_date($date),
111         title            => $results[0]->{'title'},
112         author           => $results[0]->{'author'},
113         copyrightdate    => format_date( $results[0]->{'copyrightdate'} ),
114         itemtype         => $results[0]->{'itemtype'},
115         isbn             => $results[0]->{'isbn'},
116         seriestitle      => $results[0]->{'seriestitle'},
117         barcode          => $barcode,
118         bookfund         => $results[0]->{'bookfundid'},
119         quantity         => $results[0]->{'quantity'},
120         quantityreceived => $results[0]->{'quantityreceived'},
121         rrp              => $results[0]->{'rrp'},
122         ecost            => $results[0]->{'ecost'},
123         unitprice        => $results[0]->{'unitprice'},
124         invoice          => $invoice,
125         notes            => $results[0]->{'notes'},
126     );
127 }
128 else {
129     my @loop;
130     for ( my $i = 0 ; $i < $count ; $i++ ) {
131         my %line;
132         $line{isbn}             = $results[$i]->{'isbn'};
133         $line{basketno}         = $results[$i]->{'basketno'};
134         $line{quantity}         = $results[$i]->{'quantity'};
135         $line{quantityrecieved} = $results[$i]->{'quantityreceived'};
136         $line{ordernumber}      = $results[$i]->{'ordernumber'};
137         $line{biblionumber}     = $results[$i]->{'biblionumber'};
138         $line{invoice}          = $invoice;
139         $line{freight}          = $freight;
140         $line{gst}              = $gst;
141         $line{title}            = $results[$i]->{'title'};
142         $line{author}           = $results[$i]->{'author'};
143         $line{supplierid}       = $supplierid;
144         push @loop, \%line;
145     }
146     $template->param(
147         loop       => \@loop,
148         date       => format_date($date),
149         name       => $booksellers[0]->{'name'},
150         supplierid => $supplierid,
151         invoice    => $invoice,
152     );
153
154 }
155 output_html_with_http_headers $input, $cookie, $template->output;