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