fix bad link.
[koha.git] / acqui / newbiblio.pl
index 4b714bc..23d453a 100755 (executable)
 #script to show display basket of orders
 #written by chris@katipo.co.nz 24/2/2000
 
-use C4::Acquisitions;
-use C4::Output;
-use C4::Search;
-use C4::Database;
-use CGI;
+# Copyright 2000-2002 Katipo Communications
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+
+
+=head1 NAME
+
+newbiblio.pl
+
+=head1 DESCRIPTION
+this script allows to create a new record to order it. This record shouldn't exist
+on database.
+
+=head1 CGI PARAMETERS
+
+=over 4
+
+=item booksellerid
+the bookseller the librarian has to buy a new book.
+
+=item title
+the title of this new record.
+
+=item author
+the author of this new record.
+
+=item copyright
+the copyright of this new record.
+
+=item ordnum
+the number of this order.
+
+=item biblio
+
+=item basketno
+the basket number for this new order.
+
+=item suggestionid
+if this order comes from a suggestion.
+
+=item close
+
+=back
+
+=cut
+
 use strict;
+use CGI;
+use C4::Context;
+use C4::Input;
+use C4::Database;
+use C4::Auth;
+use C4::Bookfund;
+use C4::Bookseller;
+use C4::Acquisition;
+use C4::Suggestions;
+use C4::Biblio;
+use C4::Search;
+use C4::Output;
+use C4::Input;
+use C4::Koha;
+use C4::Interface::CGI::Output;
+use HTML::Template;
+use C4::Members;
 
-my $input=new CGI;
-print $input->header();
-my $id=$input->param('id');
-my $title=$input->param('title');
-my $author=$input->param('author');
-my $copyright=$input->param('copyright');
-my ($count,@booksellers)=bookseller($id);
-my $ordnum=$input->param('ordnum');
-my $biblio=$input->param('biblio');
+my $input        = new CGI;
+my $booksellerid = $input->param('booksellerid');
+my $title        = $input->param('title');
+my $author       = $input->param('author');
+my $copyright    = $input->param('copyright');
+my @booksellers  = GetBookSeller($booksellerid);
+my $count        = scalar @booksellers;
+my $ordnum       = $input->param('ordnum');
+my $biblio       = $input->param('biblio');
+my $basketno     = $input->param('basketno');
+my $suggestionid = $input->param('suggestionid');
+# my $donation     = $input->param('donation');
+my $close        = $input->param('close');
 my $data;
 my $new;
