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/;
30 use OpenILS::Utils::Penalty;
32 __PACKAGE__->register_method(
33 method => "make_payments",
34 api_name => "open-ils.circ.money.payment",
36 desc => q/Create payments for a given user and set of transactions,
37 login must have CREATE_PAYMENT priveleges.
38 If any payments fail, all are reverted back./,
40 {desc => 'Authtoken', type => 'string'},
41 {desc => q/Arguments Hash, supporting the following params:
64 my($self, $client, $auth, $payments) = @_;
66 my $e = new_editor(authtoken => $auth, xact => 1);
67 return $e->die_event unless $e->checkauth;
69 my $type = $payments->{payment_type};
70 my $user_id = $payments->{userid};
71 my $credit = $payments->{patron_credit} || 0;
72 my $drawer = $e->requestor->wsid;
73 my $note = $payments->{note};
74 my $cc_type = $payments->{cc_type};
75 my $cc_number = $payments->{cc_number};
76 my $cc_expire_month = $payments->{cc_expire_month};
77 my $cc_expire_year = $payments->{cc_expire_year};
78 my $cc_approval_code = $payments->{cc_approval_code};
79 my $check_number = $payments->{check_number};
83 my $patron = $e->retrieve_actor_user($user_id) or return $e->die_event;
85 # A user is allowed to make credit card payments on his/her own behalf
86 # All other scenarious require permission
87 unless($type eq 'credit_card_payment' and $user_id == $e->requestor->id) {
88 return $e->die_event unless $e->allowed('CREATE_PAYMENT', $patron->home_ou);
92 # first collect the transactions and make sure the transaction
93 # user matches the requested user
95 for my $pay (@{$payments->{payments}}) {
97 my $xact_id = $pay->[0];
98 my $xact = $e->retrieve_money_billable_transaction_summary($xact_id)
99 or return $e->die_event;
101 if($xact->usr != $user_id) {
103 return OpenILS::Event->new('BAD_PARAMS', note => q/user does not match transaction/);
106 $xacts{$xact_id} = $xact;
109 for my $pay (@{$payments->{payments}}) {
111 my $transid = $pay->[0];
112 my $amount = $pay->[1];
113 $amount =~ s/\$//og; # just to be safe
114 my $trans = $xacts{$transid};
116 $total_paid += $amount;
118 $orgs{$U->xact_org($transid, $e)} = 1;
120 # making payment with existing patron credit
121 $credit -= $amount if $type eq 'credit_payment';
123 # A negative payment is a refund.
126 # Negative credit card payments are not allowed
127 if($type eq 'credit_card_payment') {
129 return OpenILS::Event->new(
131 note => q/Negative credit card payments not allowed/
135 # If the refund causes the transaction balance to exceed 0 dollars,
136 # we are in effect loaning the patron money. This is not allowed.
137 if( ($trans->balance_owed - $amount) > 0 ) {
139 return OpenILS::Event->new('REFUND_EXCEEDS_BALANCE');
142 # Otherwise, make sure the refund does not exceed desk payments
143 # This is also not allowed
145 my $desk_payments = $e->search_money_desk_payment({xact => $transid, voided => 'f'});
146 $desk_total += $_->amount for @$desk_payments;
148 if( (-$amount) > $desk_total ) {
150 return OpenILS::Event->new(
151 'REFUND_EXCEEDS_DESK_PAYMENTS',
152 payload => { allowed_refund => $desk_total, submitted_refund => -$amount } );
156 my $payobj = "Fieldmapper::money::$type";
157 $payobj = $payobj->new;
159 $payobj->amount($amount);
160 $payobj->amount_collected($amount);
161 $payobj->xact($transid);
162 $payobj->note($note);
164 if ($payobj->has_field('accepting_usr')) { $payobj->accepting_usr($e->requestor->id); }
165 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
166 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
168 # Store the last 4 digits?
169 #if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
170 #if ($payobj->has_field('approval_code')) { $payobj->approval_code($cc_approval_code); }
172 if ($payobj->has_field('expire_month')) { $payobj->expire_month($cc_expire_month); }
173 if ($payobj->has_field('expire_year')) { $payobj->expire_year($cc_expire_year); }
174 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
176 # update the transaction if it's done
177 if( (my $cred = ($trans->balance_owed - $amount)) <= 0 ) {
179 # Any overpay on this transaction goes directly into patron credit
182 my $circ = $e->retrieve_action_circulation($transid);
184 if(!$circ || $circ->stop_fines) {
185 # If this is a circulation, we can't close the transaction unless stop_fines is set
186 $trans = $e->retrieve_money_billable_transaction($transid);
187 $trans->xact_finish("now");
188 $e->update_money_billable_transaction($trans) or return $e->die_event;
192 my $method = "create_money_$type";
193 $e->$method($payobj) or return $e->die_event;
195 } # all payment objects have been created and inserted.
197 if($type eq 'credit_card_payment') {
198 # TODO send to credit card processor
199 # amount == $total_paid
201 # $e->rollback if processing fails. This will undo everything.
204 my $evt = _update_patron_credit($e, $patron, $credit);
207 for my $org_id (keys %orgs) {
208 # calculate penalties for each of the affected orgs
209 $evt = OpenILS::Utils::Penalty->calculate_penalties($e, $user_id, $org_id);
218 sub _update_patron_credit {
219 my($e, $patron, $credit) = @_;
220 return undef if $credit == 0;
221 $patron->credit_forward_balance($patron->credit_forward_balance + $credit);
222 return OpenILS::Event->new('NEGATIVE_PATRON_BALANCE') if $patron->credit_forward_balance < 0;
223 $e->update_actor_user($patron) or return $e->die_event;
228 __PACKAGE__->register_method(
229 method => "retrieve_payments",
230 api_name => "open-ils.circ.money.payment.retrieve.all_",
231 notes => "Returns a list of payments attached to a given transaction"
234 sub retrieve_payments {
235 my( $self, $client, $login, $transid ) = @_;
238 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
241 # XXX the logic here is wrong.. we need to check the owner of the transaction
242 # to make sure the requestor has access
244 # XXX grab the view, for each object in the view, grab the real object
246 return $apputils->simplereq(
248 'open-ils.cstore.direct.money.payment.search.atomic', { xact => $transid } );
253 __PACKAGE__->register_method(
254 method => "retrieve_payments2",
256 api_name => "open-ils.circ.money.payment.retrieve.all",
257 notes => "Returns a list of payments attached to a given transaction"
260 sub retrieve_payments2 {
261 my( $self, $client, $login, $transid ) = @_;
263 my $e = new_editor(authtoken=>$login);
264 return $e->event unless $e->checkauth;
265 return $e->event unless $e->allowed('VIEW_TRANSACTION');
268 my $pmnts = $e->search_money_payment({ xact => $transid });
270 my $type = $_->payment_type;
271 my $meth = "retrieve_money_$type";
272 my $p = $e->$meth($_->id) or return $e->event;
273 $p->payment_type($type);
274 $p->cash_drawer($e->retrieve_actor_workstation($p->cash_drawer))
275 if $p->has_field('cash_drawer');
276 push( @payments, $p );
284 __PACKAGE__->register_method(
285 method => "create_grocery_bill",
286 api_name => "open-ils.circ.money.grocery.create",
288 Creates a new grocery transaction using the transaction object provided
289 PARAMS: (login_session, money.grocery (mg) object)
292 sub create_grocery_bill {
293 my( $self, $client, $login, $transaction ) = @_;
295 my( $staff, $evt ) = $apputils->checkses($login);
297 $evt = $apputils->check_perms($staff->id,
298 $transaction->billing_location, 'CREATE_TRANSACTION' );
302 $logger->activity("Creating grocery bill " . Dumper($transaction) );
304 $transaction->clear_id;
305 my $session = $apputils->start_db_session;
306 my $transid = $session->request(
307 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
309 throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
311 $logger->debug("Created new grocery transaction $transid");
313 $apputils->commit_db_session($session);
315 my $e = new_editor(xact=>1);
316 $evt = _check_open_xact($e, $transid);
324 __PACKAGE__->register_method(
325 method => 'fetch_grocery',
326 api_name => 'open-ils.circ.money.grocery.retrieve'
330 my( $self, $conn, $auth, $id ) = @_;
331 my $e = new_editor(authtoken=>$auth);
332 return $e->event unless $e->checkauth;
333 return $e->event unless $e->allowed('VIEW_TRANSACTION'); # eh.. basically the same permission
334 my $g = $e->retrieve_money_grocery($id)
340 __PACKAGE__->register_method(
341 method => "billing_items",
343 api_name => "open-ils.circ.money.billing.retrieve.all",
345 Returns a list of billing items for the given transaction.
346 PARAMS( login, transaction_id )
350 my( $self, $client, $login, $transid ) = @_;
352 my( $trans, $evt ) = $U->fetch_billable_xact($transid);
356 ($staff, $evt ) = $apputils->checkses($login);
359 if($staff->id ne $trans->usr) {
360 $evt = $U->check_perms($staff->id, $staff->home_ou, 'VIEW_TRANSACTION');
364 return $apputils->simplereq( 'open-ils.cstore',
365 'open-ils.cstore.direct.money.billing.search.atomic', { xact => $transid } )
369 __PACKAGE__->register_method(
370 method => "billing_items_create",
371 api_name => "open-ils.circ.money.billing.create",
373 Creates a new billing line item
374 PARAMS( login, bill_object (mb) )
377 sub billing_items_create {
378 my( $self, $client, $login, $billing ) = @_;
380 my $e = new_editor(authtoken => $login, xact => 1);
381 return $e->die_event unless $e->checkauth;
382 return $e->die_event unless $e->allowed('CREATE_BILL');
384 my $xact = $e->retrieve_money_billable_transaction($billing->xact)
385 or return $e->die_event;
387 # if the transaction was closed, re-open it
388 if($xact->xact_finish) {
389 $xact->clear_xact_finish;
390 $e->update_money_billable_transaction($xact)
391 or return $e->die_event;
394 my $amt = $billing->amount;
396 $billing->amount($amt);
398 $e->create_money_billing($billing) or return $e->die_event;
399 my $evt = OpenILS::Utils::Penalty->calculate_penalties($e, $xact->usr, $U->xact_org($xact->id));
406 __PACKAGE__->register_method(
407 method => 'void_bill',
408 api_name => 'open-ils.circ.money.billing.void',
411 @param authtoken Login session key
412 @param billid Id for the bill to void. This parameter may be repeated to reference other bills.
413 @return 1 on success, Event on error
419 my( $s, $c, $authtoken, @billids ) = @_;
421 my $e = new_editor( authtoken => $authtoken, xact => 1 );
422 return $e->die_event unless $e->checkauth;
423 return $e->die_event unless $e->allowed('VOID_BILLING');
426 for my $billid (@billids) {
428 my $bill = $e->retrieve_money_billing($billid)
429 or return $e->die_event;
431 my $xact = $e->retrieve_money_billable_transaction($bill->xact)
432 or return $e->die_event;
434 if($U->is_true($bill->voided)) {
436 return OpenILS::Event->new('BILL_ALREADY_VOIDED', payload => $bill);
439 my $org = $U->xact_org($bill->xact, $e);
440 $users{$xact->usr} = {} unless $users{$xact->usr};
441 $users{$xact->usr}->{$org} = 1;
444 $bill->voider($e->requestor->id);
445 $bill->void_time('now');
447 $e->update_money_billing($bill) or return $e->die_event;
448 my $evt = _check_open_xact($e, $bill->xact, $xact);
452 # calculate penalties for all user/org combinations
453 for my $user_id (keys %users) {
454 for my $org_id (keys %{$users{$user_id}}) {
455 OpenILS::Utils::Penalty->calculate_penalties($e, $user_id, $org_id);
463 __PACKAGE__->register_method(
464 method => 'edit_bill_note',
465 api_name => 'open-ils.circ.money.billing.note.edit',
467 Edits the note for a bill
468 @param authtoken Login session key
469 @param note The replacement note for the bills we're editing
470 @param billid Id for the bill to edit the note of. This parameter may be repeated to reference other bills.
471 @return 1 on success, Event on error
477 my( $s, $c, $authtoken, $note, @billids ) = @_;
479 my $e = new_editor( authtoken => $authtoken, xact => 1 );
480 return $e->die_event unless $e->checkauth;
481 return $e->die_event unless $e->allowed('UPDATE_BILL_NOTE');
483 for my $billid (@billids) {
485 my $bill = $e->retrieve_money_billing($billid)
486 or return $e->die_event;
489 # FIXME: Does this get audited? Need some way so that the original creator of the bill does not get credit/blame for the new note.
491 $e->update_money_billing($bill) or return $e->die_event;
498 __PACKAGE__->register_method(
499 method => 'edit_payment_note',
500 api_name => 'open-ils.circ.money.payment.note.edit',
502 Edits the note for a payment
503 @param authtoken Login session key
504 @param note The replacement note for the payments we're editing
505 @param paymentid Id for the payment to edit the note of. This parameter may be repeated to reference other payments.
506 @return 1 on success, Event on error
511 sub edit_payment_note {
512 my( $s, $c, $authtoken, $note, @paymentids ) = @_;
514 my $e = new_editor( authtoken => $authtoken, xact => 1 );
515 return $e->die_event unless $e->checkauth;
516 return $e->die_event unless $e->allowed('UPDATE_PAYMENT_NOTE');
518 for my $paymentid (@paymentids) {
520 my $payment = $e->retrieve_money_payment($paymentid)
521 or return $e->die_event;
523 $payment->note($note);
524 # FIXME: Does this get audited? Need some way so that the original taker of the payment does not get credit/blame for the new note.
526 $e->update_money_payment($payment) or return $e->die_event;
533 sub _check_open_xact {
534 my( $editor, $xactid, $xact ) = @_;
536 # Grab the transaction
537 $xact ||= $editor->retrieve_money_billable_transaction($xactid);
538 return $editor->event unless $xact;
539 $xactid ||= $xact->id;
541 # grab the summary and see how much is owed on this transaction
542 my ($summary) = $U->fetch_mbts($xactid, $editor);
544 # grab the circulation if it is a circ;
545 my $circ = $editor->retrieve_action_circulation($xactid);
547 # If nothing is owed on the transaction but it is still open
548 # and this transaction is not an open circulation, close it
550 ( $summary->balance_owed == 0 and ! $xact->xact_finish ) and
551 ( !$circ or $circ->stop_fines )) {
553 $logger->info("closing transaction ".$xact->id. ' becauase balance_owed == 0');
554 $xact->xact_finish('now');
555 $editor->update_money_billable_transaction($xact)
556 or return $editor->event;
560 # If money is owed or a refund is due on the xact and xact_finish
561 # is set, clear it (to reopen the xact) and update
562 if( $summary->balance_owed != 0 and $xact->xact_finish ) {
563 $logger->info("re-opening transaction ".$xact->id. ' becauase balance_owed != 0');
564 $xact->clear_xact_finish;
565 $editor->update_money_billable_transaction($xact)
566 or return $editor->event;
575 __PACKAGE__->register_method (
576 method => 'fetch_mbts',
578 api_name => 'open-ils.circ.money.billable_xact_summary.retrieve'
581 my( $self, $conn, $auth, $id) = @_;
583 my $e = new_editor(xact => 1, authtoken=>$auth);
584 return $e->event unless $e->checkauth;
585 my ($mbts) = $U->fetch_mbts($id, $e);
587 my $user = $e->retrieve_actor_user($mbts->usr)
588 or return $e->die_event;
590 return $e->die_event unless $e->allowed('VIEW_TRANSACTION', $user->home_ou);
597 __PACKAGE__->register_method(
598 method => 'desk_payments',
599 api_name => 'open-ils.circ.money.org_unit.desk_payments'
603 my( $self, $conn, $auth, $org, $start_date, $end_date ) = @_;
604 my $e = new_editor(authtoken=>$auth);
605 return $e->event unless $e->checkauth;
606 return $e->event unless $e->allowed('VIEW_TRANSACTION', $org);
607 my $data = $U->storagereq(
608 'open-ils.storage.money.org_unit.desk_payments.atomic',
609 $org, $start_date, $end_date );
611 $_->workstation( $_->workstation->name ) for(@$data);
616 __PACKAGE__->register_method(
617 method => 'user_payments',
618 api_name => 'open-ils.circ.money.org_unit.user_payments'
622 my( $self, $conn, $auth, $org, $start_date, $end_date ) = @_;
623 my $e = new_editor(authtoken=>$auth);
624 return $e->event unless $e->checkauth;
625 return $e->event unless $e->allowed('VIEW_TRANSACTION', $org);
626 my $data = $U->storagereq(
627 'open-ils.storage.money.org_unit.user_payments.atomic',
628 $org, $start_date, $end_date );
631 $e->retrieve_actor_card($_->usr->card)->barcode);
633 $e->retrieve_actor_org_unit($_->usr->home_ou)->shortname);