1 # ---------------------------------------------------------------
2 # Copyright (C) 2005 Georgia Public Library Service
3 # Bill Erickson <highfalutin@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";
24 use OpenSRF::EX qw(:try);
28 __PACKAGE__->register_method(
29 method => "make_payments",
30 api_name => "open-ils.circ.money.payment",
32 Pass in a structure like so:
33 { cash_drawer: <string>, payment_type : <string>,
34 payments: [ [trans_id, amt, note], [...] ], patron_credit : <credit amt> }
35 login must have CREATE_PAYMENT priveleges.
36 If any payments fail, all are reverted back.
41 my( $self, $client, $login, $payments ) = @_;
42 my $user = $apputils->check_user_session($login);
44 if($apputils->check_user_perms($user->id, $user->home_ou, "CREATE_PAYMENT")) {
45 return OpenILS::Perm->new("CREATE_PAYMENT");
49 warn Dumper $payments;
51 my $session = $apputils->start_db_session;
52 my $type = $payments->{payment_type};
53 my $credit = $payments->{patron_credit};
54 my $drawer = $payments->{cash_drawer};
56 for my $pay (@{$payments->{payments}}) {
58 my $transid = $pay->[0];
59 my $amount = $pay->[1];
62 my $trans = $session->request(
63 "open-ils.storage.direct.money.billable_transaction_summary.retrieve",
64 $transid )->gather(1);
67 return OpenILS::EX->new("NO_TRANSACTION_FOUND")->ex unless $trans;
69 my $payobj = "Fieldmapper::money::$type";
70 $payobj = $payobj->new;
72 $payobj->amount($amount);
73 $payobj->amount_collected($amount);
74 $payobj->accepting_usr($user->id);
75 $payobj->xact($transid);
77 $payobj->cash_drawer($drawer);
79 # update the transaction if it's done
80 if( ($trans->balance_owed - $amount) <= 0 ) {
82 warn "Transaction is complete, updating...\n";
83 $trans = $session->request(
84 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
86 $trans->xact_finish("now");
87 my $s = $session->request(
88 "open-ils.storage.direct.money.billable_transaction.update", $trans )->gather(1);
89 if(!$s) { throw OpenSRF::EX::ERROR
90 ("Error updating billable_xact in circ.money.payment"); }
94 warn "Creating new $type object for \$$amount\n";
96 my $s = $session->request(
97 "open-ils.storage.direct.money.$type.create", $payobj )->gather(1);
98 if(!$s) { throw OpenSRF::EX::ERROR ("Error creating new $type"); }
102 $apputils->commit_db_session($session);