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 1175-1177 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->stor Link Here
1175
1175
1176
    $schema->storage->txn_rollback;
1176
    $schema->storage->txn_rollback;
1177
};
1177
};
1178
- 
1178
1179
subtest 'Koha::Account::payout_amount() tests' => sub {
1180
    plan tests => 21;
1181
1182
    $schema->storage->txn_begin;
1183
1184
    # delete logs and statistics
1185
    my $action_logs = $schema->resultset('ActionLog')->search()->count;
1186
    my $statistics  = $schema->resultset('Statistic')->search()->count;
1187
1188
    my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1189
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1190
    my $register =
1191
      $builder->build_object( { class => 'Koha::Cash::Registers' } );
1192
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1193
    my $account =
1194
      Koha::Account->new( { patron_id => $patron->borrowernumber } );
1195
1196
    is( $account->balance, 0, 'Test patron has no balance' );
1197
1198
    my $payout_params = {
1199
        payout_type => 'CASH',
1200
        branch      => $library->id,
1201
        register_id => $register->id,
1202
        staff_id    => $staff->id,
1203
        interface   => 'intranet',
1204
        amount      => -10,
1205
    };
1206
1207
    my @required_fields =
1208
      ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
1209
    for my $required_field (@required_fields) {
1210
        my $this_payout = { %{$payout_params} };
1211
        delete $this_payout->{$required_field};
1212
1213
        throws_ok {
1214
            $account->payout_amount($this_payout);
1215
        }
1216
        'Koha::Exceptions::MissingParameter',
1217
          "Exception thrown if $required_field parameter missing";
1218
    }
1219
1220
    throws_ok {
1221
        $account->payout_amount($payout_params);
1222
    }
1223
    'Koha::Exceptions::Account::AmountNotPositive',
1224
      'Expected validation exception thrown (amount not positive)';
1225
1226
    $payout_params->{amount} = 10;
1227
    throws_ok {
1228
        $account->payout_amount($payout_params);
1229
    }
1230
    'Koha::Exceptions::ParameterTooHigh',
1231
      'Expected validation exception thrown (amount greater than outstanding)';
1232
1233
    # Enable cash registers
1234
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1235
    throws_ok {
1236
        $account->payout_amount($payout_params);
1237
    }
1238
    'Koha::Exceptions::Account::RegisterRequired',
1239
'Exception thrown for UseCashRegisters:1 + payout_type:CASH + cash_register:undef';
1240
1241
    # Disable cash registers
1242
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1243
1244
    # Add some outstanding credits
1245
    my $credit_1 = $account->add_credit( { amount => 2,  interface => 'commandline' } );
1246
    my $credit_2 = $account->add_credit( { amount => 3,  interface => 'commandline' } );
1247
    my $credit_3 = $account->add_credit( { amount => 5,  interface => 'commandline' } );
1248
    my $credit_4 = $account->add_credit( { amount => 10, interface => 'commandline' } );
1249
    my $credits = $account->outstanding_credits();
1250
    is( $credits->count, 4, "Found 4 credits with outstanding amounts" );
1251
    is( $credits->total_outstanding + 0, -20, "Total -20 outstanding credit" );
1252
1253
    my $payout = $account->payout_amount($payout_params);
1254
    is(ref($payout), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payout');
1255
    is($payout->amount + 0, 10, "Payout amount recorded correctly");
1256
    is($payout->amountoutstanding + 0, 0, "Full amount was paid out");
1257
    $credits = $account->outstanding_credits();
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");
1260
1261
    my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1262
    $credits = $account->outstanding_credits();
1263
    is($credits->count, 2, "New credit added");
1264
    $payout_params->{amount} = 2.50;
1265
    $payout_params->{credits} = [$credit_5];
1266
    $account->payout_amount($payout_params);
1267
1268
    $credits = $account->outstanding_credits();
1269
    is($credits->count, 2, "Second credit not fully paid off");
1270
    is($credits->total_outstanding + 0, -12.50, "12.50 outstanding credit remaining");
1271
    $credit_4->discard_changes;
1272
    $credit_5->discard_changes;
1273
    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");
1275
1276
    $schema->storage->txn_rollback;
1277
};

Return to bug 24300