Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Ankush Menat | 77be982 | 2022-02-11 11:29:37 +0530 | [diff] [blame] | 6 | from frappe.utils import cstr, flt, now, nowdate, nowtime |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 8 | from erpnext.controllers.stock_controller import create_repost_item_valuation_entry |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 9 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 10 | |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 11 | def repost(only_actual=False, allow_negative_stock=False, allow_zero_rate=False, only_bin=False): |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 12 | """ |
| 13 | Repost everything! |
| 14 | """ |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 15 | frappe.db.auto_commit_on_many_writes = 1 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 16 | |
Nabin Hait | ca471f4 | 2013-11-20 13:14:12 +0530 | [diff] [blame] | 17 | if allow_negative_stock: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 18 | existing_allow_negative_stock = frappe.db.get_single_value("Stock Settings", "allow_negative_stock") |
Ankush Menat | a3ea985 | 2023-06-13 17:30:38 +0530 | [diff] [blame] | 19 | frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 20 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 21 | item_warehouses = frappe.db.sql( |
| 22 | """ |
Nabin Hait | 001edb4 | 2019-09-26 16:44:35 +0530 | [diff] [blame] | 23 | select distinct item_code, warehouse |
| 24 | from |
| 25 | (select item_code, warehouse from tabBin |
| 26 | union |
| 27 | select item_code, warehouse from `tabStock Ledger Entry`) a |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 28 | """ |
| 29 | ) |
Nabin Hait | 001edb4 | 2019-09-26 16:44:35 +0530 | [diff] [blame] | 30 | for d in item_warehouses: |
| 31 | try: |
| 32 | repost_stock(d[0], d[1], allow_zero_rate, only_actual, only_bin, allow_negative_stock) |
| 33 | frappe.db.commit() |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 34 | except Exception: |
Nabin Hait | 001edb4 | 2019-09-26 16:44:35 +0530 | [diff] [blame] | 35 | frappe.db.rollback() |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 36 | |
Nabin Hait | ca471f4 | 2013-11-20 13:14:12 +0530 | [diff] [blame] | 37 | if allow_negative_stock: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 38 | frappe.db.set_single_value("Stock Settings", "allow_negative_stock", existing_allow_negative_stock) |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 39 | frappe.db.auto_commit_on_many_writes = 0 |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 40 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 41 | |
| 42 | def repost_stock( |
| 43 | item_code, |
| 44 | warehouse, |
| 45 | allow_zero_rate=False, |
| 46 | only_actual=False, |
| 47 | only_bin=False, |
| 48 | allow_negative_stock=False, |
| 49 | ): |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 50 | if not only_bin: |
Nabin Hait | 001edb4 | 2019-09-26 16:44:35 +0530 | [diff] [blame] | 51 | repost_actual_qty(item_code, warehouse, allow_zero_rate, allow_negative_stock) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 52 | |
Nabin Hait | 2348a5f | 2014-10-15 15:31:33 +0530 | [diff] [blame] | 53 | if item_code and warehouse and not only_actual: |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 54 | qty_dict = { |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 55 | "reserved_qty": get_reserved_qty(item_code, warehouse), |
| 56 | "indented_qty": get_indented_qty(item_code, warehouse), |
| 57 | "ordered_qty": get_ordered_qty(item_code, warehouse), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 58 | "planned_qty": get_planned_qty(item_code, warehouse), |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 59 | } |
| 60 | if only_bin: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 61 | qty_dict.update({"actual_qty": get_balance_qty_from_sle(item_code, warehouse)}) |
Rushabh Mehta | c4d4c7f | 2015-10-14 17:37:28 +0530 | [diff] [blame] | 62 | |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 63 | update_bin_qty(item_code, warehouse, qty_dict) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 64 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 65 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 66 | def repost_actual_qty(item_code, warehouse, allow_zero_rate=False, allow_negative_stock=False): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 67 | create_repost_item_valuation_entry( |
| 68 | { |
| 69 | "item_code": item_code, |
| 70 | "warehouse": warehouse, |
| 71 | "posting_date": "1900-01-01", |
| 72 | "posting_time": "00:01", |
| 73 | "allow_negative_stock": allow_negative_stock, |
| 74 | "allow_zero_rate": allow_zero_rate, |
| 75 | } |
| 76 | ) |
| 77 | |
Rushabh Mehta | c4d4c7f | 2015-10-14 17:37:28 +0530 | [diff] [blame] | 78 | |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 79 | def get_balance_qty_from_sle(item_code, warehouse): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | balance_qty = frappe.db.sql( |
| 81 | """select qty_after_transaction from `tabStock Ledger Entry` |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 82 | where item_code=%s and warehouse=%s and is_cancelled=0 |
Aditya Hase | 0c16424 | 2019-01-07 22:07:13 +0530 | [diff] [blame] | 83 | order by posting_date desc, posting_time desc, creation desc |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 84 | limit 1""", |
| 85 | (item_code, warehouse), |
| 86 | ) |
Rushabh Mehta | c4d4c7f | 2015-10-14 17:37:28 +0530 | [diff] [blame] | 87 | |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 88 | return flt(balance_qty[0][0]) if balance_qty else 0.0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 89 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 90 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 91 | def get_reserved_qty(item_code, warehouse): |
Devin Slauenwhite | 3c553b0 | 2023-03-27 10:24:15 -0400 | [diff] [blame] | 92 | dont_reserve_on_return = frappe.get_cached_value( |
| 93 | "Selling Settings", "Selling Settings", "dont_reserve_sales_order_qty_on_sales_return" |
| 94 | ) |
Devin Slauenwhite | 17f8080 | 2023-03-25 00:24:53 -0400 | [diff] [blame] | 95 | reserved_qty = frappe.db.sql( |
Devin Slauenwhite | 3c553b0 | 2023-03-27 10:24:15 -0400 | [diff] [blame] | 96 | f""" |
Devin Slauenwhite | 17f8080 | 2023-03-25 00:24:53 -0400 | [diff] [blame] | 97 | select |
Devin Slauenwhite | 3c553b0 | 2023-03-27 10:24:15 -0400 | [diff] [blame] | 98 | sum(dnpi_qty * ((so_item_qty - so_item_delivered_qty - if(dont_reserve_qty_on_return, so_item_returned_qty, 0)) / so_item_qty)) |
Devin Slauenwhite | 17f8080 | 2023-03-25 00:24:53 -0400 | [diff] [blame] | 99 | from |
| 100 | ( |
| 101 | (select |
| 102 | qty as dnpi_qty, |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 103 | ( |
Devin Slauenwhite | 17f8080 | 2023-03-25 00:24:53 -0400 | [diff] [blame] | 104 | select qty from `tabSales Order Item` |
| 105 | where name = dnpi.parent_detail_docname |
| 106 | and (delivered_by_supplier is null or delivered_by_supplier = 0) |
| 107 | ) as so_item_qty, |
| 108 | ( |
| 109 | select delivered_qty from `tabSales Order Item` |
| 110 | where name = dnpi.parent_detail_docname |
| 111 | and delivered_by_supplier = 0 |
| 112 | ) as so_item_delivered_qty, |
Devin Slauenwhite | 3c553b0 | 2023-03-27 10:24:15 -0400 | [diff] [blame] | 113 | ( |
| 114 | select returned_qty from `tabSales Order Item` |
| 115 | where name = dnpi.parent_detail_docname |
| 116 | and delivered_by_supplier = 0 |
| 117 | ) as so_item_returned_qty, |
| 118 | {dont_reserve_on_return} as dont_reserve_qty_on_return, |
Devin Slauenwhite | 17f8080 | 2023-03-25 00:24:53 -0400 | [diff] [blame] | 119 | parent, name |
| 120 | from |
| 121 | ( |
| 122 | select qty, parent_detail_docname, parent, name |
| 123 | from `tabPacked Item` dnpi_in |
| 124 | where item_code = %s and warehouse = %s |
| 125 | and parenttype='Sales Order' |
| 126 | and item_code != parent_item |
| 127 | and exists (select * from `tabSales Order` so |
| 128 | where name = dnpi_in.parent and docstatus = 1 and status not in ('On Hold', 'Closed')) |
| 129 | ) dnpi) |
| 130 | union |
| 131 | (select stock_qty as dnpi_qty, qty as so_item_qty, |
Devin Slauenwhite | 3c553b0 | 2023-03-27 10:24:15 -0400 | [diff] [blame] | 132 | delivered_qty as so_item_delivered_qty, |
| 133 | returned_qty as so_item_returned_qty, |
| 134 | {dont_reserve_on_return}, parent, name |
Devin Slauenwhite | 17f8080 | 2023-03-25 00:24:53 -0400 | [diff] [blame] | 135 | from `tabSales Order Item` so_item |
| 136 | where item_code = %s and warehouse = %s |
| 137 | and (so_item.delivered_by_supplier is null or so_item.delivered_by_supplier = 0) |
| 138 | and exists(select * from `tabSales Order` so |
| 139 | where so.name = so_item.parent and so.docstatus = 1 |
| 140 | and so.status not in ('On Hold', 'Closed'))) |
| 141 | ) tab |
| 142 | where |
| 143 | so_item_qty >= so_item_delivered_qty |
| 144 | """, |
| 145 | (item_code, warehouse, item_code, warehouse), |
Devin Slauenwhite | 0328874 | 2022-06-17 14:45:33 -0400 | [diff] [blame] | 146 | ) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 147 | |
| 148 | return flt(reserved_qty[0][0]) if reserved_qty else 0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 149 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 150 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 151 | def get_indented_qty(item_code, warehouse): |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 152 | # Ordered Qty is always maintained in stock UOM |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 153 | inward_qty = frappe.db.sql( |
| 154 | """ |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 155 | select sum(mr_item.stock_qty - mr_item.ordered_qty) |
Nabin Hait | 4acd431 | 2014-11-04 15:32:31 +0530 | [diff] [blame] | 156 | from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr |
| 157 | where mr_item.item_code=%s and mr_item.warehouse=%s |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 158 | and mr.material_request_type in ('Purchase', 'Manufacture', 'Customer Provided', 'Material Transfer') |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 159 | and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name |
| 160 | and mr.status!='Stopped' and mr.docstatus=1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 161 | """, |
| 162 | (item_code, warehouse), |
| 163 | ) |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 164 | inward_qty = flt(inward_qty[0][0]) if inward_qty else 0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 165 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 166 | outward_qty = frappe.db.sql( |
| 167 | """ |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 168 | select sum(mr_item.stock_qty - mr_item.ordered_qty) |
| 169 | from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr |
| 170 | where mr_item.item_code=%s and mr_item.warehouse=%s |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 171 | and mr.material_request_type = 'Material Issue' |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 172 | and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name |
| 173 | and mr.status!='Stopped' and mr.docstatus=1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 174 | """, |
| 175 | (item_code, warehouse), |
| 176 | ) |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 177 | outward_qty = flt(outward_qty[0][0]) if outward_qty else 0 |
marination | 815c36a | 2020-03-26 14:49:28 +0530 | [diff] [blame] | 178 | |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 179 | requested_qty = inward_qty - outward_qty |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 180 | |
| 181 | return requested_qty |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 182 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 183 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 184 | def get_ordered_qty(item_code, warehouse): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 185 | ordered_qty = frappe.db.sql( |
| 186 | """ |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 187 | select sum((po_item.qty - po_item.received_qty)*po_item.conversion_factor) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 188 | from `tabPurchase Order Item` po_item, `tabPurchase Order` po |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 189 | where po_item.item_code=%s and po_item.warehouse=%s |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 190 | and po_item.qty > po_item.received_qty and po_item.parent=po.name |
patilsangram | bf2b511 | 2016-02-22 16:24:23 +0530 | [diff] [blame] | 191 | and po.status not in ('Closed', 'Delivered') and po.docstatus=1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 192 | and po_item.delivered_by_supplier = 0""", |
| 193 | (item_code, warehouse), |
| 194 | ) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 195 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 196 | return flt(ordered_qty[0][0]) if ordered_qty else 0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 197 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 198 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 199 | def get_planned_qty(item_code, warehouse): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 200 | planned_qty = frappe.db.sql( |
| 201 | """ |
Zarrar | 13ddc7e | 2018-03-20 12:38:43 +0530 | [diff] [blame] | 202 | select sum(qty - produced_qty) from `tabWork Order` |
Conor | 74a782d | 2022-06-17 06:31:27 -0500 | [diff] [blame] | 203 | where production_item = %s and fg_warehouse = %s and status not in ('Stopped', 'Completed', 'Closed') |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 204 | and docstatus=1 and qty > produced_qty""", |
| 205 | (item_code, warehouse), |
| 206 | ) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 207 | |
| 208 | return flt(planned_qty[0][0]) if planned_qty else 0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 209 | |
| 210 | |
Nabin Hait | 7f3f2a0 | 2014-09-01 18:16:05 +0530 | [diff] [blame] | 211 | def update_bin_qty(item_code, warehouse, qty_dict=None): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 212 | from erpnext.stock.utils import get_bin |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 213 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 214 | bin = get_bin(item_code, warehouse) |
| 215 | mismatch = False |
marination | 815c36a | 2020-03-26 14:49:28 +0530 | [diff] [blame] | 216 | for field, value in qty_dict.items(): |
| 217 | if flt(bin.get(field)) != flt(value): |
| 218 | bin.set(field, flt(value)) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 219 | mismatch = True |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 220 | |
Ankush Menat | 77be982 | 2022-02-11 11:29:37 +0530 | [diff] [blame] | 221 | bin.modified = now() |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 222 | if mismatch: |
Himanshu Mishra | f56aac6 | 2018-08-08 18:32:03 +0530 | [diff] [blame] | 223 | bin.set_projected_qty() |
| 224 | bin.db_update() |
| 225 | bin.clear_cache() |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 226 | |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 227 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 228 | def set_stock_balance_as_per_serial_no( |
| 229 | item_code=None, posting_date=None, posting_time=None, fiscal_year=None |
| 230 | ): |
| 231 | if not posting_date: |
| 232 | posting_date = nowdate() |
| 233 | if not posting_time: |
| 234 | posting_time = nowtime() |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 235 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 236 | condition = " and item.name='%s'" % item_code.replace("'", "'") if item_code else "" |
| 237 | |
| 238 | bin = frappe.db.sql( |
| 239 | """select bin.item_code, bin.warehouse, bin.actual_qty, item.stock_uom |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 240 | from `tabBin` bin, tabItem item |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 241 | where bin.item_code = item.name and item.has_serial_no = 1 %s""" |
| 242 | % condition |
| 243 | ) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 244 | |
| 245 | for d in bin: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 246 | serial_nos = frappe.db.sql( |
| 247 | """select count(name) from `tabSerial No` |
| 248 | where item_code=%s and warehouse=%s and docstatus < 2""", |
| 249 | (d[0], d[1]), |
| 250 | ) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 251 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 252 | sle = frappe.db.sql( |
| 253 | """select valuation_rate, company from `tabStock Ledger Entry` |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 254 | where item_code = %s and warehouse = %s and is_cancelled = 0 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 255 | order by posting_date desc limit 1""", |
| 256 | (d[0], d[1]), |
| 257 | ) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 258 | |
| 259 | sle_dict = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 260 | "doctype": "Stock Ledger Entry", |
| 261 | "item_code": d[0], |
| 262 | "warehouse": d[1], |
| 263 | "transaction_date": nowdate(), |
| 264 | "posting_date": posting_date, |
| 265 | "posting_time": posting_time, |
| 266 | "voucher_type": "Stock Reconciliation (Manual)", |
| 267 | "voucher_no": "", |
| 268 | "voucher_detail_no": "", |
| 269 | "actual_qty": flt(serial_nos[0][0]) - flt(d[2]), |
| 270 | "stock_uom": d[3], |
| 271 | "incoming_rate": sle and flt(serial_nos[0][0]) > flt(d[2]) and flt(sle[0][0]) or 0, |
| 272 | "company": sle and cstr(sle[0][1]) or 0, |
| 273 | "batch_no": "", |
| 274 | "serial_no": "", |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | sle_doc = frappe.get_doc(sle_dict) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 278 | sle_doc.flags.ignore_validate = True |
| 279 | sle_doc.flags.ignore_links = True |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 280 | sle_doc.insert() |
| 281 | |
| 282 | args = sle_dict.copy() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 283 | args.update({"sle_id": sle_doc.name}) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 284 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 285 | create_repost_item_valuation_entry( |
| 286 | { |
| 287 | "item_code": d[0], |
| 288 | "warehouse": d[1], |
| 289 | "posting_date": posting_date, |
| 290 | "posting_time": posting_time, |
| 291 | } |
| 292 | ) |