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 JSON qw ( to_json encode_json );
123 use C4::Auth qw( get_template_and_user );
124 use C4::Acquisition qw( FillWithDefaultValues ModOrderUsers );
125 use C4::Suggestions qw( ModSuggestion );
132 use C4::Budgets qw( GetBudget GetBudgetSpent GetBudgetOrdered );
133 use C4::Items qw( AddItemFromMarc );
134 use C4::Output qw( output_html_with_http_headers );
135 use C4::Log qw( logaction );
136 use Koha::Acquisition::Currencies qw( get_active );
137 use Koha::Acquisition::Orders;
138 use Koha::Acquisition::Baskets;
140 use Koha::DateUtils qw( dt_from_string );
142 ### "-------------------- addorder.pl ----------"
144 # FIXME: This needs to do actual error checking and possibly return user to the same form,
145 # not just blindly call C4 functions and print a redirect.
147 my $input = CGI->new;
148 my $use_ACQ_framework = $input->param('use_ACQ_framework');
150 # Check if order total amount exceed allowed budget
151 my $confirm_budget_exceeding = $input->param('confirm_budget_exceeding');
152 unless($confirm_budget_exceeding) {
153 my $budget_id = $input->param('budget_id');
154 my $total = $input->param('total');
155 my $budget = GetBudget($budget_id);
156 my $budget_spent = GetBudgetSpent($budget_id);
157 my $budget_ordered = GetBudgetOrdered($budget_id);
158 my $budget_used = $budget_spent + $budget_ordered;
159 my $budget_remaining = $budget->{budget_amount} - $budget_used;
160 my $budget_encumbrance = $budget->{budget_amount} * $budget->{budget_encumb} / 100;
161 my $budget_expenditure = $budget->{budget_expend};
163 if ( $total > $budget_remaining
164 || ( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance)
165 || ( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure) )
167 my ($template, $loggedinuser, $cookie) = get_template_and_user({
168 template_name => "acqui/addorder.tt",
171 flagsrequired => {acquisition => 'order_manage'},
174 my $url = $input->referer();
175 unless ( defined $url ) {
176 my $basketno = $input->param('basketno');
177 $url = "/cgi-bin/koha/acqui/basket.pl?basketno=$basketno";
180 my $vars = $input->Vars;
182 foreach (keys %$vars) {
185 values => [$input->param($_)],
189 if( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance
190 && $total <= $budget_remaining)
193 encumbrance_exceeded => 1,
194 encumbrance => sprintf("%.2f", $budget->{'budget_encumb'}),
197 if( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure
198 && $total <= $budget_remaining )
200 my $currency = Koha::Acquisition::Currencies->get_active;
202 expenditure_exceeded => 1,
203 expenditure => sprintf("%.2f", $budget_expenditure),
204 currency => ($currency) ? $currency->symbol : '',
207 if($total > $budget_remaining){
208 $template->param(budget_exceeded => 1);
212 not_enough_budget => 1,
214 vars_loop => \@vars_loop,
216 output_html_with_http_headers $input, $cookie, $template->output;
221 # get_template_and_user used only to check auth & get user id
222 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
224 template_name => "acqui/booksellers.tt",
227 flagsrequired => { acquisition => 'order_manage' },
233 ordernumber => scalar $input->param('ordernumber'),
234 basketno => scalar $input->param('basketno'),
235 biblionumber => scalar $input->param('biblionumber'),
236 invoiceid => scalar $input->param('invoiceid'),
237 quantity => scalar $input->param('quantity'),
238 budget_id => scalar $input->param('budget_id'),
239 currency => scalar $input->param('currency'),
240 listprice => scalar $input->param('listprice'),
241 uncertainprice => scalar $input->param('uncertainprice'),
242 tax_rate_on_ordering => scalar $input->param('tax_rate'),
243 discount => scalar $input->param('discount'),
244 rrp => scalar $input->param('rrp'),
245 replacementprice => scalar $input->param('replacementprice'),
246 ecost => scalar $input->param('ecost'),
247 unitprice => scalar $input->param('unitprice'),
248 order_internalnote => scalar $input->param('order_internalnote'),
249 order_vendornote => scalar $input->param('order_vendornote'),
250 sort1 => scalar $input->param('sort1'),
251 sort2 => scalar $input->param('sort2'),
252 subscriptionid => scalar $input->param('subscriptionid'),
255 $orderinfo->{uncertainprice} ||= 0;
256 $orderinfo->{subscriptionid} ||= undef;
258 my $user = $input->remote_user;
259 my $basketno = $$orderinfo{basketno};
260 my $basket = Koha::Acquisition::Baskets->find($basketno);
262 # Order related fields we're going to log
263 my @log_order_fields = (
267 'unitprice_tax_excluded',
268 'unitprice_tax_included',
274 'ecost_tax_excluded',
275 'ecost_tax_included',
276 'tax_rate_on_ordering'
279 # create, modify or delete biblio
280 # create if $quantity>0 and $existing='no'
281 # modify if $quantity>0 and $existing='yes'
282 if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) {
283 #TODO:check to see if biblio exists
284 unless ( $$orderinfo{biblionumber} ) {
287 if ( $use_ACQ_framework ) {
288 my @tags = $input->multi_param('bib_tag');
289 my @subfields = $input->multi_param('bib_subfield');
290 my @field_values = $input->multi_param('bib_field_value');
291 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );
292 $record=MARC::Record::new_from_xml($xml, 'UTF-8');
294 #if it doesn't create it
295 $record = TransformKohaToMarc(
297 "biblio.title" => $input->param('title') || '',
298 "biblio.author" => $input->param('author') || '',
299 "biblio.seriestitle" => $input->param('series') || '',
300 "biblioitems.isbn" => $input->param('isbn') || '',
301 "biblioitems.ean" => $input->param('ean') || '',
302 "biblioitems.publishercode" => $input->param('publishercode') || '',
303 "biblioitems.publicationyear" => $input->param('publicationyear') || '',
304 "biblio.copyrightdate" => $input->param('publicationyear') || '',
305 "biblioitems.itemtype" => $input->param('itemtype') || '',
306 "biblioitems.editionstatement"=> $input->param('editionstatement') || '',
310 C4::Acquisition::FillWithDefaultValues( $record );
312 # create the record in catalogue, with framework ''
313 my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
315 $orderinfo->{biblionumber}=$biblionumber;
318 # change suggestion status if applicable
319 if ( my $suggestionid = $input->param('suggestionid') ) {
322 suggestionid => $suggestionid,
323 biblionumber => $orderinfo->{biblionumber},
329 $orderinfo->{unitprice} = $orderinfo->{ecost} if not defined $orderinfo->{unitprice} or $orderinfo->{unitprice} eq '';
330 $orderinfo->{estimated_delivery_date} = $orderinfo->{estimated_delivery_date} ? dt_from_string($orderinfo->{estimated_delivery_date}) : undef;
335 if ( $orderinfo->{ordernumber} ) {
336 $order = Koha::Acquisition::Orders->find($orderinfo->{ordernumber});
337 $order->set($orderinfo);
338 $log_action_name = 'MODIFY_ORDER';
340 $order = Koha::Acquisition::Order->new($orderinfo);
341 $log_action_name = 'CREATE_ORDER';
343 $order->populate_with_prices_for_ordering();
346 # Log the order creation
347 if (C4::Context->preference("AcquisitionLog")) {
349 foreach my $field(@log_order_fields) {
350 $infos->{$field} = $order->$field;
359 my $order_users_ids = $input->param('users_ids');
360 my @order_users = split( /:/, $order_users_ids );
361 ModOrderUsers( $order->ordernumber, @order_users );
363 # now, add items if applicable
364 if ($basket->effective_create_items eq 'ordering') {
366 my @tags = $input->multi_param('tag');
367 my @subfields = $input->multi_param('subfield');
368 my @field_values = $input->multi_param('field_value');
369 my @serials = $input->multi_param('serial');
370 my @itemid = $input->multi_param('itemid');
371 #Rebuilding ALL the data for items into a hash
372 # parting them on $itemid.
376 my $range=scalar(@itemid);
377 for (my $i=0; $i<$range; $i++){
378 unless ($itemhash{$itemid[$i]}){
381 push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
382 push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
383 push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
385 foreach my $item (keys %itemhash){
386 my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
387 $itemhash{$item}->{'subfields'},
388 $itemhash{$item}->{'field_values'},
392 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
393 my ($barcodefield,$barcodesubfield) = GetMarcFromKohaField('items.barcode');
394 next unless ( defined $barcodefield && defined $barcodesubfield );
395 my $barcode = $record->subfield($barcodefield,$barcodesubfield) || '';
396 my $aBpref = C4::Context->preference('autoBarcode');
397 if( $barcode eq '' && $aBpref ne 'OFF'){
399 if ( $aBpref eq 'hbyymmincr'){
400 my ($homebranchfield,$homebranchsubfield) = GetMarcFromKohaField('items.homebranch');
401 my $homebranch = $record->subfield($homebranchfield,$homebranchsubfield);
402 $barcodeobj = C4::Barcodes->new($aBpref, $homebranch);
404 $barcodeobj = C4::Barcodes->new($aBpref);
406 $barcode = $barcodeobj->value();
407 $record->field($barcodefield)->delete_subfield( code => $barcodesubfield);
408 $record->field($barcodefield)->add_subfields($barcodesubfield => $barcode);
410 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$$orderinfo{biblionumber});
411 $order->add_item($itemnumber);
417 if (C4::Context->preference("AcquisitionLog") && $basketno) {
418 my $modified = Koha::Acquisition::Baskets->find( $basketno );
423 to_json($modified->unblessed)
427 my $booksellerid=$$orderinfo{booksellerid};
428 if (my $import_batch_id = $input->param('import_batch_id')) {
429 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid");
430 } elsif ( defined $orderinfo->{invoiceid} ) {
431 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=" . $orderinfo->{invoiceid});
433 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");