Skip to content

Commit

Permalink
Merge pull request #33955 from frappe/mergify/bp/version-14-hotfix/pr…
Browse files Browse the repository at this point in the history
…-33936

fix: negative stock error (backport #33936)
  • Loading branch information
rohitwaghchaure authored Feb 5, 2023
2 parents 6f8ff6f + 388cc31 commit 18a1707
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
42 changes: 42 additions & 0 deletions erpnext/stock/doctype/stock_entry/test_stock_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,48 @@ def test_batch_expiry(self):

self.assertRaises(BatchExpiredError, se.save)

def test_negative_stock_reco(self):
from erpnext.controllers.stock_controller import BatchExpiredError
from erpnext.stock.doctype.batch.test_batch import make_new_batch

frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 0)

item_code = "Test Negative Item - 001"
item_doc = create_item(item_code=item_code, is_stock_item=1, valuation_rate=10)

make_stock_entry(
item_code=item_code,
posting_date=add_days(today(), -3),
posting_time="00:00:00",
purpose="Material Receipt",
qty=10,
to_warehouse="_Test Warehouse - _TC",
do_not_save=True,
)

make_stock_entry(
item_code=item_code,
posting_date=today(),
posting_time="00:00:00",
purpose="Material Receipt",
qty=8,
from_warehouse="_Test Warehouse - _TC",
do_not_save=True,
)

sr_doc = create_stock_reconciliation(
purpose="Stock Reconciliation",
posting_date=add_days(today(), -3),
posting_time="00:00:00",
item_code=item_code,
warehouse="_Test Warehouse - _TC",
valuation_rate=10,
qty=7,
do_not_submit=True,
)

self.assertRaises(frappe.ValidationError, sr_doc.submit)


def make_serialized_item(**args):
args = frappe._dict(args)
Expand Down
8 changes: 4 additions & 4 deletions erpnext/stock/stock_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ def update_bin(self):
frappe.db.set_value("Bin", bin_name, updated_values, update_modified=True)


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

args["time_format"] = "%H:%i:%s"
Expand All @@ -1076,13 +1076,13 @@ def get_previous_sle_of_current_voucher(args, exclude_current_voucher=False):
posting_date < %(posting_date)s or
(
posting_date = %(posting_date)s and
time_format(posting_time, %(time_format)s) < time_format(%(posting_time)s, %(time_format)s)
time_format(posting_time, %(time_format)s) {operator} time_format(%(posting_time)s, %(time_format)s)
)
)
order by timestamp(posting_date, posting_time) desc, creation desc
limit 1
for update""".format(
voucher_condition=voucher_condition
operator=operator, voucher_condition=voucher_condition
),
args,
as_dict=1,
Expand Down Expand Up @@ -1375,7 +1375,7 @@ def get_stock_reco_qty_shift(args):
stock_reco_qty_shift = flt(args.actual_qty)
else:
# reco is being submitted
last_balance = get_previous_sle_of_current_voucher(args, exclude_current_voucher=True).get(
last_balance = get_previous_sle_of_current_voucher(args, "<=", exclude_current_voucher=True).get(
"qty_after_transaction"
)

Expand Down

0 comments on commit 18a1707

Please sign in to comment.