Skip to content

Commit

Permalink
Merge pull request #378 from shibaken/main
Browse files Browse the repository at this point in the history
shibaken/main to dbca-wa/main
  • Loading branch information
xzzy authored Dec 13, 2023
2 parents f13c222 + 28f19ee commit aacca85
Show file tree
Hide file tree
Showing 28 changed files with 1,480 additions and 237 deletions.
32 changes: 22 additions & 10 deletions mooringlicensing/components/approvals/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import traceback
from django.core.paginator import Paginator, EmptyPage
from confy import env
import datetime
import pytz
Expand Down Expand Up @@ -112,11 +113,21 @@ class GetSticker(views.APIView):
renderer_classes = [JSONRenderer, ]

def get(self, request, format=None):
search_term = request.GET.get('term', '')
search_term = request.GET.get('search_term', '')
page_number = request.GET.get('page_number', 1)
items_per_page = 10

if search_term:
data = Sticker.objects.filter(number__icontains=search_term)[:10]
data = Sticker.objects.filter(number__icontains=search_term)
paginator = Paginator(data, items_per_page)
try:
current_page = paginator.page(page_number)
my_objects = current_page.object_list
except EmptyPage:
my_objects = []

data_transform = []
for sticker in data:
for sticker in my_objects:
approval_history = sticker.approvalhistory_set.order_by('id').first() # Should not be None, but could be None for the data generated at the early stage of development.
if approval_history and approval_history.approval:
data_transform.append({
Expand All @@ -134,7 +145,12 @@ def get(self, request, format=None):
# Should not reach here
pass

return Response({"results": data_transform})
return Response({
"results": data_transform,
"pagination": {
"more": current_page.has_next()
}
})
return Response()


Expand Down Expand Up @@ -349,7 +365,7 @@ def get_queryset(self):
all = Approval.objects.all() # We may need to exclude the approvals created from the Waiting List Application

# target_email_user_id = int(self.request.GET.get('target_email_user_id', 0))
target_email_user_id = int(self.request.data.get('target_email_user_id', 0))
target_email_user_id = int(self.request.GET.get('target_email_user_id', 0))

if is_internal(self.request):
if target_email_user_id:
Expand Down Expand Up @@ -1294,13 +1310,9 @@ def create_mooring_licence_application(self, request, *args, **kwargs):
waiting_list_allocation=waiting_list_allocation,
date_invited=current_date,
)
waiting_list_allocation.proposal_applicant.copy_self_to_proposal(new_proposal)
logger.info(f'Offering new Mooring Site Licence application: [{new_proposal}], which has been created from the waiting list allocation: [{waiting_list_allocation}].')

# 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)
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
waiting_list_allocation.current_proposal.copy_vessel_details(new_proposal)
logger.info(f'Vessel details have been copied from the proposal: [{waiting_list_allocation.current_proposal}] to the mooring site licence application: [{new_proposal}].')
Expand Down
94 changes: 73 additions & 21 deletions mooringlicensing/components/approvals/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
from dateutil.relativedelta import relativedelta

import ledger_api_client.utils
Expand Down Expand Up @@ -344,10 +345,44 @@ def postal_address_obj(self):
address_obj = self.submitter_obj.postal_address
return address_obj

@property
def proposal_applicant(self):
proposal_applicant = None
if self.current_proposal:
proposal_applicant = self.current_proposal.proposal_applicant
return proposal_applicant

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

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

return ret_value

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

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

return ret_value


@property
def postal_address_line1(self):
try:
ret_value = self.postal_address_obj.line1
ret_value = self.proposal_applicant.postal_address_line1
except:
logger.error(f'Postal address line1 cannot be retrieved for the approval [{self}].')
return ''
Expand All @@ -360,7 +395,7 @@ def postal_address_line1(self):
@property
def postal_address_line2(self):
try:
ret_value = self.postal_address_obj.line2
ret_value = self.proposal_applicant.postal_address_line2
except:
logger.error(f'Postal address line2 cannot be retrieved for the approval [{self}]')
return ''
Expand All @@ -370,7 +405,7 @@ def postal_address_line2(self):
@property
def postal_address_state(self):
try:
ret_value = self.postal_address_obj.state
ret_value = self.proposal_applicant.postal_address_state
except:
logger.error(f'Postal address state cannot be retrieved for the approval [{self}]')
return ''
Expand All @@ -383,7 +418,7 @@ def postal_address_state(self):
@property
def postal_address_suburb(self):
try:
ret_value = self.postal_address_obj.locality
ret_value = self.proposal_applicant.postal_address_suburb
except:
logger.error(f'Postal address locality cannot be retrieved for the approval [{self}]')
return ''
Expand All @@ -396,7 +431,7 @@ def postal_address_suburb(self):
@property
def postal_address_postcode(self):
try:
ret_value = self.postal_address_obj.postcode
ret_value = self.proposal_applicant.postal_address_postcode
except:
logger.error(f'Postal address postcode cannot be retrieved for the approval [{self}]')
return ''
Expand Down Expand Up @@ -2333,6 +2368,13 @@ def create_fee_lines(self):

private_visit = 'YES' if dcv_admission_arrival.private_visit else 'NO'

if settings.DEBUG:
# In debug environment, we want to avoid decimal number which may cuase some kind of error.
total_amount = math.ceil(total_amount)
total_amount_excl_tax = math.ceil(calculate_excl_gst(total_amount)) if fee_constructor.incur_gst else math.ceil(total_amount)
else:
total_amount_excl_tax = calculate_excl_gst(total_amount) if fee_constructor.incur_gst else total_amount

line_item = {
'ledger_description': '{} Fee: {} (Arrival: {}, Private: {}, {})'.format(
fee_constructor.application_type.description,
Expand All @@ -2343,7 +2385,7 @@ def create_fee_lines(self):
),
'oracle_code': oracle_code,
'price_incl_tax': total_amount,
'price_excl_tax': calculate_excl_gst(total_amount) if fee_constructor.incur_gst else total_amount,
'price_excl_tax': total_amount_excl_tax,
'quantity': 1,
}
line_items.append(line_item)
Expand Down Expand Up @@ -2522,6 +2564,14 @@ def create_fee_lines(self):
db_processes_after_success['season_end_date'] = fee_constructor.fee_season.end_date.__str__()
db_processes_after_success['datetime_for_calculating_fee'] = target_datetime.__str__()

if settings.DEBUG:
# In debug environment, we want to avoid decimal number which may cuase some kind of error.
total_amount = math.ceil(fee_item.amount)
total_amount_excl_tax = math.ceil(ledger_api_client.utils.calculate_excl_gst(fee_item.amount)) if fee_constructor.incur_gst else math.ceil(fee_item.amount),
else:
total_amount = fee_item.amount
total_amount_excl_tax = ledger_api_client.utils.calculate_excl_gst(fee_item.amount) if fee_constructor.incur_gst else fee_item.amount,

line_items = [
{
# 'ledger_description': '{} Fee: {} (Season: {} to {}) @{}'.format(
Expand All @@ -2534,8 +2584,8 @@ def create_fee_lines(self):
),
# 'oracle_code': application_type.oracle_code,
'oracle_code': ApplicationType.get_current_oracle_code_by_application(application_type.code),
'price_incl_tax': fee_item.amount,
'price_excl_tax': ledger_api_client.utils.calculate_excl_gst(fee_item.amount) if fee_constructor.incur_gst else fee_item.amount,
'price_incl_tax': total_amount,
'price_excl_tax': total_amount_excl_tax,
'quantity': 1,
},
]
Expand Down Expand Up @@ -2968,15 +3018,17 @@ def save(self, *args, **kwargs):

