28
28
map_item_circulation_status , map_media_type
29
29
from ..documents .api import Document
30
30
from ..documents .utils import title_format_text_head
31
+ from ..errors import NoCirculationAction
31
32
from ..items .api import Item
32
33
from ..items .models import ItemNoteTypes
33
34
from ..libraries .api import Library
@@ -351,9 +352,8 @@ def selfcheck_checkout(transaction_user_pid, item_barcode, patron_barcode,
351
352
item_pid = item .pid ,
352
353
selfcheck_terminal_id = str (terminal .id ),
353
354
)
354
- loan_pid = data [LoanAction .CHECKOUT ].get ('pid' )
355
- loan = Loan .get_record_by_pid (loan_pid )
356
- if loan :
355
+ if data [LoanAction .CHECKOUT ]:
356
+ loan = data [LoanAction .CHECKOUT ]
357
357
checkout ['checkout' ] = True
358
358
checkout ['due_date' ] = loan .get_loan_end_date (
359
359
time_format = None , language = language )
@@ -413,6 +413,7 @@ def selfcheck_checkin(transaction_user_pid, item_barcode, **kwargs):
413
413
transaction_user_pid = staffer .pid ,
414
414
transaction_library_pid = terminal .library_pid ,
415
415
item_pid = item .pid ,
416
+ selfcheck_terminal_id = str (terminal .id ),
416
417
)
417
418
if data [LoanAction .CHECKIN ]:
418
419
checkin ['checkin' ] = True
@@ -428,3 +429,71 @@ def selfcheck_checkin(transaction_user_pid, item_barcode, **kwargs):
428
429
_ ('Error encountered: please contact a librarian' ))
429
430
raise SelfcheckCirculationError ('self checkin failed' , checkin )
430
431
return checkin
432
+
433
+
434
+ def selfcheck_renew (transaction_user_pid , item_barcode , ** kwargs ):
435
+ """SIP2 handler to perform renew.
436
+
437
+ Perform renew action received from the selfcheck.
438
+ :param transaction_user_pid: identifier of the staff user.
439
+ :param item_barcode: item identifier.
440
+ :return: The SelfcheckRenew object.
441
+ """
442
+ if check_sip2_module ():
443
+ from invenio_sip2 .errors import SelfcheckCirculationError
444
+ from invenio_sip2 .models import SelfcheckFeeType , SelfcheckRenew
445
+
446
+ terminal = SelfcheckTerminal .find_terminal (
447
+ name = kwargs .get ('terminal' ))
448
+ item = Item .get_item_by_barcode (
449
+ barcode = item_barcode ,
450
+ organisation_pid = terminal .organisation_pid
451
+ )
452
+ document = Document .get_record_by_pid (item .document_pid )
453
+
454
+ renew = SelfcheckRenew (
455
+ title_id = title_format_text_head (document .get ('title' ))
456
+ )
457
+ with current_app .test_request_context () as ctx :
458
+ language = kwargs .get ('language' , current_app .config
459
+ .get ('BABEL_DEFAULT_LANGUAGE' ))
460
+ ctx .babel_locale = language
461
+ try :
462
+ staffer = Patron .get_record_by_pid (transaction_user_pid )
463
+ if staffer .is_librarian :
464
+ # get renewal count
465
+ renewal_count = item .get_extension_count ()
466
+ if renewal_count > 1 :
467
+ renew ['renewal' ] = True
468
+ # do extend loan
469
+ result , data = item .extend_loan (
470
+ transaction_user_pid = staffer .pid ,
471
+ transaction_library_pid = terminal .library_pid ,
472
+ item_pid = item .pid ,
473
+ selfcheck_terminal_id = str (terminal .id ),
474
+ )
475
+ if data [LoanAction .EXTEND ]:
476
+ loan = data [LoanAction .EXTEND ]
477
+ renew ['success' ] = True
478
+ renew ['due_date' ] = loan .get_loan_end_date (
479
+ time_format = None , language = language )
480
+ transaction = PatronTransaction . \
481
+ get_last_transaction_by_loan_pid (
482
+ loan_pid = loan .pid ,
483
+ status = 'open' )
484
+ if transaction :
485
+ # TODO: map transaction type
486
+ renew ['fee_type' ] = SelfcheckFeeType .OVERDUE
487
+ renew ['fee_amount' ] = transaction .total_amount
488
+ renew ['currency_type' ] = transaction .currency
489
+ # TODO: When is possible, try to return fields:
490
+ # magnetic_media, resensitize
491
+
492
+ except NoCirculationAction :
493
+ renew .get ('screen_messages' , []).append (
494
+ _ ('No circulation action is possible' ))
495
+ except Exception :
496
+ renew .get ('screen_messages' , []).append (
497
+ _ ('Error encountered: please contact a librarian' ))
498
+ raise SelfcheckCirculationError ('self renewal failed' , renew )
499
+ return renew
0 commit comments