blob: 1cb0f0d0a49833cefee40231a4d8e3c9fd933221 [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
Aditya Hase6ccb6562017-08-28 18:17:36 +05304from __future__ import print_function, unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05305
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05306import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05307from frappe.utils import cstr, flt, nowdate, nowtime
8
Nabin Haita77b8c92020-12-21 14:45:50 +05309from erpnext.controllers.stock_controller import create_repost_item_valuation_entry
Chillar Anand915b3432021-09-02 16:44:59 +053010from erpnext.stock.utils import update_bin
11
Nabin Haiteea2b342013-10-11 18:31:33 +053012
Nabin Haitb7e46c42015-10-12 16:46:29 +053013def repost(only_actual=False, allow_negative_stock=False, allow_zero_rate=False, only_bin=False):
Nabin Haiteea2b342013-10-11 18:31:33 +053014 """
15 Repost everything!
16 """
Anand Doshie9baaa62014-02-26 12:35:33 +053017 frappe.db.auto_commit_on_many_writes = 1
Nabin Hait62985362014-04-04 12:05:16 +053018
Nabin Haitca471f42013-11-20 13:14:12 +053019 if allow_negative_stock:
Nabin Hait249bbbc2014-11-26 15:35:08 +053020 existing_allow_negative_stock = frappe.db.get_value("Stock Settings", None, "allow_negative_stock")
21 frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1)
Nabin Hait62985362014-04-04 12:05:16 +053022
Nabin Hait001edb42019-09-26 16:44:35 +053023 item_warehouses = frappe.db.sql("""
24 select distinct item_code, warehouse
25 from
26 (select item_code, warehouse from tabBin
27 union
28 select item_code, warehouse from `tabStock Ledger Entry`) a
29 """)
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 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:
Nabin Hait249bbbc2014-11-26 15:35:08 +053038 frappe.db.set_value("Stock Settings", None, "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
Nabin Hait001edb42019-09-26 16:44:35 +053041def repost_stock(item_code, warehouse, allow_zero_rate=False,
42 only_actual=False, only_bin=False, allow_negative_stock=False):
43
Nabin Haitb7e46c42015-10-12 16:46:29 +053044 if not only_bin:
Nabin Hait001edb42019-09-26 16:44:35 +053045 repost_actual_qty(item_code, warehouse, allow_zero_rate, allow_negative_stock)
Nabin Hait62985362014-04-04 12:05:16 +053046
Nabin Hait2348a5f2014-10-15 15:31:33 +053047 if item_code and warehouse and not only_actual:
Nabin Haitb7e46c42015-10-12 16:46:29 +053048 qty_dict = {
Nabin Haiteea2b342013-10-11 18:31:33 +053049 "reserved_qty": get_reserved_qty(item_code, warehouse),
50 "indented_qty": get_indented_qty(item_code, warehouse),
51 "ordered_qty": get_ordered_qty(item_code, warehouse),
52 "planned_qty": get_planned_qty(item_code, warehouse)
Nabin Haitb7e46c42015-10-12 16:46:29 +053053 }
54 if only_bin:
55 qty_dict.update({
56 "actual_qty": get_balance_qty_from_sle(item_code, warehouse)
57 })
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053058
Nabin Haitb7e46c42015-10-12 16:46:29 +053059 update_bin_qty(item_code, warehouse, qty_dict)
Nabin Haiteea2b342013-10-11 18:31:33 +053060
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +053061def repost_actual_qty(item_code, warehouse, allow_zero_rate=False, allow_negative_stock=False):
Nabin Haita77b8c92020-12-21 14:45:50 +053062 create_repost_item_valuation_entry({
63 "item_code": item_code,
64 "warehouse": warehouse,
65 "posting_date": "1900-01-01",
66 "posting_time": "00:01",
67 "allow_negative_stock": allow_negative_stock,
68 "allow_zero_rate": allow_zero_rate
69 })
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053070
Nabin Haitb7e46c42015-10-12 16:46:29 +053071def get_balance_qty_from_sle(item_code, warehouse):
72 balance_qty = frappe.db.sql("""select qty_after_transaction from `tabStock Ledger Entry`
Nabin Haita77b8c92020-12-21 14:45:50 +053073 where item_code=%s and warehouse=%s and is_cancelled=0
Aditya Hase0c164242019-01-07 22:07:13 +053074 order by posting_date desc, posting_time desc, creation desc
Nabin Haitb7e46c42015-10-12 16:46:29 +053075 limit 1""", (item_code, warehouse))
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053076
Nabin Haitb7e46c42015-10-12 16:46:29 +053077 return flt(balance_qty[0][0]) if balance_qty else 0.0
Nabin Hait62985362014-04-04 12:05:16 +053078
Nabin Haiteea2b342013-10-11 18:31:33 +053079def get_reserved_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +053080 reserved_qty = frappe.db.sql("""
Nabin Hait62985362014-04-04 12:05:16 +053081 select
rohitwaghchaureafa93c62017-03-29 17:29:20 +053082 sum(dnpi_qty * ((so_item_qty - so_item_delivered_qty) / so_item_qty))
Nabin Hait62985362014-04-04 12:05:16 +053083 from
Nabin Haiteea2b342013-10-11 18:31:33 +053084 (
85 (select
86 qty as dnpi_qty,
87 (
rohitwaghchaureafa93c62017-03-29 17:29:20 +053088 select qty from `tabSales Order Item`
Nabin Haiteea2b342013-10-11 18:31:33 +053089 where name = dnpi.parent_detail_docname
Saurabh2e292062015-11-18 17:03:33 +053090 and (delivered_by_supplier is null or delivered_by_supplier = 0)
Nabin Haiteea2b342013-10-11 18:31:33 +053091 ) as so_item_qty,
92 (
Anand Doshi602e8252015-11-16 19:05:46 +053093 select delivered_qty from `tabSales Order Item`
94 where name = dnpi.parent_detail_docname
95 and delivered_by_supplier = 0
Nabin Hait62985362014-04-04 12:05:16 +053096 ) as so_item_delivered_qty,
Nabin Haiteea2b342013-10-11 18:31:33 +053097 parent, name
Nabin Hait62985362014-04-04 12:05:16 +053098 from
Nabin Haiteea2b342013-10-11 18:31:33 +053099 (
100 select qty, parent_detail_docname, parent, name
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530101 from `tabPacked Item` dnpi_in
Nabin Haiteea2b342013-10-11 18:31:33 +0530102 where item_code = %s and warehouse = %s
103 and parenttype="Sales Order"
Nabin Haitfc2dd442014-10-17 13:05:24 +0530104 and item_code != parent_item
Nabin Haiteea2b342013-10-11 18:31:33 +0530105 and exists (select * from `tabSales Order` so
patilsangrama812d672016-02-23 19:04:29 +0530106 where name = dnpi_in.parent and docstatus = 1 and status != 'Closed')
Nabin Haiteea2b342013-10-11 18:31:33 +0530107 ) dnpi)
108 union
rohitwaghchaureafa93c62017-03-29 17:29:20 +0530109 (select stock_qty as dnpi_qty, qty as so_item_qty,
Anand Doshi602e8252015-11-16 19:05:46 +0530110 delivered_qty as so_item_delivered_qty, parent, name
Nabin Haiteea2b342013-10-11 18:31:33 +0530111 from `tabSales Order Item` so_item
Anand Doshi602e8252015-11-16 19:05:46 +0530112 where item_code = %s and warehouse = %s
Saurabh2e292062015-11-18 17:03:33 +0530113 and (so_item.delivered_by_supplier is null or so_item.delivered_by_supplier = 0)
Nabin Haiteea2b342013-10-11 18:31:33 +0530114 and exists(select * from `tabSales Order` so
Nabin Hait62985362014-04-04 12:05:16 +0530115 where so.name = so_item.parent and so.docstatus = 1
patilsangrama812d672016-02-23 19:04:29 +0530116 and so.status != 'Closed'))
Nabin Haiteea2b342013-10-11 18:31:33 +0530117 ) tab
Nabin Hait62985362014-04-04 12:05:16 +0530118 where
Nabin Haiteea2b342013-10-11 18:31:33 +0530119 so_item_qty >= so_item_delivered_qty
120 """, (item_code, warehouse, item_code, warehouse))
121
122 return flt(reserved_qty[0][0]) if reserved_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530123
Nabin Haiteea2b342013-10-11 18:31:33 +0530124def get_indented_qty(item_code, warehouse):
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530125 # Ordered Qty is always maintained in stock UOM
126 inward_qty = frappe.db.sql("""
Nabin Hait03af0292020-04-16 20:11:32 +0530127 select sum(mr_item.stock_qty - mr_item.ordered_qty)
Nabin Hait4acd4312014-11-04 15:32:31 +0530128 from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
129 where mr_item.item_code=%s and mr_item.warehouse=%s
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530130 and mr.material_request_type in ('Purchase', 'Manufacture', 'Customer Provided', 'Material Transfer')
Nabin Hait03af0292020-04-16 20:11:32 +0530131 and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
132 and mr.status!='Stopped' and mr.docstatus=1
133 """, (item_code, warehouse))
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530134 inward_qty = flt(inward_qty[0][0]) if inward_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530135
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530136 outward_qty = frappe.db.sql("""
Nabin Hait03af0292020-04-16 20:11:32 +0530137 select sum(mr_item.stock_qty - mr_item.ordered_qty)
138 from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
139 where mr_item.item_code=%s and mr_item.warehouse=%s
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530140 and mr.material_request_type = 'Material Issue'
Nabin Hait03af0292020-04-16 20:11:32 +0530141 and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
142 and mr.status!='Stopped' and mr.docstatus=1
143 """, (item_code, warehouse))
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530144 outward_qty = flt(outward_qty[0][0]) if outward_qty else 0
marination815c36a2020-03-26 14:49:28 +0530145
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530146 requested_qty = inward_qty - outward_qty
Nabin Hait03af0292020-04-16 20:11:32 +0530147
148 return requested_qty
Nabin Haiteea2b342013-10-11 18:31:33 +0530149
150def get_ordered_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530151 ordered_qty = frappe.db.sql("""
Anand Doshi602e8252015-11-16 19:05:46 +0530152 select sum((po_item.qty - po_item.received_qty)*po_item.conversion_factor)
Nabin Haiteea2b342013-10-11 18:31:33 +0530153 from `tabPurchase Order Item` po_item, `tabPurchase Order` po
Nabin Hait62985362014-04-04 12:05:16 +0530154 where po_item.item_code=%s and po_item.warehouse=%s
Anand Doshi602e8252015-11-16 19:05:46 +0530155 and po_item.qty > po_item.received_qty and po_item.parent=po.name
patilsangrambf2b5112016-02-22 16:24:23 +0530156 and po.status not in ('Closed', 'Delivered') and po.docstatus=1
Anand Doshi602e8252015-11-16 19:05:46 +0530157 and po_item.delivered_by_supplier = 0""", (item_code, warehouse))
Nabin Hait62985362014-04-04 12:05:16 +0530158
Nabin Haiteea2b342013-10-11 18:31:33 +0530159 return flt(ordered_qty[0][0]) if ordered_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530160
Nabin Haiteea2b342013-10-11 18:31:33 +0530161def get_planned_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530162 planned_qty = frappe.db.sql("""
Zarrar13ddc7e2018-03-20 12:38:43 +0530163 select sum(qty - produced_qty) from `tabWork Order`
Nabin Hait949a9202017-07-05 13:55:41 +0530164 where production_item = %s and fg_warehouse = %s and status not in ("Stopped", "Completed")
Anand Doshi602e8252015-11-16 19:05:46 +0530165 and docstatus=1 and qty > produced_qty""", (item_code, warehouse))
Nabin Haiteea2b342013-10-11 18:31:33 +0530166
167 return flt(planned_qty[0][0]) if planned_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530168
169
Nabin Hait7f3f2a02014-09-01 18:16:05 +0530170def update_bin_qty(item_code, warehouse, qty_dict=None):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530171 from erpnext.stock.utils import get_bin
Nabin Haiteea2b342013-10-11 18:31:33 +0530172 bin = get_bin(item_code, warehouse)
173 mismatch = False
marination815c36a2020-03-26 14:49:28 +0530174 for field, value in qty_dict.items():
175 if flt(bin.get(field)) != flt(value):
176 bin.set(field, flt(value))
Nabin Haiteea2b342013-10-11 18:31:33 +0530177 mismatch = True
Nabin Hait62985362014-04-04 12:05:16 +0530178
Nabin Haiteea2b342013-10-11 18:31:33 +0530179 if mismatch:
Himanshu Mishraf56aac62018-08-08 18:32:03 +0530180 bin.set_projected_qty()
181 bin.db_update()
182 bin.clear_cache()
Nabin Hait62985362014-04-04 12:05:16 +0530183
184def set_stock_balance_as_per_serial_no(item_code=None, posting_date=None, posting_time=None,
185 fiscal_year=None):
186 if not posting_date: posting_date = nowdate()
187 if not posting_time: posting_time = nowtime()
Nabin Hait62985362014-04-04 12:05:16 +0530188
189 condition = " and item.name='%s'" % item_code.replace("'", "\'") if item_code else ""
190
191 bin = frappe.db.sql("""select bin.item_code, bin.warehouse, bin.actual_qty, item.stock_uom
192 from `tabBin` bin, tabItem item
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530193 where bin.item_code = item.name and item.has_serial_no = 1 %s""" % condition)
Nabin Hait62985362014-04-04 12:05:16 +0530194
195 for d in bin:
196 serial_nos = frappe.db.sql("""select count(name) from `tabSerial No`
Nabin Hait398c83a2015-10-22 15:11:44 +0530197 where item_code=%s and warehouse=%s and docstatus < 2""", (d[0], d[1]))
Nabin Hait62985362014-04-04 12:05:16 +0530198
Nabin Hait62985362014-04-04 12:05:16 +0530199 sle = frappe.db.sql("""select valuation_rate, company from `tabStock Ledger Entry`
Nabin Haita77b8c92020-12-21 14:45:50 +0530200 where item_code = %s and warehouse = %s and is_cancelled = 0
Nabin Hait62985362014-04-04 12:05:16 +0530201 order by posting_date desc limit 1""", (d[0], d[1]))
202
203 sle_dict = {
204 'doctype' : 'Stock Ledger Entry',
205 'item_code' : d[0],
206 'warehouse' : d[1],
207 'transaction_date' : nowdate(),
208 'posting_date' : posting_date,
209 'posting_time' : posting_time,
210 'voucher_type' : 'Stock Reconciliation (Manual)',
211 'voucher_no' : '',
212 'voucher_detail_no' : '',
213 'actual_qty' : flt(serial_nos[0][0]) - flt(d[2]),
214 'stock_uom' : d[3],
215 'incoming_rate' : sle and flt(serial_nos[0][0]) > flt(d[2]) and flt(sle[0][0]) or 0,
216 'company' : sle and cstr(sle[0][1]) or 0,
Nabin Hait62985362014-04-04 12:05:16 +0530217 'batch_no' : '',
218 'serial_no' : ''
219 }
220
221 sle_doc = frappe.get_doc(sle_dict)
Anand Doshi6dfd4302015-02-10 14:41:27 +0530222 sle_doc.flags.ignore_validate = True
223 sle_doc.flags.ignore_links = True
Nabin Hait62985362014-04-04 12:05:16 +0530224 sle_doc.insert()
225
226 args = sle_dict.copy()
227 args.update({
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530228 "sle_id": sle_doc.name
Nabin Hait62985362014-04-04 12:05:16 +0530229 })
230
231 update_bin(args)
Suraj Shettyc7024082021-05-06 18:37:45 +0530232
Nabin Haita77b8c92020-12-21 14:45:50 +0530233 create_repost_item_valuation_entry({
Nabin Hait62985362014-04-04 12:05:16 +0530234 "item_code": d[0],
235 "warehouse": d[1],
236 "posting_date": posting_date,
237 "posting_time": posting_time
238 })
nabinhait5c384882014-07-14 11:43:00 +0530239
nabinhait7700c622014-07-14 14:21:21 +0530240def reset_serial_no_status_and_warehouse(serial_nos=None):
nabinhait5c384882014-07-14 11:43:00 +0530241 if not serial_nos:
Nabin Haitc865f222015-09-21 09:18:43 +0530242 serial_nos = frappe.db.sql_list("""select name from `tabSerial No` where docstatus = 0""")
nabinhait5c384882014-07-14 11:43:00 +0530243 for serial_no in serial_nos:
244 try:
245 sr = frappe.get_doc("Serial No", serial_no)
nabinhaitb0a8d002014-07-14 11:56:03 +0530246 last_sle = sr.get_last_sle()
247 if flt(last_sle.actual_qty) > 0:
248 sr.warehouse = last_sle.warehouse
Nabin Hait7f3f2a02014-09-01 18:16:05 +0530249
nabinhait5c384882014-07-14 11:43:00 +0530250 sr.via_stock_ledger = True
251 sr.save()
Ankush Menat694ae812021-09-01 14:40:56 +0530252 except Exception:
nabinhait5c384882014-07-14 11:43:00 +0530253 pass