2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-10-11 13:01:33 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2022-02-11 05:59:37 +00:00
|
|
|
from frappe.utils import cstr, flt, now, nowdate, nowtime
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-12-21 09:15:50 +00:00
|
|
|
from erpnext.controllers.stock_controller import create_repost_item_valuation_entry
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2013-10-11 13:01:33 +00:00
|
|
|
|
2015-10-12 11:16:29 +00:00
|
|
|
def repost(only_actual=False, allow_negative_stock=False, allow_zero_rate=False, only_bin=False):
|
2013-10-11 13:01:33 +00:00
|
|
|
"""
|
|
|
|
Repost everything!
|
|
|
|
"""
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.auto_commit_on_many_writes = 1
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2013-11-20 07:44:12 +00:00
|
|
|
if allow_negative_stock:
|
2014-11-26 10:05:08 +00:00
|
|
|
existing_allow_negative_stock = frappe.db.get_value(
|
|
|
|
"Stock Settings", None, "allow_negative_stock"
|
|
|
|
)
|
|
|
|
frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1)
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2019-09-26 11:14:35 +00:00
|
|
|
item_warehouses = frappe.db.sql(
|
|
|
|
"""
|
|
|
|
select distinct item_code, warehouse
|
|
|
|
from
|
|
|
|
(select item_code, warehouse from tabBin
|
|
|
|
union
|
|
|
|
select item_code, warehouse from `tabStock Ledger Entry`) a
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
for d in item_warehouses:
|
|
|
|
try:
|
|
|
|
repost_stock(d[0], d[1], allow_zero_rate, only_actual, only_bin, allow_negative_stock)
|
|
|
|
frappe.db.commit()
|
2021-09-01 09:10:56 +00:00
|
|
|
except Exception:
|
2019-09-26 11:14:35 +00:00
|
|
|
frappe.db.rollback()
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2013-11-20 07:44:12 +00:00
|
|
|
if allow_negative_stock:
|
2014-11-26 10:05:08 +00:00
|
|
|
frappe.db.set_value(
|
|
|
|
"Stock Settings", None, "allow_negative_stock", existing_allow_negative_stock
|
|
|
|
)
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.auto_commit_on_many_writes = 0
|
2013-10-11 13:01:33 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-09-26 11:14:35 +00:00
|
|
|
def repost_stock(
|
|
|
|
item_code,
|
|
|
|
warehouse,
|
|
|
|
allow_zero_rate=False,
|
|
|
|
only_actual=False,
|
|
|
|
only_bin=False,
|
|
|
|
allow_negative_stock=False,
|
|
|
|
):
|
|
|
|
|
2015-10-12 11:16:29 +00:00
|
|
|
if not only_bin:
|
2019-09-26 11:14:35 +00:00
|
|
|
repost_actual_qty(item_code, warehouse, allow_zero_rate, allow_negative_stock)
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2014-10-15 10:01:33 +00:00
|
|
|
if item_code and warehouse and not only_actual:
|
2015-10-12 11:16:29 +00:00
|
|
|
qty_dict = {
|
2013-10-11 13:01:33 +00:00
|
|
|
"reserved_qty": get_reserved_qty(item_code, warehouse),
|
|
|
|
"indented_qty": get_indented_qty(item_code, warehouse),
|
|
|
|
"ordered_qty": get_ordered_qty(item_code, warehouse),
|
|
|
|
"planned_qty": get_planned_qty(item_code, warehouse),
|
2015-10-12 11:16:29 +00:00
|
|
|
}
|
|
|
|
if only_bin:
|
|
|
|
qty_dict.update({"actual_qty": get_balance_qty_from_sle(item_code, warehouse)})
|
2015-10-14 12:07:28 +00:00
|
|
|
|
2015-10-12 11:16:29 +00:00
|
|
|
update_bin_qty(item_code, warehouse, qty_dict)
|
2013-10-11 13:01:33 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-04-30 05:08:58 +00:00
|
|
|
def repost_actual_qty(item_code, warehouse, allow_zero_rate=False, allow_negative_stock=False):
|
2020-12-21 09:15:50 +00:00
|
|
|
create_repost_item_valuation_entry(
|
|
|
|
{
|
|
|
|
"item_code": item_code,
|
|
|
|
"warehouse": warehouse,
|
|
|
|
"posting_date": "1900-01-01",
|
|
|
|
"posting_time": "00:01",
|
|
|
|
"allow_negative_stock": allow_negative_stock,
|
|
|
|
"allow_zero_rate": allow_zero_rate,
|
|
|
|
}
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-10-14 12:07:28 +00:00
|
|
|
|
2015-10-12 11:16:29 +00:00
|
|
|
def get_balance_qty_from_sle(item_code, warehouse):
|
|
|
|
balance_qty = frappe.db.sql(
|
|
|
|
"""select qty_after_transaction from `tabStock Ledger Entry`
|
2020-12-21 09:15:50 +00:00
|
|
|
where item_code=%s and warehouse=%s and is_cancelled=0
|
2019-01-07 16:37:13 +00:00
|
|
|
order by posting_date desc, posting_time desc, creation desc
|
2015-10-12 11:16:29 +00:00
|
|
|
limit 1""",
|
|
|
|
(item_code, warehouse),
|
|
|
|
)
|
2015-10-14 12:07:28 +00:00
|
|
|
|
2015-10-12 11:16:29 +00:00
|
|
|
return flt(balance_qty[0][0]) if balance_qty else 0.0
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-10-11 13:01:33 +00:00
|
|
|
def get_reserved_qty(item_code, warehouse):
|
2023-03-27 14:24:15 +00:00
|
|
|
dont_reserve_on_return = frappe.get_cached_value(
|
|
|
|
"Selling Settings", "Selling Settings", "dont_reserve_sales_order_qty_on_sales_return"
|
|
|
|
)
|
2023-03-25 04:24:53 +00:00
|
|
|
reserved_qty = frappe.db.sql(
|
2023-03-27 14:24:15 +00:00
|
|
|
f"""
|
2023-03-25 04:24:53 +00:00
|
|
|
select
|
2023-03-27 14:24:15 +00:00
|
|
|
sum(dnpi_qty * ((so_item_qty - so_item_delivered_qty - if(dont_reserve_qty_on_return, so_item_returned_qty, 0)) / so_item_qty))
|
2023-03-25 04:24:53 +00:00
|
|
|
from
|
|
|
|
(
|
|
|
|
(select
|
|
|
|
qty as dnpi_qty,
|
2022-05-14 15:06:24 +00:00
|
|
|
(
|
2023-03-25 04:24:53 +00:00
|
|
|
select qty from `tabSales Order Item`
|
|
|
|
where name = dnpi.parent_detail_docname
|
|
|
|
and (delivered_by_supplier is null or delivered_by_supplier = 0)
|
|
|
|
) as so_item_qty,
|
|
|
|
(
|
|
|
|
select delivered_qty from `tabSales Order Item`
|
|
|
|
where name = dnpi.parent_detail_docname
|
|
|
|
and delivered_by_supplier = 0
|
|
|
|
) as so_item_delivered_qty,
|
2023-03-27 14:24:15 +00:00
|
|
|
(
|
|
|
|
select returned_qty from `tabSales Order Item`
|
|
|
|
where name = dnpi.parent_detail_docname
|
|
|
|
and delivered_by_supplier = 0
|
|
|
|
) as so_item_returned_qty,
|
|
|
|
{dont_reserve_on_return} as dont_reserve_qty_on_return,
|
2023-03-25 04:24:53 +00:00
|
|
|
parent, name
|
|
|
|
from
|
|
|
|
(
|
|
|
|
select qty, parent_detail_docname, parent, name
|
|
|
|
from `tabPacked Item` dnpi_in
|
|
|
|
where item_code = %s and warehouse = %s
|
|
|
|
and parenttype='Sales Order'
|
|
|
|
and item_code != parent_item
|
|
|
|
and exists (select * from `tabSales Order` so
|
|
|
|
where name = dnpi_in.parent and docstatus = 1 and status not in ('On Hold', 'Closed'))
|
|
|
|
) dnpi)
|
|
|
|
union
|
|
|
|
(select stock_qty as dnpi_qty, qty as so_item_qty,
|
2023-03-27 14:24:15 +00:00
|
|
|
delivered_qty as so_item_delivered_qty,
|
|
|
|
returned_qty as so_item_returned_qty,
|
|
|
|
{dont_reserve_on_return}, parent, name
|
2023-03-25 04:24:53 +00:00
|
|
|
from `tabSales Order Item` so_item
|
|
|
|
where item_code = %s and warehouse = %s
|
|
|
|
and (so_item.delivered_by_supplier is null or so_item.delivered_by_supplier = 0)
|
|
|
|
and exists(select * from `tabSales Order` so
|
|
|
|
where so.name = so_item.parent and so.docstatus = 1
|
|
|
|
and so.status not in ('On Hold', 'Closed')))
|
|
|
|
) tab
|
|
|
|
where
|
|
|
|
so_item_qty >= so_item_delivered_qty
|
|
|
|
""",
|
|
|
|
(item_code, warehouse, item_code, warehouse),
|
2022-06-17 18:45:33 +00:00
|
|
|
)
|
2013-10-11 13:01:33 +00:00
|
|
|
|
|
|
|
return flt(reserved_qty[0][0]) if reserved_qty else 0
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-10-11 13:01:33 +00:00
|
|
|
def get_indented_qty(item_code, warehouse):
|
2020-04-17 05:22:28 +00:00
|
|
|
# Ordered Qty is always maintained in stock UOM
|
|
|
|
inward_qty = frappe.db.sql(
|
|
|
|
"""
|
2020-04-16 14:41:32 +00:00
|
|
|
select sum(mr_item.stock_qty - mr_item.ordered_qty)
|
|
|
|
from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
|
|
|
|
where mr_item.item_code=%s and mr_item.warehouse=%s
|
2020-04-17 05:22:28 +00:00
|
|
|
and mr.material_request_type in ('Purchase', 'Manufacture', 'Customer Provided', 'Material Transfer')
|
2020-04-16 14:41:32 +00:00
|
|
|
and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
|
|
|
|
and mr.status!='Stopped' and mr.docstatus=1
|
|
|
|
""",
|
|
|
|
(item_code, warehouse),
|
|
|
|
)
|
2020-04-17 05:22:28 +00:00
|
|
|
inward_qty = flt(inward_qty[0][0]) if inward_qty else 0
|
2020-04-16 14:41:32 +00:00
|
|
|
|
2020-04-17 05:22:28 +00:00
|
|
|
outward_qty = frappe.db.sql(
|
|
|
|
"""
|
2020-04-16 14:41:32 +00:00
|
|
|
select sum(mr_item.stock_qty - mr_item.ordered_qty)
|
2014-11-04 10:02:31 +00:00
|
|
|
from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr
|
|
|
|
where mr_item.item_code=%s and mr_item.warehouse=%s
|
2020-04-17 05:22:28 +00:00
|
|
|
and mr.material_request_type = 'Material Issue'
|
2020-04-16 14:41:32 +00:00
|
|
|
and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
|
|
|
|
and mr.status!='Stopped' and mr.docstatus=1
|
|
|
|
""",
|
|
|
|
(item_code, warehouse),
|
|
|
|
)
|
2020-04-17 05:22:28 +00:00
|
|
|
outward_qty = flt(outward_qty[0][0]) if outward_qty else 0
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2020-04-17 05:22:28 +00:00
|
|
|
requested_qty = inward_qty - outward_qty
|
2020-03-26 09:19:28 +00:00
|
|
|
|
2020-04-16 14:41:32 +00:00
|
|
|
return requested_qty
|
2013-10-11 13:01:33 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-10-11 13:01:33 +00:00
|
|
|
def get_ordered_qty(item_code, warehouse):
|
2014-02-26 07:05:33 +00:00
|
|
|
ordered_qty = frappe.db.sql(
|
|
|
|
"""
|
2015-11-16 13:35:46 +00:00
|
|
|
select sum((po_item.qty - po_item.received_qty)*po_item.conversion_factor)
|
2013-10-11 13:01:33 +00:00
|
|
|
from `tabPurchase Order Item` po_item, `tabPurchase Order` po
|
2014-04-04 06:35:16 +00:00
|
|
|
where po_item.item_code=%s and po_item.warehouse=%s
|
2015-11-16 13:35:46 +00:00
|
|
|
and po_item.qty > po_item.received_qty and po_item.parent=po.name
|
2016-02-22 10:54:23 +00:00
|
|
|
and po.status not in ('Closed', 'Delivered') and po.docstatus=1
|
2015-11-16 13:35:46 +00:00
|
|
|
and po_item.delivered_by_supplier = 0""",
|
|
|
|
(item_code, warehouse),
|
|
|
|
)
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2013-10-11 13:01:33 +00:00
|
|
|
return flt(ordered_qty[0][0]) if ordered_qty else 0
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-10-11 13:01:33 +00:00
|
|
|
def get_planned_qty(item_code, warehouse):
|
2014-02-26 07:05:33 +00:00
|
|
|
planned_qty = frappe.db.sql(
|
|
|
|
"""
|
2018-03-20 07:08:43 +00:00
|
|
|
select sum(qty - produced_qty) from `tabWork Order`
|
2022-06-17 11:31:27 +00:00
|
|
|
where production_item = %s and fg_warehouse = %s and status not in ('Stopped', 'Completed', 'Closed')
|
2015-11-16 13:35:46 +00:00
|
|
|
and docstatus=1 and qty > produced_qty""",
|
|
|
|
(item_code, warehouse),
|
|
|
|
)
|
2013-10-11 13:01:33 +00:00
|
|
|
|
|
|
|
return flt(planned_qty[0][0]) if planned_qty else 0
|
2014-04-04 06:35:16 +00:00
|
|
|
|
|
|
|
|
2014-09-01 12:46:05 +00:00
|
|
|
def update_bin_qty(item_code, warehouse, qty_dict=None):
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.stock.utils import get_bin
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-10-11 13:01:33 +00:00
|
|
|
bin = get_bin(item_code, warehouse)
|
|
|
|
mismatch = False
|
2020-03-26 09:19:28 +00:00
|
|
|
for field, value in qty_dict.items():
|
|
|
|
if flt(bin.get(field)) != flt(value):
|
|
|
|
bin.set(field, flt(value))
|
2013-10-11 13:01:33 +00:00
|
|
|
mismatch = True
|
2014-04-04 06:35:16 +00:00
|
|
|
|
2022-02-11 05:59:37 +00:00
|
|
|
bin.modified = now()
|
2013-10-11 13:01:33 +00:00
|
|
|
if mismatch:
|
2018-08-08 13:02:03 +00:00
|
|
|
bin.set_projected_qty()
|
|
|
|
bin.db_update()
|
|
|
|
bin.clear_cache()
|
2014-04-04 06:35:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_stock_balance_as_per_serial_no(
|
|
|
|
item_code=None, posting_date=None, posting_time=None, fiscal_year=None
|
2022-03-28 13:22:46 +00:00
|
|
|
):
|
2014-04-04 06:35:16 +00:00
|
|
|
if not posting_date:
|
|
|
|
posting_date = nowdate()
|
|
|
|
if not posting_time:
|
|
|
|
posting_time = nowtime()
|
|
|
|
|
|
|
|
condition = " and item.name='%s'" % item_code.replace("'", "'") if item_code else ""
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-04-04 06:35:16 +00:00
|
|
|
bin = frappe.db.sql(
|
|
|
|
"""select bin.item_code, bin.warehouse, bin.actual_qty, item.stock_uom
|
|
|
|
from `tabBin` bin, tabItem item
|
2015-07-24 09:46:25 +00:00
|
|
|
where bin.item_code = item.name and item.has_serial_no = 1 %s"""
|
|
|
|
% condition
|
|
|
|
)
|
2014-04-04 06:35:16 +00:00
|
|
|
|
|
|
|
for d in bin:
|
|
|
|
serial_nos = frappe.db.sql(
|
|
|
|
"""select count(name) from `tabSerial No`
|
2015-10-22 09:41:44 +00:00
|
|
|
where item_code=%s and warehouse=%s and docstatus < 2""",
|
|
|
|
(d[0], d[1]),
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-04-04 06:35:16 +00:00
|
|
|
sle = frappe.db.sql(
|
|
|
|
"""select valuation_rate, company from `tabStock Ledger Entry`
|
2020-12-21 09:15:50 +00:00
|
|
|
where item_code = %s and warehouse = %s and is_cancelled = 0
|
2014-04-04 06:35:16 +00:00
|
|
|
order by posting_date desc limit 1""",
|
|
|
|
(d[0], d[1]),
|
|
|
|
)
|
|
|
|
|
|
|
|
sle_dict = {
|
|
|
|
"doctype": "Stock Ledger Entry",
|
|
|
|
"item_code": d[0],
|
|
|
|
"warehouse": d[1],
|
|
|
|
"transaction_date": nowdate(),
|
|
|
|
"posting_date": posting_date,
|
|
|
|
"posting_time": posting_time,
|
|
|
|
"voucher_type": "Stock Reconciliation (Manual)",
|
|
|
|
"voucher_no": "",
|
|
|
|
"voucher_detail_no": "",
|
|
|
|
"actual_qty": flt(serial_nos[0][0]) - flt(d[2]),
|
|
|
|
"stock_uom": d[3],
|
|
|
|
"incoming_rate": sle and flt(serial_nos[0][0]) > flt(d[2]) and flt(sle[0][0]) or 0,
|
|
|
|
"company": sle and cstr(sle[0][1]) or 0,
|
|
|
|
"batch_no": "",
|
|
|
|
"serial_no": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
sle_doc = frappe.get_doc(sle_dict)
|
2015-02-10 09:11:27 +00:00
|
|
|
sle_doc.flags.ignore_validate = True
|
|
|
|
sle_doc.flags.ignore_links = True
|
2014-04-04 06:35:16 +00:00
|
|
|
sle_doc.insert()
|
|
|
|
|
|
|
|
args = sle_dict.copy()
|
2020-04-30 05:08:58 +00:00
|
|
|
args.update({"sle_id": sle_doc.name})
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-12-21 09:15:50 +00:00
|
|
|
create_repost_item_valuation_entry(
|
2022-03-28 13:22:46 +00:00
|
|
|
{
|
2020-04-30 05:08:58 +00:00
|
|
|
"item_code": d[0],
|
2014-04-04 06:35:16 +00:00
|
|
|
"warehouse": d[1],
|
|
|
|
"posting_date": posting_date,
|
|
|
|
"posting_time": posting_time,
|
|
|
|
}
|
|
|
|
)
|