fix: handle for product bundle (#18420)

* fix: handle for product bundle

* fix: Requested changes and some general fixes
This commit is contained in:
Anurag Mishra 2019-09-05 15:13:21 +05:30 committed by Nabin Hait
parent d114c8f88f
commit 867ac10ffd
3 changed files with 69 additions and 27 deletions

View File

@ -518,7 +518,7 @@ def make_material_request(source_name, target_doc=None):
"doctype": "Material Request Item", "doctype": "Material Request Item",
"field_map": { "field_map": {
"parent": "sales_order", "parent": "sales_order",
"stock_uom": "uom" "uom": "stock_uom"
}, },
"postprocess": update_item "postprocess": update_item
}, },

View File

@ -4,7 +4,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
from frappe import _ from frappe import _
from frappe.utils import cint,cstr from frappe.utils import flt
def execute(filters=None): def execute(filters=None):
columns = get_columns() columns = get_columns()
@ -100,7 +100,7 @@ def get_data():
so.transaction_date, so.transaction_date,
so.customer, so.customer,
so.territory, so.territory,
sum(so_item.qty) as net_qty, sum(so_item.qty) as total_qty,
so.company so.company
FROM `tabSales Order` so, `tabSales Order Item` so_item FROM `tabSales Order` so, `tabSales Order Item` so_item
WHERE WHERE
@ -111,10 +111,15 @@ def get_data():
so.name,so_item.item_code so.name,so_item.item_code
""", as_dict = 1) """, as_dict = 1)
sales_orders = [row.name for row in sales_order_entry]
mr_records = frappe.get_all("Material Request Item", mr_records = frappe.get_all("Material Request Item",
{"sales_order_item": ("!=",""), "docstatus": 1}, {"sales_order": ("in", sales_orders), "docstatus": 1},
["parent", "qty", "sales_order", "item_code"]) ["parent", "qty", "sales_order", "item_code"])
bundled_item_map = get_packed_items(sales_orders)
item_with_product_bundle = get_items_with_product_bundle([row.item_code for row in sales_order_entry])
materials_request_dict = {} materials_request_dict = {}
for record in mr_records: for record in mr_records:
@ -131,27 +136,64 @@ def get_data():
if record.parent not in details.get('material_requests'): if record.parent not in details.get('material_requests'):
details['material_requests'].append(record.parent) details['material_requests'].append(record.parent)
pending_so=[] pending_so = []
for so in sales_order_entry: for so in sales_order_entry:
# fetch all the material request records for a sales order item if so.item_code not in item_with_product_bundle:
key = (so.name, so.item_code) material_requests_against_so = materials_request_dict.get((so.name, so.item_code)) or {}
materials_request = materials_request_dict.get(key) or {} # check for pending sales order
if flt(so.total_qty) > flt(material_requests_against_so.get('qty')):
so_record = {
"item_code": so.item_code,
"item_name": so.item_name,
"description": so.description,
"sales_order_no": so.name,
"date": so.transaction_date,
"material_request": ','.join(material_requests_against_so.get('material_requests', [])),
"customer": so.customer,
"territory": so.territory,
"so_qty": so.total_qty,
"requested_qty": material_requests_against_so.get('qty'),
"pending_qty": so.total_qty - flt(material_requests_against_so.get('qty')),
"company": so.company
}
pending_so.append(so_record)
else:
for item in bundled_item_map.get((so.name, so.item_code)):
material_requests_against_so = materials_request_dict.get((so.name, item.item_code)) or {}
if flt(item.qty) > flt(material_requests_against_so.get('qty')):
so_record = {
"item_code": item.item_code,
"item_name": item.item_name,
"description": item.description,
"sales_order_no": so.name,
"date": so.transaction_date,
"material_request": ','.join(material_requests_against_so.get('material_requests', [])),
"customer": so.customer,
"territory": so.territory,
"so_qty": item.qty,
"requested_qty": material_requests_against_so.get('qty', 0),
"pending_qty": item.qty - flt(material_requests_against_so.get('qty', 0)),
"company": so.company
}
pending_so.append(so_record)
# check for pending sales order
if cint(so.net_qty) > cint(materials_request.get('qty')): return pending_so
so_record = {
"item_code": so.item_code, def get_items_with_product_bundle(item_list):
"item_name": so.item_name, bundled_items = frappe.get_all("Product Bundle", filters = [
"description": so.description, ("new_item_code", "IN", item_list)
"sales_order_no": so.name, ], fields = ["new_item_code"])
"date": so.transaction_date,
"material_request": ','.join(materials_request.get('material_requests', [])), return [d.new_item_code for d in bundled_items]
"customer": so.customer,
"territory": so.territory, def get_packed_items(sales_order_list):
"so_qty": so.net_qty, packed_items = frappe.get_all("Packed Item", filters = [
"requested_qty": cint(materials_request.get('qty')), ("parent", "IN", sales_order_list)
"pending_qty": so.net_qty - cint(materials_request.get('qty')), ], fields = ["parent_item", "item_code", "qty", "item_name", "description", "parent"])
"company": so.company
} bundled_item_map = frappe._dict()
pending_so.append(so_record) for d in packed_items:
return pending_so bundled_item_map.setdefault((d.parent, d.parent_item), []).append(d)
return bundled_item_map

View File

@ -639,7 +639,7 @@ def validate_price_list(args):
if not frappe.db.get_value("Price List", if not frappe.db.get_value("Price List",
{"name": args.price_list, args.transaction_type: 1, "enabled": 1}): {"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
throw(_("Price List {0} is disabled or does not exist").format(args.price_list)) throw(_("Price List {0} is disabled or does not exist").format(args.price_list))
elif not args.get("supplier"): elif args.get("customer"):
throw(_("Price List not selected")) throw(_("Price List not selected"))
def validate_conversion_rate(args, meta): def validate_conversion_rate(args, meta):