]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Money.pm
added some money methods
[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
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
141 __PACKAGE__->register_method(
142         method  => "create_bill",
143         api_name        => "open-ils.circ.money.grocery.create",
144         notes           => <<"  NOTE");
145         Creates a new grocery transaction using the transaction object provided
146         PARAMS: (login_session, money.grocery (mg) object)
147         NOTE
148
149 sub create_grocery_bill {
150         my( $self, $client, $login, $transaction ) = @_;
151
152         my $staff = $apputils->check_user_session($login);
153         if($apputils->check_user_perms($staff->id, 
154                         $transaction->billing_location, "CREATE_TRANSACTION")) {
155                 return OpenILS::Perm->new("CREATE_TRANSACTION");
156         }
157
158         my $session = $apputils->start_db_session;
159         my $transid = $session->request(
160                 'open-ils.storage.direct.money.grocery.create', $transaction)->gather(1);
161
162         if(!$transid) {
163                 throw OpenSRF::EX ("Error creating new money.grocery");
164         }
165
166         warn "created new grocery transaction $transid\n";
167         
168         $apputils->commit_db_session($session);
169
170         return $transid;
171 }
172
173 __PACKAGE__->register_method(
174         method  => "billing_items",
175         api_name        => "open-ils.circ.money.billing.retrieve.all",
176         notes           =><<"   NOTE");
177         Returns a list of billing items for the given transaction.
178         PARAMS( login, transaction_id )
179         NOTE
180
181 sub billing_items {
182         my( $self, $client, $login, $transid ) = @_;
183
184         my $staff = $apputils->check_user_session($login);
185         if($apputils->check_user_perms($staff->id, 
186                         $staff->home_ou, "VIEW_TRANSACTION")) {
187                 return OpenILS::Perm->new("VIEW_TRANSACTION");
188         }
189
190         return $apputils->simple_scalar_request(
191                 'open-ils.storage',
192                 'open-ils.storage.direct.money.billing.search.xact.atomic', $transid )
193 }
194
195
196 __PACKAGE__->register_method(
197         method  => "billing_items_create",
198         api_name        => "open-ils.circ.money.billing.create",
199         notes           =><<"   NOTE");
200         Creates a new billing line item
201         PARAMS( login, bill_object (mb) )
202         NOTE
203
204 sub billing_items_create {
205         my( $self, $client, $login, $billing ) = @_;
206
207         my $staff = $apputils->check_user_session($login);
208         if($apputils->check_user_perms($staff->id, 
209                         $staff->home_ou, "CREATE_BILL")) {
210                 return OpenILS::Perm->new("CREATE_BILL");
211         }
212
213         my $session = $apputils->start_db_session;
214
215         my $id = $session->request(
216                 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
217
218         if(!$id) {
219                 throw OpenSRF::EX ("Error creating new bill");
220         }
221
222         return $id;
223 }
224
225
226
227
228
229 1;
230
231
232