fix: sort before picking next stock reco
(cherry picked from commit 7e2fbc050a)
This commit is contained in:
committed by
Ankush Menat
parent
0db3013c9b
commit
e27fb58130
@@ -31,6 +31,7 @@ class TestStockReconciliation(FrappeTestCase):
|
|||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
frappe.local.future_sle = {}
|
frappe.local.future_sle = {}
|
||||||
|
frappe.flags.pop("dont_execute_stock_reposts", None)
|
||||||
|
|
||||||
def test_reco_for_fifo(self):
|
def test_reco_for_fifo(self):
|
||||||
self._test_reco_sle_gle("FIFO")
|
self._test_reco_sle_gle("FIFO")
|
||||||
@@ -383,6 +384,7 @@ class TestStockReconciliation(FrappeTestCase):
|
|||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
Var | Doc | Qty | Balance
|
Var | Doc | Qty | Balance
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
|
PR5 | PR | 10 | 10 (posting date: today-4) [backdated]
|
||||||
SR5 | Reco | 0 | 8 (posting date: today-4) [backdated]
|
SR5 | Reco | 0 | 8 (posting date: today-4) [backdated]
|
||||||
PR1 | PR | 10 | 18 (posting date: today-3)
|
PR1 | PR | 10 | 18 (posting date: today-3)
|
||||||
PR2 | PR | 1 | 19 (posting date: today-2)
|
PR2 | PR | 1 | 19 (posting date: today-2)
|
||||||
@@ -392,6 +394,14 @@ class TestStockReconciliation(FrappeTestCase):
|
|||||||
item_code = make_item().name
|
item_code = make_item().name
|
||||||
warehouse = "_Test Warehouse - _TC"
|
warehouse = "_Test Warehouse - _TC"
|
||||||
|
|
||||||
|
frappe.flags.dont_execute_stock_reposts = True
|
||||||
|
|
||||||
|
def assertBalance(doc, qty_after_transaction):
|
||||||
|
sle_balance = frappe.db.get_value(
|
||||||
|
"Stock Ledger Entry", {"voucher_no": doc.name, "is_cancelled": 0}, "qty_after_transaction"
|
||||||
|
)
|
||||||
|
self.assertEqual(sle_balance, qty_after_transaction)
|
||||||
|
|
||||||
pr1 = make_purchase_receipt(
|
pr1 = make_purchase_receipt(
|
||||||
item_code=item_code, warehouse=warehouse, qty=10, rate=100, posting_date=add_days(nowdate(), -3)
|
item_code=item_code, warehouse=warehouse, qty=10, rate=100, posting_date=add_days(nowdate(), -3)
|
||||||
)
|
)
|
||||||
@@ -401,62 +411,37 @@ class TestStockReconciliation(FrappeTestCase):
|
|||||||
pr3 = make_purchase_receipt(
|
pr3 = make_purchase_receipt(
|
||||||
item_code=item_code, warehouse=warehouse, qty=1, rate=100, posting_date=nowdate()
|
item_code=item_code, warehouse=warehouse, qty=1, rate=100, posting_date=nowdate()
|
||||||
)
|
)
|
||||||
|
assertBalance(pr1, 10)
|
||||||
pr1_balance = frappe.db.get_value(
|
assertBalance(pr3, 12)
|
||||||
"Stock Ledger Entry", {"voucher_no": pr1.name, "is_cancelled": 0}, "qty_after_transaction"
|
|
||||||
)
|
|
||||||
pr3_balance = frappe.db.get_value(
|
|
||||||
"Stock Ledger Entry", {"voucher_no": pr3.name, "is_cancelled": 0}, "qty_after_transaction"
|
|
||||||
)
|
|
||||||
self.assertEqual(pr1_balance, 10)
|
|
||||||
self.assertEqual(pr3_balance, 12)
|
|
||||||
|
|
||||||
# post backdated stock reco in between
|
# post backdated stock reco in between
|
||||||
sr4 = create_stock_reconciliation(
|
sr4 = create_stock_reconciliation(
|
||||||
item_code=item_code, warehouse=warehouse, qty=6, rate=100, posting_date=add_days(nowdate(), -1)
|
item_code=item_code, warehouse=warehouse, qty=6, rate=100, posting_date=add_days(nowdate(), -1)
|
||||||
)
|
)
|
||||||
pr3_balance = frappe.db.get_value(
|
assertBalance(pr3, 7)
|
||||||
"Stock Ledger Entry", {"voucher_no": pr3.name, "is_cancelled": 0}, "qty_after_transaction"
|
|
||||||
)
|
|
||||||
self.assertEqual(pr3_balance, 7)
|
|
||||||
|
|
||||||
# post backdated stock reco at the start
|
# post backdated stock reco at the start
|
||||||
sr5 = create_stock_reconciliation(
|
sr5 = create_stock_reconciliation(
|
||||||
item_code=item_code, warehouse=warehouse, qty=8, rate=100, posting_date=add_days(nowdate(), -4)
|
item_code=item_code, warehouse=warehouse, qty=8, rate=100, posting_date=add_days(nowdate(), -4)
|
||||||
)
|
)
|
||||||
pr1_balance = frappe.db.get_value(
|
assertBalance(pr1, 18)
|
||||||
"Stock Ledger Entry", {"voucher_no": pr1.name, "is_cancelled": 0}, "qty_after_transaction"
|
assertBalance(pr2, 19)
|
||||||
|
assertBalance(sr4, 6) # check if future stock reco is unaffected
|
||||||
|
|
||||||
|
# Make a backdated receipt and check only entries till first SR are affected
|
||||||
|
pr5 = make_purchase_receipt(
|
||||||
|
item_code=item_code, warehouse=warehouse, qty=10, rate=100, posting_date=add_days(nowdate(), -5)
|
||||||
)
|
)
|
||||||
pr2_balance = frappe.db.get_value(
|
assertBalance(pr5, 10)
|
||||||
"Stock Ledger Entry", {"voucher_no": pr2.name, "is_cancelled": 0}, "qty_after_transaction"
|
# check if future stock reco is unaffected
|
||||||
)
|
assertBalance(sr4, 6)
|
||||||
sr4_balance = frappe.db.get_value(
|
assertBalance(sr5, 8)
|
||||||
"Stock Ledger Entry", {"voucher_no": sr4.name, "is_cancelled": 0}, "qty_after_transaction"
|
|
||||||
)
|
|
||||||
self.assertEqual(pr1_balance, 18)
|
|
||||||
self.assertEqual(pr2_balance, 19)
|
|
||||||
self.assertEqual(sr4_balance, 6) # check if future stock reco is unaffected
|
|
||||||
|
|
||||||
# cancel backdated stock reco and check future impact
|
# cancel backdated stock reco and check future impact
|
||||||
sr5.cancel()
|
sr5.cancel()
|
||||||
pr1_balance = frappe.db.get_value(
|
assertBalance(pr1, 10)
|
||||||
"Stock Ledger Entry", {"voucher_no": pr1.name, "is_cancelled": 0}, "qty_after_transaction"
|
assertBalance(pr2, 11)
|
||||||
)
|
assertBalance(sr4, 6) # check if future stock reco is unaffected
|
||||||
pr2_balance = frappe.db.get_value(
|
|
||||||
"Stock Ledger Entry", {"voucher_no": pr2.name, "is_cancelled": 0}, "qty_after_transaction"
|
|
||||||
)
|
|
||||||
sr4_balance = frappe.db.get_value(
|
|
||||||
"Stock Ledger Entry", {"voucher_no": sr4.name, "is_cancelled": 0}, "qty_after_transaction"
|
|
||||||
)
|
|
||||||
self.assertEqual(pr1_balance, 10)
|
|
||||||
self.assertEqual(pr2_balance, 11)
|
|
||||||
self.assertEqual(sr4_balance, 6) # check if future stock reco is unaffected
|
|
||||||
|
|
||||||
# teardown
|
|
||||||
sr4.cancel()
|
|
||||||
pr3.cancel()
|
|
||||||
pr2.cancel()
|
|
||||||
pr1.cancel()
|
|
||||||
|
|
||||||
@change_settings("Stock Settings", {"allow_negative_stock": 0})
|
@change_settings("Stock Settings", {"allow_negative_stock": 0})
|
||||||
def test_backdated_stock_reco_future_negative_stock(self):
|
def test_backdated_stock_reco_future_negative_stock(self):
|
||||||
@@ -562,7 +547,6 @@ class TestStockReconciliation(FrappeTestCase):
|
|||||||
|
|
||||||
# repost will make this test useless, qty should update in realtime without reposts
|
# repost will make this test useless, qty should update in realtime without reposts
|
||||||
frappe.flags.dont_execute_stock_reposts = True
|
frappe.flags.dont_execute_stock_reposts = True
|
||||||
self.addCleanup(frappe.flags.pop, "dont_execute_stock_reposts")
|
|
||||||
|
|
||||||
item_code = make_item().name
|
item_code = make_item().name
|
||||||
warehouse = "_Test Warehouse - _TC"
|
warehouse = "_Test Warehouse - _TC"
|
||||||
|
|||||||
@@ -1255,7 +1255,7 @@ def update_qty_in_future_sle(args, allow_negative_stock=False):
|
|||||||
datetime_limit_condition = get_datetime_limit_condition(detail)
|
datetime_limit_condition = get_datetime_limit_condition(detail)
|
||||||
|
|
||||||
frappe.db.sql(
|
frappe.db.sql(
|
||||||
"""
|
f"""
|
||||||
update `tabStock Ledger Entry`
|
update `tabStock Ledger Entry`
|
||||||
set qty_after_transaction = qty_after_transaction + {qty_shift}
|
set qty_after_transaction = qty_after_transaction + {qty_shift}
|
||||||
where
|
where
|
||||||
@@ -1266,9 +1266,7 @@ def update_qty_in_future_sle(args, allow_negative_stock=False):
|
|||||||
and timestamp(posting_date, time_format(posting_time, %(time_format)s))
|
and timestamp(posting_date, time_format(posting_time, %(time_format)s))
|
||||||
> timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s))
|
> timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s))
|
||||||
{datetime_limit_condition}
|
{datetime_limit_condition}
|
||||||
""".format(
|
""",
|
||||||
qty_shift=qty_shift, datetime_limit_condition=datetime_limit_condition
|
|
||||||
),
|
|
||||||
args,
|
args,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -1319,6 +1317,7 @@ def get_next_stock_reco(args):
|
|||||||
and creation > %(creation)s
|
and creation > %(creation)s
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
order by timestamp(posting_date, posting_time) asc, creation asc
|
||||||
limit 1
|
limit 1
|
||||||
""",
|
""",
|
||||||
args,
|
args,
|
||||||
|
|||||||
Reference in New Issue
Block a user