fix: update validation and test cases

This commit is contained in:
Khushi Rawat
2025-06-18 01:04:51 +05:30
committed by khushi8112
parent b27b35e1ae
commit 5130dc408a
5 changed files with 104 additions and 38 deletions

View File

@@ -11,7 +11,6 @@ from frappe.model.document import Document
from frappe.utils import cint
from erpnext.accounts.utils import sync_auto_reconcile_config
from erpnext.stock.utils import check_pending_reposting
class AccountsSettings(Document):
@@ -97,8 +96,8 @@ class AccountsSettings(Document):
if old_doc.show_payment_schedule_in_print != self.show_payment_schedule_in_print:
self.enable_payment_schedule_in_print()
if old_doc.acc_frozen_upto != self.acc_frozen_upto:
self.validate_pending_reposts()
if old_doc.use_sales_invoice_in_pos != self.use_sales_invoice_in_pos:
self.validate_invoice_mode_switch_in_pos()
if clear_cache:
frappe.clear_cache()
@@ -126,10 +125,6 @@ class AccountsSettings(Document):
validate_fields_for_doctype=False,
)
def validate_pending_reposts(self):
if self.acc_frozen_upto:
check_pending_reposting(self.acc_frozen_upto)
def validate_and_sync_auto_reconcile_config(self):
if self.has_value_changed("auto_reconciliation_job_trigger"):
if (

View File

@@ -1,11 +1,16 @@
frappe.ui.form.on("Accounts Settings", {
refresh: function (frm) {
frm.set_df_property("acc_frozen_upto", "label", "Books Closed Through");
frm.set_df_property(
"frozen_accounts_modifier",
"label",
"Role Allowed to Close Books & Make Changes to Closed Periods"
);
frm.set_df_property("credit_controller", "label", "Credit Manager");
},
});
frappe.ui.form.on("Company", {
refresh: function (frm) {
frm.set_df_property("accounts_frozen_till_date", "label", "Books Closed Through");
frm.set_df_property(
"role_allowed_for_frozen_entries",
"label",
"Role Allowed to Close Books & Make Changes to Closed Periods"
);
},
});

View File

@@ -161,6 +161,83 @@ def get_conditions(filters):
return conditions
def get_data(filters):
data = []
conditions = get_conditions(filters)
pr_supplier_map = get_purchase_receipt_supplier_map()
pi_supplier_map = get_purchase_invoice_supplier_map()
assets_linked_to_fb = get_assets_linked_to_fb(filters)
company_fb = frappe.get_cached_value("Company", filters.company, "default_finance_book")
if filters.include_default_book_assets and company_fb:
finance_book = company_fb
elif filters.finance_book:
finance_book = filters.finance_book
else:
finance_book = None
depreciation_amount_map = get_asset_depreciation_amount_map(filters, finance_book)
group_by = frappe.scrub(filters.get("group_by"))
if group_by in ("asset_category", "location"):
data = get_group_by_data(group_by, conditions, assets_linked_to_fb, depreciation_amount_map)
return data
fields = [
"name as asset_id",
"asset_name",
"status",
"department",
"company",
"cost_center",
"calculate_depreciation",
"purchase_receipt",
"asset_category",
"purchase_date",
"gross_purchase_amount",
"location",
"available_for_use_date",
"purchase_invoice",
"opening_accumulated_depreciation",
]
assets_record = frappe.db.get_all("Asset", filters=conditions, fields=fields, debug=1)
for asset in assets_record:
if assets_linked_to_fb and asset.calculate_depreciation and asset.asset_id not in assets_linked_to_fb:
continue
depreciation_amount = depreciation_amount_map.get(asset.asset_id) or 0.0
asset_value = (
asset.gross_purchase_amount - asset.opening_accumulated_depreciation - depreciation_amount
)
row = {
"asset_id": asset.asset_id,
"asset_name": asset.asset_name,
"status": asset.status,
"department": asset.department,
"cost_center": asset.cost_center,
"vendor_name": pr_supplier_map.get(asset.purchase_receipt)
or pi_supplier_map.get(asset.purchase_invoice),
"gross_purchase_amount": asset.gross_purchase_amount,
"opening_accumulated_depreciation": asset.opening_accumulated_depreciation,
"depreciated_amount": depreciation_amount,
"available_for_use_date": asset.available_for_use_date,
"location": asset.location,
"asset_category": asset.asset_category,
"purchase_date": asset.purchase_date,
"asset_value": asset_value,
"company": asset.company,
}
data.append(row)
return data
def prepare_chart_data(data, filters):
if not data:
return

View File

@@ -16,6 +16,7 @@ from frappe.utils.nestedset import NestedSet, rebuild_tree
from erpnext.accounts.doctype.account.account import get_account_currency
from erpnext.setup.setup_wizard.operations.taxes_setup import setup_taxes_and_charges
from erpnext.stock.utils import check_pending_reposting
class Company(NestedSet):
@@ -145,6 +146,7 @@ class Company(NestedSet):
return exists
def validate(self):
old_doc = self.get_doc_before_save()
self.update_default_account = False
if self.is_new():
self.update_default_account = True
@@ -160,25 +162,7 @@ class Company(NestedSet):
self.check_parent_changed()
self.set_chart_of_accounts()
self.validate_parent_company()
self.set_reporting_currency()
self.validate_inventory_account_settings()
def validate_inventory_account_settings(self):
doc_before_save = self.get_doc_before_save()
if not doc_before_save:
return
if (
doc_before_save.enable_item_wise_inventory_account != self.enable_item_wise_inventory_account
and frappe.db.get_value("Stock Ledger Entry", {"is_cancelled": 0, "company": self.name}, "name")
and doc_before_save.enable_perpetual_inventory
):
frappe.throw(
_(
"Cannot enable Item-wise Inventory Account, as there are existing Stock Ledger Entries for the company {0} with Warehouse-wise Inventory Account. Please cancel the stock transactions first and try again."
).format(bold(self.name)),
title=_("Cannot Change Inventory Account Setting"),
)
self.validate_pending_reposts(old_doc)
def validate_abbr(self):
if not self.abbr:
@@ -563,6 +547,10 @@ class Company(NestedSet):
"Company", self.parent_company, ["reporting_currency"]
)
self.reporting_currency = parent_reporting_currency
def validate_pending_reposts(self, old_doc):
if old_doc.accounts_frozen_till_date != self.accounts_frozen_till_date:
if self.accounts_frozen_till_date:
check_pending_reposting(self.accounts_frozen_till_date)
def set_default_accounts(self):
default_accounts = {

View File

@@ -356,6 +356,7 @@ class TestRepostItemValuation(IntegrationTestCase, StockTestMixin):
riv = frappe.get_doc(
doctype="Repost Item Valuation",
item_code="_Test Item",
company="_Test Company",
warehouse="_Test Warehouse - _TC",
based_on="Item and Warehouse",
posting_date=today,
@@ -363,15 +364,15 @@ class TestRepostItemValuation(IntegrationTestCase, StockTestMixin):
)
riv.flags.dont_run_in_test = True # keep it queued
accounts_settings = frappe.get_doc("Accounts Settings")
accounts_settings.acc_frozen_upto = today
accounts_settings.frozen_accounts_modifier = ""
accounts_settings.save()
company = frappe.get_doc("Company", "_Test Company")
company.accounts_frozen_till_date = today
company.role_allowed_for_frozen_entries = ""
company.save()
self.assertRaises(frappe.ValidationError, riv.save)
accounts_settings.acc_frozen_upto = ""
accounts_settings.save()
company.accounts_frozen_till_date = ""
company.save()
@IntegrationTestCase.change_settings("Stock Reposting Settings", {"item_based_reposting": 0})
def test_create_repost_entry_for_cancelled_document(self):