[Agri] Item amount set on item set (#12091)

* [Agri] Item amount set on item set

- closes #12008
- fixes #12010

* used frappe.call instead of frm.call in crop

* made modifications for sql query for BOM Item

- done since Crop doctype in Agri module also uses 'BOM Item' child table

* hide BOM no from BOM Item childtable in Crop
This commit is contained in:
Ameya Shenoy 2017-12-20 12:00:24 +05:30 committed by Nabin Hait
parent 9a57049faa
commit 87b7844ae8
6 changed files with 66 additions and 13 deletions

View File

@ -1,7 +1,55 @@
// Copyright (c) 2017, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt
frappe.provide("erpnext.crop");
frappe.ui.form.on('Crop', {
validate: (frm) => {
refresh: (frm) => {
frm.fields_dict.materials_required.grid.set_column_disp('bom_no', false);
}
});
});
frappe.ui.form.on("BOM Item", {
item_code: (frm, cdt, cdn) => {
erpnext.crop.update_item_rate_uom(frm, cdt, cdn);
},
qty: (frm, cdt, cdn) => {
erpnext.crop.update_item_qty_amount(frm, cdt, cdn);
},
rate: (frm, cdt, cdn) => {
erpnext.crop.update_item_qty_amount(frm, cdt, cdn);
}
});
erpnext.crop.update_item_rate_uom = function(frm, cdt, cdn) {
let material_list = ['materials_required', 'produce', 'byproducts'];
material_list.forEach((material) => {
frm.doc[material].forEach((item, index) => {
if (item.name == cdn){
frappe.call({
method:'erpnext.agriculture.doctype.crop.crop.get_item_details',
args: {
item_code: item.item_code
},
callback: (r) => {
frappe.model.set_value('BOM Item', item.name, 'uom', r.message.uom);
frappe.model.set_value('BOM Item', item.name, 'rate', r.message.rate);
}
});
}
});
});
};
erpnext.crop.update_item_qty_amount = function(frm, cdt, cdn) {
let material_list = ['materials_required', 'produce', 'byproducts'];
material_list.forEach((material) => {
frm.doc[material].forEach((item, index) => {
if (item.name == cdn){
if (!frappe.model.get_value('BOM Item', item.name, 'qty'))
frappe.model.set_value('BOM Item', item.name, 'qty', 1);
frappe.model.set_value('BOM Item', item.name, 'amount', item.qty * item.rate);
}
});
});
};

View File

@ -15,4 +15,9 @@ class Crop(Document):
frappe.throw("Start day is greater than end day in task '{0}'".format(task.subject))
# to calculate the period of the Crop Cycle
if task.end_day > max_period: max_period = task.end_day
if max_period > self.period: self.period = max_period
if max_period > self.period: self.period = max_period
@frappe.whitelist()
def get_item_details(item_code):
item = frappe.get_doc('Item', item_code)
return { "uom": item.stock_uom, "rate": item.valuation_rate }

View File

@ -192,7 +192,7 @@ class BOM(WebsiteGenerator):
# update parent BOMs
if self.total_cost != existing_bom_cost and update_parent:
parent_boms = frappe.db.sql_list("""select distinct parent from `tabBOM Item`
where bom_no = %s and docstatus=1""", self.name)
where bom_no = %s and docstatus=1 and parenttype='BOM'""", self.name)
for bom in parent_boms:
frappe.get_doc("BOM", bom).update_cost(from_child_bom=True)
@ -330,7 +330,7 @@ class BOM(WebsiteGenerator):
for d in check_list:
bom_list, count = [self.name], 0
while (len(bom_list) > count ):
boms = frappe.db.sql(" select %s from `tabBOM Item` where %s = %s " %
boms = frappe.db.sql(" select %s from `tabBOM Item` where %s = %s and parenttype='BOM'" %
(d[0], d[1], '%s'), cstr(bom_list[count]))
count = count + 1
for b in boms:
@ -350,7 +350,7 @@ class BOM(WebsiteGenerator):
def traverse_tree(self, bom_list=None):
def _get_children(bom_no):
return [cstr(d[0]) for d in frappe.db.sql("""select bom_no from `tabBOM Item`
where parent = %s and ifnull(bom_no, '') != ''""", bom_no)]
where parent = %s and ifnull(bom_no, '') != '' and parenttype='BOM'""", bom_no)]
count = 0
if not bom_list:
@ -496,7 +496,7 @@ class BOM(WebsiteGenerator):
def validate_bom_links(self):
if not self.is_active:
act_pbom = frappe.db.sql("""select distinct bom_item.parent from `tabBOM Item` bom_item
where bom_item.bom_no = %s and bom_item.docstatus = 1
where bom_item.bom_no = %s and bom_item.docstatus = 1 and bom_item.parenttype='BOM'
and exists (select * from `tabBOM` where name = bom_item.parent
and docstatus = 1 and is_active = 1)""", self.name)
@ -612,7 +612,7 @@ def get_children(doctype, parent=None, is_tree=False):
def get_boms_in_bottom_up_order(bom_no=None):
def _get_parent(bom_no):
return frappe.db.sql_list("""select distinct parent from `tabBOM Item`
where bom_no = %s and docstatus=1""", bom_no)
where bom_no = %s and docstatus=1 and parenttype='BOM'""", bom_no)
count = 0
bom_list = []

View File

@ -55,7 +55,7 @@ class TestBOM(unittest.TestCase):
# get current rate for '_Test Item 2'
rm_rate = frappe.db.sql("""select rate from `tabBOM Item`
where parent='BOM-_Test Item Home Desktop Manufactured-001'
and item_code='_Test Item 2' and docstatus=1""")
and item_code='_Test Item 2' and docstatus=1 and parenttype='BOM'""")
rm_rate = rm_rate[0][0] if rm_rate else 0
# update valuation rate of item '_Test Item 2'
@ -74,7 +74,7 @@ class TestBOM(unittest.TestCase):
# check if new valuation rate updated in all BOMs
for d in frappe.db.sql("""select rate from `tabBOM Item`
where item_code='_Test Item 2' and docstatus=1""", as_dict=1):
where item_code='_Test Item 2' and docstatus=1 and parenttype='BOM'""", as_dict=1):
self.assertEqual(d.rate, rm_rate + 10)
def test_bom_cost(self):

View File

@ -35,12 +35,12 @@ class BOMUpdateTool(Document):
new_bom_unitcost = flt(new_bom_unitcost[0][0]) if new_bom_unitcost else 0
frappe.db.sql("""update `tabBOM Item` set bom_no=%s,
rate=%s, amount=stock_qty*%s where bom_no = %s and docstatus < 2""",
rate=%s, amount=stock_qty*%s where bom_no = %s and docstatus < 2 and parenttype='BOM'""",
(self.new_bom, new_bom_unitcost, new_bom_unitcost, self.current_bom))
def get_parent_boms(self):
return [d[0] for d in frappe.db.sql("""select distinct parent
from `tabBOM Item` where ifnull(bom_no, '') = %s and docstatus < 2""",
from `tabBOM Item` where ifnull(bom_no, '') = %s and docstatus < 2 and parenttype='BOM'""",
self.new_bom)]
@frappe.whitelist()

View File

@ -52,6 +52,6 @@ def get_bom_stock(filters):
ON bom_item.item_code = ledger.item_code
%s
WHERE
bom_item.parent = '%s'
bom_item.parent = '%s' and bom_item.parenttype='BOM'
GROUP BY bom_item.item_code""" % (conditions, bom))