LP1952931 ASN shipment notifiation Perl Test
authorBill Erickson <berickxx@gmail.com>
Mon, 6 Dec 2021 16:01:23 +0000 (11:01 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 12 May 2022 14:31:25 +0000 (10:31 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/EDI.pm
Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm
Open-ILS/src/perlmods/live_t/35-acq-asn-edi.t
Open-ILS/src/sql/Pg/200.schema.acq.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.acq-asn.sql

index c64afba..b319d80 100644 (file)
@@ -9540,6 +9540,7 @@ SELECT  usr,
                        <field reporter:label="Provider" name="provider" reporter:datatype="link"/>
                        <field reporter:label="Shipper" name="shipper" reporter:datatype="link"/>
                        <field reporter:label="Receive Date" name="recv_date" reporter:datatype="timestamp" />
+                       <field reporter:label="Receive Method" name="recv_method" reporter:datatype="link" />
                        <field reporter:label="Container Barcode" name="container_code" reporter:datatype="text" />
                        <field reporter:label="Lading Number" name="lading_number" reporter:datatype="text" />
                        <field reporter:label="Note" name="note" reporter:datatype="text" />
@@ -9550,6 +9551,7 @@ SELECT  usr,
                        <link field="receiver" reltype="has_a" key="id" map="" class="aou"/>
                        <link field="provider" reltype="has_a" key="id" map="" class="acqpro"/>
                        <link field="shipper" reltype="has_a" key="id" map="" class="acqpro"/>
+                       <link field="recv_method" reltype="has_a" key="code" map="" class="acqim"/>
                        <link field="entries" reltype="has_many" key="shipment_notification" map="" class="acqsne"/>
                </links>
                <permacrud xmlns="http://open-ils.org/spec/opensrf/IDL/permacrud/v1">
@@ -9571,7 +9573,7 @@ SELECT  usr,
                        <field reporter:label="ID" name="id" reporter:datatype="id"/>
                        <field reporter:label="Shipment Notification" name="shipment_notification" reporter:datatype="link" />
                        <field reporter:label="Line Item" name="lineitem" reporter:datatype="link"/>
-                       <field reporter:label="Item Count" name="inv_item_count" reporter:datatype="int" />
+                       <field reporter:label="Item Count" name="item_count" reporter:datatype="int" />
                </fields>
                <links>
                        <link field="shipment_notification" reltype="has_a" key="id" map="" class="acqinv"/>
index 9ff2681..c5e9dbc 100644 (file)
@@ -22,6 +22,7 @@ my $U = 'OpenILS::Application::AppUtils';
 use OpenILS::Utils::EDIReader;
 
 use Data::Dumper;
+$Data::Dumper::Indent = 0;
 our $verbose = 0;
 
 sub new {
@@ -1151,6 +1152,8 @@ sub create_shipment_notification_from_edi {
     my ($class, $msg_data, $provider_id, $edi_message) = @_;
     # $msg_data is O::U::EDIReader hash
 
+    $logger->info("ASN: " . Dumper($msg_data));
+
     my $e = new_editor();
 
     my $eg_asn = Fieldmapper::acq::shipment_notification->new;
@@ -1235,7 +1238,10 @@ sub extract_shipment_notification_entries {
                 from => {
                     jub => {
                         acqlia => {
-                            filter => {order_ident => 't', value => $ident}
+                            filter => {
+                                order_ident => 't', 
+                                attr_value => $ident
+                            }
                         }
                     }
                 },
@@ -1259,7 +1265,7 @@ sub extract_shipment_notification_entries {
         my $entry = Fieldmapper::acq::shipment_notification_entry->new;
 
         $entry->lineitem($li_id);
-        $entry->item_count($quant);
+        $entry->item_count($quantity);
 
         push(@entries, $entry);
     }
index 076667a..bf8b7c3 100644 (file)
@@ -32,7 +32,7 @@ my %edi_fields = (
     invoice_ident   => qr/^BGM\+380\+([^\+]+)/,
     total_billed    => qr/^MOA\+86:([^:]+)/,
     invoice_date    => qr/^DTM\+137:([^:]+)/, # This is really "messge date"
-    package_code    => qr/^GIN\+BJ\+([^:]+)/,
+    container_code  => qr/^GIN\+BJ\+([^:]+)/,
     lading_number   => qr/^RFF\+BM:([^:]+)/
 );
 
index 19109f7..de509dd 100755 (executable)
@@ -1,8 +1,11 @@
 #!/usr/bin/perl
 use strict; use warnings;
+use Data::Dumper;
 use OpenILS::Utils::TestUtils;
 use OpenILS::Utils::CStoreEditor (':funcs');
 use OpenILS::Utils::Fieldmapper;
+use OpenILS::Application::Acq::EDI;
+$Data::Dumper::Indent = 0;
 
 use Test::More tests => 5;
 
@@ -10,16 +13,38 @@ diag("Tests EDI Shipment Notifications");
 
 use constant {
     BR1_ID => 4,
-    BIB_ID => 248,
+    BR1_ADDR_ID => 4,
+    BR1_SAN => 1234567,
+    PROVIDER_SAN => 7654321,
     PROVIDER_ID => 2,
+    BIB_ID => 248,
+    LOCATION_ID => 1,
+    FUND_ID => 1,
+    ADMIN_ID => 1,
     ADMIN_USER => 'admin',
     ADMIN_PASS => 'demo123'
 };
 
+# Stub MARC with an ISBN as an order identifier
+my $LI_MARC = <<MARC;
+<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://www.loc.gov/MARC21/slim 
+  http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd" 
+  xmlns="http://www.loc.gov/MARC21/slim">
+  <leader>         a              </leader>
+  <datafield tag="020" ind1=" " ind2=" "> 
+    <subfield code="a">9780307887436</subfield>
+  </datafield>
+</record>
+MARC
+
 my $U = 'OpenILS::Application::AppUtils';
 my $script = OpenILS::Utils::TestUtils->new();
 $script->bootstrap;
 
+my $po_id;
+my $li_id;
+my $edi_account;
 my $e = new_editor;
 $e->init;
 
@@ -31,18 +56,139 @@ $script->authenticate({
 
 BAIL_OUT('Failed to Login') unless $script->authtoken;
 
+sub main {
+    $e->xact_begin;
+    create_seed_data();
+    create_po();
+    BAIL_OUT("Failed to commit transaction") unless $e->commit;
+    process_asn();
+}
 
-sub create_po {
+
+sub create_seed_data {
+
+    my $addr = $e->retrieve_actor_org_address(BR1_ADDR_ID);
+    $addr->san(BR1_SAN);
+
+    BAIL_OUT("Could not apply SAN to BR1 " . Dumper($e->die_event))
+        unless $e->update_actor_org_address($addr);
     
-    my $li = Fieldmapper::acq::lineitem->new;
-    $li->eg_bib_id(BIB_ID);
-    $li->marc('<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd" xmlns="http://www.loc.gov/MARC21/slim"><leader>         a              </leader><datafield tag="020" ind1=" " ind2=" "><subfield code="a">9780307887436</subfield></record>');
-    $li->
+    $edi_account = Fieldmapper::acq::edi_account->new;
+    $edi_account->owner(BR1_ID);
+    $edi_account->provider(PROVIDER_ID);
+    $edi_account->host("example.org");
+    $edi_account->label("ASN TEST");
+    $edi_account->use_attrs('f'); # doesn't matter here
+
+    BAIL_OUT("Could not create EDI account " . Dumper($e->die_event))
+        unless $e->create_acq_edi_account($edi_account);
+}
+
+sub create_po {
 
     my $po = Fieldmapper::acq::purchase_order->new;
     $po->ordering_agency(BR1_ID);
     $po->provider(PROVIDER_ID);
     $po->name("ASN-Test");
+
+    my $resp = $U->simplereq('open-ils.acq', 
+        'open-ils.acq.purchase_order.create', $script->authtoken, $po);
+
+    BAIL_OUT("Failed to create PO: " . Dumper($resp)) if $U->is_event($resp);
+
+    $po_id = $resp->{purchase_order}->id;
+
+    ok($po_id, "Created Purchase Order");
+    
+    my $li = Fieldmapper::acq::lineitem->new;
+    $li->purchase_order($po_id);
+    $li->eg_bib_id(BIB_ID);
+    $li->marc($LI_MARC);
+    $li->creator(ADMIN_ID);
+    $li->editor(ADMIN_ID);
+    $li->selector(ADMIN_ID);
+    $li->provider(PROVIDER_ID);
+    $li->estimated_unit_price('25.00');
+
+    $li_id = $U->simplereq('open-ils.acq',
+        'open-ils.acq.lineitem.create', $script->authtoken, $li);
+
+    BAIL_OUT("Failed to create Lineitem: " . Dumper($li_id)) if $U->is_event($li_id);
+
+    ok($li_id, "Created Lineitem");
+
+    my $lid = Fieldmapper::acq::lineitem_detail->new;
+    $lid->isnew(1);
+    $lid->lineitem($li_id);
+    $lid->fund(FUND_ID);
+    $lid->owning_lib(BR1_ID);
+    $lid->location(LOCATION_ID);
+
+    $resp = $U->simplereq('open-ils.acq',
+        'open-ils.acq.lineitem_detail.cud.batch', $script->authtoken, [$lid]);
+
+    BAIL_OUT("Failed to create Lineitem Detail: " . Dumper($resp)) if $U->is_event($resp);
+
+    ok($resp->{lid} == 1, 'Created a lineitem detail');
+
+    my $attr = $e->search_acq_lineitem_attr({
+        lineitem => $li_id, 
+        attr_name => 'isbn',
+        attr_type => 'lineitem_marc_attr_definition'
+    })->[0];
+
+    BAIL_OUT("Lineitem creation did not create an ISBN attribute")
+        unless $attr;
+
+    $attr->order_ident('t');
+
+    BAIL_OUT("Failed apply order_ident to ISBN attr: " . Dumper($e->die_event))
+        unless $e->update_acq_lineitem_attr($attr);
+}
+
+sub process_asn {
+
+    my $ASN = <<ASN;
+UNA:+.?'
+UNB+UNOC:3+7654321:31B+1234567:31B+211130:0825+99'
+UNG+DESADV+7654321:31B+1234567:31B+211130:0825+94+UN+D:96A:UN'
+UNH+193+DESADV:D:96A:UN'
+BGM+351+MOM9681366+9'
+DTM+137:20211130:102'
+DTM+11:20211130:102'
+DTM+132:20211207:102'
+RFF+BM:2036362399'
+NAD+SU+7654321::9'
+NAD+BY+1234567 0011::9'
+NAD+DP+1234567 0011::9'
+CPS+1'
+PAC+1+5'
+GIN+BJ+00016921002621109648'
+LIN+00001++9780307887436:EN'
+QTY+12:1'
+RFF+ON:$po_id'
+CNT+2:1'
+UNT+17+193'
+UNE+1+94'
+UNZ+1+99'
+ASN
+
+    my $in = OpenILS::Application::Acq::EDI->process_retrieval(
+        $ASN, "remote-file-name",
+        OpenILS::Application::Acq::EDI->remote_account($edi_account),
+        $edi_account
+    );
+
+    my $notification = $e->search_acq_shipment_notification([
+        {id => {'<>' => undef}},
+        {flesh => 1, flesh_fields => {acqsn => ['entries']}}
+    ])->[0];
+
+    ok($notification, 'Created a notification');
+
+    ok($notification->entries->[0]->lineitem eq $li_id, 
+        "Created notification for lineitem $li_id");
 }
 
+main();
 
index e0ca2bd..bb6b740 100644 (file)
@@ -805,7 +805,8 @@ CREATE TABLE acq.edi_message (
                                                                             'ORDRSP',
                                                                             'INVOIC',
                                                                             'OSTENQ',
-                                                                            'OSTRPT'
+                                                                            'OSTRPT',
+                                         'DESADV'
                                                                         ))
 );
 CREATE INDEX edi_message_account_status_idx ON acq.edi_message (account,status);
index c445890..7b92e1b 100644 (file)
@@ -8,6 +8,7 @@ CREATE TABLE acq.shipment_notification (
     provider        INT         NOT NULL REFERENCES acq.provider (id),
     shipper         INT         NOT NULL REFERENCES acq.provider (id),
     recv_date       TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+    recv_method     TEXT        NOT NULL REFERENCES acq.invoice_method (code) DEFAULT 'EDI',
     container_code  TEXT        NOT NULL, -- vendor-supplied super-barcode
     lading_number   TEXT,       -- informational
     note            TEXT,
@@ -18,11 +19,28 @@ CREATE INDEX acq_asn_container_code_idx ON acq.shipment_notification (container_
 
 CREATE TABLE acq.shipment_notification_entry (
     id                      SERIAL  PRIMARY KEY,
-    shipment_notification   INT NOT NULL REFERENCES acq.invoice (id) ON DELETE CASCADE,
-    lineitem                INT REFERENCES acq.lineitem (id) ON UPDATE CASCADE ON DELETE SET NULL,
+    shipment_notification   INT NOT NULL REFERENCES acq.shipment_notification (id) 
+                            ON DELETE CASCADE,
+    lineitem                INT REFERENCES acq.lineitem (id) 
+                            ON UPDATE CASCADE ON DELETE SET NULL,
     item_count              INT NOT NULL -- How many items the provider shipped
 );
 
+/* TODO alter valid_message_type constraint */
+
+ALTER TABLE acq.edi_message DROP CONSTRAINT valid_message_type;
+ALTER TABLE acq.edi_message ADD CONSTRAINT valid_message_type 
+CHECK (
+    message_type IN (
+        'ORDERS',                                                             
+        'ORDRSP',                                                             
+        'INVOIC',                                                             
+        'OSTENQ',                                                             
+        'OSTRPT',                                                             
+        'DESADV'                                                              
+    )
+);
+
 COMMIT;
 
 /* UNDO