From f3314a2f0e7f0b895c3305708fa87afb22242222 Mon Sep 17 00:00:00 2001 From: Aly Badr Date: Tue, 28 Apr 2020 17:36:00 +0200 Subject: [PATCH] loans: fix automatic item assignment on pending loans Fixes problem when the checked-in item will be assigned to all pending loans of its document. Fixed by adding the parameter assign_item to all the ITEM_RETURNED transitions. This PR needs any invenio-circulation v.a21 * Fixes inveniosoftware/invenio-circulation#127. * Removes the temporary transitions file used as a work around earlier. Co-Authored-by: Aly Badr --- rero_ils/config.py | 24 ++++-- rero_ils/modules/loans/transitions.py | 119 -------------------------- 2 files changed, 16 insertions(+), 127 deletions(-) delete mode 100644 rero_ils/modules/loans/transitions.py diff --git a/rero_ils/config.py b/rero_ils/config.py index f9f25416b5..203e21d5bc 100644 --- a/rero_ils/config.py +++ b/rero_ils/config.py @@ -35,8 +35,9 @@ CIRCULATION_LOAN_MINTER, CIRCULATION_LOAN_PID_TYPE from invenio_circulation.search.api import LoansSearch from invenio_circulation.transitions.transitions import CreatedToPending, \ - ItemAtDeskToItemOnLoan, ItemOnLoanToItemInTransitHouse, \ - ItemOnLoanToItemOnLoan, PendingToItemAtDesk, \ + ItemAtDeskToItemOnLoan, ItemInTransitHouseToItemReturned, \ + ItemOnLoanToItemInTransitHouse, ItemOnLoanToItemOnLoan, \ + ItemOnLoanToItemReturned, PendingToItemAtDesk, \ PendingToItemInTransitPickup, ToCancelled, ToItemOnLoan from invenio_records_rest.facets import terms_filter from invenio_records_rest.utils import allow_all, deny_all @@ -63,8 +64,6 @@ from .modules.loans.api import Loan from .modules.loans.permissions import can_list_loan_factory, \ can_read_loan_factory -from .modules.loans.transitions import ItemInTransitHouseToItemReturned, \ - ItemOnLoanToItemReturned from .modules.loans.utils import can_be_requested, get_default_loan_duration, \ get_extension_params, is_item_available_for_checkout, \ loan_build_document_ref, loan_build_item_ref, loan_build_patron_ref, \ @@ -1656,6 +1655,7 @@ def _(x): dest='ITEM_ON_LOAN', trigger='checkout', transition=ToItemOnLoan, + assign_item=False ), ], 'PENDING': [ @@ -1683,8 +1683,12 @@ def _(x): dict(dest='CANCELLED', trigger='cancel', transition=ToCancelled) ], 'ITEM_ON_LOAN': [ - dict(dest='ITEM_RETURNED', - transition=ItemOnLoanToItemReturned, trigger='checkin'), + dict( + dest='ITEM_RETURNED', + transition=ItemOnLoanToItemReturned, + trigger='checkin', + assign_item=False + ), dict(dest='ITEM_IN_TRANSIT_TO_HOUSE', transition=ItemOnLoanToItemInTransitHouse, trigger='checkin'), dict(dest='ITEM_ON_LOAN', transition=ItemOnLoanToItemOnLoan, @@ -1692,8 +1696,12 @@ def _(x): dict(dest='CANCELLED', trigger='cancel', transition=ToCancelled) ], 'ITEM_IN_TRANSIT_TO_HOUSE': [ - dict(dest='ITEM_RETURNED', - transition=ItemInTransitHouseToItemReturned, trigger='receive'), + dict( + dest='ITEM_RETURNED', + transition=ItemInTransitHouseToItemReturned, + trigger='receive', + assign_item=False + ), dict(dest='CANCELLED', trigger='cancel', transition=ToCancelled) ], 'ITEM_RETURNED': [], diff --git a/rero_ils/modules/loans/transitions.py b/rero_ils/modules/loans/transitions.py deleted file mode 100644 index 55a2c9bafa..0000000000 --- a/rero_ils/modules/loans/transitions.py +++ /dev/null @@ -1,119 +0,0 @@ -# -*- coding: utf-8 -*- -# -# RERO ILS -# Copyright (C) 2019 RERO -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, version 3 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -"""REROILS Circulation custom transitions.""" - - -from invenio_circulation.api import get_document_pid_by_item_pid, \ - get_pending_loans_by_doc_pid -from invenio_circulation.proxies import current_circulation -from invenio_circulation.transitions.base import Transition -from invenio_circulation.transitions.transitions import \ - _ensure_same_location as _ensure_same_location -from invenio_circulation.transitions.transitions import ensure_same_item -from invenio_db import db - -from ..documents.api import Document - - -def _update_document_pending_request_for_item(item_pid, **kwargs): - """Update pending loans on a Document with no Item attached yet. - - :param item_pid: a dict containing `value` and `type` fields to - uniquely identify the item. - """ - document_pid = get_document_pid_by_item_pid(item_pid) - document = Document.get_record_by_pid(document_pid) - if document.get_number_of_items() == 1: - for pending_loan in get_pending_loans_by_doc_pid(document_pid): - pending_loan['item_pid'] = item_pid - pending_loan.commit() - db.session.commit() - current_circulation.loan_indexer().index(pending_loan) - - -class ItemInTransitHouseToItemReturned(Transition): - """Check-in action when returning an item to its belonging location.""" - - def __init__( - self, src, dest, trigger="next", permission_factory=None, **kwargs - ): - """Constructor.""" - super().__init__( - src, - dest, - trigger=trigger, - permission_factory=permission_factory, - **kwargs - ) - self.assign_item = kwargs.get("assign_item", True) - - @ensure_same_item - def before(self, loan, **kwargs): - """Validate check-in action.""" - super().before(loan, **kwargs) - - _ensure_same_location( - loan['item_pid'], - loan['transaction_location_pid'], - self.dest, - error_msg="Item should be in transit to house.", - ) - - def after(self, loan): - """Check for pending requests on this item after check-in.""" - super().after(loan) - if self.assign_item: - _update_document_pending_request_for_item(loan['item_pid']) - - -class ItemOnLoanToItemReturned(Transition): - """Check-in action when returning an item to its belonging location.""" - - def __init__( - self, src, dest, trigger="next", permission_factory=None, **kwargs - ): - """Constructor.""" - super().__init__( - src, - dest, - trigger=trigger, - permission_factory=permission_factory, - **kwargs - ) - self.assign_item = kwargs.get("assign_item", True) - - @ensure_same_item - def before(self, loan, **kwargs): - """Validate check-in action.""" - super().before(loan, **kwargs) - - _ensure_same_location( - loan['item_pid'], - loan['transaction_location_pid'], - self.dest, - error_msg="Item should be in transit to house.", - ) - - # set end loan date as transaction date when completing loan - loan['end_date'] = loan['transaction_date'] - - def after(self, loan): - """Check for pending requests on this item after check-in.""" - super().after(loan) - if self.assign_item: - _update_document_pending_request_for_item(loan['item_pid'])