Merging Katipo changes.
[koha.git] / acqui / finishreceive.pl
1 #!/usr/bin/perl
2
3 #script to add a new item and to mark orders as received
4 #written 1/3/00 by chris@katipo.co.nz
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 # this script makes the items, addorder.pl has already made the biblio and biblioitem records: MASON
24
25 use strict;
26 use C4::Output;
27 use C4::Acquisition;
28 use C4::Biblio;
29 use CGI;
30 use C4::Search;
31 use C4::Circulation::Circ2;
32 use C4::Reserves2;
33 use C4::Interface::CGI::Output;
34 use C4::Auth;
35 use HTML::Template;
36
37 #use Data::Dumper;
38
39 my $input = new CGI;
40 my $dbh   = C4::Context->dbh;
41
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43     {
44         template_name   => "acqui/finishreceive.tmpl",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { editcatalogue => 1 },
49         debug           => 1,
50     }
51 );
52
53 my $user             = $input->remote_user;
54 my $biblionumber     = $input->param('biblio');
55 my $biblioitemnumber = $input->param('biblioitemnum');
56 my $ordnum           = $input->param('ordnum');
57 my $cost             = $input->param('cost');
58 my $invoiceno        = $input->param('invoice');
59 my $replacement    = $input->param('rrp');
60 my $gst            = $input->param('gst');
61 my $freight        = $input->param('freight');
62 my $freightperitem = $input->param('freightperitem');
63 my $supplierid     = $input->param('supplierid');
64 my $title         = $input->param('title');
65 my $author        = $input->param('author');
66 my $copyrightdate = $input->param('copyrightdate');
67 my $itemtype      = $input->param('format');
68 my $isbn          = $input->param('ISBN');
69 my $seriestitle   = $input->param('series');
70 my $branch        = $input->param('branch');
71 my $holdingbranch = $branch;
72 my $barcode       = $input->param('barcode');
73 my $bookfund      = $input->param('bookfund');
74 my $quantity      = $input->param('quantity');
75 my $quantrec      = $input->param('quantityrec');
76 my $ecost         = $input->param('ecost');
77 my $unitprice     = $input->param('unitprice');
78 my $notes         = $input->param('notes');
79 my $booksellers   = $input->param('booksellers');
80 my $foo           = $input->param('foo');
81 my $volinf        = $input->param('volinf');
82 my $catview = $input->param('catview');    # for editing from moredetail.tmpl
83 my $barcodeexists = $input->param('barcodeexists');  # if barcode exists
84 my $newitemfailed = $input->param('newitemfailed');  # if create new item failed
85 my $createbibitem =
86   $input->param('createbibitem');    # user wants to create a new bibitem
87
88 #get additional info on bib and bibitem from dbase for additional needed fields before modbiblio.
89 ( my $bibliocount,     my @biblios )     = &getbiblio($biblionumber);
90 ( my $biblioitemcount, my @biblioitems ) =
91   &getbiblioitembybiblionumber($biblionumber);
92 ( my $itemscount, my @items ) = &getitemsbybiblioitem($biblioitemnumber);
93
94 my $bibliohash = {
95     biblionumber  => $biblionumber,
96     title         => $title,
97     author        => $author,
98     abstract      => $biblios[0]->{'abstract'},
99     copyrightdate => $copyrightdate,
100     seriestitle   => $seriestitle,
101     serial        => $biblios[0]->{'serial'},
102     unititle      => $biblios[0]->{'unititle'},
103     notes         => $biblios[0]->{'notes'}
104 };
105
106 my $biblioitemhash = {
107     illus            => $biblioitems[0]->{'illus'},
108     number           => $biblioitems[0]->{'number'},
109     itemtype         => $itemtype,
110     place            => $biblioitems[0]->{'place'},
111     biblioitemnumber => $biblioitemnumber,
112     issn             => $biblioitems[0]->{'issn'},
113     size             => $biblioitems[0]->{'size'},
114     marc             => $biblioitems[0]->{'marc'},
115     timestamp        => $biblioitems[0]->{'timestamp'},
116     biblionumber     => $biblionumber,
117     url              => $biblioitems[0]->{'url'},
118     dewey            => $biblioitems[0]->{'dewey'},
119     isbn             => $isbn,
120     publishercode    => $biblioitems[0]->{'publishercode'},
121     lccn             => $biblioitems[0]->{'iccn'},
122     volume           => $biblioitems[0]->{'volume'},
123     subclass         => $biblioitems[0]->{'subclass'},
124     notes            => $biblioitems[0]->{'notes'},
125     classification   => $biblioitems[0]->{'classification'},
126     volumeddesc      => $volinf,
127     publicationyear  => $biblioitems[0]->{'publicationyear'},
128     volumedate       => $biblioitems[0]->{'volumedate'},
129     pages            => $biblioitems[0]->{'pages'}
130 };
131
132 my $itemhash = {
133     biblionumber   => $biblionumber,
134     itemnum        => $items[0]->{'itemnumber'},
135     barcode        => $barcode,
136     notes          => $items[0]->{'notes'},
137     itemcallnumber => $items[0]->{'itemcallnumber'},
138     notforloan     => $items[0]->{'notforloan'},
139     location       => $items[0]->{'location'},
140     bibitemnum     => $biblioitemnumber,
141     homebranch     => $items[0]->{'homebranch'},
142     lost           => $items[0]->{'itemlost'},
143     withdrawn      => $items[0]->{'withdrawn'},
144     holdingbranch  => $items[0]->{'holdingbranch'},
145     replacement    => $replacement
146 };
147
148 # check if barcode exists, if so redirect back to acquire.pl and give message
149 my $error = &checkitems( 1, $barcode );
150 #warn "barcode check for $barcode result = $error";
151 if ($error) {
152     print $input->redirect(
153                 "/cgi-bin/koha/acqui/acquire.pl?recieve=$ordnum&biblio=$biblionumber&invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&barcodeexists=$barcode"
154     );
155 }
156 # or if barcode is blank
157 else {
158
159     if ( $createbibitem eq "YES" ) {
160         &modbiblio($bibliohash);
161         $biblioitemnumber = &newbiblioitem($biblioitemhash);
162
163 #lets do a lookup on aqorders, with ordnum, then insert biblioitem fiels with new biblioitem number
164
165         my $query =
166           "UPDATE aqorders SET biblioitemnumber = ? where ordernumber = ? 
167                 and biblionumber =  ?";
168         my $sth = $dbh->prepare($query);
169         my $error = $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
170         #warn Dumper $error;
171         $sth->fetchrow_hashref;
172         $sth->finish;
173     }
174     else {
175         &modbiblio($bibliohash);
176         &modbibitem($biblioitemhash);
177     }
178
179     if ($catview) {
180         &moditem($itemhash);
181         print $input->redirect(
182 "/cgi-bin/koha/moredetail.pl?type=$itemtype&bib=$biblionumber&bi=$biblioitemnumber"
183         );
184     }
185
186     if ( $quantity != 0 ) {
187         # save the quantity recieved.
188         receiveorder( $biblionumber, $ordnum, $quantrec, $user, $cost,
189             $invoiceno, $freightperitem, $bookfund, $replacement );
190
191         # create items if the user has entered barcodes
192         my @barcodes = split( /\,| |\|/, $barcode );    #WTF?
193
194         my ($error) = newitems(
195             {
196                 biblioitemnumber => $biblioitemnumber,
197                 biblionumber     => $biblionumber,
198                 replacementprice => $replacement,
199                 price            => $cost,
200                 booksellerid     => $supplierid,
201                 homebranch       => $branch,
202                 loan             => 0
203             },
204             @barcodes
205         );
206
207         if ($error)
208         { #if  newitems failes then display error, and send them back to acquire.pl????
209
210             print $input->redirect(
211                         "/cgi-bin/koha/acqui/acquire.pl?recieve=$ordnum&biblio=$biblionumber&invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&newitemfailed=1"
212             );
213         }
214
215         elsif ( $itemtype ne 'P' && $itemtype ne 'PP' ) {   # chris's new if bit
216             my %env;
217             my $item = getiteminformation( \%env, 0, $barcode );
218             my ( $resfound, $resrec ) = CheckReserves( 0, $barcode );
219
220             if ($resfound) {                                # reserves is found
221                 my ($borrower) =
222                   getpatroninformation( \%env, $resrec->{'borrowernumber'}, 0 );
223                 $template->param(
224                     borrowernumber => $borrower->{'borrowernumber'},
225                     cardnumber     => $borrower->{'cardnumber'},
226                     firstname      => $borrower->{'firstname'},
227                     surname        => $borrower->{'surname'},
228                     invoice        => $invoiceno,
229                     id             => $supplierid,
230                     freight        => $freight,
231                     gst            => $gst,
232                     items          => $quantity,
233                     ordnum         => $ordnum,
234                     biblionumber   => $biblionumber,
235                     barcode        => $barcode,
236                 );
237
238                 output_html_with_http_headers $input, $cookie,
239                   $template->output;
240             }
241             else {    #no reserves found
242                 $invoiceno =~
243                   s/\&/\%26/g;   # swapping pesky & with url friendly hex codes.
244                 print $input->redirect(
245                         "/cgi-bin/koha/acqui/receive.pl?invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&quantity=$quantity"
246                 );
247             }
248         }
249         else {
250             print $input->redirect(
251                 "/cgi-bin/koha/loadmodules.pl?module=acquisitions")
252               ;    # chris's new bit
253         }    # end of if
254     }
255     else {
256
257         #        print $input->header;
258         delorder( $biblionumber, $ordnum );
259         print $input->redirect("/acquisitions/");
260     }
261 }