blob: 6ac8da16ee7d5ba430bf215d72b36c795c86ba69 [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
Chillar Anand915b3432021-09-02 16:44:59 +05306from frappe.utils import cstr, flt, nowdate, nowtime
7
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 +05309from erpnext.stock.utils import update_bin
10
Nabin Haiteea2b342013-10-11 18:31:33 +053011
Nabin Haitb7e46c42015-10-12 16:46:29 +053012def repost(only_actual=False, allow_negative_stock=False, allow_zero_rate=False, only_bin=False):
Nabin Haiteea2b342013-10-11 18:31:33 +053013 """
14 Repost everything!
15 """
Anand Doshie9baaa62014-02-26 12:35:33 +053016 frappe.db.auto_commit_on_many_writes = 1
Nabin Hait62985362014-04-04 12:05:16 +053017
Nabin Haitca471f42013-11-20 13:14:12 +053018 if allow_negative_stock:
Nabin Hait249bbbc2014-11-26 15:35:08 +053019 existing_allow_negative_stock = frappe.db.get_value("Stock Settings", None, "allow_negative_stock")
20 frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1)
Nabin Hait62985362014-04-04 12:05:16 +053021
Nabin Hait001edb42019-09-26 16:44:35 +053022 item_warehouses = frappe.db.sql("""
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
28 """)
29 for d in item_warehouses:
30 try:
31 repost_stock(d[0], d[1], allow_zero_rate, only_actual, only_bin, allow_negative_stock)
32 frappe.db.commit()
Ankush Menat694ae812021-09-01 14:40:56 +053033 except Exception:
Nabin Hait001edb42019-09-26 16:44:35 +053034 frappe.db.rollback()
Nabin Hait62985362014-04-04 12:05:16 +053035
Nabin Haitca471f42013-11-20 13:14:12 +053036 if allow_negative_stock:
Nabin Hait249bbbc2014-11-26 15:35:08 +053037 frappe.db.set_value("Stock Settings", None, "allow_negative_stock", existing_allow_negative_stock)
Anand Doshie9baaa62014-02-26 12:35:33 +053038 frappe.db.auto_commit_on_many_writes = 0
Nabin Haiteea2b342013-10-11 18:31:33 +053039
Nabin Hait001edb42019-09-26 16:44:35 +053040def repost_stock(item_code, warehouse, allow_zero_rate=False,
41 only_actual=False, only_bin=False, allow_negative_stock=False):
42
Nabin Haitb7e46c42015-10-12 16:46:29 +053043 if not only_bin:
Nabin Hait001edb42019-09-26 16:44:35 +053044 repost_actual_qty(item_code, warehouse, allow_zero_rate, allow_negative_stock)
Nabin Hait62985362014-04-04 12:05:16 +053045
Nabin Hait2348a5f2014-10-15 15:31:33 +053046 if item_code and warehouse and not only_actual:
Nabin Haitb7e46c42015-10-12 16:46:29 +053047 qty_dict = {
Nabin Haiteea2b342013-10-11 18:31:33 +053048 "reserved_qty": get_reserved_qty(item_code, warehouse),
49 "indented_qty": get_indented_qty(item_code, warehouse),
50 "ordered_qty": get_ordered_qty(item_code, warehouse),
51 "planned_qty": get_planned_qty(item_code, warehouse)
Nabin Haitb7e46c42015-10-12 16:46:29 +053052 }
53 if only_bin:
54 qty_dict.update({
55 "actual_qty": get_balance_qty_from_sle(item_code, warehouse)
56 })
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053057
Nabin Haitb7e46c42015-10-12 16:46:29 +053058 update_bin_qty(item_code, warehouse, qty_dict)
Nabin Haiteea2b342013-10-11 18:31:33 +053059
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +053060def repost_actual_qty(item_code, warehouse, allow_zero_rate=False, allow_negative_stock=False):
Nabin Haita77b8c92020-12-21 14:45:50 +053061 create_repost_item_valuation_entry({
62 "item_code": item_code,
63 "warehouse": warehouse,
64 "posting_date": "1900-01-01",
65 "posting_time": "00:01",
66 "allow_negative_stock": allow_negative_stock,
67 "allow_zero_rate": allow_zero_rate
68 })
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053069
Nabin Haitb7e46c42015-10-12 16:46:29 +053070def get_balance_qty_from_sle(item_code, warehouse):
71 balance_qty = frappe.db.sql("""select qty_after_transaction from `tabStock Ledger Entry`
Nabin Haita77b8c92020-12-21 14:45:50 +053072 where item_code=%s and warehouse=%s and is_cancelled=0
Aditya Hase0c164242019-01-07 22:07:13 +053073 order by posting_date desc, posting_time desc, creation desc
Nabin Haitb7e46c42015-10-12 16:46:29 +053074 limit 1""", (item_code, warehouse))
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053075
Nabin Haitb7e46c42015-10-12 16:46:29 +053076 return flt(balance_qty[0][0]) if balance_qty else 0.0
Nabin Hait62985362014-04-04 12:05:16 +053077
Nabin Haiteea2b342013-10-11 18:31:33 +053078def get_reserved_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +053079 reserved_qty = frappe.db.sql("""
Nabin Hait62985362014-04-04 12:05:16 +053080 select
rohitwaghchaureafa93c62017-03-29 17:29:20 +053081 sum(dnpi_qty * ((so_item_qty - so_item_delivered_qty) / so_item_qty))
Nabin Hait62985362014-04-04 12:05:16 +053082 from
Nabin Haiteea2b342013-10-11 18:31:33 +053083 (
84 (select
85 qty as dnpi_qty,
86 (
rohitwaghchaureafa93c62017-03-29 17:29:20 +053087 select qty from `tabSales Order Item`
Nabin Haiteea2b342013-10-11 18:31:33 +053088 where name = dnpi.parent_detail_docname
Saurabh2e292062015-11-18 17:03:33 +053089 and (delivered_by_supplier is null or delivered_by_supplier = 0)
Nabin Haiteea2b342013-10-11 18:31:33 +053090 ) as so_item_qty,
91 (
Anand Doshi602e8252015-11-16 19:05:46 +053092 select delivered_qty from `tabSales Order Item`
93 where name = dnpi.parent_detail_docname
94 and delivered_by_supplier = 0
Nabin Hait62985362014-04-04 12:05:16 +053095 ) as so_item_delivered_qty,
Nabin Haiteea2b342013-10-11 18:31:33 +053096 parent, name
Nabin Hait62985362014-04-04 12:05:16 +053097 from
Nabin Haiteea2b342013-10-11 18:31:33 +053098 (
99 select qty, parent_detail_docname, parent, name
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530100 from `tabPacked Item` dnpi_in
Nabin Haiteea2b342013-10-11 18:31:33 +0530101 where item_code = %s and warehouse = %s
102 and parenttype="Sales Order"
Nabin Haitfc2dd442014-10-17 13:05:24 +0530103 and item_code != parent_item
Nabin Haiteea2b342013-10-11 18:31:33 +0530104 and exists (select * from `tabSales Order` so
patilsangrama812d672016-02-23 19:04:29 +0530105 where name = dnpi_in.parent and docstatus = 1 and status != 'Closed')
Nabin Haiteea2b342013-10-11 18:31:33 +0530106 ) dnpi)
107 union
rohitwaghchaureafa93c62017-03-29 17:29:20 +0530108 (select stock_qty as dnpi_qty, qty as so_item_qty,
Anand Doshi602e8252015-11-16 19:05:46 +0530109 delivered_qty as so_item_delivered_qty, parent, name
Nabin Haiteea2b342013-10-11 18:31:33 +0530110 from `tabSales Order Item` so_item
Anand Doshi602e8252015-11-16 19:05:46 +0530111 where item_code = %s and warehouse = %s
Saurabh2e292062015-11-18 17:03:33 +0530112 and (so_item.delivered_by_supplier is null or so_item.delivered_by_supplier = 0)
Nabin Haiteea2b342013-10-11 18:31:33 +0530113 and exists(select * from `tabSales Order` so
Nabin Hait62985362014-04-04 12:05:16 +0530114 where so.name = so_item.parent and so.docstatus = 1
patilsangrama812d672016-02-23 19:04:29 +0530115 and so.status != 'Closed'))
Nabin Haiteea2b342013-10-11 18:31:33 +0530116 ) tab
Nabin Hait62985362014-04-04 12:05:16 +0530117 where
Nabin Haiteea2b342013-10-11 18:31:33 +0530118 so_item_qty >= so_item_delivered_qty
119 """, (item_code, warehouse, item_code, warehouse))
120
121 return flt(reserved_qty[0][0]) if reserved_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530122
Nabin Haiteea2b342013-10-11 18:31:33 +0530123def get_indented_qty(item_code, warehouse):
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530124 # Ordered Qty is always maintained in stock UOM
125 inward_qty = frappe.db.sql("""
Nabin Hait03af0292020-04-16 20:11:32 +0530126 select sum(mr_item.stock_qty - mr_item.ordered_qty)
Nabin Hait4acd4312014-11-04 15:32:31 +0530127 from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
128 where mr_item.item_code=%s and mr_item.warehouse=%s
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530129 and mr.material_request_type in ('Purchase', 'Manufacture', 'Customer Provided', 'Material Transfer')
Nabin Hait03af0292020-04-16 20:11:32 +0530130 and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
131 and mr.status!='Stopped' and mr.docstatus=1
132 """, (item_code, warehouse))
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530133 inward_qty = flt(inward_qty[0][0]) if inward_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530134
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530135 outward_qty = frappe.db.sql("""
Nabin Hait03af0292020-04-16 20:11:32 +0530136 select sum(mr_item.stock_qty - mr_item.ordered_qty)
137 from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
138 where mr_item.item_code=%s and mr_item.warehouse=%s
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530139 and mr.material_request_type = 'Material Issue'
Nabin Hait03af0292020-04-16 20:11:32 +0530140 and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
141 and mr.status!='Stopped' and mr.docstatus=1
142 """, (item_code, warehouse))
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530143 outward_qty = flt(outward_qty[0][0]) if outward_qty else 0
marination815c36a2020-03-26 14:49:28 +0530144
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530145 requested_qty = inward_qty - outward_qty
Nabin Hait03af0292020-04-16 20:11:32 +0530146
147 return requested_qty
Nabin Haiteea2b342013-10-11 18:31:33 +0530148
149def get_ordered_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530150 ordered_qty = frappe.db.sql("""
Anand Doshi602e8252015-11-16 19:05:46 +0530151 select sum((po_item.qty - po_item.received_qty)*po_item.conversion_factor)
Nabin Haiteea2b342013-10-11 18:31:33 +0530152 from `tabPurchase Order Item` po_item, `tabPurchase Order` po
Nabin Hait62985362014-04-04 12:05:16 +0530153 where po_item.item_code=%s and po_item.warehouse=%s
Anand Doshi602e8252015-11-16 19:05:46 +0530154 and po_item.qty > po_item.received_qty and po_item.parent=po.name
patilsangrambf2b5112016-02-22 16:24:23 +0530155 and po.status not in ('Closed', 'Delivered') and po.docstatus=1
Anand Doshi602e8252015-11-16 19:05:46 +0530156 and po_item.delivered_by_supplier = 0""", (item_code, warehouse))
Nabin Hait62985362014-04-04 12:05:16 +0530157
Nabin Haiteea2b342013-10-11 18:31:33 +0530158 return flt(ordered_qty[0][0]) if ordered_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530159
Nabin Haiteea2b342013-10-11 18:31:33 +0530160def get_planned_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530161 planned_qty = frappe.db.sql("""
Zarrar13ddc7e2018-03-20 12:38:43 +0530162 select sum(qty - produced_qty) from `tabWork Order`
Nabin Hait949a9202017-07-05 13:55:41 +0530163 where production_item = %s and fg_warehouse = %s and status not in ("Stopped", "Completed")
Anand Doshi602e8252015-11-16 19:05:46 +0530164 and docstatus=1 and qty > produced_qty""", (item_code, warehouse))
Nabin Haiteea2b342013-10-11 18:31:33 +0530165
166 return flt(planned_qty[0][0]) if planned_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530167
168
Nabin Hait7f3f2a02014-09-01 18:16:05 +0530169def update_bin_qty(item_code, warehouse, qty_dict=None):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530170 from erpnext.stock.utils import get_bin
Nabin Haiteea2b342013-10-11 18:31:33 +0530171 bin = get_bin(item_code, warehouse)
172 mismatch = False
marination815c36a2020-03-26 14:49:28 +0530173 for field, value in qty_dict.items():
174 if flt(bin.get(field)) != flt(value):
175 bin.set(field, flt(value))
Nabin Haiteea2b342013-10-11 18:31:33 +0530176 mismatch = True
Nabin Hait62985362014-04-04 12:05:16 +0530177
Nabin Haiteea2b342013-10-11 18:31:33 +0530178 if mismatch:
Himanshu Mishraf56aac62018-08-08 18:32:03 +0530179 bin.set_projected_qty()
180 bin.db_update()
181 bin.clear_cache()
Nabin Hait62985362014-04-04 12:05:16 +0530182
183def set_stock_balance_as_per_serial_no(item_code=None, posting_date=None, posting_time=None,
184 fiscal_year=None):
185 if not posting_date: posting_date = nowdate()
186 if not posting_time: posting_time = nowtime()
Nabin Hait62985362014-04-04 12:05:16 +0530187
188 condition = " and item.name='%s'" % item_code.replace("'", "\'") if item_code else ""
189
190 bin = frappe.db.sql("""select bin.item_code, bin.warehouse, bin.actual_qty, item.stock_uom
191 from `tabBin` bin, tabItem item
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530192 where bin.item_code = item.name and item.has_serial_no = 1 %s""" % condition)
Nabin Hait62985362014-04-04 12:05:16 +0530193
194 for d in bin:
195 serial_nos = frappe.db.sql("""select count(name) from `tabSerial No`
Nabin Hait398c83a2015-10-22 15:11:44 +0530196 where item_code=%s and warehouse=%s and docstatus < 2""", (d[0], d[1]))
Nabin Hait62985362014-04-04 12:05:16 +0530197
Nabin Hait62985362014-04-04 12:05:16 +0530198 sle = frappe.db.sql("""select valuation_rate, company from `tabStock Ledger Entry`
Nabin Haita77b8c92020-12-21 14:45:50 +0530199 where item_code = %s and warehouse = %s and is_cancelled = 0
Nabin Hait62985362014-04-04 12:05:16 +0530200 order by posting_date desc limit 1""", (d[0], d[1]))
201
202 sle_dict = {
203 'doctype' : 'Stock Ledger Entry',
204 'item_code' : d[0],
205 'warehouse' : d[1],
206 'transaction_date' : nowdate(),
207 'posting_date' : posting_date,
208 'posting_time' : posting_time,
209 'voucher_type' : 'Stock Reconciliation (Manual)',
210 'voucher_no' : '',
211 'voucher_detail_no' : '',
212 'actual_qty' : flt(serial_nos[0][0]) - flt(d[2]),
213 'stock_uom' : d[3],
214 'incoming_rate' : sle and flt(serial_nos[0][0]) > flt(d[2]) and flt(sle[0][0]) or 0,
215 'company' : sle and cstr(sle[0][1]) or 0,
Nabin Hait62985362014-04-04 12:05:16 +0530216 'batch_no' : '',
217 'serial_no' : ''
218 }
219
220 sle_doc = frappe.get_doc(sle_dict)
Anand Doshi6dfd4302015-02-10 14:41:27 +0530221 sle_doc.flags.ignore_validate = True
222 sle_doc.flags.ignore_links = True
Nabin Hait62985362014-04-04 12:05:16 +0530223 sle_doc.insert()
224
225 args = sle_dict.copy()
226 args.update({
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530227 "sle_id": sle_doc.name
Nabin Hait62985362014-04-04 12:05:16 +0530228 })
229
230 update_bin(args)
Suraj Shettyc7024082021-05-06 18:37:45 +0530231
Nabin Haita77b8c92020-12-21 14:45:50 +0530232 create_repost_item_valuation_entry({
Nabin Hait62985362014-04-04 12:05:16 +0530233 "item_code": d[0],
234 "warehouse": d[1],
235 "posting_date": posting_date,
236 "posting_time": posting_time
237 })
nabinhait5c384882014-07-14 11:43:00 +0530238
nabinhait7700c622014-07-14 14:21:21 +0530239def reset_serial_no_status_and_warehouse(serial_nos=None):
nabinhait5c384882014-07-14 11:43:00 +0530240 if not serial_nos:
Nabin Haitc865f222015-09-21 09:18:43 +0530241 serial_nos = frappe.db.sql_list("""select name from `tabSerial No` where docstatus = 0""")
nabinhait5c384882014-07-14 11:43:00 +0530242 for serial_no in serial_nos:
243 try:
244 sr = frappe.get_doc("Serial No", serial_no)
nabinhaitb0a8d002014-07-14 11:56:03 +0530245 last_sle = sr.get_last_sle()
246 if flt(last_sle.actual_qty) > 0:
247 sr.warehouse = last_sle.warehouse
Nabin Hait7f3f2a02014-09-01 18:16:05 +0530248
nabinhait5c384882014-07-14 11:43:00 +0530249 sr.via_stock_ledger = True
250 sr.save()
Ankush Menat694ae812021-09-01 14:40:56 +0530251 except Exception:
nabinhait5c384882014-07-14 11:43:00 +0530252 pass