Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not able to save bom #23910

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions erpnext/manufacturing/doctype/bom/bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def validate(self):
self.set_routing_operations()
self.validate_operations()
self.calculate_cost()
self.update_stock_qty()
self.update_cost(update_parent=False, from_child_bom=True, save=False)

def get_context(self, context):
Expand All @@ -84,8 +85,6 @@ def get_context(self, context):
def on_update(self):
frappe.cache().hdel('bom_children', self.name)
self.check_recursion()
self.update_stock_qty()
self.update_exploded_items()

def on_submit(self):
self.manage_default_bom()
Expand Down Expand Up @@ -237,7 +236,8 @@ def update_cost(self, update_parent=True, from_child_bom=False, save=True):
self.calculate_cost()
if save:
self.db_update()
self.update_exploded_items()

self.update_exploded_items(save=save)

# update parent BOMs
if self.total_cost != existing_bom_cost and update_parent:
Expand Down Expand Up @@ -318,8 +318,6 @@ def update_stock_qty(self):
m.uom = m.stock_uom
m.qty = m.stock_qty

m.db_update()

def validate_uom_is_interger(self):
from erpnext.utilities.transaction_base import validate_uom_is_integer
validate_uom_is_integer(self, "uom", "qty", "BOM Item")
Expand Down Expand Up @@ -372,15 +370,6 @@ def check_recursion(self, bom_list=[]):
if raise_exception:
frappe.throw(_("BOM recursion: {0} cannot be parent or child of {1}").format(self.name, self.name))

def update_cost_and_exploded_items(self, bom_list=[]):
bom_list = self.traverse_tree(bom_list)
for bom in bom_list:
bom_obj = frappe.get_doc("BOM", bom)
bom_obj.check_recursion(bom_list=bom_list)
bom_obj.update_exploded_items()

return bom_list

def traverse_tree(self, bom_list=None):
def _get_children(bom_no):
children = frappe.cache().hget('bom_children', bom_no)
Expand Down Expand Up @@ -472,10 +461,10 @@ def update_new_bom(self, old_bom, new_bom, rate):
d.rate = rate
d.amount = (d.stock_qty or d.qty) * rate

def update_exploded_items(self):
def update_exploded_items(self, save=True):
""" Update Flat BOM, following will be correct data"""
self.get_exploded_items()
self.add_exploded_items()
self.add_exploded_items(save=save)

def get_exploded_items(self):
""" Get all raw materials including items from child bom"""
Expand Down Expand Up @@ -544,19 +533,23 @@ def get_child_exploded_items(self, bom_no, stock_qty):
'sourced_by_supplier': d.get('sourced_by_supplier', 0)
}))

def add_exploded_items(self):
def add_exploded_items(self, save=True):
"Add items to Flat BOM table"
frappe.db.sql("""delete from `tabBOM Explosion Item` where parent=%s""", self.name)
self.set('exploded_items', [])

if save:
frappe.db.sql("""delete from `tabBOM Explosion Item` where parent=%s""", self.name)

for d in sorted(self.cur_exploded_items, key=itemgetter(0)):
ch = self.append('exploded_items', {})
for i in self.cur_exploded_items[d].keys():
ch.set(i, self.cur_exploded_items[d][i])
ch.amount = flt(ch.stock_qty) * flt(ch.rate)
ch.qty_consumed_per_unit = flt(ch.stock_qty) / flt(self.quantity)
ch.docstatus = self.docstatus
ch.db_insert()

if save:
ch.db_insert()

def validate_bom_links(self):
if not self.is_active:
Expand Down