]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm
fixing collections calls
[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 1;