-if ($ordnum eq ''){
-  $new='yes';
-  $ordnum=newordernum;
-  $data=bibdata($biblio);
-  if ($data->{'title'} eq ''){
-    $data->{'title'}=$title;
-    $data->{'author'}=$author;
-    $data->{'copyrightdate'}=$copyright;
-  }
-}else {
-  $data=getsingleorder($ordnum);
-  $biblio=$data->{'biblionumber'};
-} 
-
-print startpage;
-
-print startmenu('acquisitions');
-
-
-my $basket=$input->param('basket');
-print <<printend
-
-
-<script language="javascript" type="text/javascript">
-
-<!--
-
-function update(f){
-  //collect values
-  quantity=f.quantity.value
-  discount=f.discount.value
-  listinc=parseInt(f.listinc.value)
-  currency=f.currency.value
-  applygst=parseInt(f.applygst.value)
-  listprice=f.list_price.value
-  //  rrp=f.rrp.value
-  //  ecost=f.ecost.value  //budgetted cost
-  //  GST=f.GST.value
-  //  total=f.total.value  
-  //make useful constants out of the above  
-  exchangerate=f.elements[currency].value      //get exchange rate  
-  gst_on=(!listinc && applygst);
-  //do real stuff  
-  rrp=listprice*exchangerate;
-  ecost=rrp*(100-discount)/100
-  GST=0;
-  if (gst_on){
-    rrp=rrp*1.125;
-    GST=ecost*0.125
-  }
-  
-  total=(ecost+GST)*quantity
-  
-  
-  f.rrp.value=display(rrp)
-  f.ecost.value=display(ecost)
-  f.GST.value=display(GST)
-  f.total.value=display(total)
-  
+my $dbh = C4::Context->dbh;
+
+if ( $ordnum eq '' ) {    # create order
+    $new = 'yes';
+
+    #  $ordnum=newordernum;
+    if ( $biblio && !$suggestionid ) {
+        $data = bibdata($biblio);
+    }
+
+# get suggestion fields if applicable. If it's a subscription renewal, then the biblio already exists
+# otherwise, retrieve suggestion information.
+    if ($suggestionid) {
+        if ($biblio) {
+            $data = bibdata($biblio);
+        }
+        else {
+            $data = GetSuggestion($suggestionid);
+        }
+    }
+    if ( $data->{'title'} eq '' ) {
+        $data->{'title'}         = $title;
+        $data->{'author'}        = $author;
+        $data->{'copyrightdate'} = $copyright;
+    }
 }
-                                                                     
+else {    #modify order
+    $data   = GetSingleOrder($ordnum);
+    $biblio = $data->{'biblionumber'};
+    #get basketno and suppleirno. too!
+    my $data2 = GetBasket( $data->{'basketno'} );
+    $basketno     = $data2->{'basketno'};
+    $booksellerid = $data2->{'booksellerid'};
+}
+
+my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
+    {
+        template_name   => "acqui/newbiblio.tmpl",
+        query           => $input,
+        type            => "intranet",
+        authnotrequired => 0,
+        flagsrequired   => { acquisition => 1 },
+        debug           => 1,
+    }
+);
 
+# get currencies (for change rates calcs if needed)
+my @rates = GetCurrencies();
+my $count = scalar @rates;
 
