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