Skip to content

Commit

Permalink
Exemption registrations cancel transport permits. (bcgov#1909)
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Lovett <[email protected]>
  • Loading branch information
doug-lovett authored Jun 3, 2024
1 parent b24a07d commit 322b739
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
7 changes: 5 additions & 2 deletions mhr_api/src/mhr_api/models/db2/manuhome.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,8 @@ def create_from_exemption(registration, reg_json):
now_local)
doc.update_id = current_app.config.get('DB2_RACF_ID', '')
manuhome.reg_documents.append(doc)
# Cancel active permit if one exists:
legacy_reg_utils.cancel_permit_note(manuhome, doc.id)
# Add note.
if reg_json.get('note'):
reg_json['note']['noteId'] = legacy_reg_utils.get_next_note_id(manuhome.reg_notes)
Expand Down Expand Up @@ -872,9 +874,10 @@ def create_from_admin(registration, reg_json):
now_local)
doc.update_id = current_app.config.get('DB2_RACF_ID', '')
manuhome.reg_documents.append(doc)
if new_doc.document_type in (MhrDocumentTypes.NCAN, MhrDocumentTypes.NRED,
MhrDocumentTypes.EXRE, MhrDocumentTypes.CANCEL_PERMIT):
if new_doc.document_type in (MhrDocumentTypes.NCAN, MhrDocumentTypes.NRED, MhrDocumentTypes.EXRE):
manuhome.cancel_note(manuhome, reg_json, new_doc.document_type, doc.id)
elif doc_type == MhrDocumentTypes.CANCEL_PERMIT:
legacy_reg_utils.cancel_permit_note(manuhome, doc.id)
elif doc_type in (Db2Document.DocumentTypes.AMENDMENT, Db2Document.DocumentTypes.CORRECTION) and \
reg_json.get('status'):
if reg_json.get('status') == MhrRegistrationStatusTypes.EXEMPT and \
Expand Down
41 changes: 21 additions & 20 deletions mhr_api/src/mhr_api/models/db2/registration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ def set_permit_json(registration, reg_json: dict) -> dict: # pylint: disable=to
permit_status = None
permit_doc_id: int = 0
for note in registration.manuhome.reg_notes:
if note.document_type in (Db2Document.DocumentTypes.PERMIT, Db2Document.DocumentTypes.PERMIT_TRIM):
if note.document_type in (Db2Document.DocumentTypes.PERMIT,
Db2Document.DocumentTypes.PERMIT_TRIM,
Db2Document.DocumentTypes.PERMIT_EXTENSION):
for doc in registration.manuhome.reg_documents:
if doc.id == note.reg_document_id and doc.document_type in (Db2Document.DocumentTypes.PERMIT,
Db2Document.DocumentTypes.PERMIT_TRIM):
if doc.id == note.reg_document_id:
permit_status = FROM_LEGACY_STATUS.get(note.status)
expiry_dt = note.expiry_date
permit_number = doc.document_reg_id
Expand Down Expand Up @@ -306,23 +307,30 @@ def set_caution(notes) -> bool:
return has_caution


def cancel_note(manuhome, reg_json, doc_type: str, doc_id: int): # pylint: disable=too-many-branches
def cancel_permit_note(manuhome, doc_id: int):
"""Update status, candocid for a registration that cancels a transport permit/amended permit."""
if not manuhome.reg_documents or not manuhome.reg_notes:
return
for note in manuhome.reg_notes:
if note.document_type in (Db2Document.DocumentTypes.PERMIT,
Db2Document.DocumentTypes.PERMIT_TRIM,
Db2Document.DocumentTypes.PERMIT_EXTENSION) and \
note.status == Db2Mhomnote.StatusTypes.ACTIVE:
note.can_document_id = doc_id
note.status = Db2Mhomnote.StatusTypes.CANCELLED
current_app.logger.debug(f'Cancelling permit reg doc id={note.reg_document_id} type={note.document_type}')


