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

(-)a/Koha/Account.pm (-27 / +42 lines)
Lines 489-507 sub add_credit { Link Here
489
489
490
This method allows adding debits to a patron's account
490
This method allows adding debits to a patron's account
491
491
492
my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit(
492
    my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit(
493
    {
493
        {
494
        amount       => $amount,
494
            amount           => $amount,
495
        description  => $description,
495
            description      => $description,
496
        note         => $note,
496
            note             => $note,
497
        user_id      => $user_id,
497
            user_id          => $user_id,
498
        interface    => $interface,
498
            interface        => $interface,
499
        library_id   => $library_id,
499
            library_id       => $library_id,
500
        type         => $debit_type,
500
            type             => $debit_type,
501
        item_id      => $item_id,
501
            transaction_type => $transaction_type,
502
        issue_id     => $issue_id
502
            cash_register    => $register_id,
503
    }
503
            item_id          => $item_id,
504
);
504
            issue_id         => $issue_id
505
        }
506
    );
505
507
506
$debit_type can be any of:
508
$debit_type can be any of:
507
  - ACCOUNT
509
  - ACCOUNT
Lines 517-522 $debit_type can be any of: Link Here
517
  - RENT_RENEW
519
  - RENT_RENEW
518
  - RENT_DAILY_RENEW
520
  - RENT_DAILY_RENEW
519
  - RESERVE
521
  - RESERVE
522
  - PAYOUT
520
523
521
=cut
524
=cut
522
525
Lines 533-538 sub add_debit { Link Here
533
        }
536
        }
534
    }
537
    }
535
538
539
    # check for cash register if using cash
540
    Koha::Exceptions::Account::RegisterRequired->throw()
541
      if ( C4::Context->preference("UseCashRegisters")
542
        && defined( $params->{transaction_type} )
543
        && ( $params->{transaction_type} eq 'CASH' )
544
        && !defined( $params->{cash_register} ) );
545
536
    # amount should always be a positive value
546
    # amount should always be a positive value
537
    my $amount = $params->{amount};
547
    my $amount = $params->{amount};
538
    unless ( $amount > 0 ) {
548
    unless ( $amount > 0 ) {
Lines 540-554 sub add_debit { Link Here
540
            error => 'Debit amount passed is not positive' );
550
            error => 'Debit amount passed is not positive' );
541
    }
551
    }
542
552
543
    my $description = $params->{description} // q{};
553
    my $description      = $params->{description} // q{};
544
    my $note        = $params->{note} // q{};
554
    my $note             = $params->{note} // q{};
545
    my $user_id     = $params->{user_id};
555
    my $user_id          = $params->{user_id};
546
    my $interface   = $params->{interface};
556
    my $interface        = $params->{interface};
547
    my $library_id  = $params->{library_id};
557
    my $library_id       = $params->{library_id};
548
    my $debit_type  = $params->{type};
558
    my $cash_register    = $params->{cash_register};
549
    my $item_id     = $params->{item_id};
559
    my $debit_type       = $params->{type};
550
    my $issue_id    = $params->{issue_id};
560
    my $transaction_type = $params->{transaction_type};
551
    my $offset_type = $Koha::Account::offset_type->{$debit_type} // 'Manual Debit';
561
    my $item_id          = $params->{item_id};
562
    my $issue_id         = $params->{issue_id};
563
    my $offset_type      = $Koha::Account::offset_type->{$debit_type} // 'Manual Debit';
552
564
553
    my $line;
565
    my $line;
554
    my $schema = Koha::Database->new->schema;
566
    my $schema = Koha::Database->new->schema;
