Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling no sticker #529

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions mooringlicensing/components/approvals/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,65 @@ def swap_moorings(self, request, *args, **kwargs):
originated_approval.child_obj.swap_moorings(request, target_approval.child_obj)
return Response()

@detail_route(methods=['POST'], detail=True)
@basic_exception_handler
def create_new_sticker(self, request, *args, **kwargs):
# external
approval = self.get_object()
details = request.data['details']

if approval.current_proposal:
v_details = approval.current_proposal.latest_vessel_details
v_ownership = approval.current_proposal.vessel_ownership

if v_details and not v_ownership.end_date:
# Licence/Permit has a vessel
sticker_action_details = []

#only allow this if there are no sticker records associated with the approval
if Sticker.objects.filter(approval=approval).exclude(status__in=[Sticker.STICKER_STATUS_EXPIRED,Sticker.STICKER_STATUS_CANCELLED]).exists():
raise serializers.ValidationError("This approval already has an active sticker record.")

data = {}
today = datetime.now(pytz.timezone(settings.TIME_ZONE)).date()

data['action'] = 'Create new sticker'
data['user'] = request.user.id
data['reason'] = details['reason']
if is_internal(request):
data['waive_the_fee'] = request.data.get('waive_the_fee', False)
else:
data['waive_the_fee'] = False
#new address checkbox
data['change_sticker_address'] = request.data.get('change_sticker_address', False)
#address change (only applied if above True)
data['new_postal_address_line1'] = request.data.get('postal_address_line1','')
data['new_postal_address_line2'] = request.data.get('postal_address_line2','')
data['new_postal_address_line3'] = request.data.get('postal_address_line3','')
data['new_postal_address_locality'] = request.data.get('postal_address_locality','')
data['new_postal_address_state'] = request.data.get('postal_address_state','')
data['new_postal_address_country'] = request.data.get('postal_address_country','AU')
data['new_postal_address_postcode'] = request.data.get('postal_address_postcode','')
if data['change_sticker_address'] and '' in [
data['new_postal_address_line1'],
data['new_postal_address_locality'],
data['new_postal_address_state'],
data['new_postal_address_country'],
data['new_postal_address_postcode']
]:
raise serializers.ValidationError("Required address details not provided")

serializer = StickerActionDetailSerializer(data=data)
serializer.is_valid(raise_exception=True)
new_sticker_action_detail = serializer.save()
new_sticker_action_detail.approval = approval
new_sticker_action_detail.save()
sticker_action_details.append(new_sticker_action_detail.id)

return Response({'sticker_action_detail_ids': sticker_action_details})
else:
raise Exception('You cannot request a new sticker for the licence/permit without a vessel.')

@detail_route(methods=['POST'], detail=True)
@basic_exception_handler
def request_new_stickers(self, request, *args, **kwargs):
Expand Down Expand Up @@ -578,7 +637,11 @@ def request_new_stickers(self, request, *args, **kwargs):
data['user'] = request.user.id
data['reason'] = details['reason']
data['date_of_lost_sticker'] = today.strftime('%d/%m/%Y')
data['waive_the_fee'] = request.data.get('waive_the_fee', False)

if is_internal(request):
data['waive_the_fee'] = request.data.get('waive_the_fee', False)
else:
data['waive_the_fee'] = False

#new address checkbox
data['change_sticker_address'] = request.data.get('change_sticker_address', False)
Expand All @@ -602,6 +665,7 @@ def request_new_stickers(self, request, *args, **kwargs):
serializer = StickerActionDetailSerializer(data=data)
serializer.is_valid(raise_exception=True)
new_sticker_action_detail = serializer.save()
new_sticker_action_detail.approval = approval
new_sticker_action_detail.sticker = sticker
new_sticker_action_detail.save()
sticker_action_details.append(new_sticker_action_detail.id)
Expand Down Expand Up @@ -1753,7 +1817,10 @@ def request_replacement(self, request, *args, **kwargs):
data['sticker'] = sticker.id
data['action'] = 'Request replacement'
data['user'] = request.user.id
data['waive_the_fee'] = request.data.get('waive_the_fee', False)
if is_internal(request):
data['waive_the_fee'] = request.data.get('waive_the_fee', False)
else:
data['waive_the_fee'] = False
data['reason'] = request.data.get('details', {}).get('reason', '')
serializer = StickerActionDetailSerializer(data=data)
serializer.is_valid(raise_exception=True)
Expand Down
25 changes: 25 additions & 0 deletions mooringlicensing/components/approvals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,17 @@ def postal_address_line2(self):

