]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm
de-smartifying dbd::pg and adding user payment objects
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / money.pm
1 package OpenILS::Application::Storage::Publisher::money;
2 use base qw/OpenILS::Application::Storage/;
3 use OpenSRF::Utils::Logger qw/:level/;
4
5 my $log = 'OpenSRF::Utils::Logger';
6
7 sub new_collections {
8         my $self = shift;
9         my $client = shift;
10         my $age = shift;
11         my $amount = shift;
12         my @loc = @_;
13
14         my $mct = money::collections_tracker->table;
15         my $mb = money::billing->table;
16         my $circ = action::circulation->table;
17         my $mg = money::grocery->table;
18         my $descendants = "actor.org_unit_descendants((select id from actor.org_unit where shortname = ?))";
19
20         my $SQL = <<"   SQL";
21                 SELECT  lt.usr,
22                         MAX(bl.billing_ts) AS last_pertinent_billing,
23                         SUM(bl.amount) - COALESCE(SUM((SELECT SUM(amount) FROM money.payment WHERE xact = lt.id)),0) AS threshold_amount
24                   FROM  ( SELECT id,usr,billing_location AS location FROM money.grocery
25                                 UNION ALL
26                           SELECT id,usr,circ_lib AS location FROM action.circulation ) AS lt
27                         JOIN $descendants d ON (lt.location = d.id)
28                         JOIN money.billing bl ON (lt.id = bl.xact AND bl.voided IS FALSE)
29                   WHERE AGE(bl.billing_ts) > ?
30                   GROUP BY lt.usr
31                   HAVING  SUM(
32                                 (SELECT COUNT(*)
33                                   FROM  money.collections_tracker
34                                   WHERE usr = lt.usr
35                                         AND location in (
36                                                 (SELECT id
37                                                   FROM  $descendants )
38                                         )
39                                 ) ) = 0
40                         AND (SUM(bl.amount) - COALESCE(SUM((SELECT SUM(amount) FROM money.payment WHERE xact = lt.id)),0)) > ? 
41         SQL
42
43         my @l_ids;
44         for my $l (@loc) {
45                 my $sth = money::collections_tracker->db_Main->prepare($SQL);
46                 $sth->execute(uc($l), $age, uc($l), $amount );
47                 while (my $row = $sth->fetchrow_hashref) {
48                         #$row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
49                         $client->respond( $row );
50                 }
51         }
52         return undef;
53 }
54 __PACKAGE__->register_method(
55         method          => 'new_collections',
56         api_name        => 'open-ils.storage.money.collections.users_of_interest',
57         stream          => 1,
58         argc            => 3,
59 );
60
61 sub active_in_collections {
62         my $self = shift;
63         my $client = shift;
64         my $startdate = shift;
65         my $enddate = shift;
66         my @loc = @_;
67
68         my $mct = money::collections_tracker->table;
69         my $mb = money::billing->table;
70         my $circ = action::circulation->table;
71         my $mg = money::grocery->table;
72         my $descendants = "actor.org_unit_descendants((select id from actor.org_unit where shortname = ?))";
73
74         my $SQL = <<"   SQL";
75                 SELECT  lt.usr,
76                         MAX(bl.billing_ts) AS last_pertinent_billing,
77                         MAX(pm.payment_ts) AS last_pertinent_payment
78                   FROM  ( SELECT id,usr,billing_location AS location FROM money.grocery
79                                 UNION ALL
80                           SELECT id,usr,circ_lib AS location FROM action.circulation ) AS lt
81                         JOIN $descendants d ON (lt.location = d.id)
82                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
83                         LEFT JOIN money.billing bl ON (lt.id = bl.xact)
84                         LEFT JOIN money.payment pm ON (lt.id = pm.xact)
85                   WHERE cl.location IN ((SELECT id FROM $descendants))
86                         AND (   bl.billing_ts between ? and ?
87                                 OR pm.payment_ts between ? and ? )
88                   GROUP BY 1
89         SQL
90
91         my @l_ids;
92         for my $l (@loc) {
93                 my $sth = money::collections_tracker->db_Main->prepare($SQL);
94                 $sth->execute(uc($l), uc($l), $startdate, $enddate, $startdate, $enddate );
95                 while (my $row = $sth->fetchrow_hashref) {
96                         $row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
97                         $client->respond( $row );
98                 }
99         }
100         return undef;
101 }
102 __PACKAGE__->register_method(
103         method          => 'active_in_collections',
104         api_name        => 'open-ils.storage.money.collections.users_with_activity',
105         stream          => 1,
106         argc            => 3,
107 );
108
109 sub ou_desk_payments {
110         my $self = shift;
111         my $client = shift;
112         my $lib = shift;
113         my $startdate = shift;
114         my $enddate = shift;
115
116         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
117         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
118         return undef unless ($lib =~ /^\d+$/o);
119
120         my $sql = <<"   SQL";
121
122 SELECT  *
123   FROM  crosstab(\$\$
124          SELECT ws.id,
125                 p.payment_type,
126                 SUM(COALESCE(p.amount,0.0))
127           FROM  money.desk_payment_view p
128                 JOIN actor.workstation ws ON (ws.id = p.cash_drawer)
129           WHERE p.payment_ts >= '$startdate'
130                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
131                 AND p.voided IS FALSE
132                 AND ws.owning_lib = $lib
133          GROUP BY 1, 2
134          ORDER BY 1,2
135         \$\$) AS X(
136           workstation int,
137           cash_payment numeric(10,2),
138           check_payment numeric(10,2),
139           credit_card_payment numeric(10,2) );
140
141         SQL
142
143         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
144
145         for my $r (@$rows) {
146                 my $x = new Fieldmapper::money::workstation_payment_summary;
147                 $x->workstation( actor::workstation->retrieve($$r[0])->to_fieldmapper );
148                 $x->cash_payment($$r[1]);
149                 $x->check_payment($$r[2]);
150                 $x->credit_card_payment($$r[3]);
151
152                 $client->respond($x);
153         }
154
155         return undef;
156 }
157 __PACKAGE__->register_method(
158         method          => 'ou_desk_payments',
159         api_name        => 'open-ils.storage.money.org_unit.desk_payments',
160         stream          => 1,
161         argc            => 3,
162 );
163
164 sub ou_user_payments {
165         my $self = shift;
166         my $client = shift;
167         my $lib = shift;
168         my $startdate = shift;
169         my $enddate = shift;
170
171         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
172         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
173         return undef unless ($lib =~ /^\d+$/o);
174
175         my $sql = <<"   SQL";
176
177 SELECT  *
178   FROM  crosstab(\$\$
179          SELECT au.id,
180                 p.payment_type,
181                 SUM(COALESCE(p.amount,0.0))
182           FROM  money.bnm_payment_view p
183                 JOIN actor.usr au ON (au.id = p.accepting_usr)
184           WHERE p.payment_ts >= '$startdate'
185                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
186                 AND p.voided IS FALSE
187                 AND au.home_ou = $lib
188          GROUP BY 1, 2
189          ORDER BY 1,2
190         \$\$) AS X(
191           usr int,
192           forgive_payment numeric(10,2),
193           work_payment numeric(10,2),
194           credit_payment numeric(10,2) );
195
196         SQL
197
198         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
199
200         for my $r (@$rows) {
201                 my $x = new Fieldmapper::money::user_payment_summary;
202                 $x->usr( actor::user->retrieve($$r[0])->to_fieldmapper );
203                 $x->forgive_payment($$r[1]);
204                 $x->work_payment($$r[2]);
205                 $x->credit_payment($$r[3]);
206
207                 $client->respond($x);
208         }
209
210         return undef;
211 }
212 __PACKAGE__->register_method(
213         method          => 'ou_user_payments',
214         api_name        => 'open-ils.storage.money.org_unit.user_payments',
215         stream          => 1,
216         argc            => 3,
217 );
218
219
220 1;