]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Money.pm
preventing negative patron ballance and fixed typo
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / Money.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2005  Georgia Public Library Service 
3 # Bill Erickson <billserickson@gmail.com>
4
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.
9
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 # ---------------------------------------------------------------
15
16
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";
23
24 use OpenSRF::EX qw(:try);
25 use OpenILS::Perm;
26 use Data::Dumper;
27 use OpenSRF::Utils::Logger qw/:logger/;
28 use OpenILS::Event;
29
30 __PACKAGE__->register_method(
31         method  => "make_payments",
32         api_name        => "open-ils.circ.money.payment",
33         notes           => <<"  NOTE");
34         Pass in a structure like so:
35                 { 
36                         cash_drawer: <string>, 
37                         payment_type : <string>, 
38                         note : <string>, 
39                         userid : <id>,
40                         payments: [ 
41                                 [trans_id, amt], 
42                                 [...]
43                         ], 
44                         patron_credit : <credit amt> 
45                 }
46         login must have CREATE_PAYMENT priveleges.
47         If any payments fail, all are reverted back.
48         NOTE
49
50 sub make_payments {
51
52         my( $self, $client, $login, $payments ) = @_;
53
54         my( $user, $trans, $evt );
55
56         ( $user, $evt ) = $apputils->checkses($login);
57         return $evt if $evt;
58         $evt = $apputils->check_perms($user->id, $user->home_ou, 'CREATE_PAYMENT');
59         return $evt if $evt;
60
61         $logger->info("Creating payment objects: " . Dumper($payments) );
62
63         my $session = $apputils->start_db_session;
64         my $type                = $payments->{payment_type};
65         my $credit      = $payments->{patron_credit} || 0;
66         my $drawer      = $user->wsid;
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';
75
76         for my $pay (@{$payments->{payments}}) {
77
78                 my $transid = $pay->[0];
79                 my $amount = $pay->[1];
80                 ($trans, $evt) = $apputils->fetch_open_billable_transaction($transid);
81                 return $evt if $evt;
82
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  );
86                 }
87
88                 if($type eq 'credit_payment') {
89                         $credit -= $amount;
90                         $logger->activity("user ".$user->id." reducing patron credit by ".
91                                 "$credit for making a credit_payment on transaction ".$trans->id);
92                 }
93
94
95                 # A negative payment is a refund.  If the refund causes the transaction 
96                 # balance to exceed 0 dollars, we are in effect loaning the patron
97                 # money.  This is not allowed.
98                 if( $amount < 0 and ($trans->balance_owed - $amount > 0) ) {
99                         return OpenILS::Event->new('REFUND_EXCEEDS_BALANCE');
100                 }
101
102                 my $payobj = "Fieldmapper::money::$type";
103                 $payobj = $payobj->new;
104
105                 $payobj->amount($amount);
106                 $payobj->amount_collected($amount);
107                 $payobj->accepting_usr($user->id);
108                 $payobj->xact($transid);
109                 $payobj->note($note);
110
111                 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
112                 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
113                 if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
114                 if ($payobj->has_field('expire_month')) { $payobj->expire_month($expire_month); }
115                 if ($payobj->has_field('expire_year')) { $payobj->expire_year($expire_year); }
116                 if ($payobj->has_field('approval_code')) { $payobj->approval_code($approval_code); }
117                 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
118                 
119                 # update the transaction if it's done 
120                 if( (my $cred = ($trans->balance_owed - $amount)) <= 0 ) {
121
122                         # Any overpay on this transaction goes directly into patron credit 
123                         $cred = -$cred;
124                         $credit += $cred;
125                         $logger->activity("user ".$user->id." applying credit ".
126                                 "of $cred on transaction ".$trans->id. " because of overpayment");
127
128                         $logger->debug("Transactin " . $trans->id . ' is complete');
129                         $trans = $session->request(
130                                 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
131
132                         $trans->xact_finish("now");
133                         my $s = $session->request(
134                                 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
135
136                         if(!$s) { throw OpenSRF::EX::ERROR 
137                                 ("Error updating billable_xact in circ.money.payment"); }
138                                         
139                 }
140
141
142                 $logger->debug("Creating new $payobj for \$$amount\n");
143
144                 my $s = $session->request(
145                         "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
146                 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
147
148         }
149
150
151         $logger->activity("user ".$user->id." applying total ".
152                 "credit of $credit to user $userid") if $credit != 0;
153
154         $evt = _update_patron_credit( $session, $userid, $credit );
155         return $evt if $evt;
156
157         $apputils->commit_db_session($session);
158
159         $client->respond_complete(1);   
160
161         # ------------------------------------------------------------------------------
162         # Update the patron penalty info in the DB
163         # ------------------------------------------------------------------------------
164         $U->update_patron_penalties( 
165                 authtoken => $login,
166                 patronid  => $userid,
167         );
168
169         return undef;
170 }
171
172
173 sub _update_patron_credit {
174         my( $session, $userid, $credit ) = @_;
175         #return if $credit <= 0;
176
177         my $patron = $session->request( 
178                 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
179
180         $patron->credit_forward_balance( 
181                 $patron->credit_forward_balance + $credit);
182
183         if( $patron->credit_forward_balance < 0 ) {
184                 return OpenILS::Event->new('NEGATIVE_PATRON_BALANCE');
185         }
186         
187         $logger->info("Total patron credit for $userid is now " . $patron->credit_forward_balance );
188
189         $session->request( 
190                 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
191
192         return undef;
193 }
194
195
196 __PACKAGE__->register_method(
197         method  => "retrieve_payments",
198         api_name        => "open-ils.circ.money.payment.retrieve.all",
199         notes           => "Returns a list of payments attached to a given transaction"
200         );
201         
202 sub retrieve_payments {
203         my( $self, $client, $login, $transid ) = @_;
204
205         my( $staff, $evt ) =  
206                 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
207         return $evt if $evt;
208
209         # XXX the logic here is wrong.. we need to check the owner of the transaction
210         # to make sure the requestor has access
211
212         return $apputils->simplereq(
213                 'open-ils.storage',
214                 'open-ils.storage.direct.money.payment.search.xact.atomic', $transid );
215 }
216
217
218
219 __PACKAGE__->register_method(
220         method  => "create_grocery_bill",
221         api_name        => "open-ils.circ.money.grocery.create",
222         notes           => <<"  NOTE");
223         Creates a new grocery transaction using the transaction object provided
224         PARAMS: (login_session, money.grocery (mg) object)
225         NOTE
226
227 sub create_grocery_bill {
228         my( $self, $client, $login, $transaction ) = @_;
229
230         my( $staff, $evt ) = $apputils->checkses($login);
231         return $evt if $evt;
232         $evt = $apputils->check_perms($staff->id, 
233                 $transaction->billing_location, 'CREATE_TRANSACTION' );
234         return $evt if $evt;
235
236
237         $logger->activity("Creating grocery bill " . Dumper($transaction) );
238
239         $transaction->clear_id;
240         my $session = $apputils->start_db_session;
241         my $transid = $session->request(
242                 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
243
244         throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
245
246         $logger->debug("Created new grocery transaction $transid");
247         
248         $apputils->commit_db_session($session);
249
250         return $transid;
251 }
252
253 __PACKAGE__->register_method(
254         method  => "billing_items",
255         api_name        => "open-ils.circ.money.billing.retrieve.all",
256         notes           =><<"   NOTE");
257         Returns a list of billing items for the given transaction.
258         PARAMS( login, transaction_id )
259         NOTE
260
261 sub billing_items {
262         my( $self, $client, $login, $transid ) = @_;
263
264         my( $trans, $evt ) = $U->fetch_billable_xact($transid);
265         return $evt if $evt;
266
267         my $staff;
268         ($staff, $evt ) = $apputils->checkses($login);
269         return $evt if $evt;
270
271         if($staff->id ne $trans->usr) {
272                 $evt = $U->check_perms($staff->id, $staff->home_ou, 'VIEW_TRANSACTION');
273                 return $evt if $evt;
274         }
275         
276         return $apputils->simplereq( 'open-ils.storage',
277                 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
278 }
279
280
281 __PACKAGE__->register_method(
282         method  => "billing_items_create",
283         api_name        => "open-ils.circ.money.billing.create",
284         notes           =><<"   NOTE");
285         Creates a new billing line item
286         PARAMS( login, bill_object (mb) )
287         NOTE
288
289 sub billing_items_create {
290         my( $self, $client, $login, $billing ) = @_;
291
292         my( $staff, $evt ) = $apputils->checksesperm($login, 'CREATE_BILL');
293         return $evt if $evt;
294
295         my $session = $apputils->start_db_session;
296
297         my $id = $session->request(
298                 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
299
300         return $U->DB_UPDATE_FAILED($billing) unless defined $id;
301
302         $apputils->commit_db_session($session);
303
304         return $id;
305 }
306
307 __PACKAGE__->register_method(
308         method          =>      'void_bill',
309         api_name                => 'open-ils.circ.money.billing.void',
310         signature       => q/
311                 Voids a bill
312                 @param authtoken Login session key
313                 @param billid The id of the bill to void
314                 @return 1 on success, Event on error
315         /
316 );
317
318 sub void_bill {
319         my( $s, $c, $authtoken, $billid ) = @_;
320
321         my $reqr;
322         my( $bill, $evt ) = $U->fetch_bill($billid);
323         return $evt if $evt;
324
325
326         ($reqr, $evt) = $U->checkses($authtoken);
327         return $evt if $evt;
328         $evt = $U->check_perms($reqr->id, $reqr->ws_ou, 'VOID_BILLING');
329         return $evt if $evt;
330
331         $bill->voided('t');
332         $bill->voider($reqr->id);
333         $bill->void_time('now');
334
335         my $stat = $U->storagereq(
336                 'open-ils.storage.direct.money.billing.update', $bill);
337         return $U->DB_UPDATE_FAILED($bill) unless defined $stat;
338
339         return 1;
340 }
341
342 __PACKAGE__->register_method (
343         method => 'fetch_mbts',
344         api_name => 'open-ils.circ.money.billable_xact_summary.retrieve'
345 );
346 sub fetch_mbts {
347         my($s, $c, $authtoken, $id) = @_;
348
349         my $sum = $U->storagereq(
350                 'open-ils.storage.direct.money.billable_transaction_summary.retrieve', $id );
351         return OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', id => $id) unless $sum;
352
353         my ($reqr, $evt) = $U->checkses($authtoken);
354         return $evt if $evt;
355
356         my $usr;
357         ($usr, $evt) = $U->fetch_user($sum->usr);
358         return $evt if $evt;
359
360         $evt = $U->check_perms($reqr->id, $usr->home_ou, 'VIEW_TRANSACTION');
361         return $evt if $evt;
362
363         return $sum;
364 }
365
366
367
368
369
370 1;
371
372
373