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