Skip to content

fix: dynamic due date when payment terms are fetched from order#23

Closed
ljain112 wants to merge 5 commits into
developfrom
fix-due-date-orders
Closed

fix: dynamic due date when payment terms are fetched from order#23
ljain112 wants to merge 5 commits into
developfrom
fix-due-date-orders

Conversation

@ljain112

@ljain112 ljain112 commented Jul 30, 2025

Copy link
Copy Markdown
Owner

Please provide enough information so that others can review your pull request:

Explain the details for making this change. What existing problem does the pull request solve?

Screenshots/GIFs

Summary by CodeRabbit

  • Bug Fixes
    • Improved accuracy of due dates and discount dates in payment schedules by recalculating them based on configurable criteria and relevant posting dates when automatically fetching payment terms.
  • New Features
    • Added new fields to payment schedules allowing more detailed configuration of due date calculation, credit duration, and discount validity.
  • User Experience
    • Enhanced user prompts and automatic clearing of related fields when due dates or payment terms are changed to maintain consistency.
  • Tests
    • Centralized configuration of the "automatically_fetch_payment_terms" setting in tests using decorators, removing manual toggling within test methods for consistency and clarity.

@coderabbitai

coderabbitai Bot commented Jul 30, 2025

Copy link
Copy Markdown

Walkthrough

The method fetch_payment_terms_from_order within the AccountsController class was updated to calculate the due_date and discount_date for payment schedules based on specific payment term fields fetched from the Payment Term doctype, using a computed posting_date. The function get_due_date was extended to accept a default_date parameter. Additionally, multiple test files were updated to replace direct calls to enable or disable the "automatically_fetch_payment_terms" setting with the @IntegrationTestCase.change_settings decorator, centralizing configuration at the test method level. The Payment Schedule DocType and its type declarations were extended with new fields related to due date and discount validity calculations. The client-side transaction controller JS was modified to clear related fields when due dates or discount dates are changed to maintain consistency.

Changes

Cohort / File(s) Change Summary
AccountsController Payment Terms Logic
erpnext/controllers/accounts_controller.py
Enhanced fetch_payment_terms_from_order to recalculate due_date and discount_date in payment schedules based on due_date_based_on, credit_days, credit_months, discount_validity_based_on, and discount_validity fields using a computed posting_date. Extended get_due_date to accept a default_date parameter. Added new fields to the payment term details returned by get_payment_term_details.
Payment Schedule DocType and Type Declarations
erpnext/accounts/doctype/payment_schedule/payment_schedule.json, erpnext/accounts/doctype/payment_schedule/payment_schedule.py
Added new fields: due_date_based_on, credit_days, credit_months, discount_validity_based_on, and discount_validity to the Payment Schedule DocType JSON schema and Python type declarations. Adjusted field order for layout consistency.
Test Decorator Refactor for Automatically Fetch Payment Terms
erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py, erpnext/buying/doctype/purchase_order/test_purchase_order.py, erpnext/selling/doctype/sales_order/test_sales_order.py, erpnext/stock/doctype/delivery_note/test_delivery_note.py, erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
Replaced explicit calls to enable/disable "automatically_fetch_payment_terms" with the @IntegrationTestCase.change_settings decorator on relevant test methods. Removed the helper function automatically_fetch_payment_terms and related imports. Updated due date comparison logic in sales order test to use recalculated due dates. Test logic and assertions remain unchanged.
Transaction Controller JS Updates
erpnext/public/js/controllers/transaction.js
Added discount_date method to clear discount-related fields when discount date is changed. Updated due_date and payment_term methods to clear related due date and credit term fields when these are changed to maintain consistency and avoid infinite loops. Added user confirmation dialog when due date is changed on main document if payment terms or schedules exist.
Payment Terms Template Detail DocType JSON
erpnext/accounts/doctype/payment_terms_template_detail/payment_terms_template_detail.json
Removed mandatory dependency on discount validity field. Added "row_format": "Dynamic" property to the DocType JSON schema.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Poem

