]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Money.pm
d4c6b2042759b1def0b8646ec580c1995f338f1f
[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.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
142 1;
143
144
145