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

(-)a/C4/Accounts.pm (+2 lines)
Lines 278-283 sub manualinvoice { Link Here
278
        }));
278
        }));
279
    }
279
    }
280
280
281
    Koha::Account->new({ patron_id => $borrowernumber })->normalize_balance();
282
281
    return 0;
283
    return 0;
282
}
284
}
283
285
(-)a/C4/Circulation.pm (+2 lines)
Lines 2390-2395 sub _FixOverduesOnReturn { Link Here
2390
        $accountline->accounttype('F');
2390
        $accountline->accounttype('F');
2391
    }
2391
    }
2392
2392
2393
    Koha::Account->new({ patron_id => $borrowernumber })->normalize_balance();
2394
2393
    return $accountline->store();
2395
    return $accountline->store();
2394
}
2396
}
2395
2397
(-)a/Koha/Account.pm (-41 / +82 lines)
Lines 57-63 Koha::Account->new( { patron_id => $borrowernumber } )->pay( Link Here
57
        library_id  => $branchcode,
57
        library_id  => $branchcode,
58
        lines        => $lines, # Arrayref of Koha::Account::Line objects to pay
58
        lines        => $lines, # Arrayref of Koha::Account::Line objects to pay
59
        account_type => $type,  # accounttype code
59
        account_type => $type,  # accounttype code
60
        offset_type => $offset_type,    # offset type code
60
        offset_type  => $offset_type,     # offset type code
61
        credit_id    => credit_id,        # pay from balance of existing credit
61
    }
62
    }
