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

(-)a/t/db_dependent/api/v1/patrons_accounts.t (-36 / +38 lines)
Lines 371-420 subtest 'list_debits() test' => sub { Link Here
371
371
372
subtest 'add_debit() tests' => sub {
372
subtest 'add_debit() tests' => sub {
373
373
374
    plan tests => 18;
374
    plan tests => 20;
375
375
376
    $schema->storage->txn_begin;
376
    $schema->storage->txn_begin;
377
377
378
    my $patron = $builder->build_object(
378
    my $librarian = $builder->build_object(
379
        {
379
        {
380
            class => 'Koha::Patrons',
380
            class => 'Koha::Patrons',
381
            value => { flags => 1 }
381
            value => { flags => 1 }
382
        }
382
        }
383
    );
383
    );
384
    my $password = 'thePassword123';
384
    my $password = 'thePassword123';
385
    $patron->set_password( { password => $password, skip_validation => 1 } );
385
    $librarian->set_password( { password => $password, skip_validation => 1 } );
386
    my $userid = $patron->userid;
386
    my $userid = $librarian->userid;
387
    my $librarian_x = $builder->build_object({ class => 'Koha::Patrons' });
387
388
389
    my $patron    = $builder->build_object( { class => 'Koha::Patrons' } );
388
    my $library   = $builder->build_object( { class => 'Koha::Libraries' } );
390
    my $library   = $builder->build_object( { class => 'Koha::Libraries' } );
389
    my $patron_id = $patron->id;
391
    my $patron_id = $patron->id;
390
    my $account   = $patron->account;
392
    my $account   = $patron->account;
391
393
392
    is( $account->outstanding_debits->count,
394
    is(
393
        0, 'No outstanding debits for patron' );
395
        $account->outstanding_debits->count,
394
    is( $account->outstanding_credits->count,
396
        0, 'No outstanding debits for patron'
395
        0, 'No outstanding credits for patron' );
397
    );
398
    is(
399
        $account->outstanding_credits->count,
400
        0, 'No outstanding credits for patron'
401
    );
396
402
397
    my $debit = {
403
    my $debit = {
398
        amount      => 100,
404
        amount      => 100,
399
        description => "A description",
405
        description => "A description",
400
        type        => "NEW_CARD",
406
        type        => "NEW_CARD",
401
        user_id     => $patron->borrowernumber,
407
        user_id     => $librarian_x->id,
402
        interface   => 'test',
408
        interface   => 'test',
403
        library_id  => $library->id,
409
        library_id  => $library->id,
404
    };
410
    };
405
411
406
    my $ret = $t->post_ok(
412
    my $ret = $t->post_ok( "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $debit )
407
        "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" =>
413
        ->status_is(201)->json_is( '/user_id' => $librarian_x->id, 'Passed user_id is stored' )->tx->res->json;
408
          json => $debit )->status_is(201)->tx->res->json;
409
    my $account_line = Koha::Account::Debits->find( $ret->{account_line_id} );
414
    my $account_line = Koha::Account::Debits->find( $ret->{account_line_id} );
410
415
411
    is_deeply( $ret, $account_line->to_api, 'Line returned correctly' );
416
    is_deeply( $ret, $account_line->to_api, 'Line returned correctly' );
412
417
413
    is( $account_line->branchcode,
418
    is(
414
        $library->id, 'Library id is stored correctly' );
419
        $account_line->branchcode,
420
        $library->id, 'Library id is stored correctly'
421
    );
415
422
416
    my $outstanding_debits = $account->outstanding_debits;
423
    my $outstanding_debits = $account->outstanding_debits;
417
    is( $outstanding_debits->count,             1, "One outstanding debit added" );
424
    is( $outstanding_debits->count,             1,   "One outstanding debit added" );
418
    is( $outstanding_debits->total_outstanding, 100, "Outstanding debit is 100" );
425
    is( $outstanding_debits->total_outstanding, 100, "Outstanding debit is 100" );
419
426
420
    my $credit_1 = $account->add_credit(
427
    my $credit_1 = $account->add_credit(
Lines 431-447 subtest 'add_debit() tests' => sub { Link Here
431
    )->store()->apply( { debits => [$account_line] } );
438
    )->store()->apply( { debits => [$account_line] } );
432
439
433
    is( $account->outstanding_credits->total_outstanding, 0, "Credits all applied" );
440
    is( $account->outstanding_credits->total_outstanding, 0, "Credits all applied" );
434
    is( $account->outstanding_debits->total_outstanding,
441
    is(
435
        75, "Credits partially cancelled debit" );
442
        $account->outstanding_debits->total_outstanding,
443
        75, "Credits partially cancelled debit"
444
    );
436
445
437
    my $account_line_id = $ret->{account_line_id};
446
    my $account_line_id = $ret->{account_line_id};
438
447
439
    $t->post_ok(
448
    $t->post_ok( "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $debit )->status_is(201);
440
        "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" =>
441
          json => $debit )->status_is(201);
442
449
443
    is( $account->outstanding_debits->total_outstanding,
450
    is(
444
        175, "Debit added to total outstanding debits" );
451
        $account->outstanding_debits->total_outstanding,
452
        175, "Debit added to total outstanding debits"
453
    );
445
454
446
    # Cash register handling and PAYOUTs
455
    # Cash register handling and PAYOUTs
447
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
456
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
Lines 450-474 subtest 'add_debit() tests' => sub { Link Here
450
        description => "A description",
459
        description => "A description",
451
        type        => "PAYOUT",
460
        type        => "PAYOUT",
452
        payout_type => "CASH",
461
        payout_type => "CASH",
453
        user_id     => $patron->borrowernumber,
454
        interface   => 'test',
462
        interface   => 'test',
455
        library_id  => $library->id,
463
        library_id  => $library->id,
456
    };
464
    };
457
465
458
    $t->post_ok(
466
    $t->post_ok( "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $payout )->status_is(400)
459
        "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" =>
467
        ->json_is( '/error' => 'Account transaction requires a cash register' );
460
          json => $payout )->status_is(400)
461
      ->json_is( '/error' => 'Account transaction requires a cash register' );
462
468
463
    my $register = $builder->build_object(
469
    my $register = $builder->build_object( { class => 'Koha::Cash::Registers', } );
464
        {
465
            class => 'Koha::Cash::Registers',
466
        }
467
    );
468
    $payout->{cash_register_id} = $register->id;
470
    $payout->{cash_register_id} = $register->id;
469
    my $res = $t->post_ok(
471
    my $res = $t->post_ok( "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $payout )
470
        "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" =>
472
        ->status_is(201)->tx->res->json;
471
          json => $payout )->status_is(201)->tx->res->json;
473
474
    is( $res->{user_id}, $librarian->id, 'If not passed, the session user ID is picked' );
472
475
473
    $schema->storage->txn_rollback;
476
    $schema->storage->txn_rollback;
474
};
477
};
475
- 

Return to bug 37535