Skip to content

Commit

Permalink
circulation: check in an item with no loans
Browse files Browse the repository at this point in the history
Fixes a bug when checking in an on_shelf or in_transit item (with no
loans) that did not receive the correct item status.

* Closes rero#1334

Co-Authored-by: Aly Badr <aly.badr@rero.ch>
  • Loading branch information
Aly Badr committed Nov 6, 2020
1 parent c551ed9 commit f9a4e2d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
53 changes: 37 additions & 16 deletions rero_ils/modules/items/api/circulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def change_status_commit_and_reindex_item(self, item):
from . import ItemsSearch
current_search.flush_and_refresh(
current_circulation.loan_search_cls.Meta.index)
item.status_update(dbcommit=True, reindex=True, forceindex=True)
item.status_update(item, dbcommit=True, reindex=True, forceindex=True)
ItemsSearch.flush()

def prior_validate_actions(self, **kwargs):
Expand Down Expand Up @@ -195,15 +195,21 @@ def checkin_item_on_shelf(self, loans_list, **kwargs):
if transaction_item_libraries:
# CHECKIN_1_1_1, item library = transaction library
# item will be checked in in home library, no action
if self.status != ItemStatus.ON_SHELF:
self.status_update(self, dbcommit=True,
reindex=True, forceindex=True)
raise NoCirculationAction(
'Item returned at owning library')
raise NoCirculationAction(
'No circulation action performed')
else:
# CHECKIN_1_1_2: item library != transaction library
# item will be checked-in in an external library, no
# circulation action performed, add item status in_transit
self['status'] == ItemStatus.IN_TRANSIT
self['status'] = ItemStatus.IN_TRANSIT
self.status_update(
dbcommit=True, reindex=True, forceindex=True)
self, on_shelf=False, dbcommit=True, reindex=True,
forceindex=True)
raise NoCirculationAction('in_transit status added')

def checkin_item_at_desk(self, **kwargs):
Expand All @@ -226,12 +232,13 @@ def checkin_item_at_desk(self, **kwargs):
else:
# CHECKIN_2_2: pickup location != transaction library
# item is: in_transit
at_desk_loan['state'] == 'IN_TRANSIT_FOR_PICKUP'
at_desk_loan['state'] = LoanState.ITEM_IN_TRANSIT_FOR_PICKUP
at_desk_loan.update(
at_desk_loan, dbcommit=True, reindex=True)
self['status'] == ItemStatus.IN_TRANSIT
self['status'] = ItemStatus.IN_TRANSIT
self.status_update(
dbcommit=True, reindex=True, forceindex=True)
self, on_shelf=False, dbcommit=True, reindex=True,
forceindex=True)
raise NoCirculationAction(
'in_transit status added')

Expand Down Expand Up @@ -435,7 +442,8 @@ def cancel_item_request(self, pid, **kwargs):
elif actions_to_execute.get('loan_update', {}).get('state'):
loan['state'] = actions_to_execute['loan_update']['state']
loan.update(loan, dbcommit=True, reindex=True)
self.status_update(dbcommit=True, reindex=True, forceindex=True)
self.status_update(
self, dbcommit=True, reindex=True, forceindex=True)
actions.update({LoanAction.UPDATE: loan})
item = self
elif actions_to_execute.get('validate_first_pending'):
Expand Down Expand Up @@ -1109,17 +1117,30 @@ def actions(self):
# actions.add('lose')
return actions

def status_update(self, dbcommit=False, reindex=False, forceindex=False):
"""Update item status."""
loan = get_loan_for_item(item_pid_to_object(self.pid))
@classmethod
def status_update(
cls, item, on_shelf=True, dbcommit=False, reindex=False,
forceindex=False):
"""Update item status.
The item normally inherits its status from its active loan. In other
cases it goes back to on_shelf
:param item: the item record
:param on_shelf: A boolean to indicate that item is candidate to go
on_shelf
:param reindex: reindex record
:param dbcommit: commit record to database
"""
loan = get_loan_for_item(item_pid_to_object(item.pid))
if loan:
self['status'] = self.statuses[loan['state']]
item['status'] = cls.statuses[loan['state']]
else:
if self['status'] != ItemStatus.MISSING:
self['status'] = ItemStatus.ON_SHELF
if item['status'] != ItemStatus.MISSING and on_shelf is True:
item['status'] = ItemStatus.ON_SHELF
if dbcommit:
self.commit()
self.dbcommit(reindex=True, forceindex=True)
item.commit()
item.dbcommit(reindex=True, forceindex=True)

def item_has_active_loan_or_request(self):
"""Return True if active loan or a request found for item."""
Expand All @@ -1139,7 +1160,7 @@ def return_missing(self):
"""
# TODO: check transaction location
self['status'] = ItemStatus.ON_SHELF
self.status_update(dbcommit=True, reindex=True, forceindex=True)
self.status_update(self, dbcommit=True, reindex=True, forceindex=True)
return self, {
LoanAction.RETURN_MISSING: None
}
Expand Down
8 changes: 5 additions & 3 deletions tests/ui/circulation/test_actions_checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ def test_checkin_on_item_on_shelf_no_requests(
params['transaction_library_pid'] = lib_fully.pid
with pytest.raises(NoCirculationAction):
item, actions = item_lib_martigny.checkin(**params)
assert item.status == ItemStatus.IN_TRANSIT
params['transaction_library_pid'] = lib_fully.pid
item = Item.get_record_by_pid(item_lib_martigny.pid)
assert item.status == ItemStatus.IN_TRANSIT
params['transaction_library_pid'] = lib_martigny.pid
with pytest.raises(NoCirculationAction):
item, actions = item_lib_martigny.checkin(**params)
assert item.status == ItemStatus.IN_TRANSIT
item = Item.get_record_by_pid(item_lib_martigny.pid)
assert item.status == ItemStatus.ON_SHELF


def test_checkin_on_item_on_shelf_with_requests(
Expand Down

0 comments on commit f9a4e2d

Please sign in to comment.