]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.data.jedi_with_copies.sql
EDI ORDER template, now with copies
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.data.jedi_with_copies.sql
1
2
3 -- XXX
4 -- Template update included here for reference only.
5 -- The stock JEDI template is not updated here (see WHERE clause)
6 -- We do update the environment, though, for easier local template 
7 -- updating.  No env fields are removed (that aren't otherwise replaced).
8 --
9
10 UPDATE action_trigger.event_definition SET template =
11 $$[%- USE date -%]
12 [%# start JEDI document 
13   # Vendor specific kludges:
14   # BT      - vendcode goes to NAD/BY *suffix*  w/ 91 qualifier
15   # INGRAM  - vendcode goes to NAD/BY *segment* w/ 91 qualifier (separately)
16   # BRODART - vendcode goes to FTX segment (lineitem level)
17 -%]
18 [%- 
19 IF target.provider.edi_default.vendcode && target.provider.code == 'BRODART';
20     xtra_ftx = target.provider.edi_default.vendcode;
21 END;
22 -%]
23 [%- BLOCK big_block -%]
24 {
25    "recipient":"[% target.provider.san %]",
26    "sender":"[% target.ordering_agency.mailing_address.san %]",
27    "body": [{
28      "ORDERS":[ "order", {
29         "po_number":[% target.id %],
30         "date":"[% date.format(date.now, '%Y%m%d') %]",
31         "buyer":[
32             [%   IF   target.provider.edi_default.vendcode && (target.provider.code == 'BT' || target.provider.name.match('(?i)^BAKER & TAYLOR'))  -%]
33                 {"id-qualifier": 91, "id":"[% target.ordering_agency.mailing_address.san _ ' ' _ target.provider.edi_default.vendcode %]"}
34             [%- ELSIF target.provider.edi_default.vendcode && target.provider.code == 'INGRAM' -%]
35                 {"id":"[% target.ordering_agency.mailing_address.san %]"},
36                 {"id-qualifier": 91, "id":"[% target.provider.edi_default.vendcode %]"}
37             [%- ELSE -%]
38                 {"id":"[% target.ordering_agency.mailing_address.san %]"}
39             [%- END -%]
40         ],
41         "vendor":[
42             [%- # target.provider.name (target.provider.id) -%]
43             "[% target.provider.san %]",
44             {"id-qualifier": 92, "id":"[% target.provider.id %]"}
45         ],
46         "currency":"[% target.provider.currency_type %]",
47                 
48         "items":[
49         [%- FOR li IN target.lineitems %]
50         {
51             "line_index":"[% li.id %]",
52             "identifiers":[   [%-# li.isbns = helpers.get_li_isbns(li.attributes) %]
53             [% FOR isbn IN helpers.get_li_isbns(li.attributes) -%]
54                 [% IF isbn.length == 13 -%]
55                 {"id-qualifier":"EN","id":"[% isbn %]"},
56                 [% ELSE -%]
57                 {"id-qualifier":"IB","id":"[% isbn %]"},
58                 [%- END %]
59             [% END %]
60                 {"id-qualifier":"IN","id":"[% li.id %]"}
61             ],
62             "price":[% li.estimated_unit_price || '0.00' %],
63             "desc":[
64                 {"BTI":"[% helpers.get_li_attr_jedi('title',     '', li.attributes) %]"},
65                 {"BPU":"[% helpers.get_li_attr_jedi('publisher', '', li.attributes) %]"},
66                 {"BPD":"[% helpers.get_li_attr_jedi('pubdate',   '', li.attributes) %]"},
67                 {"BPH":"[% helpers.get_li_attr_jedi('pagination','', li.attributes) %]"}
68             ],
69             [%- ftx_vals = []; 
70                 FOR note IN li.lineitem_notes; 
71                     NEXT UNLESS note.vendor_public == 't'; 
72                     ftx_vals.push(note.value); 
73                 END; 
74                 IF xtra_ftx;           ftx_vals.unshift(xtra_ftx); END; 
75                 IF ftx_vals.size == 0; ftx_vals.unshift('');       END;  # BT needs FTX+LIN for every LI, even if it is an empty one
76             -%]
77
78             "free-text":[ 
79                 [% FOR note IN ftx_vals -%] "[% note %]"[% UNLESS loop.last %], [% END %][% END %] 
80             ],            
81             "quantity":[% li.lineitem_details.size %],
82             "copies" : [
83                 [%- IF 1 -%]
84                 [%- FOR lid IN li.lineitem_details;
85                         fund = lid.fund.code;
86                         item_type = lid.circ_modifier;
87                         callnumber = lid.cn_label;
88                         owning_lib = lid.owning_lib.shortname;
89                         location = lid.location;
90     
91                         # when we have real copy data, treat it as authoritative
92                         acp = lid.eg_copy_id;
93                         IF acp;
94                             item_type = acp.circ_modifier;
95                             callnumber = acp.call_number.label;
96                             location = acp.location.name;
97                         END -%]
98                 {   [%- IF fund %] "fund" : "[% fund %]",[% END -%]
99                     [%- IF callnumber %] "call_number" : "[% callnumber %]", [% END -%]
100                     [%- IF item_type %] "item_type" : "[% item_type %]", [% END -%]
101                     [%- IF location %] "copy_location" : "[% location %]", [% END -%]
102                     [%- IF owning_lib %] "owning_lib" : "[% owning_lib %]", [% END -%]
103                     [%- #chomp %]"copy_id" : "[% lid.id %]" }[% ',' UNLESS loop.last %]
104                 [% END -%]
105                 [%- END -%]
106              ]
107         }[% UNLESS loop.last %],[% END %]
108         [%-# TODO: lineitem details (later) -%]
109         [% END %]
110         ],
111         "line_items":[% target.lineitems.size %]
112      }]  [%# close ORDERS array %]
113    }]    [%# close  body  array %]
114 }
115 [% END %]
116 [% tempo = PROCESS big_block; helpers.escape_json(tempo) %]
117 $$
118 WHERE id = 23 AND FALSE; -- DON'T PERFORM THE UPDATE
119
120
121 -- add copy-related fields to the environment if they're not already there.
122 DO $$
123 BEGIN
124     PERFORM 1 
125         FROM action_trigger.environment 
126         WHERE 
127             event_def = 23 AND 
128             path = 'lineitems.lineitem_details.owning_lib';
129     IF NOT FOUND THEN
130         INSERT INTO action_trigger.environment (event_def, path) 
131             VALUES (23, 'lineitems.lineitem_details.owning_lib'); 
132     END IF;
133
134     PERFORM 1 
135         FROM action_trigger.environment 
136         WHERE 
137             event_def = 23 AND 
138             path = 'lineitems.lineitem_details.fund';
139     IF NOT FOUND THEN
140         INSERT INTO action_trigger.environment (event_def, path) 
141             VALUES (23, 'lineitems.lineitem_details.fund'); 
142     END IF;
143
144     PERFORM 1 
145         FROM action_trigger.environment 
146         WHERE 
147             event_def = 23 AND 
148             path = 'lineitems.lineitem_details.location';
149     IF NOT FOUND THEN
150         INSERT INTO action_trigger.environment (event_def, path) 
151             VALUES (23, 'lineitems.lineitem_details.location'); 
152     END IF;
153
154     PERFORM 1 
155         FROM action_trigger.environment 
156         WHERE 
157             event_def = 23 AND 
158             path = 'lineitems.lineitem_details.eg_copy_id.location';
159     IF NOT FOUND THEN
160         INSERT INTO action_trigger.environment (event_def, path) 
161             VALUES (23, 'lineitems.lineitem_details.eg_copy_id.location'); 
162     END IF;
163
164     PERFORM 1 
165         FROM action_trigger.environment 
166         WHERE 
167             event_def = 23 AND 
168             path = 'lineitems.lineitem_details.eg_copy_id.call_number';
169     IF NOT FOUND THEN
170         INSERT INTO action_trigger.environment (event_def, path) 
171             VALUES (23, 'lineitems.lineitem_details.eg_copy_id.call_number'); 
172     END IF;
173
174
175
176     -- remove redundant entry
177     DELETE FROM action_trigger.environment 
178         WHERE event_def = 23 AND path = 'lineitems.lineitem_details'; 
179
180 END $$;
181
182
183