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