--- /dev/null
+
+BEGIN;
+
+INSERT INTO action_trigger.event_definition
+ (id, active, owner, name, hook, validator, reactor, cleanup_success,
+ cleanup_failure, delay, delay_field, group_field, template)
+VALUES
+ (51, true, 1, 'PO EDI', 'acqpo.activated',
+ 'Acq::PurchaseOrderEDIRequired', 'GeneratePurchaseOrderJEDI',
+ NULL, NULL, '00:00:00', NULL, NULL,
+$$
+[%- 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;
+
+ # END GIR configuration ---------------------------------
+
+ # TODO: helpers.escape_edi() for removing/escaping single quotes!
+
+-%]
+[%- BLOCK big_block -%]
+UNA:+.? '
+UNB+UNOB:3+338277X:31B+1556150:31B+140924:1351+1'
+UNH+1+ORDERS:D:96A:UN'
+BGM+220+98+9'
+DTM+137:20140909:102'
+NAD+BY+338277X 0021::91'
+NAD+SU+1556150::31B'
+NAD+SU+12::92'
+CUX+2:USD:9'
+[% FOR li IN target.lineitems;
+ idval = '';
+ idqual = 'EN'; # default ISBN/UPC/EAN-13
+ ident_attr = helpers.get_li_order_ident(li.attributes);
+ IF ident_attr;
+ idname = ident_attr.attr_name;
+ idval = ident_attr.attr_value;
+ IF idname == 'isbn' AND idval.length != 13;
+ idqual = 'IB';
+ ELSIF idname == 'issn';
+ idqual = 'IS';
+ END;
+ ELSE;
+ idqual = 'IN';
+ idval = li.id;
+ END -%]
+LIN+[% li.id %]++[% idval %]:[% idqual %]'
+PIA+5+[% idval %]:[% idqual %]'
+IMD+F+BTI+:::[% helpers.get_li_attr_jedi('title', '', li.attributes) %]'
+IMD+F+BPU+:::[% helpers.get_li_attr_jedi('author', '', li.attributes) %]'
+IMD+F+BPD+:::[% helpers.get_li_attr_jedi('pubdate', '', li.attributes) %]'
+IMD+F+BPH+:::[% helpers.get_li_attr_jedi('pagination', '', li.attributes) %]'
+QTY+21:[% li.lineitem_details.size %]'
+FTX+LIN+1'
+PRI+AAB:30'
+RFF+LI:[% target.id %]/[% li.id %]'
+[% END %]
+UNS+S'
+CNT+2:3'
+UNT+40+1'
+UNZ+1+1'
+[% END %]
+[% tempo = PROCESS big_block; tempo; %]
+$$
+);
+
+INSERT INTO action_trigger.environment (event_def, path) VALUES
+ (51, 'lineitems.attributes'),
+ (51, 'lineitems.lineitem_details.owning_lib'),
+ (51, 'lineitems.lineitem_details.location'),
+ (51, 'lineitems.lineitem_details.fund'),
+ (51, 'lineitems.lineitem_details.eg_copy_id.location'),
+ (51, 'lineitems.lineitem_details.eg_copy_id.call_number'),
+ (51, 'lineitems.lineitem_notes'),
+ (51, 'ordering_agency.mailing_address'),
+ (51, 'provider'),
+ (51, 'provider.edi_default');
+
+COMMIT;
+
+/*
+-- UNDO:
+
+BEGIN;
+DELETE FROM action_trigger.event where event_def = 51;
+DELETE FROM action_trigger.environment where event_def = 51;
+DELETE FROM action_trigger.event_definition WHERE id = 51;
+COMMIT;
+
+-- TESTING
+
+INSERT INTO action_trigger.event (target, event_def, run_time,state)
+VALUES (1, 51, now(), 'pending');
+
+*/
+
+