LP#1373690 template continued
authorBill Erickson <berickxx@gmail.com>
Thu, 25 Sep 2014 20:14:48 +0000 (16:14 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 13 May 2015 22:41:42 +0000 (18:41 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/sql/Pg/upgrade/XXXX.data.acq-order-edi.sql

index 96bece6..1c5750a 100644 (file)
@@ -11,80 +11,83 @@ VALUES
 $$
 [%- USE date -%]
 [%
-    # extract some commonly used variables
-
-    VENDOR_SAN = target.provider.san;
-    VENDCODE = target.provider.edi_default.vendcode;
-    VENDACCT = target.provider.edi_default.vendacct;
-    ORG_UNIT_SAN = target.ordering_agency.mailing_address.san;
-
-    # set the vendor / provider
-
-    VENDOR_BT      = 0; # Baker & Taylor
-    VENDOR_INGRAM  = 0;
-    VENDOR_BRODART = 0;
-    VENDOR_MW_TAPE = 0; # Midwest Tape
-    VENDOR_RB      = 0; # Recorded Books
-    VENDOR_ULS     = 0; # ULS
-
-    IF    VENDOR_SAN == '1556150'; VENDOR_BT = 1;
-    ELSIF VENDOR_SAN == '1697684'; VENDOR_BRODART = 1;
-    ELSIF VENDOR_SAN == '1697978'; VENDOR_INGRAM = 1;
-    ELSIF VENDOR_SAN == '2549913'; VENDOR_MW_TAPE = 1;
-    ELSIF VENDOR_SAN == '1113984'; VENDOR_RB = 1;
-    ELSIF VENDOR_SAN == '1699342'; VENDOR_ULS = 1;
-    END;
-
-    # if true, pass the PO name as a secondary identifier
-    # RFF+LI:<name>/li_id
-    INC_PO_NAME = 0;
-    IF VENDOR_INGRAM;
-        INC_PO_NAME = 1;
-    END;
-
-    # GIR configuration --------------------------------------
-
-    INC_COPIES = 1; # copies on/off switch
-    INC_FUND = 0;
-    INC_CALLNUMBER = 0;
-    INC_ITEM_TYPE = 1;
-    INC_LOCATION = 0;
-    INC_COLLECTION_CODE = 1;
-    INC_OWNING_LIB = 1;
-    INC_QUANTITY = 1;
-    INC_COPY_ID = 0;
-
-    IF VENDOR_BT;
-        INC_CALLNUMBER = 1;
-    END;
-
-    IF VENDOR_BRODART;
-        INC_FUND = 1;
-    END;
-
-    IF VENDOR_MW_TAPE;
-        INC_FUND = 1;
-        INC_COLLECTION_CODE = 0;
-        INC_ITEM_TYPE = 0;
-    END;
-
-    # add one to this number for each lineitem segment and copy segment
-    SEGMENT_COUNT = 13; 
-
-    # END GIR configuration ---------------------------------
--%]
-[%- BLOCK big_block -%]
-UNA:+.? '
-UNB+UNOB:3+[% VENDCODE %]:31B+[% VENDOR_SAN %]:31B+140924:1351+1'
-UNH+1+ORDERS:D:96A:UN'
-BGM+220+98+9'
-DTM+137:[% date.format(date.now, '%Y%m%d') %]:102'
-[%- BUYER = VENDCODE; IF VENDOR_BT; BUYER = BUYER _ ' ' _ VENDACCT; END -%]
-NAD+BY+[% BUYER %]::91'
-NAD+SU+[% VENDOR_SAN %]::31B'
-NAD+SU+12::92'
-CUX+2:[% target.provider.currency_type %]:9'
-[% FOR li IN target.lineitems;
+# extract some commonly used variables
+
+VENDOR_SAN = target.provider.san;
+VENDCODE = target.provider.edi_default.vendcode;
+VENDACCT = target.provider.edi_default.vendacct;
+ORG_UNIT_SAN = target.ordering_agency.mailing_address.san;
+
+# set the vendor / provider
+
+VENDOR_BT      = 0; # Baker & Taylor
+VENDOR_INGRAM  = 0;
+VENDOR_BRODART = 0;
+VENDOR_MW_TAPE = 0; # Midwest Tape
+VENDOR_RB      = 0; # Recorded Books
+VENDOR_ULS     = 0; # ULS
+
+IF    VENDOR_SAN == '1556150'; VENDOR_BT = 1;
+ELSIF VENDOR_SAN == '1697684'; VENDOR_BRODART = 1;
+ELSIF VENDOR_SAN == '1697978'; VENDOR_INGRAM = 1;
+ELSIF VENDOR_SAN == '2549913'; VENDOR_MW_TAPE = 1;
+ELSIF VENDOR_SAN == '1113984'; VENDOR_RB = 1;
+ELSIF VENDOR_SAN == '1699342'; VENDOR_ULS = 1;
+END;
+
+# if true, pass the PO name as a secondary identifier
+# RFF+LI:<name>/li_id
+INC_PO_NAME = 0;
+IF VENDOR_INGRAM;
+    INC_PO_NAME = 1;
+END;
+
+# GIR configuration --------------------------------------
+
+INC_COPIES = 1; # copies on/off switch
+INC_FUND = 0;
+INC_CALLNUMBER = 0;
+INC_ITEM_TYPE = 1;
+INC_LOCATION = 0;
+INC_COLLECTION_CODE = 1;
+INC_OWNING_LIB = 1;
+INC_QUANTITY = 1;
+INC_COPY_ID = 0;
+
+IF VENDOR_BT;
+    INC_CALLNUMBER = 1;
+END;
+
+IF VENDOR_BRODART;
+    INC_FUND = 1;
+END;
+
+IF VENDOR_MW_TAPE;
+    INC_FUND = 1;
+    INC_COLLECTION_CODE = 0;
+    INC_ITEM_TYPE = 0;
+END;
+
+# END GIR configuration ---------------------------------
+
+BUYER_CODE = VENDACCT;
+IF VENDOR_BT; 
+    BUYER_CODE = ORG_UNIT_SAN _ ' ' _ VENDCODE;
+END;
+
+# add one to this number for each lineitem segment and copy segment
+SEGMENT_COUNT = 13; 
+
+# munge the lineitem / copy data for easier template integration
+
+LINEITEMS = [];
+FOR li IN target.lineitems;
+    SEGMENT_COUNT = SEGMENT_COUNT + 10; # needs verification
+
+    li_hash = {};
+    li_hash.id = li.id;
+
+    # extract the ISBN, etc.
     idval = '';
     idqual = 'EN'; # default ISBN/UPC/EAN-13
     ident_attr = helpers.get_li_order_ident(li.attributes);
@@ -101,111 +104,140 @@ CUX+2:[% target.provider.currency_type %]:9'
         idval = li.id;
     END;
 
-    # every lineitem has 10 segments, plus optional copy segments
-    SEGMENT_COUNT = SEGMENT_COUNT + 10;
-
--%]
-LIN+[% li.id %]++[% idval %]:[% idqual %]'
-PIA+5+[% idval %]:[% idqual %]'
-IMD+F+BTI+:::[% helpers.get_li_attr_edi('title',     '', li.attributes) %]'
-IMD+F+BPU+:::[% helpers.get_li_attr_edi('author',    '', li.attributes) %]'
-IMD+F+BPD+:::[% helpers.get_li_attr_edi('pubdate',   '', li.attributes) %]'
-IMD+F+BPH+:::[% helpers.get_li_attr_edi('pagination','', li.attributes) %]'
-QTY+21:[% li.lineitem_details.size %]'
-[%- IF INC_COPIES;
-    GIR_INDEX = 1;
-    compressed_copies = [];
-    FOR lid IN li.lineitem_details;
-        fund = lid.fund.code;
-        item_type = lid.circ_modifier;
-        callnumber = lid.cn_label;
-        owning_lib = lid.owning_lib.shortname;
-        location = lid.location;
-        collection_code = lid.collection_code;
-
-        # when we have real copy data, treat it as authoritative for some fields
-        acp = lid.eg_copy_id;
-        IF acp;
-            item_type = acp.circ_modifier;
-            callnumber = acp.call_number.label;
-            location = acp.location.name;
-        END ;
-
-
-        # collapse like copies into groups w/ quantity
-
-        found_match = 0;
-        IF !INC_COPY_ID; # INC_COPY_ID implies 1 copy per GIR
-            FOR copy IN compressed_copies;
-                IF  (fund == copy.fund OR (!fund AND !copy.fund)) AND
-                    (item_type == copy.item_type OR (!item_type AND !copy.item_type)) AND
-                    (callnumber == copy.callnumber OR (!callnumber AND !copy.callnumber)) AND
-                    (owning_lib == copy.owning_lib OR (!owning_lib AND !copy.owning_lib)) AND
-                    (location == copy.location OR (!location AND !copy.location)) AND
-                    (collection_code == copy.collection_code OR (!collection_code AND !copy.collection_code));
-
-                    copy.quantity = copy.quantity + 1;
-                    found_match = 1;
+    li_hash.idval = idval;
+    li_hash.idqual = idqual;
+    li_hash.title = helpers.get_li_attr_edi('title',     '', li.attributes);
+    li_hash.author = helpers.get_li_attr_edi('author',    '', li.attributes);
+    li_hash.pubdate = helpers.get_li_attr_edi('pubdate',   '', li.attributes);
+    li_hash.pagination = helpers.get_li_attr_edi('pagination','', li.attributes);
+    li_hash.quantity = li.lineitem_details.size;
+    li_hash.copies = [];
+
+    IF INC_COPIES;
+        SEGMENT_COUNT = SEGMENT_COUNT + 1;
+        FOR lid IN li.lineitem_details;
+            fund = lid.fund.code;
+            item_type = lid.circ_modifier;
+            callnumber = lid.cn_label;
+            owning_lib = lid.owning_lib.shortname;
+            location = lid.location;
+            collection_code = lid.collection_code;
+
+            # when we have real copy data, treat it as authoritative for some fields
+            acp = lid.eg_copy_id;
+            IF acp;
+                item_type = acp.circ_modifier;
+                callnumber = acp.call_number.label;
+                location = acp.location.name;
+            END ;
+
+
+            # collapse like copies into groups w/ quantity
+
+            found_match = 0;
+            IF !INC_COPY_ID; # INC_COPY_ID implies 1 copy per GIR
+                FOR copy IN li_hash.copies;
+                    IF  (fund == copy.fund OR (!fund AND !copy.fund)) AND
+                        (item_type == copy.item_type OR (!item_type AND !copy.item_type)) AND
+                        (callnumber == copy.callnumber OR (!callnumber AND !copy.callnumber)) AND
+                        (owning_lib == copy.owning_lib OR (!owning_lib AND !copy.owning_lib)) AND
+                        (location == copy.location OR (!location AND !copy.location)) AND
+                        (collection_code == copy.collection_code OR (!collection_code AND !copy.collection_code));
+
+                        copy.quantity = copy.quantity + 1;
+                        found_match = 1;
+                    END;
                 END;
             END;
-        END;
 
-        IF !found_match;
-            compressed_copies.push({
-                fund => fund,
-                item_type => item_type,
-                callnumber => callnumber,
-                owning_lib => owning_lib,
-                location => location,
-                collection_code => collection_code,
-                copy_id => lid.id, # for INC_COPY_ID
-                quantity => 1
-            });
-        END;
-    END;
-    FOR copy IN compressed_copies;
+            IF !found_match;
+                li_hash.copies.push({
+                    fund => helpers.escape_edi(fund),
+                    item_type => helpers.escape_edi(item_type),
+                    callnumber => helpers.escape_edi(callnumber),
+                    owning_lib => helpers.escape_edi(owning_lib),
+                    location => helpers.escape_edi(location),
+                    collection_code => helpers.escape_edi(collection_code),
+                    copy_id => lid.id, # for INC_COPY_ID
+                    quantity => 1
+                });
+            END;
+        END; # copies loop
+    END; # inc_copies
 
-    # If we assume owning_lib is required and set, 
-    # it is safe to prepend each following copy field w/ a ","
+    LINEITEMS.push(li_hash);
 
+END; # for lineitem
+-%]
+[%- BLOCK big_block -%]
+UNA:+.? '
+UNB+UNOB:3+[% ORG_UNIT_SAN %]:31B+[% VENDOR_SAN %]:31B+140924:1351+1'
+UNH+1+ORDERS:D:96A:UN'
+BGM+220+98+9'
+DTM+137:[% date.format(date.now, '%Y%m%d') %]:102'
+NAD+BY+[% BUYER_CODE %]::91'
+NAD+SU+[% VENDOR_SAN %]::31B'
+NAD+SU+12::92'
+CUX+2:[% target.provider.currency_type %]:9'
+[%- FOR li_hash IN LINEITEMS -%]
+LIN+[% li_hash.id %]++[% li_hash.idval %]:[% li_hash.idqual %]'
+PIA+5+[% li_hash.idval %]:[% li_hash.idqual %]'
+IMD+F+BTI+:::[% li_hash.title %]'
+IMD+F+BPU+:::[% li_hash.author %]'
+IMD+F+BPD+:::[% li_hash.pubdate %]'
+IMD+F+BPH+:::[% li_hash.pagination %]'
+QTY+21:[% li_hash.quantity %]'
+[%- GIR_INDEX = 0; -%]
+[% FOR copy IN li_hash.copies;
+    GIR_INDEX = GIR_INDEX + 1;
+    FIELD_COUNT = 0;
     # B&T EDI requires expected GIR fields to be 
     # present regardless of whether a value exists.  
-    # some fields are required to have a value in ACQ, 
-    # though, so they are not forced into place below. 
-    
-    OWNING_LIB='';
-    FUND='';
-    CALLNUMBER='';
-    ITEM_TYPE='';
-    LOCATION='';
-    COLLECTION_CODE='';
-    QUANTITY='';
-    COPY_ID='';
+%]
+GIR+[% GIR_INDEX | format('%03d') -%]
+owning lib is [% copy.owning_lib %]
+[% IF INC_OWNING_LIB AND copy.owning_lib; FIELD_COUNT = FIELD_COUNT + 1 -%]
++[% copy.owning_lib %]:LLO[%- END -%]
+[% IF INC_FUND AND copy.fund; FIELD_COUNT = FIELD_COUNT + 1 -%]
++[% copy.fund %]:LFN[%- END -%]
+[% IF INC_CALLNUMBER AND (VENDOR_BT OR copy.callnumber); FIELD_COUNT = FIELD_COUNT + 1 -%]
++[% copy.callnumber %]:LSM[%- END -%]
+[% IF INC_ITEM_TYPE AND (VENDOR_BT OR copy.item_type); FIELD_COUNT = FIELD_COUNT + 1 -%]
++[% copy.item_type %]:LST[%- END -%]
+[% IF INC_COLLECTION_CODE AND (VENDOR_BT OR copy.collection_code); FIELD_COUNT = FIELD_COUNT + 1 -%]
++[% copy.collection_code %]:LSQ[%- END -%]
+[%#
+EDI only allows 5 fields per GIR segment. 
+when we exceed 5, add a new GIR segement.
 -%]
-
-
-[%- IF INC_OWNING_LIB AND copy.owning_lib %]OWNING_LIB= "owning_lib":"[% copy.owning_lib %]"[% END -%]
-[%- IF INC_FUND AND copy.fund %],"fund":"[% copy.fund %]"[% END -%]
-[%- IF INC_CALLNUMBER AND (VENDOR_BT OR copy.callnumber) %],"call_number":"[% copy.callnumber %]"[% END -%]
-[%- IF INC_ITEM_TYPE AND (VENDOR_BT OR copy.item_type) %],"item_type":"[% copy.item_type %]"[% END -%]
-[%- IF INC_LOCATION AND copy.location %],"copy_location":"[% copy.location %]"[% END -%]
-[%- IF INC_COLLECTION_CODE AND (VENDOR_BT OR copy.collection_code) %],"collection_code":"[% copy.collection_code %]"[% END -%]
-[%- IF INC_QUANTITY %],"quantity":"[% copy.quantity %]"[% END -%]
-[%- IF INC_COPY_ID %],"copy_id":"[% copy.copy_id %]" [% END %]
-
-GIR+[% GIR_INDEX.format('%d.03') %]
-
-[%- END -%] [%# FOR compressed_copies -%]
-[%- END -%] [%# IF INC_COPIES -%]
+[%- IF FIELD_COUNT == 5; 
+    FIELD_COUNT = 0; SEGMENT_COUNT = SEGMENT_COUNT + 1 -%]
+GIR+[% GIR_INDEX.format('%03d') -%][% END -%]
+[%- IF INC_LOCATION AND (VENDOR_BT OR copy.location); 
+    FIELD_COUNT = FIELD_COUNT + 1 ; SEGMENT_COUNT = SEGMENT_COUNT + 1-%]
++[% copy.location %]:LFH[%- END -%]
+[%- IF FIELD_COUNT == 5; 
+    FIELD_COUNT = 0; SEGMENT_COUNT = SEGMENT_COUNT + 1 -%]
+GIR+[% GIR_INDEX.format('%03d') -%][% END -%]
+[%- IF INC_QUANTITY AND (VENDOR_BT OR copy.quantity); 
+    FIELD_COUNT = FIELD_COUNT + 1; SEGMENT_COUNT = SEGMENT_COUNT + 1 -%]
++[% copy.quantity %]:LQT[%- END -%]
+[%- IF FIELD_COUNT == 5; 
+    FIELD_COUNT = 0; SEGMENT_COUNT = SEGMENT_COUNT + 1 -%]
+GIR+[% GIR_INDEX.format('%03d') -%][% END -%]
+[%- IF INC_COPY_ID AND (VENDOR_BT OR copy.copy_id); 
+    FIELD_COUNT = FIELD_COUNT + 1 -%]
++[% copy.copy_id %]:LCO[%- END -%]
+[% END %] [%# FOR copies %]
 FTX+LIN+1'
 PRI+AAB:[% li.estimated_unit_price || "0.00" %]'
 RFF+LI:[% target.id %]/[% li.id %]'
-[%- END -%]
+[%- END -%][%# for lineitems %]
 UNS+S'
 CNT+2:[% target.lineitems.size %]'
 UNT+40+1'
 UNZ+1+1'
-[% END %]
+[% END %][%# block %]
 [% 
     tempo = PROCESS big_block; 
     # EDI should not have newlines, but they are useful for testing.