View | Details | Raw Unified | Return to bug 15896
Collapse All | Expand All

(-)a/C4/Accounts.pm (-88 / +2 lines)
Lines 86-181 was made. Link Here
86
# FIXME - I'm not at all sure about the above, because I don't
86
# FIXME - I'm not at all sure about the above, because I don't
87
# understand what the acct* tables in the Koha database are for.
87
# understand what the acct* tables in the Koha database are for.
88
sub makepayment {
88
sub makepayment {
89
90
    #here we update both the accountoffsets and the account lines
91
    #updated to check, if they are paying off a lost item, we return the item
92
    # from their card, and put a note on the item record
93
    my ( $accountlines_id, $borrowernumber, $accountno, $amount, $user, $branch, $payment_note ) = @_;
89
    my ( $accountlines_id, $borrowernumber, $accountno, $amount, $user, $branch, $payment_note ) = @_;
94
    my $dbh = C4::Context->dbh;
95
    my $manager_id = 0;
96
    $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; 
97
98
    # begin transaction
99
    my $nextaccntno = getnextacctno($borrowernumber);
100
    my $newamtos    = 0;
101
    my $sth         = $dbh->prepare("SELECT * FROM accountlines WHERE accountlines_id=?");
102
    $sth->execute( $accountlines_id );
103
    my $data = $sth->fetchrow_hashref;
104
105
    my $payment;
106
    if ( $data->{'accounttype'} eq "Pay" ){
107
        my $udp = 		
108
            $dbh->prepare(
109
                "UPDATE accountlines
110
                    SET amountoutstanding = 0
111
                    WHERE accountlines_id = ?
112
                "
113
            );
114
        $udp->execute($accountlines_id);
115
    }else{
116
        my $udp = 		
117
            $dbh->prepare(
118
                "UPDATE accountlines
119
                    SET amountoutstanding = 0
120
                    WHERE accountlines_id = ?
121
                "
122
            );
123
        $udp->execute($accountlines_id);
124
125
         # create new line
126
        my $payment = 0 - $amount;
127
        $payment_note //= "";
128
        
129
        my $ins = 
130
            $dbh->prepare( 
131
                "INSERT 
132
                    INTO accountlines (borrowernumber, accountno, date, amount, itemnumber, description, accounttype, amountoutstanding, manager_id, note)
133
                    VALUES ( ?, ?, now(), ?, ?, '', 'Pay', 0, ?, ?)"
134
            );
135
        $ins->execute($borrowernumber, $nextaccntno, $payment, $data->{'itemnumber'}, $manager_id, $payment_note);
136
    }
137
138
    if ( C4::Context->preference("FinesLog") ) {
139
        logaction("FINES", 'MODIFY', $borrowernumber, Dumper({
140
            action                => 'fee_payment',
141
            borrowernumber        => $borrowernumber,
142
            old_amountoutstanding => $data->{'amountoutstanding'},
143
            new_amountoutstanding => 0,
144
            amount_paid           => $data->{'amountoutstanding'},
145
            accountlines_id       => $data->{'accountlines_id'},
146
            accountno             => $data->{'accountno'},
147
            manager_id            => $manager_id,
148
        }));
149
150
151
        logaction("FINES", 'CREATE',$borrowernumber,Dumper({
152
            action            => 'create_payment',
153
            borrowernumber    => $borrowernumber,
154
            accountno         => $nextaccntno,
155
            amount            => $payment,
156
            amountoutstanding => 0,,
157
            accounttype       => 'Pay',
158
            accountlines_paid => [$data->{'accountlines_id'}],
159
            manager_id        => $manager_id,
160
        }));
161
    }
162
163
    UpdateStats({
164
        branch => $branch,
165
        type   => 'payment',
166
        amount => $amount,
167
        borrowernumber => $borrowernumber,
168
        accountno => $accountno
169
    });
170
90
171
    #check to see what accounttype
91
    return Koha::Account->new( { patron_id => $borrowernumber } )
172
    if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) {
92
      ->pay( { accountlines_id => $accountlines_id, amount => $amount, library_id => $branch, note => $payment_note } );
173
        C4::Circulation::ReturnLostItem( $borrowernumber, $data->{'itemnumber'} );
174
    }
175
    my $sthr = $dbh->prepare("SELECT max(accountlines_id) AS lastinsertid FROM accountlines");
176
    $sthr->execute();
177
    my $datalastinsertid = $sthr->fetchrow_hashref;
178
    return $datalastinsertid->{'lastinsertid'};
179
}
93
}
180
94
181
=head2 getnextacctno
95
=head2 getnextacctno
(-)a/Koha/Account.pm (-10 / +70 lines)
Lines 47-60 sub new { Link Here
47
47
48
This method allows payments to be made against feees
48
This method allows payments to be made against feees
49
49
50
Koha::Account->new( { patron_id => $borrowernumber } )->pay(
51
    {
52
        amount     => $amount,
53
        sip        => $sipmode,
54
        note       => $note,
55
        id         => $accountlines_id,
56
        library_id => $branchcode,
57
    }
58
);
59
50
=cut
60
=cut
51
61
52
sub pay {
62
sub pay {
53
    my ( $self, $params ) = @_;
63
    my ( $self, $params ) = @_;
54
64
55
    my $amount = $params->{amount};
65
    my $amount          = $params->{amount};
56
    my $sip    = $params->{sip};
66
    my $sip             = $params->{sip};
57
    my $note   = $params->{note} || q{};
67
    my $note            = $params->{note} || q{};
68
    my $accountlines_id = $params->{accountlines_id};
69
    my $library_id      = $params->{library_id};
58
70
59
    my $userenv = C4::Context->userenv;
71
    my $userenv = C4::Context->userenv;
60
72
Lines 71-85 sub pay { Link Here
71
83
72
    my $manager_id = $userenv ? $userenv->{number} : 0;
84
    my $manager_id = $userenv ? $userenv->{number} : 0;
73
85
74
    my @outstanding_fines = Koha::Account::Lines->search(
86
    my @fines_paid; # List of account lines paid on with this payment
87
88
    my $balance_remaining = $amount; # Set it now so we can adjust the amount if necessary
89
    $balance_remaining ||= 0;
90
91
    # We were passed a specific line to pay
92
    if ( $accountlines_id ) {
93
        my $fine = Koha::Account::Lines->find( $accountlines_id );
94
95
        # If accountline id is passed but no amount, we pay that line in full
96
        $amount = $fine->amountoutstanding unless defined($amount);
97
98
        my $old_amountoutstanding = $fine->amountoutstanding;
99
        my $new_amountoutstanding = $old_amountoutstanding - $amount;
100
        $fine->amountoutstanding( $new_amountoutstanding )->store();
101
        $balance_remaining = $balance_remaining - $amount;
102
103
        if ( $fine->accounttype eq 'Rep' || $fine->accounttype eq 'L' )
104
        {
105
            C4::Circulation::ReturnLostItem( $self->{patron_id}, $fine->itemnumber );
106
        }
107
108
        if ( C4::Context->preference("FinesLog") ) {
109
            logaction(
110
                "FINES", 'MODIFY',
111
                $self->{patron_id},
112
                Dumper(
113
                    {
114
                        action                => 'fee_payment',
115
                        borrowernumber        => $fine->borrowernumber,
116
                        old_amountoutstanding => $old_amountoutstanding,
117
                        new_amountoutstanding => 0,
118
                        amount_paid           => $old_amountoutstanding,
119
                        accountlines_id       => $fine->id,
120
                        accountno             => $fine->accountno,
121
                        manager_id            => $manager_id,
122
                        note                  => $note,
123
                    }
124
                )
125
            );
126
            push( @fines_paid, $fine->id );
127
        }
128
    }
129
130
    # Were not passed a specific line to pay, or the payment was for more
131
    # than the what was owed on the given line. In that case pay down other
132
    # lines with remaining balance.
133
    my @outstanding_fines;
134
    @outstanding_fines = Koha::Account::Lines->search(
75
        {
135
        {
76
            borrowernumber    => $self->{patron_id},
136
            borrowernumber    => $self->{patron_id},
77
            amountoutstanding => { '>' => 0 },
137
            amountoutstanding => { '>' => 0 },
78
        }
138
        }
79
    );
139
    ) if $balance_remaining > 0;
80
140
81
    my $balance_remaining = $amount;
82
    my @fines_paid;
83
    foreach my $fine (@outstanding_fines) {
141
    foreach my $fine (@outstanding_fines) {
84
        my $amount_to_pay =
142
        my $amount_to_pay =
85
            $fine->amountoutstanding > $balance_remaining
143
            $fine->amountoutstanding > $balance_remaining
Lines 131-140 sub pay { Link Here
131
        }
189
        }
132
    )->store();
190
    )->store();
133
191
134
    my $branch = $userenv ? $userenv->{'branch'} : undef;
192
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
193
135
    UpdateStats(
194
    UpdateStats(
136
        {
195
        {
137
            branch         => $branch,
196
            branch         => $library_id,
138
            type           => 'payment',
197
            type           => 'payment',
139
            amount         => $amount,
198
            amount         => $amount,
140
            borrowernumber => $self->{patron_id},
199
            borrowernumber => $self->{patron_id},
Lines 160-165 sub pay { Link Here
160
            )
219
            )
161
        );
220
        );
162
    }
221
    }
222
223
    return $payment->id;
163
}
224
}
164
225
165
1;
226
1;
166
- 

Return to bug 15896