refactor: updated sql query for item variants

This commit is contained in:
noahjacob 2021-05-26 17:52:08 +05:30 committed by Noah Jacob
parent cf4078756d
commit 9b0b2daf4a

View File

@ -116,21 +116,21 @@ class ProductionPlan(Document):
so_list = self.get_so_mr_list("sales_order", "sales_orders")
item_condition = ""
item_condition = variant_of_bom = ""
if self.item_code:
if frappe.db.exists('Item', self.item_code):
is_variant = frappe.db.get_value('Item', self.item_code, ['variant_of'])
if is_variant:
variant_of = is_variant
has_variant_bom = frappe.db.exists({'doctype': 'BOM', 'item': self.item_code})
if not has_variant_bom:
variant_of_bom = "'%s'" % frappe.db.get_value('Item', self.item_code, ['variant_of'])
item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code))
items = frappe.db.sql("""select distinct parent, item_code, warehouse,
(qty - work_order_qty) * conversion_factor as pending_qty, description, name
from `tabSales Order Item` so_item
where parent in (%s) and docstatus = 1 and qty > work_order_qty
and exists (select name from `tabBOM` bom where bom.item= "%s"
and exists (select name from `tabBOM` bom where bom.item= %s
and bom.is_active = 1) %s""" % \
(", ".join(["%s"] * len(so_list)), variant_of or "so_items.item_code", item_condition), tuple(so_list), as_dict=1)
(", ".join(["%s"] * len(so_list)), variant_of_bom or "so_item.item_code", item_condition), tuple(so_list), as_dict=1)
if self.item_code:
item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code))
@ -686,7 +686,7 @@ def get_material_request_items(row, sales_order, company,
}
def get_sales_orders(self):
so_filter = item_filter = ""
so_filter = item_filter = variant_of_bom = ""
if self.from_date:
so_filter += " and so.transaction_date >= %(from_date)s"
if self.to_date:
@ -700,9 +700,9 @@ def get_sales_orders(self):
if self.item_code:
if frappe.db.exists('Item', self.item_code):
is_variant = frappe.db.get_value('Item', self.item_code, ['variant_of'])
if is_variant:
variant_of = is_variant
has_variant_bom = frappe.db.exists({'doctype': 'BOM', 'item': self.item_code})
if not has_variant_bom:
variant_of_bom = "'%s'" % frappe.db.get_value('Item', self.item_code, ['variant_of'])
item_filter += " and so_item.item_code = %(item)s"
open_so = frappe.db.sql("""
@ -712,18 +712,17 @@ def get_sales_orders(self):
and so.docstatus = 1 and so.status not in ("Stopped", "Closed")
and so.company = %(company)s
and so_item.qty > so_item.work_order_qty {0} {1}
and (exists (select name from `tabBOM` bom where bom.item=%(item_code)s
and (exists (select name from `tabBOM` bom where bom.item= {2}
and bom.is_active = 1)
or exists (select name from `tabPacked Item` pi
where pi.parent = so.name and pi.parent_item = so_item.item_code
and exists (select name from `tabBOM` bom where bom.item=pi.item_code
and bom.is_active = 1)))
""".format(so_filter, item_filter), {
""".format(so_filter, item_filter, variant_of_bom or "so_item.item_code"), {
"from_date": self.from_date,
"to_date": self.to_date,
"customer": self.customer,
"project": self.project,
"item_code": variant_of or "so_item.item_code",
"item": self.item_code,
"company": self.company,
"sales_order_status": self.sales_order_status