blob: 8917bfeae4f92c94c5ff9e7e28b6b4c8d77844e5 [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
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Nabin Hait62985362014-04-04 12:05:16 +05306from frappe.utils import flt, cstr, nowdate, nowtime
7from erpnext.stock.utils import update_bin
8from erpnext.stock.stock_ledger import update_entries_after
Nabin Haita77b8c92020-12-21 14:45:50 +05309from erpnext.controllers.stock_controller import create_repost_item_valuation_entry
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:
Nabin Hait249bbbc2014-11-26 15:35:08 +053018 existing_allow_negative_stock = frappe.db.get_value("Stock Settings", None, "allow_negative_stock")
19 frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1)
Nabin Hait62985362014-04-04 12:05:16 +053020
Nabin Hait001edb42019-09-26 16:44:35 +053021 item_warehouses = frappe.db.sql("""
22 select distinct item_code, warehouse
23 from
24 (select item_code, warehouse from tabBin
25 union
26 select item_code, warehouse from `tabStock Ledger Entry`) a
27 """)
28 for d in item_warehouses:
29 try:
30 repost_stock(d[0], d[1], allow_zero_rate, only_actual, only_bin, allow_negative_stock)
31 frappe.db.commit()
32 except:
33 frappe.db.rollback()
Nabin Hait62985362014-04-04 12:05:16 +053034
Nabin Haitca471f42013-11-20 13:14:12 +053035 if allow_negative_stock:
Nabin Hait249bbbc2014-11-26 15:35:08 +053036 frappe.db.set_value("Stock Settings", None, "allow_negative_stock", existing_allow_negative_stock)
Anand Doshie9baaa62014-02-26 12:35:33 +053037 frappe.db.auto_commit_on_many_writes = 0
Nabin Haiteea2b342013-10-11 18:31:33 +053038
Nabin Hait001edb42019-09-26 16:44:35 +053039def repost_stock(item_code, warehouse, allow_zero_rate=False,
40 only_actual=False, only_bin=False, allow_negative_stock=False):
41
Nabin Haitb7e46c42015-10-12 16:46:29 +053042 if not only_bin:
Nabin Hait001edb42019-09-26 16:44:35 +053043 repost_actual_qty(item_code, warehouse, allow_zero_rate, allow_negative_stock)
Nabin Hait62985362014-04-04 12:05:16 +053044
Nabin Hait2348a5f2014-10-15 15:31:33 +053045 if item_code and warehouse and not only_actual:
Nabin Haitb7e46c42015-10-12 16:46:29 +053046 qty_dict = {
Nabin Haiteea2b342013-10-11 18:31:33 +053047 "reserved_qty": get_reserved_qty(item_code, warehouse),
48 "indented_qty": get_indented_qty(item_code, warehouse),
49 "ordered_qty": get_ordered_qty(item_code, warehouse),
50 "planned_qty": get_planned_qty(item_code, warehouse)
Nabin Haitb7e46c42015-10-12 16:46:29 +053051 }
52 if only_bin:
53 qty_dict.update({
54 "actual_qty": get_balance_qty_from_sle(item_code, warehouse)
55 })
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053056
Nabin Haitb7e46c42015-10-12 16:46:29 +053057 update_bin_qty(item_code, warehouse, qty_dict)
Nabin Haiteea2b342013-10-11 18:31:33 +053058
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +053059def repost_actual_qty(item_code, warehouse, allow_zero_rate=False, allow_negative_stock=False):
Nabin Haita77b8c92020-12-21 14:45:50 +053060 create_repost_item_valuation_entry({
61 "item_code": item_code,
62 "warehouse": warehouse,
63 "posting_date": "1900-01-01",
64 "posting_time": "00:01",
65 "allow_negative_stock": allow_negative_stock,
66 "allow_zero_rate": allow_zero_rate
67 })
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053068
Nabin Haitb7e46c42015-10-12 16:46:29 +053069def get_balance_qty_from_sle(item_code, warehouse):
70 balance_qty = frappe.db.sql("""select qty_after_transaction from `tabStock Ledger Entry`
Nabin Haita77b8c92020-12-21 14:45:50 +053071 where item_code=%s and warehouse=%s and is_cancelled=0
Aditya Hase0c164242019-01-07 22:07:13 +053072 order by posting_date desc, posting_time desc, creation desc
Nabin Haitb7e46c42015-10-12 16:46:29 +053073 limit 1""", (item_code, warehouse))
Rushabh Mehtac4d4c7f2015-10-14 17:37:28 +053074
Nabin Haitb7e46c42015-10-12 16:46:29 +053075 return flt(balance_qty[0][0]) if balance_qty else 0.0
Nabin Hait62985362014-04-04 12:05:16 +053076
Nabin Haiteea2b342013-10-11 18:31:33 +053077def get_reserved_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +053078 reserved_qty = frappe.db.sql("""
Nabin Hait62985362014-04-04 12:05:16 +053079 select
rohitwaghchaureafa93c62017-03-29 17:29:20 +053080 sum(dnpi_qty * ((so_item_qty - so_item_delivered_qty) / so_item_qty))
Nabin Hait62985362014-04-04 12:05:16 +053081 from
Nabin Haiteea2b342013-10-11 18:31:33 +053082 (
83 (select
84 qty as dnpi_qty,
85 (
rohitwaghchaureafa93c62017-03-29 17:29:20 +053086 select qty from `tabSales Order Item`
Nabin Haiteea2b342013-10-11 18:31:33 +053087 where name = dnpi.parent_detail_docname
Saurabh2e292062015-11-18 17:03:33 +053088 and (delivered_by_supplier is null or delivered_by_supplier = 0)
Nabin Haiteea2b342013-10-11 18:31:33 +053089 ) as so_item_qty,
90 (
Anand Doshi602e8252015-11-16 19:05:46 +053091 select delivered_qty from `tabSales Order Item`
92 where name = dnpi.parent_detail_docname
93 and delivered_by_supplier = 0
Nabin Hait62985362014-04-04 12:05:16 +053094 ) as so_item_delivered_qty,
Nabin Haiteea2b342013-10-11 18:31:33 +053095 parent, name
Nabin Hait62985362014-04-04 12:05:16 +053096 from
Nabin Haiteea2b342013-10-11 18:31:33 +053097 (
98 select qty, parent_detail_docname, parent, name
Nabin Haitd1fd1e22013-10-18 12:29:11 +053099 from `tabPacked Item` dnpi_in
Nabin Haiteea2b342013-10-11 18:31:33 +0530100 where item_code = %s and warehouse = %s
101 and parenttype="Sales Order"
Nabin Haitfc2dd442014-10-17 13:05:24 +0530102 and item_code != parent_item
Nabin Haiteea2b342013-10-11 18:31:33 +0530103 and exists (select * from `tabSales Order` so
patilsangrama812d672016-02-23 19:04:29 +0530104 where name = dnpi_in.parent and docstatus = 1 and status != 'Closed')
Nabin Haiteea2b342013-10-11 18:31:33 +0530105 ) dnpi)
106 union
rohitwaghchaureafa93c62017-03-29 17:29:20 +0530107 (select stock_qty as dnpi_qty, qty as so_item_qty,
Anand Doshi602e8252015-11-16 19:05:46 +0530108 delivered_qty as so_item_delivered_qty, parent, name
Nabin Haiteea2b342013-10-11 18:31:33 +0530109 from `tabSales Order Item` so_item
Anand Doshi602e8252015-11-16 19:05:46 +0530110 where item_code = %s and warehouse = %s
Saurabh2e292062015-11-18 17:03:33 +0530111 and (so_item.delivered_by_supplier is null or so_item.delivered_by_supplier = 0)
Nabin Haiteea2b342013-10-11 18:31:33 +0530112 and exists(select * from `tabSales Order` so
Nabin Hait62985362014-04-04 12:05:16 +0530113 where so.name = so_item.parent and so.docstatus = 1
patilsangrama812d672016-02-23 19:04:29 +0530114 and so.status != 'Closed'))
Nabin Haiteea2b342013-10-11 18:31:33 +0530115 ) tab
Nabin Hait62985362014-04-04 12:05:16 +0530116 where
Nabin Haiteea2b342013-10-11 18:31:33 +0530117 so_item_qty >= so_item_delivered_qty
118 """, (item_code, warehouse, item_code, warehouse))
119
120 return flt(reserved_qty[0][0]) if reserved_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530121
Nabin Haiteea2b342013-10-11 18:31:33 +0530122def get_indented_qty(item_code, warehouse):
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530123 # Ordered Qty is always maintained in stock UOM
124 inward_qty = frappe.db.sql("""
Nabin Hait03af0292020-04-16 20:11:32 +0530125 select sum(mr_item.stock_qty - mr_item.ordered_qty)
Nabin Hait4acd4312014-11-04 15:32:31 +0530126 from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
127 where mr_item.item_code=%s and mr_item.warehouse=%s
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530128 and mr.material_request_type in ('Purchase', 'Manufacture', 'Customer Provided', 'Material Transfer')
Nabin Hait03af0292020-04-16 20:11:32 +0530129 and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
130 and mr.status!='Stopped' and mr.docstatus=1
131 """, (item_code, warehouse))
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530132 inward_qty = flt(inward_qty[0][0]) if inward_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530133
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530134 outward_qty = frappe.db.sql("""
Nabin Hait03af0292020-04-16 20:11:32 +0530135 select sum(mr_item.stock_qty - mr_item.ordered_qty)
136 from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
137 where mr_item.item_code=%s and mr_item.warehouse=%s
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530138 and mr.material_request_type = 'Material Issue'
Nabin Hait03af0292020-04-16 20:11:32 +0530139 and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
140 and mr.status!='Stopped' and mr.docstatus=1
141 """, (item_code, warehouse))
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530142 outward_qty = flt(outward_qty[0][0]) if outward_qty else 0
marination815c36a2020-03-26 14:49:28 +0530143
Nabin Hait7eaa7f22020-04-17 10:52:28 +0530144 requested_qty = inward_qty - outward_qty
Nabin Hait03af0292020-04-16 20:11:32 +0530145
146 return requested_qty
Nabin Haiteea2b342013-10-11 18:31:33 +0530147
148def get_ordered_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530149 ordered_qty = frappe.db.sql("""
Anand Doshi602e8252015-11-16 19:05:46 +0530150 select sum((po_item.qty - po_item.received_qty)*po_item.conversion_factor)
Nabin Haiteea2b342013-10-11 18:31:33 +0530151 from `tabPurchase Order Item` po_item, `tabPurchase Order` po
Nabin Hait62985362014-04-04 12:05:16 +0530152 where po_item.item_code=%s and po_item.warehouse=%s
Anand Doshi602e8252015-11-16 19:05:46 +0530153 and po_item.qty > po_item.received_qty and po_item.parent=po.name
patilsangrambf2b5112016-02-22 16:24:23 +0530154 and po.status not in ('Closed', 'Delivered') and po.docstatus=1
Anand Doshi602e8252015-11-16 19:05:46 +0530155 and po_item.delivered_by_supplier = 0""", (item_code, warehouse))
Nabin Hait62985362014-04-04 12:05:16 +0530156
Nabin Haiteea2b342013-10-11 18:31:33 +0530157 return flt(ordered_qty[0][0]) if ordered_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530158
Nabin Haiteea2b342013-10-11 18:31:33 +0530159def get_planned_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530160 planned_qty = frappe.db.sql("""
Zarrar13ddc7e2018-03-20 12:38:43 +0530161 select sum(qty - produced_qty) from `tabWork Order`
Nabin Hait949a9202017-07-05 13:55:41 +0530162 where production_item = %s and fg_warehouse = %s and status not in ("Stopped", "Completed")
Anand Doshi602e8252015-11-16 19:05:46 +0530163 and docstatus=1 and qty > produced_qty""", (item_code, warehouse))
Nabin Haiteea2b342013-10-11 18:31:33 +0530164
165 return flt(planned_qty[0][0]) if planned_qty else 0
Nabin Hait62985362014-04-04 12:05:16 +0530166
167
Nabin Hait7f3f2a02014-09-01 18:16:05 +0530168def update_bin_qty(item_code, warehouse, qty_dict=None):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530169 from erpnext.stock.utils import get_bin
Nabin Haiteea2b342013-10-11 18:31:33 +0530170 bin = get_bin(item_code, warehouse)
171 mismatch = False
marination815c36a2020-03-26 14:49:28 +0530172 for field, value in qty_dict.items():
173 if flt(bin.get(field)) != flt(value):
174 bin.set(field, flt(value))
Nabin Haiteea2b342013-10-11 18:31:33 +0530175 mismatch = True
Nabin Hait62985362014-04-04 12:05:16 +0530176
Nabin Haiteea2b342013-10-11 18:31:33 +0530177 if mismatch:
Himanshu Mishraf56aac62018-08-08 18:32:03 +0530178 bin.set_projected_qty()
179 bin.db_update()
180 bin.clear_cache()
Nabin Hait62985362014-04-04 12:05:16 +0530181
182def set_stock_balance_as_per_serial_no(item_code=None, posting_date=None, posting_time=None,
183 fiscal_year=None):
184 if not posting_date: posting_date = nowdate()
185 if not posting_time: posting_time = nowtime()
Nabin Hait62985362014-04-04 12:05:16 +0530186
187 condition = " and item.name='%s'" % item_code.replace("'", "\'") if item_code else ""
188
189 bin = frappe.db.sql("""select bin.item_code, bin.warehouse, bin.actual_qty, item.stock_uom
190 from `tabBin` bin, tabItem item
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530191 where bin.item_code = item.name and item.has_serial_no = 1 %s""" % condition)
Nabin Hait62985362014-04-04 12:05:16 +0530192
193 for d in bin:
194 serial_nos = frappe.db.sql("""select count(name) from `tabSerial No`
Nabin Hait398c83a2015-10-22 15:11:44 +0530195 where item_code=%s and warehouse=%s and docstatus < 2""", (d[0], d[1]))
Nabin Hait62985362014-04-04 12:05:16 +0530196
Nabin Hait62985362014-04-04 12:05:16 +0530197 sle = frappe.db.sql("""select valuation_rate, company from `tabStock Ledger Entry`
Nabin Haita77b8c92020-12-21 14:45:50 +0530198 where item_code = %s and warehouse = %s and is_cancelled = 0
Nabin Hait62985362014-04-04 12:05:16 +0530199 order by posting_date desc limit 1""", (d[0], d[1]))
200
201 sle_dict = {
202 'doctype' : 'Stock Ledger Entry',
203 'item_code' : d[0],
204 'warehouse' : d[1],
205 'transaction_date' : nowdate(),
206 'posting_date' : posting_date,
207 'posting_time' : posting_time,
208 'voucher_type' : 'Stock Reconciliation (Manual)',
209 'voucher_no' : '',
210 'voucher_detail_no' : '',
211 'actual_qty' : flt(serial_nos[0][0]) - flt(d[2]),
212 'stock_uom' : d[3],
213 'incoming_rate' : sle and flt(serial_nos[0][0]) > flt(d[2]) and flt(sle[0][0]) or 0,
214 'company' : sle and cstr(sle[0][1]) or 0,
Nabin Hait62985362014-04-04 12:05:16 +0530215 'batch_no' : '',
216 'serial_no' : ''
217 }
218
219 sle_doc = frappe.get_doc(sle_dict)
Anand Doshi6dfd4302015-02-10 14:41:27 +0530220 sle_doc.flags.ignore_validate = True
221 sle_doc.flags.ignore_links = True
Nabin Hait62985362014-04-04 12:05:16 +0530222 sle_doc.insert()
223
224 args = sle_dict.copy()
225 args.update({
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530226 "sle_id": sle_doc.name
Nabin Hait62985362014-04-04 12:05:16 +0530227 })
228
229 update_bin(args)
Suraj Shettyc26d41a2021-05-06 18:37:45 +0530230
Nabin Haita77b8c92020-12-21 14:45:50 +0530231 create_repost_item_valuation_entry({
Nabin Hait62985362014-04-04 12:05:16 +0530232 "item_code": d[0],
233 "warehouse": d[1],
234 "posting_date": posting_date,
235 "posting_time": posting_time
236 })
nabinhait5c384882014-07-14 11:43:00 +0530237
nabinhait7700c622014-07-14 14:21:21 +0530238def reset_serial_no_status_and_warehouse(serial_nos=None):
nabinhait5c384882014-07-14 11:43:00 +0530239 if not serial_nos:
Nabin Haitc865f222015-09-21 09:18:43 +0530240 serial_nos = frappe.db.sql_list("""select name from `tabSerial No` where docstatus = 0""")
nabinhait5c384882014-07-14 11:43:00 +0530241 for serial_no in serial_nos:
242 try:
243 sr = frappe.get_doc("Serial No", serial_no)
nabinhaitb0a8d002014-07-14 11:56:03 +0530244 last_sle = sr.get_last_sle()
245 if flt(last_sle.actual_qty) > 0:
246 sr.warehouse = last_sle.warehouse
Nabin Hait7f3f2a02014-09-01 18:16:05 +0530247
nabinhait5c384882014-07-14 11:43:00 +0530248 sr.via_stock_ledger = True
249 sr.save()
250 except:
251 pass