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 1146-1152 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->stor Link Here
1146
};
1146
};
1147
1147
1148
subtest 'Koha::Account::payout_amount() tests' => sub {
1148
subtest 'Koha::Account::payout_amount() tests' => sub {
1149
    plan tests => 21;
1149
    plan tests => 39;
1150
1150
1151
    $schema->storage->txn_begin;
1151
    $schema->storage->txn_begin;
1152
1152
Lines 1227-1238 subtest 'Koha::Account::payout_amount() tests' => sub { Link Here
1227
    is($credits->count, 1, "Payout was applied against oldest outstanding credits first");
1227
    is($credits->count, 1, "Payout was applied against oldest outstanding credits first");
1228
    is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining");
1228
    is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining");
1229
1229
1230
    my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1231
    is( $offsets->count, 4, 'Four offsets generated' );
1232
    my $offset = $offsets->next;
1233
    is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' );
1234
    is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1235
    $offset = $offsets->next;
1236
    is( $offset->credit_id, $credit_1->id, "Offset added against credit_1");
1237
    is( $offset->type,       'PAYOUT', "PAYOUT used for offset_type" );
1238
    is( $offset->amount * 1, -2,      'Correct amount offset against credit_1' );
1239
    $offset = $offsets->next;
1240
    is( $offset->credit_id, $credit_2->id, "Offset added against credit_2");
1241
    is( $offset->type,       'PAYOUT', "PAYOUT used for offset_type" );
1242
    is( $offset->amount * 1, -3,      'Correct amount offset against credit_2' );
1243
    $offset = $offsets->next;
1244
    is( $offset->credit_id, $credit_3->id, "Offset added against credit_3");
1245
    is( $offset->type,       'PAYOUT', "PAYOUT used for offset_type" );
1246
    is( $offset->amount * 1, -5,      'Correct amount offset against credit_3' );
1247
1230
    my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1248
    my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1231
    $credits = $account->outstanding_credits();
1249
    $credits = $account->outstanding_credits();
1232
    is($credits->count, 2, "New credit added");
1250
    is($credits->count, 2, "New credit added");
1233
    $payout_params->{amount} = 2.50;
1251
    $payout_params->{amount} = 2.50;
1234
    $payout_params->{credits} = [$credit_5];
1252
    $payout_params->{credits} = [$credit_5];
1235
    $account->payout_amount($payout_params);
1253
    $payout = $account->payout_amount($payout_params);
1236
1254
1237
    $credits = $account->outstanding_credits();
1255
    $credits = $account->outstanding_credits();
1238
    is($credits->count, 2, "Second credit not fully paid off");
1256
    is($credits->count, 2, "Second credit not fully paid off");
Lines 1242-1246 subtest 'Koha::Account::payout_amount() tests' => sub { Link Here
1242
    is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount");
1260
    is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount");
1243
    is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed");
1261
    is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed");
1244
1262
1263
    $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1264
    is( $offsets->count, 2, 'Two offsets generated' );
1265
    $offset = $offsets->next;
1266
    is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' );
1267
    is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1268
    $offset = $offsets->next;
1269
    is( $offset->credit_id, $credit_5->id, "Offset added against credit_5");
1270
    is( $offset->type,       'PAYOUT', "PAYOUT used for offset_type" );
1271
    is( $offset->amount * 1, -2.50,      'Correct amount offset against credit_5' );
1272
1245
    $schema->storage->txn_rollback;
1273
    $schema->storage->txn_rollback;
1246
};
1274
};
1247
- 

Return to bug 24300