2021-06-18 15:07:42 +00:00
|
|
|
import copy
|
2021-05-24 14:41:15 +00:00
|
|
|
from collections import defaultdict
|
2021-09-02 11:14:59 +00:00
|
|
|
|
|
|
|
import frappe
|
|
|
|
from frappe import _
|
|
|
|
from frappe.utils import cint, flt, get_link_to_form
|
|
|
|
|
2021-05-24 14:41:15 +00:00
|
|
|
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
class Subcontracting:
|
2021-05-24 14:41:15 +00:00
|
|
|
def set_materials_for_subcontracted_items(self, raw_material_table):
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype == "Purchase Invoice" and not self.update_stock:
|
2021-05-24 14:41:15 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.raw_material_table = raw_material_table
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__identify_change_in_item_table()
|
|
|
|
self.__prepare_supplied_items()
|
2021-06-18 15:07:42 +00:00
|
|
|
self.__validate_supplied_items()
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __prepare_supplied_items(self):
|
2021-05-24 14:41:15 +00:00
|
|
|
self.initialized_fields()
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__get_purchase_orders()
|
|
|
|
self.__get_pending_qty_to_receive()
|
2021-05-24 14:41:15 +00:00
|
|
|
self.get_available_materials()
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__remove_changed_rows()
|
|
|
|
self.__set_supplied_items()
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
def initialized_fields(self):
|
|
|
|
self.available_materials = frappe._dict()
|
2021-06-18 15:07:42 +00:00
|
|
|
self.__transferred_items = frappe._dict()
|
2021-05-24 14:41:15 +00:00
|
|
|
self.alternative_item_details = frappe._dict()
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__get_backflush_based_on()
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __get_backflush_based_on(self):
|
2022-03-28 13:22:46 +00:00
|
|
|
self.backflush_based_on = frappe.db.get_single_value(
|
|
|
|
"Buying Settings", "backflush_raw_materials_of_subcontract_based_on"
|
|
|
|
)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __get_purchase_orders(self):
|
2021-05-24 14:41:15 +00:00
|
|
|
self.purchase_orders = []
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype == "Purchase Order":
|
2021-05-24 14:41:15 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.purchase_orders = [d.purchase_order for d in self.items if d.purchase_order]
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __identify_change_in_item_table(self):
|
2021-06-15 11:59:52 +00:00
|
|
|
self.__changed_name = []
|
|
|
|
self.__reference_name = []
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype == "Purchase Order" or self.is_new():
|
2021-05-24 14:41:15 +00:00
|
|
|
self.set(self.raw_material_table, [])
|
|
|
|
return
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
item_dict = self.__get_data_before_save()
|
2021-05-24 14:41:15 +00:00
|
|
|
if not item_dict:
|
|
|
|
return True
|
|
|
|
|
|
|
|
for n_row in self.items:
|
2021-06-15 11:59:52 +00:00
|
|
|
self.__reference_name.append(n_row.name)
|
2021-05-24 14:41:15 +00:00
|
|
|
if (n_row.name not in item_dict) or (n_row.item_code, n_row.qty) != item_dict[n_row.name]:
|
2021-06-15 11:59:52 +00:00
|
|
|
self.__changed_name.append(n_row.name)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
if item_dict.get(n_row.name):
|
|
|
|
del item_dict[n_row.name]
|
|
|
|
|
2021-06-15 11:59:52 +00:00
|
|
|
self.__changed_name.extend(item_dict.keys())
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __get_data_before_save(self):
|
2021-05-24 14:41:15 +00:00
|
|
|
item_dict = {}
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype in ["Purchase Receipt", "Purchase Invoice"] and self._doc_before_save:
|
|
|
|
for row in self._doc_before_save.get("items"):
|
2021-05-24 14:41:15 +00:00
|
|
|
item_dict[row.name] = (row.item_code, row.qty)
|
|
|
|
|
|
|
|
return item_dict
|
|
|
|
|
|
|
|
def get_available_materials(self):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Get the available raw materials which has been transferred to the supplier.
|
|
|
|
available_materials = {
|
|
|
|
(item_code, subcontracted_item, purchase_order): {
|
|
|
|
'qty': 1, 'serial_no': [ABC], 'batch_no': {'batch1': 1}, 'data': item_details
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2021-05-24 14:41:15 +00:00
|
|
|
if not self.purchase_orders:
|
|
|
|
return
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
for row in self.__get_transferred_items():
|
2021-05-24 14:41:15 +00:00
|
|
|
key = (row.rm_item_code, row.main_item_code, row.purchase_order)
|
|
|
|
|
|
|
|
if key not in self.available_materials:
|
2022-03-28 13:22:46 +00:00
|
|
|
self.available_materials.setdefault(
|
|
|
|
key,
|
|
|
|
frappe._dict(
|
|
|
|
{
|
|
|
|
"qty": 0,
|
|
|
|
"serial_no": [],
|
|
|
|
"batch_no": defaultdict(float),
|
|
|
|
"item_details": row,
|
|
|
|
"po_details": [],
|
|
|
|
}
|
|
|
|
),
|
2021-05-24 14:41:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
details = self.available_materials[key]
|
|
|
|
details.qty += row.qty
|
|
|
|
details.po_details.append(row.po_detail)
|
|
|
|
|
|
|
|
if row.serial_no:
|
|
|
|
details.serial_no.extend(get_serial_nos(row.serial_no))
|
|
|
|
|
|
|
|
if row.batch_no:
|
|
|
|
details.batch_no[row.batch_no] += row.qty
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__set_alternative_item_details(row)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-18 15:07:42 +00:00
|
|
|
self.__transferred_items = copy.deepcopy(self.available_materials)
|
2022-03-28 13:22:46 +00:00
|
|
|
for doctype in ["Purchase Receipt", "Purchase Invoice"]:
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__update_consumed_materials(doctype)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __update_consumed_materials(self, doctype, return_consumed_items=False):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Deduct the consumed materials from the available materials."""
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
pr_items = self.__get_received_items(doctype)
|
2021-05-24 14:41:15 +00:00
|
|
|
if not pr_items:
|
|
|
|
return ([], {}) if return_consumed_items else None
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
pr_items = {d.name: d.get(self.get("po_field") or "purchase_order") for d in pr_items}
|
2021-06-08 05:06:39 +00:00
|
|
|
consumed_materials = self.__get_consumed_items(doctype, pr_items.keys())
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
if return_consumed_items:
|
|
|
|
return (consumed_materials, pr_items)
|
|
|
|
|
|
|
|
for row in consumed_materials:
|
|
|
|
key = (row.rm_item_code, row.main_item_code, pr_items.get(row.reference_name))
|
|
|
|
if not self.available_materials.get(key):
|
|
|
|
continue
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
self.available_materials[key]["qty"] -= row.consumed_qty
|
2021-05-24 14:41:15 +00:00
|
|
|
if row.serial_no:
|
2022-03-28 13:22:46 +00:00
|
|
|
self.available_materials[key]["serial_no"] = list(
|
|
|
|
set(self.available_materials[key]["serial_no"]) - set(get_serial_nos(row.serial_no))
|
2021-05-24 14:41:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if row.batch_no:
|
2022-03-28 13:22:46 +00:00
|
|
|
self.available_materials[key]["batch_no"][row.batch_no] -= row.consumed_qty
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __get_transferred_items(self):
|
2022-03-28 13:22:46 +00:00
|
|
|
fields = ["`tabStock Entry`.`purchase_order`"]
|
|
|
|
alias_dict = {
|
|
|
|
"item_code": "rm_item_code",
|
|
|
|
"subcontracted_item": "main_item_code",
|
|
|
|
"basic_rate": "rate",
|
|
|
|
}
|
|
|
|
|
|
|
|
child_table_fields = [
|
|
|
|
"item_code",
|
|
|
|
"item_name",
|
|
|
|
"description",
|
|
|
|
"qty",
|
|
|
|
"basic_rate",
|
|
|
|
"amount",
|
|
|
|
"serial_no",
|
|
|
|
"uom",
|
|
|
|
"subcontracted_item",
|
|
|
|
"stock_uom",
|
|
|
|
"batch_no",
|
|
|
|
"conversion_factor",
|
|
|
|
"s_warehouse",
|
|
|
|
"t_warehouse",
|
|
|
|
"item_group",
|
|
|
|
"po_detail",
|
|
|
|
]
|
|
|
|
|
|
|
|
if self.backflush_based_on == "BOM":
|
|
|
|
child_table_fields.append("original_item")
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
for field in child_table_fields:
|
2022-03-28 13:22:46 +00:00
|
|
|
fields.append(f"`tabStock Entry Detail`.`{field}` As {alias_dict.get(field, field)}")
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
filters = [
|
|
|
|
["Stock Entry", "docstatus", "=", 1],
|
|
|
|
["Stock Entry", "purpose", "=", "Send to Subcontractor"],
|
|
|
|
["Stock Entry", "purchase_order", "in", self.purchase_orders],
|
|
|
|
]
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
return frappe.get_all("Stock Entry", fields=fields, filters=filters)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __get_received_items(self, doctype):
|
2021-05-24 14:41:15 +00:00
|
|
|
fields = []
|
2022-03-28 13:22:46 +00:00
|
|
|
self.po_field = "purchase_order"
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
for field in ["name", self.po_field, "parent"]:
|
|
|
|
fields.append(f"`tab{doctype} Item`.`{field}`")
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
filters = [
|
|
|
|
[doctype, "docstatus", "=", 1],
|
|
|
|
[f"{doctype} Item", self.po_field, "in", self.purchase_orders],
|
|
|
|
]
|
|
|
|
if doctype == "Purchase Invoice":
|
|
|
|
filters.append(["Purchase Invoice", "update_stock", "=", 1])
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
return frappe.get_all(f"{doctype}", fields=fields, filters=filters)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __get_consumed_items(self, doctype, pr_items):
|
2022-03-28 13:22:46 +00:00
|
|
|
return frappe.get_all(
|
|
|
|
"Purchase Receipt Item Supplied",
|
|
|
|
fields=[
|
|
|
|
"serial_no",
|
|
|
|
"rm_item_code",
|
|
|
|
"reference_name",
|
|
|
|
"batch_no",
|
|
|
|
"consumed_qty",
|
|
|
|
"main_item_code",
|
|
|
|
],
|
|
|
|
filters={"docstatus": 1, "reference_name": ("in", list(pr_items)), "parenttype": doctype},
|
|
|
|
)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __set_alternative_item_details(self, row):
|
2022-03-28 13:22:46 +00:00
|
|
|
if row.get("original_item"):
|
|
|
|
self.alternative_item_details[row.get("original_item")] = row
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __get_pending_qty_to_receive(self):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Get qty to be received against the purchase order."""
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
self.qty_to_be_received = defaultdict(float)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if (
|
|
|
|
self.doctype != "Purchase Order" and self.backflush_based_on != "BOM" and self.purchase_orders
|
|
|
|
):
|
|
|
|
for row in frappe.get_all(
|
|
|
|
"Purchase Order Item",
|
|
|
|
fields=["item_code", "(qty - received_qty) as qty", "parent", "name"],
|
|
|
|
filters={"docstatus": 1, "parent": ("in", self.purchase_orders)},
|
|
|
|
):
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
self.qty_to_be_received[(row.item_code, row.parent)] += row.qty
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __get_materials_from_bom(self, item_code, bom_no, exploded_item=0):
|
2022-03-28 13:22:46 +00:00
|
|
|
doctype = "BOM Item" if not exploded_item else "BOM Explosion Item"
|
|
|
|
fields = [f"`tab{doctype}`.`stock_qty` / `tabBOM`.`quantity` as qty_consumed_per_unit"]
|
|
|
|
|
|
|
|
alias_dict = {
|
|
|
|
"item_code": "rm_item_code",
|
|
|
|
"name": "bom_detail_no",
|
|
|
|
"source_warehouse": "reserve_warehouse",
|
|
|
|
}
|
|
|
|
for field in [
|
|
|
|
"item_code",
|
|
|
|
"name",
|
|
|
|
"rate",
|
|
|
|
"stock_uom",
|
|
|
|
"source_warehouse",
|
|
|
|
"description",
|
|
|
|
"item_name",
|
|
|
|
"stock_uom",
|
|
|
|
]:
|
|
|
|
fields.append(f"`tab{doctype}`.`{field}` As {alias_dict.get(field, field)}")
|
|
|
|
|
|
|
|
filters = [
|
|
|
|
[doctype, "parent", "=", bom_no],
|
|
|
|
[doctype, "docstatus", "=", 1],
|
|
|
|
["BOM", "item", "=", item_code],
|
|
|
|
[doctype, "sourced_by_supplier", "=", 0],
|
|
|
|
]
|
|
|
|
|
|
|
|
return (
|
|
|
|
frappe.get_all("BOM", fields=fields, filters=filters, order_by=f"`tab{doctype}`.`idx`") or []
|
|
|
|
)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __remove_changed_rows(self):
|
2021-06-15 11:59:52 +00:00
|
|
|
if not self.__changed_name:
|
2021-05-24 14:41:15 +00:00
|
|
|
return
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
i = 1
|
2021-05-24 14:41:15 +00:00
|
|
|
self.set(self.raw_material_table, [])
|
|
|
|
for d in self._doc_before_save.supplied_items:
|
2021-06-15 11:59:52 +00:00
|
|
|
if d.reference_name in self.__changed_name:
|
|
|
|
continue
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if d.reference_name not in self.__reference_name:
|
2021-05-24 14:41:15 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
d.idx = i
|
2022-03-28 13:22:46 +00:00
|
|
|
self.append("supplied_items", d)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __set_supplied_items(self):
|
2021-05-24 14:41:15 +00:00
|
|
|
self.bom_items = {}
|
|
|
|
|
|
|
|
has_supplied_items = True if self.get(self.raw_material_table) else False
|
|
|
|
for row in self.items:
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype != "Purchase Order" and (
|
|
|
|
(self.__changed_name and row.name not in self.__changed_name)
|
|
|
|
or (has_supplied_items and not self.__changed_name)
|
|
|
|
):
|
2021-05-24 14:41:15 +00:00
|
|
|
continue
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype == "Purchase Order" or self.backflush_based_on == "BOM":
|
|
|
|
for bom_item in self.__get_materials_from_bom(
|
|
|
|
row.item_code, row.bom, row.get("include_exploded_items")
|
|
|
|
):
|
|
|
|
qty = flt(bom_item.qty_consumed_per_unit) * flt(row.qty) * row.conversion_factor
|
2021-05-24 14:41:15 +00:00
|
|
|
bom_item.main_item_code = row.item_code
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__update_reserve_warehouse(bom_item, row)
|
|
|
|
self.__set_alternative_item(bom_item)
|
|
|
|
self.__add_supplied_item(row, bom_item, qty)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
elif self.backflush_based_on != "BOM":
|
2021-05-24 14:41:15 +00:00
|
|
|
for key, transfer_item in self.available_materials.items():
|
|
|
|
if (key[1], key[2]) == (row.item_code, row.purchase_order) and transfer_item.qty > 0:
|
2021-06-08 05:06:39 +00:00
|
|
|
qty = self.__get_qty_based_on_material_transfer(row, transfer_item) or 0
|
2021-05-24 14:41:15 +00:00
|
|
|
transfer_item.qty -= qty
|
2022-03-28 13:22:46 +00:00
|
|
|
self.__add_supplied_item(row, transfer_item.get("item_details"), qty)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
if self.qty_to_be_received:
|
|
|
|
self.qty_to_be_received[(row.item_code, row.purchase_order)] -= row.qty
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __update_reserve_warehouse(self, row, item):
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype == "Purchase Order":
|
|
|
|
row.reserve_warehouse = self.set_reserve_warehouse or item.warehouse
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __get_qty_based_on_material_transfer(self, item_row, transfer_item):
|
2021-05-24 14:41:15 +00:00
|
|
|
key = (item_row.item_code, item_row.purchase_order)
|
|
|
|
|
|
|
|
if self.qty_to_be_received == item_row.qty:
|
|
|
|
return transfer_item.qty
|
|
|
|
|
|
|
|
if self.qty_to_be_received:
|
|
|
|
qty = (flt(item_row.qty) * flt(transfer_item.qty)) / flt(self.qty_to_be_received.get(key, 0))
|
2021-06-18 15:07:42 +00:00
|
|
|
transfer_item.item_details.required_qty = transfer_item.qty
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if transfer_item.serial_no or frappe.get_cached_value(
|
|
|
|
"UOM", transfer_item.item_details.stock_uom, "must_be_whole_number"
|
|
|
|
):
|
2021-05-24 14:41:15 +00:00
|
|
|
return frappe.utils.ceil(qty)
|
|
|
|
|
|
|
|
return qty
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __set_alternative_item(self, bom_item):
|
2021-05-24 14:41:15 +00:00
|
|
|
if self.alternative_item_details.get(bom_item.rm_item_code):
|
|
|
|
bom_item.update(self.alternative_item_details[bom_item.rm_item_code])
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __add_supplied_item(self, item_row, bom_item, qty):
|
2021-05-24 14:41:15 +00:00
|
|
|
bom_item.conversion_factor = item_row.conversion_factor
|
|
|
|
rm_obj = self.append(self.raw_material_table, bom_item)
|
|
|
|
rm_obj.reference_name = item_row.name
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype == "Purchase Order":
|
2021-05-24 14:41:15 +00:00
|
|
|
rm_obj.required_qty = qty
|
|
|
|
else:
|
2021-06-18 15:07:42 +00:00
|
|
|
rm_obj.consumed_qty = 0
|
|
|
|
rm_obj.purchase_order = item_row.purchase_order
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__set_batch_nos(bom_item, item_row, rm_obj, qty)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __set_batch_nos(self, bom_item, item_row, rm_obj, qty):
|
2021-05-24 14:41:15 +00:00
|
|
|
key = (rm_obj.rm_item_code, item_row.item_code, item_row.purchase_order)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.available_materials.get(key) and self.available_materials[key]["batch_no"]:
|
2021-06-18 15:07:42 +00:00
|
|
|
new_rm_obj = None
|
2022-03-28 13:22:46 +00:00
|
|
|
for batch_no, batch_qty in self.available_materials[key]["batch_no"].items():
|
2021-05-24 14:41:15 +00:00
|
|
|
if batch_qty >= qty:
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__set_batch_no_as_per_qty(item_row, rm_obj, batch_no, qty)
|
2022-03-28 13:22:46 +00:00
|
|
|
self.available_materials[key]["batch_no"][batch_no] -= qty
|
2021-05-24 14:41:15 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
elif qty > 0 and batch_qty > 0:
|
|
|
|
qty -= batch_qty
|
|
|
|
new_rm_obj = self.append(self.raw_material_table, bom_item)
|
|
|
|
new_rm_obj.reference_name = item_row.name
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__set_batch_no_as_per_qty(item_row, new_rm_obj, batch_no, batch_qty)
|
2022-03-28 13:22:46 +00:00
|
|
|
self.available_materials[key]["batch_no"][batch_no] = 0
|
2021-06-18 15:07:42 +00:00
|
|
|
|
|
|
|
if abs(qty) > 0 and not new_rm_obj:
|
|
|
|
self.__set_consumed_qty(rm_obj, qty)
|
2021-05-24 14:41:15 +00:00
|
|
|
else:
|
2021-06-18 15:07:42 +00:00
|
|
|
self.__set_consumed_qty(rm_obj, qty, bom_item.required_qty or qty)
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__set_serial_nos(item_row, rm_obj)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-18 15:07:42 +00:00
|
|
|
def __set_consumed_qty(self, rm_obj, consumed_qty, required_qty=0):
|
|
|
|
rm_obj.required_qty = required_qty
|
|
|
|
rm_obj.consumed_qty = consumed_qty
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __set_batch_no_as_per_qty(self, item_row, rm_obj, batch_no, qty):
|
2022-03-28 13:22:46 +00:00
|
|
|
rm_obj.update(
|
|
|
|
{
|
|
|
|
"consumed_qty": qty,
|
|
|
|
"batch_no": batch_no,
|
|
|
|
"required_qty": qty,
|
|
|
|
"purchase_order": item_row.purchase_order,
|
|
|
|
}
|
|
|
|
)
|
2021-06-18 15:07:42 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__set_serial_nos(item_row, rm_obj)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __set_serial_nos(self, item_row, rm_obj):
|
2021-05-24 14:41:15 +00:00
|
|
|
key = (rm_obj.rm_item_code, item_row.item_code, item_row.purchase_order)
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.available_materials.get(key) and self.available_materials[key]["serial_no"]:
|
|
|
|
used_serial_nos = self.available_materials[key]["serial_no"][0 : cint(rm_obj.consumed_qty)]
|
|
|
|
rm_obj.serial_no = "\n".join(used_serial_nos)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
# Removed the used serial nos from the list
|
|
|
|
for sn in used_serial_nos:
|
2022-03-28 13:22:46 +00:00
|
|
|
self.available_materials[key]["serial_no"].remove(sn)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
|
|
|
def set_consumed_qty_in_po(self):
|
2021-06-15 11:59:52 +00:00
|
|
|
# Update consumed qty back in the purchase order
|
2022-03-31 14:27:42 +00:00
|
|
|
if not self.is_subcontracted:
|
2021-05-24 14:41:15 +00:00
|
|
|
return
|
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__get_purchase_orders()
|
2021-05-24 14:41:15 +00:00
|
|
|
itemwise_consumed_qty = defaultdict(float)
|
2022-03-28 13:22:46 +00:00
|
|
|
for doctype in ["Purchase Receipt", "Purchase Invoice"]:
|
2021-06-15 11:59:52 +00:00
|
|
|
consumed_items, pr_items = self.__update_consumed_materials(doctype, return_consumed_items=True)
|
|
|
|
|
|
|
|
for row in consumed_items:
|
|
|
|
key = (row.rm_item_code, row.main_item_code, pr_items.get(row.reference_name))
|
|
|
|
itemwise_consumed_qty[key] += row.consumed_qty
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
self.__update_consumed_qty_in_po(itemwise_consumed_qty)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-08 05:06:39 +00:00
|
|
|
def __update_consumed_qty_in_po(self, itemwise_consumed_qty):
|
2022-03-28 13:22:46 +00:00
|
|
|
fields = ["main_item_code", "rm_item_code", "parent", "supplied_qty", "name"]
|
|
|
|
filters = {"docstatus": 1, "parent": ("in", self.purchase_orders)}
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
for row in frappe.get_all(
|
|
|
|
"Purchase Order Item Supplied", fields=fields, filters=filters, order_by="idx"
|
|
|
|
):
|
2021-05-24 14:41:15 +00:00
|
|
|
key = (row.rm_item_code, row.main_item_code, row.parent)
|
|
|
|
consumed_qty = itemwise_consumed_qty.get(key, 0)
|
|
|
|
|
|
|
|
if row.supplied_qty < consumed_qty:
|
|
|
|
consumed_qty = row.supplied_qty
|
|
|
|
|
|
|
|
itemwise_consumed_qty[key] -= consumed_qty
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.db.set_value("Purchase Order Item Supplied", row.name, "consumed_qty", consumed_qty)
|
2021-05-24 14:41:15 +00:00
|
|
|
|
2021-06-18 15:07:42 +00:00
|
|
|
def __validate_supplied_items(self):
|
2022-03-28 13:22:46 +00:00
|
|
|
if self.doctype not in ["Purchase Invoice", "Purchase Receipt"]:
|
2021-06-18 15:07:42 +00:00
|
|
|
return
|
|
|
|
|
2021-05-24 14:41:15 +00:00
|
|
|
for row in self.get(self.raw_material_table):
|
2021-06-18 15:07:42 +00:00
|
|
|
key = (row.rm_item_code, row.main_item_code, row.purchase_order)
|
|
|
|
if not self.__transferred_items or not self.__transferred_items.get(key):
|
|
|
|
return
|
|
|
|
|
|
|
|
self.__validate_batch_no(row, key)
|
|
|
|
self.__validate_serial_no(row, key)
|
|
|
|
|
|
|
|
def __validate_batch_no(self, row, key):
|
2022-03-28 13:22:46 +00:00
|
|
|
if row.get("batch_no") and row.get("batch_no") not in self.__transferred_items.get(key).get(
|
|
|
|
"batch_no"
|
|
|
|
):
|
|
|
|
link = get_link_to_form("Purchase Order", row.purchase_order)
|
2021-06-18 15:07:42 +00:00
|
|
|
msg = f'The Batch No {frappe.bold(row.get("batch_no"))} has not supplied against the Purchase Order {link}'
|
|
|
|
frappe.throw(_(msg), title=_("Incorrect Batch Consumed"))
|
|
|
|
|
|
|
|
def __validate_serial_no(self, row, key):
|
2022-03-28 13:22:46 +00:00
|
|
|
if row.get("serial_no"):
|
|
|
|
serial_nos = get_serial_nos(row.get("serial_no"))
|
|
|
|
incorrect_sn = set(serial_nos).difference(self.__transferred_items.get(key).get("serial_no"))
|
2021-06-18 15:07:42 +00:00
|
|
|
|
|
|
|
if incorrect_sn:
|
|
|
|
incorrect_sn = "\n".join(incorrect_sn)
|
2022-03-28 13:22:46 +00:00
|
|
|
link = get_link_to_form("Purchase Order", row.purchase_order)
|
|
|
|
msg = f"The Serial Nos {incorrect_sn} has not supplied against the Purchase Order {link}"
|
2021-08-19 08:11:10 +00:00
|
|
|
frappe.throw(_(msg), title=_("Incorrect Serial Number Consumed"))
|