Bug 32030: Link external packages with agreements
[koha-ffzg.git] / Koha / EDI.pm
index 14f61c7..954d32e 100644 (file)
@@ -21,28 +21,45 @@ use strict;
 use warnings;
 use base qw(Exporter);
 use utf8;
-use Carp;
+use Carp qw( carp );
 use English qw{ -no_match_vars };
 use Business::ISBN;
 use DateTime;
 use C4::Context;
 use Koha::Database;
-use Koha::DateUtils;
-use C4::Acquisition qw( NewBasket ModOrder);
+use Koha::DateUtils qw( dt_from_string );
+use C4::Acquisition qw( ModOrder NewBasket );
 use C4::Suggestions qw( ModSuggestion );
-use C4::Biblio qw( AddBiblio TransformKohaToMarc GetMarcBiblio GetFrameworkCode GetMarcFromKohaField );
+use C4::Biblio qw(
+    AddBiblio
+    GetFrameworkCode
+    GetMarcFromKohaField
+    TransformKohaToMarc
+);
 use Koha::Edifact::Order;
 use Koha::Edifact;
-use C4::Log qw(logaction);
+use C4::Log qw( logaction );
 use Log::Log4perl;
-use Text::Unidecode;
+use Text::Unidecode qw( unidecode );
+use Koha::Plugins; # Adds plugin dirs to @INC
 use Koha::Plugins::Handler;
 use Koha::Acquisition::Baskets;
 use Koha::Acquisition::Booksellers;
 
 our $VERSION = 1.1;
