EDIReader : detect SAN vs. account number in buyer/seller
authorBill Erickson <berick@esilibrary.com>
Mon, 12 Nov 2012 14:35:53 +0000 (09:35 -0500)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Mon, 14 Jan 2013 22:10:30 +0000 (17:10 -0500)
NAD+BY+XXXXXXX::31B' -- SAN
NAD+BY+YYYYYYY::91' -- Account number

For invoices, try the SAN first followed by the account number to
determine the receiving org unit.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/EDI.pm
Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm

index 1fd0639..f41d29c 100644 (file)
@@ -585,31 +585,46 @@ sub create_acq_invoice_from_edi {
                                     # distinguish provider and shipper?
     $eg_inv->recv_method("EDI");
 
-    my $buyer_san = $invoice->{buyer_san};
 
-    if (not $buyer_san) {
-        $logger->error($log_prefix . "could not find buyer SAN in INVOIC");
-        return 0;
-    }
+    # some vendors encode the account number as the SAN.
+    # starting with the san value, then the account value, 
+    # treat each as a san, then an acct number until the first success
+    for my $buyer ( ($invoice->{buyer_san}, $invoice->{buyer_acct}) ) {
+        next unless $buyer;
 
-    # some vendors encode the SAN as "$SAN $vendcode"
-    $buyer_san =~ s/\s.*//g;
+        # some vendors encode the SAN as "$SAN $vendcode"
+        $buyer =~ s/\s.*//g;
 
-    # Find the matching org unit based on SAN via 'aoa' table.
-    my $addrs =
-        $e->search_actor_org_address({valid => "t", san => $buyer_san});
+        my $addr = $e->search_actor_org_address(
+            {valid => "t", san => $buyer})->[0];
 
-    if (not $addrs or not @$addrs) {
-        $logger->error(
-            $log_prefix . "couldn't find OU unit matching buyer SAN in INVOIC:".
-            $e->event
+        if ($addr) {
+
+            $eg_inv->receiver($addr->org_unit);
+            last;
+
+        } else {
+
+            my $acct = $e->search_acq_edi_account({vendacct => $buyer})->[0];
+
+            if ($acct) {
+                $eg_inv->receiver($acct->owner);
+                last;
+            }
+        }
+    }
+
+    if (!$eg_inv->receiver) {
+        $logger->error($log_prefix . 
+            sprintf("unable to determine buyer (org unit) in invoice; ".
+                "buyer_san=%s; buyer_acct=%s",
+                ($invoice->{buyer_san} || ''), 
+                ($invoice->{buyer_acct} || '')
+            )
         );
         return 0;
     }
 
-    # XXX Should we verify that this matches PO ordering agency later?
-    $eg_inv->receiver($addrs->[0]->org_unit);
-
     $eg_inv->inv_ident($invoice->{invoice_ident});
 
     if (!$eg_inv->inv_ident) {
index 18d46ff..d3b5697 100644 (file)
@@ -20,8 +20,10 @@ my $NEW_LIN_RE = '^LIN'; # starts a new line item
 
 my %edi_fields = (
     message_type    => qr/^UNH\+\d+\+(\S{6})/,
-    buyer_san       => qr/^NAD\+BY\+([^:]+)/,
-    vendor_san      => qr/^NAD\+SU\+([^:]+)/,
+    buyer_san       => qr/^NAD\+BY\+([^:]+)::31B/,
+    buyer_acct      => qr/^NAD\+BY\+([^:]+)::91/,
+    vendor_san      => qr/^NAD\+SU\+([^:]+)::31B/,
+    vendor_acct     => qr/^NAD\+SU\+([^:]+)::91/,
     purchase_order  => qr/^RFF\+ON:(\S+)/,
     invoice_ident   => qr/^BGM\+380\+([^\+]+)/,
     total_billed    => qr/^MOA\+86:(\d+)/