refactor: move reposting logic to common controller
This commit is contained in:
parent
e922ec60eb
commit
68effd93bd
@ -11,9 +11,6 @@ from frappe.utils import add_days, cint, cstr, flt, formatdate, get_link_to_form
|
|||||||
|
|
||||||
import erpnext
|
import erpnext
|
||||||
from erpnext.accounts.deferred_revenue import validate_service_stop_date
|
from erpnext.accounts.deferred_revenue import validate_service_stop_date
|
||||||
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
|
||||||
get_accounting_dimensions,
|
|
||||||
)
|
|
||||||
from erpnext.accounts.doctype.loyalty_program.loyalty_program import (
|
from erpnext.accounts.doctype.loyalty_program.loyalty_program import (
|
||||||
get_loyalty_program_details_with_points,
|
get_loyalty_program_details_with_points,
|
||||||
validate_loyalty_points,
|
validate_loyalty_points,
|
||||||
@ -517,79 +514,34 @@ class SalesInvoice(SellingController):
|
|||||||
|
|
||||||
def on_update_after_submit(self):
|
def on_update_after_submit(self):
|
||||||
if hasattr(self, "repost_required"):
|
if hasattr(self, "repost_required"):
|
||||||
needs_repost = 0
|
fields_to_check = [
|
||||||
|
"additional_discount_account",
|
||||||
# Check if any field affecting accounting entry is altered
|
"cash_bank_account",
|
||||||
doc_before_update = self.get_doc_before_save()
|
"account_for_change_amount",
|
||||||
accounting_dimensions = get_accounting_dimensions() + ["cost_center", "project"]
|
"write_off_account",
|
||||||
|
"loyalty_redemption_account",
|
||||||
# Check if opening entry check updated
|
"unrealized_profit_loss_account",
|
||||||
if doc_before_update.get("is_opening") != self.is_opening:
|
]
|
||||||
needs_repost = 1
|
child_tables = {
|
||||||
|
"items": ("income_account", "expense_account", "discount_account"),
|
||||||
if not needs_repost:
|
"taxes": ("account_head",),
|
||||||
# Parent Level Accounts excluding party account
|
}
|
||||||
for field in (
|
self.needs_repost = self.check_if_fields_updated(fields_to_check, child_tables)
|
||||||
"additional_discount_account",
|
self.validate_deferred_accounting_before_repost()
|
||||||
"cash_bank_account",
|
|
||||||
"account_for_change_amount",
|
|
||||||
"write_off_account",
|
|
||||||
"loyalty_redemption_account",
|
|
||||||
"unrealized_profit_loss_account",
|
|
||||||
):
|
|
||||||
if doc_before_update.get(field) != self.get(field):
|
|
||||||
needs_repost = 1
|
|
||||||
break
|
|
||||||
|
|
||||||
# Check for parent accounting dimensions
|
|
||||||
for dimension in accounting_dimensions:
|
|
||||||
if doc_before_update.get(dimension) != self.get(dimension):
|
|
||||||
needs_repost = 1
|
|
||||||
break
|
|
||||||
|
|
||||||
# Check for child tables
|
|
||||||
if self.check_if_child_table_updated(
|
|
||||||
"items",
|
|
||||||
doc_before_update,
|
|
||||||
("income_account", "expense_account", "discount_account"),
|
|
||||||
accounting_dimensions,
|
|
||||||
):
|
|
||||||
needs_repost = 1
|
|
||||||
|
|
||||||
if self.check_if_child_table_updated(
|
|
||||||
"taxes", doc_before_update, ("account_head",), accounting_dimensions
|
|
||||||
):
|
|
||||||
needs_repost = 1
|
|
||||||
|
|
||||||
self.validate_accounts()
|
self.validate_accounts()
|
||||||
|
self.db_set("repost_required", self.needs_repost)
|
||||||
|
|
||||||
# validate if deferred revenue is enabled for any item
|
def validate_deferred_accounting_before_repost(self):
|
||||||
# Don't allow to update the invoice if deferred revenue is enabled
|
# validate if deferred revenue is enabled for any item
|
||||||
if needs_repost:
|
# Don't allow to update the invoice if deferred revenue is enabled
|
||||||
for item in self.get("items"):
|
if self.needs_repost:
|
||||||
if item.enable_deferred_revenue:
|
for item in self.get("items"):
|
||||||
frappe.throw(
|
if item.enable_deferred_revenue:
|
||||||
_(
|
frappe.throw(
|
||||||
"Deferred Revenue is enabled for item {0}. You cannot update the invoice after submission."
|
_(
|
||||||
).format(item.item_code)
|
"Deferred Revenue is enabled for item {0}. You cannot update the invoice after submission."
|
||||||
)
|
).format(item.item_code)
|
||||||
|
)
|
||||||
self.db_set("repost_required", needs_repost)
|
|
||||||
|
|
||||||
def check_if_child_table_updated(
|
|
||||||
self, child_table, doc_before_update, fields_to_check, accounting_dimensions
|
|
||||||
):
|
|
||||||
# Check if any field affecting accounting entry is altered
|
|
||||||
for index, item in enumerate(self.get(child_table)):
|
|
||||||
for field in fields_to_check:
|
|
||||||
if doc_before_update.get(child_table)[index].get(field) != item.get(field):
|
|
||||||
return True
|
|
||||||
|
|
||||||
for dimension in accounting_dimensions:
|
|
||||||
if doc_before_update.get(child_table)[index].get(dimension) != item.get(dimension):
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def repost_accounting_entries(self):
|
def repost_accounting_entries(self):
|
||||||
|
|||||||
@ -2186,6 +2186,44 @@ class AccountsController(TransactionBase):
|
|||||||
_("Select finance book for the item {0} at row {1}").format(item.item_code, item.idx)
|
_("Select finance book for the item {0} at row {1}").format(item.item_code, item.idx)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def check_if_fields_updated(self, fields_to_check, child_tables):
|
||||||
|
# Check if any field affecting accounting entry is altered
|
||||||
|
doc_before_update = self.get_doc_before_save()
|
||||||
|
accounting_dimensions = get_accounting_dimensions() + ["cost_center", "project"]
|
||||||
|
|
||||||
|
# Check if opening entry check updated
|
||||||
|
needs_repost = doc_before_update.get("is_opening") != self.is_opening
|
||||||
|
|
||||||
|
if not needs_repost:
|
||||||
|
# Parent Level Accounts excluding party account
|
||||||
|
fields_to_check += accounting_dimensions
|
||||||
|
for field in fields_to_check:
|
||||||
|
if doc_before_update.get(field) != self.get(field):
|
||||||
|
needs_repost = 1
|
||||||
|
break
|
||||||
|
|
||||||
|
if not needs_repost:
|
||||||
|
# Check for child tables
|
||||||
|
for table in child_tables:
|
||||||
|
needs_repost = check_if_child_table_updated(
|
||||||
|
doc_before_update.get(table), self.get(table), child_tables[table]
|
||||||
|
)
|
||||||
|
if needs_repost:
|
||||||
|
break
|
||||||
|
|
||||||
|
return needs_repost
|
||||||
|
|
||||||
|
@frappe.whitelist()
|
||||||
|
def repost_accounting_entries(self):
|
||||||
|
if self.repost_required:
|
||||||
|
self.docstatus = 2
|
||||||
|
self.make_gl_entries_on_cancel()
|
||||||
|
self.docstatus = 1
|
||||||
|
self.make_gl_entries()
|
||||||
|
self.db_set("repost_required", 0)
|
||||||
|
else:
|
||||||
|
frappe.throw(_("No updates pending for reposting"))
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_tax_rate(account_head):
|
def get_tax_rate(account_head):
|
||||||
@ -3191,6 +3229,23 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
|||||||
parent.create_stock_reservation_entries()
|
parent.create_stock_reservation_entries()
|
||||||
|
|
||||||
|
|
||||||
|
def check_if_child_table_updated(
|
||||||
|
child_table_before_update, child_table_after_update, fields_to_check
|
||||||
|
):
|
||||||
|
accounting_dimensions = get_accounting_dimensions() + ["cost_center", "project"]
|
||||||
|
# Check if any field affecting accounting entry is altered
|
||||||
|
for index, item in enumerate(child_table_after_update):
|
||||||
|
for field in fields_to_check:
|
||||||
|
if child_table_before_update[index].get(field) != item.get(field):
|
||||||
|
return True
|
||||||
|
|
||||||
|
for dimension in accounting_dimensions:
|
||||||
|
if child_table_before_update[index].get(dimension) != item.get(dimension):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
@erpnext.allow_regional
|
@erpnext.allow_regional
|
||||||
def validate_regional(doc):
|
def validate_regional(doc):
|
||||||
pass
|
pass
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user