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