Bug 22873: Add comment to explain what disallow_overpayment is for
[koha.git] / C4 / SIP / ILS / Transaction / FeePayment.pm
1 package C4::SIP::ILS::Transaction::FeePayment;
2
3 use warnings;
4 use strict;
5
6 # Copyright 2011 PTFS-Europe Ltd.
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Try::Tiny;
24
25 use Koha::Account;
26 use Koha::Account::Lines;
27
28 use parent qw(C4::SIP::ILS::Transaction);
29
30 my %fields = ();
31
32 sub new {
33     my $class = shift;
34     my $self  = $class->SUPER::new();
35
36     foreach ( keys %fields ) {
37         $self->{_permitted}->{$_} = $fields{$_};    # overlaying _permitted
38     }
39
40     @{$self}{ keys %fields } = values %fields;    # copying defaults into object
41     return bless $self, $class;
42 }
43
44 sub pay {
45     my $self                 = shift;
46     my $borrowernumber       = shift;
47     my $amt                  = shift;
48     my $sip_type             = shift;
49     my $fee_id               = shift;
50     my $is_writeoff          = shift;
51     my $disallow_overpayment = shift;
52     my $register_id          = shift;
53
54     my $type = $is_writeoff ? 'WRITEOFF' : 'PAYMENT';
55
56     my $account = Koha::Account->new( { patron_id => $borrowernumber } );
57
58     # Bug 16899: Add ability to disallow overpayments for SIP
59     if ($disallow_overpayment) {
60         return { ok => 0 } if $account->balance < $amt;
61     }
62
63     my $pay_options = {
64         amount        => $amt,
65         type          => $type,
66         payment_type  => 'SIP' . $sip_type,
67         interface     => C4::Context->interface,
68         cash_register => $register_id,
69     };
70
71     if ($fee_id) {
72         my $fee = Koha::Account::Lines->find($fee_id);
73         if ( $fee ) {
74             $pay_options->{lines} = [$fee];
75         }
76         else {
77             return {
78                 ok => 0
79             };
80         }
81     }
82
83     my $ok = 1;
84     my $pay_response;
85     my $error;
86     try {
87         $pay_response = $account->pay($pay_options);
88     }
89     catch {
90         $ok = 0;
91
92         if ( ref($_) =~ /^Koha::Exceptions/ ) {
93             $error = $_->description;
94         }
95         else {
96             $_->rethrow;
97         }
98     };
99
100     return {
101         ok           => $ok,
102         pay_response => $pay_response,
103         error        => $error,
104     };
105 }
106
107 #sub DESTROY {
108 #}
109
110 1;
111 __END__
112