blob: 20bd4ca49e98a9e1dd656f7dcc99fa70f97041d7 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Nabin Haiteea2b342013-10-11 18:31:33 +05302# License: GNU General Public License v3. See license.txt
3
Chillar Anand915b3432021-09-02 16:44:59 +05304
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Ankush Menat77be9822022-02-11 11:29:37 +05306from frappe.utils import cstr, flt, now, nowdate, nowtime
Chillar Anand915b3432021-09-02 16:44:59 +05307
Nabin Haita77b8c92020-12-21 14:45:50 +05308from erpnext.controllers.stock_controller import create_repost_item_valuation_entry
Chillar Anand915b3432021-09-02 16:44:59 +05309
Nabin Haiteea2b342013-10-11 18:31:33 +053010
Nabin Haitb7e46c42015-10-12 16:46:29 +053011def repost(only_actual=False, allow_negative_stock=False, allow_zero_rate=False, only_bin=False):
Nabin Haiteea2b342013-10-11 18:31:33 +053012 """
13 Repost everything!
14 """
Anand Doshie9baaa62014-02-26 12:35:33 +053015 frappe.db.auto_commit_on_many_writes = 1
Nabin Hait62985362014-04-04 12:05:16 +053016
Nabin Haitca471f42013-11-20 13:14:12 +053017 if allow_negative_stock:
Akhil Narang3effaf22024-03-27 11:37:26 +053018 existing_allow_negative_stock = frappe.db.get_single_value("Stock Settings", "allow_negative_stock")
Ankush Menata3ea9852023-06-13 17:30:38 +053019 frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1)
Nabin Hait62985362014-04-04 12:05:16 +053020
Ankush Menat494bd9e2022-03-28 18:52:46 +053021 item_warehouses = frappe.db.sql(
22 """
Nabin Hait001edb42019-09-26 16:44:35 +053023 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 Menat494bd9e2022-03-28 18:52:46 +053028 """
29 )
Nabin Hait001edb42019-09-26 16:44:35 +053030 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 Menat694ae812021-09-01 14:40:56 +053034 except Exception:
Nabin Hait001edb42019-09-26 16:44:35 +053035 frappe.db.rollback()
Nabin Hait62985362014-04-04 12:05:16 +053036
Nabin Haitca471f42013-11-20 13:14:12 +053037 if allow_negative_stock:
Akhil Narang3effaf22024-03-27 11:37:26 +053038 frappe.db.set_single_value("Stock Settings", "allow_negative_stock", existing_allow_negative_stock)
Anand Doshie9baaa62014-02-26 12:35:33 +053039 frappe.db.auto_commit_on_many_writes = 0
Nabin Haiteea2b342013-10-11 18:31:33 +053040
Ankush Menat494bd9e2022-03-28 18:52:46 +053041
42def 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 Haitb7e46c42015-10-12 16:46:29 +053050 if not only_bin:
Nabin Hait001edb42019-09-26 16:44:35 +053051 repost_actual_qty(item_code, warehouse, allow_zero_rate, allow_negative_stock)
Nabin Hait62985362014-04-04 12:05:16 +053052
Nabin Hait2348a5f2014-10-15 15:31:33 +053053 if item_code and warehouse and not only_actual:
Nabin Haitb7e46c42015-10-12 16:46:29 +053054 qty_dict = {
Nabin Haiteea2b342013-10-11 18:31:33 +053055 "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 Menat494bd9e2022-03-28 18:52:46 +053058 "planned_qty": get_planned_qty(item_code, warehouse),
Nabin Haitb7e46c42015-10-12 16:46:29 +053059 }
60 if only_bin:
Ankush Menat494bd9e2022-03-28 18:52:46 +053061 qty_dict.update({"actual_qty": get_balance_qty_from_sle(item_code, warehouse)})
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053062
Nabin Haitb7e46c42015-10-12 16:46:29 +053063 update_bin_qty(item_code, warehouse, qty_dict)
Nabin Haiteea2b342013-10-11 18:31:33 +053064
Ankush Menat494bd9e2022-03-28 18:52:46 +053065
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +053066def repost_actual_qty(item_code, warehouse, allow_zero_rate=False, allow_negative_stock=False):
Ankush Menat494bd9e2022-03-28 18:52:46 +053067 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 Mehtac4d4c7f2015-10-14 17:37:28 +053078
Nabin Haitb7e46c42015-10-12 16:46:29 +053079def get_balance_qty_from_sle(item_code, warehouse):
Ankush Menat494bd9e2022-03-28 18:52:46 +053080 balance_qty = frappe.db.sql(
81 """select qty_after_transaction from `tabStock Ledger Entry`
Nabin Haita77b8c92020-12-21 14:45:50 +053082 where item_code=%s and warehouse=%s and is_cancelled=0
Aditya Hase0c164242019-01-07 22:07:13 +053083 order by posting_date desc, posting_time desc, creation desc
Ankush Menat494bd9e2022-03-28 18:52:46 +053084 limit 1""",
85 (item_code, warehouse),
86 )
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053087
Nabin Haitb7e46c42015-10-12 16:46:29 +053088 return flt(balance_qty[0][0]) if balance_qty else 0.0
Nabin Hait62985362014-04-04 12:05:16 +053089
Ankush Menat494bd9e2022-03-28 18:52:46 +053090
Nabin Haiteea2b342013-10-11 18:31:33 +053091def get_reserved_qty(item_code, warehouse):
Devin Slauenwhite3c553b02023-03-27 10:24:15 -040092 dont_reserve_on_return = frappe.get_cached_value(
93 "Selling Settings", "Selling Settings", "dont_reserve_sales_order_qty_on_sales_return"
94 )
Devin Slauenwhite17f80802023-03-25 00:24:53 -040095 reserved_qty = frappe.db.sql(
Devin Slauenwhite3c553b02023-03-27 10:24:15 -040096 f"""
Devin Slauenwhite17f80802023-03-25 00:24:53 -040097 select
Devin Slauenwhite3c553b02023-03-27 10:24:15 -040098 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 Slauenwhite17f80802023-03-25 00:24:53 -040099 from
100 (
101 (select
102 qty as dnpi_qty,
Nabin Haiteea2b342013-10-11 18:31:33 +0530103 (
Devin Slauenwhite17f80802023-03-25 00:24:53 -0400104 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 Slauenwhite3c553b02023-03-27 10:24:15 -0400113 (
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 Slauenwhite17f80802023-03-25 00:24:53 -0400119 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 Slauenwhite3c553b02023-03-27 10:24:15 -0400132 delivered_qty as so_item_delivered_qty,
133 returned_qty as so_item_returned_qty,
134 {dont_reserve_on_return}, parent, name
Devin Slauenwhite17f80802023-03-25 00:24:53 -0400135 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 Slauenwhite03288742022-06-17 14:45:33 -0400146 )
Nabin Haiteea2b342013-10-11 18:31:33 +0530147
148 return flt(reserved_qty[0][0]) if reserved_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530149
Ankush Menat494bd9e2022-03-28 18:52:46 +0530150
Nabin Haiteea2b342013-10-11 18:31:33 +0530151def get_indented_qty(item_code, warehouse):
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530152 # Ordered Qty is always maintained in stock UOM
Ankush Menat494bd9e2022-03-28 18:52:46 +0530153 inward_qty = frappe.db.sql(
154 """
Nabin Hait03af0292020-04-16 20:11:32 +0530155 select sum(mr_item.stock_qty - mr_item.ordered_qty)
Nabin Hait4acd4312014-11-04 15:32:31 +0530156 from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
157 where mr_item.item_code=%s and mr_item.warehouse=%s
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530158 and mr.material_request_type in ('Purchase', 'Manufacture', 'Customer Provided', 'Material Transfer')
Nabin Hait03af0292020-04-16 20:11:32 +0530159 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 Menat494bd9e2022-03-28 18:52:46 +0530161 """,
162 (item_code, warehouse),
163 )
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530164 inward_qty = flt(inward_qty[0][0]) if inward_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530165
Ankush Menat494bd9e2022-03-28 18:52:46 +0530166 outward_qty = frappe.db.sql(
167 """
Nabin Hait03af0292020-04-16 20:11:32 +0530168 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 Hait7eaa7f22020-04-17 10:52:28 +0530171 and mr.material_request_type = 'Material Issue'
Nabin Hait03af0292020-04-16 20:11:32 +0530172 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 Menat494bd9e2022-03-28 18:52:46 +0530174 """,
175 (item_code, warehouse),
176 )
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530177 outward_qty = flt(outward_qty[0][0]) if outward_qty else 0
marination815c36a2020-03-26 14:49:28 +0530178
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530179 requested_qty = inward_qty - outward_qty
Nabin Hait03af0292020-04-16 20:11:32 +0530180
181 return requested_qty
Nabin Haiteea2b342013-10-11 18:31:33 +0530182
Ankush Menat494bd9e2022-03-28 18:52:46 +0530183
Nabin Haiteea2b342013-10-11 18:31:33 +0530184def get_ordered_qty(item_code, warehouse):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530185 ordered_qty = frappe.db.sql(
186 """
Anand Doshi602e8252015-11-16 19:05:46 +0530187 select sum((po_item.qty - po_item.received_qty)*po_item.conversion_factor)
Nabin Haiteea2b342013-10-11 18:31:33 +0530188 from `tabPurchase Order Item` po_item, `tabPurchase Order` po
Nabin Hait62985362014-04-04 12:05:16 +0530189 where po_item.item_code=%s and po_item.warehouse=%s
Anand Doshi602e8252015-11-16 19:05:46 +0530190 and po_item.qty > po_item.received_qty and po_item.parent=po.name
patilsangrambf2b5112016-02-22 16:24:23 +0530191 and po.status not in ('Closed', 'Delivered') and po.docstatus=1
Ankush Menat494bd9e2022-03-28 18:52:46 +0530192 and po_item.delivered_by_supplier = 0""",
193 (item_code, warehouse),
194 )
Nabin Hait62985362014-04-04 12:05:16 +0530195
Nabin Haiteea2b342013-10-11 18:31:33 +0530196 return flt(ordered_qty[0][0]) if ordered_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530197
Ankush Menat494bd9e2022-03-28 18:52:46 +0530198
Nabin Haiteea2b342013-10-11 18:31:33 +0530199def get_planned_qty(item_code, warehouse):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530200 planned_qty = frappe.db.sql(
201 """
Zarrar13ddc7e2018-03-20 12:38:43 +0530202 select sum(qty - produced_qty) from `tabWork Order`
Conor74a782d2022-06-17 06:31:27 -0500203 where production_item = %s and fg_warehouse = %s and status not in ('Stopped', 'Completed', 'Closed')
Ankush Menat494bd9e2022-03-28 18:52:46 +0530204 and docstatus=1 and qty > produced_qty""",
205 (item_code, warehouse),
206 )
Nabin Haiteea2b342013-10-11 18:31:33 +0530207
208 return flt(planned_qty[0][0]) if planned_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530209
210
Nabin Hait7f3f2a02014-09-01 18:16:05 +0530211def update_bin_qty(item_code, warehouse, qty_dict=None):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530212 from erpnext.stock.utils import get_bin
Ankush Menat494bd9e2022-03-28 18:52:46 +0530213
Nabin Haiteea2b342013-10-11 18:31:33 +0530214 bin = get_bin(item_code, warehouse)
215 mismatch = False
marination815c36a2020-03-26 14:49:28 +0530216 for field, value in qty_dict.items():
217 if flt(bin.get(field)) != flt(value):
218 bin.set(field, flt(value))
Nabin Haiteea2b342013-10-11 18:31:33 +0530219 mismatch = True
Nabin Hait62985362014-04-04 12:05:16 +0530220
Ankush Menat77be9822022-02-11 11:29:37 +0530221 bin.modified = now()
Nabin Haiteea2b342013-10-11 18:31:33 +0530222 if mismatch:
Himanshu Mishraf56aac62018-08-08 18:32:03 +0530223 bin.set_projected_qty()
224 bin.db_update()
225 bin.clear_cache()
Nabin Hait62985362014-04-04 12:05:16 +0530226
Nabin Hait62985362014-04-04 12:05:16 +0530227
Ankush Menat494bd9e2022-03-28 18:52:46 +0530228def 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 Hait62985362014-04-04 12:05:16 +0530235
Ankush Menat494bd9e2022-03-28 18:52:46 +0530236 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 Hait62985362014-04-04 12:05:16 +0530240 from `tabBin` bin, tabItem item
Ankush Menat494bd9e2022-03-28 18:52:46 +0530241 where bin.item_code = item.name and item.has_serial_no = 1 %s"""
242 % condition
243 )
Nabin Hait62985362014-04-04 12:05:16 +0530244
245 for d in bin:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530246 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 Hait62985362014-04-04 12:05:16 +0530251
Ankush Menat494bd9e2022-03-28 18:52:46 +0530252 sle = frappe.db.sql(
253 """select valuation_rate, company from `tabStock Ledger Entry`
Nabin Haita77b8c92020-12-21 14:45:50 +0530254 where item_code = %s and warehouse = %s and is_cancelled = 0
Ankush Menat494bd9e2022-03-28 18:52:46 +0530255 order by posting_date desc limit 1""",
256 (d[0], d[1]),
257 )
Nabin Hait62985362014-04-04 12:05:16 +0530258
259 sle_dict = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530260 "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 Hait62985362014-04-04 12:05:16 +0530275 }
276
277 sle_doc = frappe.get_doc(sle_dict)
Anand Doshi6dfd4302015-02-10 14:41:27 +0530278 sle_doc.flags.ignore_validate = True
279 sle_doc.flags.ignore_links = True
Nabin Hait62985362014-04-04 12:05:16 +0530280 sle_doc.insert()
281
282 args = sle_dict.copy()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530283 args.update({"sle_id": sle_doc.name})
Nabin Hait62985362014-04-04 12:05:16 +0530284
Ankush Menat494bd9e2022-03-28 18:52:46 +0530285 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 )