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

feature/1977 - Update transaction Manager to have report code label in order to sort via Report Type #900

Merged
merged 2 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 44 additions & 3 deletions django-backend/fecfiler/transactions/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,41 @@
from decimal import Decimal
from enum import Enum
from .schedule_b.managers import refunds as schedule_b_refunds
from ..reports.models import Report

"""Manager to deterimine fields that are used the same way across transactions,
but are called different names"""


report_code_label_mapping = Case(
When(report_code="Q1", then=Value("APRIL 15 QUARTERLY REPORT (Q1)")),
When(report_code="Q2", then=Value("JULY 15 QUARTERLY REPORT (Q2)")),
When(report_code="Q3", then=Value("OCTOBER 15 QUARTERLY REPORT (Q3)")),
When(report_code="YE", then=Value("JANUARY 31 YEAR-END (YE)")),
When(report_code="TER", then=Value("TERMINATION REPORT (TER)")),
When(report_code="MY", then=Value("JULY 31 MID-YEAR REPORT (MY)")),
When(report_code="12G", then=Value("12-DAY PRE-GENERAL (12G)")),
When(report_code="12P", then=Value("12-DAY PRE-PRIMARY (12P)")),
When(report_code="12R", then=Value("12-DAY PRE-RUNOFF (12R)")),
When(report_code="12S", then=Value("12-DAY PRE-SPECIAL (12S)")),
When(report_code="12C", then=Value("12-DAY PRE-CONVENTION (12C)")),
When(report_code="30G", then=Value("30-DAY POST-GENERAL (30G)")),
When(report_code="30R", then=Value("30-DAY POST-RUNOFF (30R)")),
When(report_code="30S", then=Value("30-DAY POST-SPECIAL (30S)")),
When(report_code="M2", then=Value("FEBRUARY 20 MONTHLY REPORT (M2)")),
When(report_code="M3", then=Value("MARCH 20 MONTHLY REPORT (M3)")),
When(report_code="M4", then=Value("APRIL 20 MONTHLY REPORT (M4)")),
When(report_code="M5", then=Value("MAY 20 MONTHLY REPORT (M5)")),
When(report_code="M6", then=Value("JUNE 20 MONTHLY REPORT (M6)")),
When(report_code="M7", then=Value("JULY 20 MONTHLY REPORT (M7)")),
When(report_code="M8", then=Value("AUGUST 20 MONTHLY REPORT (M8)")),
When(report_code="M9", then=Value("SEPTEMBER 20 MONTHLY REPORT (M9)")),
When(report_code="M10", then=Value("OCTOBER 20 MONTHLY REPORT (M10)")),
When(report_code="M11", then=Value("NOVEMBER 20 MONTHLY REPORT (M11)")),
When(report_code="M12", then=Value("DECEMBER 20 MONTHLY REPORT (M12)")),
)


class TransactionManager(SoftDeleteManager):
entity_aggregate_window = {
"partition_by": [
Expand Down Expand Up @@ -303,6 +333,11 @@ def PAYMENT_AMOUNT_CLAUSE(self): # noqa: N802
)

def transaction_view(self):
REPORT_CODE_LABEL_CLAUSE = Subquery( # noqa: N806
Report.objects.filter(transactions=OuterRef("pk"))
.annotate(report_code_label=report_code_label_mapping)
.values("report_code_label")[:1]
)
return (
super()
.get_queryset()
Expand All @@ -324,12 +359,19 @@ def transaction_view(self):
transaction_ptr_id=F("id"),
back_reference_tran_id_number=self.BACK_REFERENCE_CLAUSE,
back_reference_sched_name=self.BACK_REFERENCE_NAME_CLAUSE,
report_code_label=REPORT_CODE_LABEL_CLAUSE,
)
)


class TransactionViewManager(Manager):
def get_queryset(self):
REPORT_CODE_LABEL_CLAUSE = Subquery( # noqa: N806
Report.objects.filter(transactions=OuterRef("pk"))
.annotate(report_code_label=report_code_label_mapping)
.values("report_code_label")[:1]
)

return (
super()
.get_queryset()
Expand Down Expand Up @@ -376,6 +418,7 @@ def get_queryset(self):
"_calendar_ytd_per_election_office",
),
line_label=self.LINE_LABEL_CLAUSE(),
report_code_label=REPORT_CODE_LABEL_CLAUSE,
)
.alias(order_key=self.ORDER_KEY_CLAUSE())
.order_by("order_key")
Expand Down Expand Up @@ -404,9 +447,7 @@ def ORDER_KEY_CLAUSE(self): # noqa: N802
output_field=TextField(),
),
),
default=Concat(
"schedule", "_form_type", "created", output_field=TextField()
),
default=Concat("schedule", "_form_type", "created", output_field=TextField()),
output_field=TextField(),
)

Expand Down
26 changes: 13 additions & 13 deletions django-backend/fecfiler/transactions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@

logger = structlog.get_logger(__name__)

MISSING_SCHEMA_NAME_ERROR = ValidationError(
{"schema_name": ["No schema_name provided"]}
)
MISSING_SCHEMA_NAME_ERROR = ValidationError({"schema_name": ["No schema_name provided"]})

SCHEDULE_SERIALIZERS = dict(
A=ScheduleASerializer,
Expand Down Expand Up @@ -76,9 +74,7 @@ class TransactionSerializer(
back_reference_tran_id_number = CharField(
required=False, allow_null=True, read_only=True
)
back_reference_sched_name = CharField(
required=False, allow_null=True, read_only=True
)
back_reference_sched_name = CharField(required=False, allow_null=True, read_only=True)
form_type = CharField(required=False, allow_null=True)
itemized = BooleanField(read_only=True)
name = CharField(read_only=True)
Expand All @@ -98,6 +94,7 @@ class TransactionSerializer(
max_digits=11, decimal_places=2, read_only=True
) # debt payments
line_label = CharField(read_only=True)
report_code_label = CharField(read_only=True)

class Meta:
model = Transaction
Expand Down Expand Up @@ -144,6 +141,7 @@ def get_fields():
"payment_amount",
"balance_at_close",
"line_label",
"report_code_label",
]

fields = get_fields()
Expand Down Expand Up @@ -261,13 +259,15 @@ def to_representation(self, instance):
representation["report_ids"] = []
for report in instance.reports.all():
representation["report_ids"].append(report.id)
representation["reports"].append({
"id": report.id,
"coverage_from_date": report.coverage_from_date,
"coverage_through_date": report.coverage_through_date,
"report_code": report.report_code,
"report_type": report.report_type
})
representation["reports"].append(
{
"id": report.id,
"coverage_from_date": report.coverage_from_date,
"coverage_through_date": report.coverage_through_date,
"report_code": report.report_code,
"report_type": report.report_type,
}
)

return representation

Expand Down
2 changes: 2 additions & 0 deletions django-backend/fecfiler/transactions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class TransactionViewSet(CommitteeOwnedViewMixin, ModelViewSet):
"aggregate",
"balance",
"back_reference_tran_id_number",
"form_type",
"report_code_label",
]
ordering = ["-created"]
queryset = Transaction.objects
Expand Down