Skip to content

Commit

Permalink
Merge pull request #2451 from prv-proton/feat/prashanth-compliance-un…
Browse files Browse the repository at this point in the history
…it-xls-update-2414

Update xls download with compliance units for 2023 compliance reports#2414
  • Loading branch information
AlexZorkin committed Jul 24, 2023
2 parents 110054b + 73abf44 commit 4110a2d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
35 changes: 21 additions & 14 deletions backend/api/services/ComplianceReportSpreadSheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,11 @@ def add_schedule_summary(self, summary, compliance_period):
'28': 'Part 3 non-compliance penalty payable'
}
if compliance_period >= 2023:
line_details['23'] = 'Total Compliance Units credits from fuel supplied (from Schedule B)'
line_details['24'] = 'Total Compliance Units debits from fuel supplied (from Schedule B)'
line_details['25'] = 'Net Compliance Units Balance for compliance period'
line_details['27'] = 'Outstanding Compliance Units Balance'
debit_amt = Decimal(summary['lines'][str(24)]) * -1
net_debit_bal = Decimal(summary['lines'][str(27)]) * -1
summary['lines'][str(24)] = str(debit_amt)
summary['lines'][str(27)] = str(net_debit_bal)
line_details['25'] = 'Net compliance unit balance for compliance period'
line_details['29A'] = 'Available compliance unit balance on March 31, ' + str(compliance_period)
line_details['29B'] = 'Compliance unit balance change from assessment'
line_details['29C'] = 'Available compliance unit balance after assessment on March 31, ' + str(compliance_period)
line_details['28'] = 'Non-compliance penalty payable'

line_format = defaultdict(lambda: quantity_format)
line_format['11'] = currency_format
Expand Down Expand Up @@ -353,19 +350,29 @@ def add_schedule_summary(self, summary, compliance_period):

row_index += 1
columns = [
"Part 3 - Low Carbon Fuel Requirement Summary",
"Part 3 - Low Carbon Fuel Requirement Summary" if compliance_period < 2023 else "Low Carbon Fuel Requirement",
"Line",
"Value"
]

for col_index, value in enumerate(columns):
worksheet.write(row_index, col_index, value, header_style)

for line in range(23, 28+1):
row_index += 1
worksheet.write(row_index, 0, line_details[str(line)], description_format)
worksheet.write(row_index, 1, 'Line {}'.format(line))
worksheet.write(row_index, 2, Decimal(summary['lines'][str(line)]), line_format[str(line)])
if compliance_period >= 2023:
compliance_lines = ['25','29A','29B','28','29C']
for line in compliance_lines:
if line != '28' or (line == '28' and summary['lines'][line] > 0):
row_index += 1
worksheet.write(row_index, 0, line_details[line], description_format)
if line.isdigit():
worksheet.write(row_index, 1, f'Line {line}')
worksheet.write(row_index, 2, Decimal(summary['lines'][line]), line_format[str(line)])
else:
for line in range(23, 28+1):
row_index += 1
worksheet.write(row_index, 0, line_details[str(line)], description_format)
worksheet.write(row_index, 1, 'Line {}'.format(line))
worksheet.write(row_index, 2, Decimal(summary['lines'][str(line)]), line_format[str(line)])

row_index += 1
columns = [
Expand Down
8 changes: 0 additions & 8 deletions backend/api/services/OrganizationService.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def get_pending_transfers_value(organization):
Q(respondent_id=organization.id) &
Q(is_rescinded=False))
).aggregate(total_credits=Sum('number_of_credits'))

if pending_trades['total_credits'] is not None:
pending_transfers_value = pending_trades['total_credits']

Expand Down Expand Up @@ -59,7 +58,6 @@ def get_pending_deductions(
"Deleted"
])
).filter(id=group_id).first()

