Change link to order.pl to booksellers.pl
[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 %row = (
181         value      => $thisbranch,
182         branchname => $branches->{$thisbranch}->{'branchname'},
183     );
184     push @branchloop, \%row;
185 }
186 $template->param( branchloop => \@branchloop );
187
188 # build bookfund list
189 my ($flags, $homebranch) = GetFlagsAndBranchFromBorrower($loggedinuser);
190
191 my $count2;
192 my @bookfund;
193 my @select_bookfund;
194 my %select_bookfunds;
195
196 @bookfund = GetBookFunds($homebranch);
197 $count2 = scalar @bookfund;
198
199 for ( my $i = 0 ; $i < $count2 ; $i++ ) {
200     push @select_bookfund, $bookfund[$i]->{'bookfundid'};
201     $select_bookfunds{ $bookfund[$i]->{'bookfundid'} } =
202       $bookfund[$i]->{'bookfundname'};
203 }
204 my $CGIbookfund = CGI::scrolling_list(
205     -name     => 'bookfund',
206     -values   => \@select_bookfund,
207     -default  => $data->{'bookfundid'},
208     -labels   => \%select_bookfunds,
209     -size     => 1,
210     -multiple => 0
211 );
212
213 my $bookfundname;
214 my $bookfundid;
215 if ($close) {
216     $bookfundid   = $data->{'bookfundid'};
217     $bookfundname = $select_bookfunds{$bookfundid};
218 }
219
220 #Build sort lists
221 my $CGIsort1 = buildCGIsort( "Asort1", "sort1", $data->{'sort1'} );
222 if ($CGIsort1) {
223     $template->param( CGIsort1 => $CGIsort1 );
224 }
225 else {
226     $template->param( sort1 => $data->{'sort1'} );
227 }
228
229 my $CGIsort2 = buildCGIsort( "Asort2", "sort2", $data->{'sort2'} );
230 if ($CGIsort2) {
231     $template->param( CGIsort2 => $CGIsort2 );
232 }
233 else {
234     $template->param( sort2 => $data->{'sort2'} );
235 }
236
237 my $bibitemsexists;
238
239 #do a biblioitems lookup on bib
240 my @bibitems = GetBiblioItemByBiblioNumber($biblio);
241 my $bibitemscount = scalar @bibitems;
242
243 if ( $bibitemscount > 0 ) {
244     # warn "NEWBIBLIO: bibitems for $biblio exists\n";
245     # warn Dumper $bibitemscount, @bibitems;
246     $bibitemsexists = 1;
247
248     my @bibitemloop;
249     for ( my $i = 0 ; $i < $bibitemscount ; $i++ ) {
250         my %line;
251         $line{biblioitemnumber} = $bibitems[$i]->{'biblioitemnumber'};
252         $line{isbn}             = $bibitems[$i]->{'isbn'};
253         $line{itemtype}         = $bibitems[$i]->{'itemtype'};
254         $line{volumeddesc}      = $bibitems[$i]->{'volumeddesc'};
255         push( @bibitemloop, \%line );
256
257         $template->param( bibitemloop => \@bibitemloop );
258     }
259     $template->param( bibitemexists => "1" );
260 }
261
262 # fill template
263 $template->param(
264     close        => $close,
265     bookfundid   => $bookfundid,
266     bookfundname => $bookfundname
267   )
268   if ($close);
269
270 $template->param(
271     existing         => $biblio,
272     title            => $title,
273     ordnum           => $ordnum,
274     basketno         => $basketno,
275     booksellerid     => $booksellerid,
276     suggestionid     => $suggestionid,
277     biblio           => $biblio,
278     biblioitemnumber => $data->{'biblioitemnumber'},
279     itemtype         => $data->{'itemtype'},
280     discount         => $booksellers[0]->{'discount'},
281     listincgst       => $booksellers[0]->{'listincgst'},
282     listprice        => $booksellers[0]->{'listprice'},
283     gstreg           => $booksellers[0]->{'gstreg'},
284     invoiceinc       => $booksellers[0]->{'invoiceincgst'},
285     invoicedisc      => $booksellers[0]->{'invoicedisc'},
286     nocalc           => $booksellers[0]->{'nocalc'},
287     name             => $booksellers[0]->{'name'},
288     currency         => $booksellers[0]->{'listprice'},
289     gstrate          => C4::Context->preference("gist"),
290     loop_currencies  => \@loop_currency,
291     orderexists      => ( $new eq 'yes' ) ? 0 : 1,
292     title            => $data->{'title'},
293     author           => $data->{'author'},
294     copyrightdate    => $data->{'copyrightdate'},
295     CGIitemtype      => $CGIitemtype,
296     CGIbookfund      => $CGIbookfund,
297     isbn             => $data->{'isbn'},
298     seriestitle      => $data->{'seriestitle'},
299     quantity         => $data->{'quantity'},
300     listprice        => $data->{'listprice'},
301     rrp              => $data->{'rrp'},
302     invoice          => $data->{'booksellerinvoicenumber'},
303     ecost            => $data->{'ecost'},
304     notes            => $data->{'notes'},
305     publishercode    => $data->{'publishercode'},
306 #     donation         => $donation
307 );
308
309 output_html_with_http_headers $input, $cookie, $template->output;