Some SQL code moved into Acquisition.pm
[wip/koha-chris_n.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
26 =head1 NAME
27
28 finishreceive.pl
29
30 =head1 DESCRIPTION
31 TODO
32
33 =head1 CGI PARAMETERS
34
35 =over 4
36
37 TODO
38
39 =back
40
41 =cut
42
43 use strict;
44 use C4::Output;
45 use C4::Acquisition;
46 use C4::Biblio;
47 use CGI;
48 use C4::Search;
49 use C4::Circulation::Circ2;
50 use C4::Reserves2;
51 use C4::Interface::CGI::Output;
52 use C4::Auth;
53 use HTML::Template;
54
55 #use Data::Dumper;
56
57 my $input = new CGI;
58 my $dbh   = C4::Context->dbh;
59
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
61     {
62         template_name   => "acqui/finishreceive.tmpl",
63         query           => $input,
64         type            => "intranet",
65         authnotrequired => 0,
66         flagsrequired   => { editcatalogue => 1 },
67         debug           => 1,
68     }
69 );
70
71 my $user             = $input->remote_user;
72 my $biblionumber     = $input->param('biblio');
73 my $biblioitemnumber = $input->param('biblioitemnum');
74 my $ordnum           = $input->param('ordnum');
75 my $cost             = $input->param('cost');
76 my $invoiceno        = $input->param('invoice');
77 my $replacement    = $input->param('rrp');
78 my $gst            = $input->param('gst');
79 my $freight        = $input->param('freight');
80 my $freightperitem = $input->param('freightperitem');
81 my $supplierid     = $input->param('supplierid');
82 my $title         = $input->param('title');
83 my $author        = $input->param('author');
84 my $copyrightdate = $input->param('copyrightdate');
85 my $itemtype      = $input->param('format');
86 my $isbn          = $input->param('ISBN');
87 my $seriestitle   = $input->param('series');
88 my $branch        = $input->param('branch');
89 my $holdingbranch = $branch;
90 my $barcode       = $input->param('barcode');
91 my $bookfund      = $input->param('bookfund');
92 my $quantity      = $input->param('quantity');
93 my $quantrec      = $input->param('quantityrec');
94 my $ecost         = $input->param('ecost');
95 my $unitprice     = $input->param('unitprice');
96 my $notes         = $input->param('notes');
97 my $booksellers   = $input->param('booksellers');
98 my $foo           = $input->param('foo');
99 my $volinf        = $input->param('volinf');
100 my $catview = $input->param('catview');    # for editing from moredetail.tmpl
101 my $barcodeexists = $input->param('barcodeexists');  # if barcode exists
102 my $newitemfailed = $input->param('newitemfailed');  # if create new item failed
103 my $createbibitem =
104   $input->param('createbibitem');    # user wants to create a new bibitem
105
106 #get additional info on bib and bibitem from dbase for additional needed fields before modbiblio.
107 ( my $bibliocount,     my @biblios )     = &getbiblio($biblionumber);
108 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
109 my $biblioitemcount = scalar @biblioitems;
110
111 ( my $itemscount, my @items ) = &getitemsbybiblioitem($biblioitemnumber);
112
113 my $bibliohash = {
114     biblionumber  => $biblionumber,
115     title         => $title,
116     author        => $author,
117     abstract      => $biblios[0]->{'abstract'},
118     copyrightdate => $copyrightdate,
119     seriestitle   => $seriestitle,
120     serial        => $biblios[0]->{'serial'},
121     unititle      => $biblios[0]->{'unititle'},
122     notes         => $biblios[0]->{'notes'}
123 };
124
125 my $biblioitemhash = {
126     illus            => $biblioitems[0]->{'illus'},
127     number           => $biblioitems[0]->{'number'},
128     itemtype         => $itemtype,
129     place            => $biblioitems[0]->{'place'},
130     biblioitemnumber => $biblioitemnumber,
131     issn             => $biblioitems[0]->{'issn'},
132     size             => $biblioitems[0]->{'size'},
133     marc             => $biblioitems[0]->{'marc'},
134     timestamp        => $biblioitems[0]->{'timestamp'},
135     biblionumber     => $biblionumber,
136     url              => $biblioitems[0]->{'url'},
137     dewey            => $biblioitems[0]->{'dewey'},
138     isbn             => $isbn,
139     publishercode    => $biblioitems[0]->{'publishercode'},
140     lccn             => $biblioitems[0]->{'iccn'},
141     volume           => $biblioitems[0]->{'volume'},
142     subclass         => $biblioitems[0]->{'subclass'},
143     notes            => $biblioitems[0]->{'notes'},
144     classification   => $biblioitems[0]->{'classification'},
145     volumeddesc      => $volinf,
146     publicationyear  => $biblioitems[0]->{'publicationyear'},
147     volumedate       => $biblioitems[0]->{'volumedate'},
148     pages            => $biblioitems[0]->{'pages'}
149 };
150
151 my $itemhash = {
152     biblionumber   => $biblionumber,
153     itemnum        => $items[0]->{'itemnumber'},
154     barcode        => $barcode,
155     notes          => $items[0]->{'notes'},
156     itemcallnumber => $items[0]->{'itemcallnumber'},
157     notforloan     => $items[0]->{'notforloan'},
158     location       => $items[0]->{'location'},
159     bibitemnum     => $biblioitemnumber,
160     homebranch     => $items[0]->{'homebranch'},
161     lost           => $items[0]->{'itemlost'},
162     withdrawn      => $items[0]->{'withdrawn'},
163     holdingbranch  => $items[0]->{'holdingbranch'},
164     replacement    => $replacement
165 };
166
167 # check if barcode exists, if so redirect back to orderreceive.pl and give message
168 my $error = &checkitems( 1, $barcode );
169 #warn "barcode check for $barcode result = $error";
170 if ($error) {
171     print $input->redirect(
172                 "/cgi-bin/koha/acqui/orderreceive.pl?recieve=$ordnum&biblio=$biblionumber&invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&barcodeexists=$barcode"
173     );
174 }
175 # or if barcode is blank
176 else {
177
178     if ( $createbibitem eq "YES" ) {
179         &modbiblio($bibliohash);
180         $biblioitemnumber = &newbiblioitem($biblioitemhash);
181
182 #lets do a lookup on aqorders, with ordnum, then insert biblioitem fiels with new biblioitem number
183
184     &ModOrderBiblioNumber($biblioitemnumber,$ordnum, $biblionumber);
185     
186     else {
187         &modbiblio($bibliohash);
188         &modbibitem($biblioitemhash);
189     }
190
191     if ($catview) {
192         &moditem($itemhash);
193         print $input->redirect(
194 "/cgi-bin/koha/moredetail.pl?type=$itemtype&bib=$biblionumber&bi=$biblioitemnumber"
195         );
196     }
197
198     if ( $quantity != 0 ) {
199         # save the quantity recieved.
200         receiveorder( $biblionumber, $ordnum, $quantrec, $user, $cost,
201             $invoiceno, $freightperitem, $bookfund, $replacement );
202
203         # create items if the user has entered barcodes
204         my @barcodes = split( /\,| |\|/, $barcode );    #WTF?
205
206         my ($error) = newitems(
207             {
208                 biblioitemnumber => $biblioitemnumber,
209                 biblionumber     => $biblionumber,
210                 replacementprice => $replacement,
211                 price            => $cost,
212                 booksellerid     => $supplierid,
213                 homebranch       => $branch,
214                 loan             => 0
215             },
216             @barcodes
217         );
218
219         if ($error)
220         { #if  newitems failes then display error, and send them back to orderreceive.pl????
221
222             print $input->redirect(
223                         "/cgi-bin/koha/acqui/orderreceive.pl?recieve=$ordnum&biblio=$biblionumber&invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&newitemfailed=1"
224             );
225         }
226
227         elsif ( $itemtype ne 'P' && $itemtype ne 'PP' ) {   # chris's new if bit
228             my %env;
229             my $item = getiteminformation( \%env, 0, $barcode );
230             my ( $resfound, $resrec ) = CheckReserves( 0, $barcode );
231
232             if ($resfound) {                                # reserves is found
233                 my ($borrower) =
234                   getpatroninformation( \%env, $resrec->{'borrowernumber'}, 0 );
235                 $template->param(
236                     borrowernumber => $borrower->{'borrowernumber'},
237                     cardnumber     => $borrower->{'cardnumber'},
238                     firstname      => $borrower->{'firstname'},
239                     surname        => $borrower->{'surname'},
240                     invoice        => $invoiceno,
241                     id             => $supplierid,
242                     freight        => $freight,
243                     gst            => $gst,
244                     items          => $quantity,
245                     ordnum         => $ordnum,
246                     biblionumber   => $biblionumber,
247                     barcode        => $barcode,
248                 );
249
250                 output_html_with_http_headers $input, $cookie,
251                   $template->output;
252             }
253             else {    #no reserves found
254                 $invoiceno =~
255                   s/\&/\%26/g;   # swapping pesky & with url friendly hex codes.
256                 print $input->redirect(
257                         "/cgi-bin/koha/acqui/receive.pl?invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&quantity=$quantity"
258                 );
259             }
260         }
261         else {
262             print $input->redirect(
263                 "/cgi-bin/koha/loadmodules.pl?module=acquisitions")
264               ;    # chris's new bit
265         }    # end of if
266     }
267     else {
268
269         #        print $input->header;
270         DelOrder( $biblionumber, $ordnum );
271         print $input->redirect("/acquisitions/");
272     }
273 }