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

(-)a/Koha/REST/V1/Patrons/Account.pm (-4 / +28 lines)
Lines 243-257 sub add_debit { Link Here
243
          Koha::Account::Debit->new_from_api( $c->validation->param('body') )
243
          Koha::Account::Debit->new_from_api( $c->validation->param('body') )
244
          ->unblessed;
244
          ->unblessed;
245
245
246
        $data->{library_id}    = $data->{branchcode};
246
        $data->{library_id}       = delete $data->{branchcode};
247
        $data->{cash_register} = $data->{cash_register_id};
247
        $data->{type}             = delete $data->{debit_type_code};
248
        $data->{item_id}       = $data->{itemnumber};
248
        $data->{cash_register}    = delete $data->{register_id};
249
        $data->{interface}     = 'api'
249
        $data->{item_id}          = delete $data->{itemnumber};
250
        $data->{transaction_type} = delete $data->{payment_type};
251
        $data->{interface}        = 'api'
250
          ; # Should this always be API, or should we allow the API consumer to choose?
252
          ; # Should this always be API, or should we allow the API consumer to choose?
251
        $data->{user_id} = $patron->borrowernumber
253
        $data->{user_id} = $patron->borrowernumber
252
          ; # Should this be API user OR staff the API may be acting on behalf of?
254
          ; # Should this be API user OR staff the API may be acting on behalf of?
253
255
254
        my $debit = $patron->account->add_debit($data);
256
        my $debit = $patron->account->add_debit($data);
257
        $debit = Koha::Account::Debit->_new_from_dbic($debit->{_result});
255
258
256
        $c->res->headers->location(
259
        $c->res->headers->location(
257
            $c->req->url->to_string . '/' . $debit->id );
260
            $c->req->url->to_string . '/' . $debit->id );
Lines 263-268 sub add_debit { Link Here
263
        );
266
        );
264
    }
267
    }
265
    catch {
268
    catch {
269
        if ( blessed $_ ) {
270
            if ( $_->isa('Koha::Exceptions::Account::RegisterRequired') ) {
271
                return $c->render(
272
                    status  => 400,
273
                    openapi => { error => $_->description }
274
                );
275
            }
276
            elsif ( $_->isa('Koha::Exceptions::Account::AmountNotPositive') ) {
277
                return $c->render(
278
                    status => 400,
279
                    openapi => { error => $_->description }
280
                );
281
            }
282
            elsif ( $_->isa('Koha::Exceptions::Account::UnrecognisedType') ) {
283
                return $c->render(
284
                    status => 400,
285
                    openapi => { error => $_->description }
286
                );
287
            }
288
        }
289
266
        $c->unhandled_exception($_);
290
        $c->unhandled_exception($_);
267
    };
291
    };
268
}
292
}
(-)a/api/v1/swagger/paths/patrons_account.yaml (-1 / +5 lines)
Lines 197-203 Link Here
197
      "201":
197
      "201":
198
        description: Debit added
198
        description: Debit added
199
        schema:
199
        schema:
200
          $ref: "../swagger.yaml#/definitions/account_line"
200
          $ref: "../swagger.yaml#/definitions/debit"
201
      "400":
202
        description: Bad parameter
203
        schema:
204
          $ref: "../swagger.yaml#/definitions/error"
201
      "401":
205
      "401":
202
        description: Authentication required
206
        description: Authentication required
203
        schema:
207
        schema:
(-)a/t/db_dependent/api/v1/patrons_accounts.t (-8 / +34 lines)
Lines 371-377 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 => 13;
374
    plan tests => 18;
375
375
376
    $schema->storage->txn_begin;
376
    $schema->storage->txn_begin;
377
377
Lines 397-403 subtest 'add_debit() tests' => sub { Link Here
397
    my $debit = {
397
    my $debit = {
398
        amount      => 100,
398
        amount      => 100,
399
        description => "A description",
399
        description => "A description",
400
        debit_type  => "NEW_CARD",
400
        type        => "NEW_CARD",
401
        user_id     => $patron->borrowernumber,
401
        user_id     => $patron->borrowernumber,
402
        interface   => 'test',
402
        interface   => 'test',
403
        library_id  => $library->id,
403
        library_id  => $library->id,
Lines 406-421 subtest 'add_debit() tests' => sub { Link Here
406
    my $ret = $t->post_ok(
406
    my $ret = $t->post_ok(
407
        "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" =>
407
        "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" =>
408
          json => $debit )->status_is(201)->tx->res->json;
408
          json => $debit )->status_is(201)->tx->res->json;
409
    my $account_line = Koha::Account::Lines->find( $ret->{account_line_id} );
409
    my $account_line = Koha::Account::Debits->find( $ret->{account_line_id} );
410
410
411
    is_deeply( $ret, $account_line->to_api, 'Line returned correctly' );
411
    is_deeply( $ret, $account_line->to_api, 'Line returned correctly' );
412
412
413
    is( $account_line->branchcode,
413
    is( $account_line->branchcode,
414
        $library->id, 'Library id is sored correctly' );
414
        $library->id, 'Library id is stored correctly' );
415
415
416
    my $outstanding_debits = $account->outstanding_debits;
416
    my $outstanding_debits = $account->outstanding_debits;
417
    is( $outstanding_debits->count,             1 );
417
    is( $outstanding_debits->count,             1, "One outstanding debit added" );
418
    is( $outstanding_debits->total_outstanding, 100 );
418
    is( $outstanding_debits->total_outstanding, 100, "Outstanding debit is 100" );
419
419
420
    my $credit_1 = $account->add_credit(
420
    my $credit_1 = $account->add_credit(
421
        {
421
        {
Lines 430-436 subtest 'add_debit() tests' => sub { Link Here
430
        }
430
        }
431
    )->store()->apply( { debits => [$account_line] } );
431
    )->store()->apply( { debits => [$account_line] } );
432
432
433
    is( $account->outstanding_credits->total_outstanding, 0 );
433
    is( $account->outstanding_credits->total_outstanding, 0, "Credits all applied" );
434
    is( $account->outstanding_debits->total_outstanding,
434
    is( $account->outstanding_debits->total_outstanding,
435
        75, "Credits partially cancelled debit" );
435
        75, "Credits partially cancelled debit" );
436
436
Lines 443-447 subtest 'add_debit() tests' => sub { Link Here
443
    is( $account->outstanding_debits->total_outstanding,
443
    is( $account->outstanding_debits->total_outstanding,
444
        175, "Debit added to total outstanding debits" );
444
        175, "Debit added to total outstanding debits" );
445
445
446
    # Cash register handling and PAYOUTs
447
    t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
448
    my $payout = {
449
        amount      => 10,
450
        description => "A description",
451
        type        => "PAYOUT",
452
        payout_type => "CASH",
453
        user_id     => $patron->borrowernumber,
454
        interface   => 'test',
455
        library_id  => $library->id,
456
    };
457
458
    $t->post_ok(
459
        "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" =>
460
          json => $payout )->status_is(400)
461
      ->json_is( '/error' => 'Account transaction requires a cash register' );
462
463
    my $register = $builder->build_object(
464
        {
465
            class => 'Koha::Cash::Registers',
466
        }
467
    );
468
    $payout->{cash_register_id} = $register->id;
469
    my $res = $t->post_ok(
470
        "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" =>
471
          json => $payout )->status_is(201)->tx->res->json;
472
446
    $schema->storage->txn_rollback;
473
    $schema->storage->txn_rollback;
447
};
474
};
448
- 

Return to bug 21043