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);
27 use OpenSRF::Utils::Logger qw/:logger/;
29 use OpenILS::Utils::Editor 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 $logger->info("Creating payment objects: " . Dumper($payments) );
64 my $session = $apputils->start_db_session;
65 my $type = $payments->{payment_type};
66 my $credit = $payments->{patron_credit} || 0;
67 my $drawer = $user->wsid;
68 my $userid = $payments->{userid};
69 my $note = $payments->{note};
70 my $cc_type = $payments->{cc_type} || 'n/a';
71 my $cc_number = $payments->{cc_number} || 'n/a';
72 my $expire_month = $payments->{expire_month};
73 my $expire_year = $payments->{expire_year};
74 my $approval_code = $payments->{approval_code} || 'n/a';
75 my $check_number = $payments->{check_number} || 'n/a';
77 for my $pay (@{$payments->{payments}}) {
79 my $transid = $pay->[0];
80 my $amount = $pay->[1];
81 ($trans, $evt) = $apputils->fetch_open_billable_transaction($transid);
84 if($trans->usr != $userid) { # Do we need to restrict this in some way ??
85 $logger->info( " * User $userid is making a payment for " .
86 "a different user: " . $trans->usr . ' for transaction ' . $trans->id );
89 if($type eq 'credit_payment') {
91 $logger->activity("user ".$user->id." reducing patron credit by ".
92 "$credit for making a credit_payment on transaction ".$trans->id);
96 # A negative payment is a refund. If the refund causes the transaction
97 # balance to exceed 0 dollars, we are in effect loaning the patron
98 # money. This is not allowed.
99 if( $amount < 0 and ($trans->balance_owed - $amount > 0) ) {
100 return OpenILS::Event->new('REFUND_EXCEEDS_BALANCE');
103 my $payobj = "Fieldmapper::money::$type";
104 $payobj = $payobj->new;
106 $payobj->amount($amount);
107 $payobj->amount_collected($amount);
108 $payobj->accepting_usr($user->id);
109 $payobj->xact($transid);
110 $payobj->note($note);
112 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
113 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
114 if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
115 if ($payobj->has_field('expire_month')) { $payobj->expire_month($expire_month); }
116 if ($payobj->has_field('expire_year')) { $payobj->expire_year($expire_year); }
117 if ($payobj->has_field('approval_code')) { $payobj->approval_code($approval_code); }
118 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
120 # update the transaction if it's done
121 if( (my $cred = ($trans->balance_owed - $amount)) <= 0 ) {
123 # Any overpay on this transaction goes directly into patron credit
126 $logger->activity("user ".$user->id." applying credit ".
127 "of $cred on transaction ".$trans->id. " because of overpayment");
129 $logger->debug("Transactin " . $trans->id . ' is complete');
130 $trans = $session->request(
131 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
133 $trans->xact_finish("now");
134 my $s = $session->request(
135 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
137 if(!$s) { throw OpenSRF::EX::ERROR
138 ("Error updating billable_xact in circ.money.payment"); }
143 $logger->debug("Creating new $payobj for \$$amount\n");
145 my $s = $session->request(
146 "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
147 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
152 $logger->activity("user ".$user->id." applying total ".
153 "credit of $credit to user $userid") if $credit != 0;
155 $evt = _update_patron_credit( $session, $userid, $credit );
158 $apputils->commit_db_session($session);
160 $client->respond_complete(1);
162 # ------------------------------------------------------------------------------
163 # Update the patron penalty info in the DB
164 # ------------------------------------------------------------------------------
165 $U->update_patron_penalties(
174 sub _update_patron_credit {
175 my( $session, $userid, $credit ) = @_;
176 #return if $credit <= 0;
178 my $patron = $session->request(
179 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
181 $patron->credit_forward_balance(
182 $patron->credit_forward_balance + $credit);
184 if( $patron->credit_forward_balance < 0 ) {
185 return OpenILS::Event->new('NEGATIVE_PATRON_BALANCE');
188 $logger->info("Total patron credit for $userid is now " . $patron->credit_forward_balance );
191 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
197 __PACKAGE__->register_method(
198 method => "retrieve_payments",
199 api_name => "open-ils.circ.money.payment.retrieve.all",
200 notes => "Returns a list of payments attached to a given transaction"
203 sub retrieve_payments {
204 my( $self, $client, $login, $transid ) = @_;
207 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
210 # XXX the logic here is wrong.. we need to check the owner of the transaction
211 # to make sure the requestor has access
213 return $apputils->simplereq(
215 'open-ils.storage.direct.money.payment.search.xact.atomic', $transid );
220 __PACKAGE__->register_method(
221 method => "create_grocery_bill",
222 api_name => "open-ils.circ.money.grocery.create",
224 Creates a new grocery transaction using the transaction object provided
225 PARAMS: (login_session, money.grocery (mg) object)
228 sub create_grocery_bill {
229 my( $self, $client, $login, $transaction ) = @_;
231 my( $staff, $evt ) = $apputils->checkses($login);
233 $evt = $apputils->check_perms($staff->id,
234 $transaction->billing_location, 'CREATE_TRANSACTION' );
238 $logger->activity("Creating grocery bill " . Dumper($transaction) );
240 $transaction->clear_id;
241 my $session = $apputils->start_db_session;
242 my $transid = $session->request(
243 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
245 throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
247 $logger->debug("Created new grocery transaction $transid");
249 $apputils->commit_db_session($session);
254 __PACKAGE__->register_method(
255 method => "billing_items",
256 api_name => "open-ils.circ.money.billing.retrieve.all",
258 Returns a list of billing items for the given transaction.
259 PARAMS( login, transaction_id )
263 my( $self, $client, $login, $transid ) = @_;
265 my( $trans, $evt ) = $U->fetch_billable_xact($transid);
269 ($staff, $evt ) = $apputils->checkses($login);
272 if($staff->id ne $trans->usr) {
273 $evt = $U->check_perms($staff->id, $staff->home_ou, 'VIEW_TRANSACTION');
277 return $apputils->simplereq( 'open-ils.storage',
278 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
282 __PACKAGE__->register_method(
283 method => "billing_items_create",
284 api_name => "open-ils.circ.money.billing.create",
286 Creates a new billing line item
287 PARAMS( login, bill_object (mb) )
290 sub billing_items_create {
291 my( $self, $client, $login, $billing ) = @_;
293 my $e = new_editor(authtoken => $login, xact => 1);
294 return $e->event unless $e->checkauth;
295 return $e->event unless $e->allowed('CREATE_BILL');
297 my $xact = $e->retrieve_money_billable_transaction($billing->xact)
300 # if the transaction was closed, re-open it
301 if($xact->xact_finish) {
302 $xact->clear_xact_finish;
303 $e->update_money_billable_transaction($xact)
307 $e->create_money_billing($billing) or return $e->event;
313 __PACKAGE__->register_method(
314 method => 'void_bill',
315 api_name => 'open-ils.circ.money.billing.void',
318 @param authtoken Login session key
319 @param billid The id of the bill to void
320 @return 1 on success, Event on error
325 my( $s, $c, $authtoken, $billid ) = @_;
327 my $e = OpenILS::Utils::Editor->new( authtoken => $authtoken );
328 return $e->event unless $e->checkauth;
329 return $e->event unless $e->allowed('VOID_BILLING');
331 my $bill = $e->retrieve_money_billing($billid)
334 return OpenILS::Event->new('BILL_ALREADY_VOIDED', payload => $bill)
335 if $bill->voided and $bill->voided =~ /t/io;
338 $bill->voider($e->requestor->id);
339 $bill->void_time('now');
341 $e->update_money_billing($bill) or return $e->event;
342 my $evt = _check_open_xact($e, $bill->xact);
349 sub _check_open_xact {
350 my( $editor, $xactid ) = @_;
352 # Grab the transaction
353 my $xact = $editor->retrieve_money_billable_transaction($xactid)
354 or return $editor->event;
356 # if it's still open, good.
357 return undef unless $xact->xact_finish;
359 $logger->info("re-opening transaction ".$xact->id);
360 my $finish = $xact->xact_finish;
362 # clear the transaction finish time
363 $xact->clear_xact_finish;
364 $editor->update_money_billable_transaction($xact);
366 # grab the summary and see how much is owed on this transaction
367 my $summary = $editor->retrieve_money_open_billable_transaction_summary($xactid)
368 or return $editor->event;
370 if( $summary->balance_owed == 0 ) {
371 # if nothing is owed, re-close the transaction
372 $xact->xact_finish($finish);
373 $editor->update_money_billable_transaction($xact);
380 __PACKAGE__->register_method (
381 method => 'fetch_mbts',
382 api_name => 'open-ils.circ.money.billable_xact_summary.retrieve'
385 my($s, $c, $authtoken, $id) = @_;
387 my $sum = $U->storagereq(
388 'open-ils.storage.direct.money.billable_transaction_summary.retrieve', $id );
389 return OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', id => $id) unless $sum;
391 my ($reqr, $evt) = $U->checkses($authtoken);
395 ($usr, $evt) = $U->fetch_user($sum->usr);
398 $evt = $U->check_perms($reqr->id, $usr->home_ou, 'VIEW_TRANSACTION');