Skip to content

Commit

Permalink
Merge pull request #363 from shibaken/main
Browse files Browse the repository at this point in the history
shibaken/main to dbca-wa/main
  • Loading branch information
jawaidm authored Nov 2, 2023
2 parents f92b9d8 + ef2030a commit 36b17d3
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 156 deletions.
2 changes: 1 addition & 1 deletion mooringlicensing/components/approvals/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ def create_mooring_licence_application(self, request, *args, **kwargs):

# Copy applicant details to the new proposal
proposal_applicant = ProposalApplicant.objects.get(proposal=waiting_list_allocation.current_proposal)
proposal_applicant.copy_self_to_proposal(new_proposal)
# proposal_applicant.copy_self_to_proposal(new_proposal)
logger.info(f'ProposalApplicant: [{proposal_applicant}] has been copied from the proposal: [{waiting_list_allocation.current_proposal}] to the mooring site licence application: [{new_proposal}].')

# Copy vessel details to the new proposal
Expand Down
86 changes: 24 additions & 62 deletions mooringlicensing/components/approvals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ def save(self, *args, **kwargs):
super(Approval, self).save(*args, **kwargs)

def _create_new_sticker_by_proposal(self, proposal, sticker_to_be_replaced=None):
sticker = Sticker.objects.create(
new_sticker = Sticker.objects.create(
approval=self,
# vessel_ownership=proposal.vessel_ownership,
# fee_constructor=proposal.fee_constructor,
Expand All @@ -1233,9 +1233,8 @@ def _create_new_sticker_by_proposal(self, proposal, sticker_to_be_replaced=None)
fee_season=self.latest_applied_season,
)

logger.info(f'Sticker: {sticker} has been created (proposal_initiated: {proposal}).')

return sticker
logger.info(f'New Sticker: [{new_sticker}] has been created for the proposal: [{proposal}].')
return new_sticker

def _get_current_stickers(self):
current_stickers = self.stickers.filter(
Expand Down Expand Up @@ -1301,6 +1300,7 @@ def manage_stickers(self, proposal):
fee_season=self.latest_applied_season,
status=new_sticker_status,
)
logger.info(f'New Sticker: [{new_sticker}] has been created for the proposal: [{proposal}].')

# Set statuses to both new and existing stickers
if existing_sticker_to_be_expired:
Expand All @@ -1315,58 +1315,6 @@ def manage_stickers(self, proposal):

return new_sticker, existing_sticker_to_be_returned


# if proposal.approval and proposal.approval.reissued:
# # Can only change the conditions, so goes to Approved
# proposal.processing_status = Proposal.PROCESSING_STATUS_APPROVED
# proposal.save()
# return [], []
#
# if proposal.proposal_type.code == PROPOSAL_TYPE_NEW:
# # New sticker created with status Ready
# new_sticker = self._create_new_sticker_by_proposal(proposal)
#
# # Application goes to status Printing Sticker
# proposal.processing_status = Proposal.PROCESSING_STATUS_PRINTING_STICKER
# proposal.save()
#
# return [], []
# elif proposal.proposal_type.code == PROPOSAL_TYPE_AMENDMENT:
# if proposal.vessel_ownership == proposal.previous_application.vessel_ownership:
# # Same vessel
# next_colour = Sticker.get_vessel_size_colour_by_length(proposal.vessel_length)
# current_colour = Sticker.get_vessel_size_colour_by_length(proposal.previous_application.vessel_length)
# if current_colour != next_colour:
# logger.info(f'Current sticker colour is {current_colour} ({proposal.previous_application.vessel_length} [m] vessel). But {next_colour} colour sticker is required for the vessel amended ({proposal.vessel_length} [m] vessel).')
# return self._calc_stickers(proposal)
# else:
# return [], []
# else:
# # Different vessel
# return self._calc_stickers(proposal)
# elif proposal.proposal_type.code == PROPOSAL_TYPE_RENEWAL:
# if not proposal.approval.reissued or not proposal.keep_existing_vessel:
# # New sticker goes to ready
# current_stickers = self._get_current_stickers()
# sticker_to_be_replaced = current_stickers.first() # There should be 0 or 1 current sticker (0: when vessel sold before renewal)
# new_sticker = self._create_new_sticker_by_proposal(proposal, sticker_to_be_replaced)
# logger.info(f'New Sticker: [{new_sticker}] has been created.')
#
# # Old sticker goes to status Expired when new sticker is printed
# new_sticker.sticker_to_replace = sticker_to_be_replaced
# new_sticker.save()
#
# logger.info(f'Sticker: [{sticker_to_be_replaced}] is replaced by the sticker: {new_sticker}.')
#
# # Application goes to status Printing Sticker
# proposal.processing_status = Proposal.PROCESSING_STATUS_PRINTING_STICKER
# proposal.save()
# logger.info(f'Status: {Proposal.PROCESSING_STATUS_PRINTING_STICKER} has been set to the proposal {proposal}.')
#
# return [], []
# else:
# return [], []

def _calc_stickers(self, proposal):
# New sticker created with status Ready
new_sticker = self._create_new_sticker_by_proposal(proposal)
Expand Down Expand Up @@ -1752,7 +1700,7 @@ def _assign_to_new_stickers(self, moas_to_be_on_new_sticker, proposal, stickers_
fee_season=self.latest_applied_season,
status=new_status
)
logger.info(f'New sticker: [{new_sticker}] has been created.')
logger.info(f'New Sticker: [{new_sticker}] has been created for the proposal: [{proposal}].')
new_stickers.append(new_sticker)
if moa_to_be_on_new_sticker.sticker in stickers_to_be_replaced_for_renewal:
# Store old sticker in the new sticker in order to set 'expired' status to it once the new sticker gets 'awaiting_printing' status
Expand Down Expand Up @@ -1933,14 +1881,15 @@ def update_auth_user_permits(self):
moa.approval.child_obj.update_moorings(self)

def _create_new_sticker_by_proposal(self, proposal):
sticker = Sticker.objects.create(
new_sticker = Sticker.objects.create(
approval=self,
vessel_ownership=proposal.vessel_ownership,
fee_constructor=proposal.fee_constructor,
proposal_initiated=proposal,
fee_season=self.latest_applied_season,
)
return sticker
logger.info(f'New Sticker: [{new_sticker}] has been created for the proposal: [{proposal}].')
return new_sticker

def manage_stickers(self, proposal):
logger.info(f'Managing stickers for the MooringSiteLicence: [{self}]...')
Expand Down Expand Up @@ -2076,6 +2025,7 @@ def manage_stickers(self, proposal):
fee_season=self.latest_applied_season,
)
stickers_to_be_kept.append(new_sticker)
logger.info(f'New Sticker: [{new_sticker}] has been created for the proposal: [{proposal}].')

if existing_sticker:
new_sticker.sticker_to_replace = existing_sticker
Expand All @@ -2087,12 +2037,24 @@ def manage_stickers(self, proposal):

return [], [] # Is this correct?

def current_vessel_attributes(self, attribute=None, proposal=None):
attribute_list = []
def get_current_vessel_ownership_on_approvals(self):
vooas = self.vesselownershiponapproval_set.filter(
Q(end_date__isnull=True) &
Q(vessel_ownership__end_date__isnull=True)
)
return vooas

def get_current_min_vessel_applicable_length(self):
min_length = 0
vessel_details = self.current_vessel_attributes('vessel_details')
for vessel_detail in vessel_details:
if not min_length or vessel_detail.vessel_applicable_length < min_length:
min_length = vessel_detail.vessel_applicable_length
return min_length

def current_vessel_attributes(self, attribute=None, proposal=None):
attribute_list = []
vooas = self.get_current_vessel_ownership_on_approvals()
for vooa in vooas:
if not attribute:
attribute_list.append(vooa.vessel_ownership)
Expand Down Expand Up @@ -2931,7 +2893,7 @@ def request_replacement(self, new_status):
fee_constructor=self.fee_constructor,
fee_season=self.approval.latest_applied_season,
)
logger.info(f'New sticker: [{new_sticker}] has been created.')
logger.info(f'New Sticker: [{new_sticker}] has been created for the proposal: [{proposal}].')

