]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/money.pm
LP#1394989: Make users_of_interest test for defined actor.usr.card values
[Evergreen.git] / Open-ILS / src / perlmods / lib / 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                 if (action::circulation->retrieve($x->id)) {
55                     $s->xact_type( 'circulation' );
56                 } elsif (money::grocery->retrieve($x->id)) {
57                     $s->xact_type( 'grocery' );
58                 } elsif (booking::reservation->retrieve($x->id)) {
59                     $s->xact_type( 'reservation' );
60                 }
61
62                 push @mbts, $s;
63         }
64
65         return @mbts;
66 }
67
68 sub search_mbts {
69     my $self = shift;
70     my $client = shift;
71     my $search = shift;
72
73     my @xacts = money::billable_transaction->search_where( $search );
74     $client->respond( $_ ) for (_make_mbts(@xacts));
75
76     return undef;
77 }
78 __PACKAGE__->register_method(
79     method      => 'search_mbts',
80     api_name    => 'open-ils.storage.money.billable_transaction.summary.search',
81     stream      => 1,
82     argc        => 1,
83 );
84
85 sub search_ous {
86     my $self = shift;
87     my $client = shift;
88     my $usr = shift;
89
90     my @xacts = $self->method_lookup( 'open-ils.storage.money.billable_transaction.summary.search' )->run( { usr => $usr, xact_finish => undef } );
91
92     my ($total,$owed,$paid) = (0.0,0.0,0.0);
93     for my $x (@xacts) {
94         $total += $x->total_owed;
95         $owed += $x->balance_owed;
96         $paid += $x->total_paid;
97     }
98
99     my $ous = Fieldmapper::money::open_user_summary->new;
100     $ous->usr( $usr );
101     $ous->total_paid( sprintf('%0.2f', $paid) );
102     $ous->total_owed( sprintf('%0.2f', $total) );
103     $ous->balance_owed( sprintf('%0.2f', $owed) );
104
105     return $ous;
106 }
107 __PACKAGE__->register_method(
108     method      => 'search_ous',
109     api_name    => 'open-ils.storage.money.open_user_summary.search',
110     argc        => 1,
111 );
112
113
114 sub new_collections {
115     my $self = shift;
116     my $client = shift;
117     my $age = shift;
118     my $amount = shift;
119     my @loc = @_;
120
121     my $mct = money::collections_tracker->table;
122     my $mb = money::billing->table;
123     my $circ = action::circulation->table;
124     my $mg = money::grocery->table;
125     my $res = booking::reservation->table;
126
127     my $SQL = <<"    SQL";
128
129 select
130         usr,
131         MAX(last_billing) as last_pertinent_billing,
132         SUM(total_billing) - SUM(COALESCE(p.amount,0)) as threshold_amount
133   from  (select
134                 x.id,
135                 x.usr,
136                 MAX(b.billing_ts) as last_billing,
137                 SUM(b.amount) AS total_billing
138           from  action.circulation x
139                 left join money.collections_tracker c ON (c.usr = x.usr AND c.location = ?)
140                 join money.billing b on (b.xact = x.id)
141                 LEFT JOIN actor.usr_setting set ON (set.usr = x.usr and set.name='circ.collections.exempt' and set.value = 'true')
142           where x.xact_finish is null
143                 and c.id is null
144                 and x.circ_lib in (XX)
145                 and b.billing_ts < current_timestamp - ? * '1 day'::interval
146                 and not b.voided
147                 and set.id IS NULL
148           group by 1,2
149
150                   union all
151
152          select
153                 x.id,
154                 x.usr,
155                 MAX(b.billing_ts) as last_billing,
156                 SUM(b.amount) AS total_billing
157           from  money.grocery x
158                 left join money.collections_tracker c ON (c.usr = x.usr AND c.location = ?)
159                 join money.billing b on (b.xact = x.id)
160                 LEFT JOIN actor.usr_setting set ON (set.usr = x.usr and set.name='circ.collections.exempt' and set.value = 'true')
161           where x.xact_finish is null
162                 and c.id is null
163                 and x.billing_location in (XX)
164                 and b.billing_ts < current_timestamp - ? * '1 day'::interval
165                 and not b.voided
166                 and set.id IS NULL
167           group by 1,2
168
169                   union all
170
171          select
172                 x.id,
173                 x.usr,
174                 MAX(b.billing_ts) as last_billing,
175                 SUM(b.amount) AS total_billing
176           from  booking.reservation x
177                 left join money.collections_tracker c ON (c.usr = x.usr AND c.location = ?)
178                 join money.billing b on (b.xact = x.id)
179                 LEFT JOIN actor.usr_setting set ON (set.usr = x.usr and set.name='circ.collections.exempt' and set.value = 'true')
180           where x.xact_finish is null
181                 and c.id is null
182                 and x.pickup_lib in (XX)
183                 and b.billing_ts < current_timestamp - ? * '1 day'::interval
184                 and not b.voided
185                 and set.id IS NULL
186           group by 1,2
187         ) full_list
188         left join money.payment p on (full_list.id = p.xact)
189   group by 1
190   having SUM(total_billing) - SUM(COALESCE(p.amount,0)) > ?
191 ;
192     SQL
193
194     my @l_ids;
195     for my $l (@loc) {
196         my ($org) = actor::org_unit->search( shortname => uc($l) );
197         next unless $org;
198
199         my $o_list = actor::org_unit->db_Main->selectcol_arrayref( "SELECT id FROM actor.org_unit_descendants(?);", {}, $org->id );
200         next unless (@$o_list);
201
202         my $o_txt = join ',' => @$o_list;
203
204         (my $real_sql = $SQL) =~ s/XX/$o_txt/gsm;
205
206         my $sth = money::collections_tracker->db_Main->prepare($real_sql);
207         $sth->execute( $org->id, $age, $org->id, $age, $org->id, $age, $amount );
208
209         while (my $row = $sth->fetchrow_hashref) {
210             #$row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
211             $client->respond( $row );
212         }
213     }
214     return undef;
215 }
216 __PACKAGE__->register_method(
217     method      => 'new_collections',
218     api_name    => 'open-ils.storage.money.collections.users_of_interest',
219     stream      => 1,
220     argc        => 3,
221 );
222
223 sub active_in_collections {
224     my $self = shift;
225     my $client = shift;
226     my $startdate = shift;
227     my $enddate = shift;
228     my @loc = @_;
229
230     my $mct = money::collections_tracker->table;
231     my $mb = money::billing->table;
232     my $circ = action::circulation->table;
233     my $mg = money::grocery->table;
234
235     my $SQL = <<"    SQL";
236 SELECT  usr,
237         MAX(last_pertinent_billing) AS last_pertinent_billing,
238         MAX(last_pertinent_payment) AS last_pertinent_payment
239   FROM  (
240                 SELECT  lt.usr,
241                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
242                         NULL::TIMESTAMPTZ AS last_pertinent_payment
243                   FROM  booking.reservation lt
244                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
245                         JOIN money.billing bl ON (lt.id = bl.xact)
246                   WHERE cl.location = ?
247                         AND lt.pickup_lib IN (XX)
248                         AND bl.void_time BETWEEN ? AND ?
249                   GROUP BY 1
250
251                                 UNION ALL
252                 SELECT  lt.usr,
253                         MAX(bl.billing_ts) AS last_pertinent_billing,
254                         NULL::TIMESTAMPTZ AS last_pertinent_payment
255                   FROM  booking.reservation lt
256                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
257                         JOIN money.billing bl ON (lt.id = bl.xact)
258                   WHERE cl.location = ?
259                         AND lt.pickup_lib IN (XX)
260                         AND bl.billing_ts BETWEEN ? AND ?
261                   GROUP BY 1
262
263                                 UNION ALL
264                 SELECT  lt.usr,
265                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
266                         MAX(pm.payment_ts) AS last_pertinent_payment
267                   FROM  booking.reservation lt
268                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
269                         JOIN money.payment pm ON (lt.id = pm.xact)
270                   WHERE cl.location = ?
271                         AND lt.pickup_lib IN (XX)
272                         AND pm.payment_ts BETWEEN ? AND ?
273                   GROUP BY 1
274
275                                 UNION ALL
276                  SELECT  lt.usr,
277                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
278                         NULL::TIMESTAMPTZ AS last_pertinent_payment
279                   FROM  money.grocery lt
280                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
281                         JOIN money.billing bl ON (lt.id = bl.xact)
282                   WHERE cl.location = ?
283                         AND lt.billing_location IN (XX)
284                         AND bl.void_time BETWEEN ? AND ?
285                   GROUP BY 1
286
287                                 UNION ALL
288                 SELECT  lt.usr,
289                         MAX(bl.billing_ts) AS last_pertinent_billing,
290                         NULL::TIMESTAMPTZ AS last_pertinent_payment
291                   FROM  money.grocery lt
292                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
293                         JOIN money.billing bl ON (lt.id = bl.xact)
294                   WHERE cl.location = ?
295                         AND lt.billing_location IN (XX)
296                         AND bl.billing_ts BETWEEN ? AND ?
297                   GROUP BY 1
298
299                                 UNION ALL
300                 SELECT  lt.usr,
301                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
302                         MAX(pm.payment_ts) AS last_pertinent_payment
303                   FROM  money.grocery lt
304                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
305                         JOIN money.payment pm ON (lt.id = pm.xact)
306                   WHERE cl.location = ?
307                         AND lt.billing_location IN (XX)
308                         AND pm.payment_ts BETWEEN ? AND ?
309                   GROUP BY 1
310
311                                 UNION ALL
312                 SELECT  lt.usr,
313                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
314                         NULL::TIMESTAMPTZ AS last_pertinent_payment
315                   FROM  action.circulation lt
316                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
317                   WHERE cl.location = ?
318                         AND lt.circ_lib IN (XX)
319                         AND lt.checkin_time BETWEEN ? AND ?
320                   GROUP BY 1
321
322                                 UNION ALL
323                 SELECT  lt.usr,
324                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
325                         MAX(pm.payment_ts) AS last_pertinent_payment
326                   FROM  action.circulation lt
327                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
328                         JOIN money.payment pm ON (lt.id = pm.xact)
329                   WHERE cl.location = ?
330                         AND lt.circ_lib IN (XX)
331                         AND pm.payment_ts BETWEEN ? AND ?
332                   GROUP BY 1
333
334                                 UNION ALL
335                 SELECT  lt.usr,
336                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
337                         NULL::TIMESTAMPTZ AS last_pertinent_payment
338                   FROM  action.circulation lt
339                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
340                         JOIN money.billing bl ON (lt.id = bl.xact)
341                   WHERE cl.location = ?
342                         AND lt.circ_lib IN (XX)
343                         AND bl.void_time BETWEEN ? AND ?
344                   GROUP BY 1
345
346                                 UNION ALL
347                 SELECT  lt.usr,
348                         MAX(bl.billing_ts) AS last_pertinent_billing,
349                         NULL::TIMESTAMPTZ AS last_pertinent_payment
350                   FROM  action.circulation lt
351                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
352                         JOIN money.billing bl ON (lt.id = bl.xact)
353                   WHERE cl.location = ?
354                         AND lt.circ_lib IN (XX)
355                         AND bl.billing_ts BETWEEN ? AND ?
356                   GROUP BY 1
357         ) foo
358   GROUP BY 1
359 ;
360     SQL
361
362     my @l_ids;
363     for my $l (@loc) {
364         my ($org) = actor::org_unit->search( shortname => uc($l) );
365         next unless $org;
366
367         my $o_list = actor::org_unit->db_Main->selectcol_arrayref( "SELECT id FROM actor.org_unit_descendants(?);", {}, $org->id );
368         next unless (@$o_list);
369
370         my $o_txt = join ',' => @$o_list;
371
372         (my $real_sql = $SQL) =~ s/XX/$o_txt/gsm;
373
374         my $sth = money::collections_tracker->db_Main->prepare($real_sql);
375         $sth->execute(
376             # reservation queries
377             $org->id, $startdate, $enddate,
378             $org->id, $startdate, $enddate,
379             $org->id, $startdate, $enddate,
380
381             # grocery queries
382             $org->id, $startdate, $enddate,
383             $org->id, $startdate, $enddate,
384             $org->id, $startdate, $enddate,
385
386             # circ queries
387             $org->id, $startdate, $enddate,
388             $org->id, $startdate, $enddate,
389             $org->id, $startdate, $enddate,
390             $org->id, $startdate, $enddate
391         );
392
393         while (my $row = $sth->fetchrow_hashref) {
394             $row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
395             $client->respond( $row );
396         }
397     }
398     return undef;
399 }
400 __PACKAGE__->register_method(
401     method      => 'active_in_collections',
402     api_name    => 'open-ils.storage.money.collections.users_with_activity',
403     stream      => 1,
404     argc        => 3,
405 );
406
407 sub ou_desk_payments {
408     my $self = shift;
409     my $client = shift;
410     my $lib = shift;
411     my $startdate = shift;
412     my $enddate = shift;
413
414     return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
415     return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
416     return undef unless ($lib =~ /^\d+$/o);
417
418     my $sql = <<"    SQL";
419
420     SELECT  ws.id as workstation,
421         SUM( CASE WHEN p.payment_type = 'cash_payment' THEN p.amount ELSE 0.0 END ) as cash_payment,
422         SUM( CASE WHEN p.payment_type = 'check_payment' THEN p.amount ELSE 0.0 END ) as check_payment,
423         SUM( CASE WHEN p.payment_type = 'credit_card_payment' THEN p.amount ELSE 0.0 END ) as credit_card_payment
424       FROM  money.desk_payment_view p
425         JOIN actor.workstation ws ON (ws.id = p.cash_drawer)
426       WHERE p.payment_ts >= '$startdate'
427         AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
428         AND p.voided IS FALSE
429         AND ws.owning_lib = $lib
430      GROUP BY 1
431      ORDER BY 1;
432
433     SQL
434
435     my $rows = money::payment->db_Main->selectall_arrayref( $sql );
436
437     for my $r (@$rows) {
438         my $x = new Fieldmapper::money::workstation_payment_summary;
439         $x->workstation( actor::workstation->retrieve($$r[0])->to_fieldmapper );
440         $x->cash_payment($$r[1]);
441         $x->check_payment($$r[2]);
442         $x->credit_card_payment($$r[3]);
443
444         $client->respond($x);
445     }
446
447     return undef;
448 }
449 __PACKAGE__->register_method(
450     method      => 'ou_desk_payments',
451     api_name    => 'open-ils.storage.money.org_unit.desk_payments',
452     stream      => 1,
453     argc        => 3,
454 );
455
456 sub ou_user_payments {
457     my $self = shift;
458     my $client = shift;
459     my $lib = shift;
460     my $startdate = shift;
461     my $enddate = shift;
462
463     return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
464     return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
465     return undef unless ($lib =~ /^\d+$/o);
466
467     my $sql = <<"    SQL";
468
469         SELECT  au.id as usr,
470         SUM( CASE WHEN p.payment_type = 'forgive_payment' THEN p.amount ELSE 0.0 END ) as forgive_payment,
471         SUM( CASE WHEN p.payment_type = 'work_payment' THEN p.amount ELSE 0.0 END ) as work_payment,
472         SUM( CASE WHEN p.payment_type = 'credit_payment' THEN p.amount ELSE 0.0 END ) as credit_payment,
473         SUM( CASE WHEN p.payment_type = 'goods_payment' THEN p.amount ELSE 0.0 END ) as goods_payment
474           FROM  money.bnm_payment_view p
475                 JOIN actor.usr au ON (au.id = p.accepting_usr)
476           WHERE p.payment_ts >= '$startdate'
477                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
478                 AND p.voided IS FALSE
479                 AND au.home_ou = $lib
480         AND p.payment_type IN ('credit_payment','forgive_payment','work_payment','goods_payment')
481          GROUP BY 1
482          ORDER BY 1;
483
484     SQL
485
486     my $rows = money::payment->db_Main->selectall_arrayref( $sql );
487
488     for my $r (@$rows) {
489         my $x = new Fieldmapper::money::user_payment_summary;
490         $x->usr( actor::user->retrieve($$r[0])->to_fieldmapper );
491         $x->forgive_payment($$r[1]);
492         $x->work_payment($$r[2]);
493         $x->credit_payment($$r[3]);
494         $x->goods_payment($$r[4]);
495
496         $client->respond($x);
497     }
498
499     return undef;
500 }
501 __PACKAGE__->register_method(
502     method      => 'ou_user_payments',
503     api_name    => 'open-ils.storage.money.org_unit.user_payments',
504     stream      => 1,
505     argc        => 3,
506 );
507
508 sub mark_unrecovered {
509     my $self = shift;
510     my $xact = shift;
511
512     my $x = money::billable_xact->retrieve($xact);
513     $x->unrecovered( 't' );
514     return $x->update;
515 }
516 __PACKAGE__->register_method(
517     method      => 'mark_unrecovered',
518     api_name    => 'open-ils.storage.money.billable_xact.mark_unrecovered',
519     argc        => 1,
520 );
521
522
523 1;