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>.
30 this script allows to add an order.
35 =item neworderempty.pl to add an order from an existing record or from nothing.
37 =item newordersuggestion.pl to add an order from an existing suggestion.
43 All of the cgi parameters below are related to the new order.
48 the number of this new order.
51 the number of this new basket
54 the bookseller the librarian has to pay.
59 the title of the record ordered.
62 the author of the record ordered.
64 =item C<copyrightdate>
65 the copyrightdate of the record ordered.
68 the ISBN of the record ordered.
73 the quantity to order.
76 the price of this order.
78 =item C<uncertainprice>
79 uncertain price, can't close basket until prices of all orders are known.
82 the branch where this order will be received.
90 budget_id used to pay this order.
92 =item C<sort1> & C<sort2>
107 the number of the invoice for this order.
109 =item C<publishercode>
111 =item C<suggestionid>
112 if it is an order from an existing suggestion : the id of this suggestion.
121 use CGI qw
( -utf8
);
122 use C4
::Auth
; # get_template_and_user
123 use C4
::Acquisition
; # ModOrder
124 use C4
::Suggestions
; # ModStatus
125 use C4
::Biblio
; # AddBiblio TransformKohaToMarc
129 use Koha
::Acquisition
::Currencies
;
130 use Koha
::Acquisition
::Orders
;
133 ### "-------------------- addorder.pl ----------"
135 # FIXME: This needs to do actual error checking and possibly return user to the same form,
136 # not just blindly call C4 functions and print a redirect.
139 my $use_ACQ_framework = $input->param('use_ACQ_framework');
141 # Check if order total amount exceed allowed budget
142 my $confirm_budget_exceeding = $input->param('confirm_budget_exceeding');
143 unless($confirm_budget_exceeding) {
144 my $budget_id = $input->param('budget_id');
145 my $total = $input->param('total');
146 my $budget = GetBudget
($budget_id);
147 my $budget_spent = GetBudgetSpent
($budget_id);
148 my $budget_ordered = GetBudgetOrdered
($budget_id);
149 my $budget_used = $budget_spent + $budget_ordered;
150 my $budget_remaining = $budget->{budget_amount
} - $budget_used;
151 my $budget_encumbrance = $budget->{budget_amount
} * $budget->{budget_encumb
} / 100;
152 my $budget_expenditure = $budget->{budget_expend
};
154 if ( $total > $budget_remaining
155 || ( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance)
156 || ( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure) )
158 my ($template, $loggedinuser, $cookie) = get_template_and_user
({
159 template_name
=> "acqui/addorder.tt",
162 flagsrequired
=> {acquisition
=> 'order_manage'},
165 my $url = $input->referer();
166 unless ( defined $url ) {
167 my $basketno = $input->param('basketno');
168 $url = "/cgi-bin/koha/acqui/basket.pl?basketno=$basketno";
171 my $vars = $input->Vars;
173 foreach (keys %$vars) {
176 values => [$input->param($_)],
180 if( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance
181 && $total <= $budget_remaining)
184 encumbrance_exceeded
=> 1,
185 encumbrance
=> sprintf("%.2f", $budget->{'budget_encumb'}),
188 if( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure
189 && $total <= $budget_remaining )
191 my $currency = Koha
::Acquisition
::Currencies
->get_active;
193 expenditure_exceeded
=> 1,
194 expenditure
=> sprintf("%.2f", $budget_expenditure),
195 currency
=> ($currency) ?
$currency->symbol : '',
198 if($total > $budget_remaining){
199 $template->param(budget_exceeded
=> 1);
203 not_enough_budget
=> 1,
205 vars_loop
=> \
@vars_loop,
207 output_html_with_http_headers
$input, $cookie, $template->output;
212 # get_template_and_user used only to check auth & get user id
213 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
215 template_name
=> "acqui/booksellers.tt",
218 flagsrequired
=> { acquisition
=> 'order_manage' },
224 my $orderinfo = $input->Vars;
225 $orderinfo->{'list_price'} ||= 0;
226 $orderinfo->{'uncertainprice'} ||= 0;
227 $orderinfo->{subscriptionid
} ||= undef;
229 my $user = $input->remote_user;
230 my $basketno = $$orderinfo{basketno
};
231 my $basket = Koha
::Acquisition
::Baskets
->find($basketno);
233 # create, modify or delete biblio
234 # create if $quantity>0 and $existing='no'
235 # modify if $quantity>0 and $existing='yes'
236 if ( $basket->{is_standing
} || $orderinfo->{quantity
} ne '0' ) {
237 #TODO:check to see if biblio exists
238 unless ( $$orderinfo{biblionumber
} ) {
241 if ( $use_ACQ_framework ) {
242 my @tags = $input->multi_param('bib_tag');
243 my @subfields = $input->multi_param('bib_subfield');
244 my @field_values = $input->multi_param('bib_field_value');
245 my $xml = TransformHtmlToXml
( \
@tags, \
@subfields, \
@field_values );
246 $record=MARC
::Record
::new_from_xml
($xml, 'UTF-8');
248 #if it doesn't create it
249 $record = TransformKohaToMarc
(
251 "biblio.title" => "$$orderinfo{title}",
252 "biblio.author" => $$orderinfo{author
} ?
$$orderinfo{author
} : "",
253 "biblio.seriestitle" => $$orderinfo{series
} ?
$$orderinfo{series
} : "",
254 "biblioitems.isbn" => $$orderinfo{isbn
} ?
$$orderinfo{isbn
} : "",
255 "biblioitems.ean" => $$orderinfo{ean
} ?
$$orderinfo{ean
} : "",
256 "biblioitems.publishercode" => $$orderinfo{publishercode
} ?
$$orderinfo{publishercode
} : "",
257 "biblioitems.publicationyear" => $$orderinfo{publicationyear
} ?
$$orderinfo{publicationyear
}: "",
258 "biblio.copyrightdate" => $$orderinfo{publicationyear
} ?
$$orderinfo{publicationyear
}: "",
259 "biblioitems.itemtype" => $$orderinfo{itemtype
} ?
$$orderinfo{itemtype
} : "",
260 "biblioitems.editionstatement"=> $$orderinfo{editionstatement
} ?
$$orderinfo{editionstatement
} : "",
264 C4
::Acquisition
::FillWithDefaultValues
( $record );
266 # create the record in catalogue, with framework ''
267 my ($biblionumber,$bibitemnum) = AddBiblio
($record,'');
269 $orderinfo->{biblionumber
}=$biblionumber;
272 # change suggestion status if applicable
273 if ( $orderinfo->{suggestionid
} ) {
276 suggestionid
=> $orderinfo->{suggestionid
},
277 biblionumber
=> $orderinfo->{biblionumber
},
283 $orderinfo->{unitprice
} = $orderinfo->{ecost
} if not defined $orderinfo->{unitprice
} or $orderinfo->{unitprice
} eq '';
285 $orderinfo = C4
::Acquisition
::populate_order_with_prices
(
288 booksellerid
=> $orderinfo->{booksellerid
},
293 # if we already have $ordernumber, then it's an ordermodif
294 my $order = Koha
::Acquisition
::Order
->new($orderinfo);
295 if ( $orderinfo->{ordernumber
} ) {
296 ModOrder
($orderinfo);
298 else { # else, it's a new line
301 my $order_users_ids = $input->param('users_ids');
302 my @order_users = split( /:/, $order_users_ids );
303 ModOrderUsers
( $order->ordernumber, @order_users );
305 # now, add items if applicable
306 if ($basket->effective_create_items eq 'ordering') {
308 my @tags = $input->multi_param('tag');
309 my @subfields = $input->multi_param('subfield');
310 my @field_values = $input->multi_param('field_value');
311 my @serials = $input->multi_param('serial');
312 my @itemid = $input->multi_param('itemid');
313 my @ind_tag = $input->multi_param('ind_tag');
314 my @indicator = $input->multi_param('indicator');
315 #Rebuilding ALL the data for items into a hash
316 # parting them on $itemid.
320 my $range=scalar(@itemid);
321 for (my $i=0; $i<$range; $i++){
322 unless ($itemhash{$itemid[$i]}){
325 push @
{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
326 push @
{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
327 push @
{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
328 push @
{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
329 push @
{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
331 foreach my $item (keys %itemhash){
332 my $xml = TransformHtmlToXml
( $itemhash{$item}->{'tags'},
333 $itemhash{$item}->{'subfields'},
334 $itemhash{$item}->{'field_values'},
335 $itemhash{$item}->{'indicator'},
336 $itemhash{$item}->{'ind_tag'},
338 my $record=MARC
::Record
::new_from_xml
($xml, 'UTF-8');
339 my ($barcodefield,$barcodesubfield) = GetMarcFromKohaField
('items.barcode');
340 next unless ( defined $barcodefield && defined $barcodesubfield );
341 my $barcode = $record->subfield($barcodefield,$barcodesubfield) || '';
342 my $aBpref = C4
::Context
->preference('autoBarcode');
343 if( $barcode eq '' && $aBpref ne 'OFF'){
345 if ( $aBpref eq 'hbyymmincr'){
346 my ($homebranchfield,$homebranchsubfield) = GetMarcFromKohaField
('items.homebranch');
347 my $homebranch = $record->subfield($homebranchfield,$homebranchsubfield);
348 $barcodeobj = C4
::Barcodes
->new($aBpref, $homebranch);
350 $barcodeobj = C4
::Barcodes
->new($aBpref);
352 $barcode = $barcodeobj->value();
353 $record->field($barcodefield)->delete_subfield( code
=> $barcodesubfield);
354 $record->field($barcodefield)->add_subfields($barcodesubfield => $barcode);
356 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc
($record,$$orderinfo{biblionumber
});
357 $order->add_item($itemnumber);
363 my $booksellerid=$$orderinfo{booksellerid
};
364 if (my $import_batch_id=$$orderinfo{import_batch_id
}) {
365 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid");
366 } elsif ( defined $orderinfo->{invoiceid
} ) {
367 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=" . $orderinfo->{invoiceid
});
369 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");