]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Money.pm
adding money
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / Money.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2005  Georgia Public Library Service 
3 # Bill Erickson <highfalutin@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 OpenILS::EX;
24 use OpenSRF::EX qw(:try);
25 use OpenILS::Perm;
26
27
28 __PACKAGE__->register_method(
29         method  => "make_payments",
30         api_name        => "open-ils.circ.money.payment",
31         notes           => <<"  NOTE");
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.
37         NOTE
38
39 sub make_payments {
40
41         my( $self, $client, $login, $payments ) = @_;
42         my $user = $apputils->check_user_session($login);
43
44         if($apputils->check_user_perms($user->id, $user->home_ou, "CREATE_PAYMENT")) {
45                 return OpenILS::Perm->new("CREATE_PAYMENT");
46         } 
47
48         use Data::Dumper;
49         warn Dumper $payments;
50
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};
55
56         for my $pay (@{$payments->{payments}}) {
57
58                 my $transid = $pay->[0];
59                 my $amount = $pay->[1];
60                 my $note = $pay->[2];
61
62                 my $trans = $session->request(
63                         "open-ils.storage.direct.money.billable_transaction_summary.retrieve", 
64                         $transid )->gather(1);
65
66
67                 return OpenILS::EX->new("NO_TRANSACTION_FOUND")->ex unless $trans; 
68
69                 my $payobj = "Fieldmapper::money::$type";
70                 $payobj = $payobj->new;
71
72                 $payobj->amount($amount);
73                 $payobj->amount_collected($amount);
74                 $payobj->accepting_usr($user->id);
75                 $payobj->xact($transid);
76                 $payobj->note($note);
77                 $payobj->cash_drawer($drawer);
78                 
79                 # update the transaction if it's done 
80                 if( ($trans->balance_owed - $amount) <= 0 ) {
81
82                         warn "Transaction is complete, updating...\n";
83                         $trans = $session->request(
84                                 "open-ils.storage.direct.money.billable_transaction.retrieve", $transid )->gather(1);
85
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"); }
91                                         
92                 }
93
94                 warn "Creating new $type object for \$$amount\n";
95
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"); }
99
100         }
101
102         $apputils->commit_db_session($session);
103         return 1;
104                 
105 }
106
107
108
109
110 1;
111
112
113