From 31c7b7aa05e47efeda7ccaf79ac6117ba1fcb3b9 Mon Sep 17 00:00:00 2001 From: Marek Biczysko <34810846+MarekBiczysko@users.noreply.github.com> Date: Tue, 3 Dec 2024 16:50:43 +0100 Subject: [PATCH] 2215787_drop_cash_assist_migration_fix_ (#4494) --- .../migrate_cash_assist_models.py | 51 +++++++++++++++---- .../test_migrate_cash_assist_models.py | 14 ++--- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/hct_mis_api/one_time_scripts/migrate_cash_assist_models.py b/src/hct_mis_api/one_time_scripts/migrate_cash_assist_models.py index 15b9312ffb..430d14baf6 100644 --- a/src/hct_mis_api/one_time_scripts/migrate_cash_assist_models.py +++ b/src/hct_mis_api/one_time_scripts/migrate_cash_assist_models.py @@ -1,7 +1,9 @@ -import logging +from decimal import Decimal from django.contrib.contenttypes.models import ContentType from django.db import transaction +from django.db.models import Sum +from django.db.models.functions import Coalesce from hct_mis_api.apps.grievance.models import ( TicketComplaintDetails, @@ -25,8 +27,6 @@ ) from hct_mis_api.apps.targeting.models import TargetPopulation -logger = logging.getLogger(__name__) - def get_status(status: str) -> str: mapping = {"Transaction Successful": "Distribution Successful"} @@ -38,18 +38,18 @@ def migrate_cash_plan_to_payment_plan() -> None: content_type_for_cash_plan = ContentType.objects.get_for_model(CashPlan) content_type_for_payment_record = ContentType.objects.get_for_model(PaymentRecord) - logger.info("**Migrating Cash Plan to Payment Plan**") + print("**Migrating Cash Plan to Payment Plan**") delivery_type_to_obj = {obj.name: obj for obj in DeliveryMechanism.objects.all()} - logger.info("Creating FinancialServiceProviders") + print("Creating FinancialServiceProviders") for sp in ServiceProvider.objects.filter(is_migrated_to_payment_plan=False): - logger.info(f"\nProcessing Service Provider {sp}") + print(f"\nProcessing Service Provider {sp}") if not sp.cash_plans.exists(): - logger.info(f"Service provider {sp} has no cash plans") + print(f"Service provider {sp} has no cash plans") continue if FinancialServiceProvider.objects.filter(vision_vendor_number=sp.vision_id).exists(): - logger.info(f"FinancialServiceProvider with vision_id {sp.vision_id} already exists") + print(f"FinancialServiceProvider with vision_id {sp.vision_id} already exists") continue if not sp.vision_id: @@ -78,12 +78,12 @@ def migrate_cash_plan_to_payment_plan() -> None: dm_cash = delivery_type_to_obj["Cash"] cash_plans = CashPlan.objects.filter(is_migrated_to_payment_plan=False) - logger.info(f"Total Cash Plans to migrate: {cash_plans.count()}") + print(f"Total Cash Plans to migrate: {cash_plans.count()}") cp_count = cash_plans.count() cp_i = 0 for cp in cash_plans.iterator(chunk_size=50): if cp_i % 50 == 0: - logger.info(f"Processing cash plan {cp_i}/{cp_count}") + print(f"Processing cash plan {cp_i}/{cp_count}") cp_i += 1 with transaction.atomic(): if not cp.payment_items.exists(): @@ -269,6 +269,35 @@ def migrate_cash_plan_to_payment_plan() -> None: ticket_sensitive_details.save() create_payment_plan_snapshot_data(pp) + + pp.update_population_count_fields() + + payments = pp.eligible_payments.aggregate( + total_entitled_quantity=Coalesce(Sum("entitlement_quantity"), Decimal(0.0)), + total_entitled_quantity_usd=Coalesce(Sum("entitlement_quantity_usd"), Decimal(0.0)), + total_delivered_quantity=Coalesce(Sum("delivered_quantity"), Decimal(0.0)), + total_delivered_quantity_usd=Coalesce(Sum("delivered_quantity_usd"), Decimal(0.0)), + ) + + pp.total_entitled_quantity = payments.get("total_entitled_quantity", 0.00) + pp.total_entitled_quantity_usd = payments.get("total_entitled_quantity_usd", 0.00) + pp.total_delivered_quantity = payments.get("total_delivered_quantity", 0.00) + pp.total_delivered_quantity_usd = payments.get("total_delivered_quantity_usd", 0.00) + + pp.total_undelivered_quantity = pp.total_entitled_quantity - pp.total_delivered_quantity + pp.total_undelivered_quantity_usd = pp.total_entitled_quantity_usd - pp.total_delivered_quantity_usd + + pp.save( + update_fields=[ + "total_entitled_quantity", + "total_entitled_quantity_usd", + "total_delivered_quantity", + "total_delivered_quantity_usd", + "total_undelivered_quantity", + "total_undelivered_quantity_usd", + ] + ) + tp_counter += 1 cp.is_migrated_to_payment_plan = True @@ -276,5 +305,5 @@ def migrate_cash_plan_to_payment_plan() -> None: def migrate_cash_assist_models() -> None: - logger.info("***Migrating Cash Assist models to Payment models***") + print("***Migrating Cash Assist models to Payment models***") migrate_cash_plan_to_payment_plan() diff --git a/tests/unit/one_time_scripts/test_migrate_cash_assist_models.py b/tests/unit/one_time_scripts/test_migrate_cash_assist_models.py index 4107905045..659890a199 100644 --- a/tests/unit/one_time_scripts/test_migrate_cash_assist_models.py +++ b/tests/unit/one_time_scripts/test_migrate_cash_assist_models.py @@ -336,14 +336,16 @@ def test_migrate(self) -> None: assert pp1.dispersion_end_date == datetime.date(2021, 1, 2) assert pp1.status_date == datetime.datetime(2021, 1, 3, tzinfo=utc) assert pp1.exchange_rate == self.cash_plan.exchange_rate - assert pp1.total_entitled_quantity == self.cash_plan.total_entitled_quantity - assert pp1.total_entitled_quantity_usd == self.cash_plan.total_entitled_quantity_usd + + assert pp1.total_entitled_quantity == self.pr1.entitlement_quantity + assert pp1.total_entitled_quantity_usd == self.pr1.entitlement_quantity_usd assert pp1.total_entitled_quantity_revised == self.cash_plan.total_entitled_quantity_revised assert pp1.total_entitled_quantity_revised_usd == self.cash_plan.total_entitled_quantity_revised_usd - assert pp1.total_delivered_quantity == self.cash_plan.total_delivered_quantity - assert pp1.total_delivered_quantity_usd == self.cash_plan.total_delivered_quantity_usd - assert pp1.total_undelivered_quantity == self.cash_plan.total_undelivered_quantity - assert pp1.total_undelivered_quantity_usd == self.cash_plan.total_undelivered_quantity_usd + assert pp1.total_delivered_quantity == self.pr1.delivered_quantity + assert pp1.total_delivered_quantity_usd == self.pr1.delivered_quantity_usd + assert pp1.total_undelivered_quantity == pp1.total_entitled_quantity - self.pr1.delivered_quantity + assert pp1.total_undelivered_quantity_usd == pp1.total_entitled_quantity_usd - self.pr1.delivered_quantity_usd + assert pp1.internal_data == { "name": self.cash_plan.name, "ca_hash_id": str(self.cash_plan.ca_hash_id),