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

(-)a/C4/Accounts.pm (+2 lines)
Lines 277-282 sub manualinvoice { Link Here
277
        }));
277
        }));
278
    }
278
    }
279
279
280
    Koha::Account->new({ patron_id => $borrowernumber })->normalize_balance();
281
280
    return 0;
282
    return 0;
281
}
283
}
282
284
(-)a/C4/Circulation.pm (+2 lines)
Lines 2374-2379 sub _FixOverduesOnReturn { Link Here
2374
        $accountline->accounttype('F');
2374
        $accountline->accounttype('F');
2375
    }
2375
    }
2376
2376
2377
    Koha::Account->new({ patron_id => $borrowernumber })->normalize_balance();
2378
2377
    return $accountline->store();
2379
    return $accountline->store();
2378
}
2380
}
2379
2381
(-)a/Koha/Account.pm (-41 / +82 lines)
Lines 60-66 Koha::Account->new( { patron_id => $borrowernumber } )->pay( Link Here
60
        library_id  => $branchcode,
60
        library_id  => $branchcode,
61
        lines        => $lines, # Arrayref of Koha::Account::Line objects to pay
61
        lines        => $lines, # Arrayref of Koha::Account::Line objects to pay
62
        account_type => $type,  # accounttype code
62
        account_type => $type,  # accounttype code
63
        offset_type => $offset_type,    # offset type code
63
        offset_type  => $offset_type,     # offset type code
64
        credit_id    => credit_id,        # pay from balance of existing credit
64
    }
65
    }