@property
def first_name(self):
if self.approval and self.approval.submitter:
return self.approval.submitter_obj.first_name
return '---'
# if self.approval and self.approval.submitter:
# return self.approval.submitter_obj.first_name
# return '---'
return self.approval.postal_first_name

@property
def last_name(self):
if self.approval and self.approval.submitter:
return self.approval.submitter_obj.last_name
return '---'
# if self.approval and self.approval.submitter:
# return self.approval.submitter_obj.last_name
# return '---'
return self.approval.postal_last_name

@property
def postal_address_line1(self):
Expand Down Expand Up @@ -3006,6 +3058,13 @@ def postal_address_suburb(self):
# return '---'
return self.approval.postal_address_suburb

@property
def postal_address_postcode(self):
# if self.approval and self.approval.submitter and self.approval.submitter_obj.postal_address:
# return self.approval.submitter_obj.postal_address.postcode
# return '---'
return self.approval.postal_address_postcode

@property
def vessel_registration_number(self):
if self.vessel_ownership and self.vessel_ownership.vessel:
Expand All @@ -3018,13 +3077,6 @@ def vessel_applicable_length(self):
return self.vessel_ownership.vessel.latest_vessel_details.vessel_applicable_length
raise ValueError('Vessel size not found for the sticker: {}'.format(self))

@property
def postal_address_postcode(self):
# if self.approval and self.approval.submitter and self.approval.submitter_obj.postal_address:
# return self.approval.submitter_obj.postal_address.postcode
# return '---'
return self.approval.postal_address_postcode


class StickerActionDetail(models.Model):
sticker = models.ForeignKey(Sticker, blank=True, null=True, related_name='sticker_action_details', on_delete=models.SET_NULL)
Expand Down
13 changes: 9 additions & 4 deletions mooringlicensing/components/compliances/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import traceback
# import os
# import datetime
Expand Down Expand Up @@ -49,6 +50,8 @@
from mooringlicensing.helpers import is_customer, is_internal
from rest_framework_datatables.pagination import DatatablesPageNumberPagination

logger = logging.getLogger(__name__)


class ComplianceViewSet(viewsets.ModelViewSet):
serializer_class = ComplianceSerializer
Expand Down Expand Up @@ -372,9 +375,6 @@ def filter_queryset(self, request, queryset, view):
if filter_compliance_status and not filter_compliance_status.lower() == 'all':
queryset = queryset.filter(customer_status=filter_compliance_status)