-function messenger(X,Y,etc){
-win=window.open("","mess","height="+X+",width="+Y+",screenX=150,screenY=0");
-win.focus();
-win.document.close();
-win.document.write("<body link='#333333' bgcolor='#ffffff' text='#000000'><font size=2><p><br>");
-win.document.write(etc);
-win.document.write("<center><form><input type=button onclick='self.close()' value=Close></form></center>");
-win.document.write("</font></body></html>");
+my @loop_currency = ();
+for ( my $i = 0 ; $i < $count ; $i++ ) {
+    my %line;
+    $line{currency} = $rates[$i]->{'currency'};
+    $line{rate}     = $rates[$i]->{'rate'};
+    push @loop_currency, \%line;
 }
-//-->
 
-</script>
-<form action=/cgi-bin/koha/acqui/addorder.pl method=post name=frusin>
-printend
-;
+# build itemtype list
+my $itemtypes = GetItemTypes;
 
-if ($biblio eq ''){
-  print "<input type=hidden name=existing value=no>";
+my @itemtypesloop;
+my %itemtypesloop;
+foreach my $thisitemtype (sort keys %$itemtypes) {
+    push @itemtypesloop, $itemtypes->{$thisitemtype}->{'itemtype'};
+    $itemtypesloop{$itemtypes->{$thisitemtype}->{'itemtype'}} =        $itemtypes->{$thisitemtype}->{'description'};
 }
 
-print <<printend
-<!--$title-->
-<input type=hidden name=ordnum value=$ordnum>
-<input type=hidden name=basket value=$basket>
-<input type=hidden name=supplier value=$id>
-<input type=hidden name=biblio value=$biblio>
-<input type=hidden name=bibitemnum value=$data->{'biblioitemnumber'}>
-<input type=hidden name=oldtype value=$data->{'itemtype'}>
-<input type=hidden name=discount value=$booksellers[0]->{'discount'}>
-<input type=hidden name=listinc value=$booksellers[0]->{'listincgst'}>
-<input type=hidden name=currency value=$booksellers[0]->{'listprice'}>
-<input type=hidden name=applygst value=$booksellers[0]->{'gstreg'}>
-printend
-;
-my ($count2,$currencies)=getcurrencies;
-for (my $i=0;$i<$count2;$i++){
-  print "<input type=hidden name=\"$currencies->[$i]->{'currency'}\" value=$currencies->[0]->{'rate'}>\n";
+my $CGIitemtype = CGI::scrolling_list(
+    -name     => 'format',
+    -values   => \@itemtypesloop,
+    -default  => $data->{'itemtype'},
+    -labels   => \%itemtypesloop,
+    -size     => 1,
+    -multiple => 0
+);
+
+# build branches list
+my $branches = GetBranches;
+my @branchloop;
+foreach my $thisbranch ( sort keys %$branches ) {
+     my %row = (
+        value      => $thisbranch,
+        branchname => $branches->{$thisbranch}->{'branchname'},
+    );
+    push @branchloop, \%row;
 }
-if ($new ne 'yes'){
-  print "<input type=hidden name=orderexists value=yes>\n";
+$template->param( branchloop => \@branchloop );
+
+# build bookfund list
+my ($flags, $homebranch) = GetFlagsAndBranchFromBorrower($loggedinuser);
+
+my $count2;
+my @bookfund;
+my @select_bookfund;
+my %select_bookfunds;
+
+@bookfund = GetBookFunds($homebranch);
+$count2 = scalar @bookfund;
+
+for ( my $i = 0 ; $i < $count2 ; $i++ ) {
+    push @select_bookfund, $bookfund[$i]->{'bookfundid'};
+    $select_bookfunds{ $bookfund[$i]->{'bookfundid'} } =
+      $bookfund[$i]->{'bookfundname'};
+}
+my $CGIbookfund = CGI::scrolling_list(
+    -name     => 'bookfund',
+    -values   => \@select_bookfund,
+    -default  => $data->{'bookfundid'},
+    -labels   => \%select_bookfunds,
+    -size     => 1,
+    -multiple => 0
+);
+
+my $bookfundname;
+my $bookfundid;
+if ($close) {
+    $bookfundid   = $data->{'bookfundid'};
+    $bookfundname = $select_bookfunds{$bookfundid};
 }
-print <<printend
-<a href=basket.pl?basket=$basket><img src=/images/view-basket.gif width=187 heigth=42 border=0 align=right alt="View Basket"></a> 
-<FONT SIZE=6><em>$ordnum - Order Details </em></FONT><br>
-Shopping Basket For: $booksellers[0]->{'name'}
-<P>
-<CENTER>
-<TABLE  CELLSPACING=0  CELLPADDING=5 border=1 align=left width="40%">
-<tr valign=top bgcolor=#99cc33><td background="/images/background-mem.gif" colspan=2><B>CATALOGUE DETAILS</B></td></tr>
-<TR VALIGN=TOP>
-<TD><b>Title *</b></td>
-<td><input type=text size=20 name=title value="$data->{'title'}">
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD>Author</td>
-<td><input type=text size=20 name=author value="$data->{'author'}" >
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD>Copyright Date</td>
-<td><input type=text size=20 name=copyright value="$data->{'copyrightdate'}">
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD>Format</td>
-<td>
-<select name=format size=1>
-printend
-;
-
-my $dbh=C4Connect;
-my $query="Select itemtype,description from itemtypes order by description";
-my $sth=$dbh->prepare($query);
-$sth->execute;
-print "<option value=\"\">Please choose:\n";
-while (my $data=$sth->fetchrow_hashref){
-  print "<option value=\"" . $data->{'itemtype'} . "\">" . $data->{'description'} . "\n";
+
+#Build sort lists
+my $CGIsort1 = buildCGIsort( "Asort1", "sort1", $data->{'sort1'} );
+if ($CGIsort1) {
+    $template->param( CGIsort1 => $CGIsort1 );
 }
-$sth->finish;
-$dbh->disconnect;
-
-print <<printend
-</select>
-
-
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD>ISBN</td>
-<td><input type=text size=20 name=ISBN value=$data->{'isbn'}>
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD>Series</td>
-<td><input type=text size=20 name=Series value="$data->{'seriestitle'}">
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD>Branch</td>
-<td><select name=branch size=1>
-printend
-;
-my ($count2,@branches)=branches;
-for (my $i=0;$i<$count2;$i++){
-  print "<option value=$branches[$i]->{'branchcode'}";
-  if ($data->{'branchcode'} == $branches[$i]->{'branchcode'}){
-    print " Selected";
-  }
-  print ">$branches[$i]->{'branchname'}";
+else {
+    $template->param( sort1 => $data->{'sort1'} );
 }
 
-print <<printend
-</select>
-</td>
-</tr>
-<TR VALIGN=TOP  bgcolor=#ffffcc>
-<TD >Item Barcode</td>
-<td><input type=text size=20 name=barcode value=
-printend
-;
-
-my %systemprefs=systemprefs();
-if ($systemprefs{'autoBarcode'} eq '1') {
-  my $dbh=C4Connect;
-  my $query="Select barcode from items order by barcode desc";
-  my $sth=$dbh->prepare($query);
-  $sth->execute;
-  my $data=$sth->fetchrow_hashref;
-  print $data->{'barcode'}+1;
-  $sth->finish;
-  $dbh->disconnect;
+my $CGIsort2 = buildCGIsort( "Asort2", "sort2", $data->{'sort2'} );
+if ($CGIsort2) {
+    $template->param( CGIsort2 => $CGIsort2 );
+}
+else {
+    $template->param( sort2 => $data->{'sort2'} );
 }
 
-print <<printend
->
-</td>
-</tr>
-</table>
-<img src="/images/holder.gif" width=32 height=250 align=left>
-<table border=1 cellspacing=0 cellpadding=5 width="40%">
-<tr valign=top bgcolor=#99cc33><td background="/images/background-mem.gif" colspan=2><B>ACCOUNTING DETAILS</B></td></tr>
-<TR VALIGN=TOP>
-<TD>Quantity</td>
-<td><input type=text size=20 name=quantity value="$data->{'quantity'}" onchange='update(this.form)' >
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD>Bookfund</td>
-<td><select name=bookfund size=1>
-printend
-;
-
-my ($count2,@bookfund)=bookfunds;
-for (my $i=0;$i<$count2;$i++){
-  print "<option value=$bookfund[$i]->{'bookfundid'}";
-  if ($data->{'bookfundid'} == $bookfund[$i]->{'bookfundid'}){
-    print " Selected";
-  }
-  print ">$bookfund[$i]->{'bookfundname'}";
+my $bibitemsexists;
+
+#do a biblioitems lookup on bib
+my @bibitems = GetBiblioItemByBiblioNumber($biblio);
+my $bibitemscount = scalar @bibitems;
+
+if ( $bibitemscount > 0 ) {
+    # warn "NEWBIBLIO: bibitems for $biblio exists\n";
+    # warn Dumper $bibitemscount, @bibitems;
+    $bibitemsexists = 1;
+
+    my @bibitemloop;
+    for ( my $i = 0 ; $i < $bibitemscount ; $i++ ) {
+        my %line;
+        $line{biblioitemnumber} = $bibitems[$i]->{'biblioitemnumber'};
+        $line{isbn}             = $bibitems[$i]->{'isbn'};
+        $line{itemtype}         = $bibitems[$i]->{'itemtype'};
+        $line{volumeddesc}      = $bibitems[$i]->{'volumeddesc'};
+        push( @bibitemloop, \%line );
+
+        $template->param( bibitemloop => \@bibitemloop );
+    }
+    $template->param( bibitemexists => "1" );
 }
 
-print <<printend
-</select>
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD>Suppliers List Price</td>
-<td><input type=text size=20 name=list_price value="$data->{'listprice'}" onchange='update(this.form)'>
-</tr>
-<TR VALIGN=TOP>
-<TD>Replacement Cost <br>
-<FONT SIZE=2>(NZ\$ inc GST)</td>
-<td><input type=text size=20 name=rrp value="$data->{'rrp'}" onchange='update(this.form)'>
-</tr>
-<TR VALIGN=TOP>
-<TD>
-Budgeted Cost<BR>
-<FONT SIZE=2>(NZ\$ ex GST, inc discount)</FONT> </td>
-<td><input type=text size=20 name=ecost value="$data->{'ecost'}" onchange='update(this.form)'>
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD>
-Budgeted GST</td>
-<td><input type=text size=20 name=GST value="" onchange='update(this.form)'>
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD><B>
-BUDGETED TOTAL</B></td>
-<td><input type=text size=20 name=total value="" onchange='update(this.form)'>
-</td>
-</tr>
-<TR VALIGN=TOP  bgcolor=#ffffcc>
-<TD>Actual Cost</td>
-<td><input type=text size=20 name=cost>
-</td>
-</tr>
-<TR VALIGN=TOP  bgcolor=#ffffcc>
-<TD>Invoice Number *</td>
-<td><input type=text size=20 name=invoice >
-<TR VALIGN=TOP>
-<TD>Notes</td>
-<td><input type=text size=20 name=notes value="$data->{'notes'}">
-</td>
-</tr>
-<TR VALIGN=TOP>
-<TD colspan=2>
-<input type=image  name=submit src=/images/add-order.gif border=0 width=187 height=42 align=right>
-</td>
-</tr>
-</table>
-</form>
-</center>
-<table>
-<tr><td bgcolor=#cccc99  background="/images/background-mem.gif"><B>HELP</B><br>
-<UL>
-<LI>If ordering more than one copy of an item you will be prompted to  choose additional bookfunds, and put in additional barcodes at the next screen<P>
-<LI><B>Bold</B> fields must be filled in to create a new bibilo and item.<p>
-<LI>Shaded fields can be used to do a "quick" receive, when items have been purchased locally or gifted. In this case the quantity "ordered" will also  be entered into the database as the quantity received.
-</UL>
-</td></tr></table>
-<p> &nbsp; </p>
-printend
-;
-
-print endmenu('acquisitions');
-
-print endpage;
+# fill template
+$template->param(
+    close        => $close,
+    bookfundid   => $bookfundid,
+    bookfundname => $bookfundname
+  )
+  if ($close);
+
+$template->param(
+    existing         => $biblio,
+    title            => $title,
+    ordnum           => $ordnum,
+    basketno         => $basketno,
+    booksellerid     => $booksellerid,
+    suggestionid     => $suggestionid,
+    biblio           => $biblio,
+    biblioitemnumber => $data->{'biblioitemnumber'},
+    itemtype         => $data->{'itemtype'},
+    discount         => $booksellers[0]->{'discount'},
+    listincgst       => $booksellers[0]->{'listincgst'},
+    listprice        => $booksellers[0]->{'listprice'},
+    gstreg           => $booksellers[0]->{'gstreg'},
+    invoiceinc       => $booksellers[0]->{'invoiceincgst'},
+    invoicedisc      => $booksellers[0]->{'invoicedisc'},
+    nocalc           => $booksellers[0]->{'nocalc'},
+    name             => $booksellers[0]->{'name'},
+    currency         => $booksellers[0]->{'listprice'},
+    gstrate          => C4::Context->preference("gist"),
+    loop_currencies  => \@loop_currency,
+    orderexists      => ( $new eq 'yes' ) ? 0 : 1,
+    title            => $data->{'title'},
+    author           => $data->{'author'},
+    copyrightdate    => $data->{'copyrightdate'},
+    CGIitemtype      => $CGIitemtype,
+    CGIbookfund      => $CGIbookfund,
+    isbn             => $data->{'isbn'},
+    seriestitle      => $data->{'seriestitle'},
+    quantity         => $data->{'quantity'},
+    listprice        => $data->{'listprice'},
+    rrp              => $data->{'rrp'},
+    invoice          => $data->{'booksellerinvoicenumber'},
+    ecost            => $data->{'ecost'},
+    notes            => $data->{'notes'},
+    publishercode    => $data->{'publishercode'},
+#     donation         => $donation
+);
+
+output_html_with_http_headers $input, $cookie, $template->output;