brotherton-erpnext/erpnext/stock/stock_balance.py

298 lines
8.8 KiB
Python
Raw Normal View History

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
2014-02-14 10:17:51 +00:00
import frappe
from frappe.utils import cstr, flt, now, nowdate, nowtime
Repost item valuation (#24031) * feat: Reposting logic for future finished/transferred item * feat: added fields to identify needs to recalculate rate while reposting * refactor: Set rate for outgoing and finished items * refactor: Arranged fields in Stock Entry item table and added fields to identify finished and scrap item * refactor: Arranged fields in Stock Entry item table and added fields to identify finished and scrap item * refactor: Get outgoing rate for purchase return * refactor: Get incoming rate for sales return * test: Added tests for reposting valuation of transferred/finished/returned items * feat: added incoming rate field in DN, SI and Packed Item table * feat: get incoming rate for returned item * fix: no error while getting valuation rate in stock entry * fix: update stock ledger for DN and SI * feat: update item valuation rate in PR and PI based on supplied items cost * feat: SLE reposting logic for sales return and subcontracted item with test cases * feat: update qty in future sle * feat: repost future sle and gle via Repost Item Valuation * fix: Skip unwanted function calling while reposting * fix: repost sle for specific item and warehouse * test: Modified tests for backdated stock reco * fix: ignore cancelled sle in few methods * feat: role allowed to do backdated entry * feat: Show reposting status on stock valuation related reports * fix: minor fixes * fix: fixed sider issues * fix: serial no fix related to immutable ledger * fix: Test cases fixes related to perpetual inventory * fix: Test cases fixed * fix: Fixed reposting on cancel and test cases * feat: Restart reposting item valuation * refactor: Code cleanup using small functions and test case fixes * fix: minor fixes * fix: Raise on error while reposting item valuation * fix: minor fix * fix: Tests fixed * fix: skip some validation ig gle made from reposting * fix: test fixes * fix: debugging stock and account validation * fix: debugging stock and account validation * fix: debugging travis for stock and account sync validation * fix: debugging travis * fix: debugging travis * fix: debugging travis
2020-12-21 09:15:50 +00:00
from erpnext.controllers.stock_controller import create_repost_item_valuation_entry
2015-10-12 11:16:29 +00:00
def repost(only_actual=False, allow_negative_stock=False, allow_zero_rate=False, only_bin=False):
"""
Repost everything!
"""
2014-02-26 07:05:33 +00:00
frappe.db.auto_commit_on_many_writes = 1
2013-11-20 07:44:12 +00:00
if allow_negative_stock:
2022-03-28 13:22:46 +00:00
existing_allow_negative_stock = frappe.db.get_value(
"Stock Settings", None, "allow_negative_stock"
)
frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1)
2022-03-28 13:22:46 +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
2022-03-28 13:22:46 +00:00
"""
)
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:
frappe.db.rollback()
2013-11-20 07:44:12 +00:00
if allow_negative_stock:
frappe.db.set_single_value(
"Stock Settings", "allow_negative_stock", existing_allow_negative_stock
2022-03-28 13:22:46 +00:00
)
2014-02-26 07:05:33 +00:00
frappe.db.auto_commit_on_many_writes = 0
2022-03-28 13:22:46 +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:
repost_actual_qty(item_code, warehouse, allow_zero_rate, allow_negative_stock)
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 = {
"reserved_qty": get_reserved_qty(item_code, warehouse),
"indented_qty": get_indented_qty(item_code, warehouse),
"ordered_qty": get_ordered_qty(item_code, warehouse),
2022-03-28 13:22:46 +00:00
"planned_qty": get_planned_qty(item_code, warehouse),
2015-10-12 11:16:29 +00:00
}
if only_bin:
2022-03-28 13:22:46 +00:00
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)
2022-03-28 13:22:46 +00:00
feat: Immutable ledger (#18740) * fix: Reverse GL entry on cancellation of document * fix: Removed set posting time field for multiple docs * fix: Stop future reposting and reverse entry for purchase receipt and delivery note * fix: Change is_cancelled field from select to check * Revert "fix: Removed set posting time field for multiple docs" This reverts commit 81fb808db7da69ab1e58ba226c0cd0b77e5b90c8. * fix: Multiple fixes in GL Entry * fix: Remove future reporting from doctypes * fix: Canceled entry filters in Stock Ledger and General Ledger Report * fix: Remove print statement * fix: Validation for back dated entries * fix: Codacy fixes * fix: Add ignore links to multiple doctypes * fix: Codacy Fixes * fix: Ignore GL Entry and Stock Ledger entry while cancel * fix: Test case fixes * fix: Patch * fix: Codacy * fix: Budget Test Cases * fix: Patch * fix: Patch * fix: Multiple test cases * fix: changes in make_reverse_entry function * fix: Update patch * fix: Test Cases * fix: Test Case fixes * fix: Move patch upward in patches.txt * fix: Budget Test Cases * fix: Test Case and codacy * fix: Patch * fix: Minor label and UX fixes * fix: Move freezing date check * fix: Test Cases * fix: Test cases * fix: Test Cases * fix: Test Case * fix: Remove update_gl_entries_after function * fix: Remove update_gl_entries_after function * fix: Test Cases * fix: Fiscal Year wise backdated entry * fix: Update entries only for current SLE * fix: Remove is_cancelled * fix: Test Cases * fix: Test cases * fix: Test Cases * fix: Uncomment account and stock balance sync logic * fix: Stock balance and Account balance out of sync fixes * fix: Test Cases * fix: Test cases for POS, Stock Reco and Purchase Receipt * fix: Stock Reco tests * fix: Test stock reco precision * fix: Test stock reco for fifo precision * fix: Test stock reco for fifo precision * fix: Stock Entry test case Co-authored-by: Nabin Hait <nabinhait@gmail.com>
2020-04-30 05:08:58 +00:00
def repost_actual_qty(item_code, warehouse, allow_zero_rate=False, allow_negative_stock=False):
2022-03-28 13:22:46 +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,
}
)
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):
2022-03-28 13:22:46 +00:00
balance_qty = frappe.db.sql(
"""select qty_after_transaction from `tabStock Ledger Entry`
Repost item valuation (#24031) * feat: Reposting logic for future finished/transferred item * feat: added fields to identify needs to recalculate rate while reposting * refactor: Set rate for outgoing and finished items * refactor: Arranged fields in Stock Entry item table and added fields to identify finished and scrap item * refactor: Arranged fields in Stock Entry item table and added fields to identify finished and scrap item * refactor: Get outgoing rate for purchase return * refactor: Get incoming rate for sales return * test: Added tests for reposting valuation of transferred/finished/returned items * feat: added incoming rate field in DN, SI and Packed Item table * feat: get incoming rate for returned item * fix: no error while getting valuation rate in stock entry * fix: update stock ledger for DN and SI * feat: update item valuation rate in PR and PI based on supplied items cost * feat: SLE reposting logic for sales return and subcontracted item with test cases * feat: update qty in future sle * feat: repost future sle and gle via Repost Item Valuation * fix: Skip unwanted function calling while reposting * fix: repost sle for specific item and warehouse * test: Modified tests for backdated stock reco * fix: ignore cancelled sle in few methods * feat: role allowed to do backdated entry * feat: Show reposting status on stock valuation related reports * fix: minor fixes * fix: fixed sider issues * fix: serial no fix related to immutable ledger * fix: Test cases fixes related to perpetual inventory * fix: Test cases fixed * fix: Fixed reposting on cancel and test cases * feat: Restart reposting item valuation * refactor: Code cleanup using small functions and test case fixes * fix: minor fixes * fix: Raise on error while reposting item valuation * fix: minor fix * fix: Tests fixed * fix: skip some validation ig gle made from reposting * fix: test fixes * fix: debugging stock and account validation * fix: debugging stock and account validation * fix: debugging travis for stock and account sync validation * fix: debugging travis * fix: debugging travis * fix: debugging travis
2020-12-21 09:15:50 +00:00
where item_code=%s and warehouse=%s and is_cancelled=0
order by posting_date desc, posting_time desc, creation desc
2022-03-28 13:22:46 +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
2022-03-28 13:22:46 +00:00
def get_reserved_qty(item_code, warehouse):
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(
f"""
2023-03-25 04:24:53 +00:00
select
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,
(
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,
(
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,
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
)
return flt(reserved_qty[0][0]) if reserved_qty else 0
2022-03-28 13:22:46 +00:00
def get_indented_qty(item_code, warehouse):
# Ordered Qty is always maintained in stock UOM
2022-03-28 13:22:46 +00:00
inward_qty = frappe.db.sql(
"""
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
and mr.material_request_type in ('Purchase', 'Manufacture', 'Customer Provided', 'Material Transfer')
and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
and mr.status!='Stopped' and mr.docstatus=1
2022-03-28 13:22:46 +00:00
""",
(item_code, warehouse),
)
inward_qty = flt(inward_qty[0][0]) if inward_qty else 0
2022-03-28 13:22:46 +00:00
outward_qty = frappe.db.sql(
"""
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
and mr.material_request_type = 'Material Issue'
and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name
and mr.status!='Stopped' and mr.docstatus=1
2022-03-28 13:22:46 +00:00
""",
(item_code, warehouse),
)
outward_qty = flt(outward_qty[0][0]) if outward_qty else 0
requested_qty = inward_qty - outward_qty
2020-03-26 09:19:28 +00:00
return requested_qty
2022-03-28 13:22:46 +00:00
def get_ordered_qty(item_code, warehouse):
2022-03-28 13:22:46 +00:00
ordered_qty = frappe.db.sql(
"""
select sum((po_item.qty - po_item.received_qty)*po_item.conversion_factor)
from `tabPurchase Order Item` po_item, `tabPurchase Order` po
where po_item.item_code=%s and po_item.warehouse=%s
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
2022-03-28 13:22:46 +00:00
and po_item.delivered_by_supplier = 0""",
(item_code, warehouse),
)
return flt(ordered_qty[0][0]) if ordered_qty else 0
2022-03-28 13:22:46 +00:00
def get_planned_qty(item_code, warehouse):
2022-03-28 13:22:46 +00:00
planned_qty = frappe.db.sql(
"""
select sum(qty - produced_qty) from `tabWork Order`
where production_item = %s and fg_warehouse = %s and status not in ('Stopped', 'Completed', 'Closed')
2022-03-28 13:22:46 +00:00
and docstatus=1 and qty > produced_qty""",
(item_code, warehouse),
)
return flt(planned_qty[0][0]) if planned_qty else 0
2014-09-01 12:46:05 +00:00
def update_bin_qty(item_code, warehouse, qty_dict=None):
from erpnext.stock.utils import get_bin
2022-03-28 13:22:46 +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))
mismatch = True
bin.modified = now()
if mismatch:
2018-08-08 13:02:03 +00:00
bin.set_projected_qty()
bin.db_update()
bin.clear_cache()
2022-03-28 13:22:46 +00:00
def set_stock_balance_as_per_serial_no(
item_code=None, posting_date=None, posting_time=None, fiscal_year=None
):
if not posting_date:
posting_date = nowdate()
if not posting_time:
posting_time = nowtime()
2022-03-28 13:22:46 +00:00
condition = " and item.name='%s'" % item_code.replace("'", "'") if item_code else ""
bin = frappe.db.sql(
"""select bin.item_code, bin.warehouse, bin.actual_qty, item.stock_uom
from `tabBin` bin, tabItem item
2022-03-28 13:22:46 +00:00
where bin.item_code = item.name and item.has_serial_no = 1 %s"""
% condition
)
for d in bin:
2022-03-28 13:22:46 +00:00
serial_nos = frappe.db.sql(
"""select count(name) from `tabSerial No`
where item_code=%s and warehouse=%s and docstatus < 2""",
(d[0], d[1]),
)
sle = frappe.db.sql(
"""select valuation_rate, company from `tabStock Ledger Entry`
Repost item valuation (#24031) * feat: Reposting logic for future finished/transferred item * feat: added fields to identify needs to recalculate rate while reposting * refactor: Set rate for outgoing and finished items * refactor: Arranged fields in Stock Entry item table and added fields to identify finished and scrap item * refactor: Arranged fields in Stock Entry item table and added fields to identify finished and scrap item * refactor: Get outgoing rate for purchase return * refactor: Get incoming rate for sales return * test: Added tests for reposting valuation of transferred/finished/returned items * feat: added incoming rate field in DN, SI and Packed Item table * feat: get incoming rate for returned item * fix: no error while getting valuation rate in stock entry * fix: update stock ledger for DN and SI * feat: update item valuation rate in PR and PI based on supplied items cost * feat: SLE reposting logic for sales return and subcontracted item with test cases * feat: update qty in future sle * feat: repost future sle and gle via Repost Item Valuation * fix: Skip unwanted function calling while reposting * fix: repost sle for specific item and warehouse * test: Modified tests for backdated stock reco * fix: ignore cancelled sle in few methods * feat: role allowed to do backdated entry * feat: Show reposting status on stock valuation related reports * fix: minor fixes * fix: fixed sider issues * fix: serial no fix related to immutable ledger * fix: Test cases fixes related to perpetual inventory * fix: Test cases fixed * fix: Fixed reposting on cancel and test cases * feat: Restart reposting item valuation * refactor: Code cleanup using small functions and test case fixes * fix: minor fixes * fix: Raise on error while reposting item valuation * fix: minor fix * fix: Tests fixed * fix: skip some validation ig gle made from reposting * fix: test fixes * fix: debugging stock and account validation * fix: debugging stock and account validation * fix: debugging travis for stock and account sync validation * fix: debugging travis * fix: debugging travis * fix: debugging travis
2020-12-21 09:15:50 +00:00
where item_code = %s and warehouse = %s and is_cancelled = 0
2022-03-28 13:22:46 +00:00
order by posting_date desc limit 1""",
(d[0], d[1]),
)
sle_dict = {
2022-03-28 13:22:46 +00:00
"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)
sle_doc.flags.ignore_validate = True
sle_doc.flags.ignore_links = True
sle_doc.insert()
args = sle_dict.copy()
2022-03-28 13:22:46 +00:00
args.update({"sle_id": sle_doc.name})
create_repost_item_valuation_entry(
{
"item_code": d[0],
"warehouse": d[1],
"posting_date": posting_date,
"posting_time": posting_time,
}
)