Skip to content

Commit

Permalink
Merge pull request #360 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 Oct 10, 2023
2 parents 5dd3301 + 3e9f1e0 commit c191a85
Show file tree
Hide file tree
Showing 19 changed files with 392 additions and 212 deletions.
2 changes: 2 additions & 0 deletions mooringlicensing/components/approvals/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ 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)
serializer = StickerActionDetailSerializer(data=data)
serializer.is_valid(raise_exception=True)
new_sticker_action_detail = serializer.save()
Expand Down Expand Up @@ -1308,6 +1309,7 @@ def create_mooring_licence_application(self, request, *args, **kwargs):
waiting_list_allocation.internal_status = Approval.INTERNAL_STATUS_OFFERED
waiting_list_allocation.wla_order = None
waiting_list_allocation.save()
waiting_list_allocation.set_wla_order()

return Response({"proposal_created": new_proposal.lodgement_number})

Expand Down
49 changes: 41 additions & 8 deletions mooringlicensing/components/approvals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,17 +485,22 @@ def add_vessel_ownership(self, vessel_ownership):

def add_mooring(self, mooring, site_licensee):
# do not add if this mooring already exists for the approval
mooring_on_approval = None
target_date=datetime.datetime.now(pytz.timezone(TIME_ZONE)).date()
query = Q()
query &= Q(mooring=mooring)
query &= (Q(end_date__gt=target_date) | Q(end_date__isnull=True)) # Not ended yet

created = None
if not self.mooringonapproval_set.filter(mooring=mooring):
if not self.mooringonapproval_set.filter(query):
mooring_on_approval, created = MooringOnApproval.objects.update_or_create(
mooring=mooring,
approval=self,
site_licensee=site_licensee
)
mooring=mooring,
approval=self,
site_licensee=site_licensee
)
if created:
logger.info('New Mooring {} has been added to the approval {}'.format(mooring.name, self.lodgement_number))
return mooring_on_approval, created
else:
logger.warning(f'There is already a current MooringOnApproval object whose approval: [{self}], mooring: [{mooring}] and site_licensee: [{site_licensee}].')

@property
def bpay_allowed(self):
Expand Down Expand Up @@ -1022,7 +1027,7 @@ def get_intermediate_approvals(email_user_id):

def get_grace_period_end_date(self):
end_date = None
if self.current_proposal.vessel_ownership.end_date:
if self.current_proposal and self.current_proposal.vessel_ownership and self.current_proposal.vessel_ownership.end_date:
end_date = self.current_proposal.vessel_ownership.end_date + relativedelta(months=+6)
return end_date

Expand Down Expand Up @@ -1109,6 +1114,13 @@ def process_after_approval(self):
self.save()
logger.info(f'Set attributes as follows: [internal_status=approved, status=fulfilled, wla_order=None] of the WL Allocation: [{self}].')
self.set_wla_order()

def process_after_withdrawn(self):
self.wla_order = None
self.internal_status = Approval.INTERNAL_STATUS_WAITING
self.save()
logger.info(f'Set attributes as follows: [internal_status=waiting, wla_order=None] of the WL Allocation: [{self}].')
self.set_wla_order()

def process_after_discarded(self):
self.wla_order = None
Expand All @@ -1117,6 +1129,17 @@ def process_after_discarded(self):
logger.info(f'Set attributes as follows: [status=fulfilled, wla_order=None] of the WL Allocation: [{self}].')
self.set_wla_order()

def reinstate_wla_order(self, request):
"""
This function makes this WL allocation back to the 'waiting' status
"""
self.wla_order = None
self.status = Approval.APPROVAL_STATUS_CURRENT
self.internal_status = Approval.INTERNAL_STATUS_WAITING
self.save()
logger.info(f'Set attributes as follows: [status=current, internal_status=waiting, wla_order=None] of the WL Allocation: [{self}].')
self.set_wla_order()


class AnnualAdmissionPermit(Approval):
approval = models.OneToOneField(Approval, parent_link=True, on_delete=models.CASCADE)
Expand All @@ -1138,6 +1161,9 @@ def get_grace_period_end_date(self):
def process_after_discarded(self):
logger.debug(f'in AAP called.')

def process_after_withdrawn(self):
logger.debug(f'in AAP called.')

