]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Money.pm
removed patron penalty info from circ scripts and from the circ code proper
[working/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->activity("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};
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                 my $payobj = "Fieldmapper::money::$type";
90                 $payobj = $payobj->new;
91
92                 $payobj->amount($amount);
93                 $payobj->amount_collected($amount);
94                 $payobj->accepting_usr($user->id);
95                 $payobj->xact($transid);
96                 $payobj->note($note);
97                 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
98                 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
99                 if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
100                 if ($payobj->has_field('expire_month')) { $payobj->expire_month($expire_month); }
101                 if ($payobj->has_field('expire_year')) { $payobj->expire_year($expire_year); }
102                 if ($payobj->has_field('approval_code')) { $payobj->approval_code($approval_code); }
103                 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
104                 
105                 # update the transaction if it's done 
106                 if( ($trans->balance_owed - $amount) <= 0 ) {
107
108                         $logger->debug("Transactin " . $trans->id . ' is complete');
109                         $trans = $session->request(
110                                 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
111
112                         $trans->xact_finish("now");
113                         my $s = $session->request(
114                                 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
115
116                         if(!$s) { throw OpenSRF::EX::ERROR 
117                                 ("Error updating billable_xact in circ.money.payment"); }
118                                         
119                 }
120
121                 $logger->debug("Creating new $payobj for \$$amount\n");
122
123                 my $s = $session->request(
124                         "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
125                 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
126
127         }
128
129         _update_patron_credit( $session, $userid, $credit );
130
131         $apputils->commit_db_session($session);
132
133         $client->respond_complete(1);   
134
135         # ------------------------------------------------------------------------------
136         # Update the patron penalty info in the DB
137         # ------------------------------------------------------------------------------
138         $U->update_patron_penalties( 
139                 authtoken => $login,
140                 patronid  => $userid,
141         );
142
143         return undef;
144 }
145
146
147 sub _update_patron_credit {
148         my( $session, $userid, $credit ) = @_;
149         return if $credit <= 0;
150
151         my $patron = $session->request( 
152                 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
153
154         $logger->activity( "Adding to patron [$userid] credit: $credit" );
155
156         $patron->credit_forward_balance( 
157                 $patron->credit_forward_balance + $credit);
158         
159         $logger->debug("Total patron credit is now " . $patron->credit_forward_balance );
160
161         my $res = $session->request(
162                 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
163
164         if(!$res) {
165                 throw OpenSRF::EX("Error updating patron credit");
166         }
167 }
168
169
170 __PACKAGE__->register_method(
171         method  => "retrieve_payments",
172         api_name        => "open-ils.circ.money.payment.retrieve.all",
173         notes           => "Returns a list of payments attached to a given transaction"
174         );
175         
176 sub retrieve_payments {
177         my( $self, $client, $login, $transid ) = @_;
178
179         my( $staff, $evt ) =  
180                 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
181         return $evt if $evt;
182
183         # XXX the logic here is wrong.. we need to check the owner of the transaction
184         # to make sure the requestor has access
185
186         return $apputils->simplereq(
187                 'open-ils.storage',
188                 'open-ils.storage.direct.money.payment.search.xact.atomic', $transid );
189 }
190
191
192
193 __PACKAGE__->register_method(
194         method  => "create_grocery_bill",
195         api_name        => "open-ils.circ.money.grocery.create",
196         notes           => <<"  NOTE");
197         Creates a new grocery transaction using the transaction object provided
198         PARAMS: (login_session, money.grocery (mg) object)
199         NOTE
200
201 sub create_grocery_bill {
202         my( $self, $client, $login, $transaction ) = @_;
203
204         my( $staff, $evt ) = $apputils->checkses($login);
205         return $evt if $evt;
206         $evt = $apputils->check_perms($staff->id, 
207                 $transaction->billing_location, 'CREATE_TRANSACTION' );
208         return $evt if $evt;
209
210
211         $logger->activity("Creating grocery bill " . Dumper($transaction) );
212
213         $transaction->clear_id;
214         my $session = $apputils->start_db_session;
215         my $transid = $session->request(
216                 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
217
218         throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
219
220         $logger->debug("Created new grocery transaction $transid");
221         
222         $apputils->commit_db_session($session);
223
224         return $transid;
225 }
226
227 __PACKAGE__->register_method(
228         method  => "billing_items",
229         api_name        => "open-ils.circ.money.billing.retrieve.all",
230         notes           =><<"   NOTE");
231         Returns a list of billing items for the given transaction.
232         PARAMS( login, transaction_id )
233         NOTE
234
235 sub billing_items {
236         my( $self, $client, $login, $transid ) = @_;
237
238         my( $trans, $evt ) = $U->fetch_billable_xact($transid);
239         return $evt if $evt;
240
241         my $staff;
242         ($staff, $evt ) = $apputils->checkses($login);
243         return $evt if $evt;
244
245         if($staff->id ne $trans->usr) {
246                 $evt = $U->check_perms($staff->id, $staff->home_ou, 'VIEW_TRANSACTION');
247                 return $evt if $evt;
248         }
249         
250         return $apputils->simplereq( 'open-ils.storage',
251                 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
252 }
253
254
255 __PACKAGE__->register_method(
256         method  => "billing_items_create",
257         api_name        => "open-ils.circ.money.billing.create",
258         notes           =><<"   NOTE");
259         Creates a new billing line item
260         PARAMS( login, bill_object (mb) )
261         NOTE
262
263 sub billing_items_create {
264         my( $self, $client, $login, $billing ) = @_;
265
266         my( $staff, $evt ) = $apputils->checksesperm($login, 'CREATE_BILL');
267         return $evt if $evt;
268
269         my $session = $apputils->start_db_session;
270
271         my $id = $session->request(
272                 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
273
274         throw OpenSRF::EX ("Error creating new bill") unless defined $id;
275
276         $apputils->commit_db_session($session);
277
278         return $id;
279 }
280
281
282
283
284
285 1;
286
287
288