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

(-)a/C4/Accounts.pm (+2 lines)
Lines 312-317 sub manualinvoice { Link Here
312
        }));
312
        }));
313
    }
313
    }
314
314
315
    Koha::Account->new({ patron_id => $borrowernumber })->normalize_balance();
316
315
    return 0;
317
    return 0;
316
}
318
}
317
319
(-)a/C4/Circulation.pm (+2 lines)
Lines 2349-2354 sub _FixOverduesOnReturn { Link Here
2349
        $accountline->accounttype('F');
2349
        $accountline->accounttype('F');
2350
    }
2350
    }
2351
2351
2352
    Koha::Account->new({ patron_id => $borrowernumber })->normalize_balance();
2353
2352
    return $accountline->store();
2354
    return $accountline->store();
2353
}
2355
}
2354
2356
(-)a/Koha/Account.pm (-41 / +82 lines)
Lines 56-62 Koha::Account->new( { patron_id => $borrowernumber } )->pay( Link Here
56
        library_id  => $branchcode,
56
        library_id  => $branchcode,
57
        lines        => $lines, # Arrayref of Koha::Account::Line objects to pay
57
        lines        => $lines, # Arrayref of Koha::Account::Line objects to pay
58
        account_type => $type,  # accounttype code
58
        account_type => $type,  # accounttype code
59
        offset_type => $offset_type,    # offset type code
59
        offset_type  => $offset_type,     # offset type code
60
        credit_id    => credit_id,        # pay from balance of existing credit
60
    }
61
    }