def cancel_note(manuhome, reg_json, doc_type: str, doc_id: int):
"""Update status, candocid for a registration that cancels a unit note."""
if not reg_json.get('cancelDocumentId') and not reg_json.get('updateDocumentId') and \
(not doc_type or doc_type != MhrDocumentTypes.CANCEL_PERMIT):
if not reg_json.get('cancelDocumentId') and not reg_json.get('updateDocumentId') and not doc_type:
return
cancel_doc_type: str = None
cancel_doc_id: str = reg_json.get('cancelDocumentId')
if not cancel_doc_id:
cancel_doc_id: str = reg_json.get('updateDocumentId')
for note in manuhome.reg_notes:
if doc_type and doc_type == MhrDocumentTypes.CANCEL_PERMIT and \
note.document_type in (Db2Document.DocumentTypes.PERMIT, Db2Document.DocumentTypes.PERMIT_TRIM) and \
note.status == Db2Mhomnote.StatusTypes.ACTIVE:
note.can_document_id = doc_id
note.status = Db2Mhomnote.StatusTypes.CANCELLED
cancel_doc_type = note.document_type
elif note.reg_document_id == cancel_doc_id:
if note.reg_document_id == cancel_doc_id:
note.can_document_id = doc_id
note.status = Db2Mhomnote.StatusTypes.CANCELLED
cancel_doc_type = note.document_type
Expand All @@ -346,13 +354,6 @@ def cancel_note(manuhome, reg_json, doc_type: str, doc_id: int): # pylint: disa
note.document_type in (MhrDocumentTypes.EXNR, MhrDocumentTypes.EXRS, MhrDocumentTypes.EXMN):
note.can_document_id = doc_id
note.status = Db2Mhomnote.StatusTypes.CANCELLED
elif doc_type == MhrDocumentTypes.CANCEL_PERMIT:
current_app.logger.debug('Cancel transport permit looking for amended registration notes to cancel.')
for note in manuhome.reg_notes:
if note.status == Db2Mhomnote.StatusTypes.ACTIVE and \
note.document_type in (Db2Document.DocumentTypes.PERMIT, Db2Document.DocumentTypes.PERMIT_TRIM):
note.can_document_id = doc_id
note.status = Db2Mhomnote.StatusTypes.CANCELLED


def get_note_doc_reg_num(reg_documents, doc_id: str) -> str:
Expand Down
10 changes: 9 additions & 1 deletion mhr_api/src/mhr_api/models/mhr_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,17 @@ def save(self):
with db.engines['db2'].connect() as conn:
self.manuhome.update_serial_keys(conn)

def save_exemption(self):
def save_exemption(self, new_reg_id: int):
"""Set the state of the original MH registration to exempt."""
self.status_type = MhrRegistrationStatusTypes.EXEMPT
if self.change_registrations: # Close out active transport permit without reverting location.
for reg in self.change_registrations:
if reg.notes and reg.notes[0].document_type in (MhrDocumentTypes.REG_103,
MhrDocumentTypes.AMEND_PERMIT) and \
reg.notes[0].status_type == MhrNoteStatusTypes.ACTIVE:
note: MhrNote = reg.notes[0]
note.status_type = MhrNoteStatusTypes.CANCELLED
note.change_registration_id = new_reg_id
db.session.commit()

def save_transfer(self, json_data, new_reg_id):
Expand Down
2 changes: 1 addition & 1 deletion mhr_api/src/mhr_api/resources/registration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def pay_and_save_exemption(req: request, # pylint: disable=too-many-arguments
registration.pay_invoice_id = int(invoice_id)
registration.pay_path = pay_ref['receipt']
registration.save()
current_reg.save_exemption()
current_reg.save_exemption(registration.id)
return registration
except Exception as db_exception: # noqa: B902; handle all db related errors.
current_app.logger.error(SAVE_ERROR_MESSAGE.format(account_id, 'registration', str(db_exception)))
Expand Down
2 changes: 1 addition & 1 deletion mhr_api/src/mhr_api/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
Development release segment: .devN
"""

__version__ = '1.8.12' # pylint: disable=invalid-name
__version__ = '1.8.13' # pylint: disable=invalid-name
2 changes: 1 addition & 1 deletion mhr_api/tests/unit/models/test_mhr_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ def test_save_exemption(session, mhr_num, user_group, account_id):
'userid',
user_group)
registration.save()
base_reg.save_exemption()
base_reg.save_exemption(registration.id)
reg_new = MhrRegistration.find_by_mhr_number(registration.mhr_number,
account_id,
False,
Expand Down

0 comments on commit 322b739

Please sign in to comment.