return ret_value

@property
def postal_address_line3(self):
try:
ret_value = self.proposal_applicant.postal_address_line3
except:
logger.error(f'Postal address line3 cannot be retrieved for the approval [{self}]')
return ''

return ret_value


@property
def postal_address_state(self):
try:
Expand Down Expand Up @@ -470,6 +481,19 @@ def postal_address_postcode(self):

return ret_value

@property
def postal_address_country(self):
try:
ret_value = self.proposal_applicant.postal_address_country
except:
logger.error(f'Postal address country cannot be retrieved for the approval [{self}]')
return ''

if not ret_value:
logger.warning(f'Empty country found for the postal address of the Approval: [{self}].')

return ret_value

@property
def description(self):
if hasattr(self, 'child_obj'):
Expand Down Expand Up @@ -3676,6 +3700,7 @@ def vessel_applicable_length(self):


class StickerActionDetail(models.Model):
approval = models.ForeignKey(Approval, blank=True, null=True, related_name='sticker_action_approval', on_delete=models.SET_NULL)
sticker = models.ForeignKey(Sticker, blank=True, null=True, related_name='sticker_action_details', on_delete=models.SET_NULL)
reason = models.TextField(blank=True)
date_created = models.DateTimeField(blank=True, null=True, auto_now_add=True)
Expand Down
7 changes: 7 additions & 0 deletions mooringlicensing/components/approvals/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ class ListApprovalSerializer(serializers.ModelSerializer):
allowed_assessors_user = serializers.SerializerMethodField()
stickers = serializers.SerializerMethodField()
stickers_historical = serializers.SerializerMethodField()
has_sticker = serializers.SerializerMethodField()
is_approver = serializers.SerializerMethodField()
is_assessor = serializers.SerializerMethodField()
vessel_regos = serializers.SerializerMethodField()
Expand Down Expand Up @@ -641,6 +642,7 @@ class Meta:
'allowed_assessors_user',
'stickers',
'stickers_historical',
'has_sticker',
'licence_document',
'authorised_user_summary_document',
'is_assessor',
Expand Down Expand Up @@ -685,6 +687,7 @@ class Meta:
'allowed_assessors_user',
'stickers',
'stickers_historical',
'has_sticker',
'licence_document',
'authorised_user_summary_document',
'is_assessor',
Expand Down Expand Up @@ -719,6 +722,9 @@ def get_mooring_offered(self, obj):
}
return mooring

def get_has_sticker(self,obj):
return Sticker.objects.filter(approval=obj).exclude(status__in=[Sticker.STICKER_STATUS_EXPIRED,Sticker.STICKER_STATUS_CANCELLED]).exists()

