fixing bug when a search gets more than 10 biblios : now, user can reach biblio 10...
[koha.git] / acqui.simple / addbookslccn.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 #
6 # Modified saas@users.sf.net 12:00 01 April 2001
7 # The biblioitemnumber was not correctly initialised
8 # The max(barcode) value was broken - koha 'barcode' is a string value!
9 # - If left blank, barcode value now defaults to max(biblionumber)
10
11 #
12 # TODO
13 #
14 # Error checking for pre-existing barcodes, biblionumbers and maybe others
15 #
16 # Add info on biblioitems and items already entered as you enter new ones
17
18
19 # Copyright 2000-2002 Katipo Communications
20 #
21 # This file is part of Koha.
22 #
23 # Koha is free software; you can redistribute it and/or modify it under the
24 # terms of the GNU General Public License as published by the Free Software
25 # Foundation; either version 2 of the License, or (at your option) any later
26 # version.
27 #
28 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
29 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
30 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
31 #
32 # You should have received a copy of the GNU General Public License along with
33 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
34 # Suite 330, Boston, MA  02111-1307 USA
35
36 use strict;
37 use CGI;
38 use C4::Context;
39 use C4::Catalogue;
40 use C4::Biblio;
41 use C4::Output;
42 use C4::Circulation::Circ2;
43
44 my $input = new CGI;
45 my $dbh = C4::Context->dbh;
46
47 my $lccn=$input->param('lccn');
48 my $q_lccn=$dbh->quote($lccn);
49 my $biblioitemnumber;
50
51 print $input->header;
52 print startpage();
53 print startmenu('acquisitions');
54
55 ($input->param('checkforbiblio')) && (checkforbiblio());
56 ($input->param('newbiblioitem')) && (newbiblioitem());
57 ($input->param('newitem')) && (newitem());
58
59 sub checkforbiblio {
60     my $title=$input->param('title');
61     my $q_title=$dbh->quote($title);
62     my $author=$input->param('author');
63     my $q_author=$dbh->quote($author);
64     my $seriestitle=$input->param('seriestitle');
65     my $serial=0;
66     ($seriestitle) && ($serial=1);
67     my $q_seriestitle=$dbh->quote($seriestitle);
68     my $copyrightdate=$input->param('copyrightdate');
69     my $q_copyrightdate=$dbh->quote($copyrightdate);
70     my $notes=$input->param('notes');
71     my $q_notes=$dbh->quote($notes);
72     my $subtitle=$input->param('subtitle');
73     my $q_subtitle=$dbh->quote($subtitle);
74     my $sth=$dbh->prepare("select biblionumber from biblio where title=$q_title
75         and author=$q_author and copyrightdate=$q_copyrightdate");
76     $sth->execute;
77     my $biblionumber=0;
78     if ($sth->rows) {
79         ($biblionumber) = $sth->fetchrow;
80     } else {
81         print "Adding new biblio for <i>$title</i> by $author<br>\n";
82         my $sth=$dbh->prepare("select max(biblionumber) from biblio");
83         $sth->execute;
84         ($biblionumber) = $sth->fetchrow;
85         $biblionumber++;
86         $sth=$dbh->prepare("insert into biblio (biblionumber, title, author,
87             serial, seriestitle, copyrightdate, notes) values ($biblionumber,
88             $q_title, $q_author, $serial, $q_seriestitle, $q_copyrightdate,
89             $q_notes)");
90         $sth->execute;
91         $sth=$dbh->prepare("insert into bibliosubtitle (subtitle, biblionumber)
92             values ($q_subtitle, $biblionumber)");
93         $sth->execute;
94     }
95     my $itemtypeselect='';
96     $sth=$dbh->prepare("select itemtype,description from itemtypes");
97     $sth->execute;
98     while (my ($itemtype, $description) = $sth->fetchrow) {
99         $itemtypeselect.="<option value=$itemtype>$itemtype - $description\n";
100     }
101     my $authortext="by $author";
102     ($author) || ($authortext='');
103     sectioninfo();
104     $sth=$dbh->prepare("select BI.isbn,IT.description,BI.volume,BI.number,BI.volumeddesc,BI.dewey,BI.subclass from biblioitems BI, itemtypes IT where BI.itemtype=IT.itemtype and biblionumber=$biblionumber");
105     $sth->execute;
106     my $biblioitemdata='';
107     while (my ($isbn, $itemtype, $volume, $number, $volumeddesc, $dewey, $subclass) = $sth->fetchrow) {
108         my $volumeinfo='';
109         if ($volume) {
110             if ($number) {
111                 $volumeinfo="V$volume, N$number";
112             } else {
113                 $volumeinfo="Vol $volume";
114             }
115         }
116         if ($volumeddesc) {
117             $volumeinfo.=" $volumeddesc";
118         }
119         $dewey=~s/0*$//;
120         $biblioitemdata.="<tr><td>$isbn</td><td align=center>$itemtype</td><td align=center>$volumeinfo</td><td align=center>$dewey$subclass</td></tr>\n";
121
122     }
123     if ($biblioitemdata) {
124         print << "EOF";
125 <center>
126 <p>
127 <table border=1 bgcolor=#dddddd>
128 <tr>
129 <th colspan=4>Existing entries using Biblio number $biblionumber</th>
130 </tr>
131 <tr>
132 <th>ISBN</th><th>Item Type</th><th>Volume</th><th>Classification</th></tr>
133 $biblioitemdata
134 </table>
135 </center>
136
137 EOF
138     }
139     print << "EOF";
140 <center>
141 <form>
142 <table border=1 bgcolor=#dddddd>
143 <tr><th colspan=4>Section Two: Publication Information for<br><i>$title</i>
144     $authortext</th></tr>
145
146 <tr><td align=right>Publisher</td><td colspan=3><input name=publishercode size=30></td></tr>
147 <tr><td align=right>Publication Year</td><td><input name=publicationyear size=10></td>
148 <td align=right>Place of Publication</td><td><input name=place size=20></td></tr>
149 <tr><td align=right>Illustrator</td><td colspan=3><input name=illus size=20></td></tr>
150 <tr><td align=right>Additional Authors<br>(One author per line)</td><td colspan=3><textarea
151     name=additionalauthors rows=4 cols=30></textarea></td></tr>
152 <tr><td align=right>Subject Headings<br>(One subject per line)</td><td colspan=3><textarea
153     name=subjectheadings rows=4 cols=30></textarea></td></tr>
154 <tr><td align=right>Item Type</td><td colspan=3><select name=itemtype>$itemtypeselect</select></td></tr>
155 <tr><td align=right>Dewey</td><td><input name=dewey size=10></td>
156 <td align=right>Dewey Subclass</td><td><input name=subclass size=10></td></tr>
157 <tr><td align=right>ISSN</td><td colspan=3><input name=issn size=10></td></tr>
158 <tr><td align=right>ISBN</td><td colspan=3><input name=isbn size=10></td></tr>
159 <tr><td align=right>Volume</td><td><input name=volume size=10></td>
160 <td align=right>Number</td><td><input name=number size=10></td></tr>
161 <tr><td align=right>Volume Description</td><td colspan=3><input name=volumeddesc size=40></td></tr>
162 <tr><td align=right>Pages</td><td><input name=pages size=10></td>
163 <td align=right>Size</td><td><input name=size size=10></td></tr>
164
165 <tr><td align=right>Notes</td><td colspan=3><textarea name=notes rows=4 cols=50
166     wrap=physical></textarea></td></tr>
167
168
169
170 </table>
171 <input type=submit value="Add New Bibliography Item">
172 </center>
173 <input type=hidden name=biblionumber value=$biblionumber>
174 <input type=hidden name=lccn value=$lccn>
175 <input type=hidden name=newbiblioitem value=1>
176 </form>
177 EOF
178     print endmenu();
179     print endpage();
180     exit;
181 }
182
183 # FIXME - There's already a &C4::Biblio::newbiblioitem.
184 sub newbiblioitem {
185     my $biblionumber=$input->param('biblionumber');
186     my $volume=$input->param('volume');
187     my $q_volume=$dbh->quote($volume);
188     my $number=$input->param('number');
189     my $q_number=$dbh->quote($number);
190     my $classification=$input->param('classification');
191     my $q_classification=$dbh->quote($classification);
192     my $itemtype=$input->param('itemtype');
193     my $q_itemtype=$dbh->quote($itemtype);
194     my $issn=$input->param('issn');
195     my $q_issn=$dbh->quote($issn);
196     my $isbn=$input->param('isbn');
197     my $q_isbn=$dbh->quote($isbn);
198     my $dewey=$input->param('dewey');
199     my $q_dewey=$dbh->quote($dewey);
200     my $subclass=$input->param('subclass');
201     my $q_subclass=$dbh->quote($subclass);
202     my $publicationyear=$input->param('publicationyear');
203     my $q_publicationyear=$dbh->quote($publicationyear);
204     my $publishercode=$input->param('publishercode');
205     my $q_publishercode=$dbh->quote($publishercode);
206     my $volumedate=$input->param('volumedate');
207     my $q_volumedate=$dbh->quote($volumedate);
208     my $volumeddesc=$input->param('volumeddesc');
209     my $q_volumeddesc=$dbh->quote($volumeddesc);
210     my $illus=$input->param('illus');
211     my $q_illus=$dbh->quote($illus);
212     my $pages=$input->param('pages');
213     my $q_pages=$dbh->quote($pages);
214     my $notes=$input->param('notes');
215     my $q_notes=$dbh->quote($notes);
216     my $size=$input->param('size');
217     my $q_size=$dbh->quote($size);
218     my $place=$input->param('place');
219     my $q_place=$dbh->quote($place);
220     my $subjectheadings=$input->param('subjectheadings');
221     my $additionalauthors=$input->param('additionalauthors');
222     my $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
223     $sth->execute;
224     ($biblioitemnumber) = $sth->fetchrow;
225     $biblioitemnumber++;
226 #    print STDERR "NEW BiblioItemNumber: $biblioitemnumber \n";
227     ($q_lccn='') if ($q_lccn eq 'NULL');
228     $sth=$dbh->prepare("insert into biblioitems (biblioitemnumber,
229     biblionumber, volume, number, classification, itemtype, isbn, issn, lccn, dewey, subclass,
230     publicationyear, publishercode, volumedate, volumeddesc, illus, pages,
231     notes, size, place) values ($biblioitemnumber, $biblionumber, $q_volume,
232     $q_number, $q_classification, $q_itemtype, $q_isbn, $q_issn, $q_lccn, $q_dewey, $q_subclass,
233     $q_publicationyear, $q_publishercode, $q_volumedate, $q_volumeddesc,
234     $q_illus, $q_pages, $q_notes, $q_size, $q_place)");
235     $sth->execute;
236     my @subjectheadings=split(/\n/,$subjectheadings);
237     my $subjectheading;
238     foreach $subjectheading (@subjectheadings) {
239         # remove any line ending characters (Ctrl-J or M)
240         $subjectheading=~s/\013//g;
241         $subjectheading=~s/\010//g;
242         # convert to upper case
243         $subjectheading=uc($subjectheading);
244         print STDERR "S: $biblionumber, $subjectheading  ";
245         chomp ($subjectheading);
246         print STDERR "B: ".ord(substr($subjectheading, length($subjectheading)-1, 1))." ";
247         while (ord(substr($subjectheading, length($subjectheading)-1, 1))<14) {
248             chop $subjectheading;
249         }
250         print STDERR "A: ".ord(substr($subjectheading, length($subjectheading)-1, 1))."\n";
251         # quote value
252         my $q_subjectheading=$dbh->quote($subjectheading);
253         $sth=$dbh->prepare("insert into bibliosubject (biblionumber,subject)
254             values ($biblionumber, $q_subjectheading)");
255         $sth->execute;
256     }
257     my @additionalauthors=split(/\n/,$additionalauthors);
258     my $additionalauthor;
259     foreach $additionalauthor (@additionalauthors) {
260         # remove any line ending characters (Ctrl-L or Ctrl-M)
261         $additionalauthor=~s/\013//g;
262         $additionalauthor=~s/\010//g;
263         # convert to upper case
264         $additionalauthor=uc($additionalauthor);
265         # quote value
266         my $q_additionalauthor=$dbh->quote($additionalauthor);
267         $sth=$dbh->prepare("insert into additionalauthors (biblionumber,author)
268             values ($biblionumber, $q_additionalauthor)");
269         $sth->execute;
270     }
271 }
272
273 sub newitem {
274     my $biblionumber=$input->param('biblionumber');
275     my $biblioitemnumber=$input->param('biblioitemnumber');
276     my $barcode=$input->param('barcode');
277     my $itemnotes=$input->param('notes');
278     my $q_itemnotes=$dbh->quote($itemnotes);
279     my $replacementprice=$input->param('replacementprice');
280     ($replacementprice) || ($replacementprice=0);
281     my $sth=$dbh->prepare("select max(itemnumber) from items");
282     $sth->execute;
283     my ($itemnumber) = $sth->fetchrow;
284     $itemnumber++;
285     my @datearr=localtime(time);
286     my $date=(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
287     my $q_homebranch=$dbh->quote($input->param('homebranch'));
288     $sth=$dbh->prepare("insert into items (itemnumber, biblionumber,
289     biblioitemnumber,barcode, itemnotes, holdingbranch, homebranch, dateaccessioned, replacementprice) values ($itemnumber,
290     $biblionumber, $biblioitemnumber, $barcode, $q_itemnotes, $q_homebranch, $q_homebranch, '$date', $replacementprice)");
291     $sth->execute;
292 }
293
294 if ($lccn) {
295     my $sth;
296     if ($lccn eq 'NULL') {
297         # set biblioitemnumber if not already initialised...
298         if ($biblioitemnumber eq '') {
299            $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
300            $sth->execute;
301            ($biblioitemnumber) = $sth->fetchrow;
302            $biblioitemnumber++;
303 #           print STDERR "BiblioItemNumber was missing: $biblioitemnumber \n";
304            }
305         $sth=$dbh->prepare("select biblionumber,biblioitemnumber from
306         biblioitems where biblioitemnumber=$biblioitemnumber");
307     } else {
308         $sth=$dbh->prepare("select biblionumber,biblioitemnumber from
309         biblioitems where lccn=$q_lccn");
310     }
311     $sth->execute;
312     if (my ($biblionumber, $biblioitemnumber) = $sth->fetchrow) {
313         sectioninfo();
314         $sth=$dbh->prepare("select I.barcode,I.itemnotes,B.title,B.author from items I, biblio B where B.biblionumber=I.biblionumber and biblioitemnumber=$biblioitemnumber");
315         $sth->execute;
316         my $itemdata='';
317         while (my ($barcode, $itemnotes, $title, $author) = $sth->fetchrow) {
318             $itemdata.="<tr><td align=center>$barcode</td><td><u>$title</u></td><td>$author</td><td>$itemnotes</td></tr>\n";
319
320         }
321         if ($itemdata) {
322             print << "EOF";
323     <center>
324     <p>
325     <table border=1 bgcolor=#dddddd>
326     <tr>
327     <th colspan=4>Existing Items with LCCN $lccn</th>
328     </tr>
329     <tr>
330     <th>Barcode</th><th>Title</th><th>Author</th><th>Notes</th></tr>
331     $itemdata
332     </table>
333     </center>
334
335 EOF
336         }
337 #       my $sth=$dbh->prepare("select max(barcode) from items");
338 #       $sth->execute;
339 #       my ($maxbarcode) = $sth->fetchrow;
340 #       $maxbarcode++;
341 #        print STDERR "MaxBarCode: $maxbarcode";
342         print << "EOF";
343 <center>
344 <h2>Section Three: Specific Item Information</h2>
345 <form>
346 <input type=hidden name=newitem value=1>
347 <input type=hidden name=biblionumber value=$biblionumber>
348 <input type=hidden name=biblioitemnumber value=$biblioitemnumber>
349 <table>
350 <!-- tr><td>BARCODE</td><td><input name=barcode size=10 value=\$maxbarcode -->
351 <tr><td>BARCODE</td><td><input name=barcode size=10 value=$biblionumber>
352 Home Branch: <select name=homebranch>
353 EOF
354 my $branches=getbranches();
355         foreach my $key (sort(keys %$branches)) {
356             print "<option value=\"$key\">$branches->{$key}->{'branchname'}</option>";
357         }
358 print << "EOF";
359         </select></td></tr>
360 </tr><td colspan=2>Replacement Price: <input name=replacementprice size=10></td></tr>
361 <tr><td>Notes</td><td><textarea name=notes rows=4 cols=40
362 wrap=physical></textarea></td></tr>
363 </table>
364 <input type=submit value="Add Item">
365 </form>
366 <h3>LCCN $lccn Information</h3>
367
368 </center>
369 EOF
370     } else {
371         sectioninfo();
372 print << "EOF";
373 <center>
374 <form>
375 <input type=hidden name=lccn value='$lccn'>
376 <input type=hidden name=checkforbiblio value=1>
377 <table border=0>
378 <tr><th colspan=2>Section One: Copyright Information</th></tr>
379 <tr><td>Title</td><td><input name=title size=40></td></tr>
380 <tr><td>Subtitle</td><td><input name=subtitle size=40></td></tr>
381 <tr><td>Author</td><td><input name=author size=40></td></tr>
382 <tr><td>Series Title<br>(if applicable)</td><td><input name=seriestitle
383     size=40></td></tr>
384 <tr><td>Copyright Date</td><td><input name=copyrightdate size=10></td></tr>
385 <tr><td>Notes</td><td><textarea name=notes rows=4 cols=40
386     wrap=physical></textarea></td></tr>
387 </table>
388 <input type=submit value="Add new Bibliography">
389 </center>
390 EOF
391     }
392 } else {
393     print << "EOF";
394 <h2>Adding new items to the Library Inventory</h2>
395 To add a new item, scan or type the LCCN number:
396 <br>
397 <form>
398 LCCN: <input name=lccn>
399 </form>
400 <p>
401 <a href=addbooks.pl?lccn=NULL>Enter book with no LCCN</a>
402 <hr>
403 Or use the <a href=marcimport.pl>MARC Importing tool</a>
404 EOF
405 }
406 print endmenu();
407 print endpage();
408
409
410
411
412
413 sub sectioninfo {
414     print << "EOF";
415     <center>
416     <table border=1 width=70% bgcolor=#bbddbb>
417     <tr>
418     <td>
419     <center>
420     Koha stores data in three sections.
421     </center>
422     <ol>
423     <li>The first section records bibliographic data such as title, author and
424     copyright for a particular work.
425     <li>The second records bibliographic data for a particular publication of that
426     work, such as ISBN number, physical description, publisher information, etc.
427     <li>The third section holds specific item information, such as the bar code
428     number.
429     </ul>
430     </td>
431     </tr>
432     </table>
433     </center>
434 EOF
435
436 }