A hop through code, a due date anew,
Payment terms fetched with a bunny’s view.
Dates aligned with careful care,
Credit days and months now fair.
In the ledger’s warren, order’s clear—
The rabbit smiles, review is near! 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b56ef91 and 81dd6d3.

📒 Files selected for processing (4)
  • erpnext/accounts/doctype/payment_schedule/payment_schedule.json (3 hunks)
  • erpnext/accounts/doctype/payment_terms_template_detail/payment_terms_template_detail.json (2 hunks)
  • erpnext/controllers/accounts_controller.py (4 hunks)
  • erpnext/public/js/controllers/transaction.js (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • erpnext/accounts/doctype/payment_terms_template_detail/payment_terms_template_detail.json
🚧 Files skipped from review as they are similar to previous changes (3)
  • erpnext/accounts/doctype/payment_schedule/payment_schedule.json
  • erpnext/controllers/accounts_controller.py
  • erpnext/public/js/controllers/transaction.js
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Patch Test
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-due-date-orders

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ljain112 ljain112 force-pushed the fix-due-date-orders branch from 2254e49 to dd78aa4 Compare July 31, 2025 07:42

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (1)
erpnext/accounts/doctype/payment_schedule/payment_schedule.json (1)

22-25: Minor ordering nit – consider showing “Discount Type” after “Discount” in the UI as well

field_order now places discount_type after discount, but the DocField definitions still appear with discount_type first (Lines 125-137 vs 133-137). Not functionally wrong, yet aligning definition order with field_order reduces cognitive load when reading the JSON.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7de7a10 and 9744403.

📒 Files selected for processing (4)
  • erpnext/accounts/doctype/payment_schedule/payment_schedule.json (3 hunks)
  • erpnext/accounts/doctype/payment_schedule/payment_schedule.py (1 hunks)
  • erpnext/controllers/accounts_controller.py (4 hunks)
  • erpnext/selling/doctype/sales_order/test_sales_order.py (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • erpnext/accounts/doctype/payment_schedule/payment_schedule.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • erpnext/selling/doctype/sales_order/test_sales_order.py
  • erpnext/controllers/accounts_controller.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Patch Test
🔇 Additional comments (1)
erpnext/accounts/doctype/payment_schedule/payment_schedule.json (1)

13-18: Field-order update aligns with the new calculation logic

Moving invoice_portion ahead of the newly-added credit & due-date fields keeps related attributes grouped and should make the form easier to scan.

Comment on lines +182 to +188
"fetch_from": "payment_term.due_date_based_on",
"fetch_if_empty": 1,
"fieldname": "due_date_based_on",
"fieldtype": "Select",
"label": "Due Date Based On",
"options": "\nDay(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove the leading newline in options to avoid an empty select entry

The first character in the options string is \n, which introduces a blank option in the drop-down:

-   "options": "\nDay(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"
+   "options": "Day(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"

Having an empty option can break validation when the field is marked mandatory or when downstream code assumes a non-blank value.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"fetch_from": "payment_term.due_date_based_on",
"fetch_if_empty": 1,
"fieldname": "due_date_based_on",
"fieldtype": "Select",
"label": "Due Date Based On",
"options": "\nDay(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"
},
"fetch_from": "payment_term.due_date_based_on",
"fetch_if_empty": 1,
"fieldname": "due_date_based_on",
"fieldtype": "Select",
"label": "Due Date Based On",
"options": "Day(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"
},
🤖 Prompt for AI Agents
In erpnext/accounts/doctype/payment_schedule/payment_schedule.json between lines
182 and 188, the "options" field for "due_date_based_on" starts with a newline
character, causing an empty option in the select dropdown. Remove the leading
newline so the options string begins directly with the first option to prevent
an empty select entry and avoid validation issues.

