]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0326.data.payment_receipt_format.sql
LP#1643709: Stamping upgrade scripts
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0326.data.payment_receipt_format.sql
1 BEGIN;
2
3 -- Replaces and augments some of the data from 0289.data.payment_receipt_format.sql
4 INSERT INTO config.upgrade_log (version) VALUES ('0326'); -- phasefx
5
6 UPDATE action_trigger.event_definition SET template =
7 $$
8 [%- USE date -%]
9 [%- SET user = target.0.xact.usr -%]
10 To: [%- params.recipient_email || user.email %]
11 From: [%- params.sender_email || default_sender %]
12 Subject: Payment Receipt
13
14 [% date.format -%]
15 [%- SET xact_mp_hash = {} -%]
16 [%- FOR mp IN target %][%# Template is hooked around payments, but let us make the receipt focused on transactions -%]
17     [%- SET xact_id = mp.xact.id -%]
18     [%- IF ! xact_mp_hash.defined( xact_id ) -%][%- xact_mp_hash.$xact_id = { 'xact' => mp.xact, 'payments' => [] } -%][%- END -%]
19     [%- xact_mp_hash.$xact_id.payments.push(mp) -%]
20 [%- END -%]
21 [%- FOR xact_id IN xact_mp_hash.keys.sort -%]
22     [%- SET xact = xact_mp_hash.$xact_id.xact %]
23 Transaction ID: [% xact_id %]
24     [% IF xact.circulation %][% helpers.get_copy_bib_basics(xact.circulation.target_copy).title %]
25     [% ELSE %]Miscellaneous
26     [% END %]
27     Line item billings:
28         [%- SET mb_type_hash = {} -%]
29         [%- FOR mb IN xact.billings %][%# Group billings by their btype -%]
30             [%- IF mb.voided == 'f' -%]
31                 [%- SET mb_type = mb.btype.id -%]
32                 [%- IF ! mb_type_hash.defined( mb_type ) -%][%- mb_type_hash.$mb_type = { 'sum' => 0.00, 'billings' => [] } -%][%- END -%]
33                 [%- IF ! mb_type_hash.$mb_type.defined( 'first_ts' ) -%][%- mb_type_hash.$mb_type.first_ts = mb.billing_ts -%][%- END -%]
34                 [%- mb_type_hash.$mb_type.last_ts = mb.billing_ts -%]
35                 [%- mb_type_hash.$mb_type.sum = mb_type_hash.$mb_type.sum + mb.amount -%]
36                 [%- mb_type_hash.$mb_type.billings.push( mb ) -%]
37             [%- END -%]
38         [%- END -%]
39         [%- FOR mb_type IN mb_type_hash.keys.sort -%]
40             [%- IF mb_type == 1 %][%-# Consolidated view of overdue billings -%]
41                 $[% mb_type_hash.$mb_type.sum %] for [% mb_type_hash.$mb_type.billings.0.btype.name %] 
42                     on [% mb_type_hash.$mb_type.first_ts %] through [% mb_type_hash.$mb_type.last_ts %]
43             [%- ELSE -%][%# all other billings show individually %]
44                 [% FOR mb IN mb_type_hash.$mb_type.billings %]
45                     $[% mb.amount %] for [% mb.btype.name %] on [% mb.billing_ts %] [% mb.note %]
46                 [% END %]
47             [% END %]
48         [% END %]
49     Line item payments:
50         [% FOR mp IN xact_mp_hash.$xact_id.payments %]
51             Payment ID: [% mp.id %]
52                 Paid [% mp.amount %] via [% SWITCH mp.payment_type -%]
53                     [% CASE "cash_payment" %]cash
54                     [% CASE "check_payment" %]check
55                     [% CASE "credit_card_payment" %]credit card (
56                         [%- SET cc_chunks = mp.credit_card_payment.cc_number.replace(' ','').chunk(4); -%]
57                         [%- cc_chunks.slice(0, -1+cc_chunks.max).join.replace('\S','X') -%] 
58                         [% cc_chunks.last -%]
59                         exp [% mp.credit_card_payment.expire_month %]/[% mp.credit_card_payment.expire_year -%]
60                     )
61                     [% CASE "credit_payment" %]credit
62                     [% CASE "forgive_payment" %]forgiveness
63                     [% CASE "goods_payment" %]goods
64                     [% CASE "work_payment" %]work
65                 [%- END %] on [% mp.payment_ts %] [% mp.note %]
66         [% END %]
67 [% END %]
68 $$
69 WHERE id = 29;
70
71 UPDATE action_trigger.event_definition SET template =
72 $$
73 [%- USE date -%][%- SET user = target.0.xact.usr -%]
74 <div style="li { padding: 8px; margin 5px; }">
75     <div>[% date.format %]</div><br/>
76     <ol>
77     [% SET xact_mp_hash = {} %]
78     [% FOR mp IN target %][%# Template is hooked around payments, but let us make the receipt focused on transactions %]
79         [% SET xact_id = mp.xact.id %]
80         [% IF ! xact_mp_hash.defined( xact_id ) %][% xact_mp_hash.$xact_id = { 'xact' => mp.xact, 'payments' => [] } %][% END %]
81         [% xact_mp_hash.$xact_id.payments.push(mp) %]
82     [% END %]
83     [% FOR xact_id IN xact_mp_hash.keys.sort %]
84         [% SET xact = xact_mp_hash.$xact_id.xact %]
85         <li>Transaction ID: [% xact_id %]
86             [% IF xact.circulation %][% helpers.get_copy_bib_basics(xact.circulation.target_copy).title %]
87             [% ELSE %]Miscellaneous
88             [% END %]
89             Line item billings:<ol>
90                 [% SET mb_type_hash = {} %]
91                 [% FOR mb IN xact.billings %][%# Group billings by their btype %]
92                     [% IF mb.voided == 'f' %]
93                         [% SET mb_type = mb.btype.id %]
94                         [% IF ! mb_type_hash.defined( mb_type ) %][% mb_type_hash.$mb_type = { 'sum' => 0.00, 'billings' => [] } %][% END %]
95                         [% IF ! mb_type_hash.$mb_type.defined( 'first_ts' ) %][% mb_type_hash.$mb_type.first_ts = mb.billing_ts %][% END %]
96                         [% mb_type_hash.$mb_type.last_ts = mb.billing_ts %]
97                         [% mb_type_hash.$mb_type.sum = mb_type_hash.$mb_type.sum + mb.amount %]
98                         [% mb_type_hash.$mb_type.billings.push( mb ) %]
99                     [% END %]
100                 [% END %]
101                 [% FOR mb_type IN mb_type_hash.keys.sort %]
102                     <li>[% IF mb_type == 1 %][%# Consolidated view of overdue billings %]
103                         $[% mb_type_hash.$mb_type.sum %] for [% mb_type_hash.$mb_type.billings.0.btype.name %] 
104                             on [% mb_type_hash.$mb_type.first_ts %] through [% mb_type_hash.$mb_type.last_ts %]
105                     [% ELSE %][%# all other billings show individually %]
106                         [% FOR mb IN mb_type_hash.$mb_type.billings %]
107                             $[% mb.amount %] for [% mb.btype.name %] on [% mb.billing_ts %] [% mb.note %]
108                         [% END %]
109                     [% END %]</li>
110                 [% END %]
111             </ol>
112             Line item payments:<ol>
113                 [% FOR mp IN xact_mp_hash.$xact_id.payments %]
114                     <li>Payment ID: [% mp.id %]
115                         Paid [% mp.amount %] via [% SWITCH mp.payment_type -%]
116                             [% CASE "cash_payment" %]cash
117                             [% CASE "check_payment" %]check
118                             [% CASE "credit_card_payment" %]credit card (
119                                 [%- SET cc_chunks = mp.credit_card_payment.cc_number.replace(' ','').chunk(4); -%]
120                                 [%- cc_chunks.slice(0, -1+cc_chunks.max).join.replace('\S','X') -%] 
121                                 [% cc_chunks.last -%]
122                                 exp [% mp.credit_card_payment.expire_month %]/[% mp.credit_card_payment.expire_year -%]
123                             )
124                             [% CASE "credit_payment" %]credit
125                             [% CASE "forgive_payment" %]forgiveness
126                             [% CASE "goods_payment" %]goods
127                             [% CASE "work_payment" %]work
128                         [%- END %] on [% mp.payment_ts %] [% mp.note %]
129                     </li>
130                 [% END %]
131             </ol>
132         </li>
133     [% END %]
134     </ol>
135 </div>
136 $$
137 WHERE id = 30;
138
139 INSERT INTO action_trigger.environment (
140         event_def,
141         path
142     ) VALUES -- for fleshing mp objects
143          ( 29, 'credit_card_payment')
144         ,( 29, 'xact.billings')
145         ,( 29, 'xact.billings.btype')
146         ,( 30, 'credit_card_payment')
147         ,( 30, 'xact.billings')
148         ,( 30, 'xact.billings.btype')
149 ;
150
151 COMMIT;
152