test: Add unit tests

This commit is contained in:
Deepesh Garg 2022-10-23 23:03:50 +05:30
parent 1105e52031
commit ed98015a56
2 changed files with 76 additions and 46 deletions

View File

@ -521,52 +521,53 @@ class SalesInvoice(SellingController):
self.set_paid_amount() self.set_paid_amount()
def on_update_after_submit(self): def on_update_after_submit(self):
needs_repost = 0 if hasattr(self, "repost_required"):
needs_repost = 0
# Check if any field affecting accounting entry is altered # Check if any field affecting accounting entry is altered
doc_before_update = self.get_doc_before_save() doc_before_update = self.get_doc_before_save()
accounting_dimensions = get_accounting_dimensions() + ["cost_center", "project"] accounting_dimensions = get_accounting_dimensions() + ["cost_center", "project"]
# Check if opening entry check updated # Check if opening entry check updated
if doc_before_update.get("is_opening") != self.is_opening: if doc_before_update.get("is_opening") != self.is_opening:
needs_repost = 1
if not needs_repost:
# Parent Level Accounts excluding party account
for field in (
"additional_discount_account",
"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 needs_repost = 1
if self.check_if_child_table_updated( if not needs_repost:
"taxes", doc_before_update, ("account_head",), accounting_dimensions # Parent Level Accounts excluding party account
): for field in (
needs_repost = 1 "additional_discount_account",
"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
self.validate_accounts() # Check for parent accounting dimensions
self.db_set("repost_required", needs_repost) 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.db_set("repost_required", needs_repost)
def check_if_child_table_updated( def check_if_child_table_updated(
self, child_table, doc_before_update, fields_to_check, accounting_dimensions self, child_table, doc_before_update, fields_to_check, accounting_dimensions
@ -585,11 +586,14 @@ class SalesInvoice(SellingController):
@frappe.whitelist() @frappe.whitelist()
def repost_accounting_entries(self): def repost_accounting_entries(self):
self.docstatus = 2 if self.repost_required:
self.make_gl_entries_on_cancel() self.docstatus = 2
self.docstatus = 1 self.make_gl_entries_on_cancel()
self.make_gl_entries() self.docstatus = 1
self.db_set("repost_required", 0) self.make_gl_entries()
self.db_set("repost_required", 0)
else:
frappe.throw(_("No updates pending for reposting"))
def set_paid_amount(self): def set_paid_amount(self):
paid_amount = 0.0 paid_amount = 0.0

View File

@ -2728,6 +2728,31 @@ class TestSalesInvoice(unittest.TestCase):
check_gl_entries(self, si.name, expected_gle, add_days(nowdate(), -1)) check_gl_entries(self, si.name, expected_gle, add_days(nowdate(), -1))
# Update Invoice post submit and then check GL Entries again
si.load_from_db()
si.items[0].income_account = "Service - _TC"
si.additional_discount_account = "_Test Account Sales - _TC"
si.taxes[0].account_head = "VAT 5% - _TC"
si.save()
si.load_from_db()
self.assertTrue(si.repost_required)
si.repost_accounting_entries()
expected_gle = [
["_Test Account Sales - _TC", 22.0, 0.0, nowdate()],
["Debtors - _TC", 88, 0.0, nowdate()],
["Service - _TC", 0.0, 100.0, nowdate()],
["VAT 5% - _TC", 0.0, 10.0, nowdate()],
]
check_gl_entries(self, si.name, expected_gle, add_days(nowdate(), -1))
si.load_from_db()
self.assertFalse(si.repost_required)
def test_asset_depreciation_on_sale_with_pro_rata(self): def test_asset_depreciation_on_sale_with_pro_rata(self):
""" """
Tests if an Asset set to depreciate yearly on June 30, that gets sold on Sept 30, creates an additional depreciation entry on its date of sale. Tests if an Asset set to depreciate yearly on June 30, that gets sold on Sept 30, creates an additional depreciation entry on its date of sale.
@ -3269,6 +3294,7 @@ def check_gl_entries(doc, voucher_no, expected_gle, posting_date):
"""select account, debit, credit, posting_date """select account, debit, credit, posting_date
from `tabGL Entry` from `tabGL Entry`
where voucher_type='Sales Invoice' and voucher_no=%s and posting_date > %s where voucher_type='Sales Invoice' and voucher_no=%s and posting_date > %s
and is_cancelled = 0
order by posting_date asc, account asc""", order by posting_date asc, account asc""",
(voucher_no, posting_date), (voucher_no, posting_date),
as_dict=1, as_dict=1,