# getter = request.query_params.get
# fields = self.get_fields(getter)
# ordering = self.get_ordering(getter, fields)
fields = self.get_fields(request)
ordering = self.get_ordering(request, view, fields)
queryset = queryset.order_by(*ordering)
Expand All @@ -383,8 +383,13 @@ def filter_queryset(self, request, queryset, view):

try:
queryset = super(ComplianceFilterBackend, self).filter_queryset(request, queryset, view)

# Custom search
# search_term = request.GET.get('search[value]') # This has a search term.
# email_users = EmailUser.objects.filter(Q(first_name__icontains=search_term) | Q(last_name__icontains=search_term) | Q(email__icontains=search_term)).values_list('id', flat=True)
# q_set = Compliance.objects.filter(submitter__in=list(email_users))
except Exception as e:
print(e)
logger.error(f'ComplianceFilterBackend raises an error: [{e}]. Query may not work correctly.')
setattr(view, '_datatables_total_count', total_count)
return queryset

Expand Down
2 changes: 0 additions & 2 deletions mooringlicensing/components/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ class CommunicationsLogEntry(models.Model):
subject = models.CharField(max_length=200, blank=True, verbose_name="Subject / Description")
text = models.TextField(blank=True)

# customer = models.ForeignKey(EmailUser, null=True, related_name='+')
customer = models.IntegerField(null=True) # EmailUserRO
# staff = models.ForeignKey(EmailUser, null=True, related_name='+')
staff = models.IntegerField(null=True, blank=True) # EmailUserRO

created = models.DateTimeField(auto_now_add=True, null=False, blank=False)
Expand Down
3 changes: 1 addition & 2 deletions mooringlicensing/components/main/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,7 @@ def update_personal_details(request, user_id):
# 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 = ProposalApplicantSerializer(proposal.proposal_applicant, data=payload)
serializer.is_valid(raise_exception=True)
proposal_applicant = serializer.save()
logger.info(f'ProposalApplicant: [{proposal_applicant}] has been updated with the data: [{payload}].')
Expand Down
15 changes: 13 additions & 2 deletions mooringlicensing/components/payments_ml/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
# from _pydecimal import Decimal
import decimal
import math

import pytz
from django.http import HttpResponse
Expand Down Expand Up @@ -106,12 +107,22 @@ def generate_line_item(application_type, fee_amount_adjusted, fee_constructor, i
instance.lodgement_number,
target_datetime_str,
)

if settings.DEBUG:
# In debug environment, we want to avoid decimal number which may cuase some kind of error.
total_amount = math.ceil(float(fee_amount_adjusted))
total_amount_excl_tax = math.ceil(float(calculate_excl_gst(fee_amount_adjusted))) if fee_constructor.incur_gst else math.ceil(float(fee_amount_adjusted))
else:
total_amount = float(fee_amount_adjusted)
total_amount_excl_tax = float(calculate_excl_gst(fee_amount_adjusted)) if fee_constructor.incur_gst else float(fee_amount_adjusted)

return {
'ledger_description': ledger_description,
'oracle_code': application_type.get_oracle_code_by_date(target_datetime.date()),
'price_incl_tax': float(fee_amount_adjusted),
'price_excl_tax': float(calculate_excl_gst(fee_amount_adjusted)) if fee_constructor.incur_gst else float(fee_amount_adjusted),
# 'price_incl_tax': float(fee_amount_adjusted),
# 'price_excl_tax': float(calculate_excl_gst(fee_amount_adjusted)) if fee_constructor.incur_gst else float(fee_amount_adjusted),
'price_incl_tax': total_amount,
'price_excl_tax': total_amount_excl_tax,
'quantity': 1,
}

Expand Down
14 changes: 12 additions & 2 deletions mooringlicensing/components/payments_ml/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
import math

import ledger_api_client.utils
# from ledger.checkout.utils import calculate_excl_gst
Expand Down Expand Up @@ -242,14 +243,23 @@ def post(self, request, *args, **kwargs):
application_type = ApplicationType.objects.get(code=settings.APPLICATION_TYPE_REPLACEMENT_STICKER['code'])
fee_item = FeeItemStickerReplacement.get_fee_item_by_date(current_datetime.date())

if settings.DEBUG:
total_amount = 0 if sticker_action_detail.waive_the_fee else math.ceil(fee_item.amount)
total_amount_excl_tax = 0 if sticker_action_detail.waive_the_fee else math.ceil(ledger_api_client.utils.calculate_excl_gst(fee_item.amount)) if fee_item.incur_gst else math.ceil(fee_item.amount)
else:
total_amount = 0 if sticker_action_detail.waive_the_fee else fee_item.amount
total_amount_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

lines = []
applicant = None
for sticker_action_detail in sticker_action_details:
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': 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,
# '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,
'price_incl_tax': total_amount,
'price_excl_tax': total_amount_excl_tax,
'quantity': 1,
}
if not applicant:
Expand Down
Loading

0 comments on commit aacca85

Please sign in to comment.