1 # ---------------------------------------------------------------
2 # Copyright (C) 2011 Merrimack Valley Library Consortium
3 # Jason Stephenson <jstephenson@mvlc.org>
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 # ---------------------------------------------------------------
16 # An object to handle fee payment
19 package OpenILS::SIP::Transaction::FeePayment;
24 use POSIX qw(strftime);
27 use OpenILS::SIP::Transaction;
28 use OpenILS::SIP::Msg qw/:const/;
29 use Sys::Syslog qw(syslog);
31 use OpenILS::Application::AppUtils;
32 my $U = 'OpenILS::Application::AppUtils';
35 our @ISA = qw(OpenILS::SIP::Transaction);
37 # Most fields are handled by the Transaction superclass
39 sip_payment_type => undef,
41 patron_password => undef,
47 my $self = $class->SUPER::new(@_);
49 foreach my $element (keys %fields) {
50 $self->{_permitted}->{$element} = $fields{$element};
53 @{$self}{keys %fields} = values %fields;
54 return bless $self, $class;
60 # Just in case something completely unexpected happens, we'll
61 # reject the payment to be 'safe.'
64 # If the SC sends over a fee id, we try to pay that
65 # fee/transaction on the patron's record.
68 $bill = $U->simplereq('open-ils.actor', 'open-ils.actor.user.transaction.retrieve', $self->{authtoken}, $self->fee_id);
69 syslog('LOG_DEBUG', 'OILS: open-ils.actor.user.transaction.retrieve returned ' . OpenSRF::Utils::JSON->perl2JSON($bill));
70 # If we got an event or the bill belongs to another patron, set bill to undef.
71 $bill = undef if ($U->event_code($bill) || $bill->usr != $self->patron->internal_id);
73 # Attempt the payment here.
74 if ($bill && $bill->balance_owed >= $self->fee_amount) {
75 # We only attempt payment if the balance_owed on the bill
76 # is greater than or equal to the amount paid by the
78 my $payref = [ [$bill->id, $self->fee_amount] ];
79 my $resp = $self->pay_bills($payref);
80 syslog('LOG_INFO', 'OILS: pay_bills returned ' . OpenSRF::Utils::JSON->perl2JSON($resp));
81 if ($U->event_code($resp)) {
83 $self->screen_msg($resp->{descr});
90 # The payment had to be greater than the bill balance
91 # to end up here. We don't allow credits or
93 $self->sreen_msg(OILS_SIP_MSG_OVERPAYMENT);
96 # In this case, the bill was not found or did not
97 # belong to the patron.
98 $self->screen_msg(OILS_SIP_MSG_NO_BILL);
102 # We attempt to pay as many of the patron's bills as possible with the payment provided.
104 my $results = $U->simplereq('open-ils.actor', 'open-ils.actor.user.transactions.history.have_balance', $self->{authtoken}, $self->patron->internal_id);
105 if ($results && ref($results) eq 'ARRAY') {
106 syslog('LOG_INFO', 'OILS: ' . scalar @$results . " bills found for " . $self->patron->internal_id);
108 # We fill an array with the payment information as
109 # open-ils.circ.money.payment expects it, i.e. an arrayref
110 # with the bill_id and payment amount of its members. To
111 # actually pay the bils, we pass the reference to this
112 # array to our pay_bils method.
115 # Pay each bill from the fee_amount provided until we
116 # either run out of bills or the input payment balance
118 my $amount_paid = $self->fee_amount; # If this hits zero, we're done.
119 foreach my $bill (@{$results}) {
121 syslog('LOG_INFO', 'OILS: bill '. $bill->id . ' amount ' . $bill->balance_owed);
122 if ($bill->balance_owed >= $amount_paid) {
123 # We owe as much as or more than we have money
124 # left, so pay what we have left.
125 $payment = $amount_paid;
128 # This bill is for less than the amount we have
129 # left, so pay the full bill amount.
130 $payment = $bill->balance_owed;
131 $amount_paid -= $bill->balance_owed;
133 # Add the payment to our array.
134 push(@payments, [$bill->id, $payment]);
135 # Leave if we ran out of money.
136 last if ($amount_paid == 0);
138 if (@payments && $amount_paid == 0) {
139 # pay the bills with a reference to our payments
141 my $resp = $self->pay_bills(\@payments);
142 syslog('LOG_INFO', 'OILS: pay_bills returned ' . OpenSRF::Utils::JSON->perl2JSON($resp));
143 if ($U->event_code($resp)) {
145 $self->screen_msg($resp->{descr});
151 if (scalar(@payments) == 0) {
152 # We didn't find any bills for the patron.
153 $self->screen_msg(OILS_SIP_MSG_NO_BILL);
155 # We have an overpayment
156 $self->screen_msg(OILS_SIP_MSG_OVERPAYMENT);
161 if ($results && $U->event_code($results)) {
162 $self->screen_msg($results->{descr});
164 $self->screen_msg(OILS_SIP_MSG_NO_BILL);
171 # Takes array ref of array ref of [billid, payment_amount] to pay in
174 my ($self, $paymentref) = @_;
175 my $user = $self->patron->{user};
176 return $U->simplereq('open-ils.circ', 'open-ils.circ.money.payment', $self->{authtoken},
177 { payment_type => "cash_payment", userid => $user->id, note => "via SIP2",
178 payments => $paymentref}, $user->last_xact_id);