Bug 23185: Realistic POD for Koha::Objects->search
[srvgit] / Koha / EDI.pm
index 0e632c9..a692da1 100644 (file)
@@ -27,10 +27,10 @@ use Business::ISBN;
 use DateTime;
 use C4::Context;
 use Koha::Database;
+use Koha::DateUtils;
 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;
@@ -175,7 +175,7 @@ sub process_ordrsp {
                             ordernumber             => $ordernumber,
                             cancellationreason      => $reason,
                             orderstatus             => 'cancelled',
-                            datecancellationprinted => DateTime->now()->ymd(),
+                            datecancellationprinted => dt_from_string()->ymd(),
                         }
                     );
                 }
@@ -388,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 );
@@ -410,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 {
@@ -541,7 +570,7 @@ sub process_quote {
         for my $b (@added_baskets) {
             create_edi_order(
                 {
-
+                    ean      => $messages->[0]->buyer_ean,
                     basketno => $b,
                 }
             );
@@ -558,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');
@@ -594,7 +625,7 @@ sub quote_item {
     # database definitions should set some of these defaults but dont
     my $order_hash = {
         biblionumber       => $bib->{biblionumber},
-        entrydate          => DateTime->now( time_zone => 'local' )->ymd(),
+        entrydate          => dt_from_string()->ymd(),
         basketno           => $basketno,
         listprice          => $item->price,
         quantity           => $order_quantity,
@@ -687,9 +718,9 @@ sub quote_item {
 
             my $created = 0;
             while ( $created < $order_quantity ) {
-                my $itemnumber;
-                ( $bib->{biblionumber}, $bib->{biblioitemnumber}, $itemnumber )
-                  = AddItem( $item_hash, $bib->{biblionumber} );
+                $item_hash->{biblionumber} = $bib->{biblionumber};
+                my $item = Koha::Item->new( $item_hash );
+                my $itemnumber = $item->itemnumber;
                 $logger->trace("Added item:$itemnumber");
                 $schema->resultset('AqordersItem')->create(
                     {
@@ -778,9 +809,9 @@ sub quote_item {
                         $item_hash->{homebranch} = $new_item->{homebranch};
                     }
 
-                    my $itemnumber;
-                    ( undef, undef, $itemnumber ) =
-                      AddItem( $item_hash, $bib->{biblionumber} );
+                    $item_hash->{biblionumber} = $bib->{biblionumber};
+                    my $item = Koha::Item->new( $item_hash );
+                    my $itemnumber = $item->itemnumber;
                     $logger->trace("New item $itemnumber added");
                     $schema->resultset('AqordersItem')->create(
                         {
@@ -788,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;
@@ -810,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{},
@@ -829,9 +878,9 @@ sub quote_item {
                           $item->girfield( 'branch', $occurrence ),
                         homebranch => $item->girfield( 'branch', $occurrence ),
                     };
-                    my $itemnumber;
-                    ( undef, undef, $itemnumber ) =
-                      AddItem( $new_item, $bib->{biblionumber} );
+                    $new_item->{biblionumber} = $bib->{biblionumber};
+                    my $item = Koha::Item->new( $new_item );
+                    my $itemnumber = $item->itemnumber;
                     $logger->trace("New item $itemnumber added");
                     $schema->resultset('AqordersItem')->create(
                         {
@@ -839,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;
@@ -894,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( [] );
             }