62
);
63
);
63
64
Lines 76-81 sub pay { Link Here
76
    my $payment_type = $params->{payment_type} || undef;
77
    my $payment_type = $params->{payment_type} || undef;
77
    my $account_type = $params->{account_type};
78
    my $account_type = $params->{account_type};
78
    my $offset_type  = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
79
    my $offset_type  = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
80
    my $credit_id    = $params->{credit_id};
79
81
80
    my $userenv = C4::Context->userenv;
82
    my $userenv = C4::Context->userenv;
81
83
Lines 209-228 sub pay { Link Here
209
211
210
    $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
212
    $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
211
213
212
    my $payment = Koha::Account::Line->new(
214
    my $payment;
213
        {
215
    if ($credit_id) {
214
            borrowernumber    => $self->{patron_id},
216
        $payment = Koha::Account::Lines->find($credit_id);
215
            accountno         => $accountno,
217
        $payment->amountoutstanding( $balance_remaining * -1 );
216
            date              => dt_from_string(),
218
        $payment->store();
217
            amount            => 0 - $amount,
219
    }
218
            description       => $description,
220
    else {
219
            accounttype       => $account_type,
221
        $payment = Koha::Account::Line->new(
220
            payment_type      => $payment_type,
222
            {
221
            amountoutstanding => 0 - $balance_remaining,
223
                borrowernumber    => $self->{patron_id},
222
            manager_id        => $manager_id,
224
                accountno         => $accountno,
223
            note              => $note,
225
                date              => dt_from_string(),
224
        }
226
                amount            => 0 - $amount,
225
    )->store();
227
                description       => $description,
228
                accounttype       => $account_type,
229
                payment_type      => $payment_type,
230
                amountoutstanding => 0 - $balance_remaining,
231
                manager_id        => $manager_id,
232
                note              => $note,
233
            }
234
        )->store();
235
    }
226
236
227
    foreach my $o ( @account_offsets ) {
237
    foreach my $o ( @account_offsets ) {
228
        $o->credit_id( $payment->id() );
238
        $o->credit_id( $payment->id() );
Lines 231-263 sub pay { Link Here
231
241
232
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
242
    $library_id ||= $userenv ? $userenv->{'branch'} : undef;
233
243
234
    UpdateStats(
244
    unless ( $credit_id ) {
235
        {
245
        UpdateStats(
236
            branch         => $library_id,
246
            {
237
            type           => $type,
247
                branch         => $library_id,
238
            amount         => $amount,
248
                type           => $type,
239
            borrowernumber => $self->{patron_id},
249
                amount         => $amount,
240
            accountno      => $accountno,
250
                borrowernumber => $self->{patron_id},
241
        }
251
                accountno      => $accountno,
242
    );
252
            }
243
244
    if ( C4::Context->preference("FinesLog") ) {
245
        logaction(
246
            "FINES", 'CREATE',
247
            $self->{patron_id},
248
            Dumper(
249
                {
250
                    action            => "create_$type",
251
                    borrowernumber    => $self->{patron_id},
252
                    accountno         => $accountno,
253
                    amount            => 0 - $amount,
254
                    amountoutstanding => 0 - $balance_remaining,
255
                    accounttype       => $account_type,
256
                    accountlines_paid => \@fines_paid,
257
                    manager_id        => $manager_id,
258
                }
259
            )
260
        );
253
        );
254
255
        if ( C4::Context->preference("FinesLog") ) {
256
            logaction(
257
                "FINES", 'CREATE',
258
                $self->{patron_id},
259
                Dumper(
260
                    {
261
                        action            => "create_$type",
262
                        borrowernumber    => $self->{patron_id},
263
                        accountno         => $accountno,
264
                        amount            => 0 - $amount,
265
                        amountoutstanding => 0 - $balance_remaining,
266
                        accounttype       => $account_type,
267
                        accountlines_paid => \@fines_paid,
268
                        manager_id        => $manager_id,
269
                    }
270
                )
271
            );
272
        }
261
    }
273
    }
262
274
263
    return $payment->id;
275
    return $payment->id;
Lines 287-292 sub balance { Link Here
287
      : 0;
299
      : 0;
288
}
300
}
289
301
302
=head3 normalize_balance
303
304
$account->normalize_balance();
305
306
Find outstanding credits and use them to pay outstanding debits
307
308
=cut
309
310
sub normalize_balance {
311
    my ($self) = @_;
312
    my @credits = Koha::Account::Lines->search(
313
        {
314
            borrowernumber    => $self->{patron_id},
315
            amountoutstanding => { '<' => 0 },
316
        }
317
    );
318
319
    foreach my $credit (@credits) {
320
        $self->pay(
321
            {
322
                credit_id => $credit->id,
323
                amount    => $credit->amountoutstanding * -1,
324
            }
325
        );
326
    }
327
328
    return $self;
329
}
330
290
1;
331
1;
291
332
292
=head1 AUTHOR
333
=head1 AUTHOR
(-)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 => 23;
21
use Test::More tests => 24;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
Lines 547-550 subtest "Koha::Account::Line::void tests" => sub { Link Here
547
    is( $line2->amountoutstanding, '20.000000', 'Second fee again has amount outstanding of 20' );
547
    is( $line2->amountoutstanding, '20.000000', 'Second fee again has amount outstanding of 20' );
548
};
548
};
549
549
550
subtest "Koha::Account::normalize_balance tests" => sub {
551
552
    plan tests => 6;
553
554
    # Create a borrower
555
    my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
556
    my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
557
558
    my $borrower = Koha::Patron->new( {
559
        cardnumber => 'kyliehall',
560
        surname => 'Hall',
561
        firstname => 'Kylie',
562
    } );
563
    $borrower->categorycode( $categorycode );
564
    $borrower->branchcode( $branchcode );
565
    $borrower->store;
566
567
    my $account = Koha::Account->new({ patron_id => $borrower->id });
568
569
    my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => -10, amountoutstanding => -10 })->store();
570
    my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10 })->store();
571
572
    is( $account->balance(), "0.000000", "Account balance is 0" );
573
    is( $line1->amountoutstanding, "-10", 'Credit has amount outstanding of -10' );
574
    is( $line2->amountoutstanding, "10", 'Fee has amount outstanding of 10' );
575
576
    $account->normalize_balance();
577
578
    is( $account->balance(), "0.000000", "Account balance is 0" );
579
580
    $line1->_result->discard_changes();
581
    $line2->_result->discard_changes();
582
    is( $line1->amountoutstanding, '0.000000', 'First fee has amount outstanding of 0' );
583
    is( $line2->amountoutstanding, '0.000000', 'Second fee has amount outstanding of 0' );
584
};
585
550
1;
586
1;
551
- 

Return to bug 18805