return new_sticker

Expand Down
45 changes: 36 additions & 9 deletions mooringlicensing/components/proposals/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from mooringlicensing.components.main.models import GlobalSettings
from mooringlicensing.components.organisations.models import Organisation
from mooringlicensing.components.proposals.utils import (
save_proponent_data, make_proposal_applicant_ready, make_ownership_ready,
save_proponent_data, create_proposal_applicant_if_not_exist, make_ownership_ready,
)
from mooringlicensing.components.proposals.models import VesselOwnershipCompanyOwnership, searchKeyWords, search_reference, ProposalUserAction, \
ProposalType, ProposalApplicant, VesselRegistrationDocument
Expand Down Expand Up @@ -277,12 +277,39 @@ class GetVesselRegoNos(views.APIView):
renderer_classes = [JSONRenderer, ]
def get(self, request, format=None):
search_term = request.GET.get('term', '')
if search_term:
allow_add_new_vessel = request.GET.get('allow_add_new_vessel', 'true')
allow_add_new_vessel = True if allow_add_new_vessel.lower() == 'true' else False
proposal_id = request.GET.get('proposal_id', 0)
proposal = Proposal.objects.get(id=proposal_id)

if not search_term or not proposal:
return Response()

if proposal.proposal_type.code == PROPOSAL_TYPE_NEW:
# Make sure 'allow_add_new_vessel' is True when new application
allow_add_new_vessel = True
data = Vessel.objects.filter(rego_no__icontains=search_term).values('id', 'rego_no')[:10]
data_transform = [{'id': rego['id'], 'text': rego['rego_no']} for rego in data]
return Response({"results": data_transform})
return Response()
else:
# Amendment/Renewal
if proposal.approval:
vooas = proposal.approval.child_obj.get_current_vessel_ownership_on_approvals()
existing_vessel_ids = [] # Store id of the vessel that belongs to the proposal.approval
for vessel_ownership_on_approval in vooas:
existing_vessel_ids.append(vessel_ownership_on_approval.vessel_ownership.vessel.id)

