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