A new acquisition to handle different tax values to each item, receiving multiple...
[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::Auth;
69 use C4::Bookfund;
70 use C4::Bookseller;
71 use C4::Acquisition;
72 use C4::Suggestions;
73 use C4::Biblio;
74 use C4::Search;
75 use C4::Koha;
76 use C4::Interface::CGI::Output;
77 use C4::Members;
78 use C4::Input;
79 use C4::Date;
80
81 my $input        = new CGI;
82 my $booksellerid = $input->param('booksellerid');
83 my $title        = $input->param('title');
84 my $author       = $input->param('author');
85 my $copyright    = $input->param('copyright');
86 my @booksellers  = GetBookSeller($booksellerid);
87 my $count        = scalar @booksellers;
88 my $ordnum       = $input->param('ordnum');
89 my $biblionumber       = $input->param('biblionumber');
90 my $basketno     = $input->param('basketno');
91 my $suggestionid = $input->param('suggestionid');
92 my $close        = $input->param('close');
93 my $data;
94 my $new;
95
96 my $dbh = C4::Context->dbh;
97
98 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
99     {
100         template_name   => "acqui/neworderempty.tmpl",
101         query           => $input,
102         type            => "intranet",
103         authnotrequired => 0,
104         flagsrequired   => { acquisition => 1 },
105         debug           => 1,
106     }
107 );
108 my $me= C4::Context->userenv;
109 my $homebranch=$me->{'branch'} ;
110 my $branch;
111 my $bookfundid;
112 my $discount= $booksellers[0]->{'discount'};
113 my $gstrate=C4::Context->preference('gist')*100;
114 if ( $ordnum eq '' ) {    # create order
115     $new = 'yes';
116     if ( $biblionumber  ) {
117         my $record=XMLgetbibliohash($dbh,$biblionumber);
118           ###Error checking if a non existent biblionumber given manually
119         if (!$record){
120         print $input->redirect("/cgi-bin/koha/acqui/basket.pl?supplierid=$booksellerid");
121         }
122          $data = XMLmarc2koha_onerecord($dbh,$record,"biblios");
123     }elsif($suggestionid){
124         $data = GetSuggestion($suggestionid);
125     
126          if ( $data->{'title'} eq '' ) {
127                 $data->{'title'}         = $title;
128          $data->{'author'}        = $author;
129          $data->{'copyrightdate'} = $copyright;
130         }
131    }### if biblionumber
132  if ($basketno){
133          my $basket = GetBasket( $basketno);
134         my @orders=GetOrders($basketno);
135                 if (@orders){
136                 $template->param(
137                 purchaseordernumber     =>  $orders[0]->{purchaseordernumber}, );
138                 }
139         $template->param(
140         creationdate     => format_date( $basket->{creationdate} ),
141         authorisedbyname => $basket->{authorisedbyname},);
142   }else{
143         my @datetoday = localtime();
144         my $date = (1900+$datetoday[5])."-".($datetoday[4]+1)."-". $datetoday[3];
145         $template->param(
146         creationdate     => format_date($date),
147         authorisedbyname => $loggedinuser,);
148   }
149 }else {    #modify order
150     $data   = GetSingleOrder($ordnum);
151     $biblionumber = $data->{'biblionumber'};
152     #get basketno and suppleirno. too!
153     my $data2 = GetBasket( $data->{'basketno'} );
154     $basketno     = $data->{'basketno'};
155     $booksellerid = $data2->{'booksellerid'};
156     $discount=$data->{'discount'};
157        $gstrate=$data->{'gst'} ;
158     $bookfundid =$data->{'bookfundid'};
159  my $aqbookfund=GetBookFund($data->{'bookfundid'});
160 $branch=$aqbookfund->{branchcode};
161 $template->param(       
162         purchaseordernumber     =>  $data->{purchaseordernumber},
163         creationdate     => format_date( $data2->{creationdate} ),
164         authorisedbyname => $data2->{authorisedbyname},);
165         
166 }
167
168
169
170 # get currencies (for exchange rates calcs if needed)
171 my @rates = GetCurrencies();
172 my $count = scalar @rates;
173
174 my @loop_currency = ();
175 for ( my $i = 0 ; $i < $count ; $i++ ) {
176     my %line;
177     $line{currency} = $rates[$i]->{'currency'};
178     $line{rate}     = $rates[$i]->{'rate'};
179     push @loop_currency, \%line;
180 }
181
182
183
184
185
186 # build branches list
187 my $branches = GetBranches;
188 my @branchloop;
189 foreach my $thisbranch ( sort keys %$branches ) {
190 my $selected=1 if $thisbranch eq $branch;
191      my %row = ( 
192         value      => $thisbranch,
193         branchname => $branches->{$thisbranch}->{'branchname'},
194         selected=>$selected ,
195     );
196     push @branchloop, \%row;
197 }
198 $template->param( branchloop => \@branchloop );
199
200 # build bookfund list
201
202 my $count2;
203 my @bookfund;
204 my @select_bookfund;
205 my %select_bookfunds;
206 my $selbookfund;
207 @bookfund = GetBookFunds($homebranch);
208 $count2 = scalar @bookfund;
209
210 for ( my $i = 0 ; $i < $count2 ; $i++ ) {
211     push @select_bookfund, $bookfund[$i]->{'bookfundid'};
212     $select_bookfunds{ $bookfund[$i]->{'bookfundid'} } =
213       $bookfund[$i]->{'bookfundname'};
214         if ($bookfund[$i]->{'bookfundid'} eq $bookfundid){
215         $selbookfund=1;
216         }
217 }
218 my $CGIbookfund = CGI::scrolling_list(
219     -name     => 'bookfundid',
220     -values   => \@select_bookfund,
221     -default  => $data->{'bookfundid'},
222     -labels   => \%select_bookfunds,
223     -size     => 1,
224     -selected =>$selbookfund,
225     -multiple => 0
226 );
227
228 my $bookfundname;
229
230 if ($close) {
231     $bookfundid   = $data->{'bookfundid'};
232     $bookfundname = $select_bookfunds{$bookfundid};
233 }
234
235 #Build sort lists
236 my $CGIsort1 = buildCGIsort( "Asort1", "sort1", $data->{'sort1'} );
237 if ($CGIsort1) {
238     $template->param( CGIsort1 => $CGIsort1 );
239 }
240 else {
241     $template->param( sort1 => $data->{'sort1'} );
242 }
243
244 my $CGIsort2 = buildCGIsort( "Asort2", "sort2", $data->{'sort2'} );
245 if ($CGIsort2) {
246     $template->param( CGIsort2 => $CGIsort2 );
247 }
248 else {
249     $template->param( sort2 => $data->{'sort2'} );
250 }
251
252 my $bibitemsexists;
253
254 #
255
256     $template->param( bibitemexists => "1" ) if $biblionumber;
257          my @bibitemloop;
258           my %line;
259         $line{isbn}             = $data->{'isbn'};
260         $line{itemtype}         = $data->{'itemtype'};
261         $line{volumeddesc}      = $data->{'volumeddesc'};
262         push( @bibitemloop, \%line );
263
264         $template->param( bibitemloop => \@bibitemloop );
265     
266
267
268 # fill template
269 $template->param(
270     close        => $close,
271     bookfundid   => $bookfundid,
272     bookfundname => $bookfundname
273   )
274   if ($close);
275
276 $template->param(
277     existing         => $biblionumber,
278     ordnum           => $ordnum,
279     basketno         => $basketno,
280     booksellerid     => $booksellerid,
281     suggestionid     => $suggestionid,
282     biblionumber           => $biblionumber,
283     itemtype         => $data->{'itemtype'},
284     discount         => $discount,
285     listincgst       => $booksellers[0]->{'listincgst'},
286     listprice        => $booksellers[0]->{'listprice'},
287     gstreg           => $booksellers[0]->{'gstreg'},
288     invoiceinc       => $booksellers[0]->{'invoiceincgst'},
289     invoicedisc      => $booksellers[0]->{'invoicedisc'},
290     nocalc           => $booksellers[0]->{'nocalc'},
291     name             => $booksellers[0]->{'name'},
292     currency         => $booksellers[0]->{'listprice'},
293     gstrate          =>$gstrate,
294     loop_currencies  => \@loop_currency,
295     orderexists      => ( $new eq 'yes' ) ? 0 : 1,
296     title            => $data->{'title'},
297     author           => $data->{'author'},
298     copyrightdate    => $data->{'copyrightdate'},
299     CGIbookfund      => $CGIbookfund,
300     isbn             => $data->{'isbn'},
301     seriestitle      => $data->{'seriestitle'},
302     quantity         => $data->{'quantity'},
303     listprice        => $data->{'listprice'},
304     rrp              => $data->{'rrp'},
305     invoice          => $data->{'booksellerinvoicenumber'},
306     ecost            => $data->{'ecost'},
307     total               =>$data->{'unitprice'}* $data->{'quantity'},
308   unitprice            => $data->{'unitprice'},
309  gst        => $data->{'ecost'}*$gstrate/100,
310     notes            => $data->{'notes'},
311     publishercode    => $data->{'publishercode'},
312 #     donation         => $donation
313 );
314
315 output_html_with_http_headers $input, $cookie, $template->output;