Bug 17301 - Add callnumber to label-edit-batch.pl
[koha.git] / acqui / addorder.pl
blob5223b7fc04d334d0d078601b4430202690ad7ea2
1 #!/usr/bin/perl
3 #script to add an order into the system
4 #written 29/2/00 by chris@katipo.co.nz
6 # Copyright 2000-2002 Katipo Communications
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 =head1 NAME
26 addorder.pl
28 =head1 DESCRIPTION
30 this script allows to add an order.
31 It is called by :
33 =over
35 =item neworderbiblio.pl to add an order from nothing.
37 =item neworderempty.pl to add an order from an existing biblio.
39 =item newordersuggestion.pl to add an order from an existing suggestion.
41 =back
43 =head1 CGI PARAMETERS
45 All of the cgi parameters below are related to the new order.
47 =over
49 =item C<ordernumber>
50 the number of this new order.
52 =item C<basketno>
53 the number of this new basket
55 =item C<booksellerid>
56 the bookseller the librarian has to pay.
58 =item C<existing>
60 =item C<title>
61 the title of the record ordered.
63 =item C<author>
64 the author of the record ordered.
66 =item C<copyrightdate>
67 the copyrightdate of the record ordered.
69 =item C<ISBN>
70 the ISBN of the record ordered.
72 =item C<format>
74 =item C<quantity>
75 the quantity to order.
77 =item C<list_price>
78 the price of this order.
80 =item C<uncertainprice>
81 uncertain price, can't close basket until prices of all orders are known.
83 =item C<branch>
84 the branch where this order will be received.
86 =item C<series>
88 =item C<notes>
89 Notes on this basket.
91 =item C<budget_id>
92 budget_id used to pay this order.
94 =item C<sort1> & C<sort2>
96 =item C<rrp>
98 =item C<ecost>
100 =item C<GST>
102 =item C<budget>
104 =item C<cost>
106 =item C<sub>
108 =item C<invoice>
109 the number of the invoice for this order.
111 =item C<publishercode>
113 =item C<suggestionid>
114 if it is an order from an existing suggestion : the id of this suggestion.
116 =item C<donation>
118 =back
120 =cut
122 use strict;
123 use warnings;
124 use CGI qw ( -utf8 );
125 use C4::Auth; # get_template_and_user
126 use C4::Acquisition; # ModOrder
127 use C4::Suggestions; # ModStatus
128 use C4::Biblio; # AddBiblio TransformKohaToMarc
129 use C4::Budgets;
130 use C4::Items;
131 use C4::Output;
132 use Koha::Acquisition::Currencies;
133 use C4::Barcodes;
135 ### "-------------------- addorder.pl ----------"
137 # FIXME: This needs to do actual error checking and possibly return user to the same form,
138 # not just blindly call C4 functions and print a redirect.
140 my $input = new CGI;
142 # Check if order total amount exceed allowed budget
143 my $confirm_budget_exceeding = $input->param('confirm_budget_exceeding');
144 unless($confirm_budget_exceeding) {
145 my $budget_id = $input->param('budget_id');
146 my $total = $input->param('total');
147 my $budget = GetBudget($budget_id);
148 my $budget_spent = GetBudgetSpent($budget_id);
149 my $budget_ordered = GetBudgetOrdered($budget_id);
150 my $budget_used = $budget_spent + $budget_ordered;
151 my $budget_remaining = $budget->{budget_amount} - $budget_used;
152 my $budget_encumbrance = $budget->{budget_amount} * $budget->{budget_encumb} / 100;
153 my $budget_expenditure = $budget->{budget_expend};
155 if ( $total > $budget_remaining
156 || ( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance)
157 || ( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure) )
159 my ($template, $loggedinuser, $cookie) = get_template_and_user({
160 template_name => "acqui/addorder.tt",
161 query => $input,
162 type => "intranet",
163 authnotrequired => 0,
164 flagsrequired => {acquisition => 'order_manage'},
167 my $url = $input->referer();
168 unless ( defined $url ) {
169 my $basketno = $input->param('basketno');
170 $url = "/cgi-bin/koha/acqui/basket.pl?basketno=$basketno";
173 my $vars = $input->Vars;
174 my @vars_loop;
175 foreach (keys %$vars) {
176 push @vars_loop, {
177 name => $_,
178 values => [$input->param($_)],
182 if( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance
183 && $total <= $budget_remaining)
185 $template->param(
186 encumbrance_exceeded => 1,
187 encumbrance => sprintf("%.2f", $budget->{'budget_encumb'}),
190 if( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure
191 && $total <= $budget_remaining )
193 my $currency = Koha::Acquisition::Currencies->get_active;
194 $template->param(
195 expenditure_exceeded => 1,
196 expenditure => sprintf("%.2f", $budget_expenditure),
197 currency => ($currency) ? $currency->symbol : '',
200 if($total > $budget_remaining){
201 $template->param(budget_exceeded => 1);
204 $template->param(
205 not_enough_budget => 1,
206 referer => $url,
207 vars_loop => \@vars_loop,
209 output_html_with_http_headers $input, $cookie, $template->output;
210 exit;
214 # get_template_and_user used only to check auth & get user id
215 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
217 template_name => "acqui/booksellers.tt",
218 query => $input,
219 type => "intranet",
220 authnotrequired => 0,
221 flagsrequired => { acquisition => 'order_manage' },
222 debug => 1,
226 # get CGI parameters
227 my $orderinfo = $input->Vars;
228 $orderinfo->{'list_price'} ||= 0;
229 $orderinfo->{'uncertainprice'} ||= 0;
230 $orderinfo->{subscriptionid} ||= undef;
232 my $basketno=$$orderinfo{basketno};
233 my $basket = GetBasket($basketno);
235 my $user = $input->remote_user;
237 # create, modify or delete biblio
238 # create if $quantity>0 and $existing='no'
239 # modify if $quantity>0 and $existing='yes'
240 if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) {
241 #TODO:check to see if biblio exists
242 unless ( $$orderinfo{biblionumber} ) {
243 #if it doesn't create it
244 my $record = TransformKohaToMarc(
246 "biblio.title" => "$$orderinfo{title}",
247 "biblio.author" => $$orderinfo{author} ? $$orderinfo{author} : "",
248 "biblio.seriestitle" => $$orderinfo{series} ? $$orderinfo{series} : "",
249 "biblioitems.isbn" => $$orderinfo{isbn} ? $$orderinfo{isbn} : "",
250 "biblioitems.ean" => $$orderinfo{ean} ? $$orderinfo{ean} : "",
251 "biblioitems.publishercode" => $$orderinfo{publishercode} ? $$orderinfo{publishercode} : "",
252 "biblioitems.publicationyear" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
253 "biblio.copyrightdate" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
254 "biblioitems.itemtype" => $$orderinfo{itemtype} ? $$orderinfo{itemtype} : "",
255 "biblioitems.editionstatement"=> $$orderinfo{editionstatement} ? $$orderinfo{editionstatement} : "",
258 C4::Acquisition::FillWithDefaultValues( $record );
260 # create the record in catalogue, with framework ''
261 my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
262 # change suggestion status if applicable
263 if ($$orderinfo{suggestionid}) {
264 ModSuggestion( {suggestionid=>$$orderinfo{suggestionid}, STATUS=>'ORDERED', biblionumber=>$biblionumber} );
266 $orderinfo->{biblionumber}=$biblionumber;
269 $orderinfo->{unitprice} = $orderinfo->{ecost} if not defined $orderinfo->{unitprice} or $orderinfo->{unitprice} eq '';
271 # if we already have $ordernumber, then it's an ordermodif
272 my $order = Koha::Acquisition::Order->new($orderinfo);
273 if ( $orderinfo->{ordernumber} ) {
274 ModOrder($orderinfo);
275 my $order_users_ids = $input->param('users_ids');
276 my @order_users = split( /:/, $order_users_ids );
278 ModOrderUsers( $orderinfo->{ordernumber}, @order_users );
280 else { # else, it's a new line
281 $order->insert;
284 # now, add items if applicable
285 if (C4::Context->preference('AcqCreateItem') eq 'ordering') {
287 my @tags = $input->multi_param('tag');
288 my @subfields = $input->multi_param('subfield');
289 my @field_values = $input->multi_param('field_value');
290 my @serials = $input->multi_param('serial');
291 my @itemid = $input->multi_param('itemid');
292 my @ind_tag = $input->multi_param('ind_tag');
293 my @indicator = $input->multi_param('indicator');
294 #Rebuilding ALL the data for items into a hash
295 # parting them on $itemid.
297 my %itemhash;
298 my $countdistinct;
299 my $range=scalar(@itemid);
300 for (my $i=0; $i<$range; $i++){
301 unless ($itemhash{$itemid[$i]}){
302 $countdistinct++;
304 push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
305 push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
306 push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
307 push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
308 push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
310 foreach my $item (keys %itemhash){
311 my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
312 $itemhash{$item}->{'subfields'},
313 $itemhash{$item}->{'field_values'},
314 $itemhash{$item}->{'indicator'},
315 $itemhash{$item}->{'ind_tag'},
316 'ITEM');
317 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
318 my ($barcodefield,$barcodesubfield) = GetMarcFromKohaField('items.barcode');
319 my $barcode = $record->subfield($barcodefield,$barcodesubfield) || '';
320 my $aBpref = C4::Context->preference('autoBarcode');
321 if( $barcode eq '' && $aBpref ne 'OFF'){
322 my $barcodeobj;
323 if ( $aBpref eq 'hbyymmincr'){
324 my ($homebranchfield,$homebranchsubfield) = GetMarcFromKohaField('items.homebranch');
325 my $homebranch = $record->subfield($homebranchfield,$homebranchsubfield);
326 $barcodeobj = C4::Barcodes->new($aBpref, $homebranch);
327 } else {
328 $barcodeobj = C4::Barcodes->new($aBpref);
330 $barcode = $barcodeobj->value();
331 $record->field($barcodefield)->delete_subfield( code => $barcodesubfield);
332 $record->field($barcodefield)->add_subfields($barcodesubfield => $barcode);
334 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$$orderinfo{biblionumber});
335 $order->add_item($itemnumber);
341 my $booksellerid=$$orderinfo{booksellerid};
342 if (my $import_batch_id=$$orderinfo{import_batch_id}) {
343 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid");
344 } elsif ( defined $orderinfo->{invoiceid} ) {
345 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=" . $orderinfo->{invoiceid});
346 } else {
347 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");