]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm
using a more direct pivot query for payment summaries -- there is something amiss...
[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 _make_mbts {
8         my @xacts = @_;
9
10         my @mbts;
11         for my $x (@xacts) {
12                 my $s = new Fieldmapper::money::billable_transaction_summary;
13                 $s->id( $x->id );
14                 $s->usr( $x->usr );
15                 $s->xact_start( $x->xact_start );
16                 $s->xact_finish( $x->xact_finish );
17
18                 my $to = 0;
19                 my $lb = undef;
20                 for my $b ($x->billings) {
21                         next if ($b->voided);
22                         #$log->debug( "billing is ".$b->amount, DEBUG );
23                         $to += ($b->amount * 100);
24                         $lb ||= $b->billing_ts;
25                         if ($b->billing_ts ge $lb) {
26                                 $lb = $b->billing_ts;
27                                 $s->last_billing_note($b->note);
28                                 $s->last_billing_ts($b->billing_ts);
29                                 $s->last_billing_type($b->billing_type);
30                         }
31                 }
32
33                 $s->total_owed( sprintf('%0.2f', ($to) / 100 ) );
34
35                 my $tp = 0;
36                 my $lp = undef;
37                 for my $p ($x->payments) {
38                         #$log->debug( "payment is ".$p->amount." voided = ".$p->voided, DEBUG );
39                         next if ($p->voided eq 't');
40                         $tp += ($p->amount * 100);
41                         $lp ||= $p->payment_ts;
42                         if ($p->payment_ts ge $lp) {
43                                 $lp = $p->payment_ts;
44                                 $s->last_payment_note($p->note);
45                                 $s->last_payment_ts($p->payment_ts);
46                                 $s->last_payment_type($p->payment_type);
47                         }
48                 }
49                 $s->total_paid( sprintf('%0.2f', ($tp) / 100 ) );
50
51                 $s->balance_owed( sprintf('%0.2f', (($to) - ($tp)) / 100) );
52                 #$log->debug( "balance of ".$x->id." == ".$s->balance_owed, DEBUG );
53
54                 $s->xact_type( 'grocery' ) if (money::grocery->retrieve($x->id));
55                 $s->xact_type( 'circulation' ) if (action::circulation->retrieve($x->id));
56
57                 push @mbts, $s;
58         }
59
60         return @mbts;
61 }
62
63 sub search_mbts {
64         my $self = shift;
65         my $client = shift;
66         my $search = shift;
67
68         my @xacts = money::billable_transaction->search_where( $search );
69         $client->respond( $_ ) for (_make_mbts(@xacts));
70
71         return undef;
72 }
73 __PACKAGE__->register_method(
74         method          => 'search_mbts',
75         api_name        => 'open-ils.storage.money.billable_transaction.summary.search',
76         stream          => 1,
77         argc            => 1,
78 );
79
80 sub search_ous {
81         my $self = shift;
82         my $client = shift;
83         my $usr = shift;
84
85         my @xacts = $self->method_lookup( 'open-ils.storage.money.billable_transaction.summary.search' )->run( { usr => $usr, xact_finish => undef } );
86
87         my ($total,$owed,$paid) = (0.0,0.0,0.0);
88         for my $x (@xacts) {
89                 $total += $x->total_owed;
90                 $owed += $x->balance_owed;
91                 $paid += $x->total_paid;
92         }
93
94         my $ous = Fieldmapper::money::open_user_summary->new;
95         $ous->usr( $usr );
96         $ous->total_paid( sprintf('%0.2f', $paid) );
97         $ous->total_owed( sprintf('%0.2f', $total) );
98         $ous->balance_owed( sprintf('%0.2f', $owed) );
99
100         return $ous;
101 }
102 __PACKAGE__->register_method(
103         method          => 'search_ous',
104         api_name        => 'open-ils.storage.money.open_user_summary.search',
105         argc            => 1,
106 );
107
108
109 sub new_collections {
110         my $self = shift;
111         my $client = shift;
112         my $age = shift;
113         my $amount = shift;
114         my @loc = @_;
115
116         my $mct = money::collections_tracker->table;
117         my $mb = money::billing->table;
118         my $circ = action::circulation->table;
119         my $mg = money::grocery->table;
120         my $descendants = "actor.org_unit_descendants((select id from actor.org_unit where shortname = ?))";
121
122         my $SQL = <<"   SQL";
123
124 select
125         usr,
126         MAX(last_billing) as last_pertinent_billing,
127         SUM(total_billing) - SUM(COALESCE(p.amount,0)) as threshold_amount
128   from  (select
129                 x.id,
130                 x.usr,
131                 MAX(b.billing_ts) as last_billing,
132                 SUM(b.amount) AS total_billing
133           from  action.circulation x
134                 left join money.collections_tracker c ON (c.usr = x.usr AND c.location = ?)
135                 join money.billing b on (b.xact = x.id)
136           where x.xact_finish is null
137                 and c.id is null
138                 and x.circ_lib in (XX)
139                 and b.billing_ts < current_timestamp - ? * '1 day'::interval
140                 and not b.voided
141           group by 1,2
142
143                   union all
144
145          select
146                 x.id,
147                 x.usr,
148                 MAX(b.billing_ts) as last_billing,
149                 SUM(b.amount) AS total_billing
150           from  money.grocery x
151                 left join money.collections_tracker c ON (c.usr = x.usr AND c.location = ?)
152                 join money.billing b on (b.xact = x.id)
153           where x.xact_finish is null
154                 and c.id is null
155                 and x.billing_location in (XX)
156                 and b.billing_ts < current_timestamp - ? * '1 day'::interval
157                 and not b.voided
158           group by 1,2
159         ) full_list
160         left join money.payment p on (full_list.id = p.xact)
161   group by 1
162   having SUM(total_billing) - SUM(COALESCE(p.amount,0)) > ?
163 ;
164         SQL
165
166         my @l_ids;
167         for my $l (@loc) {
168                 my ($org) = actor::org_unit->search( shortname => uc($l) );
169                 next unless $org;
170
171                 my $o_list = actor::org_unit->db_Main->selectcol_arrayref( "SELECT id FROM actor.org_unit_descendants(?);", {}, $org->id );
172                 next unless (@$o_list);
173
174                 my $o_txt = join ',' => @$o_list;
175
176                 (my $real_sql = $SQL) =~ s/XX/$o_txt/gsm;
177
178                 my $sth = money::collections_tracker->db_Main->prepare($real_sql);
179                 $sth->execute( $org->id, $age, $org->id, $age, $amount );
180
181                 while (my $row = $sth->fetchrow_hashref) {
182                         #$row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
183                         $client->respond( $row );
184                 }
185         }
186         return undef;
187 }
188 __PACKAGE__->register_method(
189         method          => 'new_collections',
190         api_name        => 'open-ils.storage.money.collections.users_of_interest',
191         stream          => 1,
192         argc            => 3,
193 );
194
195 sub active_in_collections {
196         my $self = shift;
197         my $client = shift;
198         my $startdate = shift;
199         my $enddate = shift;
200         my @loc = @_;
201
202         my $mct = money::collections_tracker->table;
203         my $mb = money::billing->table;
204         my $circ = action::circulation->table;
205         my $mg = money::grocery->table;
206
207         my $SQL = <<"   SQL";
208 SELECT  usr,
209         MAX(last_pertinent_billing) AS last_pertinent_billing,
210         MAX(last_pertinent_payment) AS last_pertinent_payment
211   FROM  (
212                 SELECT  lt.usr,
213                         MAX(bl.billing_ts) AS last_pertinent_billing,
214                         NULL::TIMESTAMPTZ AS last_pertinent_payment
215                   FROM  money.grocery lt
216                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
217                         JOIN money.billing bl ON (lt.id = bl.xact)
218                   WHERE cl.location = ?
219                         AND lt.billing_location IN (XX)
220                         AND bl.billing_ts BETWEEN ? AND ?
221                   GROUP BY 1
222
223                                 UNION ALL
224                 SELECT  lt.usr,
225                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
226                         MAX(pm.payment_ts) AS last_pertinent_payment
227                   FROM  money.grocery lt
228                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
229                         JOIN money.payment pm ON (lt.id = pm.xact)
230                   WHERE cl.location = ?
231                         AND lt.billing_location IN (XX)
232                         AND pm.payment_ts BETWEEN ? AND ?
233                   GROUP BY 1
234
235                                 UNION ALL
236                 SELECT  lt.usr,
237                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
238                         NULL::TIMESTAMPTZ AS last_pertinent_payment
239                   FROM  action.circulation lt
240                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
241                   WHERE cl.location = ?
242                         AND lt.circ_lib IN (XX)
243                         AND lt.checkin_time BETWEEN ? AND ?
244                   GROUP BY 1
245
246                                 UNION ALL
247                 SELECT  lt.usr,
248                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
249                         MAX(pm.payment_ts) AS last_pertinent_payment
250                   FROM  action.circulation lt
251                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
252                         JOIN money.payment pm ON (lt.id = pm.xact)
253                   WHERE cl.location = ?
254                         AND lt.circ_lib IN (XX)
255                         AND pm.payment_ts BETWEEN ? AND ?
256                   GROUP BY 1
257
258                                 UNION ALL
259                 SELECT  lt.usr,
260                         MAX(bl.billing_ts) AS last_pertinent_billing,
261                         NULL::TIMESTAMPTZ AS last_pertinent_payment
262                   FROM  action.circulation lt
263                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
264                         JOIN money.billing bl ON (lt.id = bl.xact)
265                   WHERE cl.location = ?
266                         AND lt.circ_lib IN (XX)
267                         AND bl.billing_ts BETWEEN ? AND ?
268                   GROUP BY 1
269         ) foo
270   GROUP BY 1
271 ;
272         SQL
273
274         my @l_ids;
275         for my $l (@loc) {
276                 my ($org) = actor::org_unit->search( shortname => uc($l) );
277                 next unless $org;
278
279                 my $o_list = actor::org_unit->db_Main->selectcol_arrayref( "SELECT id FROM actor.org_unit_descendants(?);", {}, $org->id );
280                 next unless (@$o_list);
281
282                 my $o_txt = join ',' => @$o_list;
283
284                 (my $real_sql = $SQL) =~ s/XX/$o_txt/gsm;
285
286                 my $sth = money::collections_tracker->db_Main->prepare($real_sql);
287                 $sth->execute(
288                         $org->id, $startdate, $enddate,
289                         $org->id, $startdate, $enddate,
290                         $org->id, $startdate, $enddate,
291                         $org->id, $startdate, $enddate,
292                         $org->id, $startdate, $enddate
293                 );
294
295                 while (my $row = $sth->fetchrow_hashref) {
296                         $row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
297                         $client->respond( $row );
298                 }
299         }
300         return undef;
301 }
302 __PACKAGE__->register_method(
303         method          => 'active_in_collections',
304         api_name        => 'open-ils.storage.money.collections.users_with_activity',
305         stream          => 1,
306         argc            => 3,
307 );
308
309 sub ou_desk_payments {
310         my $self = shift;
311         my $client = shift;
312         my $lib = shift;
313         my $startdate = shift;
314         my $enddate = shift;
315
316         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
317         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
318         return undef unless ($lib =~ /^\d+$/o);
319
320         my $sql = <<"   SQL";
321
322         SELECT  ws.id as workstation,
323                 SUM( CASE WHEN p.payment_type = 'cash_payment' THEN p.amount ELSE 0.0 END ) as cash_payment,
324                 SUM( CASE WHEN p.payment_type = 'check_payment' THEN p.amount ELSE 0.0 END ) as check_payment,
325                 SUM( CASE WHEN p.payment_type = 'credit_card_payment' THEN p.amount ELSE 0.0 END ) as credit_card_payment
326           FROM  money.desk_payment_view p
327                 JOIN actor.workstation ws ON (ws.id = p.cash_drawer)
328           WHERE p.payment_ts >= '$startdate'
329                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
330                 AND p.voided IS FALSE
331                 AND ws.owning_lib = $lib
332          GROUP BY 1
333          ORDER BY 1;
334
335         SQL
336
337         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
338
339         for my $r (@$rows) {
340                 my $x = new Fieldmapper::money::workstation_payment_summary;
341                 $x->workstation( actor::workstation->retrieve($$r[0])->to_fieldmapper );
342                 $x->cash_payment($$r[1]);
343                 $x->check_payment($$r[2]);
344                 $x->credit_card_payment($$r[3]);
345
346                 $client->respond($x);
347         }
348
349         return undef;
350 }
351 __PACKAGE__->register_method(
352         method          => 'ou_desk_payments',
353         api_name        => 'open-ils.storage.money.org_unit.desk_payments',
354         stream          => 1,
355         argc            => 3,
356 );
357
358 sub ou_user_payments {
359         my $self = shift;
360         my $client = shift;
361         my $lib = shift;
362         my $startdate = shift;
363         my $enddate = shift;
364
365         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
366         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
367         return undef unless ($lib =~ /^\d+$/o);
368
369         my $sql = <<"   SQL";
370
371         SELECT  au.id as usr,
372                 SUM( CASE WHEN p.payment_type = 'forgive_payment' THEN p.amount ELSE 0.0 END ) as forgive_payment,
373                 SUM( CASE WHEN p.payment_type = 'work_payment' THEN p.amount ELSE 0.0 END ) as work_payment,
374                 SUM( CASE WHEN p.payment_type = 'credit_payment' THEN p.amount ELSE 0.0 END ) as credit_payment
375           FROM  money.bnm_payment_view p
376                 JOIN actor.usr au ON (au.id = p.accepting_usr)
377           WHERE p.payment_ts >= '$startdate'
378                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
379                 AND p.voided IS FALSE
380                 AND au.home_ou = $lib
381                 AND p.payment_type IN ('credit_payment','forgive_payment','work_payment')
382          GROUP BY 1
383          ORDER BY 1;
384
385         SQL
386
387         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
388
389         for my $r (@$rows) {
390                 my $x = new Fieldmapper::money::user_payment_summary;
391                 $x->usr( actor::user->retrieve($$r[0])->to_fieldmapper );
392                 $x->forgive_payment($$r[1]);
393                 $x->work_payment($$r[2]);
394                 $x->credit_payment($$r[3]);
395
396                 $client->respond($x);
397         }
398
399         return undef;
400 }
401 __PACKAGE__->register_method(
402         method          => 'ou_user_payments',
403         api_name        => 'open-ils.storage.money.org_unit.user_payments',
404         stream          => 1,
405         argc            => 3,
406 );
407
408
409 1;