-our @EXPORT_OK =
-  qw( process_quote process_invoice process_ordrsp create_edi_order get_edifact_ean );
+
+our (@ISA, @EXPORT_OK);
+BEGIN {
+    require Exporter;
+    @ISA = qw(Exporter);
+    @EXPORT_OK = qw(
+      process_quote
+      process_invoice
+      process_ordrsp
+      create_edi_order
+      get_edifact_ean
+    );
+};
 
 sub create_edi_order {
     my $parameters = shift;
@@ -216,6 +233,7 @@ sub process_invoice {
 
     # Plugin has its own invoice processor, only run it and not the standard invoice processor below
     if ( $plugin_class ) {
+        eval "require $plugin_class"; # Import the class, eval is needed because requiring a string doesn't work like requiring a bareword
         my $plugin = $plugin_class->new();
         if ( $plugin->can('edifact_process_invoice') ) {
             Koha::Plugins::Handler->run(
@@ -294,12 +312,22 @@ sub process_invoice {
 
             foreach my $line ( @{$lines} ) {
                 my $ordernumber = $line->ordernumber;
+                if (!$ordernumber ) {
+                   $logger->trace( "Skipping invoice line, no associated ordernumber" );
+                   next;
+                }
+
                 $logger->trace( "Receipting order:$ordernumber Qty: ",
                     $line->quantity );
 
                 my $order = $schema->resultset('Aqorder')->find($ordernumber);
+                if (my $bib = $order->biblionumber) {
+                    my $b = $bib->biblionumber;
+                    my $id = $line->item_number_id;
+                    $logger->trace("Updating bib:$b id:$id");
+                }
 
-      # ModReceiveOrder does not validate that $ordernumber exists validate here
+                # ModReceiveOrder does not validate that $ordernumber exists validate here
                 if ($order) {
 
                     # check suggestions
@@ -430,7 +458,7 @@ sub receipt_items {
                 "Cannot find aqorder item for $i :Order:$ordernumber");
             next;
         }
-        my $b = $item->homebranch->branchcode;
+        my $b = $item->get_column('homebranch');
         if ( !exists $branch_map{$b} ) {
             $branch_map{$b} = [];
         }
@@ -527,7 +555,7 @@ sub transfer_items {
     foreach my $ilink (@item_links) {
         my $ino      = $ilink->itemnumber;
         my $item     = $schema->resultset('Item')->find( $ilink->itemnumber );
-        my $i_branch = $item->homebranch;
+        my $i_branch = $item->get_column('homebranch');
         if ( exists $mapped_by_branch{$i_branch}
             && $mapped_by_branch{$i_branch} > 0 )
         {
@@ -623,7 +651,7 @@ sub process_quote {
             );
             Koha::Acquisition::Baskets->find($b)->close;
             # Log the approval
-            if (C4::Context->preference("AcqLog")) {
+            if (C4::Context->preference("AcquisitionLog")) {
                 my $approved = Koha::Acquisition::Baskets->find( $b );
                 logaction(
                     'ACQUISITIONS',
@@ -697,7 +725,7 @@ sub quote_item {
         quantity           => $order_quantity,
         quantityreceived   => 0,
         order_vendornote   => q{},
-        order_internalnote => $order_note,
+        order_internalnote => q{},
         replacementprice   => $price,
         rrp_tax_included   => $price,
         rrp_tax_excluded   => $price,
@@ -736,7 +764,9 @@ sub quote_item {
             $txt .= $si;
             ++$occ;
         }
-        $order_hash->{order_vendornote} = $txt;
+    }
+    if ($order_note) {
+        $order_hash->{order_vendornote} = $order_note;
     }
 
     if ( $item->internal_notes() ) {
@@ -851,8 +881,6 @@ sub quote_item {
                     my $new_item = {
                         itype =>
                           $item->girfield( 'stock_category', $occurrence ),
-                        location =>
-                          $item->girfield( 'collection_code', $occurrence ),
                         itemcallnumber =>
                           $item->girfield( 'shelfmark', $occurrence )
                           || $item->girfield( 'classification', $occurrence )
@@ -861,11 +889,15 @@ sub quote_item {
                           $item->girfield( 'branch', $occurrence ),
                         homebranch => $item->girfield( 'branch', $occurrence ),
                     };
+
+                    my $lsq_field = C4::Context->preference('EdifactLSQ');
+                    $new_item->{$lsq_field} = $item->girfield( 'sequence_code', $occurrence );
+
                     if ( $new_item->{itype} ) {
                         $item_hash->{itype} = $new_item->{itype};
                     }
-                    if ( $new_item->{location} ) {
-                        $item_hash->{location} = $new_item->{location};
+                    if ( $new_item->{$lsq_field} ) {
+                        $item_hash->{$lsq_field} = $new_item->{$lsq_field};
                     }
                     if ( $new_item->{itemcallnumber} ) {
                         $item_hash->{itemcallnumber} =
@@ -939,8 +971,6 @@ sub quote_item {
                         replacementprice => $price,
                         itype =>
                           $item->girfield( 'stock_category', $occurrence ),
-                        location =>
-                          $item->girfield( 'collection_code', $occurrence ),
                         itemcallnumber =>
                           $item->girfield( 'shelfmark', $occurrence )
                           || $item->girfield( 'classification', $occurrence )
@@ -949,6 +979,8 @@ sub quote_item {
                           $item->girfield( 'branch', $occurrence ),
                         homebranch => $item->girfield( 'branch', $occurrence ),
                     };
+                    my $lsq_field = C4::Context->preference('EdifactLSQ');
+                    $new_item->{$lsq_field} = $item->girfield( 'sequence_code', $occurrence );
                     $new_item->{biblionumber} = $bib->{biblionumber};
                     $new_item->{biblioitemnumber} = $bib->{biblioitemnumber};
                     my $kitem = Koha::Item->new( $new_item )->store;
@@ -1155,7 +1187,8 @@ sub _create_item_from_quote {
     $item_hash->{booksellerid} = $quote->vendor_id;
     $item_hash->{price}        = $item_hash->{replacementprice} = $item->price;
     $item_hash->{itype}        = $item->girfield('stock_category');
-    $item_hash->{location}     = $item->girfield('collection_code');
+    my $lsq_field = C4::Context->preference('EdifactLSQ');
+    $item_hash->{$lsq_field}     = $item->girfield('sequence_code');
 
     my $note = {};