if allow_add_new_vessel:
# Customer wants to add a new vessel
# Return all the vessels except existing vessels that belongs to the proposal.approval
data = Vessel.objects.filter(rego_no__icontains=search_term).exclude(id__in=existing_vessel_ids).values('id', 'rego_no')[:10]
else:
# Customer wants to edit an existing vessel which belongs to the proposal.approval
# Return only existing vessels that belong to the proposal.approval
data = Vessel.objects.filter(rego_no__icontains=search_term, id__in=existing_vessel_ids).values('id', 'rego_no')[:10]
else:
data = []

data_transform = [{'id': rego['id'], 'text': rego['rego_no']} for rego in data]
return Response({"results": data_transform})

class GetCompanyNames(views.APIView):
renderer_classes = [JSONRenderer, ]
Expand Down Expand Up @@ -585,7 +612,7 @@ def create(self, request, *args, **kwargs):
)
logger.info(f'Annual Admission Application: [{obj}] has been created by the user: [{request.user}].')

make_proposal_applicant_ready(obj, request)
# make_proposal_applicant_ready(obj, request)

serialized_obj = ProposalSerializer(obj.proposal)
return Response(serialized_obj.data)
Expand Down Expand Up @@ -616,7 +643,7 @@ def create(self, request, *args, **kwargs):
)
logger.info(f'Authorised User Application: [{obj}] has been created by the user: [{request.user}].')

make_proposal_applicant_ready(obj, request)
# make_proposal_applicant_ready(obj, request)

serialized_obj = ProposalSerializer(obj.proposal)
return Response(serialized_obj.data)
Expand Down Expand Up @@ -652,7 +679,7 @@ def create(self, request, *args, **kwargs):
)
logger.info(f'Mooring Licence Application: [{obj}] has been created by the user: [{request.user}].')

make_proposal_applicant_ready(obj, request)
# make_proposal_applicant_ready(obj, request)

serialized_obj = ProposalSerializer(obj.proposal)
return Response(serialized_obj.data)
Expand Down Expand Up @@ -685,7 +712,7 @@ def create(self, request, *args, **kwargs):

logger.info(f'Waiting List Application: [{obj}] has been created by the user: [{request.user}].')

make_proposal_applicant_ready(obj, request)
# make_proposal_applicant_ready(obj, request)

# make_ownership_ready(obj, request)

Expand Down
29 changes: 19 additions & 10 deletions mooringlicensing/components/proposals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2034,7 +2034,7 @@ def clone_proposal_with_status_reset(self):

logger.info(f'Cloning the proposal: [{self}] to the proposal: [{proposal}]...')

self.proposal_applicant.copy_self_to_proposal(proposal)
# self.proposal_applicant.copy_self_to_proposal(proposal)

proposal.save(no_revision=True)
return proposal
Expand Down Expand Up @@ -3537,18 +3537,27 @@ def validate_vessel_length(self, request):
min_mooring_vessel_size_str = GlobalSettings.objects.get(key=GlobalSettings.KEY_MINUMUM_MOORING_VESSEL_LENGTH).value
min_mooring_vessel_size = float(min_mooring_vessel_size_str)

if self.proposal_type.code in [PROPOSAL_TYPE_RENEWAL, PROPOSAL_TYPE_AMENDMENT] and self.vessel_details.vessel_applicable_length < min_vessel_size:
logger.error("Proposal {}: Vessel must be at least {}m in length".format(self, min_vessel_size_str))
raise serializers.ValidationError("Vessel must be at least {}m in length".format(min_vessel_size_str))
if self.proposal_type.code in [PROPOSAL_TYPE_RENEWAL, PROPOSAL_TYPE_AMENDMENT]:
# Even when renewal/amendment, there might not be the vessels on the approval because of the sales of the vessels. Check it.
min_vessel_applicable_length = self.approval.child_obj.get_current_min_vessel_applicable_length()

