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 );
123 use C4::Auth qw( get_template_and_user );
124 use C4::Acquisition qw( FillWithDefaultValues populate_order_with_prices ModOrder 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;
141 ### "-------------------- addorder.pl ----------"
143 # FIXME: This needs to do actual error checking and possibly return user to the same form,
144 # not just blindly call C4 functions and print a redirect.
146 my $input = CGI->new;
147 my $use_ACQ_framework = $input->param('use_ACQ_framework');
149 # Check if order total amount exceed allowed budget
150 my $confirm_budget_exceeding = $input->param('confirm_budget_exceeding');
151 unless($confirm_budget_exceeding) {
152 my $budget_id = $input->param('budget_id');
153 my $total = $input->param('total');
154 my $budget = GetBudget($budget_id);
155 my $budget_spent = GetBudgetSpent($budget_id);
156 my $budget_ordered = GetBudgetOrdered($budget_id);
157 my $budget_used = $budget_spent + $budget_ordered;
158 my $budget_remaining = $budget->{budget_amount} - $budget_used;
159 my $budget_encumbrance = $budget->{budget_amount} * $budget->{budget_encumb} / 100;
160 my $budget_expenditure = $budget->{budget_expend};
162 if ( $total > $budget_remaining
163 || ( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance)
164 || ( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure) )
166 my ($template, $loggedinuser, $cookie) = get_template_and_user({
167 template_name => "acqui/addorder.tt",
170 flagsrequired => {acquisition => 'order_manage'},
173 my $url = $input->referer();
174 unless ( defined $url ) {
175 my $basketno = $input->param('basketno');
176 $url = "/cgi-bin/koha/acqui/basket.pl?basketno=$basketno";
179 my $vars = $input->Vars;
181 foreach (keys %$vars) {
184 values => [$input->param($_)],
188 if( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance
189 && $total <= $budget_remaining)
192 encumbrance_exceeded => 1,
193 encumbrance => sprintf("%.2f", $budget->{'budget_encumb'}),
196 if( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure
197 && $total <= $budget_remaining )
199 my $currency = Koha::Acquisition::Currencies->get_active;
201 expenditure_exceeded => 1,
202 expenditure => sprintf("%.2f", $budget_expenditure),
203 currency => ($currency) ? $currency->symbol : '',
206 if($total > $budget_remaining){
207 $template->param(budget_exceeded => 1);
211 not_enough_budget => 1,
213 vars_loop => \@vars_loop,
215 output_html_with_http_headers $input, $cookie, $template->output;
220 # get_template_and_user used only to check auth & get user id
221 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
223 template_name => "acqui/booksellers.tt",
226 flagsrequired => { acquisition => 'order_manage' },
231 my $orderinfo = $input->Vars;
232 $orderinfo->{'list_price'} ||= 0;
233 $orderinfo->{'uncertainprice'} ||= 0;
234 $orderinfo->{subscriptionid} ||= undef;
236 my $user = $input->remote_user;
237 my $basketno = $$orderinfo{basketno};
238 my $basket = Koha::Acquisition::Baskets->find($basketno);
240 # create, modify or delete biblio
241 # create if $quantity>0 and $existing='no'
242 # modify if $quantity>0 and $existing='yes'
243 if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) {
244 #TODO:check to see if biblio exists
245 unless ( $$orderinfo{biblionumber} ) {
248 if ( $use_ACQ_framework ) {
249 my @tags = $input->multi_param('bib_tag');
250 my @subfields = $input->multi_param('bib_subfield');
251 my @field_values = $input->multi_param('bib_field_value');
252 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );
253 $record=MARC::Record::new_from_xml($xml, 'UTF-8');
255 #if it doesn't create it
256 $record = TransformKohaToMarc(
258 "biblio.title" => "$$orderinfo{title}",
259 "biblio.author" => $$orderinfo{author} ? $$orderinfo{author} : "",
260 "biblio.seriestitle" => $$orderinfo{series} ? $$orderinfo{series} : "",
261 "biblioitems.isbn" => $$orderinfo{isbn} ? $$orderinfo{isbn} : "",
262 "biblioitems.ean" => $$orderinfo{ean} ? $$orderinfo{ean} : "",
263 "biblioitems.publishercode" => $$orderinfo{publishercode} ? $$orderinfo{publishercode} : "",
264 "biblioitems.publicationyear" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
265 "biblio.copyrightdate" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
266 "biblioitems.itemtype" => $$orderinfo{itemtype} ? $$orderinfo{itemtype} : "",
267 "biblioitems.editionstatement"=> $$orderinfo{editionstatement} ? $$orderinfo{editionstatement} : "",
271 C4::Acquisition::FillWithDefaultValues( $record );
273 # create the record in catalogue, with framework ''
274 my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
276 $orderinfo->{biblionumber}=$biblionumber;
279 # change suggestion status if applicable
280 if ( $orderinfo->{suggestionid} ) {
283 suggestionid => $orderinfo->{suggestionid},
284 biblionumber => $orderinfo->{biblionumber},
290 $orderinfo->{unitprice} = $orderinfo->{ecost} if not defined $orderinfo->{unitprice} or $orderinfo->{unitprice} eq '';
292 $orderinfo = C4::Acquisition::populate_order_with_prices(
295 booksellerid => $orderinfo->{booksellerid},
300 # if we already have $ordernumber, then it's an ordermodif
301 my $order = Koha::Acquisition::Order->new($orderinfo);
302 if ( $orderinfo->{ordernumber} ) {
303 ModOrder($orderinfo);
305 else { # else, it's a new line
308 my $order_users_ids = $input->param('users_ids');
309 my @order_users = split( /:/, $order_users_ids );
310 ModOrderUsers( $order->ordernumber, @order_users );
312 # now, add items if applicable
313 if ($basket->effective_create_items eq 'ordering') {
315 my @tags = $input->multi_param('tag');
316 my @subfields = $input->multi_param('subfield');
317 my @field_values = $input->multi_param('field_value');
318 my @serials = $input->multi_param('serial');
319 my @itemid = $input->multi_param('itemid');
320 my @ind_tag = $input->multi_param('ind_tag');
321 my @indicator = $input->multi_param('indicator');
322 #Rebuilding ALL the data for items into a hash
323 # parting them on $itemid.
327 my $range=scalar(@itemid);
328 for (my $i=0; $i<$range; $i++){
329 unless ($itemhash{$itemid[$i]}){
332 push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
333 push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
334 push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
335 push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
336 push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
338 foreach my $item (keys %itemhash){
339 my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
340 $itemhash{$item}->{'subfields'},
341 $itemhash{$item}->{'field_values'},
342 $itemhash{$item}->{'indicator'},
343 $itemhash{$item}->{'ind_tag'},
345 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
346 my ($barcodefield,$barcodesubfield) = GetMarcFromKohaField('items.barcode');
347 next unless ( defined $barcodefield && defined $barcodesubfield );
348 my $barcode = $record->subfield($barcodefield,$barcodesubfield) || '';
349 my $aBpref = C4::Context->preference('autoBarcode');
350 if( $barcode eq '' && $aBpref ne 'OFF'){
352 if ( $aBpref eq 'hbyymmincr'){
353 my ($homebranchfield,$homebranchsubfield) = GetMarcFromKohaField('items.homebranch');
354 my $homebranch = $record->subfield($homebranchfield,$homebranchsubfield);
355 $barcodeobj = C4::Barcodes->new($aBpref, $homebranch);
357 $barcodeobj = C4::Barcodes->new($aBpref);
359 $barcode = $barcodeobj->value();
360 $record->field($barcodefield)->delete_subfield( code => $barcodesubfield);
361 $record->field($barcodefield)->add_subfields($barcodesubfield => $barcode);
363 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$$orderinfo{biblionumber});
364 $order->add_item($itemnumber);
370 if (C4::Context->preference("AcquisitionLog") && $basketno) {
371 my $modified = Koha::Acquisition::Baskets->find( $basketno );
376 to_json($modified->unblessed)
380 my $booksellerid=$$orderinfo{booksellerid};
381 if (my $import_batch_id=$$orderinfo{import_batch_id}) {
382 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid");
383 } elsif ( defined $orderinfo->{invoiceid} ) {
384 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=" . $orderinfo->{invoiceid});
386 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");