1 # ---------------------------------------------------------------
2 # Copyright (C) 2005 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
17 package OpenILS::Application::Circ::Money;
18 use base qw/OpenILS::Application/;
19 use strict; use warnings;
20 use OpenILS::Application::AppUtils;
21 my $apputils = "OpenILS::Application::AppUtils";
22 my $U = "OpenILS::Application::AppUtils";
24 use OpenSRF::EX qw(:try);
28 use OpenSRF::Utils::Logger qw/:logger/;
29 use OpenILS::Utils::CStoreEditor qw/:funcs/;
31 __PACKAGE__->register_method(
32 method => "make_payments",
33 api_name => "open-ils.circ.money.payment",
35 Pass in a structure like so:
37 cash_drawer: <string>,
38 payment_type : <string>,
45 patron_credit : <credit amt>
47 login must have CREATE_PAYMENT priveleges.
48 If any payments fail, all are reverted back.
53 my( $self, $client, $login, $payments ) = @_;
55 my( $user, $trans, $evt );
57 ( $user, $evt ) = $apputils->checkses($login);
59 $evt = $apputils->check_perms($user->id, $user->ws_ou, 'CREATE_PAYMENT');
62 my $e = new_editor(); # at this point, just for convenience
64 $logger->info("Creating payment objects: " . Dumper($payments) );
66 my $session = $apputils->start_db_session;
67 my $type = $payments->{payment_type};
68 my $credit = $payments->{patron_credit} || 0;
69 my $drawer = $user->wsid;
70 my $userid = $payments->{userid};
71 my $note = $payments->{note};
72 my $cc_type = $payments->{cc_type} || 'n/a';
73 my $cc_number = $payments->{cc_number} || 'n/a';
74 my $expire_month = $payments->{expire_month};
75 my $expire_year = $payments->{expire_year};
76 my $approval_code = $payments->{approval_code} || 'n/a';
77 my $check_number = $payments->{check_number} || 'n/a';
81 for my $pay (@{$payments->{payments}}) {
83 my $transid = $pay->[0];
84 my $amount = $pay->[1];
85 $amount =~ s/\$//og; # just to be safe
87 $total_paid += $amount;
89 $trans = fetch_mbts($self, $client, $login, $transid);
90 return $trans if $U->event_code($trans);
92 $logger->info("payment: processing transaction [$transid] with balance_owed = ".
93 $trans->balance_owed. ", payment amount = $amount, and payment type = $type");
95 if($trans->usr != $userid) { # Do we need to restrict this in some way ??
96 $logger->info( " * User $userid is making a payment for " .
97 "a different user: " . $trans->usr . ' for transaction ' . $trans->id );
100 if($type eq 'credit_payment') {
102 $logger->activity("user ".$user->id." reducing patron credit by ".
103 "$credit for making a credit_payment on transaction ".$trans->id);
106 # A negative payment is a refund.
109 $logger->info("payment: received a negative payment (refund) of $amount");
111 # If the refund causes the transaction balance to exceed 0 dollars,
112 # we are in effect loaning the patron money. This is not allowed.
113 if( ($trans->balance_owed - $amount) > 0 ) {
114 return OpenILS::Event->new('REFUND_EXCEEDS_BALANCE');
117 # Otherwise, make sure the refund does not exceed desk payments
118 # This is also not allowed
120 my $desk_payments = $e->search_money_desk_payment(
121 { xact => $transid, voided => 'f' });
122 $desk_total += $_->amount for @$desk_payments;
124 if( (-$amount) > $desk_total ) {
125 return OpenILS::Event->new(
126 'REFUND_EXCEEDS_DESK_PAYMENTS',
127 payload => { allowed_refund => $desk_total, submitted_refund => -$amount } );
131 my $payobj = "Fieldmapper::money::$type";
132 $payobj = $payobj->new;
134 $payobj->amount($amount);
135 $payobj->amount_collected($amount);
136 $payobj->xact($transid);
137 $payobj->note($note);
139 if ($payobj->has_field('accepting_usr')) { $payobj->accepting_usr($user->id); }
140 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
141 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
142 if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
143 if ($payobj->has_field('expire_month')) { $payobj->expire_month($expire_month); }
144 if ($payobj->has_field('expire_year')) { $payobj->expire_year($expire_year); }
145 if ($payobj->has_field('approval_code')) { $payobj->approval_code($approval_code); }
146 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
148 # update the transaction if it's done
149 if( (my $cred = ($trans->balance_owed - $amount)) <= 0 ) {
151 # Any overpay on this transaction goes directly into patron credit
154 $logger->info("payment: amount ($amount) exceeds transaction balance of ".
155 $trans->balance_owed.". Applying patron credit of $cred");
159 $trans = $session->request(
160 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
162 # If this is a circulation, we can't close the transaction unless stop_fines is set
163 my $circ = $session->request(
164 'open-ils.storage.direct.action.circulation.retrieve', $transid )->gather(1);
166 if( !$circ || $circ->stop_fines ) {
168 $trans->xact_finish("now");
169 my $s = $session->request(
170 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
172 if(!$s) { throw OpenSRF::EX::ERROR
173 ("Error updating billable_xact in circ.money.payment"); }
177 my $s = $session->request(
178 "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
179 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
185 $logger->info("user $uid applying total ".
186 "credit of $credit to user $userid") if $credit != 0;
188 $logger->info("user $uid applying total payment of $total_paid to user $userid");
190 $evt = _update_patron_credit( $session, $userid, $credit );
193 $apputils->commit_db_session($session);
195 # ------------------------------------------------------------------------------
196 # Update the patron penalty info in the DB
197 # ------------------------------------------------------------------------------
198 $U->update_patron_penalties_nonblock(patronid => $userid);
200 $client->respond_complete(1);
206 sub _update_patron_credit {
207 my( $session, $userid, $credit ) = @_;
208 #return if $credit <= 0;
210 my $patron = $session->request(
211 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
213 $patron->credit_forward_balance(
214 $patron->credit_forward_balance + $credit);
216 if( $patron->credit_forward_balance < 0 ) {
217 return OpenILS::Event->new('NEGATIVE_PATRON_BALANCE');
220 $logger->info("Total patron credit for $userid is now " . $patron->credit_forward_balance );
223 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
229 __PACKAGE__->register_method(
230 method => "retrieve_payments",
231 api_name => "open-ils.circ.money.payment.retrieve.all_",
232 notes => "Returns a list of payments attached to a given transaction"
235 sub retrieve_payments {
236 my( $self, $client, $login, $transid ) = @_;
239 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
242 # XXX the logic here is wrong.. we need to check the owner of the transaction
243 # to make sure the requestor has access
245 # XXX grab the view, for each object in the view, grab the real object
247 return $apputils->simplereq(
249 'open-ils.cstore.direct.money.payment.search.atomic', { xact => $transid } );
254 __PACKAGE__->register_method(
255 method => "retrieve_payments2",
257 api_name => "open-ils.circ.money.payment.retrieve.all",
258 notes => "Returns a list of payments attached to a given transaction"
261 sub retrieve_payments2 {
262 my( $self, $client, $login, $transid ) = @_;
264 my $e = new_editor(authtoken=>$login);
265 return $e->event unless $e->checkauth;
266 return $e->event unless $e->allowed('VIEW_TRANSACTION');
269 my $pmnts = $e->search_money_payment({ xact => $transid });
271 my $type = $_->payment_type;
272 my $meth = "retrieve_money_$type";
273 my $p = $e->$meth($_->id) or return $e->event;
274 $p->payment_type($type);
275 $p->cash_drawer($e->retrieve_actor_workstation($p->cash_drawer))
276 if $p->has_field('cash_drawer');
277 push( @payments, $p );
285 __PACKAGE__->register_method(
286 method => "create_grocery_bill",
287 api_name => "open-ils.circ.money.grocery.create",
289 Creates a new grocery transaction using the transaction object provided
290 PARAMS: (login_session, money.grocery (mg) object)
293 sub create_grocery_bill {
294 my( $self, $client, $login, $transaction ) = @_;
296 my( $staff, $evt ) = $apputils->checkses($login);
298 $evt = $apputils->check_perms($staff->id,
299 $transaction->billing_location, 'CREATE_TRANSACTION' );
303 $logger->activity("Creating grocery bill " . Dumper($transaction) );
305 $transaction->clear_id;
306 my $session = $apputils->start_db_session;
307 my $transid = $session->request(
308 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
310 throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
312 $logger->debug("Created new grocery transaction $transid");
314 $apputils->commit_db_session($session);
316 my $e = new_editor(xact=>1);
317 $evt = _check_open_xact($e, $transid);
325 __PACKAGE__->register_method(
326 method => 'fetch_grocery',
327 api_name => 'open-ils.circ.money.grocery.retrieve'
331 my( $self, $conn, $auth, $id ) = @_;
332 my $e = new_editor(authtoken=>$auth);
333 return $e->event unless $e->checkauth;
334 return $e->event unless $e->allowed('VIEW_TRANSACTION'); # eh.. basically the same permission
335 my $g = $e->retrieve_money_grocery($id)
341 __PACKAGE__->register_method(
342 method => "billing_items",
344 api_name => "open-ils.circ.money.billing.retrieve.all",
346 Returns a list of billing items for the given transaction.
347 PARAMS( login, transaction_id )
351 my( $self, $client, $login, $transid ) = @_;
353 my( $trans, $evt ) = $U->fetch_billable_xact($transid);
357 ($staff, $evt ) = $apputils->checkses($login);
360 if($staff->id ne $trans->usr) {
361 $evt = $U->check_perms($staff->id, $staff->home_ou, 'VIEW_TRANSACTION');
365 return $apputils->simplereq( 'open-ils.cstore',
366 'open-ils.cstore.direct.money.billing.search.atomic', { xact => $transid } )
370 __PACKAGE__->register_method(
371 method => "billing_items_create",
372 api_name => "open-ils.circ.money.billing.create",
374 Creates a new billing line item
375 PARAMS( login, bill_object (mb) )
378 sub billing_items_create {
379 my( $self, $client, $login, $billing ) = @_;
381 my $e = new_editor(authtoken => $login, xact => 1);
382 return $e->event unless $e->checkauth;
383 return $e->event unless $e->allowed('CREATE_BILL');
385 my $xact = $e->retrieve_money_billable_transaction($billing->xact)
388 # if the transaction was closed, re-open it
389 if($xact->xact_finish) {
390 $xact->clear_xact_finish;
391 $e->update_money_billable_transaction($xact)
395 my $amt = $billing->amount;
397 $billing->amount($amt);
399 $e->create_money_billing($billing) or return $e->event;
402 # ------------------------------------------------------------------------------
403 # Update the patron penalty info in the DB
404 # ------------------------------------------------------------------------------
405 $U->update_patron_penalties_nonblock(patronid => $xact->usr);
410 __PACKAGE__->register_method(
411 method => 'void_bill',
412 api_name => 'open-ils.circ.money.billing.void',
415 @param authtoken Login session key
416 @param billid The id of the bill to void
417 @return 1 on success, Event on error
423 my( $s, $c, $authtoken, @billids ) = @_;
425 my $e = new_editor( authtoken => $authtoken, xact => 1 );
426 return $e->event unless $e->checkauth;
427 return $e->event unless $e->allowed('VOID_BILLING');
430 for my $billid (@billids) {
432 my $bill = $e->retrieve_money_billing($billid)
435 my $xact = $e->retrieve_money_billable_transaction($bill->xact)
438 $users{$xact->usr} = 1;
440 return OpenILS::Event->new('BILL_ALREADY_VOIDED', payload => $bill)
441 if $bill->voided and $bill->voided =~ /t/io;
444 $bill->voider($e->requestor->id);
445 $bill->void_time('now');
447 $e->update_money_billing($bill) or return $e->event;
448 my $evt = _check_open_xact($e, $bill->xact, $xact);
453 # update the penalties for each affected user
454 $U->update_patron_penalties_nonblock(patronid => $_) for keys %users;
459 sub _check_open_xact {
460 my( $editor, $xactid, $xact ) = @_;
462 # Grab the transaction
463 $xact ||= $editor->retrieve_money_billable_transaction($xactid);
464 return $editor->event unless $xact;
465 $xactid ||= $xact->id;
467 # grab the summary and see how much is owed on this transaction
468 my ($summary) = $U->fetch_mbts($xactid, $editor);
470 # grab the circulation if it is a circ;
471 my $circ = $editor->retrieve_action_circulation($xactid);
473 # If nothing is owed on the transaction but it is still open
474 # and this transaction is not an open circulation, close it
476 ( $summary->balance_owed == 0 and ! $xact->xact_finish ) and
477 ( !$circ or $circ->stop_fines )) {
479 $logger->info("closing transaction ".$xact->id. ' becauase balance_owed == 0');
480 $xact->xact_finish('now');
481 $editor->update_money_billable_transaction($xact)
482 or return $editor->event;
486 # If money is owed or a refund is due on the xact and xact_finish
487 # is set, clear it (to reopen the xact) and update
488 if( $summary->balance_owed != 0 and $xact->xact_finish ) {
489 $logger->info("re-opening transaction ".$xact->id. ' becauase balance_owed != 0');
490 $xact->clear_xact_finish;
491 $editor->update_money_billable_transaction($xact)
492 or return $editor->event;
501 __PACKAGE__->register_method (
502 method => 'fetch_mbts',
504 api_name => 'open-ils.circ.money.billable_xact_summary.retrieve'
507 my( $self, $conn, $auth, $id) = @_;
509 my $e = new_editor(xact => 1, authtoken=>$auth);
510 return $e->event unless $e->checkauth;
511 my ($mbts) = $U->fetch_mbts($id, $e);
513 my $user = $e->retrieve_actor_user($mbts->usr)
514 or return $e->die_event;
516 return $e->die_event unless $e->allowed('VIEW_TRANSACTION', $user->home_ou);
523 __PACKAGE__->register_method(
524 method => 'desk_payments',
525 api_name => 'open-ils.circ.money.org_unit.desk_payments'
529 my( $self, $conn, $auth, $org, $start_date, $end_date ) = @_;
530 my $e = new_editor(authtoken=>$auth);
531 return $e->event unless $e->checkauth;
532 return $e->event unless $e->allowed('VIEW_TRANSACTION', $org);
533 my $data = $U->storagereq(
534 'open-ils.storage.money.org_unit.desk_payments.atomic',
535 $org, $start_date, $end_date );
537 $_->workstation( $_->workstation->name ) for(@$data);
542 __PACKAGE__->register_method(
543 method => 'user_payments',
544 api_name => 'open-ils.circ.money.org_unit.user_payments'
548 my( $self, $conn, $auth, $org, $start_date, $end_date ) = @_;
549 my $e = new_editor(authtoken=>$auth);
550 return $e->event unless $e->checkauth;
551 return $e->event unless $e->allowed('VIEW_TRANSACTION', $org);
552 my $data = $U->storagereq(
553 'open-ils.storage.money.org_unit.user_payments.atomic',
554 $org, $start_date, $end_date );
557 $e->retrieve_actor_card($_->usr->card)->barcode);
559 $e->retrieve_actor_org_unit($_->usr->home_ou)->shortname);