Bug 15685: Allow creation of items (AcqCreateItem) to be customizable per-basket
[koha.git] / acqui / addorder.pl
index 80f01c6..8f9d7e4 100755 (executable)
@@ -130,6 +130,7 @@ use C4::Budgets;
 use C4::Items;
 use C4::Output;
 use Koha::Acquisition::Currencies;
+use Koha::Acquisition::Orders;
 use C4::Barcodes;
 
 ### "-------------------- addorder.pl ----------"
@@ -224,7 +225,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 );
 
 # get CGI parameters
-my $orderinfo                                  = $input->Vars;
+my $orderinfo = $input->Vars;
 $orderinfo->{'list_price'}    ||=  0;
 $orderinfo->{'uncertainprice'} ||= 0;
 $orderinfo->{subscriptionid} ||= undef;
@@ -233,6 +234,8 @@ my $basketno=$$orderinfo{basketno};
 my $basket = GetBasket($basketno);
 
 my $user = $input->remote_user;
+my $basketno=$$orderinfo{basketno};
+my $basket = Koha::Acquisition::Baskets->find( $basketno );
 
 # create, modify or delete biblio
 # create if $quantity>0 and $existing='no'
@@ -268,6 +271,14 @@ if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) {
 
     $orderinfo->{unitprice} = $orderinfo->{ecost} if not defined $orderinfo->{unitprice} or $orderinfo->{unitprice} eq '';
 
+    $orderinfo = C4::Acquisition::populate_order_with_prices(
+        {
+            order        => $orderinfo,
+            booksellerid => $orderinfo->{booksellerid},
+            ordering     => 1,
+        }
+    );
+
     # if we already have $ordernumber, then it's an ordermodif
     my $order = Koha::Acquisition::Order->new($orderinfo);
     if ( $orderinfo->{ordernumber} ) {
@@ -278,11 +289,11 @@ if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) {
         ModOrderUsers( $orderinfo->{ordernumber}, @order_users );
     }
     else { # else, it's a new line
-        $order->insert;
+        $order->store;
     }
 
     # now, add items if applicable
-    if (C4::Context->preference('AcqCreateItem') eq 'ordering') {
+    if ($basket->effective_create_items eq 'ordering') {
 
         my @tags         = $input->multi_param('tag');
         my @subfields    = $input->multi_param('subfield');
@@ -316,7 +327,8 @@ if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) {
                                     'ITEM');
             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
             my ($barcodefield,$barcodesubfield) = GetMarcFromKohaField('items.barcode');
-            my $barcode = $record->subfield($barcodefield,$barcodesubfield);
+            next unless ( defined $barcodefield && defined $barcodesubfield );
+            my $barcode = $record->subfield($barcodefield,$barcodesubfield) || '';
             my $aBpref = C4::Context->preference('autoBarcode');
             if( $barcode eq '' && $aBpref ne 'OFF'){
                 my $barcodeobj;