Tidying up formatting
[koha.git] / acqui / neworderempty.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 neworderempty.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
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::Output;
77 use C4::Input;
78 use C4::Koha;
79 use C4::Branch; # GetBranches
80 use C4::Members;
81
82 my $input        = new CGI;
83 my $booksellerid = $input->param('booksellerid');
84 my $title        = $input->param('title');
85 my $author       = $input->param('author');
86 my $copyright    = $input->param('copyright');
87 my @booksellers  = GetBookSeller($booksellerid);
88 my $count        = scalar @booksellers;
89 my $ordnum       = $input->param('ordnum');
90 my $biblionumber       = $input->param('biblionumber');
91 my $basketno     = $input->param('basketno');
92 my $purchaseorder= $input->param('purchaseordernumber');
93 my $suggestionid = $input->param('suggestionid');
94 # my $donation     = $input->param('donation');
95 my $close        = $input->param('close');
96 my $data;
97 my $new;
98 my $dbh = C4::Context->dbh;
99
100 if ( $ordnum eq '' ) {    # create order
101     $new = 'yes';
102
103     #   $ordnum=newordernum;
104     if ( $biblionumber && !$suggestionid ) {
105         $data = GetBiblioData($biblionumber);
106     }
107
108 # get suggestion fields if applicable. If it's a subscription renewal, then the biblio already exists
109 # otherwise, retrieve suggestion information.
110     if ($suggestionid) {
111         if ($biblionumber) {
112             $data = GetBiblioData($biblionumber);
113         }
114         else {
115             $data = GetSuggestion($suggestionid);
116         }
117     }
118 }
119 else {    #modify order
120     $data   = GetOrder($ordnum);
121     $biblionumber = $data->{'biblionumber'};
122     #get basketno and suppleirno. too!
123     my $data2 = GetBasket( $data->{'basketno'} );
124     $basketno     = $data2->{'basketno'};
125     $booksellerid = $data2->{'booksellerid'};
126 }
127
128 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
129     {
130         template_name   => "acqui/neworderempty.tmpl",
131         query           => $input,
132         type            => "intranet",
133         authnotrequired => 0,
134         flagsrequired   => { acquisition => 1 },
135         debug           => 1,
136     }
137 );
138
139 # get currencies (for change rates calcs if needed)
140 my @rates = GetCurrencies();
141 $count = scalar @rates;
142
143 my @loop_currency = ();
144 for ( my $i = 0 ; $i < $count ; $i++ ) {
145     my %line;
146     $line{currency} = $rates[$i]->{'currency'};
147     $line{rate}     = $rates[$i]->{'rate'};
148     push @loop_currency, \%line;
149 }
150
151 # build itemtype list
152 my $itemtypes = GetItemTypes;
153
154 my @itemtypesloop;
155 foreach my $thisitemtype (sort keys %$itemtypes) {
156     push @itemtypesloop, { itemtype => $itemtypes->{$thisitemtype}->{'itemtype'} , desc =>  $itemtypes->{$thisitemtype}->{'description'} } ;
157 }
158
159 # build branches list
160 my $onlymine=C4::Context->preference('IndependantBranches') && 
161              C4::Context->userenv && 
162              C4::Context->userenv->{flags}!=1 && 
163              C4::Context->userenv->{branch};
164 my $branches = GetBranches($onlymine);
165 my @branchloop;
166 foreach my $thisbranch ( sort keys %$branches ) {
167      my %row = (
168         value      => $thisbranch,
169         branchname => $branches->{$thisbranch}->{'branchname'},
170     );
171     push @branchloop, \%row;
172 }
173 $template->param( branchloop => \@branchloop , itypeloop => \@itemtypesloop );
174
175 # build bookfund list
176 my $borrower= GetMember($loggedinuser);
177 my ( $flags, $homebranch )= ($borrower->{'flags'},$borrower->{'branchcode'});
178
179 my $count2;
180 my @bookfund;
181 my @select_bookfund;
182 my %select_bookfunds;
183
184 @bookfund = GetBookFunds($homebranch);
185 $count2 = scalar @bookfund;
186
187 for ( my $i = 0 ; $i < $count2 ; $i++ ) {
188     push @select_bookfund, $bookfund[$i]->{'bookfundid'};
189     $select_bookfunds{ $bookfund[$i]->{'bookfundid'} } =
190       $bookfund[$i]->{'bookfundname'};
191 }
192 my $CGIbookfund = CGI::scrolling_list(
193     -name     => 'bookfund',
194         -id         => 'bookfund',
195     -values   => \@select_bookfund,
196     -default  => $data->{'bookfundid'},
197     -labels   => \%select_bookfunds,
198     -size     => 1,
199         -tabindex=>'',
200     -multiple => 0
201 );
202
203 my $bookfundname;
204 my $bookfundid;
205 if ($close) {
206     $bookfundid   = $data->{'bookfundid'};
207     $bookfundname = $select_bookfunds{$bookfundid};
208 }
209
210 #Build sort lists
211 my $CGIsort1 = buildCGIsort( "Asort1", "sort1", $data->{'sort1'} );
212 if ($CGIsort1) {
213     $template->param( CGIsort1 => $CGIsort1 );
214 }
215 else {
216     $template->param( sort1 => $data->{'sort1'} );
217 }
218
219 my $CGIsort2 = buildCGIsort( "Asort2", "sort2", $data->{'sort2'} );
220 if ($CGIsort2) {
221     $template->param( CGIsort2 => $CGIsort2 );
222 }
223 else {
224     $template->param( sort2 => $data->{'sort2'} );
225 }
226
227 my $bibitemsexists;
228
229 #do a biblioitems lookup on bib
230 my @bibitems = GetBiblioItemByBiblioNumber($biblionumber);
231 my $bibitemscount = scalar @bibitems;
232
233 if ( $bibitemscount > 0 ) {
234     # warn "NEWBIBLIO: bibitems for $biblio exists\n";
235     $bibitemsexists = 1;
236
237     my @bibitemloop;
238     for ( my $i = 0 ; $i < $bibitemscount ; $i++ ) {
239         my %line;
240         $line{biblioitemnumber} = $bibitems[$i]->{'biblioitemnumber'};
241         $line{isbn}             = $bibitems[$i]->{'isbn'};
242         $line{itemtype}         = $bibitems[$i]->{'itemtype'};
243         $line{volumeddesc}      = $bibitems[$i]->{'volumeddesc'};
244         push( @bibitemloop, \%line );
245
246         $template->param( bibitemloop => \@bibitemloop );
247     }
248     $template->param( bibitemexists => "1" );
249 }
250
251 # fill template
252 $template->param(
253     close        => $close,
254     bookfundid   => $bookfundid,
255     bookfundname => $bookfundname
256   )
257   if ($close);
258
259 $template->param(
260     existing         => $biblionumber,
261     ordnum           => $ordnum,
262     basketno         => $basketno,
263     booksellerid     => $booksellerid,
264     suggestionid     => $suggestionid,
265     biblionumber           => $biblionumber,
266     authorisedbyname => $borrower->{'firstname'} . " " . $borrower->{'surname'},
267         biblioitemnumber => $data->{'biblioitemnumber'},
268     itemtype         => $data->{'itemtype'},
269     itemtype_desc         => $itemtypes->{$data->{'itemtype'}}->{description},
270     discount         => $booksellers[0]->{'discount'},
271     listincgst       => $booksellers[0]->{'listincgst'},
272     listprice        => $booksellers[0]->{'listprice'},
273     gstreg           => $booksellers[0]->{'gstreg'},
274     invoiceinc       => $booksellers[0]->{'invoiceincgst'},
275     invoicedisc      => $booksellers[0]->{'invoicedisc'},
276     nocalc           => $booksellers[0]->{'nocalc'},
277     name             => $booksellers[0]->{'name'},
278     currency         => $booksellers[0]->{'listprice'},
279     gstrate          => C4::Context->preference("gist"),
280     loop_currencies  => \@loop_currency,
281     orderexists      => ( $new eq 'yes' ) ? 0 : 1,
282     title            => $data->{'title'},
283     author           => $data->{'author'},
284     copyrightdate    => $data->{'copyrightdate'},
285     CGIbookfund      => $CGIbookfund,
286     isbn             => $data->{'isbn'},
287     seriestitle      => $data->{'seriestitle'},
288     quantity         => $data->{'quantity'},
289     listprice        => $data->{'listprice'},
290     rrp              => $data->{'rrp'},
291     total            => $data->{ecost}*$data->{quantity},
292     invoice          => $data->{'booksellerinvoicenumber'},
293     ecost            => $data->{'ecost'},
294     purchaseordernumber            => $data->{'purchaseordernumber'},
295     notes            => $data->{'notes'},
296     publishercode    => $data->{'publishercode'},
297 #     donation         => $donation
298 );
299
300 output_html_with_http_headers $input, $cookie, $template->output;