def post(self, mine_guid, permit_guid, permit_amendment_guid=None): if permit_amendment_guid: raise BadRequest('Unexpected permit_amendement_guid.') permit = Permit.find_by_permit_guid(permit_guid) if not permit: raise NotFound('Permit does not exist.') if not str(permit.mine_guid) == mine_guid: raise BadRequest( 'Permits mine_guid and provided mine_guid mismatch.') data = self.parser.parse_args() current_app.logger.info(f'creating permit_amendment with >> {data}') party = Party.find_by_party_guid(data.get('permittee_party_guid')) if not party: raise NotFound('Party not found') party_is_active_permittee = False permittees = MinePartyAppointment.find_by_permit_guid(permit_guid) for permittee in permittees: if permittee.end_date is None: if permittee.party_guid == party.party_guid: party_is_active_permittee = True else: # inactive old permittees permittee.end_date = datetime.utcnow() permittee.save() if not party_is_active_permittee: new_permittee = MinePartyAppointment.create(permit.mine_guid, data.get( 'permittee_party_guid'), 'PMT', datetime.utcnow(), None, self.get_user_info(), permit_guid, True) new_permittee.save() new_pa = PermitAmendment.create( permit, data.get('received_date'), data.get('issue_date'), data.get('authorization_end_date'), data.get('permit_amendment_type_code', 'AMD'), description=data.get('description')) uploadedFiles = data.get('uploadedFiles', []) for newFile in uploadedFiles: new_pa_doc = PermitAmendmentDocument( document_name=newFile['fileName'], document_manager_guid=newFile['document_manager_guid'], mine_guid=permit.mine_guid, ) new_pa.related_documents.append(new_pa_doc) new_pa.save() return new_pa
def post(self, mine_guid): data = self.parser.parse_args() mine = Mine.find_by_mine_guid(mine_guid) if not mine: raise NotFound( 'There was no mine found with the provided mine_guid.') party = Party.find_by_party_guid(data.get('permittee_party_guid')) if not party: raise NotFound('Party not found') permit = Permit.find_by_permit_no(data.get('permit_no')) if permit: raise BadRequest("That permit number is already in use.") uploadedFiles = data.get('uploadedFiles', []) permit = Permit.create(mine.mine_guid, data.get('permit_no'), data.get('permit_status_code')) amendment = PermitAmendment.create( permit, data.get('received_date'), data.get('issue_date'), data.get('authorization_end_date'), 'OGP', description='Initial permit issued.') db.session.add(permit) db.session.add(amendment) for newFile in uploadedFiles: new_pa_doc = PermitAmendmentDocument( document_name=newFile['fileName'], document_manager_guid=newFile['document_manager_guid'], mine_guid=permit.mine_guid, ) amendment.related_documents.append(new_pa_doc) db.session.commit() permittee_start_date = data.get('issue_date'), permittee = MinePartyAppointment.create( mine.mine_guid, data.get('permittee_party_guid'), 'PMT', start_date=permittee_start_date, processed_by=self.get_user_info(), permit_guid=permit.permit_guid) db.session.add(permittee) db.session.commit() return permit
def post(self, mine_guid, permit_guid, permit_amendment_guid=None): if permit_amendment_guid: raise BadRequest('Unexpected permit_amendement_guid.') permit = Permit.find_by_permit_guid(permit_guid) if not permit: raise NotFound('Permit does not exist.') if not str(permit.mine_guid) == mine_guid: raise BadRequest('Permits mine_guid and provided mine_guid mismatch.') data = self.parser.parse_args() current_app.logger.info(f'creating permit_amendment with >> {data}') party = Party.find_by_party_guid(data.get('permittee_party_guid')) if not party: raise NotFound('Party not found') permittees = MinePartyAppointment.find_by_permit_guid(permit_guid) if not permittees: raise NotFound('Party appointments not found') permit_issue_datetime = data.get('issue_date') # convert permit_issue_date to a date object to compare with permittee start_date, #Both dates are stored in the DB as Dates, and are being converted to SQLAlchemy dateTimes in the modals, but for some reason being returned as Python Dates. permit_issue_date = datetime.date(permit_issue_datetime) is_historical_permit = False new_end_dates = MinePartyAppointment.find_appointment_end_dates( permit_guid, permit_issue_date) for permittee in permittees: # check if the new appointment is older than the current appointment, if so create a new permittee appointment if permittee.start_date > permit_issue_date: is_historical_permit = True else: # if the amendment is the newest, change the end dates of the other appointments position = new_end_dates.index(permittee.start_date) if new_end_dates.index(permittee.start_date) == 0: permittee.save() else: permittee.end_date = new_end_dates[position - 1] permittee.save() permittee_start_date = permit_issue_date position = new_end_dates.index(permit_issue_date) permittee_end_date = new_end_dates[position - 1] if is_historical_permit else None # create a new appointment, so every amendment is associated with a permittee new_permittee = MinePartyAppointment.create( permit.mine_guid, data.get('permittee_party_guid'), 'PMT', self.get_user_info(), start_date=permittee_start_date, end_date=permittee_end_date, permit_guid=permit_guid) new_permittee.save() new_pa = PermitAmendment.create( permit, data.get('received_date'), data.get('issue_date'), data.get('authorization_end_date'), data.get('permit_amendment_type_code', 'AMD'), description=data.get('description')) uploadedFiles = data.get('uploadedFiles', []) for newFile in uploadedFiles: new_pa_doc = PermitAmendmentDocument( document_name=newFile['fileName'], document_manager_guid=newFile['document_manager_guid'], mine_guid=permit.mine_guid, ) new_pa.related_documents.append(new_pa_doc) new_pa.save() return new_pa