|
38 | 38 | from hope.apps.payment.services.payment_plan_services import PaymentPlanService |
39 | 39 | from hope.apps.payment.utils import to_decimal |
40 | 40 | from hope.apps.payment.xlsx.xlsx_error import XlsxError |
| 41 | +from hope.apps.payment.xlsx.xlsx_payment_plan_base_service import XlsxPaymentPlanBaseService |
41 | 42 | from hope.apps.payment.xlsx.xlsx_payment_plan_export_per_fsp_service import XlsxPaymentPlanExportPerFspService |
42 | 43 | from hope.apps.payment.xlsx.xlsx_payment_plan_export_service import XlsxPaymentPlanExportService |
43 | 44 | from hope.apps.payment.xlsx.xlsx_payment_plan_import_service import XlsxPaymentPlanImportService |
@@ -328,6 +329,123 @@ def test_import_valid_file_with_reordered_required_columns(payment_plan, payment |
328 | 329 | assert payment_2.entitlement_quantity == Decimal("222.00") |
329 | 330 |
|
330 | 331 |
|
| 332 | +def test_entitlement_import_updates_only_modified_rows_for_household_program( |
| 333 | + payment_plan, |
| 334 | + payments, |
| 335 | + django_assert_num_queries, |
| 336 | +): |
| 337 | + payment_plan.exchange_rate = Decimal("1.00") |
| 338 | + payment_plan.save(update_fields=["exchange_rate"]) |
| 339 | + PaymentHouseholdSnapshot.objects.all().delete() |
| 340 | + create_payment_plan_snapshot_data(payment_plan) |
| 341 | + |
| 342 | + payment_1, payment_2, payment_3 = list(payment_plan.eligible_payments.order_by("unicef_id")) |
| 343 | + original_amount_3 = payment_3.entitlement_quantity |
| 344 | + original_usd_3 = payment_3.entitlement_quantity_usd |
| 345 | + original_date_3 = payment_3.entitlement_date |
| 346 | + |
| 347 | + export_service = XlsxPaymentPlanExportService(payment_plan) |
| 348 | + wb = export_service.generate_workbook() |
| 349 | + ws = wb.active |
| 350 | + payment_id_col = export_service.headers.index(XlsxPaymentPlanBaseService.COLUMN_PAYMENT_ID) + 1 |
| 351 | + entitlement_col = export_service.headers.index(XlsxPaymentPlanBaseService.COLUMN_ENTITLEMENT_QUANTITY) + 1 |
| 352 | + |
| 353 | + assert ws.cell(row=2, column=payment_id_col).value == str(payment_1.unicef_id) |
| 354 | + assert ws.cell(row=3, column=payment_id_col).value == str(payment_2.unicef_id) |
| 355 | + assert ws.cell(row=4, column=payment_id_col).value == str(payment_3.unicef_id) |
| 356 | + |
| 357 | + ws.cell(row=2, column=entitlement_col).value = "111.00" |
| 358 | + ws.cell(row=3, column=entitlement_col).value = "222.00" |
| 359 | + |
| 360 | + with NamedTemporaryFile() as tmp: |
| 361 | + wb.save(tmp.name) |
| 362 | + tmp.seek(0) |
| 363 | + file = BytesIO(tmp.read()) |
| 364 | + |
| 365 | + import_service = XlsxPaymentPlanImportService(payment_plan, file) |
| 366 | + import_service.open_workbook() |
| 367 | + import_service.validate() |
| 368 | + assert import_service.errors == [] |
| 369 | + |
| 370 | + # bulk_update of entitlements + signature_hash refresh path; pinned to catch N+1 regressions |
| 371 | + with django_assert_num_queries(7): |
| 372 | + import_service.import_payment_list() |
| 373 | + |
| 374 | + payment_1.refresh_from_db() |
| 375 | + payment_2.refresh_from_db() |
| 376 | + payment_3.refresh_from_db() |
| 377 | + |
| 378 | + assert payment_1.entitlement_quantity == Decimal("111.00") |
| 379 | + assert payment_2.entitlement_quantity == Decimal("222.00") |
| 380 | + assert payment_1.entitlement_quantity_usd == Decimal("111.00") |
| 381 | + assert payment_2.entitlement_quantity_usd == Decimal("222.00") |
| 382 | + assert payment_3.entitlement_quantity == original_amount_3 |
| 383 | + assert payment_3.entitlement_quantity_usd == original_usd_3 |
| 384 | + assert payment_3.entitlement_date == original_date_3 |
| 385 | + |
| 386 | + |
| 387 | +def test_entitlement_import_updates_only_modified_rows_for_social_worker_program( |
| 388 | + payment_plan, |
| 389 | + payments, |
| 390 | + django_assert_num_queries, |
| 391 | +): |
| 392 | + program = payment_plan.program |
| 393 | + program.beneficiary_group.master_detail = False |
| 394 | + program.beneficiary_group.save() |
| 395 | + program.data_collecting_type.type = DataCollectingType.Type.SOCIAL |
| 396 | + program.save() |
| 397 | + assert payment_plan.is_social_worker_program is True |
| 398 | + |
| 399 | + payment_plan.exchange_rate = Decimal("1.00") |
| 400 | + payment_plan.save(update_fields=["exchange_rate"]) |
| 401 | + PaymentHouseholdSnapshot.objects.all().delete() |
| 402 | + create_payment_plan_snapshot_data(payment_plan) |
| 403 | + |
| 404 | + payment_1, payment_2, payment_3 = list(payment_plan.eligible_payments.order_by("unicef_id")) |
| 405 | + original_amount_3 = payment_3.entitlement_quantity |
| 406 | + original_usd_3 = payment_3.entitlement_quantity_usd |
| 407 | + original_date_3 = payment_3.entitlement_date |
| 408 | + |
| 409 | + export_service = XlsxPaymentPlanExportService(payment_plan) |
| 410 | + wb = export_service.generate_workbook() |
| 411 | + ws = wb.active |
| 412 | + payment_id_col = export_service.headers.index(XlsxPaymentPlanBaseService.COLUMN_PAYMENT_ID) + 1 |
| 413 | + entitlement_col = export_service.headers.index(XlsxPaymentPlanBaseService.COLUMN_ENTITLEMENT_QUANTITY) + 1 |
| 414 | + |
| 415 | + assert ws.cell(row=2, column=payment_id_col).value == str(payment_1.unicef_id) |
| 416 | + assert ws.cell(row=3, column=payment_id_col).value == str(payment_2.unicef_id) |
| 417 | + assert ws.cell(row=4, column=payment_id_col).value == str(payment_3.unicef_id) |
| 418 | + |
| 419 | + ws.cell(row=2, column=entitlement_col).value = "111.00" |
| 420 | + ws.cell(row=3, column=entitlement_col).value = "222.00" |
| 421 | + |
| 422 | + with NamedTemporaryFile() as tmp: |
| 423 | + wb.save(tmp.name) |
| 424 | + tmp.seek(0) |
| 425 | + file = BytesIO(tmp.read()) |
| 426 | + |
| 427 | + import_service = XlsxPaymentPlanImportService(payment_plan, file) |
| 428 | + import_service.open_workbook() |
| 429 | + import_service.validate() |
| 430 | + assert import_service.errors == [] |
| 431 | + |
| 432 | + # bulk_update of entitlements + signature_hash refresh path; pinned to catch N+1 regressions |
| 433 | + with django_assert_num_queries(7): |
| 434 | + import_service.import_payment_list() |
| 435 | + |
| 436 | + payment_1.refresh_from_db() |
| 437 | + payment_2.refresh_from_db() |
| 438 | + payment_3.refresh_from_db() |
| 439 | + |
| 440 | + assert payment_1.entitlement_quantity == Decimal("111.00") |
| 441 | + assert payment_2.entitlement_quantity == Decimal("222.00") |
| 442 | + assert payment_1.entitlement_quantity_usd == Decimal("111.00") |
| 443 | + assert payment_2.entitlement_quantity_usd == Decimal("222.00") |
| 444 | + assert payment_3.entitlement_quantity == original_amount_3 |
| 445 | + assert payment_3.entitlement_quantity_usd == original_usd_3 |
| 446 | + assert payment_3.entitlement_date == original_date_3 |
| 447 | + |
| 448 | + |
331 | 449 | def test_validate_headers_resolves_positions_when_mapping_empty(payment_plan, xlsx_valid_file): |
332 | 450 | service = XlsxPaymentPlanImportService(payment_plan, xlsx_valid_file) |
333 | 451 | service.open_workbook() |
|
0 commit comments