Lines 565-577 sub add_debit { Link Here
565
                        description       => $description,
577
                        description       => $description,
566
                        debit_type_code   => $debit_type,
578
                        debit_type_code   => $debit_type,
567
                        amountoutstanding => $amount,
579
                        amountoutstanding => $amount,
568
                        payment_type      => undef,
580
                        payment_type      => $transaction_type,
569
                        note              => $note,
581
                        note              => $note,
570
                        manager_id        => $user_id,
582
                        manager_id        => $user_id,
571
                        interface         => $interface,
583
                        interface         => $interface,
572
                        itemnumber        => $item_id,
584
                        itemnumber        => $item_id,
573
                        issue_id          => $issue_id,
585
                        issue_id          => $issue_id,
574
                        branchcode        => $library_id,
586
                        branchcode        => $library_id,
587
                        register_id       => $cash_register,
575
                        (
588
                        (
576
                            $debit_type eq 'OVERDUE'
589
                            $debit_type eq 'OVERDUE'
577
                            ? ( status => 'UNRETURNED' )
590
                            ? ( status => 'UNRETURNED' )
Lines 669-675 sub payout_amount { Link Here
669
    my $amount = $params->{amount};
682
    my $amount = $params->{amount};
670
    unless ( $amount > 0 ) {
683
    unless ( $amount > 0 ) {
671
        Koha::Exceptions::Account::AmountNotPositive->throw(
684
        Koha::Exceptions::Account::AmountNotPositive->throw(
672
            error => 'Debit amount passed is not positive' );
685
            error => 'Payout amount passed is not positive' );
673
    }
686
    }
674
687
675
    # Amount should always be less than or equal to outstanding credit
688
    # Amount should always be less than or equal to outstanding credit
Lines 696-707 sub payout_amount { Link Here
696
                {
709
                {
697
                    amount            => $params->{amount},
710
                    amount            => $params->{amount},
698
                    type              => 'PAYOUT',
711
                    type              => 'PAYOUT',
699
                    payment_type      => $params->{payout_type},
712
                    transaction_type  => $params->{payout_type},
700
                    amountoutstanding => $params->{amount},
713
                    amountoutstanding => $params->{amount},
701
                    manager_id        => $params->{staff_id},
714
                    manager_id        => $params->{staff_id},
702
                    interface         => $params->{interface},
715
                    interface         => $params->{interface},
703
                    branchcode        => $params->{branch},
716
                    branchcode        => $params->{branch},
704
                    register_id       => $params->{cash_register}
717
                    cash_register     => $params->{cash_register}
705
                }
718
                }
706
            );
719
            );
707
720
Lines 710-715 sub payout_amount { Link Here
710
                $credit->apply(
723
                $credit->apply(
711
                    { debits => [$payout], offset_type => 'PAYOUT' } );
724
                    { debits => [$payout], offset_type => 'PAYOUT' } );
712
                $payout->discard_changes;
725
                $payout->discard_changes;
726
                last if $payout->amountoutstanding == 0;
713
            }
727
            }
714
728
715
            # Set payout as paid
729
            # Set payout as paid
Lines 884-890 our $offset_type = { Link Here
884
    'RENT_RENEW'       => 'Rental Fee',
898
    'RENT_RENEW'       => 'Rental Fee',
885
    'RENT_DAILY_RENEW' => 'Rental Fee',
899
    'RENT_DAILY_RENEW' => 'Rental Fee',
886
    'OVERDUE'          => 'OVERDUE',
900
    'OVERDUE'          => 'OVERDUE',
887
    'RESERVE_EXPIRED'  => 'Hold Expired'
901
    'RESERVE_EXPIRED'  => 'Hold Expired',
902
    'PAYOUT'           => 'PAYOUT',
888
};
903
};
889
904
890
=head1 AUTHORS
905
=head1 AUTHORS
(-)a/t/db_dependent/Koha/Account.t (-3 / +30 lines)
Lines 1177-1183 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->stor Link Here
1177
};
1177
};
1178
1178
1179
subtest 'Koha::Account::payout_amount() tests' => sub {
1179
subtest 'Koha::Account::payout_amount() tests' => sub {
1180
    plan tests => 21;
1180
    plan tests => 39;
1181
1181
1182
    $schema->storage->txn_begin;
1182
    $schema->storage->txn_begin;
1183
1183
Lines 1258-1269 subtest 'Koha::Account::payout_amount() tests' => sub { Link Here
1258
    is($credits->count, 1, "Payout was applied against oldest outstanding credits first");
1258
    is($credits->count, 1, "Payout was applied against oldest outstanding credits first");
1259
    is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining");
1259
    is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining");
1260
1260
1261
    my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1262
    is( $offsets->count, 4, 'Four offsets generated' );
1263
    my $offset = $offsets->next;
1264
    is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' );
1265
    is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1266
    $offset = $offsets->next;
1267
    is( $offset->credit_id, $credit_1->id, "Offset added against credit_1");
1268
    is( $offset->type,       'PAYOUT', "PAYOUT used for offset_type" );
1269
    is( $offset->amount * 1, -2,      'Correct amount offset against credit_1' );
1270
    $offset = $offsets->next;
1271
    is( $offset->credit_id, $credit_2->id, "Offset added against credit_2");
1272
    is( $offset->type,       'PAYOUT', "PAYOUT used for offset_type" );
1273
    is( $offset->amount * 1, -3,      'Correct amount offset against credit_2' );
1274
    $offset = $offsets->next;
1275
    is( $offset->credit_id, $credit_3->id, "Offset added against credit_3");
1276
    is( $offset->type,       'PAYOUT', "PAYOUT used for offset_type" );
1277
    is( $offset->amount * 1, -5,      'Correct amount offset against credit_3' );
1278
1261
    my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1279
    my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1262
    $credits = $account->outstanding_credits();
1280
    $credits = $account->outstanding_credits();
1263
    is($credits->count, 2, "New credit added");
1281
    is($credits->count, 2, "New credit added");
1264
    $payout_params->{amount} = 2.50;
1282
    $payout_params->{amount} = 2.50;
1265
    $payout_params->{credits} = [$credit_5];
1283
    $payout_params->{credits} = [$credit_5];
1266
    $account->payout_amount($payout_params);
1284
    $payout = $account->payout_amount($payout_params);
1267
1285
1268
    $credits = $account->outstanding_credits();
1286
    $credits = $account->outstanding_credits();
1269
    is($credits->count, 2, "Second credit not fully paid off");
1287
    is($credits->count, 2, "Second credit not fully paid off");
Lines 1273-1277 subtest 'Koha::Account::payout_amount() tests' => sub { Link Here
1273
    is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount");
1291
    is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount");
1274
    is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed");
1292
    is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed");
1275
1293
1294
    $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1295
    is( $offsets->count, 2, 'Two offsets generated' );
1296
    $offset = $offsets->next;
1297
    is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' );
1298
    is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1299
    $offset = $offsets->next;
1300
    is( $offset->credit_id, $credit_5->id, "Offset added against credit_5");
1301
    is( $offset->type,       'PAYOUT', "PAYOUT used for offset_type" );
1302
    is( $offset->amount * 1, -2.50,      'Correct amount offset against credit_5' );
1303
1276
    $schema->storage->txn_rollback;
1304
    $schema->storage->txn_rollback;
1277
};
1305
};
1278
- 

Return to bug 24300