61
);
62
);
62
63
Lines 75-80 sub pay { Link Here
75
    my $payment_type = $params->{payment_type} || undef;
76
    my $payment_type = $params->{payment_type} || undef;
76
    my $account_type = $params->{account_type};
77
    my $account_type = $params->{account_type};
77
    my $offset_type  = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
78
    my $offset_type  = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
79
    my $credit_id    = $params->{credit_id};
78
80
79
    my $userenv = C4::Context->userenv;
81
    my $userenv = C4::Context->userenv;
80
82
Lines 208-227 sub pay { Link Here
208
210
209
    $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
211
    $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
210
212
211
    my $payment = Koha::Account::Line->new(
213
    my $payment;
212
        {
214
    if ($credit_id) {
213
            borrowernumber    => $self->{patron_id},
215
        $payment = Koha::Account::Lines->find($credit_id);
214
            accountno         => $accountno,
216
        $payment->amountoutstanding( $balance_remaining * -1 );
215
            date              => dt_from_string(),
217
        $payment->store();
216
            amount            => 0 - $amount,
218
    }
217
            description       => $description,
219
    else {
218
            accounttype       => $account_type,
220
        $payment = Koha::Account::Line->new(
219
            payment_type      => $payment_type,
221
            {
220
            amountoutstanding => 0 - $balance_remaining,
222
                borrowernumber    => $self->{patron_id},
221
            manager_id        => $manager_id,
223
                accountno         => $accountno,
222
            note              => $note,
224
                date              => dt_from_string(),
223
        }
225
                amount            => 0 - $amount,
224
    )->store();
226
                description       => $description,
227
                accounttype       => $account_type,
228
                payment_type      => $payment_type,
229
                amountoutstanding => 0 - $balance_remaining,
230
                manager_id        => $manager_id,
231
                note              => $note,
232
            }
233
        )->store();
234
    }
225
235
226
    foreach my $o ( @account_offsets ) {
236
    foreach my $o ( @account_offsets ) {
227
        $o->credit_id( $payment->id() );
237
        $o->credit_id( $payment->id() );
Lines 230-262 sub pay { Link Here
230
240
231
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
241
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
232
242
233
    UpdateStats(
243
    unless ( $credit_id ) {
234
        {
244
        UpdateStats(
235
            branch         => $library_id,
245
            {
236
            type           => $type,
246
                branch         => $library_id,
237
            amount         => $amount,
247
                type           => $type,
238
            borrowernumber => $self->{patron_id},
248
                amount         => $amount,
239
            accountno      => $accountno,
249
                borrowernumber => $self->{patron_id},
240
        }
250
                accountno      => $accountno,
241
    );
251
            }
242
243
    if ( C4::Context->preference("FinesLog") ) {
244
        logaction(
245
            "FINES", 'CREATE',
246
            $self->{patron_id},
247
            Dumper(
248
                {
249
                    action            => "create_$type",
250
                    borrowernumber    => $self->{patron_id},
251
                    accountno         => $accountno,
252
                    amount            => 0 - $amount,
253
                    amountoutstanding => 0 - $balance_remaining,
254
                    accounttype       => $account_type,
255
                    accountlines_paid => \@fines_paid,
256
                    manager_id        => $manager_id,
257
                }
258
            )
259
        );
252
        );
253
254
        if ( C4::Context->preference("FinesLog") ) {
255
            logaction(
256
                "FINES", 'CREATE',
257
                $self->{patron_id},
258
                Dumper(
259
                    {
260
                        action            => "create_$type",
261
                        borrowernumber    => $self->{patron_id},
262
                        accountno         => $accountno,
263
                        amount            => 0 - $amount,
264
                        amountoutstanding => 0 - $balance_remaining,
265
                        accounttype       => $account_type,
266
                        accountlines_paid => \@fines_paid,
267
                        manager_id        => $manager_id,
268
                    }
269
                )
270
            );
271
        }
260
    }
272
    }
261
273
262
    return $payment->id;
274
    return $payment->id;
Lines 286-291 sub balance { Link Here
286
      : 0;
298
      : 0;
287
}
299
}
288
300
301
=head3 normalize_balance
302
303
$account->normalize_balance();
304
305
Find outstanding credits and use them to pay outstanding debits
306
307
=cut
308
309
sub normalize_balance {
310
    my ($self) = @_;
311
    my @credits = Koha::Account::Lines->search(
312
        {
313
            borrowernumber    => $self->{patron_id},
314
            amountoutstanding => { '<' => 0 },
315
        }
316
    );
317
318
    foreach my $credit (@credits) {
319
        $self->pay(
320
            {
321
                credit_id => $credit->id,
322
                amount    => $credit->amountoutstanding * -1,
323
            }
324
        );
325
    }
326
327
    return $self;
328
}
329
289
1;
330
1;
290
331
291
=head1 AUTHOR
332
=head1 AUTHOR
(-)a/t/db_dependent/Accounts.t (-1 / +36 lines)
Lines 693-696 subtest "Koha::Account::Line::void tests" => sub { Link Here
693
    is( $line2->amountoutstanding, '20.000000', 'Second fee again has amount outstanding of 20' );
693
    is( $line2->amountoutstanding, '20.000000', 'Second fee again has amount outstanding of 20' );
694
};
694
};
695
695
696
subtest "Koha::Account::normalize_balance tests" => sub {
697
698
    plan tests => 6;
699
700
    # Create a borrower
701
    my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
702
    my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
703
704
    my $borrower = Koha::Patron->new( {
705
        cardnumber => 'kyliehall',
706
        surname => 'Hall',
707
        firstname => 'Kylie',
708
    } );
709
    $borrower->categorycode( $categorycode );
710
    $borrower->branchcode( $branchcode );
711
    $borrower->store;
712
713
    my $account = Koha::Account->new({ patron_id => $borrower->id });
714
715
    my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => -10, amountoutstanding => -10 })->store();
716
    my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10 })->store();
717
718
    is( $account->balance(), "0.000000", "Account balance is 0" );
719
    is( $line1->amountoutstanding, "-10", 'Credit has amount outstanding of -10' );
720
    is( $line2->amountoutstanding, "10", 'Fee has amount outstanding of 10' );
721
722
    $account->normalize_balance();
723
724
    is( $account->balance(), "0.000000", "Account balance is 0" );
725
726
    $line1->_result->discard_changes();
727
    $line2->_result->discard_changes();
728
    is( $line1->amountoutstanding, '0.000000', 'First fee has amount outstanding of 0' );
729
    is( $line2->amountoutstanding, '0.000000', 'Second fee has amount outstanding of 0' );
730
};
731
696
1;
732
1;
697
- 

Return to bug 18805