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);
27 __PACKAGE__->register_method(
28 method => "make_payments",
29 api_name => "open-ils.circ.money.payment",
31 Pass in a structure like so:
33 cash_drawer: <string>,
34 payment_type : <string>,
41 patron_credit : <credit amt>
43 login must have CREATE_PAYMENT priveleges.
44 If any payments fail, all are reverted back.
49 my( $self, $client, $login, $payments ) = @_;
50 my $user = $apputils->check_user_session($login);
52 if($apputils->check_user_perms($user->id, $user->home_ou, "CREATE_PAYMENT")) {
53 return OpenILS::Perm->new("CREATE_PAYMENT");
57 warn Dumper $payments;
59 my $session = $apputils->start_db_session;
60 my $type = $payments->{payment_type};
61 my $credit = $payments->{patron_credit};
62 my $drawer = $payments->{cash_drawer};
63 my $userid = $payments->{userid};
64 my $note = $payments->{note};
66 for my $pay (@{$payments->{payments}}) {
68 my $transid = $pay->[0];
69 my $amount = $pay->[1];
70 my $trans = $session->request(
71 "open-ils.storage.direct.money.open_billable_transaction_summary.retrieve",
72 $transid )->gather(1);
74 return OpenILS::EX->new("NO_TRANSACTION_FOUND")->ex unless $trans;
76 if($trans->usr != $userid) { # XXX exception
77 warn "Userid $userid does not match the user " . $trans->usr .
78 "attached to transaction " . $trans->id . "\n";
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 warn "Transaction is complete, updating...\n";
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);
101 if(!$s) { throw OpenSRF::EX::ERROR
102 ("Error updating billable_xact in circ.money.payment"); }
106 warn "Creating new $type object for \$$amount\n";
108 my $s = $session->request(
109 "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
110 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
114 _update_patron_credit( $session, $userid, $credit );
116 $apputils->commit_db_session($session);
121 sub _update_patron_credit {
122 my( $session, $userid, $credit ) = @_;
123 return if $credit < 0;
125 my $patron = $session->request(
126 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
128 $patron->credit_forward_balance(
129 $patron->credit_forward_balance + $credit);
131 my $res = $session->request(
132 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
135 throw OpenSRF::EX("Error updating patron credit");
140 __PACKAGE__->register_method(
141 method => "retrieve_payments",
142 api_name => "open-ils.circ.money.payment.retrieve.all",
143 notes => "Returns a list of payments attached to a given transaction"
146 sub retrieve_payments {
147 my( $self, $client, $login, $transid ) = @_;
149 my $staff = $apputils->check_user_session($login);
150 if($apputils->check_user_perms($staff->id,
151 $staff->home_ou, "VIEW_TRANSACTION")) {
152 return OpenILS::Perm->new("VIEW_TRANSACTION");
155 return $apputils->simple_scalar_request(
157 'open-ils.storage.direct.money.payment.search.xact.atomic', $transid );
162 __PACKAGE__->register_method(
163 method => "create_grocery_bill",
164 api_name => "open-ils.circ.money.grocery.create",
166 Creates a new grocery transaction using the transaction object provided
167 PARAMS: (login_session, money.grocery (mg) object)
170 sub create_grocery_bill {
171 my( $self, $client, $login, $transaction ) = @_;
173 my $staff = $apputils->check_user_session($login);
174 if($apputils->check_user_perms($staff->id,
175 $transaction->billing_location, "CREATE_TRANSACTION")) {
176 return OpenILS::Perm->new("CREATE_TRANSACTION");
180 warn "Grocery Transaction: " . Dumper($transaction) ."\n";
182 $transaction->clear_id;
183 my $session = $apputils->start_db_session;
184 my $transid = $session->request(
185 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
188 throw OpenSRF::EX ("Error creating new money.grocery");
191 warn "created new grocery transaction $transid\n";
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 = $apputils->check_user_session($login);
210 if($apputils->check_user_perms($staff->id,
211 $staff->home_ou, "VIEW_TRANSACTION")) {
212 return OpenILS::Perm->new("VIEW_TRANSACTION");
215 return $apputils->simple_scalar_request(
217 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
221 __PACKAGE__->register_method(
222 method => "billing_items_create",
223 api_name => "open-ils.circ.money.billing.create",
225 Creates a new billing line item
226 PARAMS( login, bill_object (mb) )
229 sub billing_items_create {
230 my( $self, $client, $login, $billing ) = @_;
232 my $staff = $apputils->check_user_session($login);
233 if($apputils->check_user_perms($staff->id,
234 $staff->home_ou, "CREATE_BILL")) {
235 return OpenILS::Perm->new("CREATE_BILL");
238 my $session = $apputils->start_db_session;
240 my $id = $session->request(
241 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
244 throw OpenSRF::EX ("Error creating new bill");
247 $apputils->commit_db_session($session);