Skip to content

Commit

Permalink
fix: incorrect previous sle (breaking change)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure committed Aug 12, 2024
1 parent deccb00 commit 749c581
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions erpnext/stock/stock_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ def __init__(
self.distinct_item_warehouses = args.get("distinct_item_warehouses", frappe._dict())
self.affected_transactions: set[tuple[str, str]] = set()
self.reserved_stock = flt(self.args.reserved_stock)
self.is_cancelled_voucher = (
frappe.db.get_value(self.args.voucher_type, self.args.voucher_no, "docstatus") == 2 or False
)

self.data = frappe._dict()
self.initialize_previous_data(self.args)
Expand Down Expand Up @@ -587,9 +590,14 @@ def initialize_previous_data(self, args):
}
"""
self.data.setdefault(args.warehouse, frappe._dict())
warehouse_dict = self.data[args.warehouse]
previous_sle = get_previous_sle_of_current_voucher(args)
self.data.setdefault((args.warehouse, args.item_code), frappe._dict())
warehouse_dict = self.data[(args.warehouse, args.item_code)]

exclude_current_sle = False
if self.args.get("sle_id") and not self.is_cancelled_voucher:
exclude_current_sle = True

previous_sle = get_previous_sle_of_current_voucher(args, exclude_current_sle=exclude_current_sle)
warehouse_dict.previous_sle = previous_sle

for key in ("qty_after_transaction", "valuation_rate", "stock_value"):
Expand Down Expand Up @@ -629,15 +637,19 @@ def build(self):

def process_sle_against_current_timestamp(self):
sl_entries = self.get_sle_against_current_voucher()

for sle in sl_entries:
self.process_sle(sle)

def get_sle_against_current_voucher(self):
self.args["posting_datetime"] = get_combine_datetime(self.args.posting_date, self.args.posting_time)
self.args["updated_creation_time"] = add_to_date(self.args.creation, seconds=-2)

create_condition = ""
if not self.is_cancelled_voucher:
create_condition = "and creation = %(creation)s"

return frappe.db.sql(
"""
f"""
select
*, posting_datetime as "timestamp"
from
Expand All @@ -649,7 +661,7 @@ def get_sle_against_current_voucher(self):
and (
posting_datetime = %(posting_datetime)s
)
and creation >= %(updated_creation_time)s
{create_condition}
order by
creation ASC
for update
Expand All @@ -660,7 +672,7 @@ def get_sle_against_current_voucher(self):

def get_future_entries_to_fix(self):
# includes current entry!
args = self.data[self.args.warehouse].previous_sle or frappe._dict(
args = self.data[(self.args.warehouse, self.args.item_code)].previous_sle or frappe._dict(
{"item_code": self.item_code, "warehouse": self.args.warehouse}
)

Expand All @@ -673,12 +685,14 @@ def get_dependent_entries_to_fix(self, entries_to_fix, sle):

if not dependant_sle:
return entries_to_fix
elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse == self.args.warehouse:

key = (dependant_sle.warehouse, dependant_sle.item_code)
if dependant_sle.item_code == self.item_code and dependant_sle.warehouse == self.args.warehouse:
return entries_to_fix
elif dependant_sle.item_code != self.item_code:
self.update_distinct_item_warehouses(dependant_sle)
return entries_to_fix
elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse in self.data:
elif dependant_sle.item_code == self.item_code and key in self.data:
return entries_to_fix
else:
self.initialize_previous_data(dependant_sle)
Expand Down Expand Up @@ -748,7 +762,7 @@ def get_dependent_voucher_detail_nos(self, key):
return self.distinct_item_warehouses[key].dependent_voucher_detail_nos

def validate_previous_sle_qty(self, sle):
previous_sle = self.data[sle.warehouse].previous_sle
previous_sle = self.data[(sle.warehouse, sle.item_code)].previous_sle
if previous_sle and previous_sle.get("qty_after_transaction") < 0 and sle.get("actual_qty") > 0:
frappe.msgprint(
_(
Expand All @@ -767,7 +781,7 @@ def validate_previous_sle_qty(self, sle):

def process_sle(self, sle):
# previous sle data for this warehouse
self.wh_data = self.data[sle.warehouse]
self.wh_data = self.data[(sle.warehouse, sle.item_code)]

self.validate_previous_sle_qty(sle)
self.affected_transactions.add((sle.voucher_type, sle.voucher_no))
Expand Down Expand Up @@ -1506,16 +1520,18 @@ def update_bin_data(self, sle):

def update_bin(self):
# update bin for each warehouse
for warehouse, data in self.data.items():
bin_name = get_or_make_bin(self.item_code, warehouse)
for (warehouse, item_code), data in self.data.items():
bin_name = get_or_make_bin(item_code, warehouse)

updated_values = {"actual_qty": data.qty_after_transaction, "stock_value": data.stock_value}
if data.valuation_rate is not None:
updated_values["valuation_rate"] = data.valuation_rate
frappe.db.set_value("Bin", bin_name, updated_values, update_modified=True)


def get_previous_sle_of_current_voucher(args, operator="<", exclude_current_voucher=False):
def get_previous_sle_of_current_voucher(
args, operator="<", exclude_current_voucher=False, exclude_current_sle=False
):
"""get stock ledger entries filtered by specific posting datetime conditions"""

if not args.get("posting_date"):
Expand All @@ -1525,7 +1541,12 @@ def get_previous_sle_of_current_voucher(args, operator="<", exclude_current_vouc
args["posting_datetime"] = get_combine_datetime(args["posting_date"], args["posting_time"])

voucher_condition = ""
if exclude_current_voucher:
if exclude_current_sle and args.get("creation"):
creation = args.get("creation")
operator = "<="
voucher_condition = f"and creation != '{creation}'"

elif exclude_current_voucher:
voucher_no = args.get("voucher_no")
voucher_condition = f"and voucher_no != '{voucher_no}'"

Expand Down

0 comments on commit 749c581

Please sign in to comment.