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