def get_moorings(self, obj):
links = []
request = self.context.get('request')
Expand Down Expand Up @@ -1123,6 +1129,7 @@ class Meta:
model = StickerActionDetail
fields = (
'id',
'approval',
'sticker',
'reason',
'date_created',
Expand Down
8 changes: 6 additions & 2 deletions mooringlicensing/components/payments_ml/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ class StickerActionFee(Payment):
def __str__(self):
stickers = []
for sticker_action_detail in self.sticker_action_details.all():
stickers.append(sticker_action_detail.sticker.number)
return 'Sticker(s): [{}] : Invoice {}'.format(','.join(stickers), self.invoice_reference)
if sticker_action_detail.sticker:
stickers.append(sticker_action_detail.sticker.number)
if stickers:
return 'Sticker(s): [{}] : Invoice {}'.format(','.join(stickers), self.invoice_reference)
else:
return 'New Sticker Invoice {}'.format(self.invoice_reference)

class Meta:
app_label = 'mooringlicensing'
Expand Down
45 changes: 40 additions & 5 deletions mooringlicensing/components/payments_ml/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def post(self, request, *args, **kwargs):
'quantity': 1,
}
if not applicant:
applicant = sticker_action_detail.sticker.approval.applicant_obj
applicant = sticker_action_detail.approval.applicant_obj
lines.append(line)


Expand Down Expand Up @@ -316,17 +316,52 @@ def get(self, request, uuid, format=None):

sticker_action_fee.invoice_reference = invoice.reference
sticker_action_fee.save()

if sticker_action_fee.payment_type == StickerActionFee.PAYMENT_TYPE_TEMPORARY:
sticker_action_fee.payment_type = ApplicationFee.PAYMENT_TYPE_INTERNET
sticker_action_fee.expiry_time = None
sticker_action_fee.save()

old_sticker_numbers = []
for sticker_action_detail in sticker_action_details.all():
old_sticker = sticker_action_detail.sticker
new_sticker = old_sticker.request_replacement(Sticker.STICKER_STATUS_LOST, sticker_action_detail)
old_sticker_numbers.append(old_sticker.number)
if sticker_action_detail.sticker:
old_sticker = sticker_action_detail.sticker
new_sticker = old_sticker.request_replacement(Sticker.STICKER_STATUS_LOST, sticker_action_detail)
old_sticker_numbers.append(old_sticker.number)
else:
if sticker_action_detail.change_sticker_address:
# Create replacement sticker
new_sticker = Sticker.objects.create(
approval=sticker_action_detail.approval,
vessel_ownership=sticker_action_detail.approval.current_proposal.vessel_ownership,
fee_constructor=sticker_action_detail.approval.current_proposal.fee_constructor,
fee_season=sticker_action_detail.approval.latest_applied_season,
postal_address_line1 = sticker_action_detail.new_postal_address_line1,
postal_address_line2 = sticker_action_detail.new_postal_address_line2,
postal_address_line3 = sticker_action_detail.new_postal_address_line3,
postal_address_locality = sticker_action_detail.new_postal_address_locality,
postal_address_state = sticker_action_detail.new_postal_address_state,
postal_address_country = sticker_action_detail.new_postal_address_country,
postal_address_postcode = sticker_action_detail.new_postal_address_postcode,
)
logger.info(f'New Sticker: [{new_sticker}] has been created for the approval with a new postal address: [{sticker_action_detail.approval}].')
else:
# Create replacement sticker
new_sticker = Sticker.objects.create(
approval=sticker_action_detail.approval,
vessel_ownership=sticker_action_detail.approval.current_proposal.vessel_ownership,
fee_constructor=sticker_action_detail.approval.current_proposal.fee_constructor,
fee_season=sticker_action_detail.approval.latest_applied_season,
postal_address_line1 = sticker_action_detail.approval.postal_address_line1,
postal_address_line2 = sticker_action_detail.approval.postal_address_line2,
postal_address_line3 = sticker_action_detail.approval.postal_address_line3,
postal_address_locality = sticker_action_detail.approval.postal_address_suburb,
postal_address_state = sticker_action_detail.approval.postal_address_state,
postal_address_country = sticker_action_detail.approval.postal_address_country,
postal_address_postcode = sticker_action_detail.approval.postal_address_postcode,
)
logger.info(f'New Sticker: [{new_sticker}] has been created for the approval: [{sticker_action_detail.approval}].')

# Send email with the invoice
send_sticker_replacement_email(request, old_sticker_numbers, new_sticker.approval, invoice.reference)

Expand Down
Loading
Loading