65
);
66
);
66
67
Lines 79-84 sub pay { Link Here
79
    my $payment_type = $params->{payment_type} || undef;
80
    my $payment_type = $params->{payment_type} || undef;
80
    my $account_type = $params->{account_type};
81
    my $account_type = $params->{account_type};
81
    my $offset_type  = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
82
    my $offset_type  = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
83
    my $credit_id    = $params->{credit_id};
82
84
83
    my $userenv = C4::Context->userenv;
85
    my $userenv = C4::Context->userenv;
84
86
Lines 214-233 sub pay { Link Here
214
216
215
    $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
217
    $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
216
218
217
    my $payment = Koha::Account::Line->new(
219
    my $payment;
218
        {
220
    if ($credit_id) {
219
            borrowernumber    => $self->{patron_id},
221
        $payment = Koha::Account::Lines->find($credit_id);
220
            accountno         => $accountno,
222
        $payment->amountoutstanding( $balance_remaining * -1 );
221
            date              => dt_from_string(),
223
        $payment->store();
222
            amount            => 0 - $amount,
224
    }
223
            description       => $description,
225
    else {
224
            accounttype       => $account_type,
226
        $payment = Koha::Account::Line->new(
225
            payment_type      => $payment_type,
227
            {
226
            amountoutstanding => 0 - $balance_remaining,
228
                borrowernumber    => $self->{patron_id},
227
            manager_id        => $manager_id,
229
                accountno         => $accountno,
228
            note              => $note,
230
                date              => dt_from_string(),
229
        }
231
                amount            => 0 - $amount,
230
    )->store();
232
                description       => $description,
233
                accounttype       => $account_type,
234
                payment_type      => $payment_type,
235
                amountoutstanding => 0 - $balance_remaining,
236
                manager_id        => $manager_id,
237
                note              => $note,
238
            }
239
        )->store();
240
    }
231
241
232
    foreach my $o ( @account_offsets ) {
242
    foreach my $o ( @account_offsets ) {
233
        $o->credit_id( $payment->id() );
243
        $o->credit_id( $payment->id() );
Lines 236-268 sub pay { Link Here
236
246
237
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
247
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
238
248
239
    UpdateStats(
249
    unless ( $credit_id ) {
240
        {
250
        UpdateStats(
241
            branch         => $library_id,
251
            {
242
            type           => $type,
252
                branch         => $library_id,
243
            amount         => $amount,
253
                type           => $type,
244
            borrowernumber => $self->{patron_id},
254
                amount         => $amount,
245
            accountno      => $accountno,
255
                borrowernumber => $self->{patron_id},
246
        }
256
                accountno      => $accountno,
247
    );
257
            }
248
249
    if ( C4::Context->preference("FinesLog") ) {
250
        logaction(
251
            "FINES", 'CREATE',
252
            $self->{patron_id},
253
            Dumper(
254
                {
255
                    action            => "create_$type",
256
                    borrowernumber    => $self->{patron_id},
257
                    accountno         => $accountno,
258
                    amount            => 0 - $amount,
259
                    amountoutstanding => 0 - $balance_remaining,
260
                    accounttype       => $account_type,
261
                    accountlines_paid => \@fines_paid,
262
                    manager_id        => $manager_id,
263
                }
264
            )
265
        );
258
        );
259
260
        if ( C4::Context->preference("FinesLog") ) {
261
            logaction(
262
                "FINES", 'CREATE',
263
                $self->{patron_id},
264
                Dumper(
265
                    {
266
                        action            => "create_$type",
267
                        borrowernumber    => $self->{patron_id},
268
                        accountno         => $accountno,
269
                        amount            => 0 - $amount,
270
                        amountoutstanding => 0 - $balance_remaining,
271
                        accounttype       => $account_type,
272
                        accountlines_paid => \@fines_paid,
273
                        manager_id        => $manager_id,
274
                    }
275
                )
276
            );
277
        }
266
    }
278
    }
267
279
268
    if ( C4::Context->preference('UseEmailReceipts') ) {
280
    if ( C4::Context->preference('UseEmailReceipts') ) {
Lines 524-529 sub non_issues_charges { Link Here
524
      : 0;
536
      : 0;
525
}
537
}
526
538
539
=head3 normalize_balance
540
541
$account->normalize_balance();
542
543
Find outstanding credits and use them to pay outstanding debits
544
545
=cut
546
547
sub normalize_balance {
548
    my ($self) = @_;
549
    my @credits = Koha::Account::Lines->search(
550
        {
551
            borrowernumber    => $self->{patron_id},
552
            amountoutstanding => { '<' => 0 },
553
        }
554
    );
555
556
    foreach my $credit (@credits) {
557
        $self->pay(
558
            {
559
                credit_id => $credit->id,
560
                amount    => $credit->amountoutstanding * -1,
561
            }
562
        );
563
    }
564
565
    return $self;
566
}
567
527
1;
568
1;
528
569
529
=head2 Name mappings
570
=head2 Name mappings
(-)a/t/db_dependent/Accounts.t (-2 / +37 lines)
Lines 18-24 Link Here
18
18
19
use Modern::Perl;
19
use Modern::Perl;
20
20
21
use Test::More tests => 28;
21
use Test::More tests => 29;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
Lines 1018-1021 subtest "Payment notice tests" => sub { Link Here
1018
    is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );
1018
    is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );
1019
};
1019
};
1020
1020
1021
subtest "Koha::Account::normalize_balance tests" => sub {
1022
1023
    plan tests => 6;
1024
1025
    # Create a borrower
1026
    my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1027
    my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
1028
1029
    my $borrower = Koha::Patron->new( {
1030
        cardnumber => 'kyliehall',
1031
        surname => 'Hall',
1032
        firstname => 'Kylie',
1033
    } );
1034
    $borrower->categorycode( $categorycode );
1035
    $borrower->branchcode( $branchcode );
1036
    $borrower->store;
1037
1038
    my $account = Koha::Account->new({ patron_id => $borrower->id });
1039
1040
    my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => -10, amountoutstanding => -10 })->store();
1041
    my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10 })->store();
1042
1043
    is( $account->balance(), 0, "Account balance is 0" );
1044
    is( $line1->amountoutstanding, "-10", 'Credit has amount outstanding of -10' );
1045
    is( $line2->amountoutstanding, "10", 'Fee has amount outstanding of 10' );
1046
1047
    $account->normalize_balance();
1048
1049
    is( $account->balance(), 0, "Account balance is 0" );
1050
1051
    $line1->_result->discard_changes();
1052
    $line2->_result->discard_changes();
1053
    is( $line1->amountoutstanding, '0.000000', 'First fee has amount outstanding of 0' );
1054
    is( $line2->amountoutstanding, '0.000000', 'Second fee has amount outstanding of 0' );
1055
};
1056
1021
1;
1057
1;
1022
- 

Return to bug 18805