From 9c82c2b5d3bf790ba87a672d1b87e9efff8bc010 Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Wed, 21 Aug 2024 12:11:22 +0530 Subject: [PATCH] fix: custom stock entry type issue (#42835) --- erpnext/patches.txt | 1 + .../v15_0/set_standard_stock_entry_type.py | 16 ++++++++++ .../operations/install_fixtures.py | 32 ++++++++++++++++--- .../stock/doctype/stock_entry/stock_entry.py | 2 +- .../stock_entry_type/stock_entry_type.json | 12 +++++-- .../stock_entry_type/stock_entry_type.py | 15 +++++++++ .../stock_entry_type/test_stock_entry_type.py | 29 ++++++++++++++++- 7 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 erpnext/patches/v15_0/set_standard_stock_entry_type.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index bed17fe00e3..4b62284979e 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -376,3 +376,4 @@ erpnext.patches.v15_0.update_asset_repair_field_in_stock_entry erpnext.patches.v15_0.update_total_number_of_booked_depreciations erpnext.patches.v15_0.do_not_use_batchwise_valuation erpnext.patches.v15_0.drop_index_posting_datetime_from_sle +erpnext.patches.v15_0.set_standard_stock_entry_type diff --git a/erpnext/patches/v15_0/set_standard_stock_entry_type.py b/erpnext/patches/v15_0/set_standard_stock_entry_type.py new file mode 100644 index 00000000000..7551b0d4afc --- /dev/null +++ b/erpnext/patches/v15_0/set_standard_stock_entry_type.py @@ -0,0 +1,16 @@ +import frappe + + +def execute(): + for stock_entry_type in [ + "Material Issue", + "Material Receipt", + "Material Transfer", + "Material Transfer for Manufacture", + "Material Consumption for Manufacture", + "Manufacture", + "Repack", + "Send to Subcontractor", + ]: + if frappe.db.exists("Stock Entry Type", stock_entry_type): + frappe.db.set_value("Stock Entry Type", stock_entry_type, "is_standard", 1) diff --git a/erpnext/setup/setup_wizard/operations/install_fixtures.py b/erpnext/setup/setup_wizard/operations/install_fixtures.py index 1d2530cfdf0..6365ad2a4e1 100644 --- a/erpnext/setup/setup_wizard/operations/install_fixtures.py +++ b/erpnext/setup/setup_wizard/operations/install_fixtures.py @@ -66,29 +66,53 @@ def install(country=None): "parent_item_group": _("All Item Groups"), }, # Stock Entry Type - {"doctype": "Stock Entry Type", "name": "Material Issue", "purpose": "Material Issue"}, - {"doctype": "Stock Entry Type", "name": "Material Receipt", "purpose": "Material Receipt"}, + { + "doctype": "Stock Entry Type", + "name": "Material Issue", + "purpose": "Material Issue", + "is_standard": 1, + }, + { + "doctype": "Stock Entry Type", + "name": "Material Receipt", + "purpose": "Material Receipt", + "is_standard": 1, + }, { "doctype": "Stock Entry Type", "name": "Material Transfer", "purpose": "Material Transfer", + "is_standard": 1, + }, + { + "doctype": "Stock Entry Type", + "name": "Manufacture", + "purpose": "Manufacture", + "is_standard": 1, + }, + { + "doctype": "Stock Entry Type", + "name": "Repack", + "purpose": "Repack", + "is_standard": 1, }, - {"doctype": "Stock Entry Type", "name": "Manufacture", "purpose": "Manufacture"}, - {"doctype": "Stock Entry Type", "name": "Repack", "purpose": "Repack"}, { "doctype": "Stock Entry Type", "name": "Send to Subcontractor", "purpose": "Send to Subcontractor", + "is_standard": 1, }, { "doctype": "Stock Entry Type", "name": "Material Transfer for Manufacture", "purpose": "Material Transfer for Manufacture", + "is_standard": 1, }, { "doctype": "Stock Entry Type", "name": "Material Consumption for Manufacture", "purpose": "Material Consumption for Manufacture", + "is_standard": 1, }, # territory: with two default territories, one for home country and one named Rest of the World { diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index caf4cc85ae0..e265ec123c9 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -1000,7 +1000,7 @@ class StockEntry(StockController): def set_stock_entry_type(self): if self.purpose: self.stock_entry_type = frappe.get_cached_value( - "Stock Entry Type", {"purpose": self.purpose}, "name" + "Stock Entry Type", {"purpose": self.purpose, "is_standard": 1}, "name" ) def set_purpose_for_stock_entry(self): diff --git a/erpnext/stock/doctype/stock_entry_type/stock_entry_type.json b/erpnext/stock/doctype/stock_entry_type/stock_entry_type.json index 7944f0ed5af..303ce95476e 100644 --- a/erpnext/stock/doctype/stock_entry_type/stock_entry_type.json +++ b/erpnext/stock/doctype/stock_entry_type/stock_entry_type.json @@ -7,7 +7,8 @@ "engine": "InnoDB", "field_order": [ "purpose", - "add_to_transit" + "add_to_transit", + "is_standard" ], "fields": [ { @@ -26,10 +27,17 @@ "fieldname": "add_to_transit", "fieldtype": "Check", "label": "Add to Transit" + }, + { + "default": "0", + "fieldname": "is_standard", + "fieldtype": "Check", + "label": "Is Standard", + "read_only": 1 } ], "links": [], - "modified": "2024-07-08 08:41:19.385020", + "modified": "2024-08-20 15:35:45.696958", "modified_by": "Administrator", "module": "Stock", "name": "Stock Entry Type", diff --git a/erpnext/stock/doctype/stock_entry_type/stock_entry_type.py b/erpnext/stock/doctype/stock_entry_type/stock_entry_type.py index 2ebd23ed016..f5c5f8047a0 100644 --- a/erpnext/stock/doctype/stock_entry_type/stock_entry_type.py +++ b/erpnext/stock/doctype/stock_entry_type/stock_entry_type.py @@ -19,6 +19,7 @@ class StockEntryType(Document): from frappe.types import DF add_to_transit: DF.Check + is_standard: DF.Check purpose: DF.Literal[ "", "Material Issue", @@ -33,9 +34,23 @@ class StockEntryType(Document): # end: auto-generated types def validate(self): + self.validate_standard_type() if self.add_to_transit and self.purpose != "Material Transfer": self.add_to_transit = 0 + def validate_standard_type(self): + if self.is_standard and self.name not in [ + "Material Issue", + "Material Receipt", + "Material Transfer", + "Material Transfer for Manufacture", + "Material Consumption for Manufacture", + "Manufacture", + "Repack", + "Send to Subcontractor", + ]: + frappe.throw(f"Stock Entry Type {self.name} cannot be set as standard") + class ManufactureEntry: def __init__(self, kwargs) -> None: diff --git a/erpnext/stock/doctype/stock_entry_type/test_stock_entry_type.py b/erpnext/stock/doctype/stock_entry_type/test_stock_entry_type.py index 83ebe7e651b..61156545e34 100644 --- a/erpnext/stock/doctype/stock_entry_type/test_stock_entry_type.py +++ b/erpnext/stock/doctype/stock_entry_type/test_stock_entry_type.py @@ -3,6 +3,33 @@ import unittest +import frappe + class TestStockEntryType(unittest.TestCase): - pass + def test_stock_entry_type_non_standard(self): + stock_entry_type = "Test Manufacturing" + + doc = frappe.get_doc( + { + "doctype": "Stock Entry Type", + "__newname": stock_entry_type, + "purpose": "Manufacture", + "is_standard": 1, + } + ) + + self.assertRaises(frappe.ValidationError, doc.insert) + + def test_stock_entry_type_is_standard(self): + for stock_entry_type in [ + "Material Issue", + "Material Receipt", + "Material Transfer", + "Material Transfer for Manufacture", + "Material Consumption for Manufacture", + "Manufacture", + "Repack", + "Send to Subcontractor", + ]: + self.assertTrue(frappe.db.get_value("Stock Entry Type", stock_entry_type, "is_standard"))