@property
def child_obj(self):
raise NotImplementedError('This method cannot be called on a child_obj')
Expand Down Expand Up @@ -1385,6 +1411,9 @@ def get_grace_period_end_date(self):
def process_after_discarded(self):
logger.debug(f'in AUP called.')

def process_after_withdrawn(self):
logger.debug(f'in AUP called.')

def get_authorised_by(self):
preference = self.current_proposal.child_obj.get_mooring_authorisation_preference()
return preference
Expand Down Expand Up @@ -1747,6 +1776,9 @@ def get_grace_period_end_date(self):

return end_date

def process_after_withdrawn(self):
logger.debug(f'in ML called.')

def process_after_discarded(self):
logger.debug(f'in ML called.')

Expand Down Expand Up @@ -3028,6 +3060,7 @@ class StickerActionDetail(models.Model):
# user = models.ForeignKey(EmailUser, null=True, blank=True, on_delete=models.SET_NULL)
user = models.IntegerField(null=True, blank=True)
sticker_action_fee = models.ForeignKey(StickerActionFee, null=True, blank=True, related_name='sticker_action_details', on_delete=models.SET_NULL)
waive_the_fee = models.BooleanField(default=False)

class Meta:
app_label = 'mooringlicensing'
Expand Down
25 changes: 24 additions & 1 deletion mooringlicensing/components/approvals/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ def get_offer_link(self, obj):
link = ''
if (
type(obj.child_obj) == WaitingListAllocation and
obj.status == 'current' and
obj.status == Approval.APPROVAL_STATUS_CURRENT and
obj.current_proposal.preferred_bay and
obj.internal_status == Approval.INTERNAL_STATUS_WAITING
):
Expand Down Expand Up @@ -1099,6 +1099,7 @@ class LookupApprovalSerializer(serializers.ModelSerializer):
submitter_phone_number = serializers.SerializerMethodField()
vessel_data = serializers.SerializerMethodField()
url = serializers.SerializerMethodField()
allocated_by = serializers.SerializerMethodField()

class Meta:
model = Approval
Expand All @@ -1111,8 +1112,28 @@ class Meta:
'submitter_phone_number',
'vessel_data',
'url',
'allocated_by',
)

def get_allocated_by(self, obj):
allocated_by = ''
mooring = self.context.get('mooring', None)

if mooring and obj.code == AuthorisedUserPermit.code:
query = Q()
query &= Q(mooring=mooring)
query &= Q(approval=obj)
# query &= (Q(end_date__gt=target_date) | Q(end_date__isnull=True))

try:
moa = MooringOnApproval.objects.get(query)
allocated_by = 'LIC' if moa.site_licensee else 'RIA'
except Exception as e:
logger.error(f'{e}')
pass

return allocated_by

def get_url(self, obj):
return '/internal/approval/{}'.format(obj.id)

Expand Down Expand Up @@ -1181,6 +1202,7 @@ class StickerActionDetailSerializer(serializers.ModelSerializer):
date_created = serializers.DateTimeField(read_only=True)
date_updated = serializers.DateTimeField(read_only=True)
user_detail = serializers.SerializerMethodField()
waive_the_fee = serializers.BooleanField()

class Meta:
model = StickerActionDetail
Expand All @@ -1195,6 +1217,7 @@ class Meta:
'action',
'user', # For saving the user data
'user_detail', # For reading the user data
'waive_the_fee',
)

def get_user_detail(self, obj):
Expand Down
43 changes: 43 additions & 0 deletions mooringlicensing/components/main/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from datetime import datetime
from io import BytesIO

from ledger_api_client import api
from ledger_api_client.settings_base import TIME_ZONE
from ledger_api_client.ledger_models import EmailUserRO
from django.utils import timezone
from confy import env

Expand All @@ -18,6 +21,8 @@
from mooringlicensing.components.proposals.models import (
MooringBay,
Mooring,
Proposal,
ProposalApplicant,
StickerPrintingBatch
)
from mooringlicensing.components.main.decorators import query_debugger
Expand All @@ -26,6 +31,8 @@
from copy import deepcopy
import logging

from mooringlicensing.components.users.serializers import ProposalApplicantSerializer

