]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm
settings server controlled opt-in functionallity for non-home_ou transactions
[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                         NULL::TIMESTAMPTZ 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.void_time BETWEEN ? AND ?
221                   GROUP BY 1
222
223                                 UNION ALL
224                 SELECT  lt.usr,
225                         MAX(bl.billing_ts) AS last_pertinent_billing,
226                         NULL::TIMESTAMPTZ AS last_pertinent_payment
227                   FROM  money.grocery lt
228                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
229                         JOIN money.billing bl ON (lt.id = bl.xact)
230                   WHERE cl.location = ?
231                         AND lt.billing_location IN (XX)
232                         AND bl.billing_ts BETWEEN ? AND ?
233                   GROUP BY 1
234
235                                 UNION ALL
236                 SELECT  lt.usr,
237                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
238                         MAX(pm.payment_ts) AS last_pertinent_payment
239                   FROM  money.grocery lt
240                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
241                         JOIN money.payment pm ON (lt.id = pm.xact)
242                   WHERE cl.location = ?
243                         AND lt.billing_location IN (XX)
244                         AND pm.payment_ts BETWEEN ? AND ?
245                   GROUP BY 1
246
247                                 UNION ALL
248                 SELECT  lt.usr,
249                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
250                         NULL::TIMESTAMPTZ AS last_pertinent_payment
251                   FROM  action.circulation lt
252                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
253                   WHERE cl.location = ?
254                         AND lt.circ_lib IN (XX)
255                         AND lt.checkin_time BETWEEN ? AND ?
256                   GROUP BY 1
257
258                                 UNION ALL
259                 SELECT  lt.usr,
260                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
261                         MAX(pm.payment_ts) AS last_pertinent_payment
262                   FROM  action.circulation lt
263                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
264                         JOIN money.payment pm ON (lt.id = pm.xact)
265                   WHERE cl.location = ?
266                         AND lt.circ_lib IN (XX)
267                         AND pm.payment_ts BETWEEN ? AND ?
268                   GROUP BY 1
269
270                                 UNION ALL
271                 SELECT  lt.usr,
272                         NULL::TIMESTAMPTZ AS last_pertinent_billing,
273                         NULL::TIMESTAMPTZ AS last_pertinent_payment
274                   FROM  action.circulation lt
275                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
276                         JOIN money.billing bl ON (lt.id = bl.xact)
277                   WHERE cl.location = ?
278                         AND lt.circ_lib IN (XX)
279                         AND bl.void_time BETWEEN ? AND ?
280                   GROUP BY 1
281
282                                 UNION ALL
283                 SELECT  lt.usr,
284                         MAX(bl.billing_ts) AS last_pertinent_billing,
285                         NULL::TIMESTAMPTZ AS last_pertinent_payment
286                   FROM  action.circulation lt
287                         JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
288                         JOIN money.billing bl ON (lt.id = bl.xact)
289                   WHERE cl.location = ?
290                         AND lt.circ_lib IN (XX)
291                         AND bl.billing_ts BETWEEN ? AND ?
292                   GROUP BY 1
293         ) foo
294   GROUP BY 1
295 ;
296         SQL
297
298         my @l_ids;
299         for my $l (@loc) {
300                 my ($org) = actor::org_unit->search( shortname => uc($l) );
301                 next unless $org;
302
303                 my $o_list = actor::org_unit->db_Main->selectcol_arrayref( "SELECT id FROM actor.org_unit_descendants(?);", {}, $org->id );
304                 next unless (@$o_list);
305
306                 my $o_txt = join ',' => @$o_list;
307
308                 (my $real_sql = $SQL) =~ s/XX/$o_txt/gsm;
309
310                 my $sth = money::collections_tracker->db_Main->prepare($real_sql);
311                 $sth->execute(
312                         $org->id, $startdate, $enddate,
313                         $org->id, $startdate, $enddate,
314                         $org->id, $startdate, $enddate,
315                         $org->id, $startdate, $enddate,
316                         $org->id, $startdate, $enddate
317                 );
318
319                 while (my $row = $sth->fetchrow_hashref) {
320                         $row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
321                         $client->respond( $row );
322                 }
323         }
324         return undef;
325 }
326 __PACKAGE__->register_method(
327         method          => 'active_in_collections',
328         api_name        => 'open-ils.storage.money.collections.users_with_activity',
329         stream          => 1,
330         argc            => 3,
331 );
332
333 sub ou_desk_payments {
334         my $self = shift;
335         my $client = shift;
336         my $lib = shift;
337         my $startdate = shift;
338         my $enddate = shift;
339
340         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
341         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
342         return undef unless ($lib =~ /^\d+$/o);
343
344         my $sql = <<"   SQL";
345
346         SELECT  ws.id as workstation,
347                 SUM( CASE WHEN p.payment_type = 'cash_payment' THEN p.amount ELSE 0.0 END ) as cash_payment,
348                 SUM( CASE WHEN p.payment_type = 'check_payment' THEN p.amount ELSE 0.0 END ) as check_payment,
349                 SUM( CASE WHEN p.payment_type = 'credit_card_payment' THEN p.amount ELSE 0.0 END ) as credit_card_payment
350           FROM  money.desk_payment_view p
351                 JOIN actor.workstation ws ON (ws.id = p.cash_drawer)
352           WHERE p.payment_ts >= '$startdate'
353                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
354                 AND p.voided IS FALSE
355                 AND ws.owning_lib = $lib
356          GROUP BY 1
357          ORDER BY 1;
358
359         SQL
360
361         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
362
363         for my $r (@$rows) {
364                 my $x = new Fieldmapper::money::workstation_payment_summary;
365                 $x->workstation( actor::workstation->retrieve($$r[0])->to_fieldmapper );
366                 $x->cash_payment($$r[1]);
367                 $x->check_payment($$r[2]);
368                 $x->credit_card_payment($$r[3]);
369
370                 $client->respond($x);
371         }
372
373         return undef;
374 }
375 __PACKAGE__->register_method(
376         method          => 'ou_desk_payments',
377         api_name        => 'open-ils.storage.money.org_unit.desk_payments',
378         stream          => 1,
379         argc            => 3,
380 );
381
382 sub ou_user_payments {
383         my $self = shift;
384         my $client = shift;
385         my $lib = shift;
386         my $startdate = shift;
387         my $enddate = shift;
388
389         return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
390         return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
391         return undef unless ($lib =~ /^\d+$/o);
392
393         my $sql = <<"   SQL";
394
395         SELECT  au.id as usr,
396                 SUM( CASE WHEN p.payment_type = 'forgive_payment' THEN p.amount ELSE 0.0 END ) as forgive_payment,
397                 SUM( CASE WHEN p.payment_type = 'work_payment' THEN p.amount ELSE 0.0 END ) as work_payment,
398                 SUM( CASE WHEN p.payment_type = 'credit_payment' THEN p.amount ELSE 0.0 END ) as credit_payment
399           FROM  money.bnm_payment_view p
400                 JOIN actor.usr au ON (au.id = p.accepting_usr)
401           WHERE p.payment_ts >= '$startdate'
402                 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
403                 AND p.voided IS FALSE
404                 AND au.home_ou = $lib
405                 AND p.payment_type IN ('credit_payment','forgive_payment','work_payment')
406          GROUP BY 1
407          ORDER BY 1;
408
409         SQL
410
411         my $rows = money::payment->db_Main->selectall_arrayref( $sql );
412
413         for my $r (@$rows) {
414                 my $x = new Fieldmapper::money::user_payment_summary;
415                 $x->usr( actor::user->retrieve($$r[0])->to_fieldmapper );
416                 $x->forgive_payment($$r[1]);
417                 $x->work_payment($$r[2]);
418                 $x->credit_payment($$r[3]);
419
420                 $client->respond($x);
421         }
422
423         return undef;
424 }
425 __PACKAGE__->register_method(
426         method          => 'ou_user_payments',
427         api_name        => 'open-ils.storage.money.org_unit.user_payments',
428         stream          => 1,
429         argc            => 3,
430 );
431
432
433 1;