]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm
add support for "no billings or payments, but had checkins"
[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, 'g'::char AS x_type FROM money.grocery
79                                 UNION ALL
80                           SELECT id,usr,circ_lib AS location, 'c'::char AS x_type FROM action.circulation
81                                 UNION ALL
82                           SELECT id,usr,circ_lib AS location, 'i'::char AS x_type FROM action.circulation
83                             WHERE checkin_time between ? and ? ) AS lt
84                         JOIN $descendants d ON (lt.location = d.id)
85                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
86                         LEFT JOIN money.billing bl ON (lt.id = bl.xact)
87                         LEFT JOIN money.payment pm ON (lt.id = pm.xact)
88                   WHERE bl.billing_ts between ? and ?
89                         OR pm.payment_ts between ? and ?
90                         OR lt.x_type = 'i'::char
91                   GROUP BY 1
92         SQL
93
94         my @l_ids;
95         for my $l (@loc) {
96                 my $sth = money::collections_tracker->db_Main->prepare($SQL);
97                 $sth->execute( $startdate, $enddate, uc($l), $startdate, $enddate, $startdate, $enddate );
98                 while (my $row = $sth->fetchrow_hashref) {
99                         $row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
100                         $client->respond( $row );
101                 }
102         }
103         return undef;
104 }
105 __PACKAGE__->register_method(
106         method          => 'active_in_collections',
107         api_name        => 'open-ils.storage.money.collections.users_with_activity',
108         stream          => 1,
109         argc            => 3,
110 );
111
112 sub ou_desk_payments {
113         my $self = shift;
114         my $client = shift;
115         my $lib = shift;
116         my $startdate = shift;
117         my $enddate = shift;
118
119         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
120         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
121         return undef unless ($lib =~ /^\d+$/o);
122
123         my $sql = <<"   SQL";
124
125 SELECT  *
126   FROM  crosstab(\$\$
127          SELECT ws.id,
128                 p.payment_type,
129                 SUM(COALESCE(p.amount,0.0))
130           FROM  money.desk_payment_view p
131                 JOIN actor.workstation ws ON (ws.id = p.cash_drawer)
132           WHERE p.payment_ts >= '$startdate'
133                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
134                 AND p.voided IS FALSE
135                 AND ws.owning_lib = $lib
136          GROUP BY 1, 2
137          ORDER BY 1,2
138         \$\$) AS X(
139           workstation int,
140           cash_payment numeric(10,2),
141           check_payment numeric(10,2),
142           credit_card_payment numeric(10,2) );
143
144         SQL
145
146         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
147
148         for my $r (@$rows) {
149                 my $x = new Fieldmapper::money::workstation_payment_summary;
150                 $x->workstation( actor::workstation->retrieve($$r[0])->to_fieldmapper );
151                 $x->cash_payment($$r[1]);
152                 $x->check_payment($$r[2]);
153                 $x->credit_card_payment($$r[3]);
154
155                 $client->respond($x);
156         }
157
158         return undef;
159 }
160 __PACKAGE__->register_method(
161         method          => 'ou_desk_payments',
162         api_name        => 'open-ils.storage.money.org_unit.desk_payments',
163         stream          => 1,
164         argc            => 3,
165 );
166
167 sub ou_user_payments {
168         my $self = shift;
169         my $client = shift;
170         my $lib = shift;
171         my $startdate = shift;
172         my $enddate = shift;
173
174         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
175         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
176         return undef unless ($lib =~ /^\d+$/o);
177
178         my $sql = <<"   SQL";
179
180 SELECT  *
181   FROM  crosstab(\$\$
182          SELECT au.id,
183                 p.payment_type,
184                 SUM(COALESCE(p.amount,0.0))
185           FROM  money.bnm_payment_view p
186                 JOIN actor.usr au ON (au.id = p.accepting_usr)
187           WHERE p.payment_ts >= '$startdate'
188                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
189                 AND p.voided IS FALSE
190                 AND au.home_ou = $lib
191          GROUP BY 1, 2
192          ORDER BY 1,2
193         \$\$) AS X(
194           usr int,
195           forgive_payment numeric(10,2),
196           work_payment numeric(10,2),
197           credit_payment numeric(10,2) );
198
199         SQL
200
201         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
202
203         for my $r (@$rows) {
204                 my $x = new Fieldmapper::money::user_payment_summary;
205                 $x->usr( actor::user->retrieve($$r[0])->to_fieldmapper );
206                 $x->forgive_payment($$r[1]);
207                 $x->work_payment($$r[2]);
208                 $x->credit_payment($$r[3]);
209
210                 $client->respond($x);
211         }
212
213         return undef;
214 }
215 __PACKAGE__->register_method(
216         method          => 'ou_user_payments',
217         api_name        => 'open-ils.storage.money.org_unit.user_payments',
218         stream          => 1,
219         argc            => 3,
220 );
221
222
223 1;