]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Money.pm
credit_payment now reduces the patron's credit
[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
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     = $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';
76
77         for my $pay (@{$payments->{payments}}) {
78
79                 my $transid = $pay->[0];
80                 my $amount = $pay->[1];
81                 ($trans, $evt) = $apputils->fetch_open_billable_transaction($transid);
82                 return $evt if $evt;
83
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  );
87                 }
88
89                 if($type == 'credit_payment') {
90                         $credit -= $amount;
91                         $logger->activity("user ".$user->id." reducing patron credit to ".
92                                 "$credit by making a credit_payment on transaction ".$trans->id);
93                 }
94
95                 my $payobj = "Fieldmapper::money::$type";
96                 $payobj = $payobj->new;
97
98                 $payobj->amount($amount);
99                 $payobj->amount_collected($amount);
100                 $payobj->accepting_usr($user->id);
101                 $payobj->xact($transid);
102                 $payobj->note($note);
103
104                 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
105                 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
106                 if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
107                 if ($payobj->has_field('expire_month')) { $payobj->expire_month($expire_month); }
108                 if ($payobj->has_field('expire_year')) { $payobj->expire_year($expire_year); }
109                 if ($payobj->has_field('approval_code')) { $payobj->approval_code($approval_code); }
110                 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
111                 
112                 # update the transaction if it's done 
113                 if( (my $cred = ($trans->balance_owed - $amount)) <= 0 ) {
114
115                         # Any overpay on this transaction goes directly into patron credit 
116                         $cred = -$cred;
117                         $credit += $cred;
118                         $logger->activity("user ".$user->id." applying credit ".
119                                 "of $cred on transaction ".$trans->id. " because of overpayment");
120
121                         $logger->debug("Transactin " . $trans->id . ' is complete');
122                         $trans = $session->request(
123                                 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
124
125                         $trans->xact_finish("now");
126                         my $s = $session->request(
127                                 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
128
129                         if(!$s) { throw OpenSRF::EX::ERROR 
130                                 ("Error updating billable_xact in circ.money.payment"); }
131                                         
132                 }
133
134
135                 $logger->debug("Creating new $payobj for \$$amount\n");
136
137                 my $s = $session->request(
138                         "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
139                 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
140
141         }
142
143
144         $logger->activity("user ".$user->id." applying total ".
145                 "credit of $credit to user $userid") if $credit;
146
147         _update_patron_credit( $session, $userid, $credit );
148
149         $apputils->commit_db_session($session);
150
151         $client->respond_complete(1);   
152
153         # ------------------------------------------------------------------------------
154         # Update the patron penalty info in the DB
155         # ------------------------------------------------------------------------------
156         $U->update_patron_penalties( 
157                 authtoken => $login,
158                 patronid  => $userid,
159         );
160
161         return undef;
162 }
163
164
165 sub _update_patron_credit {
166         my( $session, $userid, $credit ) = @_;
167         #return if $credit <= 0;
168
169         my $patron = $session->request( 
170                 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
171
172         $patron->credit_forward_balance( 
173                 $patron->credit_forward_balance + $credit);
174         
175         $logger->info("Total patron credit for $userid is now " . $patron->credit_forward_balance );
176
177         my $res = $session->request(
178                 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
179
180         if(!$res) {
181                 throw OpenSRF::EX("Error updating patron credit");
182         }
183 }
184
185
186 __PACKAGE__->register_method(
187         method  => "retrieve_payments",
188         api_name        => "open-ils.circ.money.payment.retrieve.all",
189         notes           => "Returns a list of payments attached to a given transaction"
190         );
191         
192 sub retrieve_payments {
193         my( $self, $client, $login, $transid ) = @_;
194
195         my( $staff, $evt ) =  
196                 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
197         return $evt if $evt;
198
199         # XXX the logic here is wrong.. we need to check the owner of the transaction
200         # to make sure the requestor has access
201
202         return $apputils->simplereq(
203                 'open-ils.storage',
204                 'open-ils.storage.direct.money.payment.search.xact.atomic', $transid );
205 }
206
207
208
209 __PACKAGE__->register_method(
210         method  => "create_grocery_bill",
211         api_name        => "open-ils.circ.money.grocery.create",
212         notes           => <<"  NOTE");
213         Creates a new grocery transaction using the transaction object provided
214         PARAMS: (login_session, money.grocery (mg) object)
215         NOTE
216
217 sub create_grocery_bill {
218         my( $self, $client, $login, $transaction ) = @_;
219
220         my( $staff, $evt ) = $apputils->checkses($login);
221         return $evt if $evt;
222         $evt = $apputils->check_perms($staff->id, 
223                 $transaction->billing_location, 'CREATE_TRANSACTION' );
224         return $evt if $evt;
225
226
227         $logger->activity("Creating grocery bill " . Dumper($transaction) );
228
229         $transaction->clear_id;
230         my $session = $apputils->start_db_session;
231         my $transid = $session->request(
232                 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
233
234         throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
235
236         $logger->debug("Created new grocery transaction $transid");
237         
238         $apputils->commit_db_session($session);
239
240         return $transid;
241 }
242
243 __PACKAGE__->register_method(
244         method  => "billing_items",
245         api_name        => "open-ils.circ.money.billing.retrieve.all",
246         notes           =><<"   NOTE");
247         Returns a list of billing items for the given transaction.
248         PARAMS( login, transaction_id )
249         NOTE
250
251 sub billing_items {
252         my( $self, $client, $login, $transid ) = @_;
253
254         my( $trans, $evt ) = $U->fetch_billable_xact($transid);
255         return $evt if $evt;
256
257         my $staff;
258         ($staff, $evt ) = $apputils->checkses($login);
259         return $evt if $evt;
260
261         if($staff->id ne $trans->usr) {
262                 $evt = $U->check_perms($staff->id, $staff->home_ou, 'VIEW_TRANSACTION');
263                 return $evt if $evt;
264         }
265         
266         return $apputils->simplereq( 'open-ils.storage',
267                 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
268 }
269
270
271 __PACKAGE__->register_method(
272         method  => "billing_items_create",
273         api_name        => "open-ils.circ.money.billing.create",
274         notes           =><<"   NOTE");
275         Creates a new billing line item
276         PARAMS( login, bill_object (mb) )
277         NOTE
278
279 sub billing_items_create {
280         my( $self, $client, $login, $billing ) = @_;
281
282         my( $staff, $evt ) = $apputils->checksesperm($login, 'CREATE_BILL');
283         return $evt if $evt;
284
285         my $session = $apputils->start_db_session;
286
287         my $id = $session->request(
288                 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
289
290         return $U->DB_UPDATE_FAILED($billing) unless defined $id;
291
292         $apputils->commit_db_session($session);
293
294         return $id;
295 }
296
297 __PACKAGE__->register_method(
298         method          =>      'void_bill',
299         api_name                => 'open-ils.circ.money.billing.void',
300         signature       => q/
301                 Voids a bill
302                 @param authtoken Login session key
303                 @param billid The id of the bill to void
304                 @return 1 on success, Event on error
305         /
306 );
307
308 sub void_bill {
309         my( $s, $c, $authtoken, $billid ) = @_;
310
311         my $reqr;
312         my( $bill, $evt ) = $U->fetch_bill($billid);
313         return $evt if $evt;
314
315
316         ($reqr, $evt) = $U->checkses($authtoken);
317         return $evt if $evt;
318         $evt = $U->check_perms($reqr->id, $reqr->ws_ou, 'VOID_BILLING');
319         return $evt if $evt;
320
321         $bill->voided('t');
322         $bill->voider($reqr->id);
323         $bill->void_time('now');
324
325         my $stat = $U->storagereq(
326                 'open-ils.storage.direct.money.billing.update', $bill);
327         return $U->DB_UPDATE_FAILED($bill) unless defined $stat;
328
329         return 1;
330 }
331
332
333
334
335
336 1;
337
338
339