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