Skip to content

fix: POS Closing Entry, reload invoices when changing dates#54106

Open
devdiogenes wants to merge 1 commit intofrappe:developfrom
devdiogenes:develop-26-312
Open

fix: POS Closing Entry, reload invoices when changing dates#54106
devdiogenes wants to merge 1 commit intofrappe:developfrom
devdiogenes:develop-26-312

Conversation

@devdiogenes
Copy link
Copy Markdown
Contributor

For simulate, I have:

  • 1 POS Opening Entry having period_start_date "06/04/2026 00:00:00"
  • 1 POS Invoice having posting_time "01:17:58"
  • 1 POS Invoice having posting_time "17:18:10"
  • 1 Draft POS Closing Entry linked to this POS Opening Entry.

The problem is: If the POS Invoices is already loaded in POS Closing Entry but we want to change the period_end_date to some time between the two invoices, they don't reload the invoices, and we still seeing the two invoices.

Before my changes:

2026-04-07.09-31-15.mp4

After my changes:

2026-04-07.09-31-46.mp4

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 2026

📝 Walkthrough

Walkthrough

The pos_closing_entry.js file is refactored to introduce a shared invoice-reloading handler. The existing pos_opening_entry field handler is simplified to delegate to a new reload_invoices handler that encapsulates the workflow of freezing the UI, setting opening amounts, and fetching invoices. Two additional field event handlers are added for period_start_date and period_end_date that also invoke the shared reload_invoices handler, enabling the same invoice reloading workflow to execute when these dates are modified.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing POS Closing Entry to reload invoices when dates are changed, which directly matches the changeset's purpose.
Description check ✅ Passed The description provides a concrete reproduction scenario with specific details about the bug (unchanged invoice list when period_end_date changes) and demonstrates the fix with before/after screenshots, directly addressing the changeset's functionality.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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
Contributor

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.js (1)

59-72: ⚠️ Potential issue | 🟠 Major

Guard reload_invoices against concurrent execution and promise rejection.

The current implementation has two reliability issues:

  • Rapid changes to period_start_date, period_end_date, or user can trigger overlapping reloads, causing reset_values and append operations to interleave and produce stale or duplicated totals.
  • If set_opening_amounts or get_invoices fails, the promise chain breaks and frappe.dom.unfreeze() never runs, leaving the UI frozen.

Add an overlap guard and attach .finally() to ensure cleanup runs regardless of success or failure:

Proposed fix
 	reload_invoices: function (frm) {
 		if (
 			frm.doc.pos_opening_entry &&
 			frm.doc.period_start_date &&
 			frm.doc.period_end_date &&
 			frm.doc.user
 		) {
+			if (frm.__reloading_invoices) {
+				frm.__reload_invoices_pending = true;
+				return;
+			}
+
+			frm.__reloading_invoices = true;
 			reset_values(frm);
-			frappe.run_serially([
+			return frappe
+				.run_serially([
 				() => frappe.dom.freeze(__("Loading Invoices! Please Wait...")),
 				() => frm.trigger("set_opening_amounts"),
 				() => frm.trigger("get_invoices"),
-				() => frappe.dom.unfreeze(),
-			]);
+				])
+				.finally(() => {
+					frappe.dom.unfreeze();
+					frm.__reloading_invoices = false;
+					if (frm.__reload_invoices_pending) {
+						frm.__reload_invoices_pending = false;
+						frm.trigger("reload_invoices");
+					}
+				});
 		}
 	},
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.js` around lines
59 - 72, The reload_invoices function can run concurrently and leave the UI
frozen on error; add an overlap guard (e.g., a boolean like
this._reloadingInvoices) at the start of reload_invoices to return early if a
reload is already in progress, set it true before starting and false when
finished, and run the serial tasks inside a Promise chain that always calls
frappe.dom.unfreeze() in a .finally() block so unfreeze runs even if
frm.trigger("set_opening_amounts") or frm.trigger("get_invoices") rejects;
ensure reset_values remains before the guard exit and clear the guard in the
finally along with unfreeze.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.js`:
- Around line 59-72: The reload_invoices function can run concurrently and leave
the UI frozen on error; add an overlap guard (e.g., a boolean like
this._reloadingInvoices) at the start of reload_invoices to return early if a
reload is already in progress, set it true before starting and false when
finished, and run the serial tasks inside a Promise chain that always calls
frappe.dom.unfreeze() in a .finally() block so unfreeze runs even if
frm.trigger("set_opening_amounts") or frm.trigger("get_invoices") rejects;
ensure reset_values remains before the guard exit and clear the guard in the
finally along with unfreeze.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 03e64bc4-ae5d-462a-b29f-a1661d150254

📥 Commits

Reviewing files that changed from the base of the PR and between 6da5dff and ef2f9b7.

📒 Files selected for processing (1)
  • erpnext/accounts/doctype/pos_closing_entry/pos_closing_entry.js

@diptanilsaha
Copy link
Copy Markdown
Member

The problem is: If the POS Invoices is already loaded in POS Closing Entry but we want to change the period_end_date to some time between the two invoices, they don't reload the invoices, and we still seeing the two invoices.

What is the use case of changing the Period End Date?

@devdiogenes
Copy link
Copy Markdown
Contributor Author

The problem is: If the POS Invoices is already loaded in POS Closing Entry but we want to change the period_end_date to some time between the two invoices, they don't reload the invoices, and we still seeing the two invoices.

What is the use case of changing the Period End Date?

Users shouldn’t be limited to doing only one closing per day. For example, they might want to do one in the morning and another in the afternoon.

Also, if ERPNext allows users to edit this end date, I believe it should work correctly when it is changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants