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

(-)a/t/db_dependent/Koha/Account.t (-4 / +103 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 13;
22
use Test::More tests => 14;
23
use Test::MockModule;
23
use Test::MockModule;
24
use Test::Exception;
24
use Test::Exception;
25
25
Lines 305-311 subtest 'add_credit() tests' => sub { Link Here
305
      'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
305
      'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
306
306
307
    # Disable cash registers
307
    # Disable cash registers
308
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
308
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
309
309
310
    $schema->storage->txn_rollback;
310
    $schema->storage->txn_rollback;
311
};
311
};
Lines 717-723 subtest 'pay() tests' => sub { Link Here
717
      'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
717
      'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
718
718
719
    # Disable cash registers
719
    # Disable cash registers
720
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
720
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
721
721
722
    # Undef userenv
722
    # Undef userenv
723
    $context->mock( 'userenv', undef );
723
    $context->mock( 'userenv', undef );
Lines 1144-1146 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->stor Link Here
1144
1144
1145
    $schema->storage->txn_rollback;
1145
    $schema->storage->txn_rollback;
1146
};
1146
};
1147
- 
1147
1148
subtest 'Koha::Account::payout_amount() tests' => sub {
1149
    plan tests => 21;
1150
1151
    $schema->storage->txn_begin;
1152
1153
    # delete logs and statistics
1154
    my $action_logs = $schema->resultset('ActionLog')->search()->count;
1155
    my $statistics  = $schema->resultset('Statistic')->search()->count;
1156
1157
    my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1158
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1159
    my $register =
1160
      $builder->build_object( { class => 'Koha::Cash::Registers' } );
1161
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1162
    my $account =
1163
      Koha::Account->new( { patron_id => $patron->borrowernumber } );
1164
1165
    is( $account->balance, 0, 'Test patron has no balance' );
1166
1167
    my $payout_params = {
1168
        payout_type => 'CASH',
1169
        branch      => $library->id,
1170
        register_id => $register->id,
1171
        staff_id    => $staff->id,
1172
        interface   => 'intranet',
1173
        amount      => -10,
1174
    };
1175
1176
    my @required_fields =
1177
      ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
1178
    for my $required_field (@required_fields) {
1179
        my $this_payout = { %{$payout_params} };
1180
        delete $this_payout->{$required_field};
1181
1182
        throws_ok {
1183
            $account->payout_amount($this_payout);
1184
        }
1185
        'Koha::Exceptions::MissingParameter',
1186
          "Exception thrown if $required_field parameter missing";
1187
    }
1188
1189
    throws_ok {
1190
        $account->payout_amount($payout_params);
1191
    }
1192
    'Koha::Exceptions::Account::AmountNotPositive',
1193
      'Expected validation exception thrown (amount not positive)';
1194
1195
    $payout_params->{amount} = 10;
1196
    throws_ok {
1197
        $account->payout_amount($payout_params);
1198
    }
1199
    'Koha::Exceptions::ParameterTooHigh',
1200
      'Expected validation exception thrown (amount greater than outstanding)';
1201
1202
    # Enable cash registers
1203
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1204
    throws_ok {
1205
        $account->payout_amount($payout_params);
1206
    }
1207
    'Koha::Exceptions::Account::RegisterRequired',
1208
'Exception thrown for UseCashRegisters:1 + payout_type:CASH + cash_register:undef';
1209
1210
    # Disable cash registers
1211
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1212
1213
    # Add some outstanding credits
1214
    my $credit_1 = $account->add_credit( { amount => 2,  interface => 'commandline' } );
1215
    my $credit_2 = $account->add_credit( { amount => 3,  interface => 'commandline' } );
1216
    my $credit_3 = $account->add_credit( { amount => 5,  interface => 'commandline' } );
1217
    my $credit_4 = $account->add_credit( { amount => 10, interface => 'commandline' } );
1218
    my $credits = $account->outstanding_credits();
1219
    is( $credits->count, 4, "Found 4 credits with outstanding amounts" );
1220
    is( $credits->total_outstanding + 0, -20, "Total -20 outstanding credit" );
1221
1222
    my $payout = $account->payout_amount($payout_params);
1223
    is(ref($payout), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payout');
1224
    is($payout->amount + 0, 10, "Payout amount recorded correctly");
1225
    is($payout->amountoutstanding + 0, 0, "Full amount was paid out");
1226
    $credits = $account->outstanding_credits();
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");
1229
    
1230
    my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1231
    $credits = $account->outstanding_credits();
1232
    is($credits->count, 2, "New credit added");
1233
    $payout_params->{amount} = 2.50;
1234
    $payout_params->{credits} = [$credit_5];
1235
    $account->payout_amount($payout_params);
1236
1237
    $credits = $account->outstanding_credits();
1238
    is($credits->count, 2, "Second credit not fully paid off");
1239
    is($credits->total_outstanding + 0, -12.50, "12.50 outstanding credit remaining");
1240
    $credit_4->discard_changes;
1241
    $credit_5->discard_changes;
1242
    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");
1244
1245
    $schema->storage->txn_rollback;
1246
};

Return to bug 24300