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

(-)a/Koha/REST/V1/Checkouts.pm (-69 / +54 lines)
Lines 52-64 sub list { Link Here
52
    try {
52
    try {
53
        my $checkouts_set;
53
        my $checkouts_set;
54
54
55
        if ( $checked_in ) {
55
        if ($checked_in) {
56
            $checkouts_set = Koha::Old::Checkouts->new;
56
            $checkouts_set = Koha::Old::Checkouts->new;
57
        } else {
57
        } else {
58
            $checkouts_set = Koha::Checkouts->new;
58
            $checkouts_set = Koha::Checkouts->new;
59
        }
59
        }
60
60
61
        my $checkouts = $c->objects->search( $checkouts_set );
61
        my $checkouts = $c->objects->search($checkouts_set);
62
62
63
        return $c->render(
63
        return $c->render(
64
            status  => 200,
64
            status  => 200,
Lines 79-91 sub get { Link Here
79
    my $c = shift->openapi->valid_input or return;
79
    my $c = shift->openapi->valid_input or return;
80
80
81
    my $checkout_id = $c->param('checkout_id');
81
    my $checkout_id = $c->param('checkout_id');
82
    my $checkout = Koha::Checkouts->find( $checkout_id );
82
    my $checkout    = Koha::Checkouts->find($checkout_id);
83
    $checkout = Koha::Old::Checkouts->find( $checkout_id )
83
    $checkout = Koha::Old::Checkouts->find($checkout_id)
84
        unless ($checkout);
84
        unless ($checkout);
85
85
86
    unless ($checkout) {
86
    unless ($checkout) {
87
        return $c->render(
87
        return $c->render(
88
            status => 404,
88
            status  => 404,
89
            openapi => { error => "Checkout doesn't exist" }
89
            openapi => { error => "Checkout doesn't exist" }
90
        );
90
        );
91
    }
91
    }
Lines 95-102 sub get { Link Here
95
            status  => 200,
95
            status  => 200,
96
            openapi => $checkout->to_api
96
            openapi => $checkout->to_api
97
        );
97
        );
98
    }
98
    } catch {
99
    catch {
100
        $c->unhandled_exception($_);
99
        $c->unhandled_exception($_);
101
    };
100
    };
102
}
101
}
Lines 115-129 sub _check_availability { Link Here
115
    my $ignore_reserves = 0;                   # Don't ignore reserves
114
    my $ignore_reserves = 0;                   # Don't ignore reserves
116
    my $params          = { item => $item };
115
    my $params          = { item => $item };
117
116
118
    my ( $impossible, $confirmation, $alerts, $messages ) =
117
    my ( $impossible, $confirmation, $alerts, $messages ) = C4::Circulation::CanBookBeIssued(
119
      C4::Circulation::CanBookBeIssued( $patron, undef, undef, $inprocess,
118
        $patron,          undef, undef, $inprocess,
120
        $ignore_reserves, $params );
119
        $ignore_reserves, $params
120
    );
121
121
122
    if ( $c->stash('is_public') ) {
122
    if ( $c->stash('is_public') ) {
123
123
124
        # Upgrade some confirmations to blockers
124
        # Upgrade some confirmations to blockers
125
        my @should_block =
125
        my @should_block =
126
          qw/TOO_MANY ISSUED_TO_ANOTHER RESERVED RESERVED_WAITING TRANSFERRED PROCESSING AGE_RESTRICTION/;
126
            qw/TOO_MANY ISSUED_TO_ANOTHER RESERVED RESERVED_WAITING TRANSFERRED PROCESSING AGE_RESTRICTION/;
127
        for my $block (@should_block) {
127
        for my $block (@should_block) {
128
            if ( exists( $confirmation->{$block} ) ) {
128
            if ( exists( $confirmation->{$block} ) ) {
129
                $impossible->{$block} = $confirmation->{$block};
129
                $impossible->{$block} = $confirmation->{$block};
Lines 133-139 sub _check_availability { Link Here
133
133
134
        # Remove any non-public info that's returned by CanBookBeIssued
134
        # Remove any non-public info that's returned by CanBookBeIssued
135
        my @restricted_keys =
135
        my @restricted_keys =
136
          qw/issued_borrowernumber issued_cardnumber issued_firstname issued_surname resborrowernumber resbranchcode rescardnumber reserve_id resfirstname resreservedate ressurname item_notforloan/;
136
            qw/issued_borrowernumber issued_cardnumber issued_firstname issued_surname resborrowernumber resbranchcode rescardnumber reserve_id resfirstname resreservedate ressurname item_notforloan/;
137
        for my $key (@restricted_keys) {
137
        for my $key (@restricted_keys) {
138
            delete $confirmation->{$key};
138
            delete $confirmation->{$key};
139
            delete $impossible->{$key};
139
            delete $impossible->{$key};
Lines 152-170 Controller function that handles retrieval of Checkout availability Link Here
152
=cut
152
=cut
153
153
154
sub get_availability {
154
sub get_availability {
155
    my $c = shift->openapi->valid_input or return;
155
    my $c    = shift->openapi->valid_input or return;
156
    my $user = $c->stash('koha.user');
156
    my $user = $c->stash('koha.user');
157
157
158
    my $patron = Koha::Patrons->find( $c->param('patron_id') );
158
    my $patron = Koha::Patrons->find( $c->param('patron_id') );
159
    my $item   = Koha::Items->find( $c->param('item_id') );
159
    my $item   = Koha::Items->find( $c->param('item_id') );
160
160
161
    my ( $impossible, $confirmation, $warnings ) =
161
    my ( $impossible, $confirmation, $warnings ) = $c->_check_availability( $patron, $item );
162
      $c->_check_availability( $patron, $item );
163
162
164
    my @confirm_keys = sort keys %{$confirmation};
163
    my @confirm_keys = sort keys %{$confirmation};
165
    unshift @confirm_keys, $item->id;
164
    unshift @confirm_keys, $item->id;
166
    unshift @confirm_keys, $user->id
165
    unshift @confirm_keys, $user->id
167
      if $user;
166
        if $user;
168
167
169
    my $token = Koha::Token->new->generate_jwt( { id => join( ':', @confirm_keys ) } );
168
    my $token = Koha::Token->new->generate_jwt( { id => join( ':', @confirm_keys ) } );
170
169
Lines 185-191 Add a new checkout Link Here
185
=cut
184
=cut
186
185
187
sub add {
186
sub add {
188
    my $c = shift->openapi->valid_input or return;
187
    my $c    = shift->openapi->valid_input or return;
189
    my $user = $c->stash('koha.user');
188
    my $user = $c->stash('koha.user');
190
189
191
    my $body      = $c->req->json;
190
    my $body      = $c->req->json;
Lines 228-235 sub add { Link Here
228
            );
227
            );
229
        }
228
        }
230
229
231
        my ( $impossible, $confirmation, $warnings ) =
230
        my ( $impossible, $confirmation, $warnings ) = $c->_check_availability( $patron, $item );
232
          $c->_check_availability( $patron, $item );
233
231
234
        # * Fail for blockers - render 403
232
        # * Fail for blockers - render 403
235
        if ( keys %{$impossible} ) {
233
        if ( keys %{$impossible} ) {
Lines 250-257 sub add { Link Here
250
            if ( my $token = $c->param('confirmation') ) {
248
            if ( my $token = $c->param('confirmation') ) {
251
                my $confirm_keys = join( ":", sort keys %{$confirmation} );
249
                my $confirm_keys = join( ":", sort keys %{$confirmation} );
252
                $confirm_keys = $user->id . ":" . $item->id . ":" . $confirm_keys;
250
                $confirm_keys = $user->id . ":" . $item->id . ":" . $confirm_keys;
253
                $confirmed = Koha::Token->new->check_jwt(
251
                $confirmed    = Koha::Token->new->check_jwt( { id => $confirm_keys, token => $token } );
254
                    { id => $confirm_keys, token => $token } );
255
            }
252
            }
256
253
257
            unless ($confirmed) {
254
            unless ($confirmed) {
Lines 265-285 sub add { Link Here
265
        # Call 'AddIssue'
262
        # Call 'AddIssue'
266
        my $checkout = AddIssue( $patron->unblessed, $item->barcode );
263
        my $checkout = AddIssue( $patron->unblessed, $item->barcode );
267
        if ($checkout) {
264
        if ($checkout) {
268
            $c->res->headers->location(
265
            $c->res->headers->location( $c->req->url->to_string . '/' . $checkout->id );
269
                $c->req->url->to_string . '/' . $checkout->id );
270
            return $c->render(
266
            return $c->render(
271
                status  => 201,
267
                status  => 201,
272
                openapi => $checkout->to_api
268
                openapi => $checkout->to_api
273
            );
269
            );
274
        }
270
        } else {
275
        else {
276
            return $c->render(
271
            return $c->render(
277
                status  => 500,
272
                status  => 500,
278
                openapi => { error => 'Unknown error during checkout' }
273
                openapi => { error => 'Unknown error during checkout' }
279
            );
274
            );
280
        }
275
        }
281
    }
276
    } catch {
282
    catch {
283
        $c->unhandled_exception($_);
277
        $c->unhandled_exception($_);
284
    };
278
    };
285
}
279
}
Lines 295-333 sub delete { Link Here
295
289
296
    my $c = shift->openapi->valid_input or return;
290
    my $c = shift->openapi->valid_input or return;
297
291
298
    my $body      = $c->validation->param('body');
292
    my $body    = $c->validation->param('body');
299
    my $item_id   = $body->{item_id};
293
    my $item_id = $body->{item_id};
300
294
301
    return try {
295
    return try {
302
        my $item = Koha::Items->find($item_id);
296
        my $item = Koha::Items->find($item_id);
303
        unless ($item) {
297
        unless ($item) {
304
            return $c->render(
298
            return $c->render(
305
                status  => 409,
299
                status  => 409,
306
                openapi => {
300
                openapi => { error => 'Item not found' }
307
                    error      => 'Item not found'
308
                }
309
            );
301
            );
310
        }
302
        }
311
        my $checkout = Koha::Checkouts->find({ itemnumber => $item_id });
303
        my $checkout = Koha::Checkouts->find( { itemnumber => $item_id } );
312
        unless ( $checkout ) {
304
        unless ($checkout) {
313
            return $c->render(
305
            return $c->render(
314
                status => 400,
306
                status  => 400,
315
                openapi => { error => "Item not checked out" }
307
                openapi => { error => "Item not checked out" }
316
            );
308
            );
317
        }
309
        }
318
        my $library_id = $c->validation->output->{library_id};
310
        my $library_id = $c->validation->output->{library_id};
319
        unless ( $library_id ) {
311
        unless ($library_id) {
320
           $library_id = $checkout->patron->branchcode;
312
            $library_id = $checkout->patron->branchcode;
321
        }
313
        }
322
        my $library = Koha::Libraries->find( $library_id );
314
        my $library = Koha::Libraries->find($library_id);
323
315
324
        try {
316
        try {
325
            my $barcode = $item->barcode;
317
            my $barcode = $item->barcode;
326
            my ( $returned ) = C4::Circulation::AddReturn( $barcode, $library_id );
318
            my ($returned) = C4::Circulation::AddReturn( $barcode, $library_id );
327
319
328
            unless ( $returned ) {
320
            unless ($returned) {
329
                return $c->render(
321
                return $c->render(
330
                    status => 403,
322
                    status  => 403,
331
                    openapi => { error => "Checkin not allowed" }
323
                    openapi => { error => "Checkin not allowed" }
332
                );
324
                );
333
            }
325
            }
Lines 337-349 sub delete { Link Here
337
                status  => 201,
329
                status  => 201,
338
                openapi => $item->to_api
330
                openapi => $item->to_api
339
            );
331
            );
340
        }
332
        } catch {
341
        catch {
342
            $c->unhandled_exception($_);
333
            $c->unhandled_exception($_);
343
        };
334
        };
344
    }
335
    }
345
 }
336
}
346
347
337
348
=head3 get_renewals
338
=head3 get_renewals
349
339
Lines 358-364 sub get_renewals { Link Here
358
        my $checkout_id = $c->param('checkout_id');
348
        my $checkout_id = $c->param('checkout_id');
359
        my $checkout    = Koha::Checkouts->find($checkout_id);
349
        my $checkout    = Koha::Checkouts->find($checkout_id);
360
        $checkout = Koha::Old::Checkouts->find($checkout_id)
350
        $checkout = Koha::Old::Checkouts->find($checkout_id)
361
          unless ($checkout);
351
            unless ($checkout);
362
352
363
        unless ($checkout) {
353
        unless ($checkout) {
364
            return $c->render(
354
            return $c->render(
Lines 368-386 sub get_renewals { Link Here
368
        }
358
        }
369
359
370
        my $renewals_rs = $checkout->renewals;
360
        my $renewals_rs = $checkout->renewals;
371
        my $renewals = $c->objects->search( $renewals_rs );
361
        my $renewals    = $c->objects->search($renewals_rs);
372
362
373
        return $c->render(
363
        return $c->render(
374
            status  => 200,
364
            status  => 200,
375
            openapi => $renewals
365
            openapi => $renewals
376
        );
366
        );
377
    }
367
    } catch {
378
    catch {
379
        $c->unhandled_exception($_);
368
        $c->unhandled_exception($_);
380
    };
369
    };
381
}
370
}
382
371
383
384
=head3 renew
372
=head3 renew
385
373
386
Renew a checkout
374
Renew a checkout
Lines 391-412 sub renew { Link Here
391
    my $c = shift->openapi->valid_input or return;
379
    my $c = shift->openapi->valid_input or return;
392
380
393
    my $checkout_id = $c->param('checkout_id');
381
    my $checkout_id = $c->param('checkout_id');
394
    my $seen = $c->param('seen') || 1;
382
    my $seen        = $c->param('seen') || 1;
395
    my $checkout = Koha::Checkouts->find( $checkout_id );
383
    my $checkout    = Koha::Checkouts->find($checkout_id);
396
384
397
    unless ($checkout) {
385
    unless ($checkout) {
398
        return $c->render(
386
        return $c->render(
399
            status => 404,
387
            status  => 404,
400
            openapi => { error => "Checkout doesn't exist" }
388
            openapi => { error => "Checkout doesn't exist" }
401
        );
389
        );
402
    }
390
    }
403
391
404
    return try {
392
    return try {
405
        my ($can_renew, $error) = CanBookBeRenewed($checkout->patron, $checkout);
393
        my ( $can_renew, $error ) = CanBookBeRenewed( $checkout->patron, $checkout );
406
394
407
        if (!$can_renew) {
395
        if ( !$can_renew ) {
408
            return $c->render(
396
            return $c->render(
409
                status => 403,
397
                status  => 403,
410
                openapi => { error => "Renewal not authorized ($error)" }
398
                openapi => { error => "Renewal not authorized ($error)" }
411
            );
399
            );
412
        }
400
        }
Lines 426-433 sub renew { Link Here
426
            status  => 201,
414
            status  => 201,
427
            openapi => $checkout->to_api
415
            openapi => $checkout->to_api
428
        );
416
        );
429
    }
417
    } catch {
430
    catch {
431
        $c->unhandled_exception($_);
418
        $c->unhandled_exception($_);
432
    };
419
    };
433
}
420
}
Lines 442-458 sub allows_renewal { Link Here
442
    my $c = shift->openapi->valid_input or return;
429
    my $c = shift->openapi->valid_input or return;
443
430
444
    my $checkout_id = $c->param('checkout_id');
431
    my $checkout_id = $c->param('checkout_id');
445
    my $checkout = Koha::Checkouts->find( $checkout_id );
432
    my $checkout    = Koha::Checkouts->find($checkout_id);
446
433
447
    unless ($checkout) {
434
    unless ($checkout) {
448
        return $c->render(
435
        return $c->render(
449
            status => 404,
436
            status  => 404,
450
            openapi => { error => "Checkout doesn't exist" }
437
            openapi => { error => "Checkout doesn't exist" }
451
        );
438
        );
452
    }
439
    }
453
440
454
    return try {
441
    return try {
455
        my ($can_renew, $error) = CanBookBeRenewed($checkout->patron, $checkout);
442
        my ( $can_renew, $error ) = CanBookBeRenewed( $checkout->patron, $checkout );
456
443
457
        my $renewable = Mojo::JSON->false;
444
        my $renewable = Mojo::JSON->false;
458
        $renewable = Mojo::JSON->true if $can_renew;
445
        $renewable = Mojo::JSON->true if $can_renew;
Lines 466-482 sub allows_renewal { Link Here
466
            }
453
            }
467
        );
454
        );
468
        return $c->render(
455
        return $c->render(
469
            status => 200,
456
            status  => 200,
470
            openapi => {
457
            openapi => {
471
                allows_renewal => $renewable,
458
                allows_renewal   => $renewable,
472
                max_renewals => $rule->rule_value,
459
                max_renewals     => $rule->rule_value,
473
                current_renewals => $checkout->renewals_count,
460
                current_renewals => $checkout->renewals_count,
474
                unseen_renewals => $checkout->unseen_renewals,
461
                unseen_renewals  => $checkout->unseen_renewals,
475
                error => $error
462
                error            => $error
476
            }
463
            }
477
        );
464
        );
478
    }
465
    } catch {
479
    catch {
480
        $c->unhandled_exception($_);
466
        $c->unhandled_exception($_);
481
    };
467
    };
482
}
468
}
483
- 

Return to bug 24401