Fix some broken links & renamed some function.
[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
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         my $query =
185           "UPDATE aqorders SET biblioitemnumber = ? where ordernumber = ? 
186                 and biblionumber =  ?";
187         my $sth = $dbh->prepare($query);
188         my $error = $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
189         #warn Dumper $error;
190         $sth->fetchrow_hashref;
191         $sth->finish;
192     }
193     else {
194         &modbiblio($bibliohash);
195         &modbibitem($biblioitemhash);
196     }
197
198     if ($catview) {
199         &moditem($itemhash);
200         print $input->redirect(
201 "/cgi-bin/koha/moredetail.pl?type=$itemtype&bib=$biblionumber&bi=$biblioitemnumber"
202         );
203     }
204
205     if ( $quantity != 0 ) {
206         # save the quantity recieved.
207         receiveorder( $biblionumber, $ordnum, $quantrec, $user, $cost,
208             $invoiceno, $freightperitem, $bookfund, $replacement );
209
210         # create items if the user has entered barcodes
211         my @barcodes = split( /\,| |\|/, $barcode );    #WTF?
212
213         my ($error) = newitems(
214             {
215                 biblioitemnumber => $biblioitemnumber,
216                 biblionumber     => $biblionumber,
217                 replacementprice => $replacement,
218                 price            => $cost,
219                 booksellerid     => $supplierid,
220                 homebranch       => $branch,
221                 loan             => 0
222             },
223             @barcodes
224         );
225
226         if ($error)
227         { #if  newitems failes then display error, and send them back to orderreceive.pl????
228
229             print $input->redirect(
230                         "/cgi-bin/koha/acqui/orderreceive.pl?recieve=$ordnum&biblio=$biblionumber&invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&newitemfailed=1"
231             );
232         }
233
234         elsif ( $itemtype ne 'P' && $itemtype ne 'PP' ) {   # chris's new if bit
235             my %env;
236             my $item = getiteminformation( \%env, 0, $barcode );
237             my ( $resfound, $resrec ) = CheckReserves( 0, $barcode );
238
239             if ($resfound) {                                # reserves is found
240                 my ($borrower) =
241                   getpatroninformation( \%env, $resrec->{'borrowernumber'}, 0 );
242                 $template->param(
243                     borrowernumber => $borrower->{'borrowernumber'},
244                     cardnumber     => $borrower->{'cardnumber'},
245                     firstname      => $borrower->{'firstname'},
246                     surname        => $borrower->{'surname'},
247                     invoice        => $invoiceno,
248                     id             => $supplierid,
249                     freight        => $freight,
250                     gst            => $gst,
251                     items          => $quantity,
252                     ordnum         => $ordnum,
253                     biblionumber   => $biblionumber,
254                     barcode        => $barcode,
255                 );
256
257                 output_html_with_http_headers $input, $cookie,
258                   $template->output;
259             }
260             else {    #no reserves found
261                 $invoiceno =~
262                   s/\&/\%26/g;   # swapping pesky & with url friendly hex codes.
263                 print $input->redirect(
264                         "/cgi-bin/koha/acqui/receive.pl?invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&quantity=$quantity"
265                 );
266             }
267         }
268         else {
269             print $input->redirect(
270                 "/cgi-bin/koha/loadmodules.pl?module=acquisitions")
271               ;    # chris's new bit
272         }    # end of if
273     }
274     else {
275
276         #        print $input->header;
277         DelOrder( $biblionumber, $ordnum );
278         print $input->redirect("/acquisitions/");
279     }
280 }