Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 1 | import copy |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 2 | from collections import defaultdict |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 3 | |
| 4 | import frappe |
| 5 | from frappe import _ |
| 6 | from frappe.utils import cint, flt, get_link_to_form |
| 7 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 8 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos |
| 9 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 10 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 11 | class Subcontracting: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 12 | def set_materials_for_subcontracted_items(self, raw_material_table): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 13 | if self.doctype == "Purchase Invoice" and not self.update_stock: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 14 | return |
| 15 | |
| 16 | self.raw_material_table = raw_material_table |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 17 | self.__identify_change_in_item_table() |
| 18 | self.__prepare_supplied_items() |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 19 | self.__validate_supplied_items() |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 20 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 21 | def __prepare_supplied_items(self): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 22 | self.initialized_fields() |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 23 | self.__get_purchase_orders() |
| 24 | self.__get_pending_qty_to_receive() |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 25 | self.get_available_materials() |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 26 | self.__remove_changed_rows() |
| 27 | self.__set_supplied_items() |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 28 | |
| 29 | def initialized_fields(self): |
| 30 | self.available_materials = frappe._dict() |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 31 | self.__transferred_items = frappe._dict() |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 32 | self.alternative_item_details = frappe._dict() |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 33 | self.__get_backflush_based_on() |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 34 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 35 | def __get_backflush_based_on(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 36 | self.backflush_based_on = frappe.db.get_single_value( |
| 37 | "Buying Settings", "backflush_raw_materials_of_subcontract_based_on" |
| 38 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 39 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 40 | def __get_purchase_orders(self): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 41 | self.purchase_orders = [] |
| 42 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 43 | if self.doctype == "Purchase Order": |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 44 | return |
| 45 | |
| 46 | self.purchase_orders = [d.purchase_order for d in self.items if d.purchase_order] |
| 47 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 48 | def __identify_change_in_item_table(self): |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 49 | self.__changed_name = [] |
| 50 | self.__reference_name = [] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 51 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 52 | if self.doctype == "Purchase Order" or self.is_new(): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 53 | self.set(self.raw_material_table, []) |
| 54 | return |
| 55 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 56 | item_dict = self.__get_data_before_save() |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 57 | if not item_dict: |
| 58 | return True |
| 59 | |
| 60 | for n_row in self.items: |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 61 | self.__reference_name.append(n_row.name) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 62 | if (n_row.name not in item_dict) or (n_row.item_code, n_row.qty) != item_dict[n_row.name]: |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 63 | self.__changed_name.append(n_row.name) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 64 | |
| 65 | if item_dict.get(n_row.name): |
| 66 | del item_dict[n_row.name] |
| 67 | |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 68 | self.__changed_name.extend(item_dict.keys()) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 69 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 70 | def __get_data_before_save(self): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 71 | item_dict = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 72 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"] and self._doc_before_save: |
| 73 | for row in self._doc_before_save.get("items"): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 74 | item_dict[row.name] = (row.item_code, row.qty) |
| 75 | |
| 76 | return item_dict |
| 77 | |
| 78 | def get_available_materials(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 79 | """Get the available raw materials which has been transferred to the supplier. |
| 80 | available_materials = { |
| 81 | (item_code, subcontracted_item, purchase_order): { |
| 82 | 'qty': 1, 'serial_no': [ABC], 'batch_no': {'batch1': 1}, 'data': item_details |
| 83 | } |
| 84 | } |
| 85 | """ |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 86 | if not self.purchase_orders: |
| 87 | return |
| 88 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 89 | for row in self.__get_transferred_items(): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 90 | key = (row.rm_item_code, row.main_item_code, row.purchase_order) |
| 91 | |
| 92 | if key not in self.available_materials: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 93 | self.available_materials.setdefault( |
| 94 | key, |
| 95 | frappe._dict( |
| 96 | { |
| 97 | "qty": 0, |
| 98 | "serial_no": [], |
| 99 | "batch_no": defaultdict(float), |
| 100 | "item_details": row, |
| 101 | "po_details": [], |
| 102 | } |
| 103 | ), |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 104 | ) |
| 105 | |
| 106 | details = self.available_materials[key] |
| 107 | details.qty += row.qty |
| 108 | details.po_details.append(row.po_detail) |
| 109 | |
| 110 | if row.serial_no: |
| 111 | details.serial_no.extend(get_serial_nos(row.serial_no)) |
| 112 | |
| 113 | if row.batch_no: |
| 114 | details.batch_no[row.batch_no] += row.qty |
| 115 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 116 | self.__set_alternative_item_details(row) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 117 | |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 118 | self.__transferred_items = copy.deepcopy(self.available_materials) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 119 | for doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 120 | self.__update_consumed_materials(doctype) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 121 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 122 | def __update_consumed_materials(self, doctype, return_consumed_items=False): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 123 | """Deduct the consumed materials from the available materials.""" |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 124 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 125 | pr_items = self.__get_received_items(doctype) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 126 | if not pr_items: |
| 127 | return ([], {}) if return_consumed_items else None |
| 128 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 129 | pr_items = {d.name: d.get(self.get("po_field") or "purchase_order") for d in pr_items} |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 130 | consumed_materials = self.__get_consumed_items(doctype, pr_items.keys()) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 131 | |
| 132 | if return_consumed_items: |
| 133 | return (consumed_materials, pr_items) |
| 134 | |
| 135 | for row in consumed_materials: |
| 136 | key = (row.rm_item_code, row.main_item_code, pr_items.get(row.reference_name)) |
| 137 | if not self.available_materials.get(key): |
| 138 | continue |
| 139 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 140 | self.available_materials[key]["qty"] -= row.consumed_qty |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 141 | if row.serial_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 142 | self.available_materials[key]["serial_no"] = list( |
| 143 | set(self.available_materials[key]["serial_no"]) - set(get_serial_nos(row.serial_no)) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 144 | ) |
| 145 | |
| 146 | if row.batch_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 147 | self.available_materials[key]["batch_no"][row.batch_no] -= row.consumed_qty |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 148 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 149 | def __get_transferred_items(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 150 | fields = ["`tabStock Entry`.`purchase_order`"] |
| 151 | alias_dict = { |
| 152 | "item_code": "rm_item_code", |
| 153 | "subcontracted_item": "main_item_code", |
| 154 | "basic_rate": "rate", |
| 155 | } |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 156 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 157 | child_table_fields = [ |
| 158 | "item_code", |
| 159 | "item_name", |
| 160 | "description", |
| 161 | "qty", |
| 162 | "basic_rate", |
| 163 | "amount", |
| 164 | "serial_no", |
| 165 | "uom", |
| 166 | "subcontracted_item", |
| 167 | "stock_uom", |
| 168 | "batch_no", |
| 169 | "conversion_factor", |
| 170 | "s_warehouse", |
| 171 | "t_warehouse", |
| 172 | "item_group", |
| 173 | "po_detail", |
| 174 | ] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 175 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 176 | if self.backflush_based_on == "BOM": |
| 177 | child_table_fields.append("original_item") |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 178 | |
| 179 | for field in child_table_fields: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 180 | fields.append(f"`tabStock Entry Detail`.`{field}` As {alias_dict.get(field, field)}") |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 181 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 182 | filters = [ |
| 183 | ["Stock Entry", "docstatus", "=", 1], |
| 184 | ["Stock Entry", "purpose", "=", "Send to Subcontractor"], |
| 185 | ["Stock Entry", "purchase_order", "in", self.purchase_orders], |
| 186 | ] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 187 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 188 | return frappe.get_all("Stock Entry", fields=fields, filters=filters) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 189 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 190 | def __get_received_items(self, doctype): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 191 | fields = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 192 | self.po_field = "purchase_order" |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 193 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 194 | for field in ["name", self.po_field, "parent"]: |
| 195 | fields.append(f"`tab{doctype} Item`.`{field}`") |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 196 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 197 | filters = [ |
| 198 | [doctype, "docstatus", "=", 1], |
| 199 | [f"{doctype} Item", self.po_field, "in", self.purchase_orders], |
| 200 | ] |
| 201 | if doctype == "Purchase Invoice": |
| 202 | filters.append(["Purchase Invoice", "update_stock", "=", 1]) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 203 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 204 | return frappe.get_all(f"{doctype}", fields=fields, filters=filters) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 205 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 206 | def __get_consumed_items(self, doctype, pr_items): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 207 | return frappe.get_all( |
| 208 | "Purchase Receipt Item Supplied", |
| 209 | fields=[ |
| 210 | "serial_no", |
| 211 | "rm_item_code", |
| 212 | "reference_name", |
| 213 | "batch_no", |
| 214 | "consumed_qty", |
| 215 | "main_item_code", |
| 216 | ], |
| 217 | filters={"docstatus": 1, "reference_name": ("in", list(pr_items)), "parenttype": doctype}, |
| 218 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 219 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 220 | def __set_alternative_item_details(self, row): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 221 | if row.get("original_item"): |
| 222 | self.alternative_item_details[row.get("original_item")] = row |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 223 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 224 | def __get_pending_qty_to_receive(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 225 | """Get qty to be received against the purchase order.""" |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 226 | |
| 227 | self.qty_to_be_received = defaultdict(float) |
| 228 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 229 | if ( |
| 230 | self.doctype != "Purchase Order" and self.backflush_based_on != "BOM" and self.purchase_orders |
| 231 | ): |
| 232 | for row in frappe.get_all( |
| 233 | "Purchase Order Item", |
| 234 | fields=["item_code", "(qty - received_qty) as qty", "parent", "name"], |
| 235 | filters={"docstatus": 1, "parent": ("in", self.purchase_orders)}, |
| 236 | ): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 237 | |
| 238 | self.qty_to_be_received[(row.item_code, row.parent)] += row.qty |
| 239 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 240 | def __get_materials_from_bom(self, item_code, bom_no, exploded_item=0): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 241 | doctype = "BOM Item" if not exploded_item else "BOM Explosion Item" |
| 242 | fields = [f"`tab{doctype}`.`stock_qty` / `tabBOM`.`quantity` as qty_consumed_per_unit"] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 243 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 244 | alias_dict = { |
| 245 | "item_code": "rm_item_code", |
| 246 | "name": "bom_detail_no", |
| 247 | "source_warehouse": "reserve_warehouse", |
| 248 | } |
| 249 | for field in [ |
| 250 | "item_code", |
| 251 | "name", |
| 252 | "rate", |
| 253 | "stock_uom", |
| 254 | "source_warehouse", |
| 255 | "description", |
| 256 | "item_name", |
| 257 | "stock_uom", |
| 258 | ]: |
| 259 | fields.append(f"`tab{doctype}`.`{field}` As {alias_dict.get(field, field)}") |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 260 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 261 | filters = [ |
| 262 | [doctype, "parent", "=", bom_no], |
| 263 | [doctype, "docstatus", "=", 1], |
| 264 | ["BOM", "item", "=", item_code], |
| 265 | [doctype, "sourced_by_supplier", "=", 0], |
| 266 | ] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 267 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 268 | return ( |
| 269 | frappe.get_all("BOM", fields=fields, filters=filters, order_by=f"`tab{doctype}`.`idx`") or [] |
| 270 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 271 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 272 | def __remove_changed_rows(self): |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 273 | if not self.__changed_name: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 274 | return |
| 275 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 276 | i = 1 |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 277 | self.set(self.raw_material_table, []) |
| 278 | for d in self._doc_before_save.supplied_items: |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 279 | if d.reference_name in self.__changed_name: |
| 280 | continue |
| 281 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 282 | if d.reference_name not in self.__reference_name: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 283 | continue |
| 284 | |
| 285 | d.idx = i |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 286 | self.append("supplied_items", d) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 287 | |
| 288 | i += 1 |
| 289 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 290 | def __set_supplied_items(self): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 291 | self.bom_items = {} |
| 292 | |
| 293 | has_supplied_items = True if self.get(self.raw_material_table) else False |
| 294 | for row in self.items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 295 | if self.doctype != "Purchase Order" and ( |
| 296 | (self.__changed_name and row.name not in self.__changed_name) |
| 297 | or (has_supplied_items and not self.__changed_name) |
| 298 | ): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 299 | continue |
| 300 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 301 | if self.doctype == "Purchase Order" or self.backflush_based_on == "BOM": |
| 302 | for bom_item in self.__get_materials_from_bom( |
| 303 | row.item_code, row.bom, row.get("include_exploded_items") |
| 304 | ): |
| 305 | qty = flt(bom_item.qty_consumed_per_unit) * flt(row.qty) * row.conversion_factor |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 306 | bom_item.main_item_code = row.item_code |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 307 | self.__update_reserve_warehouse(bom_item, row) |
| 308 | self.__set_alternative_item(bom_item) |
| 309 | self.__add_supplied_item(row, bom_item, qty) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 310 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 311 | elif self.backflush_based_on != "BOM": |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 312 | for key, transfer_item in self.available_materials.items(): |
| 313 | if (key[1], key[2]) == (row.item_code, row.purchase_order) and transfer_item.qty > 0: |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 314 | qty = self.__get_qty_based_on_material_transfer(row, transfer_item) or 0 |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 315 | transfer_item.qty -= qty |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 316 | self.__add_supplied_item(row, transfer_item.get("item_details"), qty) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 317 | |
| 318 | if self.qty_to_be_received: |
| 319 | self.qty_to_be_received[(row.item_code, row.purchase_order)] -= row.qty |
| 320 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 321 | def __update_reserve_warehouse(self, row, item): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 322 | if self.doctype == "Purchase Order": |
| 323 | row.reserve_warehouse = self.set_reserve_warehouse or item.warehouse |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 324 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 325 | def __get_qty_based_on_material_transfer(self, item_row, transfer_item): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 326 | key = (item_row.item_code, item_row.purchase_order) |
| 327 | |
| 328 | if self.qty_to_be_received == item_row.qty: |
| 329 | return transfer_item.qty |
| 330 | |
| 331 | if self.qty_to_be_received: |
| 332 | qty = (flt(item_row.qty) * flt(transfer_item.qty)) / flt(self.qty_to_be_received.get(key, 0)) |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 333 | transfer_item.item_details.required_qty = transfer_item.qty |
| 334 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 335 | if transfer_item.serial_no or frappe.get_cached_value( |
| 336 | "UOM", transfer_item.item_details.stock_uom, "must_be_whole_number" |
| 337 | ): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 338 | return frappe.utils.ceil(qty) |
| 339 | |
| 340 | return qty |
| 341 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 342 | def __set_alternative_item(self, bom_item): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 343 | if self.alternative_item_details.get(bom_item.rm_item_code): |
| 344 | bom_item.update(self.alternative_item_details[bom_item.rm_item_code]) |
| 345 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 346 | def __add_supplied_item(self, item_row, bom_item, qty): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 347 | bom_item.conversion_factor = item_row.conversion_factor |
| 348 | rm_obj = self.append(self.raw_material_table, bom_item) |
| 349 | rm_obj.reference_name = item_row.name |
| 350 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 351 | if self.doctype == "Purchase Order": |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 352 | rm_obj.required_qty = qty |
| 353 | else: |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 354 | rm_obj.consumed_qty = 0 |
| 355 | rm_obj.purchase_order = item_row.purchase_order |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 356 | self.__set_batch_nos(bom_item, item_row, rm_obj, qty) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 357 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 358 | def __set_batch_nos(self, bom_item, item_row, rm_obj, qty): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 359 | key = (rm_obj.rm_item_code, item_row.item_code, item_row.purchase_order) |
| 360 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 361 | if self.available_materials.get(key) and self.available_materials[key]["batch_no"]: |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 362 | new_rm_obj = None |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 363 | for batch_no, batch_qty in self.available_materials[key]["batch_no"].items(): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 364 | if batch_qty >= qty: |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 365 | self.__set_batch_no_as_per_qty(item_row, rm_obj, batch_no, qty) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 366 | self.available_materials[key]["batch_no"][batch_no] -= qty |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 367 | return |
| 368 | |
| 369 | elif qty > 0 and batch_qty > 0: |
| 370 | qty -= batch_qty |
| 371 | new_rm_obj = self.append(self.raw_material_table, bom_item) |
| 372 | new_rm_obj.reference_name = item_row.name |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 373 | self.__set_batch_no_as_per_qty(item_row, new_rm_obj, batch_no, batch_qty) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 374 | self.available_materials[key]["batch_no"][batch_no] = 0 |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 375 | |
| 376 | if abs(qty) > 0 and not new_rm_obj: |
| 377 | self.__set_consumed_qty(rm_obj, qty) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 378 | else: |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 379 | self.__set_consumed_qty(rm_obj, qty, bom_item.required_qty or qty) |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 380 | self.__set_serial_nos(item_row, rm_obj) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 381 | |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 382 | def __set_consumed_qty(self, rm_obj, consumed_qty, required_qty=0): |
| 383 | rm_obj.required_qty = required_qty |
| 384 | rm_obj.consumed_qty = consumed_qty |
| 385 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 386 | def __set_batch_no_as_per_qty(self, item_row, rm_obj, batch_no, qty): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 387 | rm_obj.update( |
| 388 | { |
| 389 | "consumed_qty": qty, |
| 390 | "batch_no": batch_no, |
| 391 | "required_qty": qty, |
| 392 | "purchase_order": item_row.purchase_order, |
| 393 | } |
| 394 | ) |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 395 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 396 | self.__set_serial_nos(item_row, rm_obj) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 397 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 398 | def __set_serial_nos(self, item_row, rm_obj): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 399 | key = (rm_obj.rm_item_code, item_row.item_code, item_row.purchase_order) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 400 | if self.available_materials.get(key) and self.available_materials[key]["serial_no"]: |
| 401 | used_serial_nos = self.available_materials[key]["serial_no"][0 : cint(rm_obj.consumed_qty)] |
| 402 | rm_obj.serial_no = "\n".join(used_serial_nos) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 403 | |
| 404 | # Removed the used serial nos from the list |
| 405 | for sn in used_serial_nos: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 406 | self.available_materials[key]["serial_no"].remove(sn) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 407 | |
| 408 | def set_consumed_qty_in_po(self): |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 409 | # Update consumed qty back in the purchase order |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 410 | if not self.is_subcontracted: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 411 | return |
| 412 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 413 | self.__get_purchase_orders() |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 414 | itemwise_consumed_qty = defaultdict(float) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 415 | for doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 416 | consumed_items, pr_items = self.__update_consumed_materials(doctype, return_consumed_items=True) |
| 417 | |
| 418 | for row in consumed_items: |
| 419 | key = (row.rm_item_code, row.main_item_code, pr_items.get(row.reference_name)) |
| 420 | itemwise_consumed_qty[key] += row.consumed_qty |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 421 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 422 | self.__update_consumed_qty_in_po(itemwise_consumed_qty) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 423 | |
Rohit Waghchaure | 6bbc8ec | 2021-06-08 10:36:39 +0530 | [diff] [blame] | 424 | def __update_consumed_qty_in_po(self, itemwise_consumed_qty): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 425 | fields = ["main_item_code", "rm_item_code", "parent", "supplied_qty", "name"] |
| 426 | filters = {"docstatus": 1, "parent": ("in", self.purchase_orders)} |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 427 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 428 | for row in frappe.get_all( |
| 429 | "Purchase Order Item Supplied", fields=fields, filters=filters, order_by="idx" |
| 430 | ): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 431 | key = (row.rm_item_code, row.main_item_code, row.parent) |
| 432 | consumed_qty = itemwise_consumed_qty.get(key, 0) |
| 433 | |
| 434 | if row.supplied_qty < consumed_qty: |
| 435 | consumed_qty = row.supplied_qty |
| 436 | |
| 437 | itemwise_consumed_qty[key] -= consumed_qty |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 438 | frappe.db.set_value("Purchase Order Item Supplied", row.name, "consumed_qty", consumed_qty) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 439 | |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 440 | def __validate_supplied_items(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 441 | if self.doctype not in ["Purchase Invoice", "Purchase Receipt"]: |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 442 | return |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 443 | |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 444 | for row in self.get(self.raw_material_table): |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 445 | key = (row.rm_item_code, row.main_item_code, row.purchase_order) |
| 446 | if not self.__transferred_items or not self.__transferred_items.get(key): |
| 447 | return |
| 448 | |
| 449 | self.__validate_batch_no(row, key) |
| 450 | self.__validate_serial_no(row, key) |
| 451 | |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 452 | def __validate_batch_no(self, row, key): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 453 | if row.get("batch_no") and row.get("batch_no") not in self.__transferred_items.get(key).get( |
| 454 | "batch_no" |
| 455 | ): |
| 456 | link = get_link_to_form("Purchase Order", row.purchase_order) |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 457 | msg = f'The Batch No {frappe.bold(row.get("batch_no"))} has not supplied against the Purchase Order {link}' |
| 458 | frappe.throw(_(msg), title=_("Incorrect Batch Consumed")) |
| 459 | |
| 460 | def __validate_serial_no(self, row, key): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 461 | if row.get("serial_no"): |
| 462 | serial_nos = get_serial_nos(row.get("serial_no")) |
| 463 | incorrect_sn = set(serial_nos).difference(self.__transferred_items.get(key).get("serial_no")) |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 464 | |
| 465 | if incorrect_sn: |
| 466 | incorrect_sn = "\n".join(incorrect_sn) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 467 | link = get_link_to_form("Purchase Order", row.purchase_order) |
| 468 | msg = f"The Serial Nos {incorrect_sn} has not supplied against the Purchase Order {link}" |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 469 | frappe.throw(_(msg), title=_("Incorrect Serial Number Consumed")) |