@@ -, +, @@ $ ktd --shell k$ qa --- Koha/Cash/Register.pm | 14 ++++++ Koha/REST/V1/Libraries.pm | 31 ++++++++++++ api/v1/swagger/definitions/cash_register.yaml | 33 +++++++++++++ api/v1/swagger/paths/libraries.yaml | 42 +++++++++++++++++ api/v1/swagger/swagger.yaml | 7 +++ t/db_dependent/api/v1/libraries.t | 47 ++++++++++++++++++- 6 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 api/v1/swagger/definitions/cash_register.yaml --- a/Koha/Cash/Register.pm +++ a/Koha/Cash/Register.pm @@ -233,6 +233,20 @@ sub add_cashup { return Koha::Cash::Register::Cashup->_new_from_dbic($rs); } +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Cash::Register object +on the API. + +=cut + +sub to_api_mapping { + return { + branch => 'library_id', + id => 'cash_register_id', + }; +} + =head2 Internal methods =cut --- a/Koha/REST/V1/Libraries.pm +++ a/Koha/REST/V1/Libraries.pm @@ -200,4 +200,35 @@ sub list_desks { }; } +=head3 list_cash_registers + +Controller function that handles retrieving the library's cash registers + +=cut + +sub list_cash_registers { + my $c = shift->openapi->valid_input or return; + + return $c->render( status => 404, openapi => { error => "Feature disabled" } ) + unless C4::Context->preference('UseCashRegisters'); + + return try { + my $library = Koha::Libraries->find( $c->param('library_id') ); + + unless ($library) { + return $c->render( + status => 404, + openapi => { error => "Library not found" } + ); + } + + return $c->render( + status => 200, + openapi => $c->objects->to_api( $library->cash_registers ) + ); + } catch { + $c->unhandled_exception($_); + }; +} + 1; --- a/api/v1/swagger/definitions/cash_register.yaml +++ a/api/v1/swagger/definitions/cash_register.yaml @@ -0,0 +1,33 @@ +--- +type: object +properties: + cash_register_id: + type: integer + description: Internal cash register identifier + name: + type: string + description: The cash register display name + library_id: + type: string + description: Internally assigned library identifier + maxLength: 10 + minLength: 1 + description: + type: string + description: A description + starting_float: + type: + - number + - "null" + description: The starting float this account register should be assigned + archived: + type: boolean + description: If this till is archived + branch_default: + type: boolean + description: If this till is the branch default +additionalProperties: false +required: + - cash_register_id + - name + - library_id --- a/api/v1/swagger/paths/libraries.yaml +++ a/api/v1/swagger/paths/libraries.yaml @@ -346,6 +346,48 @@ x-koha-authorization: permissions: catalogue: 1 +"/libraries/{library_id}/cash_registers": + get: + x-mojo-to: Libraries#list_cash_registers + operationId: listLibraryCashRegisters + tags: + - cash_registers + summary: List the library's cash registers + parameters: + - $ref: "../swagger.yaml#/parameters/match" + - $ref: "../swagger.yaml#/parameters/order_by" + - $ref: "../swagger.yaml#/parameters/page" + - $ref: "../swagger.yaml#/parameters/per_page" + - $ref: "../swagger.yaml#/parameters/q_param" + - $ref: "../swagger.yaml#/parameters/q_body" + - $ref: "../swagger.yaml#/parameters/request_id_header" + produces: + - application/json + responses: + 200: + description: A list of desks for the library + schema: + type: array + items: + $ref: "../swagger.yaml#/definitions/cash_register" + 404: + description: Resource not found + schema: + $ref: "../swagger.yaml#/definitions/error" + 500: + description: | + Internal server error. Possible `error_code` attribute values: + + * `internal_server_error` + schema: + $ref: "../swagger.yaml#/definitions/error" + 503: + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + catalogue: 1 /public/libraries: get: x-mojo-to: Libraries#list --- a/api/v1/swagger/swagger.yaml +++ a/api/v1/swagger/swagger.yaml @@ -22,6 +22,8 @@ definitions: $ref: ./definitions/booking.yaml bundle_link: $ref: ./definitions/bundle_link.yaml + cash_register: + $ref: ./definitions/cash_register.yaml cashup: $ref: ./definitions/cashup.yaml checkout: @@ -409,6 +411,8 @@ paths: $ref: ./paths/libraries.yaml#/~1libraries "/libraries/{library_id}": $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}" + "/libraries/{library_id}/cash_registers": + $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}~1cash_registers" "/libraries/{library_id}/desks": $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}~1desks" "/oauth/login/{provider_code}/{interface}": @@ -1025,6 +1029,9 @@ tags: - description: "Manage bibliographic records\n" name: biblios x-displayName: Biblios + - description: "Manage cash registers\n" + name: cash_registers + x-displayName: Cash registers - description: "Manage cash register cashups\n" name: cashups x-displayName: Cashups --- a/t/db_dependent/api/v1/libraries.t +++ a/t/db_dependent/api/v1/libraries.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use Test::Mojo; use Test::Warn; @@ -356,3 +356,48 @@ subtest 'list_desks() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'list_cash_registers() tests' => sub { + + plan tests => 11; + + $schema->storage->txn_begin; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 4 } + } + ); + my $password = 'thePassword123'; + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $patron->userid; + + t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 ); + + $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode . "/cash_registers" )->status_is(404) + ->json_is( '/error' => q{Feature disabled} ); + + my $non_existent_code = $library->branchcode; + $library->delete; + + t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 ); + + $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $non_existent_code . "/cash_registers" )->status_is(404) + ->json_is( '/error' => 'Library not found' ); + + my $cash_register_1 = + $builder->build_object( { class => 'Koha::Cash::Registers', value => { branch => $library->id } } ); + my $cash_register_2 = + $builder->build_object( { class => 'Koha::Cash::Registers', value => { branch => $library->id } } ); + + my $res = + $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode . "/cash_registers" ) + ->status_is(200)->json_is( '/0/cash_register_id' => $cash_register_1->id ) + ->json_is( '/1/cash_register_id' => $cash_register_2->id )->tx->res->json; + + is( scalar @{$res}, 2 ); + + $schema->storage->txn_rollback; +}; --