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/;
30 __PACKAGE__->register_method(
31 method => "make_payments",
32 api_name => "open-ils.circ.money.payment",
34 Pass in a structure like so:
36 cash_drawer: <string>,
37 payment_type : <string>,
44 patron_credit : <credit amt>
46 login must have CREATE_PAYMENT priveleges.
47 If any payments fail, all are reverted back.
52 my( $self, $client, $login, $payments ) = @_;
54 my( $user, $trans, $evt );
56 ( $user, $evt ) = $apputils->checkses($login);
58 $evt = $apputils->check_perms($user->id, $user->home_ou, 'CREATE_PAYMENT');
61 $logger->activity("Creating payment objects: " . Dumper($payments) );
63 my $session = $apputils->start_db_session;
64 my $type = $payments->{payment_type};
65 my $credit = $payments->{patron_credit};
66 #my $drawer = $payments->{cash_drawer};
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 my $payobj = "Fieldmapper::money::$type";
90 $payobj = $payobj->new;
92 $payobj->amount($amount);
93 $payobj->amount_collected($amount);
94 $payobj->accepting_usr($user->id);
95 $payobj->xact($transid);
97 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
98 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
99 if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
100 if ($payobj->has_field('expire_month')) { $payobj->expire_month($expire_month); }
101 if ($payobj->has_field('expire_year')) { $payobj->expire_year($expire_year); }
102 if ($payobj->has_field('approval_code')) { $payobj->approval_code($approval_code); }
103 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
105 # update the transaction if it's done
106 if( ($trans->balance_owed - $amount) <= 0 ) {
108 $logger->debug("Transactin " . $trans->id . ' is complete');
109 $trans = $session->request(
110 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
112 $trans->xact_finish("now");
113 my $s = $session->request(
114 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
116 if(!$s) { throw OpenSRF::EX::ERROR
117 ("Error updating billable_xact in circ.money.payment"); }
121 $logger->debug("Creating new $payobj for \$$amount\n");
123 my $s = $session->request(
124 "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
125 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
129 _update_patron_credit( $session, $userid, $credit );
131 $apputils->commit_db_session($session);
133 $client->respond_complete(1);
135 # ------------------------------------------------------------------------------
136 # Update the patron penalty info in the DB
137 # ------------------------------------------------------------------------------
138 $U->update_patron_penalties(
147 sub _update_patron_credit {
148 my( $session, $userid, $credit ) = @_;
149 return if $credit <= 0;
151 my $patron = $session->request(
152 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
154 $logger->activity( "Adding to patron [$userid] credit: $credit" );
156 $patron->credit_forward_balance(
157 $patron->credit_forward_balance + $credit);
159 $logger->debug("Total patron credit is now " . $patron->credit_forward_balance );
161 my $res = $session->request(
162 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
165 throw OpenSRF::EX("Error updating patron credit");
170 __PACKAGE__->register_method(
171 method => "retrieve_payments",
172 api_name => "open-ils.circ.money.payment.retrieve.all",
173 notes => "Returns a list of payments attached to a given transaction"
176 sub retrieve_payments {
177 my( $self, $client, $login, $transid ) = @_;
180 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
183 # XXX the logic here is wrong.. we need to check the owner of the transaction
184 # to make sure the requestor has access
186 return $apputils->simplereq(
188 'open-ils.storage.direct.money.payment.search.xact.atomic', $transid );
193 __PACKAGE__->register_method(
194 method => "create_grocery_bill",
195 api_name => "open-ils.circ.money.grocery.create",
197 Creates a new grocery transaction using the transaction object provided
198 PARAMS: (login_session, money.grocery (mg) object)
201 sub create_grocery_bill {
202 my( $self, $client, $login, $transaction ) = @_;
204 my( $staff, $evt ) = $apputils->checkses($login);
206 $evt = $apputils->check_perms($staff->id,
207 $transaction->billing_location, 'CREATE_TRANSACTION' );
211 $logger->activity("Creating grocery bill " . Dumper($transaction) );
213 $transaction->clear_id;
214 my $session = $apputils->start_db_session;
215 my $transid = $session->request(
216 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
218 throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
220 $logger->debug("Created new grocery transaction $transid");
222 $apputils->commit_db_session($session);
227 __PACKAGE__->register_method(
228 method => "billing_items",
229 api_name => "open-ils.circ.money.billing.retrieve.all",
231 Returns a list of billing items for the given transaction.
232 PARAMS( login, transaction_id )
236 my( $self, $client, $login, $transid ) = @_;
238 my( $trans, $evt ) = $U->fetch_billable_xact($transid);
242 ($staff, $evt ) = $apputils->checkses($login);
245 if($staff->id ne $trans->usr) {
246 $evt = $U->check_perms($staff->id, $staff->home_ou, 'VIEW_TRANSACTION');
250 return $apputils->simplereq( 'open-ils.storage',
251 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
255 __PACKAGE__->register_method(
256 method => "billing_items_create",
257 api_name => "open-ils.circ.money.billing.create",
259 Creates a new billing line item
260 PARAMS( login, bill_object (mb) )
263 sub billing_items_create {
264 my( $self, $client, $login, $billing ) = @_;
266 my( $staff, $evt ) = $apputils->checksesperm($login, 'CREATE_BILL');
269 my $session = $apputils->start_db_session;
271 my $id = $session->request(
272 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
274 throw OpenSRF::EX ("Error creating new bill") unless defined $id;
276 $apputils->commit_db_session($session);