# logger = logging.getLogger('mooringlicensing')
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -527,3 +534,39 @@ def reorder_wla(target_bay):
logger.info(f'Allocation order: [{w.wla_order}] has been set to the WaitingListAllocation: [{w}].')
place += 1

def update_personal_details(request, user_id):
"""
Update the ledger and the proposal_applicant(s) of all the applications of this user with 'draft' status
"""
data = json.loads(request.body.decode())
payload = data.get('payload', None)

# Format data to use ProposalApplicantSerializer to save
dob = payload.get('dob', None)
if dob:
dob = datetime.strptime(dob, '%d/%m/%Y')
payload['dob'] = dob.strftime('%Y-%m-%d')

residential_address = payload.get('residential_address', None)
if residential_address:
payload.update(residential_address)

postal_address = payload.get('postal_address', None)
if postal_address:
payload.update(postal_address)

# Update the ledger
ret = api.update_account_details(request, user_id)

ret_content = json.loads(ret.content.decode())
if ret_content.get('status', None) == 200 and payload:
# Personal details have successfully updated the ledger.
# Now we want to update the proposal_applicant of all of this user's proposals with 'draft' status
proposals = Proposal.objects.filter(submitter=user_id, processing_status=Proposal.PROCESSING_STATUS_DRAFT)
for proposal in proposals:
proposal_applicant = ProposalApplicant.objects.filter(proposal=proposal).order_by('updated_at').last()
serializer = ProposalApplicantSerializer(proposal_applicant, data=payload)
serializer.is_valid(raise_exception=True)
proposal_applicant = serializer.save()

return ret
6 changes: 2 additions & 4 deletions mooringlicensing/components/payments_ml/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ def post(self, request, *args, **kwargs):
sticker_action_details = StickerActionDetail.objects.filter(id__in=ids)
sticker_action_details.update(sticker_action_fee=sticker_action_fee)

# set_session_sticker_action_invoice(request.session, sticker_action_fee)

target_datetime_str = current_datetime.astimezone(pytz.timezone(TIME_ZONE)).strftime('%d/%m/%Y %I:%M %p')
application_type = ApplicationType.objects.get(code=settings.APPLICATION_TYPE_REPLACEMENT_STICKER['code'])
fee_item = FeeItemStickerReplacement.get_fee_item_by_date(current_datetime.date())
Expand All @@ -249,8 +247,8 @@ def post(self, request, *args, **kwargs):
line = {
'ledger_description': 'Sticker Replacement Fee, sticker: {} @{}'.format(sticker_action_detail.sticker, target_datetime_str),
'oracle_code': application_type.get_oracle_code_by_date(current_datetime.date()),
'price_incl_tax': fee_item.amount,
'price_excl_tax': ledger_api_client.utils.calculate_excl_gst(fee_item.amount) if fee_item.incur_gst else fee_item.amount,
'price_incl_tax': 0 if sticker_action_detail.waive_the_fee else fee_item.amount,
'price_excl_tax': 0 if sticker_action_detail.waive_the_fee else ledger_api_client.utils.calculate_excl_gst(fee_item.amount) if fee_item.incur_gst else fee_item.amount,
'quantity': 1,
}
lines.append(line)
Expand Down
6 changes: 5 additions & 1 deletion mooringlicensing/components/proposals/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ class VesselRegistrationDocumentAdmin(admin.ModelAdmin):

@admin.register(models.VesselOwnership)
class VesselOwnershipAdmin(admin.ModelAdmin):
# list_display = ['id', 'owner', 'vessel', 'company_ownership', 'percentage', 'start_date', 'end_date', 'dot_name',]
list_display = ['id', 'owner', 'vessel', 'percentage', 'start_date', 'end_date', 'dot_name',]


@admin.register(models.VesselDetails)
class VesselDetailsAdmin(admin.ModelAdmin):
list_display = ['id', 'vessel', 'vessel_type', 'vessel_name', 'vessel_length', 'vessel_draft',]


@admin.register(models.CompanyOwnership)
class CompanyOwnershipAdmin(admin.ModelAdmin):
list_display = ['id', 'company', 'vessel', 'percentage', 'start_date', 'end_date',]
Expand Down
Loading

0 comments on commit c191a85

Please sign in to comment.