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 $to += int($b->amount * 100);
23 $lb ||= $b->billing_ts;
24 if ($b->billing_ts ge $lb) {
26 $s->last_billing_note($b->note);
27 $s->last_billing_ts($b->billing_ts);
28 $s->last_billing_type($b->billing_type);
32 $s->total_owed( sprintf('%0.2f', $to / 100 ) );
36 for my $p ($x->payments) {
38 $tp += int($p->amount * 100);
39 $lp ||= $p->payment_ts;
40 if ($p->payment_ts ge $lp) {
42 $s->last_payment_note($p->note);
43 $s->last_payment_ts($p->payment_ts);
44 $s->last_payment_type($p->payment_type);
47 $s->total_paid( sprintf('%0.2f', $tp / 100 ) );
49 $s->balance_owed( sprintf('%0.2f', int($to - $tp) / 100) );
51 $s->xact_type( 'grocery' ) if (money::grocery->retrieve($x->id));
52 $s->xact_type( 'circulation' ) if (action::circulation->retrieve($x->id));
65 my @xacts = money::billable_transaction->search_where( $search );
66 $client->respond( $_ ) for (_make_mbts(@xacts));
70 __PACKAGE__->register_method(
71 method => 'search_mbts',
72 api_name => 'open-ils.storage.money.billable_transaction.summary.search',
85 my $mct = money::collections_tracker->table;
86 my $mb = money::billing->table;
87 my $circ = action::circulation->table;
88 my $mg = money::grocery->table;
89 my $descendants = "actor.org_unit_descendants((select id from actor.org_unit where shortname = ?))";
93 MAX(bl.billing_ts) AS last_pertinent_billing,
94 SUM(bl.amount) - COALESCE(SUM((SELECT SUM(amount) FROM money.payment WHERE xact = lt.id)),0) AS threshold_amount
95 FROM ( SELECT id,usr,billing_location AS location FROM money.grocery
97 SELECT id,usr,circ_lib AS location FROM action.circulation ) AS lt
98 JOIN $descendants d ON (lt.location = d.id)
99 JOIN money.billing bl ON (lt.id = bl.xact AND bl.voided IS FALSE)
100 WHERE AGE(bl.billing_ts) > ?
104 FROM money.collections_tracker
111 AND (SUM(bl.amount) - COALESCE(SUM((SELECT SUM(amount) FROM money.payment WHERE xact = lt.id)),0)) > ?
116 my $sth = money::collections_tracker->db_Main->prepare($SQL);
117 $sth->execute(uc($l), $age, uc($l), $amount );
118 while (my $row = $sth->fetchrow_hashref) {
119 #$row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
120 $client->respond( $row );
125 __PACKAGE__->register_method(
126 method => 'new_collections',
127 api_name => 'open-ils.storage.money.collections.users_of_interest',
132 sub active_in_collections {
135 my $startdate = shift;
139 my $mct = money::collections_tracker->table;
140 my $mb = money::billing->table;
141 my $circ = action::circulation->table;
142 my $mg = money::grocery->table;
143 my $descendants = "actor.org_unit_descendants((select id from actor.org_unit where shortname = ?))";
147 MAX(bl.billing_ts) AS last_pertinent_billing,
148 MAX(pm.payment_ts) AS last_pertinent_payment
149 FROM ( SELECT id,usr,billing_location AS location, 'g'::char AS x_type FROM money.grocery
151 SELECT id,usr,circ_lib AS location, 'c'::char AS x_type FROM action.circulation
153 SELECT id,usr,circ_lib AS location, 'i'::char AS x_type FROM action.circulation
154 WHERE checkin_time between ? and ? ) AS lt
155 JOIN $descendants d ON (lt.location = d.id)
156 JOIN money.collections_tracker cl ON (lt.usr = cl.usr)
157 LEFT JOIN money.billing bl ON (lt.id = bl.xact)
158 LEFT JOIN money.payment pm ON (lt.id = pm.xact)
159 WHERE bl.billing_ts between ? and ?
160 OR pm.payment_ts between ? and ?
161 OR lt.x_type = 'i'::char
167 my $sth = money::collections_tracker->db_Main->prepare($SQL);
168 $sth->execute( $startdate, $enddate, uc($l), $startdate, $enddate, $startdate, $enddate );
169 while (my $row = $sth->fetchrow_hashref) {
170 $row->{usr} = actor::user->retrieve($row->{usr})->to_fieldmapper;
171 $client->respond( $row );
176 __PACKAGE__->register_method(
177 method => 'active_in_collections',
178 api_name => 'open-ils.storage.money.collections.users_with_activity',
183 sub ou_desk_payments {
187 my $startdate = shift;
190 return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
191 return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
192 return undef unless ($lib =~ /^\d+$/o);
200 SUM(COALESCE(p.amount,0.0))
201 FROM money.desk_payment_view p
202 JOIN actor.workstation ws ON (ws.id = p.cash_drawer)
203 WHERE p.payment_ts >= '$startdate'
204 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
205 AND p.voided IS FALSE
206 AND ws.owning_lib = $lib
211 cash_payment numeric(10,2),
212 check_payment numeric(10,2),
213 credit_card_payment numeric(10,2) );
217 my $rows = money::payment->db_Main->selectall_arrayref( $sql );
220 my $x = new Fieldmapper::money::workstation_payment_summary;
221 $x->workstation( actor::workstation->retrieve($$r[0])->to_fieldmapper );
222 $x->cash_payment($$r[1]);
223 $x->check_payment($$r[2]);
224 $x->credit_card_payment($$r[3]);
226 $client->respond($x);
231 __PACKAGE__->register_method(
232 method => 'ou_desk_payments',
233 api_name => 'open-ils.storage.money.org_unit.desk_payments',
238 sub ou_user_payments {
242 my $startdate = shift;
245 return undef unless ($startdate =~ /^\d{4}-\d{2}-\d{2}$/o);
246 return undef unless ($enddate =~ /^\d{4}-\d{2}-\d{2}$/o);
247 return undef unless ($lib =~ /^\d+$/o);
255 SUM(COALESCE(p.amount,0.0))
256 FROM money.bnm_payment_view p
257 JOIN actor.usr au ON (au.id = p.accepting_usr)
258 WHERE p.payment_ts >= '$startdate'
259 AND p.payment_ts < '$enddate'::TIMESTAMPTZ + INTERVAL '1 day'
260 AND p.voided IS FALSE
261 AND au.home_ou = $lib
262 AND p.payment_type IN ('credit_payment','forgive_payment','work_payment')
267 forgive_payment numeric(10,2),
268 work_payment numeric(10,2),
269 credit_payment numeric(10,2) );
273 my $rows = money::payment->db_Main->selectall_arrayref( $sql );
276 my $x = new Fieldmapper::money::user_payment_summary;
277 $x->usr( actor::user->retrieve($$r[0])->to_fieldmapper );
278 $x->forgive_payment($$r[1]);
279 $x->work_payment($$r[2]);
280 $x->credit_payment($$r[3]);
282 $client->respond($x);
287 __PACKAGE__->register_method(
288 method => 'ou_user_payments',
289 api_name => 'open-ils.storage.money.org_unit.user_payments',