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