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/OpenSRF::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';
79 for my $pay (@{$payments->{payments}}) {
81 my $transid = $pay->[0];
82 my $amount = $pay->[1];
83 $amount =~ s/\$//og; # just to be safe
85 ($trans, $evt) = $apputils->fetch_open_billable_transaction($transid);
88 if($trans->usr != $userid) { # Do we need to restrict this in some way ??
89 $logger->info( " * User $userid is making a payment for " .
90 "a different user: " . $trans->usr . ' for transaction ' . $trans->id );
93 if($type eq 'credit_payment') {
95 $logger->activity("user ".$user->id." reducing patron credit by ".
96 "$credit for making a credit_payment on transaction ".$trans->id);
99 # A negative payment is a refund.
102 # If the refund causes the transaction balance to exceed 0 dollars,
103 # we are in effect loaning the patron money. This is not allowed.
104 if( ($trans->balance_owed - $amount) > 0 ) {
105 return OpenILS::Event->new('REFUND_EXCEEDS_BALANCE');
108 # Otherwise, make sure the refund does not exceed desk payments
109 # This is also not allowed
111 my $desk_payments = $e->search_money_desk_payment(
112 { xact => $transid, voided => 'f' });
113 #$desk_total += $_->amount_collected for @$desk_payments;
114 $desk_total += $_->amount for @$desk_payments;
116 if( (-$amount) > $desk_total ) {
117 return OpenILS::Event->new(
118 'REFUND_EXCEEDS_DESK_PAYMENTS',
119 payload => { allowed_refund => $desk_total, submitted_refund => -$amount } );
123 my $payobj = "Fieldmapper::money::$type";
124 $payobj = $payobj->new;
126 $payobj->amount($amount);
127 $payobj->amount_collected($amount);
128 $payobj->accepting_usr($user->id);
129 $payobj->xact($transid);
130 $payobj->note($note);
132 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
133 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
134 if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
135 if ($payobj->has_field('expire_month')) { $payobj->expire_month($expire_month); }
136 if ($payobj->has_field('expire_year')) { $payobj->expire_year($expire_year); }
137 if ($payobj->has_field('approval_code')) { $payobj->approval_code($approval_code); }
138 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
140 # update the transaction if it's done
141 if( (my $cred = ($trans->balance_owed - $amount)) <= 0 ) {
143 # Any overpay on this transaction goes directly into patron credit
146 $logger->activity("user ".$user->id." applying credit ".
147 "of $cred on transaction ".$trans->id. " because of overpayment");
149 $logger->debug("Transactin " . $trans->id . ' is complete');
150 $trans = $session->request(
151 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
153 # If this is a circulation, we can't close the transaction unless stop_fines is set
154 my $circ = $session->request(
155 'open-ils.storage.direct.action.circulation.retrieve', $transid )->gather(1);
157 if( !$circ || $circ->stop_fines ) {
159 $trans->xact_finish("now");
160 my $s = $session->request(
161 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
163 if(!$s) { throw OpenSRF::EX::ERROR
164 ("Error updating billable_xact in circ.money.payment"); }
169 $logger->debug("Creating new $payobj for \$$amount\n");
171 my $s = $session->request(
172 "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
173 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
178 $logger->activity("user ".$user->id." applying total ".
179 "credit of $credit to user $userid") if $credit != 0;
181 $evt = _update_patron_credit( $session, $userid, $credit );
184 $apputils->commit_db_session($session);
186 $client->respond_complete(1);
188 # ------------------------------------------------------------------------------
189 # Update the patron penalty info in the DB
190 # ------------------------------------------------------------------------------
191 $U->update_patron_penalties(
200 sub _update_patron_credit {
201 my( $session, $userid, $credit ) = @_;
202 #return if $credit <= 0;
204 my $patron = $session->request(
205 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
207 $patron->credit_forward_balance(
208 $patron->credit_forward_balance + $credit);
210 if( $patron->credit_forward_balance < 0 ) {
211 return OpenILS::Event->new('NEGATIVE_PATRON_BALANCE');
214 $logger->info("Total patron credit for $userid is now " . $patron->credit_forward_balance );
217 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
223 __PACKAGE__->register_method(
224 method => "retrieve_payments",
225 api_name => "open-ils.circ.money.payment.retrieve.all_",
226 notes => "Returns a list of payments attached to a given transaction"
229 sub retrieve_payments {
230 my( $self, $client, $login, $transid ) = @_;
233 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
236 # XXX the logic here is wrong.. we need to check the owner of the transaction
237 # to make sure the requestor has access
239 # XXX grab the view, for each object in the view, grab the real object
241 return $apputils->simplereq(
243 'open-ils.cstore.direct.money.payment.search.atomic', { xact => $transid } );
248 __PACKAGE__->register_method(
249 method => "retrieve_payments2",
250 api_name => "open-ils.circ.money.payment.retrieve.all",
251 notes => "Returns a list of payments attached to a given transaction"
254 sub retrieve_payments2 {
255 my( $self, $client, $login, $transid ) = @_;
257 my $e = new_editor(authtoken=>$login);
258 return $e->event unless $e->checkauth;
259 return $e->event unless $e->allowed('VIEW_TRANSACTION');
262 my $pmnts = $e->search_money_payment({ xact => $transid });
264 my $type = $_->payment_type;
265 my $meth = "retrieve_money_$type";
266 my $p = $e->$meth($_->id) or return $e->event;
267 $p->payment_type($type);
268 $p->cash_drawer($e->retrieve_actor_workstation($p->cash_drawer))
269 if $p->has_field('cash_drawer');
270 push( @payments, $p );
278 __PACKAGE__->register_method(
279 method => "create_grocery_bill",
280 api_name => "open-ils.circ.money.grocery.create",
282 Creates a new grocery transaction using the transaction object provided
283 PARAMS: (login_session, money.grocery (mg) object)
286 sub create_grocery_bill {
287 my( $self, $client, $login, $transaction ) = @_;
289 my( $staff, $evt ) = $apputils->checkses($login);
291 $evt = $apputils->check_perms($staff->id,
292 $transaction->billing_location, 'CREATE_TRANSACTION' );
296 $logger->activity("Creating grocery bill " . Dumper($transaction) );
298 $transaction->clear_id;
299 my $session = $apputils->start_db_session;
300 my $transid = $session->request(
301 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
303 throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
305 $logger->debug("Created new grocery transaction $transid");
307 $apputils->commit_db_session($session);
313 __PACKAGE__->register_method(
314 method => 'fetch_grocery',
315 api_name => 'open-ils.circ.money.grocery.retrieve'
319 my( $self, $conn, $auth, $id ) = @_;
320 my $e = new_editor(authtoken=>$auth);
321 return $e->event unless $e->checkauth;
322 return $e->event unless $e->allowed('VIEW_TRANSACTION'); # eh.. basically the same permission
323 my $g = $e->retrieve_money_grocery($id)
329 __PACKAGE__->register_method(
330 method => "billing_items",
331 api_name => "open-ils.circ.money.billing.retrieve.all",
333 Returns a list of billing items for the given transaction.
334 PARAMS( login, transaction_id )
338 my( $self, $client, $login, $transid ) = @_;
340 my( $trans, $evt ) = $U->fetch_billable_xact($transid);
344 ($staff, $evt ) = $apputils->checkses($login);
347 if($staff->id ne $trans->usr) {
348 $evt = $U->check_perms($staff->id, $staff->home_ou, 'VIEW_TRANSACTION');
352 return $apputils->simplereq( 'open-ils.cstore',
353 'open-ils.cstore.direct.money.billing.search.atomic', { xact => $transid } )
357 __PACKAGE__->register_method(
358 method => "billing_items_create",
359 api_name => "open-ils.circ.money.billing.create",
361 Creates a new billing line item
362 PARAMS( login, bill_object (mb) )
365 sub billing_items_create {
366 my( $self, $client, $login, $billing ) = @_;
368 my $e = new_editor(authtoken => $login, xact => 1);
369 return $e->event unless $e->checkauth;
370 return $e->event unless $e->allowed('CREATE_BILL');
372 my $xact = $e->retrieve_money_billable_transaction($billing->xact)
375 # if the transaction was closed, re-open it
376 if($xact->xact_finish) {
377 $xact->clear_xact_finish;
378 $e->update_money_billable_transaction($xact)
382 my $amt = $billing->amount;
384 $billing->amount($amt);
386 $e->create_money_billing($billing) or return $e->event;
389 # ------------------------------------------------------------------------------
390 # Update the patron penalty info in the DB
391 # ------------------------------------------------------------------------------
392 $U->update_patron_penalties(
394 patronid => $xact->usr,
400 __PACKAGE__->register_method(
401 method => 'void_bill',
402 api_name => 'open-ils.circ.money.billing.void',
405 @param authtoken Login session key
406 @param billid The id of the bill to void
407 @return 1 on success, Event on error
412 my( $s, $c, $authtoken, $billid ) = @_;
414 #my $e = OpenILS::Utils::Editor->new( authtoken => $authtoken );
415 my $e = new_editor( authtoken => $authtoken, xact => 1 );
416 return $e->event unless $e->checkauth;
417 return $e->event unless $e->allowed('VOID_BILLING');
419 my $bill = $e->retrieve_money_billing($billid)
422 return OpenILS::Event->new('BILL_ALREADY_VOIDED', payload => $bill)
423 if $bill->voided and $bill->voided =~ /t/io;
426 $bill->voider($e->requestor->id);
427 $bill->void_time('now');
429 $e->update_money_billing($bill) or return $e->event;
430 my $evt = _check_open_xact($e, $bill->xact);
435 # ------------------------------------------------------------------------------
436 # Update the patron penalty info in the DB
437 # ------------------------------------------------------------------------------
438 my $xact = $e->retrieve_money_billable_transaction($bill->xact)
441 $U->update_patron_penalties(
442 authtoken => $authtoken,
443 patronid => $xact->usr,
449 sub _check_open_xact {
450 my( $editor, $xactid ) = @_;
452 # Grab the transaction
453 my $xact = $editor->retrieve_money_billable_transaction($xactid)
454 or return $editor->event;
456 # grab the summary and see how much is owed on this transaction
457 my $summary = $editor->retrieve_money_billable_transaction_summary($xactid)
458 or return $editor->event;
460 # grab the circulation if it is a circ;
461 my $circ = $editor->retrieve_action_circulation($xactid);
463 # If nothing is owed on the transaction but it is still open
464 # and this transaction is not an open circulation, close it
466 ( $summary->balance_owed == 0 and ! $xact->xact_finish ) and
467 ( !$circ or $circ->stop_fines )) {
469 $logger->info("closing transaction ".$xact->id. ' becauase balance_owed == 0');
470 $xact->xact_finish('now');
471 $editor->update_money_billable_transaction($xact)
472 or return $editor->event;
476 # If money is owed or a refund is due on the xact and xact_finish
477 # is set, clear it (to reopen the xact) and update
478 if( $summary->balance_owed != 0 and $xact->xact_finish ) {
479 $logger->info("re-opening transaction ".$xact->id. ' becauase balance_owed != 0');
480 $xact->clear_xact_finish;
481 $editor->update_money_billable_transaction($xact)
482 or return $editor->event;
491 __PACKAGE__->register_method (
492 method => 'fetch_mbts',
493 api_name => 'open-ils.circ.money.billable_xact_summary.retrieve'
496 my($s, $c, $authtoken, $id) = @_;
498 my $sum = $U->cstorereq(
499 'open-ils.cstore.direct.money.billable_transaction_summary.retrieve', $id );
500 return OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', id => $id) unless $sum;
502 my ($reqr, $evt) = $U->checkses($authtoken);
506 ($usr, $evt) = $U->fetch_user($sum->usr);
509 $evt = $U->check_perms($reqr->id, $usr->home_ou, 'VIEW_TRANSACTION');
516 __PACKAGE__->register_method(
517 method => 'desk_payments',
518 api_name => 'open-ils.circ.money.org_unit.desk_payments'
522 my( $self, $conn, $auth, $org, $start_date, $end_date ) = @_;
523 my $e = new_editor(authtoken=>$auth);
524 return $e->event unless $e->checkauth;
525 return $e->event unless $e->allowed('VIEW_TRANSACTION', $org);
526 my $data = $U->storagereq(
527 'open-ils.storage.money.org_unit.desk_payments.atomic',
528 $org, $start_date, $end_date );
530 $_->workstation( $_->workstation->name ) for(@$data);
535 __PACKAGE__->register_method(
536 method => 'user_payments',
537 api_name => 'open-ils.circ.money.org_unit.user_payments'
541 my( $self, $conn, $auth, $org, $start_date, $end_date ) = @_;
542 my $e = new_editor(authtoken=>$auth);
543 return $e->event unless $e->checkauth;
544 return $e->event unless $e->allowed('VIEW_TRANSACTION', $org);
545 my $data = $U->storagereq(
546 'open-ils.storage.money.org_unit.user_payments.atomic',
547 $org, $start_date, $end_date );
550 $e->retrieve_actor_card($_->usr->card)->barcode);
552 $e->retrieve_actor_org_unit($_->usr->home_ou)->shortname);