Comment on lines +190 to +202
"fetch_if_empty": 1,
"fieldname": "credit_days",
"fieldtype": "Int",
"label": "Credit Days",
"non_negative": 1
},
{
"fetch_if_empty": 1,
"fieldname": "credit_months",
"fieldtype": "Int",
"label": "Credit Months",
"non_negative": 1
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Replace non-standard non_negative flag with the supported min property

Frappe DocFields don’t recognise non_negative; the framework silently ignores unknown keys, so the restriction will not be enforced.

-   "fieldtype": "Int",
-   "label": "Credit Days",
-   "non_negative": 1
+   "fieldtype": "Int",
+   "label": "Credit Days",
+   "min": 0

Apply the same change to credit_months (Lines 197-202) and keep the user from entering negative values.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"fetch_if_empty": 1,
"fieldname": "credit_days",
"fieldtype": "Int",
"label": "Credit Days",
"non_negative": 1
},
{
"fetch_if_empty": 1,
"fieldname": "credit_months",
"fieldtype": "Int",
"label": "Credit Months",
"non_negative": 1
},
{
"fetch_if_empty": 1,
"fieldname": "credit_days",
"fieldtype": "Int",
"label": "Credit Days",
"min": 0
},
{
"fetch_if_empty": 1,
"fieldname": "credit_months",
"fieldtype": "Int",
"label": "Credit Months",
"min": 0
},
🤖 Prompt for AI Agents
In erpnext/accounts/doctype/payment_schedule/payment_schedule.json between lines
190 and 202, replace the unsupported "non_negative" flag with the supported
"min" property set to 0 for both "credit_days" and "credit_months" fields. This
change will enforce the restriction preventing users from entering negative
values as intended.

Comment on lines +204 to +210
"fetch_from": "payment_term.discount_validity_based_on",
"fetch_if_empty": 1,
"fieldname": "discount_validity_based_on",
"fieldtype": "Select",
"label": "Discount Validity Based On",
"options": "\nDay(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Duplicate newline in discount_validity_based_on.options – same issue as above

-   "options": "\nDay(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"
+   "options": "Day(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"fetch_from": "payment_term.discount_validity_based_on",
"fetch_if_empty": 1,
"fieldname": "discount_validity_based_on",
"fieldtype": "Select",
"label": "Discount Validity Based On",
"options": "\nDay(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"
},
"fetch_from": "payment_term.discount_validity_based_on",
"fetch_if_empty": 1,
"fieldname": "discount_validity_based_on",
"fieldtype": "Select",
"label": "Discount Validity Based On",
"options": "Day(s) after invoice date\nDay(s) after the end of the invoice month\nMonth(s) after the end of the invoice month"
},
🤖 Prompt for AI Agents
In erpnext/accounts/doctype/payment_schedule/payment_schedule.json around lines
204 to 210, the "options" field for "discount_validity_based_on" contains a
duplicate leading newline character. Remove the extra newline at the start of
the options string so that the options begin immediately without an empty line.

Comment on lines +212 to 217
"fetch_from": "payment_term.discount_validity",
"fetch_if_empty": 1,
"fieldname": "discount_validity",
"fieldtype": "Int",
"label": "Discount Validity"
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enforce non-negative value on discount_validity as well

Follow the same pattern as credit_days / credit_months to keep data clean:

-   "fieldtype": "Int",
-   "label": "Discount Validity"
+   "fieldtype": "Int",
+   "label": "Discount Validity",
+   "min": 0
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"fetch_from": "payment_term.discount_validity",
"fetch_if_empty": 1,
"fieldname": "discount_validity",
"fieldtype": "Int",
"label": "Discount Validity"
}
"fetch_from": "payment_term.discount_validity",
"fetch_if_empty": 1,
"fieldname": "discount_validity",
"fieldtype": "Int",
"label": "Discount Validity",
"min": 0
}
🤖 Prompt for AI Agents
In erpnext/accounts/doctype/payment_schedule/payment_schedule.json around lines
212 to 217, the discount_validity field lacks validation to prevent negative
values. Add a validation rule similar to credit_days and credit_months by
including a "depends_on" or "validate" property that enforces discount_validity
to be zero or positive, ensuring data integrity.

@ljain112 ljain112 force-pushed the fix-due-date-orders branch from b56ef91 to 81dd6d3 Compare July 31, 2025 12:21
@ljain112 ljain112 closed this Jul 31, 2025
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.

1 participant