]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Money.pm
some small cleaning
[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
23 use OpenSRF::EX qw(:try);
24 use OpenILS::Perm;
25 use Data::Dumper;
26 use OpenSRF::Utils::Logger qw/:logger/;
27
28
29 __PACKAGE__->register_method(
30         method  => "make_payments",
31         api_name        => "open-ils.circ.money.payment",
32         notes           => <<"  NOTE");
33         Pass in a structure like so:
34                 { 
35                         cash_drawer: <string>, 
36                         payment_type : <string>, 
37                         note : <string>, 
38                         userid : <id>,
39                         payments: [ 
40                                 [trans_id, amt], 
41                                 [...]
42                         ], 
43                         patron_credit : <credit amt> 
44                 }
45         login must have CREATE_PAYMENT priveleges.
46         If any payments fail, all are reverted back.
47         NOTE
48
49 sub make_payments {
50
51         my( $self, $client, $login, $payments ) = @_;
52
53         my( $user, $trans, $evt );
54
55         ( $user, $evt ) = $apputils->checkses($login);
56         return $evt if $evt;
57         $evt = $apputils->check_perms($user->id, $user->home_ou, 'CREATE_PAYMENT');
58         return $evt if $evt;
59
60         $logger->activity("Creating payment objects: " . Dumper($payments) );
61
62         my $session = $apputils->start_db_session;
63         my $type                = $payments->{payment_type};
64         my $credit      = $payments->{patron_credit};
65         my $drawer      = $payments->{cash_drawer};
66         my $userid      = $payments->{userid};
67         my $note                = $payments->{note};
68         my $cc_type = $payments->{cc_type} || 'n/a';
69         my $cc_number = $payments->{cc_number} || 'n/a';
70         my $expire_month = $payments->{expire_month};
71         my $expire_year = $payments->{expire_year};
72         my $approval_code = $payments->{approval_code} || 'n/a';
73         my $check_number = $payments->{check_number} || 'n/a';
74
75         for my $pay (@{$payments->{payments}}) {
76
77                 my $transid = $pay->[0];
78                 my $amount = $pay->[1];
79                 ($trans, $evt) = $apputils->fetch_open_billable_transaction($transid);
80                 return $evt if $evt;
81
82                 if($trans->usr != $userid) { # Do we need to restrict this in some way ??
83                         $logger->info( " * User $userid is making a payment for " . 
84                                 "a different user: " .  $trans->usr . ' for transaction ' . $trans->id  );
85                 }
86
87                 my $payobj = "Fieldmapper::money::$type";
88                 $payobj = $payobj->new;
89
90                 $payobj->amount($amount);
91                 $payobj->amount_collected($amount);
92                 $payobj->accepting_usr($user->id);
93                 $payobj->xact($transid);
94                 $payobj->note($note);
95                 if ($payobj->has_field('cash_drawer')) { $payobj->cash_drawer($drawer); }
96                 if ($payobj->has_field('cc_type')) { $payobj->cc_type($cc_type); }
97                 if ($payobj->has_field('cc_number')) { $payobj->cc_number($cc_number); }
98                 if ($payobj->has_field('expire_month')) { $payobj->expire_month($expire_month); }
99                 if ($payobj->has_field('expire_year')) { $payobj->expire_year($expire_year); }
100                 if ($payobj->has_field('approval_code')) { $payobj->approval_code($approval_code); }
101                 if ($payobj->has_field('check_number')) { $payobj->check_number($check_number); }
102                 
103                 # update the transaction if it's done 
104                 if( ($trans->balance_owed - $amount) <= 0 ) {
105
106                         $logger->debug("Transactin " . $trans->id . ' is complete');
107                         $trans = $session->request(
108                                 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
109
110                         $trans->xact_finish("now");
111                         my $s = $session->request(
112                                 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
113
114                         if(!$s) { throw OpenSRF::EX::ERROR 
115                                 ("Error updating billable_xact in circ.money.payment"); }
116                                         
117                 }
118
119                 $logger->debug("Creating new $payobj for \$$amount\n");
120
121                 my $s = $session->request(
122                         "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
123                 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
124
125         }
126
127         _update_patron_credit( $session, $userid, $credit );
128
129         $apputils->commit_db_session($session);
130         return 1;
131                 
132 }
133
134 sub _update_patron_credit {
135         my( $session, $userid, $credit ) = @_;
136         return if $credit <= 0;
137
138         my $patron = $session->request( 
139                 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
140
141         $logger->activity( "Adding to patron [$userid] credit: $credit" );
142
143         $patron->credit_forward_balance( 
144                 $patron->credit_forward_balance + $credit);
145         
146         $logger->debug("Total patron credit is now " . $patron->credit_forward_balance );
147
148         my $res = $session->request(
149                 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
150
151         if(!$res) {
152                 throw OpenSRF::EX("Error updating patron credit");
153         }
154 }
155
156
157 __PACKAGE__->register_method(
158         method  => "retrieve_payments",
159         api_name        => "open-ils.circ.money.payment.retrieve.all",
160         notes           => "Returns a list of payments attached to a given transaction"
161         );
162         
163 sub retrieve_payments {
164         my( $self, $client, $login, $transid ) = @_;
165
166         my( $staff, $evt ) =  
167                 $apputils->checksesperm($login, 'VIEW_TRANSACTION');
168         return $evt if $evt;
169
170         # XXX the logic here is wrong.. we need to check the owner of the transaction
171         # to make sure the requestor has access
172
173         return $apputils->simplereq(
174                 'open-ils.storage',
175                 'open-ils.storage.direct.money.payment.search.xact.atomic', $transid );
176 }
177
178
179
180 __PACKAGE__->register_method(
181         method  => "create_grocery_bill",
182         api_name        => "open-ils.circ.money.grocery.create",
183         notes           => <<"  NOTE");
184         Creates a new grocery transaction using the transaction object provided
185         PARAMS: (login_session, money.grocery (mg) object)
186         NOTE
187
188 sub create_grocery_bill {
189         my( $self, $client, $login, $transaction ) = @_;
190
191         my( $staff, $evt ) = $apputils->checkses($login);
192         return $evt if $evt;
193         $evt = $apputils->check_perms($staff->id, 
194                 $transaction->billing_location, 'CREATE_TRANSACTION' );
195         return $evt if $evt;
196
197
198         $logger->activity("Creating grocery bill " . Dumper($transaction) );
199
200         $transaction->clear_id;
201         my $session = $apputils->start_db_session;
202         my $transid = $session->request(
203                 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
204
205         throw OpenSRF::EX ("Error creating new money.grocery") unless defined $transid;
206
207         $logger->debug("Created new grocery transaction $transid");
208         
209         $apputils->commit_db_session($session);
210
211         return $transid;
212 }
213
214 __PACKAGE__->register_method(
215         method  => "billing_items",
216         api_name        => "open-ils.circ.money.billing.retrieve.all",
217         notes           =><<"   NOTE");
218         Returns a list of billing items for the given transaction.
219         PARAMS( login, transaction_id )
220         NOTE
221
222 sub billing_items {
223         my( $self, $client, $login, $transid ) = @_;
224
225         my( $staff, $evt ) = $apputils->checksesperm($login, 'VIEW_TRANSACTION');
226         return $evt if $evt;
227
228 # we need to grab the transaction by id and check the billing location
229 # to determin the permissibility XXX
230
231 #       $evt = $apputils->check_perms($staff->id, 
232 #               $transaction->billing_location, 'VIEW_TRANSACTION' );
233 #       return $evt if $evt;
234
235         return $apputils->simplereq( 'open-ils.storage',
236                 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
237
238 }
239
240
241 __PACKAGE__->register_method(
242         method  => "billing_items_create",
243         api_name        => "open-ils.circ.money.billing.create",
244         notes           =><<"   NOTE");
245         Creates a new billing line item
246         PARAMS( login, bill_object (mb) )
247         NOTE
248
249 sub billing_items_create {
250         my( $self, $client, $login, $billing ) = @_;
251
252         my( $staff, $evt ) = $apputils->checksesperm($login, 'CREATE_BILL');
253         return $evt if $evt;
254
255         my $session = $apputils->start_db_session;
256
257         my $id = $session->request(
258                 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
259
260         throw OpenSRF::EX ("Error creating new bill") unless defined $id;
261
262         $apputils->commit_db_session($session);
263
264         return $id;
265 }
266
267
268
269
270
271 1;
272
273
274