POD added. Call to Bookfund.pm & Bookseller.pm added. Some sub renamed/API changed.
[koha.git] / acqui / newbiblio.pl
1 #!/usr/bin/perl
2
3 #script to show display basket of orders
4 #written by chris@katipo.co.nz 24/2/2000
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23
24 =head1 NAME
25
26 newbiblio.pl
27
28 =head1 DESCRIPTION
29 this script allows to create a new record to order it. This record shouldn't exist
30 on database.
31
32 =head1 CGI PARAMETERS
33
34 =over 4
35
36 =item booksellerid
37 the bookseller the librarian has to buy a new book.
38
39 =item title
40 the title of this new record.
41
42 =item author
43 the author of this new record.
44
45 =item copyright
46 the copyright of this new record.
47
48 =item ordnum
49 the number of this order.
50
51 =item biblio
52
53 =item basketno
54 the basket number for this new order.
55
56 =item suggestionid
57 if this order comes from a suggestion.
58
59 =item close
60
61 =back
62
63 =cut
64
65 use strict;
66 use CGI;
67 use C4::Context;
68 use C4::Input;
69 use C4::Database;
70 use C4::Auth;
71 use C4::Bookfund;
72 use C4::Bookseller;
73 use C4::Acquisition;
74 use C4::Suggestions;
75 use C4::Biblio;
76 use C4::Search;
77 use C4::Output;
78 use C4::Input;
79 use C4::Koha;
80 use C4::Interface::CGI::Output;
81 use HTML::Template;
82 use C4::Members;
83
84 my $input        = new CGI;
85 my $booksellerid = $input->param('booksellerid');
86 my $title        = $input->param('title');
87 my $author       = $input->param('author');
88 my $copyright    = $input->param('copyright');
89 my @booksellers  = GetBookSeller($booksellerid);
90 my $count        = scalar @booksellers;
91 my $ordnum       = $input->param('ordnum');
92 my $biblio       = $input->param('biblio');
93 my $basketno     = $input->param('basketno');
94 my $suggestionid = $input->param('suggestionid');
95 # my $donation     = $input->param('donation');
96 my $close        = $input->param('close');
97 my $data;
98 my $new;
99 my $dbh = C4::Context->dbh;
100
101 if ( $ordnum eq '' ) {    # create order
102     $new = 'yes';
103
104     #   $ordnum=newordernum;
105     if ( $biblio && !$suggestionid ) {
106         $data = bibdata($biblio);
107     }
108
109 # get suggestion fields if applicable. If it's a subscription renewal, then the biblio already exists
110 # otherwise, retrieve suggestion information.
111     if ($suggestionid) {
112         if ($biblio) {
113             $data = bibdata($biblio);
114         }
115         else {
116             $data = GetSuggestion($suggestionid);
117         }
118     }
119     if ( $data->{'title'} eq '' ) {
120         $data->{'title'}         = $title;
121         $data->{'author'}        = $author;
122         $data->{'copyrightdate'} = $copyright;
123     }
124 }
125 else {    #modify order
126     $data   = GetSingleOrder($ordnum);
127     $biblio = $data->{'biblionumber'};
128     #get basketno and suppleirno. too!
129     my $data2 = GetBasket( $data->{'basketno'} );
130     $basketno     = $data2->{'basketno'};
131     $booksellerid = $data2->{'booksellerid'};
132 }
133
134 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
135     {
136         template_name   => "acqui/newbiblio.tmpl",
137         query           => $input,
138         type            => "intranet",
139         authnotrequired => 0,
140         flagsrequired   => { acquisition => 1 },
141         debug           => 1,
142     }
143 );
144
145 # get currencies (for change rates calcs if needed)
146 my @rates = GetCurrencies();
147 my $count = scalar @rates;
148
149 my @loop_currency = ();
150 for ( my $i = 0 ; $i < $count ; $i++ ) {
151     my %line;
152     $line{currency} = $rates[$i]->{'currency'};
153     $line{rate}     = $rates[$i]->{'rate'};
154     push @loop_currency, \%line;
155 }
156
157 # build itemtype list
158 my $itemtypes = GetItemTypes;
159
160 my @itemtypesloop;
161 my %itemtypesloop;
162 foreach my $thisitemtype (sort keys %$itemtypes) {
163     push @itemtypesloop, $itemtypes->{$thisitemtype}->{'itemtype'};
164     $itemtypesloop{$itemtypes->{$thisitemtype}->{'itemtype'}} =        $itemtypes->{$thisitemtype}->{'description'};
165 }
166
167 my $CGIitemtype = CGI::scrolling_list(
168     -name     => 'format',
169     -values   => \@itemtypesloop,
170     -default  => $data->{'itemtype'},
171     -labels   => \%itemtypesloop,
172     -size     => 1,
173     -multiple => 0
174 );
175
176 # build branches list
177 my $branches = GetBranches;
178 my @branchloop;
179 foreach my $thisbranch ( sort keys %$branches ) {
180      my $selected = 1 if $thisbranch eq $branch;
181      my %row = (
182         value      => $thisbranch,
183         selected => $selected,
184         branchname => $branches->{$thisbranch}->{'branchname'},
185     );
186     push @branchloop, \%row;
187 }
188 $template->param( branchloop => \@branchloop );
189
190 # build bookfund list
191 my ($flags, $homebranch) = GetFlagsAndBranchFromBorrower($loggedinuser);
192
193 my $count2;
194 my @bookfund;
195 my @select_bookfund;
196 my %select_bookfunds;
197
198 @bookfund = GetBookFunds($homebranch);
199 $count2 = scalar @bookfund;
200
201 for ( my $i = 0 ; $i < $count2 ; $i++ ) {
202     push @select_bookfund, $bookfund[$i]->{'bookfundid'};
203     $select_bookfunds{ $bookfund[$i]->{'bookfundid'} } =
204       $bookfund[$i]->{'bookfundname'};
205 }
206 my $CGIbookfund = CGI::scrolling_list(
207     -name     => 'bookfund',
208     -values   => \@select_bookfund,
209     -default  => $data->{'bookfundid'},
210     -labels   => \%select_bookfunds,
211     -size     => 1,
212     -multiple => 0
213 );
214
215 my $bookfundname;
216 my $bookfundid;
217 if ($close) {
218     $bookfundid   = $data->{'bookfundid'};
219     $bookfundname = $select_bookfunds{$bookfundid};
220 }
221
222 #Build sort lists
223 my $CGIsort1 = buildCGIsort( "Asort1", "sort1", $data->{'sort1'} );
224 if ($CGIsort1) {
225     $template->param( CGIsort1 => $CGIsort1 );
226 }
227 else {
228     $template->param( sort1 => $data->{'sort1'} );
229 }
230
231 my $CGIsort2 = buildCGIsort( "Asort2", "sort2", $data->{'sort2'} );
232 if ($CGIsort2) {
233     $template->param( CGIsort2 => $CGIsort2 );
234 }
235 else {
236     $template->param( sort2 => $data->{'sort2'} );
237 }
238
239 my $bibitemsexists;
240
241 #do a biblioitems lookup on bib
242 my @bibitems = GetBiblioItemByBiblioNumber($biblio);
243 my $bibitemscount = scalar @bibitems;
244
245 if ( $bibitemscount > 0 ) {
246     # warn "NEWBIBLIO: bibitems for $biblio exists\n";
247     # warn Dumper $bibitemscount, @bibitems;
248     $bibitemsexists = 1;
249
250     my @bibitemloop;
251     for ( my $i = 0 ; $i < $bibitemscount ; $i++ ) {
252         my %line;
253         $line{biblioitemnumber} = $bibitems[$i]->{'biblioitemnumber'};
254         $line{isbn}             = $bibitems[$i]->{'isbn'};
255         $line{itemtype}         = $bibitems[$i]->{'itemtype'};
256         $line{volumeddesc}      = $bibitems[$i]->{'volumeddesc'};
257         push( @bibitemloop, \%line );
258
259         $template->param( bibitemloop => \@bibitemloop );
260     }
261     $template->param( bibitemexists => "1" );
262 }
263
264 # fill template
265 $template->param(
266     close        => $close,
267     bookfundid   => $bookfundid,
268     bookfundname => $bookfundname
269   )
270   if ($close);
271
272 $template->param(
273     existing         => $biblio,
274     title            => $title,
275     ordnum           => $ordnum,
276     basketno         => $basketno,
277     booksellerid     => $booksellerid,
278     suggestionid     => $suggestionid,
279     biblio           => $biblio,
280     biblioitemnumber => $data->{'biblioitemnumber'},
281     itemtype         => $data->{'itemtype'},
282     discount         => $booksellers[0]->{'discount'},
283     listincgst       => $booksellers[0]->{'listincgst'},
284     listprice        => $booksellers[0]->{'listprice'},
285     gstreg           => $booksellers[0]->{'gstreg'},
286     invoiceinc       => $booksellers[0]->{'invoiceincgst'},
287     invoicedisc      => $booksellers[0]->{'invoicedisc'},
288     nocalc           => $booksellers[0]->{'nocalc'},
289     name             => $booksellers[0]->{'name'},
290     currency         => $booksellers[0]->{'listprice'},
291     gstrate          => C4::Context->preference("gist"),
292     loop_currencies  => \@loop_currency,
293     orderexists      => ( $new eq 'yes' ) ? 0 : 1,
294     title            => $data->{'title'},
295     author           => $data->{'author'},
296     copyrightdate    => $data->{'copyrightdate'},
297     CGIitemtype      => $CGIitemtype,
298     CGIbookfund      => $CGIbookfund,
299     isbn             => $data->{'isbn'},
300     seriestitle      => $data->{'seriestitle'},
301     quantity         => $data->{'quantity'},
302     listprice        => $data->{'listprice'},
303     rrp              => $data->{'rrp'},
304     invoice          => $data->{'booksellerinvoicenumber'},
305     ecost            => $data->{'ecost'},
306     notes            => $data->{'notes'},
307     publishercode    => $data->{'publishercode'},
308 #     donation         => $donation
309 );
310
311 output_html_with_http_headers $input, $cookie, $template->output;