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; # get_template_and_user
124 use C4::Acquisition; # ModOrder
125 use C4::Suggestions; # ModStatus
126 use C4::Biblio; # AddBiblio TransformKohaToMarc
130 use C4::Log qw(logaction);
131 use Koha::Acquisition::Currencies;
132 use Koha::Acquisition::Orders;
133 use Koha::Acquisition::Baskets;
136 ### "-------------------- addorder.pl ----------"
138 # FIXME: This needs to do actual error checking and possibly return user to the same form,
139 # not just blindly call C4 functions and print a redirect.
141 my $input = CGI->new;
142 my $use_ACQ_framework = $input->param('use_ACQ_framework');
144 # Check if order total amount exceed allowed budget
145 my $confirm_budget_exceeding = $input->param('confirm_budget_exceeding');
146 unless($confirm_budget_exceeding) {
147 my $budget_id = $input->param('budget_id');
148 my $total = $input->param('total');
149 my $budget = GetBudget($budget_id);
150 my $budget_spent = GetBudgetSpent($budget_id);
151 my $budget_ordered = GetBudgetOrdered($budget_id);
152 my $budget_used = $budget_spent + $budget_ordered;
153 my $budget_remaining = $budget->{budget_amount} - $budget_used;
154 my $budget_encumbrance = $budget->{budget_amount} * $budget->{budget_encumb} / 100;
155 my $budget_expenditure = $budget->{budget_expend};
157 if ( $total > $budget_remaining
158 || ( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance)
159 || ( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure) )
161 my ($template, $loggedinuser, $cookie) = get_template_and_user({
162 template_name => "acqui/addorder.tt",
165 flagsrequired => {acquisition => 'order_manage'},
168 my $url = $input->referer();
169 unless ( defined $url ) {
170 my $basketno = $input->param('basketno');
171 $url = "/cgi-bin/koha/acqui/basket.pl?basketno=$basketno";
174 my $vars = $input->Vars;
176 foreach (keys %$vars) {
179 values => [$input->param($_)],
183 if( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance
184 && $total <= $budget_remaining)
187 encumbrance_exceeded => 1,
188 encumbrance => sprintf("%.2f", $budget->{'budget_encumb'}),
191 if( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure
192 && $total <= $budget_remaining )
194 my $currency = Koha::Acquisition::Currencies->get_active;
196 expenditure_exceeded => 1,
197 expenditure => sprintf("%.2f", $budget_expenditure),
198 currency => ($currency) ? $currency->symbol : '',
201 if($total > $budget_remaining){
202 $template->param(budget_exceeded => 1);
206 not_enough_budget => 1,
208 vars_loop => \@vars_loop,
210 output_html_with_http_headers $input, $cookie, $template->output;
215 # get_template_and_user used only to check auth & get user id
216 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
218 template_name => "acqui/booksellers.tt",
221 flagsrequired => { acquisition => 'order_manage' },
227 my $orderinfo = $input->Vars;
228 $orderinfo->{'list_price'} ||= 0;
229 $orderinfo->{'uncertainprice'} ||= 0;
230 $orderinfo->{subscriptionid} ||= undef;
232 my $user = $input->remote_user;
233 my $basketno = $$orderinfo{basketno};
234 my $basket = Koha::Acquisition::Baskets->find($basketno);
236 # create, modify or delete biblio
237 # create if $quantity>0 and $existing='no'
238 # modify if $quantity>0 and $existing='yes'
239 if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) {
240 #TODO:check to see if biblio exists
241 unless ( $$orderinfo{biblionumber} ) {
244 if ( $use_ACQ_framework ) {
245 my @tags = $input->multi_param('bib_tag');
246 my @subfields = $input->multi_param('bib_subfield');
247 my @field_values = $input->multi_param('bib_field_value');
248 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );
249 $record=MARC::Record::new_from_xml($xml, 'UTF-8');
251 #if it doesn't create it
252 $record = TransformKohaToMarc(
254 "biblio.title" => "$$orderinfo{title}",
255 "biblio.author" => $$orderinfo{author} ? $$orderinfo{author} : "",
256 "biblio.seriestitle" => $$orderinfo{series} ? $$orderinfo{series} : "",
257 "biblioitems.isbn" => $$orderinfo{isbn} ? $$orderinfo{isbn} : "",
258 "biblioitems.ean" => $$orderinfo{ean} ? $$orderinfo{ean} : "",
259 "biblioitems.publishercode" => $$orderinfo{publishercode} ? $$orderinfo{publishercode} : "",
260 "biblioitems.publicationyear" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
261 "biblio.copyrightdate" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
262 "biblioitems.itemtype" => $$orderinfo{itemtype} ? $$orderinfo{itemtype} : "",
263 "biblioitems.editionstatement"=> $$orderinfo{editionstatement} ? $$orderinfo{editionstatement} : "",
267 C4::Acquisition::FillWithDefaultValues( $record );
269 # create the record in catalogue, with framework ''
270 my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
272 $orderinfo->{biblionumber}=$biblionumber;
275 # change suggestion status if applicable
276 if ( $orderinfo->{suggestionid} ) {
279 suggestionid => $orderinfo->{suggestionid},
280 biblionumber => $orderinfo->{biblionumber},
286 $orderinfo->{unitprice} = $orderinfo->{ecost} if not defined $orderinfo->{unitprice} or $orderinfo->{unitprice} eq '';
288 $orderinfo = C4::Acquisition::populate_order_with_prices(
291 booksellerid => $orderinfo->{booksellerid},
296 # if we already have $ordernumber, then it's an ordermodif
297 my $order = Koha::Acquisition::Order->new($orderinfo);
298 if ( $orderinfo->{ordernumber} ) {
299 ModOrder($orderinfo);
301 else { # else, it's a new line
304 my $order_users_ids = $input->param('users_ids');
305 my @order_users = split( /:/, $order_users_ids );
306 ModOrderUsers( $order->ordernumber, @order_users );
308 # now, add items if applicable
309 if ($basket->effective_create_items eq 'ordering') {
311 my @tags = $input->multi_param('tag');
312 my @subfields = $input->multi_param('subfield');
313 my @field_values = $input->multi_param('field_value');
314 my @serials = $input->multi_param('serial');
315 my @itemid = $input->multi_param('itemid');
316 my @ind_tag = $input->multi_param('ind_tag');
317 my @indicator = $input->multi_param('indicator');
318 #Rebuilding ALL the data for items into a hash
319 # parting them on $itemid.
323 my $range=scalar(@itemid);
324 for (my $i=0; $i<$range; $i++){
325 unless ($itemhash{$itemid[$i]}){
328 push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
329 push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
330 push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
331 push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
332 push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
334 foreach my $item (keys %itemhash){
335 my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
336 $itemhash{$item}->{'subfields'},
337 $itemhash{$item}->{'field_values'},
338 $itemhash{$item}->{'indicator'},
339 $itemhash{$item}->{'ind_tag'},
341 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
342 my ($barcodefield,$barcodesubfield) = GetMarcFromKohaField('items.barcode');
343 next unless ( defined $barcodefield && defined $barcodesubfield );
344 my $barcode = $record->subfield($barcodefield,$barcodesubfield) || '';
345 my $aBpref = C4::Context->preference('autoBarcode');
346 if( $barcode eq '' && $aBpref ne 'OFF'){
348 if ( $aBpref eq 'hbyymmincr'){
349 my ($homebranchfield,$homebranchsubfield) = GetMarcFromKohaField('items.homebranch');
350 my $homebranch = $record->subfield($homebranchfield,$homebranchsubfield);
351 $barcodeobj = C4::Barcodes->new($aBpref, $homebranch);
353 $barcodeobj = C4::Barcodes->new($aBpref);
355 $barcode = $barcodeobj->value();
356 $record->field($barcodefield)->delete_subfield( code => $barcodesubfield);
357 $record->field($barcodefield)->add_subfields($barcodesubfield => $barcode);
359 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$$orderinfo{biblionumber});
360 $order->add_item($itemnumber);
366 if (C4::Context->preference("AcquisitionLog") && $basketno) {
367 my $modified = Koha::Acquisition::Baskets->find( $basketno );
372 to_json($modified->unblessed)
376 my $booksellerid=$$orderinfo{booksellerid};
377 if (my $import_batch_id=$$orderinfo{import_batch_id}) {
378 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid");
379 } elsif ( defined $orderinfo->{invoiceid} ) {
380 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=" . $orderinfo->{invoiceid});
382 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");