Added simple acquisitions script
[koha.git] / acqui.simple / addbooks.pl
blob50efdd7c6b3a90353528ad673b1f6392be91c24d
1 #!/usr/bin/perl
4 # TODO
6 # Add info on biblioitems and items already entered as you enter new ones
9 use C4::Database;
10 use CGI;
11 use strict;
12 use C4::Acquisitions;
13 use C4::Output;
16 my $input = new CGI;
17 my $dbh=C4Connect;
20 my $isbn=$input->param('isbn');
21 my $q_isbn=$dbh->quote($isbn);
22 my $biblioitemnumber;
24 print $input->header;
25 print startpage();
26 print startmenu('acquisitions');
28 ($input->param('checkforbiblio')) && (checkforbiblio());
29 ($input->param('newbiblioitem')) && (newbiblioitem());
30 ($input->param('newitem')) && (newitem());
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;
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";
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";
89 if ($volumeddesc) {
90 $volumeinfo.=" $volumeddesc";
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";
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>
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>
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>
138 <tr><td align=right>Notes</td><td colspan=3><textarea name=notes rows=4 cols=50
139 wrap=physical></textarea></td></tr>
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>
151 print endmenu();
152 print endpage();
153 exit;
157 sub newbiblioitem {
158 my $biblionumber=$input->param('biblionumber');
159 my $volume=$input->param('volume');
160 my $q_volume=$dbh->quote($volume);
161 my $number=$input->param('number');
162 my $q_number=$dbh->quote($number);
163 my $classification=$input->param('classification');
164 my $q_classification=$dbh->quote($classification);
165 my $itemtype=$input->param('itemtype');
166 my $q_itemtype=$dbh->quote($itemtype);
167 my $issn=$input->param('issn');
168 my $q_issn=$dbh->quote($issn);
169 my $lccn=$input->param('lccn');
170 my $q_lccn=$dbh->quote($lccn);
171 my $dewey=$input->param('dewey');
172 my $q_dewey=$dbh->quote($dewey);
173 my $subclass=$input->param('subclass');
174 my $q_subclass=$dbh->quote($subclass);
175 my $publicationyear=$input->param('publicationyear');
176 my $q_publicationyear=$dbh->quote($publicationyear);
177 my $publishercode=$input->param('publishercode');
178 my $q_publishercode=$dbh->quote($publishercode);
179 my $volumedate=$input->param('volumedate');
180 my $q_volumedate=$dbh->quote($volumedate);
181 my $volumeddesc=$input->param('volumeddesc');
182 my $q_volumeddesc=$dbh->quote($volumeddesc);
183 my $illus=$input->param('illus');
184 my $q_illus=$dbh->quote($illus);
185 my $pages=$input->param('pages');
186 my $q_pages=$dbh->quote($pages);
187 my $notes=$input->param('notes');
188 my $q_notes=$dbh->quote($notes);
189 my $size=$input->param('size');
190 my $q_size=$dbh->quote($size);
191 my $place=$input->param('place');
192 my $q_place=$dbh->quote($place);
193 my $subjectheadings=$input->param('subjectheadings');
194 my $additionalauthors=$input->param('additionalauthors');
195 my $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
196 $sth->execute;
197 ($biblioitemnumber) = $sth->fetchrow;
198 $biblioitemnumber++;
199 ($q_isbn='') if ($q_isbn eq 'NULL');
200 $sth=$dbh->prepare("insert into biblioitems (biblioitemnumber,
201 biblionumber, volume, number, classification, itemtype, isbn, issn, lccn, dewey, subclass,
202 publicationyear, publishercode, volumedate, volumeddesc, illus, pages,
203 notes, size, place) values ($biblioitemnumber, $biblionumber, $q_volume,
204 $q_number, $q_classification, $q_itemtype, $q_isbn, $q_issn, $q_lccn, $q_dewey, $q_subclass,
205 $q_publicationyear, $q_publishercode, $q_volumedate, $q_volumeddesc,
206 $q_illus, $q_pages, $q_notes, $q_size, $q_place)");
207 $sth->execute;
208 my @subjectheadings=split(/\n/,$subjectheadings);
209 my $subjectheading;
210 foreach $subjectheading (@subjectheadings) {
211 # remove any line ending characters (Ctrl-J or M)
212 $subjectheading=~s/\013//g;
213 $subjectheading=~s/\010//g;
214 # convert to upper case
215 $subjectheading=uc($subjectheading);
216 print STDERR "S: $biblionumber, $subjectheading ";
217 chomp ($subjectheading);
218 print STDERR "B: ".ord(substr($subjectheading, length($subjectheading)-1, 1))." ";
219 while (ord(substr($subjectheading, length($subjectheading)-1, 1))<14) {
220 chop $subjectheading;
222 print STDERR "A: ".ord(substr($subjectheading, length($subjectheading)-1, 1))."\n";
223 # quote value
224 my $q_subjectheading=$dbh->quote($subjectheading);
225 $sth=$dbh->prepare("insert into bibliosubject (biblionumber,subject)
226 values ($biblionumber, $q_subjectheading)");
227 $sth->execute;
229 my @additionalauthors=split(/\n/,$additionalauthors);
230 my $additionalauthor;
231 foreach $additionalauthor (@additionalauthors) {
232 # remove any line ending characters (Ctrl-L or Ctrl-M)
233 $additionalauthor=~s/\013//g;
234 $additionalauthor=~s/\010//g;
235 # convert to upper case
236 $additionalauthor=uc($additionalauthor);
237 # quote value
238 my $q_additionalauthor=$dbh->quote($additionalauthor);
239 $sth=$dbh->prepare("insert into additionalauthors (biblionumber,author)
240 values ($biblionumber, $q_additionalauthor)");
241 $sth->execute;
245 sub newitem {
246 my $biblionumber=$input->param('biblionumber');
247 my $biblioitemnumber=$input->param('biblioitemnumber');
248 my $barcode=$input->param('barcode');
249 my $itemnotes=$input->param('notes');
250 my $q_itemnotes=$dbh->quote($itemnotes);
251 my $replacementprice=$input->param('replacementprice');
252 ($replacementprice) || ($replacementprice=0);
253 my $sth=$dbh->prepare("select max(itemnumber) from items");
254 $sth->execute;
255 my ($itemnumber) = $sth->fetchrow;
256 $itemnumber++;
257 my @datearr=localtime(time);
258 my $date=(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
259 my $q_homebranch=$dbh->quote($input->param('homebranch'));
260 $sth=$dbh->prepare("insert into items (itemnumber, biblionumber,
261 biblioitemnumber,barcode, itemnotes, holdingbranch, homebranch, dateaccessioned, replacementprice) values ($itemnumber,
262 $biblionumber, $biblioitemnumber, $barcode, $q_itemnotes, 'STWE', $q_homebranch, '$date', $replacementprice)");
263 $sth->execute;
266 if ($isbn) {
267 my $sth;
268 if ($isbn eq 'NULL') {
269 $sth=$dbh->prepare("select biblionumber,biblioitemnumber from
270 biblioitems where biblioitemnumber=$biblioitemnumber");
271 } else {
272 $sth=$dbh->prepare("select biblionumber,biblioitemnumber from
273 biblioitems where isbn=$q_isbn");
275 $sth->execute;
276 if (my ($biblionumber, $biblioitemnumber) = $sth->fetchrow) {
277 sectioninfo();
278 $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");
279 $sth->execute;
280 my $itemdata='';
281 while (my ($barcode, $itemnotes, $title, $author) = $sth->fetchrow) {
282 $itemdata.="<tr><td align=center>$barcode</td><td><u>$title</u></td><td>$author</td><td>$itemnotes</td></tr>\n";
285 if ($itemdata) {
286 print << "EOF";
287 <center>
289 <table border=1 bgcolor=#dddddd>
290 <tr>
291 <th colspan=4>Existing Items with ISBN $isbn</th>
292 </tr>
293 <tr>
294 <th>Barcode</th><th>Title</th><th>Author</th><th>Notes</th></tr>
295 $itemdata
296 </table>
297 </center>
301 my $sth=$dbh->prepare("select max(barcode) from items");
302 $sth->execute;
303 my ($maxbarcode) = $sth->fetchrow;
304 $maxbarcode++;
305 print << "EOF";
306 <center>
307 <h2>Section Three: Specific Item Information</h2>
308 <form>
309 <input type=hidden name=newitem value=1>
310 <input type=hidden name=biblionumber value=$biblionumber>
311 <input type=hidden name=biblioitemnumber value=$biblioitemnumber>
312 <table>
313 <tr><td>BARCODE</td><td><input name=barcode size=10 value=$maxbarcode> Home Branch: <select name=homebranch><option value='STWE'>Stewart Elementary<option value='MEZ'>Meziadin Elementary</select></td></tr>
314 </tr><td colspan=2>Replacement Price: <input name=replacementprice size=10></td></tr>
315 <tr><td>Notes</td><td><textarea name=notes rows=4 cols=40
316 wrap=physical></textarea></td></tr>
317 </table>
318 <input type=submit value="Add Item">
319 </form>
320 <h3>ISBN $isbn Information</h3>
322 </center>
324 } else {
325 sectioninfo();
326 print << "EOF";
327 <center>
328 <form>
329 <input type=hidden name=isbn value='$isbn'>
330 <input type=hidden name=checkforbiblio value=1>
331 <table border=0>
332 <tr><th colspan=2>Section One: Copyright Information</th></tr>
333 <tr><td>Title</td><td><input name=title size=40></td></tr>
334 <tr><td>Subtitle</td><td><input name=subtitle size=40></td></tr>
335 <tr><td>Author</td><td><input name=author size=40></td></tr>
336 <tr><td>Series Title<br>(if applicable)</td><td><input name=seriestitle
337 size=40></td></tr>
338 <tr><td>Copyright Date</td><td><input name=copyrightdate size=10></td></tr>
339 <tr><td>Notes</td><td><textarea name=notes rows=4 cols=40
340 wrap=physical></textarea></td></tr>
341 </table>
342 <input type=submit value="Add new Bibliography">
343 </center>
346 } else {
347 print << "EOF";
348 <h2>Adding new items to the Library Inventory</h2>
349 To add a new item, scan or type the ISBN number:
350 <br>
351 <form>
352 ISBN: <input name=isbn>
353 </form>
355 <a href=addbooks.pl?isbn=NULL>Enter book with no ISBN</a>
356 <hr>
357 Or use the <a href=marcimport.pl>MARC Importing tool</a>
360 print endmenu();
361 print endpage();
367 sub sectioninfo {
368 print << "EOF";
369 <center>
370 <table border=1 width=70% bgcolor=#bbddbb>
371 <tr>
372 <td>
373 <center>
374 Koha stores data in three sections.
375 </center>
376 <ol>
377 <li>The first section records bibliographic data such as title, author and
378 copyright for a particular work.
379 <li>The second records bibliographic data for a particular publication of that
380 work, such as ISBN number, physical description, publisher information, etc.
381 <li>The third section holds specific item information, such as the bar code
382 number.
383 </ul>
384 </td>
385 </tr>
386 </table>
387 </center>