if compliance_report and compliance_report.summary:
if compliance_report.supplements_id and \
compliance_report.supplements_id > 0:
Expand Down Expand Up @@ -113,7 +111,6 @@ def get_pending_deductions(
# if report.status.director_status_id == 'Accepted' and \
# ignore_pending_supplemental:
# deductions -= report.summary.credits_offset

if deductions < 0:
deductions = 0

Expand All @@ -127,7 +124,6 @@ def get_max_credit_offset(organization, compliance_year, exclude_reserved=False)
compliance_period_effective_date = datetime.date(
int(compliance_year), 1, 1
)

credits = CreditTrade.objects.filter(
(Q(status__status="Approved") &
Q(type__the_type="Sell") &
Expand All @@ -150,7 +146,6 @@ def get_max_credit_offset(organization, compliance_year, exclude_reserved=False)
Q(is_rescinded=False) &
Q(compliance_period__effective_date__lte=compliance_period_effective_date))
).aggregate(total=Sum('number_of_credits'))

debits = CreditTrade.objects.filter(
(Q(status__status="Approved") &
Q(type__the_type="Sell") &
Expand All @@ -168,7 +163,6 @@ def get_max_credit_offset(organization, compliance_year, exclude_reserved=False)
Q(is_rescinded=False) &
Q(compliance_period__effective_date__lte=compliance_period_effective_date))
).aggregate(total=Sum('number_of_credits'))

total_in_compliance_period = 0
if credits and credits.get('total') is not None:
total_in_compliance_period = credits.get('total')
Expand All @@ -183,10 +177,8 @@ def get_max_credit_offset(organization, compliance_year, exclude_reserved=False)
validated_credits = organization.organization_balance.get(
'validated_credits', 0
)

total_balance = validated_credits - pending_deductions
total_available_credits = min(total_in_compliance_period, total_balance)

if total_available_credits < 0:
total_available_credits = 0

Expand Down
46 changes: 26 additions & 20 deletions backend/api/viewsets/ComplianceReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,29 @@ def list(self, request, *args, **kwargs):
cached_page.set(sanitized_cache_key, data, 60 * 15)
return Response(data)

def compliance_to_new_act(self, obj, snapshot):
if int(obj.compliance_period.description) > 2022 and snapshot is not None:
lines = snapshot.get('summary').get('lines')
if lines.get('29A') is None:
compliance_unit_balance = OrganizationService.get_max_credit_offset_for_interval(
obj.organization,
obj.update_timestamp
)
change_assessment_balance = compliance_unit_balance + int(lines['25'])
lines['29A'] = compliance_unit_balance
if change_assessment_balance < 0:
lines['28'] = (change_assessment_balance * Decimal('-600.00')).max(Decimal(0))
lines['29B'] = change_assessment_balance
lines['29C'] = 0
else:
lines['28'] = 0
lines['29B'] = lines['25']
lines['29C'] = change_assessment_balance
snapshot['summary']['total_payable'] = Decimal(lines['11']) + Decimal(lines['22']) + lines[
'28']
snapshot['summary']['lines'] = lines
return snapshot

@action(detail=False, methods=['post'])
def paginated(self, request):
queryset = self.get_queryset()
Expand Down Expand Up @@ -603,26 +626,8 @@ def snapshot(self, request, pk=None):
# failure to find an object will trigger an exception that is
# translated into a 404
snapshot = ComplianceReportSnapshot.objects.get(compliance_report=obj)
if int(obj.compliance_period.description) > 2022 and snapshot is not None:
lines = snapshot.snapshot.get('summary').get('lines')
if lines.get('29A') is None:
compliance_unit_balance = OrganizationService.get_max_credit_offset_for_interval(
obj.organization,
obj.update_timestamp
)
change_assessment_balance = compliance_unit_balance + int(lines['25'])
lines['29A'] = compliance_unit_balance
if change_assessment_balance < 0:
lines['28'] = (change_assessment_balance * Decimal('-600.00')).max(Decimal(0))
lines['29B'] = change_assessment_balance
lines['29C'] = 0
else:
lines['28'] = 0
lines['29B'] = lines['25']
lines['29C'] = change_assessment_balance
snapshot.snapshot['summary']['total_payable'] = Decimal(lines['11']) + Decimal(lines['22']) + lines['28']
snapshot.snapshot['summary']['lines'] = lines
return Response(snapshot.snapshot)
snapshot = self.compliance_to_new_act(obj, snapshot.snapshot)
return Response(snapshot)

@action(detail=True, methods=['patch'])
def compute_totals(self, request, pk=None):
Expand Down Expand Up @@ -692,6 +697,7 @@ def xls(self, request, pk=None):
if obj.type.the_type == 'Exclusion Report':
workbook.add_exclusion_agreement(snapshot['exclusion_agreement'])
if obj.type.the_type == 'Compliance Report':
snapshot = self.compliance_to_new_act(obj, snapshot)
workbook.add_schedule_a(snapshot['schedule_a'])
workbook.add_schedule_b(snapshot['schedule_b'],
int(snapshot['compliance_period']['description']))
Expand Down

0 comments on commit 4110a2d

Please sign in to comment.