Code Cleaning Members.
[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::Interface::CGI::Output;
80 use C4::Branch; # GetBranches
81 use C4::Members;
82
83 my $input        = new CGI;
84 my $booksellerid = $input->param('booksellerid');
85 my $title        = $input->param('title');
86 my $author       = $input->param('author');
87 my $copyright    = $input->param('copyright');
88 my @booksellers  = GetBookSeller($booksellerid);
89 my $count        = scalar @booksellers;
90 my $ordnum       = $input->param('ordnum');
91 my $biblionumber       = $input->param('biblionumber');
92 my $basketno     = $input->param('basketno');
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 my %itemtypesloop;
156 foreach my $thisitemtype (sort keys %$itemtypes) {
157     push @itemtypesloop, $itemtypes->{$thisitemtype}->{'itemtype'};
158     $itemtypesloop{$itemtypes->{$thisitemtype}->{'itemtype'}} =        $itemtypes->{$thisitemtype}->{'description'};
159 }
160
161 my $CGIitemtype = CGI::scrolling_list(
162     -name     => 'format',
163     -values   => \@itemtypesloop,
164     -default  => $data->{'itemtype'},
165     -labels   => \%itemtypesloop,
166     -size     => 1,
167         -tabindex=>'',
168     -multiple => 0
169 );
170
171 # build branches list
172 my $onlymine=C4::Context->preference('IndependantBranches') && 
173              C4::Context->userenv && 
174              C4::Context->userenv->{flags}!=1 && 
175              C4::Context->userenv->{branch};
176 my $branches = GetBranches($onlymine);
177 my @branchloop;
178 foreach my $thisbranch ( sort keys %$branches ) {
179      my %row = (
180         value      => $thisbranch,
181         branchname => $branches->{$thisbranch}->{'branchname'},
182     );
183     push @branchloop, \%row;
184 }
185 $template->param( branchloop => \@branchloop );
186
187 # build bookfund list
188 my $borrower= GetBorrower($loggedinuser);
189 my ( $flags, $homebranch )= ($borrower->{'flags'},$borrower->{'branchcode'});
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         -tabindex=>'',
211     -multiple => 0
212 );
213
214 my $bookfundname;
215 my $bookfundid;
216 if ($close) {
217     $bookfundid   = $data->{'bookfundid'};
218     $bookfundname = $select_bookfunds{$bookfundid};
219 }
220
221 #Build sort lists
222 my $CGIsort1 = buildCGIsort( "Asort1", "sort1", $data->{'sort1'} );
223 if ($CGIsort1) {
224     $template->param( CGIsort1 => $CGIsort1 );
225 }
226 else {
227     $template->param( sort1 => $data->{'sort1'} );
228 }
229
230 my $CGIsort2 = buildCGIsort( "Asort2", "sort2", $data->{'sort2'} );
231 if ($CGIsort2) {
232     $template->param( CGIsort2 => $CGIsort2 );
233 }
234 else {
235     $template->param( sort2 => $data->{'sort2'} );
236 }
237
238 my $bibitemsexists;
239
240 #do a biblioitems lookup on bib
241 my @bibitems = GetBiblioItemByBiblioNumber($biblionumber);
242 my $bibitemscount = scalar @bibitems;
243
244 if ( $bibitemscount > 0 ) {
245     # warn "NEWBIBLIO: bibitems for $biblio exists\n";
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         => $biblionumber,
272     ordnum           => $ordnum,
273     basketno         => $basketno,
274     booksellerid     => $booksellerid,
275     suggestionid     => $suggestionid,
276     biblionumber           => $biblionumber,
277     biblioitemnumber => $data->{'biblioitemnumber'},
278     itemtype         => $data->{'itemtype'},
279     discount         => $booksellers[0]->{'discount'},
280     listincgst       => $booksellers[0]->{'listincgst'},
281     listprice        => $booksellers[0]->{'listprice'},
282     gstreg           => $booksellers[0]->{'gstreg'},
283     invoiceinc       => $booksellers[0]->{'invoiceincgst'},
284     invoicedisc      => $booksellers[0]->{'invoicedisc'},
285     nocalc           => $booksellers[0]->{'nocalc'},
286     name             => $booksellers[0]->{'name'},
287     currency         => $booksellers[0]->{'listprice'},
288     gstrate          => C4::Context->preference("gist"),
289     loop_currencies  => \@loop_currency,
290     orderexists      => ( $new eq 'yes' ) ? 0 : 1,
291     title            => $data->{'title'},
292     author           => $data->{'author'},
293     copyrightdate    => $data->{'copyrightdate'},
294     CGIitemtype      => $CGIitemtype,
295     CGIbookfund      => $CGIbookfund,
296     isbn             => $data->{'isbn'},
297     seriestitle      => $data->{'seriestitle'},
298     quantity         => $data->{'quantity'},
299     listprice        => $data->{'listprice'},
300     rrp              => $data->{'rrp'},
301     total            => $data->{ecost}*$data->{quantity},
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;