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

(-)a/Koha/REST/V1/Checkouts.pm (+69 lines)
Lines 281-286 sub add { Link Here
281
    };
281
    };
282
}
282
}
283
283
284
=head3 checkin
285
286
Check in a checked out item via item_id or barcode
287
288
=cut
289
290
sub checkin {
291
292
    my $c          = shift->openapi->valid_input or return;
293
    my $body       = $c->req->json;
294
    my $item_id    = $body->{item_id};
295
    my $barcode    = $body->{external_id};
296
    my $library_id = $body->{library_id};
297
298
    my $exemptfine = $body->{exemptfine};
299
300
    return try {
301
302
        unless ($item_id or $barcode){
303
          return $c->render(
304
              status  => 400,
305
              openapi => { error => 'Missing or wrong parameters.' }
306
          );
307
        }
308
309
        # if item_id and barcode are set barcode gets overwritten
310
        if ( $item_id ) {
311
            my $item = Koha::Items->find($item_id);
312
            unless ($item) {
313
              return $c->render(
314
                  status  => 409,
315
                  openapi => { error => 'Item not found.' }
316
              );
317
            }
318
            $barcode = $item->barcode;
319
        }
320
321
        my ( $returned, $return_messages ) =
322
          C4::Circulation::AddReturn( $barcode, $library_id, $exemptfine );
323
324
        my $response = { returned => $returned, messages => $return_messages };
325
326
        if ($return_messages->{BadBarcode}){
327
          $response->{error} = 'Item not found.';
328
          return $c->render(
329
                  status  => 409,
330
                  openapi => $response
331
              );
332
        }
333
334
        unless ($returned) {
335
            return $c->render(
336
                status  => 403,
337
                openapi => $response 
338
            );
339
        }
340
341
        $c->res->headers->location( $c->req->url->to_string );
342
        return $c->render(
343
            status  => 201,
344
            openapi => $response
345
        );
346
347
    }
348
    catch {
349
        $c->unhandled_exception($_);
350
    };
351
}
352
284
=head3 get_renewals
353
=head3 get_renewals
285
354
286
List Koha::Checkout::Renewals
355
List Koha::Checkout::Renewals
(-)a/api/v1/swagger/definitions/checkin.yaml (+20 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  returned:
5
    type: integer
6
    description: True if return checkin succeeded
7
    readOnly: true
8
  messages:
9
    description: Messages to the returned item
10
    type: object
11
  item_id:
12
    type: integer
13
    description: Internal item identifier
14
  external_id:
15
    type: integer
16
    description: Barcode of the item
17
  exemptfine:
18
    type: boolean
19
    description: If true overdue charges for the item will be removed
20
additionalProperties: true
(-)a/api/v1/swagger/paths/checkin.yaml (+63 lines)
Line 0 Link Here
1
---
2
/checkin:
3
  post:
4
    x-mojo-to: Checkouts#checkin
5
    operationId: addCheckin
6
    tags:
7
      - checkin
8
      - patrons
9
    summary: Add a checkin
10
    parameters:
11
      - name: body
12
        in: body
13
        description: A JSON object containing information about the checkin
14
        required: true
15
        schema:
16
          $ref: "../swagger.yaml#/definitions/checkin"
17
    consumes:
18
      - application/json
19
    produces:
20
      - application/json
21
    responses:
22
      "201":
23
        description: Created checkin
24
        schema:
25
          $ref: "../swagger.yaml#/definitions/checkin"
26
      "400":
27
        description: Missing or wrong parameters
28
        schema:
29
          $ref: "../swagger.yaml#/definitions/error"
30
      "401":
31
        description: Authentication required
32
        schema:
33
          $ref: "../swagger.yaml#/definitions/error"
34
      "403":
35
        description: Cannot create checkin
36
        schema:
37
          $ref: "../swagger.yaml#/definitions/checkin"
38
      "404":
39
        description: Item not found
40
        schema:
41
          $ref: "../swagger.yaml#/definitions/error"    
42
      "409":
43
        description: Conflict in creating checkin
44
        schema:
45
          $ref: "../swagger.yaml#/definitions/error"
46
      "412":
47
        description: Precondition failed
48
        schema:
49
          $ref: "../swagger.yaml#/definitions/error"
50
      "500":
51
        description: |
52
          Internal server error. Possible `error_code` attribute values:
53
54
          * `internal_server_error`
55
        schema:
56
          $ref: "../swagger.yaml#/definitions/error"
57
      "503":
58
        description: Under maintenance
59
        schema:
60
          $ref: "../swagger.yaml#/definitions/error"
61
    x-koha-authorization:
62
      permissions:
63
        circulate: circulate_remaining_permissions
(-)a/api/v1/swagger/swagger.yaml (+4 lines)
Lines 26-31 definitions: Link Here
26
    $ref: ./definitions/cash_register.yaml
26
    $ref: ./definitions/cash_register.yaml
27
  cashup:
27
  cashup:
28
    $ref: ./definitions/cashup.yaml
28
    $ref: ./definitions/cashup.yaml
29
  checkin:
30
    $ref: ./definitions/checkin.yaml
29
  checkout:
31
  checkout:
30
    $ref: ./definitions/checkout.yaml
32
    $ref: ./definitions/checkout.yaml
31
  checkouts:
33
  checkouts:
Lines 271-276 paths: Link Here
271
    $ref: "./paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups"
273
    $ref: "./paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups"
272
  "/cashups/{cashup_id}":
274
  "/cashups/{cashup_id}":
273
    $ref: "./paths/cash_registers.yaml#/~1cashups~1{cashup_id}"
275
    $ref: "./paths/cash_registers.yaml#/~1cashups~1{cashup_id}"
276
  /checkin:
277
    $ref: ./paths/checkin.yaml#/~1checkin
274
  /checkouts:
278
  /checkouts:
275
    $ref: ./paths/checkouts.yaml#/~1checkouts
279
    $ref: ./paths/checkouts.yaml#/~1checkouts
276
  "/checkouts/{checkout_id}":
280
  "/checkouts/{checkout_id}":
(-)a/t/db_dependent/api/v1/checkin.t (-1 / +130 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 17;
21
use Test::MockModule;
22
use Test::Mojo;
23
use t::lib::Mocks;
24
use t::lib::TestBuilder;
25
26
use DateTime;
27
28
use C4::Context;
29
use C4::Circulation qw( AddIssue AddReturn CanBookBeIssued );
30
31
use Koha::Database;
32
use Koha::DateUtils qw( dt_from_string output_pref );
33
use Koha::Token;
34
35
my $schema  = Koha::Database->schema;
36
my $builder = t::lib::TestBuilder->new;
37
38
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
39
my $t = Test::Mojo->new('Koha::REST::V1');
40
41
$schema->storage->txn_begin;
42
43
my $dbh = C4::Context->dbh;
44
45
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
46
my $librarian = $builder->build_object(
47
    {
48
        class => 'Koha::Patrons',
49
        value => { flags => 2, branchcode => $branchcode  }
50
    }
51
);
52
my $password = 'thePassword123';
53
$librarian->set_password( { password => $password, skip_validation => 1 } );
54
my $userid = $librarian->userid;
55
56
my $other_branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
57
my $patron           = $builder->build_object(
58
    {
59
        class => 'Koha::Patrons',
60
        value => { flags => 0, branchcode => $other_branchcode },
61
    }
62
);
63
my $unauth_password = 'thePassword000';
64
$patron->set_password(
65
    { password => $unauth_password, skip_validattion => 1 } );
66
my $unauth_userid = $patron->userid;
67
my $patron_id     = $patron->borrowernumber;
68
69
my $item1    = $builder->build_sample_item;
70
my $item1_id = $item1->id;
71
72
my $not_issued_item    = $builder->build_sample_item;
73
my $not_issued_item_id = $not_issued_item->id;
74
my $not_issued_item_barcode = $not_issued_item->barcode;
75
76
my $barcode = '321321321321';
77
my $matching_items = Koha::Items->search({ barcode => $barcode });
78
while (my $item = $matching_items->next) {
79
  $item->delete;
80
}
81
82
my $unknown_item1_id = 578;
83
my $del_item = Koha::Items->find($unknown_item1_id);
84
if ($del_item){
85
  $del_item->delete;
86
}
87
88
my $checkout = $builder->build_object(
89
    {
90
        class => 'Koha::Checkouts',
91
        value => {
92
            itemnumber     => $item1_id,
93
            branchcode     => $branchcode,
94
            borrowernumber => $patron_id,
95
            note           => undef,
96
            notedate       => undef,
97
            noteseen       => undef
98
        }
99
    }
100
);
101
my $checkout_id = $checkout->id;
102
103
# empty checkin
104
$t->post_ok(
105
    "//$userid:$password@/api/v1/checkin" => json => { } )
106
  ->status_is(400);
107
108
# checkin on unknow item id 
109
$t->post_ok(
110
    "//$userid:$password@/api/v1/checkin" => json => { item_id => $unknown_item1_id } )
111
  ->status_is(409)->json_is( { error => "Item not found." } );
112
113
# checkin with unknow barcode
114
$t->post_ok(
115
    "//$userid:$password@/api/v1/checkin" => json => { external_id => $barcode } )
116
  ->status_is(409)->json_is( '/error' => "Item not found."  )->json_is( '/messages' => { BadBarcode => $barcode}  );
117
118
# not isued
119
$t->post_ok(
120
    "//$userid:$password@/api/v1/checkin" => json => { item_id => $not_issued_item_id, library_id => $branchcode } )
121
  ->status_is(403)->json_is( '/returned' => 0  )->json_has( '/messages' => { NotIssued => $not_issued_item_barcode } );
122
123
# checkin okay
124
$t->post_ok(
125
    "//$userid:$password@/api/v1/checkin" => json => { item_id => $item1_id, library_id => $branchcode } )
126
  ->status_is(201)->json_is( '/returned' => 1  )->json_has( '/messages' => { TransferTrigger => 'ReturnToHome', WasReturned => '1' } );
127
128
129
130
$schema->storage->txn_rollback;

Return to bug 24401