1 # ---------------------------------------------------------------
2 # Copyright (C) 2005 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
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.
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 # ---------------------------------------------------------------
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";
23 use OpenSRF::EX qw(:try);
27 __PACKAGE__->register_method(
28 method => "make_payments",
29 api_name => "open-ils.circ.money.payment",
31 Pass in a structure like so:
33 cash_drawer: <string>,
34 payment_type : <string>,
41 patron_credit : <credit amt>
43 login must have CREATE_PAYMENT priveleges.
44 If any payments fail, all are reverted back.
49 my( $self, $client, $login, $payments ) = @_;
50 my $user = $apputils->check_user_session($login);
52 if($apputils->check_user_perms($user->id, $user->home_ou, "CREATE_PAYMENT")) {
53 return OpenILS::Perm->new("CREATE_PAYMENT");
57 warn Dumper $payments;
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};
66 for my $pay (@{$payments->{payments}}) {
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);
74 return OpenILS::EX->new("NO_TRANSACTION_FOUND")->ex unless $trans;
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";
81 my $payobj = "Fieldmapper::money::$type";
82 $payobj = $payobj->new;
84 $payobj->amount($amount);
85 $payobj->amount_collected($amount);
86 $payobj->accepting_usr($user->id);
87 $payobj->xact($transid);
89 $payobj->cash_drawer($drawer);
91 # update the transaction if it's done
92 if( ($trans->balance_owed - $amount) <= 0 ) {
94 warn "Transaction is complete, updating...\n";
95 $trans = $session->request(
96 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
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"); }
106 warn "Creating new $type object for \$$amount\n";
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"); }
114 _update_patron_credit( $session, $userid, $credit );
116 $apputils->commit_db_session($session);
121 sub _update_patron_credit {
122 my( $session, $userid, $credit ) = @_;
123 return if $credit < 0;
125 my $patron = $session->request(
126 'open-ils.storage.direct.actor.user.retrieve', $userid )->gather(1);
128 $patron->credit_forward_balance(
129 $patron->credit_forward_balance + $credit);
131 my $res = $session->request(
132 'open-ils.storage.direct.actor.user.update', $patron )->gather(1);
135 throw OpenSRF::EX("Error updating patron credit");