@@ -, +, @@ --- Koha/REST/V1/Patrons/Account.pm | 8 ++- api/v1/swagger/definitions/debit.yaml | 1 - api/v1/swagger/paths/patrons_account.yaml | 2 +- t/db_dependent/api/v1/patrons_accounts.t | 73 ++++++++++++++++++++++- 4 files changed, 80 insertions(+), 4 deletions(-) --- a/Koha/REST/V1/Patrons/Account.pm +++ a/Koha/REST/V1/Patrons/Account.pm @@ -220,15 +220,21 @@ sub add_debit { } return try { - my $data = Koha::Patron::Account::Debit->new_from_api( + my $data = Koha::Account::Debit->new_from_api( $c->validation->param('body') )->unblessed; + $data->{library_id} = $data->{branchcode}; + $data->{type} = $data->{debit_type_code}; + $data->{cash_register} = $data->{register_id}; + $data->{item_id} = $data->{itemnumber}; $data->{interface} = 'api'; # Should this always be API, or should we allow the API consumer to choose? $data->{user_id} = $patron->borrowernumber; # Should this be API user OR staff the API may be acting on behalf of? my $debit = $patron->account->add_debit($data); $c->res->headers->location( $c->req->url->to_string . '/' . $debit->id ); + $debit->discard_changes; + return $c->render( status => 201, openapi => $debit->to_api --- a/api/v1/swagger/definitions/debit.yaml +++ a/api/v1/swagger/definitions/debit.yaml @@ -32,7 +32,6 @@ properties: type: - string - "null" - readOnly: true description: Account line description account_type: type: --- a/api/v1/swagger/paths/patrons_account.yaml +++ a/api/v1/swagger/paths/patrons_account.yaml @@ -190,7 +190,7 @@ description: A JSON object containing debit information required: true schema: - $ref: "../swagger.yaml#/definitions/patron_account_debit" + $ref: "../swagger.yaml#/definitions/debit" produces: - application/json responses: --- a/t/db_dependent/api/v1/patrons_accounts.t +++ a/t/db_dependent/api/v1/patrons_accounts.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use Test::Mojo; @@ -268,3 +268,74 @@ subtest 'add_credit() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'add_debit() tests' => sub { + + plan tests => 13; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object({ + class => 'Koha::Patrons', + value => { flags => 1 } + }); + my $password = 'thePassword123'; + $patron->set_password({ password => $password, skip_validation => 1 }); + my $userid = $patron->userid; + + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + my $patron_id = $patron->id; + my $account = $patron->account; + + is( $account->outstanding_debits->count, 0, 'No outstanding debits for patron' ); + is( $account->outstanding_credits->count, 0, 'No outstanding credits for patron' ); + + my $debit = { + amount => 100, + description => "A description", + debit_type => "NEW_CARD", + user_id => $patron->borrowernumber, + interface => 'test', + library_id => $library->id, + }; + + my $ret = $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $debit) + ->status_is(201)->tx->res->json; + my $account_line = Koha::Account::Lines->find( $ret->{account_line_id} ); + + is_deeply( $ret, $account_line->to_api, 'Line returned correctly' ); + + is( $account_line->branchcode, + $library->id, 'Library id is sored correctly' ); + + my $outstanding_debits = $account->outstanding_debits; + is( $outstanding_debits->count, 1 ); + is( $outstanding_debits->total_outstanding, 100 ); + + my $credit_1 = $account->add_credit( + { + amount => 10, + interface => 'test', + } + )->store()->apply({ debits => [ $account_line ] }); + my $credit_2 = $account->add_credit( + { + amount => 15, + interface => 'test' + } + )->store()->apply({ debits => [ $account_line ]}); + + is( $account->outstanding_credits->total_outstanding, 0 ); + is( $account->outstanding_debits->total_outstanding, + 75, "Credits partially cancelled debit" ); + + my $account_line_id = $ret->{account_line_id}; + + $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $debit) + ->status_is(201); + + is( $account->outstanding_debits->total_outstanding, + 175, "Debit added to total outstanding debits" ); + + $schema->storage->txn_rollback; +}; --