]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.data.acq-order-ident.sql
Teach ACQ EDI and print PO templates about order identifiers
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.data.acq-order-ident.sql
1
2 BEGIN;
3
4 -- Listed here for reference / ease of access.  The update
5 -- is not applied here (see the WHERE clause).
6 UPDATE action_trigger.event_definition SET template = 
7 $$
8 [%- USE date -%]
9 [%
10     # extract some commonly used variables
11
12     VENDOR_SAN = target.provider.san;
13     VENDCODE = target.provider.edi_default.vendcode;
14     VENDACCT = target.provider.edi_default.vendacct;
15     ORG_UNIT_SAN = target.ordering_agency.mailing_address.san;
16
17     # set the vendor / provider
18
19     VENDOR_BT      = 0; # Baker & Taylor
20     VENDOR_INGRAM  = 0;
21     VENDOR_BRODART = 0;
22     VENDOR_MW_TAPE = 0; # Midwest Tape
23     VENDOR_RB      = 0; # Recorded Books
24     VENDOR_ULS     = 0; # ULS
25
26     IF    VENDOR_SAN == '1556150'; VENDOR_BT = 1;
27     ELSIF VENDOR_SAN == '1697684'; VENDOR_BRODART = 1;
28     ELSIF VENDOR_SAN == '1697978'; VENDOR_INGRAM = 1;
29     ELSIF VENDOR_SAN == '2549913'; VENDOR_MW_TAPE = 1;
30     ELSIF VENDOR_SAN == '1113984'; VENDOR_RB = 1;
31     ELSIF VENDOR_SAN == '1699342'; VENDOR_ULS = 1;
32     END;
33
34     # if true, pass the PO name as a secondary identifier
35     # RFF+LI:<name>/li_id
36     INC_PO_NAME = 0;
37     IF VENDOR_INGRAM;
38         INC_PO_NAME = 1;
39     END;
40
41     # GIR configuration --------------------------------------
42
43     INC_COPIES = 1; # copies on/off switch
44     INC_FUND = 0;
45     INC_CALLNUMBER = 0;
46     INC_ITEM_TYPE = 1;
47     INC_LOCATION = 0;
48     INC_COLLECTION_CODE = 1;
49     INC_OWNING_LIB = 1;
50     INC_QUANTITY = 1;
51     INC_COPY_ID = 0;
52
53     IF VENDOR_BT;
54         INC_CALLNUMBER = 1;
55     END;
56
57     IF VENDOR_BRODART;
58         INC_FUND = 1;
59     END;
60
61     IF VENDOR_MW_TAPE;
62         INC_FUND = 1;
63         INC_COLLECTION_CODE = 0;
64         INC_ITEM_TYPE = 0;
65     END;
66
67     # END GIR configuration ---------------------------------
68
69 -%]
70 [%- BLOCK big_block -%]
71 {
72    "recipient":"[% VENDOR_SAN %]",
73    "sender":"[% ORG_UNIT_SAN %]",
74    "body": [{
75      "ORDERS":[ "order", {
76
77         "po_number":[% target.id %],
78
79         [% IF INC_PO_NAME %]
80         "po_name":"[% target.name | replace('\/', ' ') | replace('"', '\"') %]",
81         [% END %]
82
83         "date":"[% date.format(date.now, '%Y%m%d') %]",
84
85         "buyer":[
86             [% IF VENDOR_BT %]
87                 {"id-qualifier": 91, "id":"[% ORG_UNIT_SAN %] [% VENDCODE %]"}
88             [% ELSE %]
89                 {"id":"[% ORG_UNIT_SAN %]"},
90                 {"id-qualifier": 91, "id":"[% VENDACCT %]"}
91             [% END %]
92         ],
93
94         "vendor":[
95             "[% VENDOR_SAN %]",
96             {"id-qualifier": 92, "id":"[% target.provider.id %]"}
97         ],
98
99         "currency":"[% target.provider.currency_type %]",
100                 
101         "items":[
102         [%- FOR li IN target.lineitems %]
103         {
104             "line_index":"[% li.id %]",
105             "identifiers":[   
106             [%- 
107                 idval = '';
108                 idqual = 'EN'; # default ISBN/UPC/EAN-13
109                 ident_attr = helpers.get_li_order_ident(li.attributes);
110                 IF ident_attr;
111                     idname = ident_attr.attr_name;
112                     idval = ident_attr.attr_value;
113                     IF idname == 'isbn' AND idval.length != 13;
114                         idqual = 'IB';
115                     ELSIF idname == 'issn';
116                         idqual = 'IS';
117                     END;
118                 ELSE;
119                     idqual = 'IN';
120                     idval = li.id;
121                 END -%]
122                 {"id-qualifier":"[% idqual %]","id":"[% idval %]"}
123             ],
124             "price":[% li.estimated_unit_price || '0.00' %],
125             "desc":[
126                 {"BTI":"[% helpers.get_li_attr_jedi('title',     '', li.attributes) %]"},
127                 {"BPU":"[% helpers.get_li_attr_jedi('publisher', '', li.attributes) %]"},
128                 {"BPD":"[% helpers.get_li_attr_jedi('pubdate',   '', li.attributes) %]"},
129                 [% IF VENDOR_ULS -%]
130                 {"BEN":"[% helpers.get_li_attr_jedi('edition',   '', li.attributes) %]"},
131                 {"BAU":"[% helpers.get_li_attr_jedi('author',    '', li.attributes) %]"}
132                 [%- ELSE -%]
133                 {"BPH":"[% helpers.get_li_attr_jedi('pagination','', li.attributes) %]"}
134                 [%- END %]
135             ],
136             [%- ftx_vals = []; 
137                 FOR note IN li.lineitem_notes;
138                     NEXT UNLESS note.vendor_public == 't'; 
139                     ftx_vals.push(note.value); 
140                 END; 
141                 IF VENDOR_BRODART; # look for copy-level spec code
142                     FOR lid IN li.lineitem_details;
143                         IF lid.note;
144                             spec_note = lid.note.match('spec code ([a-zA-Z0-9_])');
145                             IF spec_note.0; ftx_vals.push(spec_note.0); END;
146                         END;
147                     END;
148                 END; 
149                 IF xtra_ftx;           ftx_vals.unshift(xtra_ftx); END; 
150
151                 # BT & ULS want FTX+LIN for every LI, even if empty
152                 IF ((VENDOR_BT OR VENDOR_ULS) AND ftx_vals.size == 0);
153                     ftx_vals.unshift('');
154                 END;  
155             -%]
156
157             "free-text":[ 
158                 [% FOR note IN ftx_vals -%] "[% note %]"[% UNLESS loop.last %], [% END %][% END %] 
159             ],            
160
161             "quantity":[% li.lineitem_details.size %],
162
163             [%- IF INC_COPIES -%]
164             "copies" : [
165                 [%- compressed_copies = [];
166                     FOR lid IN li.lineitem_details;
167                         fund = lid.fund.code;
168                         item_type = lid.circ_modifier;
169                         callnumber = lid.cn_label;
170                         owning_lib = lid.owning_lib.shortname;
171                         location = lid.location;
172                         collection_code = lid.collection_code;
173     
174                         # when we have real copy data, treat it as authoritative for some fields
175                         acp = lid.eg_copy_id;
176                         IF acp;
177                             item_type = acp.circ_modifier;
178                             callnumber = acp.call_number.label;
179                             location = acp.location.name;
180                         END ;
181
182
183                         # collapse like copies into groups w/ quantity
184
185                         found_match = 0;
186                         IF !INC_COPY_ID; # INC_COPY_ID implies 1 copy per GIR
187                             FOR copy IN compressed_copies;
188                                 IF  (fund == copy.fund OR (!fund AND !copy.fund)) AND
189                                     (item_type == copy.item_type OR (!item_type AND !copy.item_type)) AND
190                                     (callnumber == copy.callnumber OR (!callnumber AND !copy.callnumber)) AND
191                                     (owning_lib == copy.owning_lib OR (!owning_lib AND !copy.owning_lib)) AND
192                                     (location == copy.location OR (!location AND !copy.location)) AND
193                                     (collection_code == copy.collection_code OR (!collection_code AND !copy.collection_code));
194
195                                     copy.quantity = copy.quantity + 1;
196                                     found_match = 1;
197                                 END;
198                             END;
199                         END;
200
201                         IF !found_match;
202                             compressed_copies.push({
203                                 fund => fund,
204                                 item_type => item_type,
205                                 callnumber => callnumber,
206                                 owning_lib => owning_lib,
207                                 location => location,
208                                 collection_code => collection_code,
209                                 copy_id => lid.id, # for INC_COPY_ID
210                                 quantity => 1
211                             });
212                         END;
213                     END;
214                     FOR copy IN compressed_copies;
215
216                     # If we assume owning_lib is required and set, 
217                     # it is safe to prepend each following copy field w/ a ","
218
219                     # B&T EDI requires expected GIR fields to be 
220                     # present regardless of whether a value exists.  
221                     # some fields are required to have a value in ACQ, 
222                     # though, so they are not forced into place below.
223
224                  %]{[%- IF INC_OWNING_LIB AND copy.owning_lib %] "owning_lib":"[% copy.owning_lib %]"[% END -%]
225                     [%- IF INC_FUND AND copy.fund %],"fund":"[% copy.fund %]"[% END -%]
226                     [%- IF INC_CALLNUMBER AND (VENDOR_BT OR copy.callnumber) %],"call_number":"[% copy.callnumber %]"[% END -%]
227                     [%- IF INC_ITEM_TYPE AND (VENDOR_BT OR copy.item_type) %],"item_type":"[% copy.item_type %]"[% END -%]
228                     [%- IF INC_LOCATION AND copy.location %],"copy_location":"[% copy.location %]"[% END -%]
229                     [%- IF INC_COLLECTION_CODE AND (VENDOR_BT OR copy.collection_code) %],"collection_code":"[% copy.collection_code %]"[% END -%]
230                     [%- IF INC_QUANTITY %],"quantity":"[% copy.quantity %]"[% END -%]
231                     [%- IF INC_COPY_ID %],"copy_id":"[% copy.copy_id %]" [% END %]}[% ',' UNLESS loop.last -%]
232                 [%- END -%] [%# FOR compressed_copies -%]
233             ]
234             [%- END -%] [%# IF INC_COPIES %]
235
236         }[% UNLESS loop.last %],[% END -%]
237
238         [% END %] [%# END lineitems %]
239         ],
240         "line_items":[% target.lineitems.size %]
241      }]  [%# close ORDERS array %]
242    }]    [%# close  body  array %]
243 }
244 [% END %]
245 [% tempo = PROCESS big_block; helpers.escape_json(tempo) %]
246 $$
247 WHERE ID = 23 AND FALSE; -- remove 'AND FALSE' to apply this update
248
249
250 -- lineitem worksheet
251 UPDATE action_trigger.event_definition SET template = 
252 $$
253 [%- USE date -%]
254 [%-
255     # find a lineitem attribute by name and optional type
256     BLOCK get_li_attr;
257         FOR attr IN li.attributes;
258             IF attr.attr_name == attr_name;
259                 IF !attr_type OR attr_type == attr.attr_type;
260                     attr.attr_value;
261                     LAST;
262                 END;
263             END;
264         END;
265     END
266 -%]
267
268 <h2>Purchase Order [% target.id %]</h2>
269 <br/>
270 date <b>[% date.format(date.now, '%Y%m%d') %]</b>
271 <br/>
272
273 <style>
274     table td { padding:5px; border:1px solid #aaa;}
275     table { width:95%; border-collapse:collapse; }
276     #vendor-notes { padding:5px; border:1px solid #aaa; }
277 </style>
278 <table id='vendor-table'>
279   <tr>
280     <td valign='top'>Vendor</td>
281     <td>
282       <div>[% target.provider.name %]</div>
283       <div>[% target.provider.addresses.0.street1 %]</div>
284       <div>[% target.provider.addresses.0.street2 %]</div>
285       <div>[% target.provider.addresses.0.city %]</div>
286       <div>[% target.provider.addresses.0.state %]</div>
287       <div>[% target.provider.addresses.0.country %]</div>
288       <div>[% target.provider.addresses.0.post_code %]</div>
289     </td>
290     <td valign='top'>Ship to / Bill to</td>
291     <td>
292       <div>[% target.ordering_agency.name %]</div>
293       <div>[% target.ordering_agency.billing_address.street1 %]</div>
294       <div>[% target.ordering_agency.billing_address.street2 %]</div>
295       <div>[% target.ordering_agency.billing_address.city %]</div>
296       <div>[% target.ordering_agency.billing_address.state %]</div>
297       <div>[% target.ordering_agency.billing_address.country %]</div>
298       <div>[% target.ordering_agency.billing_address.post_code %]</div>
299     </td>
300   </tr>
301 </table>
302
303 <br/><br/>
304 <fieldset id='vendor-notes'>
305     <legend>Notes to the Vendor</legend>
306     <ul>
307     [% FOR note IN target.notes %]
308         [% IF note.vendor_public == 't' %]
309             <li>[% note.value %]</li>
310         [% END %]
311     [% END %]
312     </ul>
313 </fieldset>
314 <br/><br/>
315
316 <table>
317   <thead>
318     <tr>
319       <th>PO#</th>
320       <th>ISBN or Item #</th>
321       <th>Title</th>
322       <th>Quantity</th>
323       <th>Unit Price</th>
324       <th>Line Total</th>
325       <th>Notes</th>
326     </tr>
327   </thead>
328   <tbody>
329
330   [% subtotal = 0 %]
331   [% FOR li IN target.lineitems %]
332
333   <tr>
334     [% count = li.lineitem_details.size %]
335     [% price = li.estimated_unit_price %]
336     [% litotal = (price * count) %]
337     [% subtotal = subtotal + litotal %]
338     [% 
339         ident_attr = helpers.get_li_order_ident(li.attributes);
340         SET ident_value = ident_attr.attr_value IF ident_attr;
341     %]
342     <td>[% target.id %]</td>
343     <td>[% ident_value %]</td>
344     <td>[% PROCESS get_li_attr attr_name = 'title' %]</td>
345     <td>[% count %]</td>
346     <td>[% price %]</td>
347     <td>[% litotal %]</td>
348     <td>
349         <ul>
350         [% FOR note IN li.lineitem_notes %]
351             [% IF note.vendor_public == 't' %]
352                 <li>[% note.value %]</li>
353             [% END %]
354         [% END %]
355         </ul>
356     </td>
357   </tr>
358   [% END %]
359   <tr>
360     <td/><td/><td/><td/>
361     <td>Subtotal</td>
362     <td>[% subtotal %]</td>
363   </tr>
364   </tbody>
365 </table>
366
367 <br/>
368
369 Total Line Item Count: [% target.lineitems.size %]
370 $$
371 WHERE ID = 4; -- PO HTML
372
373 COMMIT;