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 $userid = $payments->{userid};
68 my $note = $payments->{note};
69 my $cc_type = $payments->{cc_type} || 'n/a';
70 my $cc_number = $payments->{cc_number} || 'n/a';
71 my $expire_month = $payments->{expire_month};
72 my $expire_year = $payments->{expire_year};
73 my $approval_code = $payments->{approval_code} || 'n/a';
74 my $check_number = $payments->{check_number} || 'n/a';
76 for my $pay (@{$payments->{payments}}) {
78 my $transid = $pay->[0];
79 my $amount = $pay->[1];
80 ($trans, $evt) = $apputils->fetch_open_billable_transaction($transid);
83 if($trans->usr != $userid) { # Do we need to restrict this in some way ??
84 $logger->info( " * User $userid is making a payment for " .
85 "a different user: " . $trans->usr . ' for transaction ' . $trans->id );
88 my $payobj = "Fieldmapper::money::$type";
89 $payobj = $payobj->new;
91 $payobj->amount($amount);
92 $payobj->amount_collected($amount);
93 $payobj->accepting_usr($user->id);
94 $payobj->xact($transid);
96 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
97 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
98 if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
99 if ($payobj->has_field('expire_month')) { $payobj->expire_month($expire_month); }
100 if ($payobj->has_field('expire_year')) { $payobj->expire_year($expire_year); }
101 if ($payobj->has_field('approval_code')) { $payobj->approval_code($approval_code); }
102 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
104 # update the transaction if it's done
105 if( ($trans->balance_owed - $amount) <= 0 ) {
107 $logger->debug("Transactin " . $trans->id . ' is complete');
108 $trans = $session->request(
109 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
111 $trans->xact_finish("now");
112 my $s = $session->request(
113 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
115 if(!$s) { throw OpenSRF::EX::ERROR
116 ("Error updating billable_xact in circ.money.payment"); }
120 $logger->debug("Creating new $payobj for \$$amount\n");
122 my $s = $session->request(
123 "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
124 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
128 _update_patron_credit( $session, $userid, $credit );
130 $apputils->commit_db_session($session);
135 sub _update_patron_credit {
136 my( $session, $userid, $credit ) = @_;
137 return if $credit <= 0;
139 my $patron = $session->request(
140 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
142 $logger->activity( "Adding to patron [$userid] credit: $credit" );
144 $patron->credit_forward_balance(
145 $patron->credit_forward_balance + $credit);
147 $logger->debug("Total patron credit is now " . $patron->credit_forward_balance );
149 my $res = $session->request(
150 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
153 throw OpenSRF::EX("Error updating patron credit");
158 __PACKAGE__->register_method(
159 method => "retrieve_payments",
160 api_name => "open-ils.circ.money.payment.retrieve.all",
161 notes => "Returns a list of payments attached to a given transaction"
164 sub retrieve_payments {
165 my( $self, $client, $login, $transid ) = @_;
168 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
171 # XXX the logic here is wrong.. we need to check the owner of the transaction
172 # to make sure the requestor has access
174 return $apputils->simplereq(
176 'open-ils.storage.direct.money.payment.search.xact.atomic', $transid );
181 __PACKAGE__->register_method(
182 method => "create_grocery_bill",
183 api_name => "open-ils.circ.money.grocery.create",
185 Creates a new grocery transaction using the transaction object provided
186 PARAMS: (login_session, money.grocery (mg) object)
189 sub create_grocery_bill {
190 my( $self, $client, $login, $transaction ) = @_;
192 my( $staff, $evt ) = $apputils->checkses($login);
194 $evt = $apputils->check_perms($staff->id,
195 $transaction->billing_location, 'CREATE_TRANSACTION' );
199 $logger->activity("Creating grocery bill " . Dumper($transaction) );
201 $transaction->clear_id;
202 my $session = $apputils->start_db_session;
203 my $transid = $session->request(
204 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
206 throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
208 $logger->debug("Created new grocery transaction $transid");
210 $apputils->commit_db_session($session);
215 __PACKAGE__->register_method(
216 method => "billing_items",
217 api_name => "open-ils.circ.money.billing.retrieve.all",
219 Returns a list of billing items for the given transaction.
220 PARAMS( login, transaction_id )
224 my( $self, $client, $login, $transid ) = @_;
226 my( $trans, $evt ) = $U->fetch_billable_xact($transid);
230 ($staff, $evt ) = $apputils->checkses($login);
233 if($staff->id ne $trans->usr) {
234 $evt = $U->check_perms($staff->id, $staff->home_ou, 'VIEW_TRANSACTION');
238 return $apputils->simplereq( 'open-ils.storage',
239 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
243 __PACKAGE__->register_method(
244 method => "billing_items_create",
245 api_name => "open-ils.circ.money.billing.create",
247 Creates a new billing line item
248 PARAMS( login, bill_object (mb) )
251 sub billing_items_create {
252 my( $self, $client, $login, $billing ) = @_;
254 my( $staff, $evt ) = $apputils->checksesperm($login, 'CREATE_BILL');
257 my $session = $apputils->start_db_session;
259 my $id = $session->request(
260 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
262 throw OpenSRF::EX ("Error creating new bill") unless defined $id;
264 $apputils->commit_db_session($session);