Skip to content

fix: post restructure charges capitalization#1239

Merged
Nihantra-Patel merged 8 commits into
frappe:developfrom
Nihantra-Patel:restructure-charge-capitalization-flow
May 28, 2026
Merged

fix: post restructure charges capitalization#1239
Nihantra-Patel merged 8 commits into
frappe:developfrom
Nihantra-Patel:restructure-charge-capitalization-flow

Conversation

@Nihantra-Patel
Copy link
Copy Markdown
Member

@Nihantra-Patel Nihantra-Patel commented May 25, 2026

Summary by CodeRabbit

  • New Features

    • Mark charges as post-restructure on sales invoices.
    • Auto-create principal-capitalization repayments when post-restructure charges are invoiced.
  • Improvements

    • Allow payable charges for principal capitalization during restructures; refine repayment allocation, GL entry flow, and payment account resolution.
  • Tests

    • Expect an additional repayment record and verify post-restructure invoice creation and paid status.
  • Chores

    • Add migration patch to add the new invoice field.

Review Change Stack

@Nihantra-Patel Nihantra-Patel marked this pull request as ready for review May 25, 2026 18:58
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 25, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

This PR adds a read-only checkbox field is_post_restructure_charge to Sales Invoice and registers a migration patch. The charge invoice creation helper accepts an optional flag to set this field. Restructure logic captures created invoices and creates Principal Capitalization Loan Repayments for invoice totals, extending repayment creation to accept a list of charges. Loan Repayment logic and GL generation are updated to handle Principal Capitalization during restructures. Tests are updated to expect the additional repayment and verify the post-restructure invoice is paid.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: post restructure charges capitalization' directly describes the primary change: enabling charges to be capitalized as part of the loan restructure process.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lending/loan_management/doctype/loan_repayment/loan_repayment.py`:
- Around line 127-130: The validation error message for payable charges is
stale: when checking self.repayment_type (allowed values "Charge Payment" or
("Charges Waiver", "Charges Capitalization", "Principal Capitalization") with
self.loan_restructure) update the frappe.throw message so it mentions "Principal
Capitalization" as an allowed case (match the allowed list in the if condition)
— locate the check around self.repayment_type in loan_repayment.py and modify
the thrown string passed to frappe.throw to include Principal Capitalization.

In `@lending/loan_management/doctype/loan_restructure/test_loan_restructure.py`:
- Around line 267-269: The test currently queries Sales Invoice by loan,
docstatus and value_date which can match the wrong invoice; update the
frappe.db.get_value call in test_loan_restructure.py (the sales_invoice lookup)
to include the is_post_restructure_charge filter (e.g., add
"is_post_restructure_charge": 1 to the filters) so the assertion targets the
post-restructure invoice deterministically and then assert flt(...) == 0 as
before.

In `@lending/patches/v16_0/add_is_post_restructure_charge_field.py`:
- Around line 10-15: The patch that adds the field with "fieldname":
"is_post_restructure_charge" creates it without the print_hide property, causing
inconsistency with the install-time definition; update the patch that constructs
this field (the dict containing "fieldname": "is_post_restructure_charge",
"label": "Is Post Restructure Charge", "fieldtype": "Check", "insert_after":
"loan_repayment", "read_only": 1) to include "print_hide": 1 so upgraded sites
match the behavior in lending/install.py where the same field is defined with
print_hide=1.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7d8b0456-2762-4639-ad7c-2a4aaca87149

📥 Commits

Reviewing files that changed from the base of the PR and between e50d464 and 5d74936.

📒 Files selected for processing (7)
  • lending/install.py
  • lending/loan_management/doctype/loan_disbursement/loan_disbursement.py
  • lending/loan_management/doctype/loan_repayment/loan_repayment.py
  • lending/loan_management/doctype/loan_restructure/loan_restructure.py
  • lending/loan_management/doctype/loan_restructure/test_loan_restructure.py
  • lending/patches.txt
  • lending/patches/v16_0/add_is_post_restructure_charge_field.py

Comment thread lending/loan_management/doctype/loan_repayment/loan_repayment.py Outdated
Comment thread lending/loan_management/doctype/loan_restructure/test_loan_restructure.py Outdated
Comment thread lending/patches/v16_0/add_is_post_restructure_charge_field.py Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lending/loan_management/doctype/loan_repayment/loan_repayment.py`:
- Around line 1540-1543: The current conditional routes all restructure-linked
"Principal Capitalization" repayments through allocate_charges, but
make_loan_repayment_for_adjustment() can create "Principal Capitalization"
repayments with no payable_charges which causes allocate_charges to produce no
repayment_details and prevents update_demands() from clearing overdue principal;
change the conditional in loan_repayment.py (the block that checks
self.repayment_type and self.loan_restructure) to only call allocate_charges
when there are payable charges to allocate (e.g., require self.payable_charges
or similar non-empty collection) or explicitly exclude "Principal
Capitalization" cases that have no payable_charges so that repayments created by
make_loan_repayment_for_adjustment() bypass allocate_charges and still let
update_demands() update/clear principal demands.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 23fd0af9-3ea9-4e34-b55a-1591ff1a4f17

📥 Commits

Reviewing files that changed from the base of the PR and between 02cace5 and a5c4732.

📒 Files selected for processing (1)
  • lending/loan_management/doctype/loan_repayment/loan_repayment.py

Comment thread lending/loan_management/doctype/loan_repayment/loan_repayment.py
@Nihantra-Patel Nihantra-Patel force-pushed the restructure-charge-capitalization-flow branch from a5c4732 to 6713d25 Compare May 26, 2026 16:12
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 26, 2026

Codecov Report

❌ Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.33%. Comparing base (1a18f1c) to head (a33f16c).
⚠️ Report is 4 commits behind head on develop.

Files with missing lines Patch % Lines
...anagement/doctype/loan_repayment/loan_repayment.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1239      +/-   ##
===========================================
- Coverage    76.37%   76.33%   -0.04%     
===========================================
  Files          142      142              
  Lines        10207    10245      +38     
===========================================
+ Hits          7796     7821      +25     
- Misses        2411     2424      +13     
Files with missing lines Coverage Δ
...ement/doctype/loan_restructure/loan_restructure.py 88.73% <100.00%> (+0.12%) ⬆️
.../doctype/loan_restructure/test_loan_restructure.py 100.00% <100.00%> (ø)
...anagement/doctype/loan_repayment/loan_repayment.py 81.50% <75.00%> (ø)

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Nihantra-Patel Nihantra-Patel merged commit 6b20426 into frappe:develop May 28, 2026
8 checks passed
Nihantra-Patel added a commit that referenced this pull request May 28, 2026
fix: post restructure charges capitalization (backport #1239)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants