1 package OpenILS::Application::Storage::Publisher::money;
2 use base qw/OpenILS::Application::Storage/;
3 use OpenSRF::Utils::Logger qw/:level/;
5 my $log = 'OpenSRF::Utils::Logger';
12 my $s = new Fieldmapper::money::billable_transaction_summary;
15 $s->xact_start( $x->xact_start );
16 $s->xact_finish( $x->xact_finish );
20 for my $b ($x->billings) {
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) {
27 $s->last_billing_note($b->note);
28 $s->last_billing_ts($b->billing_ts);
29 $s->last_billing_type($b->billing_type);
33 $s->total_owed( sprintf('%0.2f', int($to) / 100 ) );
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) {
44 $s->last_payment_note($p->note);
45 $s->last_payment_ts($p->payment_ts);
46 $s->last_payment_type($p->payment_type);
49 $s->total_paid( sprintf('%0.2f', int($tp) / 100 ) );
51 $s->balance_owed( sprintf('%0.2f', int(int($to) - int($tp)) / 100) );
52 #$log->debug( "balance of ".$x->id." == ".$s->balance_owed, DEBUG );
54 $s->xact_type( 'grocery' ) if (money::grocery->retrieve($x->id));
55 $s->xact_type( 'circulation' ) if (action::circulation->retrieve($x->id));
68 my @xacts = money::billable_transaction->search_where( $search );
69 $client->respond( $_ ) for (_make_mbts(@xacts));
73 __PACKAGE__->register_method(
74 method => 'search_mbts',
75 api_name => 'open-ils.storage.money.billable_transaction.summary.search',
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 = ?))";
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
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) > ?
107 FROM money.collections_tracker
114 AND (SUM(bl.amount) - COALESCE(SUM((SELECT SUM(amount) FROM money.payment WHERE xact = lt.id)),0)) > ?
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 );
128 __PACKAGE__->register_method(
129 method => 'new_collections',
130 api_name => 'open-ils.storage.money.collections.users_of_interest',
135 sub active_in_collections {
138 my $startdate = shift;
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 = ?))";
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
154 SELECT id,usr,circ_lib AS location, 'c'::char AS x_type FROM action.circulation
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
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 );
179 __PACKAGE__->register_method(
180 method => 'active_in_collections',
181 api_name => 'open-ils.storage.money.collections.users_with_activity',
186 sub ou_desk_payments {
190 my $startdate = shift;
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);
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
214 cash_payment numeric(10,2),
215 check_payment numeric(10,2),
216 credit_card_payment numeric(10,2) );
220 my $rows = money::payment->db_Main->selectall_arrayref( $sql );
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]);
229 $client->respond($x);
234 __PACKAGE__->register_method(
235 method => 'ou_desk_payments',
236 api_name => 'open-ils.storage.money.org_unit.desk_payments',
241 sub ou_user_payments {
245 my $startdate = shift;
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);
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')
270 forgive_payment numeric(10,2),
271 work_payment numeric(10,2),
272 credit_payment numeric(10,2) );
276 my $rows = money::payment->db_Main->selectall_arrayref( $sql );
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]);
285 $client->respond($x);
290 __PACKAGE__->register_method(
291 method => 'ou_user_payments',
292 api_name => 'open-ils.storage.money.org_unit.user_payments',