]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm
WHY ARE THEY DIFFERENT?!?! ... it works now
[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 += int($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', int($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 += int($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', int($tp) / 100 ) );
50
51                 $s->balance_owed( sprintf('%0.2f', int(int($to) - int($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
81 sub new_collections {
82         my $self = shift;
83         my $client = shift;
84         my $age = shift;
85         my $amount = shift;
86         my @loc = @_;
87
88         my $mct = money::collections_tracker->table;
89         my $mb = money::billing->table;
90         my $circ = action::circulation->table;
91         my $mg = money::grocery->table;
92         my $descendants = "actor.org_unit_descendants((select id from actor.org_unit where shortname = ?))";
93
94         my $SQL = <<"   SQL";
95                 SELECT  lt.usr,
96                         MAX(bl.billing_ts) AS last_pertinent_billing,
97                         SUM(bl.amount) - COALESCE(SUM((SELECT SUM(amount) FROM money.payment WHERE xact = lt.id)),0) AS threshold_amount
98                   FROM  ( SELECT id,usr,billing_location AS location FROM money.grocery
99                                 UNION ALL
100                           SELECT id,usr,circ_lib AS location FROM action.circulation ) AS lt
101                         JOIN $descendants d ON (lt.location = d.id)
102                         JOIN money.billing bl ON (lt.id = bl.xact AND bl.voided IS FALSE)
103                   WHERE AGE(bl.billing_ts) > ?
104                   GROUP BY lt.usr
105                   HAVING  SUM(
106                                 (SELECT COUNT(*)
107                                   FROM  money.collections_tracker
108                                   WHERE usr = lt.usr
109                                         AND location in (
110                                                 (SELECT id
111                                                   FROM  $descendants )
112                                         )
113                                 ) ) = 0
114                         AND (SUM(bl.amount) - COALESCE(SUM((SELECT SUM(amount) FROM money.payment WHERE xact = lt.id)),0)) > ? 
115         SQL
116
117         my @l_ids;
118         for my $l (@loc) {
119                 my $sth = money::collections_tracker->db_Main->prepare($SQL);
120                 $sth->execute(uc($l), $age, uc($l), $amount );
121                 while (my $row = $sth->fetchrow_hashref) {
122                         #$row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
123                         $client->respond( $row );
124                 }
125         }
126         return undef;
127 }
128 __PACKAGE__->register_method(
129         method          => 'new_collections',
130         api_name        => 'open-ils.storage.money.collections.users_of_interest',
131         stream          => 1,
132         argc            => 3,
133 );
134
135 sub active_in_collections {
136         my $self = shift;
137         my $client = shift;
138         my $startdate = shift;
139         my $enddate = shift;
140         my @loc = @_;
141
142         my $mct = money::collections_tracker->table;
143         my $mb = money::billing->table;
144         my $circ = action::circulation->table;
145         my $mg = money::grocery->table;
146         my $descendants = "actor.org_unit_descendants((select id from actor.org_unit where shortname = ?))";
147
148         my $SQL = <<"   SQL";
149                 SELECT  lt.usr,
150                         MAX(bl.billing_ts) AS last_pertinent_billing,
151                         MAX(pm.payment_ts) AS last_pertinent_payment
152                   FROM  ( SELECT id,usr,billing_location AS location, 'g'::char AS x_type FROM money.grocery
153                                 UNION ALL
154                           SELECT id,usr,circ_lib AS location, 'c'::char AS x_type FROM action.circulation
155                                 UNION ALL
156                           SELECT id,usr,circ_lib AS location, 'i'::char AS x_type FROM action.circulation
157                             WHERE checkin_time between ? and ? ) AS lt
158                         JOIN $descendants d ON (lt.location = d.id)
159                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
160                         LEFT JOIN money.billing bl ON (lt.id = bl.xact)
161                         LEFT JOIN money.payment pm ON (lt.id = pm.xact)
162                   WHERE bl.billing_ts between ? and ?
163                         OR pm.payment_ts between ? and ?
164                         OR lt.x_type = 'i'::char
165                   GROUP BY 1
166         SQL
167
168         my @l_ids;
169         for my $l (@loc) {
170                 my $sth = money::collections_tracker->db_Main->prepare($SQL);
171                 $sth->execute( $startdate, $enddate, uc($l), $startdate, $enddate, $startdate, $enddate );
172                 while (my $row = $sth->fetchrow_hashref) {
173                         $row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
174                         $client->respond( $row );
175                 }
176         }
177         return undef;
178 }
179 __PACKAGE__->register_method(
180         method          => 'active_in_collections',
181         api_name        => 'open-ils.storage.money.collections.users_with_activity',
182         stream          => 1,
183         argc            => 3,
184 );
185
186 sub ou_desk_payments {
187         my $self = shift;
188         my $client = shift;
189         my $lib = shift;
190         my $startdate = shift;
191         my $enddate = shift;
192
193         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
194         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
195         return undef unless ($lib =~ /^\d+$/o);
196
197         my $sql = <<"   SQL";
198
199 SELECT  *
200   FROM  crosstab(\$\$
201          SELECT ws.id,
202                 p.payment_type,
203                 SUM(COALESCE(p.amount,0.0))
204           FROM  money.desk_payment_view p
205                 JOIN actor.workstation ws ON (ws.id = p.cash_drawer)
206           WHERE p.payment_ts >= '$startdate'
207                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
208                 AND p.voided IS FALSE
209                 AND ws.owning_lib = $lib
210          GROUP BY 1, 2
211          ORDER BY 1,2
212         \$\$) AS X(
213           workstation int,
214           cash_payment numeric(10,2),
215           check_payment numeric(10,2),
216           credit_card_payment numeric(10,2) );
217
218         SQL
219
220         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
221
222         for my $r (@$rows) {
223                 my $x = new Fieldmapper::money::workstation_payment_summary;
224                 $x->workstation( actor::workstation->retrieve($$r[0])->to_fieldmapper );
225                 $x->cash_payment($$r[1]);
226                 $x->check_payment($$r[2]);
227                 $x->credit_card_payment($$r[3]);
228
229                 $client->respond($x);
230         }
231
232         return undef;
233 }
234 __PACKAGE__->register_method(
235         method          => 'ou_desk_payments',
236         api_name        => 'open-ils.storage.money.org_unit.desk_payments',
237         stream          => 1,
238         argc            => 3,
239 );
240
241 sub ou_user_payments {
242         my $self = shift;
243         my $client = shift;
244         my $lib = shift;
245         my $startdate = shift;
246         my $enddate = shift;
247
248         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
249         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
250         return undef unless ($lib =~ /^\d+$/o);
251
252         my $sql = <<"   SQL";
253
254 SELECT  *
255   FROM  crosstab(\$\$
256          SELECT au.id,
257                 p.payment_type,
258                 SUM(COALESCE(p.amount,0.0))
259           FROM  money.bnm_payment_view p
260                 JOIN actor.usr au ON (au.id = p.accepting_usr)
261           WHERE p.payment_ts >= '$startdate'
262                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
263                 AND p.voided IS FALSE
264                 AND au.home_ou = $lib
265                 AND p.payment_type IN ('credit_payment','forgive_payment','work_payment')
266          GROUP BY 1, 2
267          ORDER BY 1,2
268         \$\$) AS X(
269           usr int,
270           forgive_payment numeric(10,2),
271           work_payment numeric(10,2),
272           credit_payment numeric(10,2) );
273
274         SQL
275
276         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
277
278         for my $r (@$rows) {
279                 my $x = new Fieldmapper::money::user_payment_summary;
280                 $x->usr( actor::user->retrieve($$r[0])->to_fieldmapper );
281                 $x->forgive_payment($$r[1]);
282                 $x->work_payment($$r[2]);
283                 $x->credit_payment($$r[3]);
284
285                 $client->respond($x);
286         }
287
288         return undef;
289 }
290 __PACKAGE__->register_method(
291         method          => 'ou_user_payments',
292         api_name        => 'open-ils.storage.money.org_unit.user_payments',
293         stream          => 1,
294         argc            => 3,
295 );
296
297
298 1;