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";
23 use OpenSRF::EX qw(:try);
26 use OpenSRF::Utils::Logger qw/:logger/;
29 __PACKAGE__->register_method(
30 method => "make_payments",
31 api_name => "open-ils.circ.money.payment",
33 Pass in a structure like so:
35 cash_drawer: <string>,
36 payment_type : <string>,
43 patron_credit : <credit amt>
45 login must have CREATE_PAYMENT priveleges.
46 If any payments fail, all are reverted back.
51 my( $self, $client, $login, $payments ) = @_;
53 my( $user, $trans, $evt );
55 ( $user, $evt ) = $apputils->checkses($login);
57 $evt = $apputils->check_perms($user->id, $user->home_ou, 'CREATE_PAYMENT');
60 $logger->activity("Creating payment objects: " . Dumper($payments) );
62 my $session = $apputils->start_db_session;
63 my $type = $payments->{payment_type};
64 my $credit = $payments->{patron_credit};
65 my $drawer = $payments->{cash_drawer};
66 my $userid = $payments->{userid};
67 my $note = $payments->{note};
69 for my $pay (@{$payments->{payments}}) {
71 my $transid = $pay->[0];
72 my $amount = $pay->[1];
73 ($trans, $evt) = $apputils->fetch_open_billable_transaction($transid);
76 if($trans->usr != $userid) { # Do we need to restrict this in some way ??
77 $logger->info( " * User $userid is making a payment for " .
78 "a different user: " . $trans->usr . ' for transaction ' . $trans->id );
81 my $payobj = "Fieldmapper::money::$type";
82 $payobj = $payobj->new;
84 $payobj->amount($amount);
85 $payobj->amount_collected($amount);
86 $payobj->accepting_usr($user->id);
87 $payobj->xact($transid);
89 $payobj->cash_drawer($drawer);
91 # update the transaction if it's done
92 if( ($trans->balance_owed - $amount) <= 0 ) {
94 $logger->debug("Transactin " . $trans->id . ' is complete');
95 $trans = $session->request(
96 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
98 $trans->xact_finish("now");
99 my $s = $session->request(
100 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
102 if(!$s) { throw OpenSRF::EX::ERROR
103 ("Error updating billable_xact in circ.money.payment"); }
107 $logger->debug("Creating new $payobj for \$$amount\n");
109 my $s = $session->request(
110 "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
111 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
115 _update_patron_credit( $session, $userid, $credit );
117 $apputils->commit_db_session($session);
122 sub _update_patron_credit {
123 my( $session, $userid, $credit ) = @_;
124 return if $credit < 0;
126 my $patron = $session->request(
127 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
129 $patron->credit_forward_balance(
130 $patron->credit_forward_balance + $credit);
132 my $res = $session->request(
133 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
136 throw OpenSRF::EX("Error updating patron credit");
141 __PACKAGE__->register_method(
142 method => "retrieve_payments",
143 api_name => "open-ils.circ.money.payment.retrieve.all",
144 notes => "Returns a list of payments attached to a given transaction"
147 sub retrieve_payments {
148 my( $self, $client, $login, $transid ) = @_;
151 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
154 # XXX the logic here is wrong.. we need to check the owner of the transaction
155 # to make sure the requestor has access
157 return $apputils->simplereq(
159 'open-ils.storage.direct.money.payment.search.xact.atomic', $transid );
164 __PACKAGE__->register_method(
165 method => "create_grocery_bill",
166 api_name => "open-ils.circ.money.grocery.create",
168 Creates a new grocery transaction using the transaction object provided
169 PARAMS: (login_session, money.grocery (mg) object)
172 sub create_grocery_bill {
173 my( $self, $client, $login, $transaction ) = @_;
175 my( $staff, $evt ) = $apputils->checkses($login);
177 $evt = $apputils->check_perms($staff->id,
178 $transaction->billing_location, 'CREATE_TRANSACTION' );
182 $logger->activity("Creating grocery bill " . Dumper($transaction) );
184 $transaction->clear_id;
185 my $session = $apputils->start_db_session;
186 my $transid = $session->request(
187 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
189 throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
191 $logger->debug("Created new grocery transaction $transid");
193 $apputils->commit_db_session($session);
198 __PACKAGE__->register_method(
199 method => "billing_items",
200 api_name => "open-ils.circ.money.billing.retrieve.all",
202 Returns a list of billing items for the given transaction.
203 PARAMS( login, transaction_id )
207 my( $self, $client, $login, $transid ) = @_;
209 my( $staff, $evt ) = $apputils->checksesperm($login, 'VIEW_TRANSACTION');
212 # we need to grab the transaction by id and check the billing location
213 # to determin the permissibility XXX
215 # $evt = $apputils->check_perms($staff->id,
216 # $transaction->billing_location, 'VIEW_TRANSACTION' );
217 # return $evt if $evt;
219 return $apputils->simplereq( 'open-ils.storage',
220 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
225 __PACKAGE__->register_method(
226 method => "billing_items_create",
227 api_name => "open-ils.circ.money.billing.create",
229 Creates a new billing line item
230 PARAMS( login, bill_object (mb) )
233 sub billing_items_create {
234 my( $self, $client, $login, $billing ) = @_;
236 my( $staff, $evt ) = $apputils->checksesperm($login, 'CREATE_BILL');
239 my $session = $apputils->start_db_session;
241 my $id = $session->request(
242 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
244 throw OpenSRF::EX ("Error creating new bill") unless defined $id;
246 $apputils->commit_db_session($session);