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