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