if min_vessel_applicable_length > min_mooring_vessel_size:
# There is already a sufficient size vessel for ML.
if self.vessel_details.vessel_applicable_length < min_vessel_size:
logger.error("Proposal {}: Vessel must be at least {}m in length".format(self, min_vessel_size_str))
raise serializers.ValidationError("Vessel must be at least {}m in length".format(min_vessel_size_str))
else:
# There are no sufficient size vessels for ML.
if self.vessel_details.vessel_applicable_length < min_mooring_vessel_size:
logger.error("Proposal {}: Vessel must be at least {}m in length".format(self, min_mooring_vessel_size_str))
raise serializers.ValidationError("Vessel must be at least {}m in length".format(min_mooring_vessel_size))
if self.vessel_details.vessel_applicable_length > self.approval.child_obj.mooring.vessel_size_limit or self.vessel_details.vessel_draft > self.approval.child_obj.mooring.vessel_draft_limit:
# Vessel is too long / Vessel draft is too deep
logger.error("Proposal {}: Vessel unsuitable for mooring".format(self))
raise serializers.ValidationError("Vessel unsuitable for mooring")
elif self.vessel_details.vessel_applicable_length < min_mooring_vessel_size:
logger.error("Proposal {}: Vessel must be at least {}m in length".format(self, min_mooring_vessel_size_str))
raise serializers.ValidationError("Vessel must be at least {}m in length".format(min_mooring_vessel_size_str))
elif self.proposal_type.code in [PROPOSAL_TYPE_RENEWAL, PROPOSAL_TYPE_AMENDMENT] and (
self.vessel_details.vessel_applicable_length > self.approval.child_obj.mooring.vessel_size_limit or
self.vessel_details.vessel_draft > self.approval.child_obj.mooring.vessel_draft_limit
):
logger.error("Proposal {}: Vessel unsuitable for mooring".format(self))
raise serializers.ValidationError("Vessel unsuitable for mooring")

def process_after_discarded(self):
if self.waiting_list_allocation:
Expand Down
11 changes: 10 additions & 1 deletion mooringlicensing/components/proposals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ def save_proponent_data_aaa(instance, request, viewset):
logger.info(f'Update the Proposal: [{instance}] with the data: [{proposal_data}].')

if viewset.action == 'submit':
create_proposal_applicant_if_not_exist(instance.child_obj, request)

# if instance.invoice and instance.invoice.payment_status in ['paid', 'over_paid']:
if instance.invoice and get_invoice_payment_status(instance.id) in ['paid', 'over_paid']:
# Save + Submit + Paid ==> We have to update the status
Expand Down Expand Up @@ -410,6 +412,8 @@ def save_proponent_data_wla(instance, request, viewset):
logger.info(f'Update the Proposal: [{instance}] with the data: [{proposal_data}].')

if viewset.action == 'submit':
create_proposal_applicant_if_not_exist(instance.child_obj, request)

# if instance.invoice and instance.invoice.payment_status in ['paid', 'over_paid']:
if instance.invoice and get_invoice_payment_status(instance.invoice.id) in ['paid', 'over_paid']:
# Save + Submit + Paid ==> We have to update the status
Expand Down Expand Up @@ -446,6 +450,8 @@ def save_proponent_data_mla(instance, request, viewset):
logger.info(f'Update the Proposal: [{instance}] with the data: [{proposal_data}].')

if viewset.action == 'submit':
create_proposal_applicant_if_not_exist(instance.child_obj, request)

instance.child_obj.process_after_submit(request)
instance.refresh_from_db()

Expand Down Expand Up @@ -476,6 +482,8 @@ def save_proponent_data_aua(instance, request, viewset):
logger.info(f'Update the Proposal: [{instance}] with the data: [{proposal_data}].')

if viewset.action == 'submit':
create_proposal_applicant_if_not_exist(instance.child_obj, request)

instance.child_obj.process_after_submit(request)
instance.refresh_from_db()

Expand Down Expand Up @@ -1015,9 +1023,10 @@ def get_fee_amount_adjusted(proposal, fee_item_being_applied, vessel_length):
return fee_amount_adjusted


def make_proposal_applicant_ready(proposal, request):
def create_proposal_applicant_if_not_exist(proposal, request):
proposal_applicant, created = ProposalApplicant.objects.get_or_create(proposal=proposal)
if created:
# Copy data from the EmailUserRO only when a new proposal_applicant obj is created
proposal_applicant.first_name = request.user.first_name
proposal_applicant.last_name = request.user.last_name
proposal_applicant.dob = request.user.dob
Expand Down
Loading

0 comments on commit 36b17d3

Please sign in to comment.