Bug 23354: (follow-up) Update for bugs 23049 and 23805
[koha-ffzg.git] / Koha / EDI.pm
index fcf352c..0ab4658 100644 (file)
@@ -30,7 +30,7 @@ use Koha::Database;
 use C4::Acquisition qw( NewBasket CloseBasket ModOrder);
 use C4::Suggestions qw( ModSuggestion );
 use C4::Items qw(AddItem);
-use C4::Biblio qw( AddBiblio TransformKohaToMarc GetMarcBiblio );
+use C4::Biblio qw( AddBiblio TransformKohaToMarc GetMarcBiblio GetFrameworkCode GetMarcFromKohaField );
 use Koha::Edifact::Order;
 use Koha::Edifact;
 use Log::Log4perl;
@@ -81,6 +81,16 @@ sub create_edi_order {
     my $ean_obj =
       $schema->resultset('EdifactEan')->search($ean_search_keys)->single;
 
+    # If no branch specific each can be found, look for a default ean
+    unless ($ean_obj) {
+        $ean_obj = $schema->resultset('EdifactEan')->search(
+            {
+                ean        => $ean,
+                branchcode => undef,
+            }
+        )->single;
+    }
+
     my $dbh     = C4::Context->dbh;
     my $arr_ref = $dbh->selectcol_arrayref(
 'select id from edifact_messages where basketno = ? and message_type = \'QUOTE\'',
@@ -378,6 +388,23 @@ sub receipt_items {
         }
         push @{ $branch_map{$b} }, $item;
     }
+
+    # Handling for 'AcqItemSetSubfieldsWhenReceived'
+    my @affects;
+    my $biblionumber;
+    my $itemfield;
+    if ( C4::Context->preference('AcqCreateItem') eq 'ordering' ) {
+        @affects = split q{\|},
+          C4::Context->preference("AcqItemSetSubfieldsWhenReceived");
+        if (@affects) {
+            my $order = Koha::Acquisition::Orders->find($ordernumber);
+            $biblionumber = $order->biblionumber;
+            my $frameworkcode = GetFrameworkCode($biblionumber);
+            ($itemfield) = GetMarcFromKohaField( 'items.itemnumber',
+                $frameworkcode );
+        }
+    }
+
     my $gir_occurrence = 0;
     while ( $gir_occurrence < $quantity ) {
         my $branch = $inv_line->girfield( 'branch', $gir_occurrence );
@@ -400,6 +427,18 @@ sub receipt_items {
                 }
             }
 
+            # Handling for 'AcqItemSetSubfieldsWhenReceived'
+            if (@affects) {
+                my $item_marc = C4::Items::GetMarcItem( $biblionumber, $item->itemnumber );
+                for my $affect (@affects) {
+                    my ( $sf, $v ) = split q{=}, $affect, 2;
+                    foreach ( $item_marc->field($itemfield) ) {
+                        $_->update( $sf => $v );
+                    }
+                }
+                C4::Items::ModItemFromMarc( $item_marc, $biblionumber, $item->itemnumber );
+            }
+
             $item->update;
         }
         else {
@@ -531,7 +570,7 @@ sub process_quote {
         for my $b (@added_baskets) {
             create_edi_order(
                 {
-
+                    ean      => $messages->[0]->buyer_ean,
                     basketno => $b,
                 }
             );
@@ -548,6 +587,8 @@ sub quote_item {
     my $schema = Koha::Database->new()->schema();
     my $logger = Log::Log4perl->get_logger();
 
+    # $basketno is the return from AddBasket in the calling routine
+    # So this call should not fail unless that has
     my $basket = Koha::Acquisition::Baskets->find( $basketno );
     unless ( $basket ) {
         $logger->error('Skipping order creation no valid basketno');
@@ -591,7 +632,9 @@ sub quote_item {
         quantityreceived   => 0,
         order_vendornote   => q{},
         order_internalnote => $order_note,
-        rrp                => $item->price,
+        replacementprice   => $item->price,
+        rrp_tax_included   => $item->price,
+        rrp_tax_excluded   => $item->price,
         ecost => _discounted_price( $quote->vendor->discount, $item->price ),
         uncertainprice => 0,
         sort1          => q{},
@@ -776,6 +819,22 @@ sub quote_item {
                             itemnumber  => $itemnumber,
                         }
                     );
+
+                    my $lrp =
+                      $item->girfield( 'library_rotation_plan', $occurrence );
+                    if ($lrp) {
+                        my $rota =
+                          Koha::StockRotationRotas->find( { title => $lrp },
+                            { key => 'stockrotationrotas_title' } );
+                        if ($rota) {
+                            $rota->add_item($itemnumber);
+                            $logger->trace("Item added to rota $rota->id");
+                        }
+                        else {
+                            $logger->error(
+                                "No rota found matching $lrp in orderline");
+                        }
+                    }
                 }
 
                 ++$occurrence;
@@ -798,7 +857,9 @@ sub quote_item {
                     );
                 }
 
-                if ( $basket->effective_create_item eq 'ordering' ) {
+                # Do not use the basket level value as it is always NULL
+                # See calling subs call to AddBasket
+                if ( C4::Context->preference('AcqCreateItem') eq 'ordering' ) {
                     my $new_item = {
                         notforloan       => -1,
                         cn_sort          => q{},
@@ -827,6 +888,22 @@ sub quote_item {
                             itemnumber  => $itemnumber,
                         }
                     );
+
+                    my $lrp =
+                      $item->girfield( 'library_rotation_plan', $occurrence );
+                    if ($lrp) {
+                        my $rota =
+                          Koha::StockRotationRotas->find( { title => $lrp },
+                            { key => 'stockrotationrotas_title' } );
+                        if ($rota) {
+                            $rota->add_item($itemnumber);
+                            $logger->trace("Item added to rota $rota->id");
+                        }
+                        else {
+                            $logger->error(
+                                "No rota found matching $lrp in orderline");
+                        }
+                    }
                 }
 
                 ++$occurrence;
@@ -882,7 +959,7 @@ sub _check_for_existing_bib {
         $isbn =~ s/\-//xmsg;
         if ( $isbn =~ m/(\d{13})/xms ) {
             my $b_isbn = Business::ISBN->new($1);
-            if ( $b_isbn && $b_isbn->is_valid ) {
+            if ( $b_isbn && $b_isbn->is_valid && $b_isbn->as_isbn10 ) {
                 $search_isbn = $b_isbn->as_isbn10->as_string( [] );
             }