mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-05 05:09:11 +00:00
[material request] Added feature to pull items from BOM
This commit is contained in:
@@ -402,4 +402,53 @@ class DocType:
|
||||
if act_pbom and act_pbom[0][0]:
|
||||
action = self.doc.docstatus < 2 and _("deactivate") or _("cancel")
|
||||
msgprint(_("Cannot ") + action + _(": It is linked to other active BOM(s)"),
|
||||
raise_exception=1)
|
||||
raise_exception=1)
|
||||
|
||||
def get_bom_items_as_dict(bom, qty=1, fetch_exploded=1):
|
||||
item_dict = {}
|
||||
|
||||
query = """select
|
||||
bom_item.item_code,
|
||||
ifnull(sum(bom_item.qty_consumed_per_unit),0) * %(qty)s as qty,
|
||||
item.description,
|
||||
item.stock_uom,
|
||||
item.default_warehouse
|
||||
from
|
||||
`tab%(table)s` bom_item, `tabItem` item
|
||||
where
|
||||
bom_item.docstatus < 2
|
||||
and bom_item.parent = "%(bom)s"
|
||||
and item.name = bom_item.item_code
|
||||
%(conditions)s
|
||||
group by item_code, stock_uom"""
|
||||
|
||||
if fetch_exploded:
|
||||
items = webnotes.conn.sql(query % {
|
||||
"qty": qty,
|
||||
"table": "BOM Explosion Item",
|
||||
"bom": bom,
|
||||
"conditions": """and ifnull(item.is_pro_applicable, 'No') = 'No'
|
||||
and ifnull(item.is_sub_contracted_item, 'No') = 'No' """
|
||||
}, as_dict=True)
|
||||
else:
|
||||
items = webnotes.conn.sql(query % {
|
||||
"qty": qty,
|
||||
"table": "BOM Item",
|
||||
"bom": bom,
|
||||
"conditions": ""
|
||||
}, as_dict=True)
|
||||
|
||||
# make unique
|
||||
for item in items:
|
||||
if item_dict.has_key(item.item_code):
|
||||
item_dict[item.item_code]["qty"] += flt(item.qty)
|
||||
else:
|
||||
item_dict[item.item_code] = item
|
||||
|
||||
return item_dict
|
||||
|
||||
@webnotes.whitelist()
|
||||
def get_bom_items(bom, qty=1, fetch_exploded=1):
|
||||
items = get_bom_items_as_dict(bom, qty, fetch_exploded).values()
|
||||
items.sort(lambda a, b: a.item_code > b.item_code and 1 or -1)
|
||||
return items
|
||||
@@ -7,6 +7,35 @@ import unittest
|
||||
import webnotes
|
||||
|
||||
test_records = [
|
||||
[
|
||||
{
|
||||
"doctype": "BOM",
|
||||
"item": "_Test Item Home Desktop 100",
|
||||
"quantity": 1.0,
|
||||
"is_active": 1,
|
||||
"is_default": 1,
|
||||
"docstatus": 1
|
||||
},
|
||||
{
|
||||
"doctype": "BOM Item",
|
||||
"item_code": "_Test Serialized Item With Series",
|
||||
"parentfield": "bom_materials",
|
||||
"qty": 1.0,
|
||||
"rate": 5000.0,
|
||||
"amount": 5000.0,
|
||||
"stock_uom": "_Test UOM"
|
||||
},
|
||||
{
|
||||
"doctype": "BOM Item",
|
||||
"item_code": "_Test Item 2",
|
||||
"parentfield": "bom_materials",
|
||||
"qty": 2.0,
|
||||
"rate": 1000.0,
|
||||
"amount": 2000.0,
|
||||
"stock_uom": "_Test UOM"
|
||||
}
|
||||
],
|
||||
|
||||
[
|
||||
{
|
||||
"doctype": "BOM",
|
||||
@@ -29,10 +58,33 @@ test_records = [
|
||||
"doctype": "BOM Item",
|
||||
"item_code": "_Test Item Home Desktop 100",
|
||||
"parentfield": "bom_materials",
|
||||
"bom_no": "BOM/_Test Item Home Desktop 100/001",
|
||||
"qty": 2.0,
|
||||
"rate": 1000.0,
|
||||
"amount": 2000.0,
|
||||
"stock_uom": "_Test UOM"
|
||||
}
|
||||
]
|
||||
]
|
||||
],
|
||||
]
|
||||
|
||||
class TestBOM(unittest.TestCase):
|
||||
def test_get_items(self):
|
||||
from manufacturing.doctype.bom.bom import get_bom_items_as_dict
|
||||
items_dict = get_bom_items_as_dict(bom="BOM/_Test FG Item/001", qty=1, fetch_exploded=0)
|
||||
self.assertTrue(test_records[1][1]["item_code"] in items_dict)
|
||||
self.assertTrue(test_records[1][2]["item_code"] in items_dict)
|
||||
self.assertEquals(len(items_dict.values()), 2)
|
||||
|
||||
def test_get_items_exploded(self):
|
||||
from manufacturing.doctype.bom.bom import get_bom_items_as_dict
|
||||
items_dict = get_bom_items_as_dict(bom="BOM/_Test FG Item/001", qty=1, fetch_exploded=1)
|
||||
self.assertTrue(test_records[1][1]["item_code"] in items_dict)
|
||||
self.assertFalse(test_records[1][2]["item_code"] in items_dict)
|
||||
self.assertTrue(test_records[0][1]["item_code"] in items_dict)
|
||||
self.assertTrue(test_records[0][2]["item_code"] in items_dict)
|
||||
self.assertEquals(len(items_dict.values()), 3)
|
||||
|
||||
def test_get_items_list(self):
|
||||
from manufacturing.doctype.bom.bom import get_bom_items
|
||||
self.assertEquals(len(get_bom_items(bom="BOM/_Test FG Item/001", qty=1, fetch_exploded=1)), 3)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user