-
Notifications
You must be signed in to change notification settings - Fork 9.9k
fix: show company-details popup only for the targeted print format/le… #50404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: show company-details popup only for the targeted print format/le… #50404
Conversation
📝 WalkthroughWalkthroughA guard condition has been added to the Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
erpnext/public/js/print.js (2)
7-8: Hardcoded configuration reduces maintainability.The allowed print formats and letterheads are hardcoded, requiring code changes whenever new formats need to be added to the allowlist. This couples configuration with implementation.
Consider moving these to a configurable location (e.g., DocType settings, system settings, or at minimum, a well-documented constant at the top of the file):
+// Configuration: Print formats and letterheads that should trigger the company details dialog +const COMPANY_DETAILS_PRINT_FORMATS = ["Sales Invoice Standard", "Sales Invoice with Item Image"]; +const COMPANY_DETAILS_LETTERHEADS = ["Company Letterhead", "Company Letterhead - Grey"]; + let beforePrintHandled = false; frappe.realtime.on("sales_invoice_before_print", (data) => { let print_format = $('input[data-fieldname="print_format"]').val(); let letterhead = $('input[data-fieldname="letterhead"]').val(); - let allowed_print_formats = ["Sales Invoice Standard", "Sales Invoice with Item Image"]; - let allowed_letterheads = ["Company Letterhead", "Company Letterhead - Grey"]; + let allowed_print_formats = COMPANY_DETAILS_PRINT_FORMATS; + let allowed_letterheads = COMPANY_DETAILS_LETTERHEADS;Even better, consider making this configurable through a DocType setting so administrators can manage it without code changes.
4-12: Additional robustness suggestions.Consider these improvements to make the guard more robust:
Case-insensitive matching: The current
includes()check is case-sensitive. If the actual values have different casing, they won't match.Trim whitespace: Input values might have leading/trailing whitespace.
Early validation: Check if the elements exist before querying.
let print_format = $('input[data-fieldname="print_format"]').val(); let letterhead = $('input[data-fieldname="letterhead"]').val(); + + // Normalize values: trim and convert to lowercase for case-insensitive comparison + print_format = (print_format || "").trim().toLowerCase(); + letterhead = (letterhead || "").trim().toLowerCase(); - let allowed_print_formats = ["Sales Invoice Standard", "Sales Invoice with Item Image"]; - let allowed_letterheads = ["Company Letterhead", "Company Letterhead - Grey"]; + // Convert allowed values to lowercase for comparison + let allowed_print_formats = ["Sales Invoice Standard", "Sales Invoice with Item Image"].map(f => f.toLowerCase()); + let allowed_letterheads = ["Company Letterhead", "Company Letterhead - Grey"].map(l => l.toLowerCase()); if (!allowed_print_formats.includes(print_format) && !allowed_letterheads.includes(letterhead)) { return; }
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
erpnext/public/js/print.js(1 hunks)
⏰ 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: Summary
🔇 Additional comments (2)
erpnext/public/js/print.js (2)
10-12: Verify the guard condition logic matches requirements.The condition
!allowed_print_formats.includes(print_format) && !allowed_letterheads.includes(letterhead)will show the dialog when either the print format or letterhead matches the allowed lists. Please confirm this is the intended behavior, as the PR description mentions "show company-details popup only for the targeted print format/le[tterhead]."If the requirement is to show the dialog only when both match (more restrictive), the logic should be:
- if (!allowed_print_formats.includes(print_format) && !allowed_letterheads.includes(letterhead)) { + if (!allowed_print_formats.includes(print_format) || !allowed_letterheads.includes(letterhead)) { return; }However, if showing the dialog when either matches is correct, consider adding a comment for clarity:
+ // Show dialog if either the print format OR letterhead is in the allowed lists if (!allowed_print_formats.includes(print_format) && !allowed_letterheads.includes(letterhead)) { return; }
4-5: Verify DOM selector timing and defensive checks for external framework fields.The print dialog exposes print_format and letterhead as standard controls in the print preview toolbar. However, these input fields are queried from the Frappe framework (not defined in the erpnext codebase) and may or may not exist in the DOM when the event fires.
The guard condition lacks defensive checks for undefined values. If the selectors fail to find elements,
.val()returnsundefined, and the condition silently skips showing the dialog. Recommend adding null coalescing:- let print_format = $('input[data-fieldname="print_format"]').val(); - let letterhead = $('input[data-fieldname="letterhead"]').val(); + let print_format = $('input[data-fieldname="print_format"]').val() || ""; + let letterhead = $('input[data-fieldname="letterhead"]').val() || "";Verify through manual testing that these input fields exist and are populated when the
sales_invoice_before_printevent fires.
Issue:
Before
Screen.Recording.2025-11-07.at.1.01.30.PM.mov
After
Screen.Recording.2025-11-07.at.12.58.59.PM.mov