Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1 | import copy |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 2 | import unittest |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 3 | from collections import defaultdict |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
| 5 | import frappe |
| 6 | from frappe.utils import cint |
| 7 | |
| 8 | from erpnext.buying.doctype.purchase_order.purchase_order import ( |
| 9 | get_materials_from_supplier, |
| 10 | make_purchase_invoice, |
| 11 | make_purchase_receipt, |
| 12 | make_rm_stock_entry, |
| 13 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 14 | from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 15 | from erpnext.manufacturing.doctype.production_plan.test_production_plan import make_bom |
| 16 | from erpnext.stock.doctype.item.test_item import make_item |
| 17 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos |
| 18 | from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry |
| 19 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 20 | |
| 21 | class TestSubcontracting(unittest.TestCase): |
| 22 | def setUp(self): |
| 23 | make_subcontract_items() |
| 24 | make_raw_materials() |
| 25 | make_bom_for_subcontracted_items() |
| 26 | |
| 27 | def test_po_with_bom(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 28 | """ |
| 29 | - Set backflush based on BOM |
| 30 | - Create subcontracted PO for the item Subcontracted Item SA1 and add same item two times. |
| 31 | - Transfer the components from Stores to Supplier warehouse with batch no and serial nos. |
| 32 | - Create purchase receipt against the PO and check serial nos and batch no. |
| 33 | """ |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 34 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 35 | set_backflush_based_on("BOM") |
| 36 | item_code = "Subcontracted Item SA1" |
| 37 | items = [ |
| 38 | {"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 5, "rate": 100}, |
| 39 | {"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 6, "rate": 100}, |
| 40 | ] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 41 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 42 | rm_items = [ |
| 43 | {"item_code": "Subcontracted SRM Item 1", "qty": 5}, |
| 44 | {"item_code": "Subcontracted SRM Item 2", "qty": 5}, |
| 45 | {"item_code": "Subcontracted SRM Item 3", "qty": 5}, |
| 46 | {"item_code": "Subcontracted SRM Item 1", "qty": 6}, |
| 47 | {"item_code": "Subcontracted SRM Item 2", "qty": 6}, |
| 48 | {"item_code": "Subcontracted SRM Item 3", "qty": 6}, |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 49 | ] |
| 50 | |
| 51 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 52 | po = create_purchase_order( |
| 53 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 54 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 55 | |
| 56 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 57 | d["po_detail"] = po.items[0].name if d.get("qty") == 5 else po.items[1].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 58 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 59 | make_stock_transfer_entry( |
| 60 | po_no=po.name, |
| 61 | main_item_code=item_code, |
| 62 | rm_items=rm_items, |
| 63 | itemwise_details=copy.deepcopy(itemwise_details), |
| 64 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 65 | |
| 66 | pr1 = make_purchase_receipt(po.name) |
| 67 | pr1.submit() |
| 68 | |
| 69 | for key, value in get_supplied_items(pr1).items(): |
| 70 | transferred_detais = itemwise_details.get(key) |
| 71 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 72 | for field in ["qty", "serial_no", "batch_no"]: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 73 | if value.get(field): |
| 74 | transfer, consumed = (transferred_detais.get(field), value.get(field)) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 75 | if field == "serial_no": |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 76 | transfer, consumed = (sorted(transfer), sorted(consumed)) |
| 77 | |
| 78 | self.assertEqual(transfer, consumed) |
| 79 | |
| 80 | def test_po_with_material_transfer(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 81 | """ |
| 82 | - Set backflush based on Material Transfer |
| 83 | - Create subcontracted PO for the item Subcontracted Item SA1 and Subcontracted Item SA5. |
| 84 | - Transfer the components from Stores to Supplier warehouse with batch no and serial nos. |
| 85 | - Transfer extra item Subcontracted SRM Item 4 for the subcontract item Subcontracted Item SA5. |
| 86 | - Create partial purchase receipt against the PO and check serial nos and batch no. |
| 87 | """ |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 88 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 89 | set_backflush_based_on("Material Transferred for Subcontract") |
| 90 | items = [ |
| 91 | { |
| 92 | "warehouse": "_Test Warehouse - _TC", |
| 93 | "item_code": "Subcontracted Item SA1", |
| 94 | "qty": 5, |
| 95 | "rate": 100, |
| 96 | }, |
| 97 | { |
| 98 | "warehouse": "_Test Warehouse - _TC", |
| 99 | "item_code": "Subcontracted Item SA5", |
| 100 | "qty": 6, |
| 101 | "rate": 100, |
| 102 | }, |
| 103 | ] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 104 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 105 | rm_items = [ |
| 106 | {"item_code": "Subcontracted SRM Item 1", "qty": 5, "main_item_code": "Subcontracted Item SA1"}, |
| 107 | {"item_code": "Subcontracted SRM Item 2", "qty": 5, "main_item_code": "Subcontracted Item SA1"}, |
| 108 | {"item_code": "Subcontracted SRM Item 3", "qty": 5, "main_item_code": "Subcontracted Item SA1"}, |
| 109 | {"item_code": "Subcontracted SRM Item 5", "qty": 6, "main_item_code": "Subcontracted Item SA5"}, |
| 110 | {"item_code": "Subcontracted SRM Item 4", "qty": 6, "main_item_code": "Subcontracted Item SA5"}, |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 111 | ] |
| 112 | |
| 113 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 114 | po = create_purchase_order( |
| 115 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 116 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 117 | |
| 118 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 119 | d["po_detail"] = po.items[0].name if d.get("qty") == 5 else po.items[1].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 120 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 121 | make_stock_transfer_entry( |
| 122 | po_no=po.name, rm_items=rm_items, itemwise_details=copy.deepcopy(itemwise_details) |
| 123 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 124 | |
| 125 | pr1 = make_purchase_receipt(po.name) |
| 126 | pr1.remove(pr1.items[1]) |
| 127 | pr1.submit() |
| 128 | |
| 129 | for key, value in get_supplied_items(pr1).items(): |
| 130 | transferred_detais = itemwise_details.get(key) |
| 131 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 132 | for field in ["qty", "serial_no", "batch_no"]: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 133 | if value.get(field): |
| 134 | self.assertEqual(value.get(field), transferred_detais.get(field)) |
| 135 | |
| 136 | pr2 = make_purchase_receipt(po.name) |
| 137 | pr2.submit() |
| 138 | |
| 139 | for key, value in get_supplied_items(pr2).items(): |
| 140 | transferred_detais = itemwise_details.get(key) |
| 141 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 142 | for field in ["qty", "serial_no", "batch_no"]: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 143 | if value.get(field): |
| 144 | self.assertEqual(value.get(field), transferred_detais.get(field)) |
| 145 | |
| 146 | def test_subcontract_with_same_components_different_fg(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 147 | """ |
| 148 | - Set backflush based on Material Transfer |
| 149 | - Create subcontracted PO for the item Subcontracted Item SA2 and Subcontracted Item SA3. |
| 150 | - Transfer the components from Stores to Supplier warehouse with serial nos. |
| 151 | - Transfer extra qty of components for the item Subcontracted Item SA2. |
| 152 | - Create partial purchase receipt against the PO and check serial nos and batch no. |
| 153 | """ |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 154 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 155 | set_backflush_based_on("Material Transferred for Subcontract") |
| 156 | items = [ |
| 157 | { |
| 158 | "warehouse": "_Test Warehouse - _TC", |
| 159 | "item_code": "Subcontracted Item SA2", |
| 160 | "qty": 5, |
| 161 | "rate": 100, |
| 162 | }, |
| 163 | { |
| 164 | "warehouse": "_Test Warehouse - _TC", |
| 165 | "item_code": "Subcontracted Item SA3", |
| 166 | "qty": 6, |
| 167 | "rate": 100, |
| 168 | }, |
| 169 | ] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 170 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 171 | rm_items = [ |
| 172 | {"item_code": "Subcontracted SRM Item 2", "qty": 6, "main_item_code": "Subcontracted Item SA2"}, |
| 173 | {"item_code": "Subcontracted SRM Item 2", "qty": 6, "main_item_code": "Subcontracted Item SA3"}, |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 174 | ] |
| 175 | |
| 176 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 177 | po = create_purchase_order( |
| 178 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 179 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 180 | |
| 181 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 182 | d["po_detail"] = po.items[0].name if d.get("qty") == 5 else po.items[1].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 183 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 184 | make_stock_transfer_entry( |
| 185 | po_no=po.name, rm_items=rm_items, itemwise_details=copy.deepcopy(itemwise_details) |
| 186 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 187 | |
| 188 | pr1 = make_purchase_receipt(po.name) |
| 189 | pr1.items[0].qty = 3 |
| 190 | pr1.remove(pr1.items[1]) |
| 191 | pr1.submit() |
| 192 | |
| 193 | for key, value in get_supplied_items(pr1).items(): |
| 194 | transferred_detais = itemwise_details.get(key) |
| 195 | self.assertEqual(value.qty, 4) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 196 | self.assertEqual(sorted(value.serial_no), sorted(transferred_detais.get("serial_no")[0:4])) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 197 | |
| 198 | pr2 = make_purchase_receipt(po.name) |
| 199 | pr2.items[0].qty = 2 |
| 200 | pr2.remove(pr2.items[1]) |
| 201 | pr2.submit() |
| 202 | |
| 203 | for key, value in get_supplied_items(pr2).items(): |
| 204 | transferred_detais = itemwise_details.get(key) |
| 205 | |
| 206 | self.assertEqual(value.qty, 2) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 207 | self.assertEqual(sorted(value.serial_no), sorted(transferred_detais.get("serial_no")[4:6])) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 208 | |
| 209 | pr3 = make_purchase_receipt(po.name) |
| 210 | pr3.submit() |
| 211 | for key, value in get_supplied_items(pr3).items(): |
| 212 | transferred_detais = itemwise_details.get(key) |
| 213 | |
| 214 | self.assertEqual(value.qty, 6) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 215 | self.assertEqual(sorted(value.serial_no), sorted(transferred_detais.get("serial_no")[6:12])) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 216 | |
| 217 | def test_return_non_consumed_materials(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 218 | """ |
| 219 | - Set backflush based on Material Transfer |
| 220 | - Create subcontracted PO for the item Subcontracted Item SA2. |
| 221 | - Transfer the components from Stores to Supplier warehouse with serial nos. |
| 222 | - Transfer extra qty of component for the subcontracted item Subcontracted Item SA2. |
| 223 | - Create purchase receipt for full qty against the PO and change the qty of raw material. |
| 224 | - After that return the non consumed material back to the store from supplier's warehouse. |
| 225 | """ |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 226 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 227 | set_backflush_based_on("Material Transferred for Subcontract") |
| 228 | items = [ |
| 229 | { |
| 230 | "warehouse": "_Test Warehouse - _TC", |
| 231 | "item_code": "Subcontracted Item SA2", |
| 232 | "qty": 5, |
| 233 | "rate": 100, |
| 234 | } |
| 235 | ] |
| 236 | rm_items = [ |
| 237 | {"item_code": "Subcontracted SRM Item 2", "qty": 6, "main_item_code": "Subcontracted Item SA2"} |
| 238 | ] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 239 | |
| 240 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 241 | po = create_purchase_order( |
| 242 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 243 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 244 | |
| 245 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 246 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 247 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 248 | make_stock_transfer_entry( |
| 249 | po_no=po.name, rm_items=rm_items, itemwise_details=copy.deepcopy(itemwise_details) |
| 250 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 251 | |
| 252 | pr1 = make_purchase_receipt(po.name) |
| 253 | pr1.save() |
| 254 | pr1.supplied_items[0].consumed_qty = 5 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 255 | pr1.supplied_items[0].serial_no = "\n".join( |
| 256 | sorted(itemwise_details.get("Subcontracted SRM Item 2").get("serial_no")[0:5]) |
| 257 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 258 | pr1.submit() |
| 259 | |
| 260 | for key, value in get_supplied_items(pr1).items(): |
| 261 | transferred_detais = itemwise_details.get(key) |
| 262 | self.assertEqual(value.qty, 5) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 263 | self.assertEqual(sorted(value.serial_no), sorted(transferred_detais.get("serial_no")[0:5])) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 264 | |
| 265 | po.load_from_db() |
| 266 | self.assertEqual(po.supplied_items[0].consumed_qty, 5) |
| 267 | doc = get_materials_from_supplier(po.name, [d.name for d in po.supplied_items]) |
| 268 | self.assertEqual(doc.items[0].qty, 1) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 269 | self.assertEqual(doc.items[0].s_warehouse, "_Test Warehouse 1 - _TC") |
| 270 | self.assertEqual(doc.items[0].t_warehouse, "_Test Warehouse - _TC") |
| 271 | self.assertEqual( |
| 272 | get_serial_nos(doc.items[0].serial_no), |
| 273 | itemwise_details.get(doc.items[0].item_code)["serial_no"][5:6], |
| 274 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 275 | |
| 276 | def test_item_with_batch_based_on_bom(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 277 | """ |
| 278 | - Set backflush based on BOM |
| 279 | - Create subcontracted PO for the item Subcontracted Item SA4 (has batch no). |
| 280 | - Transfer the components from Stores to Supplier warehouse with batch no and serial nos. |
| 281 | - Transfer the components in multiple batches. |
| 282 | - Create the 3 purchase receipt against the PO and split Subcontracted Items into two batches. |
| 283 | - Keep the qty as 2 for Subcontracted Item in the purchase receipt. |
| 284 | """ |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 285 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 286 | set_backflush_based_on("BOM") |
| 287 | item_code = "Subcontracted Item SA4" |
| 288 | items = [{"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 10, "rate": 100}] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 289 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 290 | rm_items = [ |
| 291 | {"item_code": "Subcontracted SRM Item 1", "qty": 10}, |
| 292 | {"item_code": "Subcontracted SRM Item 2", "qty": 10}, |
| 293 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 294 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 295 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 296 | {"item_code": "Subcontracted SRM Item 3", "qty": 1}, |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 297 | ] |
| 298 | |
| 299 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 300 | po = create_purchase_order( |
| 301 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 302 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 303 | |
| 304 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 305 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 306 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 307 | make_stock_transfer_entry( |
| 308 | po_no=po.name, |
| 309 | main_item_code=item_code, |
| 310 | rm_items=rm_items, |
| 311 | itemwise_details=copy.deepcopy(itemwise_details), |
| 312 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 313 | |
| 314 | pr1 = make_purchase_receipt(po.name) |
| 315 | pr1.items[0].qty = 2 |
| 316 | add_second_row_in_pr(pr1) |
| 317 | pr1.save() |
| 318 | pr1.submit() |
| 319 | |
| 320 | for key, value in get_supplied_items(pr1).items(): |
| 321 | self.assertEqual(value.qty, 4) |
| 322 | |
| 323 | pr1 = make_purchase_receipt(po.name) |
| 324 | pr1.items[0].qty = 2 |
| 325 | add_second_row_in_pr(pr1) |
| 326 | pr1.save() |
| 327 | pr1.submit() |
| 328 | |
| 329 | for key, value in get_supplied_items(pr1).items(): |
| 330 | self.assertEqual(value.qty, 4) |
| 331 | |
| 332 | pr1 = make_purchase_receipt(po.name) |
| 333 | pr1.items[0].qty = 2 |
| 334 | pr1.save() |
| 335 | pr1.submit() |
| 336 | |
| 337 | for key, value in get_supplied_items(pr1).items(): |
| 338 | self.assertEqual(value.qty, 2) |
| 339 | |
| 340 | def test_item_with_batch_based_on_material_transfer(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 341 | """ |
| 342 | - Set backflush based on Material Transferred for Subcontract |
| 343 | - Create subcontracted PO for the item Subcontracted Item SA4 (has batch no). |
| 344 | - Transfer the components from Stores to Supplier warehouse with batch no and serial nos. |
| 345 | - Transfer the components in multiple batches with extra 2 qty for the batched item. |
| 346 | - Create the 3 purchase receipt against the PO and split Subcontracted Items into two batches. |
| 347 | - Keep the qty as 2 for Subcontracted Item in the purchase receipt. |
| 348 | - In the first purchase receipt the batched raw materials will be consumed 2 extra qty. |
| 349 | """ |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 350 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 351 | set_backflush_based_on("Material Transferred for Subcontract") |
| 352 | item_code = "Subcontracted Item SA4" |
| 353 | items = [{"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 10, "rate": 100}] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 354 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 355 | rm_items = [ |
| 356 | {"item_code": "Subcontracted SRM Item 1", "qty": 10}, |
| 357 | {"item_code": "Subcontracted SRM Item 2", "qty": 10}, |
| 358 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 359 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 360 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 361 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 362 | ] |
| 363 | |
| 364 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 365 | po = create_purchase_order( |
| 366 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 367 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 368 | |
| 369 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 370 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 371 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 372 | make_stock_transfer_entry( |
| 373 | po_no=po.name, |
| 374 | main_item_code=item_code, |
| 375 | rm_items=rm_items, |
| 376 | itemwise_details=copy.deepcopy(itemwise_details), |
| 377 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 378 | |
| 379 | pr1 = make_purchase_receipt(po.name) |
| 380 | pr1.items[0].qty = 2 |
| 381 | add_second_row_in_pr(pr1) |
| 382 | pr1.save() |
| 383 | pr1.submit() |
| 384 | |
| 385 | for key, value in get_supplied_items(pr1).items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 386 | qty = 4 if key != "Subcontracted SRM Item 3" else 6 |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 387 | self.assertEqual(value.qty, qty) |
| 388 | |
| 389 | pr1 = make_purchase_receipt(po.name) |
| 390 | pr1.items[0].qty = 2 |
| 391 | add_second_row_in_pr(pr1) |
| 392 | pr1.save() |
| 393 | pr1.submit() |
| 394 | |
| 395 | for key, value in get_supplied_items(pr1).items(): |
| 396 | self.assertEqual(value.qty, 4) |
| 397 | |
| 398 | pr1 = make_purchase_receipt(po.name) |
| 399 | pr1.items[0].qty = 2 |
| 400 | pr1.save() |
| 401 | pr1.submit() |
| 402 | |
| 403 | for key, value in get_supplied_items(pr1).items(): |
| 404 | self.assertEqual(value.qty, 2) |
| 405 | |
| 406 | def test_partial_transfer_serial_no_components_based_on_material_transfer(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 407 | """ |
| 408 | - Set backflush based on Material Transferred for Subcontract |
| 409 | - Create subcontracted PO for the item Subcontracted Item SA2. |
| 410 | - Transfer the partial components from Stores to Supplier warehouse with serial nos. |
| 411 | - Create partial purchase receipt against the PO and change the qty manually. |
| 412 | - Transfer the remaining components from Stores to Supplier warehouse with serial nos. |
| 413 | - Create purchase receipt for remaining qty against the PO and change the qty manually. |
| 414 | """ |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 415 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 416 | set_backflush_based_on("Material Transferred for Subcontract") |
| 417 | item_code = "Subcontracted Item SA2" |
| 418 | items = [{"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 10, "rate": 100}] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 419 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 420 | rm_items = [{"item_code": "Subcontracted SRM Item 2", "qty": 5}] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 421 | |
| 422 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 423 | po = create_purchase_order( |
| 424 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 425 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 426 | |
| 427 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 428 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 429 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 430 | make_stock_transfer_entry( |
| 431 | po_no=po.name, |
| 432 | main_item_code=item_code, |
| 433 | rm_items=rm_items, |
| 434 | itemwise_details=copy.deepcopy(itemwise_details), |
| 435 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 436 | |
| 437 | pr1 = make_purchase_receipt(po.name) |
| 438 | pr1.items[0].qty = 5 |
| 439 | pr1.save() |
| 440 | |
| 441 | for key, value in get_supplied_items(pr1).items(): |
| 442 | details = itemwise_details.get(key) |
| 443 | self.assertEqual(value.qty, 3) |
| 444 | self.assertEqual(sorted(value.serial_no), sorted(details.serial_no[0:3])) |
| 445 | |
| 446 | pr1.load_from_db() |
| 447 | pr1.supplied_items[0].consumed_qty = 5 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 448 | pr1.supplied_items[0].serial_no = "\n".join( |
| 449 | itemwise_details[pr1.supplied_items[0].rm_item_code]["serial_no"] |
| 450 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 451 | pr1.save() |
| 452 | pr1.submit() |
| 453 | |
| 454 | for key, value in get_supplied_items(pr1).items(): |
| 455 | details = itemwise_details.get(key) |
| 456 | self.assertEqual(value.qty, details.qty) |
| 457 | self.assertEqual(sorted(value.serial_no), sorted(details.serial_no)) |
| 458 | |
| 459 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
| 460 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 461 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 462 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 463 | make_stock_transfer_entry( |
| 464 | po_no=po.name, |
| 465 | main_item_code=item_code, |
| 466 | rm_items=rm_items, |
| 467 | itemwise_details=copy.deepcopy(itemwise_details), |
| 468 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 469 | |
| 470 | pr1 = make_purchase_receipt(po.name) |
| 471 | pr1.submit() |
| 472 | |
| 473 | for key, value in get_supplied_items(pr1).items(): |
| 474 | details = itemwise_details.get(key) |
| 475 | self.assertEqual(value.qty, details.qty) |
| 476 | self.assertEqual(sorted(value.serial_no), sorted(details.serial_no)) |
| 477 | |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 478 | def test_incorrect_serial_no_components_based_on_material_transfer(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 479 | """ |
| 480 | - Set backflush based on Material Transferred for Subcontract |
| 481 | - Create subcontracted PO for the item Subcontracted Item SA2. |
| 482 | - Transfer the serialized componenets to the supplier. |
| 483 | - Create purchase receipt and change the serial no which is not transferred. |
| 484 | - System should throw the error and not allowed to save the purchase receipt. |
| 485 | """ |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 486 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 487 | set_backflush_based_on("Material Transferred for Subcontract") |
| 488 | item_code = "Subcontracted Item SA2" |
| 489 | items = [{"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 10, "rate": 100}] |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 490 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 491 | rm_items = [{"item_code": "Subcontracted SRM Item 2", "qty": 10}] |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 492 | |
| 493 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 494 | po = create_purchase_order( |
| 495 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 496 | ) |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 497 | |
| 498 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 499 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 500 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 501 | make_stock_transfer_entry( |
| 502 | po_no=po.name, |
| 503 | main_item_code=item_code, |
| 504 | rm_items=rm_items, |
| 505 | itemwise_details=copy.deepcopy(itemwise_details), |
| 506 | ) |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 507 | |
| 508 | pr1 = make_purchase_receipt(po.name) |
| 509 | pr1.save() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 510 | pr1.supplied_items[0].serial_no = "ABCD" |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 511 | self.assertRaises(frappe.ValidationError, pr1.save) |
| 512 | pr1.delete() |
| 513 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 514 | def test_partial_transfer_batch_based_on_material_transfer(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 515 | """ |
| 516 | - Set backflush based on Material Transferred for Subcontract |
| 517 | - Create subcontracted PO for the item Subcontracted Item SA6. |
| 518 | - Transfer the partial components from Stores to Supplier warehouse with batch. |
| 519 | - Create partial purchase receipt against the PO and change the qty manually. |
| 520 | - Transfer the remaining components from Stores to Supplier warehouse with batch. |
| 521 | - Create purchase receipt for remaining qty against the PO and change the qty manually. |
| 522 | """ |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 523 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 524 | set_backflush_based_on("Material Transferred for Subcontract") |
| 525 | item_code = "Subcontracted Item SA6" |
| 526 | items = [{"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 10, "rate": 100}] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 527 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 528 | rm_items = [{"item_code": "Subcontracted SRM Item 3", "qty": 5}] |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 529 | |
| 530 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 531 | po = create_purchase_order( |
| 532 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 533 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 534 | |
| 535 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 536 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 537 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 538 | make_stock_transfer_entry( |
| 539 | po_no=po.name, |
| 540 | main_item_code=item_code, |
| 541 | rm_items=rm_items, |
| 542 | itemwise_details=copy.deepcopy(itemwise_details), |
| 543 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 544 | |
| 545 | pr1 = make_purchase_receipt(po.name) |
| 546 | pr1.items[0].qty = 5 |
| 547 | pr1.save() |
| 548 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 549 | transferred_batch_no = "" |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 550 | for key, value in get_supplied_items(pr1).items(): |
| 551 | details = itemwise_details.get(key) |
| 552 | self.assertEqual(value.qty, 3) |
| 553 | transferred_batch_no = details.batch_no |
| 554 | self.assertEqual(value.batch_no, details.batch_no) |
| 555 | |
| 556 | pr1.load_from_db() |
| 557 | pr1.supplied_items[0].consumed_qty = 5 |
| 558 | pr1.supplied_items[0].batch_no = list(transferred_batch_no.keys())[0] |
| 559 | pr1.save() |
| 560 | pr1.submit() |
| 561 | |
| 562 | for key, value in get_supplied_items(pr1).items(): |
| 563 | details = itemwise_details.get(key) |
| 564 | self.assertEqual(value.qty, details.qty) |
| 565 | self.assertEqual(value.batch_no, details.batch_no) |
| 566 | |
| 567 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
| 568 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 569 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 570 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 571 | make_stock_transfer_entry( |
| 572 | po_no=po.name, |
| 573 | main_item_code=item_code, |
| 574 | rm_items=rm_items, |
| 575 | itemwise_details=copy.deepcopy(itemwise_details), |
| 576 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 577 | |
| 578 | pr1 = make_purchase_receipt(po.name) |
| 579 | pr1.submit() |
| 580 | |
| 581 | for key, value in get_supplied_items(pr1).items(): |
| 582 | details = itemwise_details.get(key) |
| 583 | self.assertEqual(value.qty, details.qty) |
| 584 | self.assertEqual(value.batch_no, details.batch_no) |
| 585 | |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 586 | def test_item_with_batch_based_on_material_transfer_for_purchase_invoice(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 587 | """ |
| 588 | - Set backflush based on Material Transferred for Subcontract |
| 589 | - Create subcontracted PO for the item Subcontracted Item SA4 (has batch no). |
| 590 | - Transfer the components from Stores to Supplier warehouse with batch no and serial nos. |
| 591 | - Transfer the components in multiple batches with extra 2 qty for the batched item. |
| 592 | - Create the 3 purchase receipt against the PO and split Subcontracted Items into two batches. |
| 593 | - Keep the qty as 2 for Subcontracted Item in the purchase receipt. |
| 594 | - In the first purchase receipt the batched raw materials will be consumed 2 extra qty. |
| 595 | """ |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 596 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 597 | set_backflush_based_on("Material Transferred for Subcontract") |
| 598 | item_code = "Subcontracted Item SA4" |
| 599 | items = [{"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 10, "rate": 100}] |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 600 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 601 | rm_items = [ |
| 602 | {"item_code": "Subcontracted SRM Item 1", "qty": 10}, |
| 603 | {"item_code": "Subcontracted SRM Item 2", "qty": 10}, |
| 604 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 605 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 606 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 607 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 608 | ] |
| 609 | |
| 610 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 611 | po = create_purchase_order( |
| 612 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 613 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 614 | |
| 615 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 616 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 617 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 618 | make_stock_transfer_entry( |
| 619 | po_no=po.name, |
| 620 | main_item_code=item_code, |
| 621 | rm_items=rm_items, |
| 622 | itemwise_details=copy.deepcopy(itemwise_details), |
| 623 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 624 | |
| 625 | pr1 = make_purchase_invoice(po.name) |
| 626 | pr1.update_stock = 1 |
| 627 | pr1.items[0].qty = 2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 628 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 629 | add_second_row_in_pr(pr1) |
| 630 | pr1.save() |
| 631 | pr1.submit() |
| 632 | |
| 633 | for key, value in get_supplied_items(pr1).items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 634 | qty = 4 if key != "Subcontracted SRM Item 3" else 6 |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 635 | self.assertEqual(value.qty, qty) |
| 636 | |
| 637 | pr1 = make_purchase_invoice(po.name) |
| 638 | pr1.update_stock = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 639 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 640 | pr1.items[0].qty = 2 |
| 641 | add_second_row_in_pr(pr1) |
| 642 | pr1.save() |
| 643 | pr1.submit() |
| 644 | |
| 645 | for key, value in get_supplied_items(pr1).items(): |
| 646 | self.assertEqual(value.qty, 4) |
| 647 | |
| 648 | pr1 = make_purchase_invoice(po.name) |
| 649 | pr1.update_stock = 1 |
| 650 | pr1.items[0].qty = 2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 651 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 652 | pr1.save() |
| 653 | pr1.submit() |
| 654 | |
| 655 | for key, value in get_supplied_items(pr1).items(): |
| 656 | self.assertEqual(value.qty, 2) |
| 657 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 658 | def test_partial_transfer_serial_no_components_based_on_material_transfer_for_purchase_invoice( |
| 659 | self, |
| 660 | ): |
| 661 | """ |
| 662 | - Set backflush based on Material Transferred for Subcontract |
| 663 | - Create subcontracted PO for the item Subcontracted Item SA2. |
| 664 | - Transfer the partial components from Stores to Supplier warehouse with serial nos. |
| 665 | - Create partial purchase receipt against the PO and change the qty manually. |
| 666 | - Transfer the remaining components from Stores to Supplier warehouse with serial nos. |
| 667 | - Create purchase receipt for remaining qty against the PO and change the qty manually. |
| 668 | """ |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 669 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 670 | set_backflush_based_on("Material Transferred for Subcontract") |
| 671 | item_code = "Subcontracted Item SA2" |
| 672 | items = [{"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 10, "rate": 100}] |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 673 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 674 | rm_items = [{"item_code": "Subcontracted SRM Item 2", "qty": 5}] |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 675 | |
| 676 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 677 | po = create_purchase_order( |
| 678 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 679 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 680 | |
| 681 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 682 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 683 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 684 | make_stock_transfer_entry( |
| 685 | po_no=po.name, |
| 686 | main_item_code=item_code, |
| 687 | rm_items=rm_items, |
| 688 | itemwise_details=copy.deepcopy(itemwise_details), |
| 689 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 690 | |
| 691 | pr1 = make_purchase_invoice(po.name) |
| 692 | pr1.update_stock = 1 |
| 693 | pr1.items[0].qty = 5 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 694 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 695 | pr1.save() |
| 696 | |
| 697 | for key, value in get_supplied_items(pr1).items(): |
| 698 | details = itemwise_details.get(key) |
| 699 | self.assertEqual(value.qty, 3) |
| 700 | self.assertEqual(sorted(value.serial_no), sorted(details.serial_no[0:3])) |
| 701 | |
| 702 | pr1.load_from_db() |
| 703 | pr1.supplied_items[0].consumed_qty = 5 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 704 | pr1.supplied_items[0].serial_no = "\n".join( |
| 705 | itemwise_details[pr1.supplied_items[0].rm_item_code]["serial_no"] |
| 706 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 707 | pr1.save() |
| 708 | pr1.submit() |
| 709 | |
| 710 | for key, value in get_supplied_items(pr1).items(): |
| 711 | details = itemwise_details.get(key) |
| 712 | self.assertEqual(value.qty, details.qty) |
| 713 | self.assertEqual(sorted(value.serial_no), sorted(details.serial_no)) |
| 714 | |
| 715 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
| 716 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 717 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 718 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 719 | make_stock_transfer_entry( |
| 720 | po_no=po.name, |
| 721 | main_item_code=item_code, |
| 722 | rm_items=rm_items, |
| 723 | itemwise_details=copy.deepcopy(itemwise_details), |
| 724 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 725 | |
| 726 | pr1 = make_purchase_invoice(po.name) |
| 727 | pr1.update_stock = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 728 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 729 | pr1.submit() |
| 730 | |
| 731 | for key, value in get_supplied_items(pr1).items(): |
| 732 | details = itemwise_details.get(key) |
| 733 | self.assertEqual(value.qty, details.qty) |
| 734 | self.assertEqual(sorted(value.serial_no), sorted(details.serial_no)) |
| 735 | |
| 736 | def test_partial_transfer_batch_based_on_material_transfer_for_purchase_invoice(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 737 | """ |
| 738 | - Set backflush based on Material Transferred for Subcontract |
| 739 | - Create subcontracted PO for the item Subcontracted Item SA6. |
| 740 | - Transfer the partial components from Stores to Supplier warehouse with batch. |
| 741 | - Create partial purchase receipt against the PO and change the qty manually. |
| 742 | - Transfer the remaining components from Stores to Supplier warehouse with batch. |
| 743 | - Create purchase receipt for remaining qty against the PO and change the qty manually. |
| 744 | """ |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 745 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 746 | set_backflush_based_on("Material Transferred for Subcontract") |
| 747 | item_code = "Subcontracted Item SA6" |
| 748 | items = [{"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 10, "rate": 100}] |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 749 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 750 | rm_items = [{"item_code": "Subcontracted SRM Item 3", "qty": 5}] |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 751 | |
| 752 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 753 | po = create_purchase_order( |
| 754 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 755 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 756 | |
| 757 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 758 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 759 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 760 | make_stock_transfer_entry( |
| 761 | po_no=po.name, |
| 762 | main_item_code=item_code, |
| 763 | rm_items=rm_items, |
| 764 | itemwise_details=copy.deepcopy(itemwise_details), |
| 765 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 766 | |
| 767 | pr1 = make_purchase_invoice(po.name) |
| 768 | pr1.update_stock = 1 |
| 769 | pr1.items[0].qty = 5 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 770 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 771 | pr1.save() |
| 772 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 773 | transferred_batch_no = "" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 774 | for key, value in get_supplied_items(pr1).items(): |
| 775 | details = itemwise_details.get(key) |
| 776 | self.assertEqual(value.qty, 3) |
| 777 | transferred_batch_no = details.batch_no |
| 778 | self.assertEqual(value.batch_no, details.batch_no) |
| 779 | |
| 780 | pr1.load_from_db() |
| 781 | pr1.supplied_items[0].consumed_qty = 5 |
| 782 | pr1.supplied_items[0].batch_no = list(transferred_batch_no.keys())[0] |
| 783 | pr1.save() |
| 784 | pr1.submit() |
| 785 | |
| 786 | for key, value in get_supplied_items(pr1).items(): |
| 787 | details = itemwise_details.get(key) |
| 788 | self.assertEqual(value.qty, details.qty) |
| 789 | self.assertEqual(value.batch_no, details.batch_no) |
| 790 | |
| 791 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
| 792 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 793 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 794 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 795 | make_stock_transfer_entry( |
| 796 | po_no=po.name, |
| 797 | main_item_code=item_code, |
| 798 | rm_items=rm_items, |
| 799 | itemwise_details=copy.deepcopy(itemwise_details), |
| 800 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 801 | |
| 802 | pr1 = make_purchase_invoice(po.name) |
| 803 | pr1.update_stock = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 804 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 805 | pr1.submit() |
| 806 | |
| 807 | for key, value in get_supplied_items(pr1).items(): |
| 808 | details = itemwise_details.get(key) |
| 809 | self.assertEqual(value.qty, details.qty) |
| 810 | self.assertEqual(value.batch_no, details.batch_no) |
| 811 | |
| 812 | def test_item_with_batch_based_on_bom_for_purchase_invoice(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 813 | """ |
| 814 | - Set backflush based on BOM |
| 815 | - Create subcontracted PO for the item Subcontracted Item SA4 (has batch no). |
| 816 | - Transfer the components from Stores to Supplier warehouse with batch no and serial nos. |
| 817 | - Transfer the components in multiple batches. |
| 818 | - Create the 3 purchase receipt against the PO and split Subcontracted Items into two batches. |
| 819 | - Keep the qty as 2 for Subcontracted Item in the purchase receipt. |
| 820 | """ |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 821 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 822 | set_backflush_based_on("BOM") |
| 823 | item_code = "Subcontracted Item SA4" |
| 824 | items = [{"warehouse": "_Test Warehouse - _TC", "item_code": item_code, "qty": 10, "rate": 100}] |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 825 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 826 | rm_items = [ |
| 827 | {"item_code": "Subcontracted SRM Item 1", "qty": 10}, |
| 828 | {"item_code": "Subcontracted SRM Item 2", "qty": 10}, |
| 829 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 830 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 831 | {"item_code": "Subcontracted SRM Item 3", "qty": 3}, |
| 832 | {"item_code": "Subcontracted SRM Item 3", "qty": 1}, |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 833 | ] |
| 834 | |
| 835 | itemwise_details = make_stock_in_entry(rm_items=rm_items) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 836 | po = create_purchase_order( |
| 837 | rm_items=items, is_subcontracted="Yes", supplier_warehouse="_Test Warehouse 1 - _TC" |
| 838 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 839 | |
| 840 | for d in rm_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 841 | d["po_detail"] = po.items[0].name |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 842 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 843 | make_stock_transfer_entry( |
| 844 | po_no=po.name, |
| 845 | main_item_code=item_code, |
| 846 | rm_items=rm_items, |
| 847 | itemwise_details=copy.deepcopy(itemwise_details), |
| 848 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 849 | |
| 850 | pr1 = make_purchase_invoice(po.name) |
| 851 | pr1.update_stock = 1 |
| 852 | pr1.items[0].qty = 2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 853 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 854 | add_second_row_in_pr(pr1) |
| 855 | pr1.save() |
| 856 | pr1.submit() |
| 857 | |
| 858 | for key, value in get_supplied_items(pr1).items(): |
| 859 | self.assertEqual(value.qty, 4) |
| 860 | |
| 861 | pr1 = make_purchase_invoice(po.name) |
| 862 | pr1.update_stock = 1 |
| 863 | pr1.items[0].qty = 2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 864 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 865 | add_second_row_in_pr(pr1) |
| 866 | pr1.save() |
| 867 | pr1.submit() |
| 868 | |
| 869 | for key, value in get_supplied_items(pr1).items(): |
| 870 | self.assertEqual(value.qty, 4) |
| 871 | |
| 872 | pr1 = make_purchase_invoice(po.name) |
| 873 | pr1.update_stock = 1 |
| 874 | pr1.items[0].qty = 2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 875 | pr1.items[0].expense_account = "Stock Adjustment - _TC" |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 876 | pr1.save() |
| 877 | pr1.submit() |
| 878 | |
| 879 | for key, value in get_supplied_items(pr1).items(): |
| 880 | self.assertEqual(value.qty, 2) |
| 881 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 882 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 883 | def add_second_row_in_pr(pr): |
| 884 | item_dict = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 885 | for column in [ |
| 886 | "item_code", |
| 887 | "item_name", |
| 888 | "qty", |
| 889 | "uom", |
| 890 | "warehouse", |
| 891 | "stock_uom", |
| 892 | "purchase_order", |
| 893 | "purchase_order_item", |
| 894 | "conversion_factor", |
| 895 | "rate", |
| 896 | "expense_account", |
| 897 | "po_detail", |
| 898 | ]: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 899 | item_dict[column] = pr.items[0].get(column) |
| 900 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 901 | pr.append("items", item_dict) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 902 | pr.set_missing_values() |
| 903 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 904 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 905 | def get_supplied_items(pr_doc): |
| 906 | supplied_items = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 907 | for row in pr_doc.get("supplied_items"): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 908 | if row.rm_item_code not in supplied_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 909 | supplied_items.setdefault( |
| 910 | row.rm_item_code, frappe._dict({"qty": 0, "serial_no": [], "batch_no": defaultdict(float)}) |
| 911 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 912 | |
| 913 | details = supplied_items[row.rm_item_code] |
| 914 | update_item_details(row, details) |
| 915 | |
| 916 | return supplied_items |
| 917 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 918 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 919 | def make_stock_in_entry(**args): |
| 920 | args = frappe._dict(args) |
| 921 | |
| 922 | items = {} |
| 923 | for row in args.rm_items: |
| 924 | row = frappe._dict(row) |
| 925 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 926 | doc = make_stock_entry( |
| 927 | target=row.warehouse or "_Test Warehouse - _TC", |
| 928 | item_code=row.item_code, |
| 929 | qty=row.qty or 1, |
| 930 | basic_rate=row.rate or 100, |
| 931 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 932 | |
| 933 | if row.item_code not in items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 934 | items.setdefault( |
| 935 | row.item_code, frappe._dict({"qty": 0, "serial_no": [], "batch_no": defaultdict(float)}) |
| 936 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 937 | |
| 938 | child_row = doc.items[0] |
| 939 | details = items[child_row.item_code] |
| 940 | update_item_details(child_row, details) |
| 941 | |
| 942 | return items |
| 943 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 944 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 945 | def update_item_details(child_row, details): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 946 | details.qty += ( |
| 947 | child_row.get("qty") |
| 948 | if child_row.doctype == "Stock Entry Detail" |
| 949 | else child_row.get("consumed_qty") |
| 950 | ) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 951 | |
| 952 | if child_row.serial_no: |
| 953 | details.serial_no.extend(get_serial_nos(child_row.serial_no)) |
| 954 | |
| 955 | if child_row.batch_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 956 | details.batch_no[child_row.batch_no] += child_row.get("qty") or child_row.get("consumed_qty") |
| 957 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 958 | |
| 959 | def make_stock_transfer_entry(**args): |
| 960 | args = frappe._dict(args) |
| 961 | |
| 962 | items = [] |
| 963 | for row in args.rm_items: |
| 964 | row = frappe._dict(row) |
| 965 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 966 | item = { |
| 967 | "item_code": row.main_item_code or args.main_item_code, |
| 968 | "rm_item_code": row.item_code, |
| 969 | "qty": row.qty or 1, |
| 970 | "item_name": row.item_code, |
| 971 | "rate": row.rate or 100, |
| 972 | "stock_uom": row.stock_uom or "Nos", |
| 973 | "warehouse": row.warehuose or "_Test Warehouse - _TC", |
| 974 | } |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 975 | |
| 976 | item_details = args.itemwise_details.get(row.item_code) |
| 977 | |
| 978 | if item_details and item_details.serial_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 979 | serial_nos = item_details.serial_no[0 : cint(row.qty)] |
| 980 | item["serial_no"] = "\n".join(serial_nos) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 981 | item_details.serial_no = list(set(item_details.serial_no) - set(serial_nos)) |
| 982 | |
| 983 | if item_details and item_details.batch_no: |
| 984 | for batch_no, batch_qty in item_details.batch_no.items(): |
| 985 | if batch_qty >= row.qty: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 986 | item["batch_no"] = batch_no |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 987 | item_details.batch_no[batch_no] -= row.qty |
| 988 | break |
| 989 | |
| 990 | items.append(item) |
| 991 | |
| 992 | ste_dict = make_rm_stock_entry(args.po_no, items) |
| 993 | doc = frappe.get_doc(ste_dict) |
| 994 | doc.insert() |
| 995 | doc.submit() |
| 996 | |
| 997 | return doc |
| 998 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 999 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1000 | def make_subcontract_items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1001 | sub_contracted_items = { |
| 1002 | "Subcontracted Item SA1": {}, |
| 1003 | "Subcontracted Item SA2": {}, |
| 1004 | "Subcontracted Item SA3": {}, |
| 1005 | "Subcontracted Item SA4": { |
| 1006 | "has_batch_no": 1, |
| 1007 | "create_new_batch": 1, |
| 1008 | "batch_number_series": "SBAT.####", |
| 1009 | }, |
| 1010 | "Subcontracted Item SA5": {}, |
| 1011 | "Subcontracted Item SA6": {}, |
| 1012 | } |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1013 | |
| 1014 | for item, properties in sub_contracted_items.items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1015 | if not frappe.db.exists("Item", item): |
| 1016 | properties.update({"is_stock_item": 1, "is_sub_contracted_item": 1}) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1017 | make_item(item, properties) |
| 1018 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1019 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1020 | def make_raw_materials(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1021 | raw_materials = { |
| 1022 | "Subcontracted SRM Item 1": {}, |
| 1023 | "Subcontracted SRM Item 2": {"has_serial_no": 1, "serial_no_series": "SRI.####"}, |
| 1024 | "Subcontracted SRM Item 3": { |
| 1025 | "has_batch_no": 1, |
| 1026 | "create_new_batch": 1, |
| 1027 | "batch_number_series": "BAT.####", |
| 1028 | }, |
| 1029 | "Subcontracted SRM Item 4": {"has_serial_no": 1, "serial_no_series": "SRII.####"}, |
| 1030 | "Subcontracted SRM Item 5": {"has_serial_no": 1, "serial_no_series": "SRII.####"}, |
| 1031 | } |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1032 | |
| 1033 | for item, properties in raw_materials.items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1034 | if not frappe.db.exists("Item", item): |
| 1035 | properties.update({"is_stock_item": 1}) |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1036 | make_item(item, properties) |
| 1037 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1038 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1039 | def make_bom_for_subcontracted_items(): |
| 1040 | boms = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1041 | "Subcontracted Item SA1": [ |
| 1042 | "Subcontracted SRM Item 1", |
| 1043 | "Subcontracted SRM Item 2", |
| 1044 | "Subcontracted SRM Item 3", |
| 1045 | ], |
| 1046 | "Subcontracted Item SA2": ["Subcontracted SRM Item 2"], |
| 1047 | "Subcontracted Item SA3": ["Subcontracted SRM Item 2"], |
| 1048 | "Subcontracted Item SA4": [ |
| 1049 | "Subcontracted SRM Item 1", |
| 1050 | "Subcontracted SRM Item 2", |
| 1051 | "Subcontracted SRM Item 3", |
| 1052 | ], |
| 1053 | "Subcontracted Item SA5": ["Subcontracted SRM Item 5"], |
| 1054 | "Subcontracted Item SA6": ["Subcontracted SRM Item 3"], |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | for item_code, raw_materials in boms.items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1058 | if not frappe.db.exists("BOM", {"item": item_code}): |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1059 | make_bom(item=item_code, raw_materials=raw_materials, rate=100) |
| 1060 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1061 | |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 1062 | def set_backflush_based_on(based_on): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1063 | frappe.db.set_value( |
| 1064 | "Buying Settings", None, "backflush_raw_materials_of_subcontract_based_on", based_on |
| 1065 | ) |