Fix stylistic nits pointed out by Mike Rylander.
[evergreen-equinox.git] / Open-ILS / src / perlmods / lib / OpenILS / SIP / Transaction / FeePayment.pm
1 #
2 # An object to handle fee payment
3 #
4
5 package OpenILS::SIP::Transaction::FeePayment;
6
7 use warnings;
8 use strict;
9
10 use POSIX qw(strftime);
11
12 use OpenILS::SIP;
13 use OpenILS::SIP::Transaction;
14 use OpenILS::SIP::Msg qw/:const/;
15 use Sys::Syslog qw(syslog);
16
17 use OpenILS::Application::AppUtils;
18 my $U = 'OpenILS::Application::AppUtils';
19
20
21 our @ISA = qw(OpenILS::SIP::Transaction);
22
23 # Most fields are handled by the Transaction superclass
24 my %fields = (
25     sip_payment_type => undef,
26     fee_id => 0,
27     patron_password => undef,
28     );
29
30 sub new {
31     my $class = shift;
32
33     my $self = $class->SUPER::new(@_);
34
35     foreach my $element (keys %fields) {
36         $self->{_permitted}->{$element} = $fields{$element};
37     }
38
39     @{$self}{keys %fields} = values %fields;
40     return bless $self, $class;
41 }
42
43 sub do_fee_payment {
44     my $self = shift;
45     my $results = $U->simplereq('open-ils.actor', 'open-ils.actor.user.transactions.history.have_balance', $self->{authtoken}, $self->patron->internal_id);
46     if (ref $results eq 'ARRAY') {
47         syslog('LOG_INFO', scalar @$results . " bills found for " . $self->patron->internal_id);
48         my $payment = $self->fee_amount;
49         foreach my $bill (@{$results}) {
50             syslog('LOG_INFO', 'bill '. $bill->id . ' amount ' . $bill->balance_owed);
51             if (!$self->transaction_id) {
52                 if ($bill->balance_owed >= $payment) {
53                     $self->pay_bill($bill->id, $payment);
54                     $payment = 0;
55                 }
56                 else {
57                     $self->pay_bill($bill->id, $bill->balance_owed);
58                     $payment -= $bill->balance_owed;
59                 }
60                 last unless ($payment);
61             }
62             elsif ($self->transaction_id == $bill->id) {
63                 $self->pay_bill($bill->id, $payment);
64                 $payment = 0;
65                 last;
66             }
67         }
68         if ($payment == $self->fee_amount) {
69             $self->ok(0);
70             if (!$self->transaction_id) {
71                 $self->screen_msg("Payment failed: you have no bills.");
72             }
73             else {
74                 $self->screen_msg("Payment failed: transaction " + $self->transaction_id + " not found.");
75             }
76         }
77         else {
78             $self->ok(1);
79         }
80     }
81     else {
82         $self->ok(0);
83         $self->screen_msg($results->{descr});
84     }
85 }
86
87 sub pay_bill {
88     my ($self, $bill, $amount) = @_;
89     my $user = $self->patron->{user};
90     my $r = $U->simplereq('open-ils.circ', 'open-ils.circ.money.payment', $self->{authtoken},
91                           { payment_type => "cash_payment", userid => $user->id, note => "via SIP2",
92                             payments => [[$bill, $amount ]]}, $user->last_xact_id);
93 }
94
95
96 1;