Skip to content

Commit

Permalink
Merge pull request #4500 from unicef/staging
Browse files Browse the repository at this point in the history
Merge pull request #4499 from unicef/develop
  • Loading branch information
domdinicola authored Dec 3, 2024
2 parents 9fcf861 + 31c7b7a commit dc4970d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
51 changes: 40 additions & 11 deletions src/hct_mis_api/one_time_scripts/migrate_cash_assist_models.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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"}
Expand All @@ -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:
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -269,12 +269,41 @@ 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
cp.save(update_fields=["is_migrated_to_payment_plan"])


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()
14 changes: 8 additions & 6 deletions tests/unit/one_time_scripts/test_migrate_cash_assist_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit dc4970d

Please sign in to comment.