From f49839c61bd3e6512b7f853c0ebda451327267d0 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 24 Nov 2021 13:44:03 +0000 Subject: [PATCH] Bug 29566: Add API route for fetching item slip content This patch adds an API endpoint to allow the generation and fetching of an item print slip. --- Koha/REST/V1/Items.pm | 60 ++++++++++++++++++++++++++++ api/v1/swagger/definitions.json | 3 ++ api/v1/swagger/definitions/slip.yaml | 18 +++++++++ api/v1/swagger/paths.json | 3 ++ api/v1/swagger/paths/items.yaml | 40 +++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 api/v1/swagger/definitions/slip.yaml diff --git a/Koha/REST/V1/Items.pm b/Koha/REST/V1/Items.pm index 61150013e1..b1edf4b62e 100644 --- a/Koha/REST/V1/Items.pm +++ b/Koha/REST/V1/Items.pm @@ -20,6 +20,9 @@ use Modern::Perl; use Mojo::Base 'Mojolicious::Controller'; use Koha::Items; +use Koha::DateUtils qw( dt_from_string output_pref ); + +use C4::Letters qw(GetPreparedLetter); use List::MoreUtils qw( any ); use Try::Tiny qw( catch try ); @@ -273,4 +276,61 @@ sub remove_from_bundle { ); } +=head3 print_slip + +Controller function that handles constructing and returning the contents for a print slip for this item + +=cut + +sub print_slip { + my $c = shift->openapi->valid_input or return; + + my $item_id = $c->validation->param('item_id'); + my $item = Koha::Items->find( $item_id ); + + unless ($item) { + return $c->render( + status => 404, + openapi => { error => "Item not found" } + ); + } + + my $print_slip_id = $c->validation->param('slip_id'); + + #FIXME: This should come from the session + my $user = $c->stash('koha.user'); + my $branchcode = $user->branchcode; + my $lang = $user->lang; + + my $slip = C4::Letters::GetPreparedLetter( + module => 'circulation', + letter_code => $print_slip_id, + branchcode => $branchcode, + message_transport_type => 'print', + #lang => $lang, + tables => { + items => $item_id + } + ); + + unless ($slip) { + return $c->render( + status => 404, + openapi => { error => "Slip not found" } + ); + } + + return $c->render( + status => 200, + openapi => { + timestamp => output_pref( + { dt => dt_from_string(), dateformat => 'rfc3339' } + ), + slip_id => $print_slip_id, + content => $slip->{content}, + title => $slip->{title} + } + ); +} + 1; diff --git a/api/v1/swagger/definitions.json b/api/v1/swagger/definitions.json index 1f0ac48422..ab65f79a79 100644 --- a/api/v1/swagger/definitions.json +++ b/api/v1/swagger/definitions.json @@ -89,6 +89,9 @@ "return_claim": { "$ref": "definitions/return_claim.json" }, + "slip": { + "$ref": "definitions/slip.yaml" + }, "smtp_server": { "$ref": "definitions/smtp_server.json" }, diff --git a/api/v1/swagger/definitions/slip.yaml b/api/v1/swagger/definitions/slip.yaml new file mode 100644 index 0000000000..96a6341b73 --- /dev/null +++ b/api/v1/swagger/definitions/slip.yaml @@ -0,0 +1,18 @@ +--- +type: object +properties: + slip_id: + type: string + readOnly: true + description: unique identifier code for the slip template + content: + type: string + description: Raw string content of the generated slip + title: + type: string + description: Raw string title of generated slip + timestamp: + type: string + format: date-time + description: timestamp of date the slip content was generated +additionalProperties: false diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index ed5cfae2c8..d26b6c1948 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -89,6 +89,9 @@ "/items/{item_id}": { "$ref": "paths/items.yaml#/~1items~1{item_id}" }, + "/items/{item_id}/print_slips/{slip_id}": { + "$ref": "paths/items.yaml#/~1items~1{item_id}~1print_slips~1{slip_id}" + }, "/items/{item_id}/bundled_items": { "$ref": "paths/items.yaml#/~1items~1{item_id}~1bundled_items" }, diff --git a/api/v1/swagger/paths/items.yaml b/api/v1/swagger/paths/items.yaml index 72bd16b514..e0d90efba9 100644 --- a/api/v1/swagger/paths/items.yaml +++ b/api/v1/swagger/paths/items.yaml @@ -303,6 +303,46 @@ x-koha-authorization: permissions: reserveforothers: place_holds +/items/{item_id}/print_slips/{slip_id}: + get: + x-mojo-to: Items#print_slip + operationId: printSlipItems + tags: + - items + summary: Print slip for item as defined in notices and slips + parameters: + - $ref: ../parameters.json#/item_id_pp + - name: slip_id + in: path + description: The required slip letter code identifier + required: true + type: string + produces: + - application/json + responses: + "200": + description: A slip object + schema: + $ref: ../definitions.json#/slip + "401": + description: Authentication required + schema: + $ref: ../definitions.json#/error + "403": + description: Access forbidden + schema: + $ref: ../definitions.json#/error + "500": + description: Internal server error + schema: + $ref: ../definitions.json#/error + "503": + description: Under maintenance + schema: + $ref: ../definitions.json#/error + x-koha-authorization: + permissions: + catalogue: "1" /public/items/{item_id}/bundled_items: get: x-mojo-to: Items#bundled_items -- 2.20.1