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