* fix: same posting date and time, creation causing incorrect balance qty (#42904)
fix: same posting date and time, creation causing incorrect balance quantity
(cherry picked from commit 27364b7e6b)
# Conflicts:
# erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
# erpnext/stock/stock_ledger.py
* chore: fix conflicts
* chore: fix conflicts
---------
Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
import frappe
|
||||
from frappe.tests.utils import FrappeTestCase, change_settings
|
||||
from frappe.utils import add_days, cint, cstr, flt, nowtime, today
|
||||
from frappe.utils import add_days, cint, cstr, flt, get_datetime, getdate, nowtime, today
|
||||
from pypika import functions as fn
|
||||
|
||||
import erpnext
|
||||
@@ -2603,6 +2603,71 @@ class TestPurchaseReceipt(FrappeTestCase):
|
||||
company_doc.default_inventory_account = None
|
||||
company_doc.save()
|
||||
|
||||
def test_sles_with_same_posting_datetime_and_creation(self):
|
||||
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
|
||||
from erpnext.stock.report.stock_balance.stock_balance import execute
|
||||
|
||||
item_code = "Test Item for SLE with same posting datetime and creation"
|
||||
create_item(item_code)
|
||||
|
||||
pr = make_purchase_receipt(
|
||||
item_code=item_code,
|
||||
qty=10,
|
||||
rate=100,
|
||||
posting_date="2023-11-06",
|
||||
posting_time="00:00:00",
|
||||
)
|
||||
|
||||
sr = make_stock_entry(
|
||||
item_code=item_code,
|
||||
source=pr.items[0].warehouse,
|
||||
qty=10,
|
||||
posting_date="2023-11-07",
|
||||
posting_time="14:28:0.330404",
|
||||
)
|
||||
|
||||
sle = frappe.db.get_value(
|
||||
"Stock Ledger Entry",
|
||||
{"voucher_type": sr.doctype, "voucher_no": sr.name, "item_code": sr.items[0].item_code},
|
||||
"name",
|
||||
)
|
||||
|
||||
sle_doc = frappe.get_doc("Stock Ledger Entry", sle)
|
||||
sle_doc.db_set("creation", "2023-11-07 14:28:01.208930")
|
||||
|
||||
sle_doc.reload()
|
||||
self.assertEqual(get_datetime(sle_doc.creation), get_datetime("2023-11-07 14:28:01.208930"))
|
||||
|
||||
sr = make_stock_entry(
|
||||
item_code=item_code,
|
||||
target=pr.items[0].warehouse,
|
||||
qty=50,
|
||||
posting_date="2023-11-07",
|
||||
posting_time="14:28:0.920825",
|
||||
)
|
||||
|
||||
sle = frappe.db.get_value(
|
||||
"Stock Ledger Entry",
|
||||
{"voucher_type": sr.doctype, "voucher_no": sr.name, "item_code": sr.items[0].item_code},
|
||||
"name",
|
||||
)
|
||||
|
||||
sle_doc = frappe.get_doc("Stock Ledger Entry", sle)
|
||||
sle_doc.db_set("creation", "2023-11-07 14:28:01.044561")
|
||||
|
||||
sle_doc.reload()
|
||||
self.assertEqual(get_datetime(sle_doc.creation), get_datetime("2023-11-07 14:28:01.044561"))
|
||||
|
||||
pr.repost_future_sle_and_gle(force=True)
|
||||
|
||||
columns, data = execute(
|
||||
filters=frappe._dict(
|
||||
{"item_code": item_code, "warehouse": pr.items[0].warehouse, "company": pr.company}
|
||||
)
|
||||
)
|
||||
|
||||
self.assertEqual(data[0].get("bal_qty"), 50.0)
|
||||
|
||||
|
||||
def prepare_data_for_internal_transfer():
|
||||
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_internal_supplier
|
||||
|
||||
@@ -1172,7 +1172,7 @@ class TestStockLedgerEntry(FrappeTestCase, StockTestMixin):
|
||||
qty=5,
|
||||
posting_date="2021-01-01",
|
||||
rate=10,
|
||||
posting_time="02:00:00.1234",
|
||||
posting_time="02:00:00",
|
||||
)
|
||||
|
||||
time.sleep(3)
|
||||
@@ -1184,7 +1184,7 @@ class TestStockLedgerEntry(FrappeTestCase, StockTestMixin):
|
||||
qty=100,
|
||||
rate=10,
|
||||
posting_date="2021-01-01",
|
||||
posting_time="02:00:00",
|
||||
posting_time="02:00:00.1234",
|
||||
)
|
||||
|
||||
sle = frappe.get_all(
|
||||
|
||||
@@ -1253,7 +1253,7 @@ def get_previous_sle_of_current_voucher(args, operator="<", exclude_current_vouc
|
||||
and (
|
||||
posting_datetime {operator} %(posting_datetime)s
|
||||
)
|
||||
order by posting_datetime desc, creation desc
|
||||
order by posting_date desc, posting_time desc, creation desc
|
||||
limit 1
|
||||
for update""",
|
||||
{
|
||||
@@ -1347,7 +1347,7 @@ def get_stock_ledger_entries(
|
||||
where item_code = %(item_code)s
|
||||
and is_cancelled = 0
|
||||
{conditions}
|
||||
order by posting_datetime {order}, creation {order}
|
||||
order by posting_date {order}, posting_time {order}, creation {order}
|
||||
{limit} {for_update}""".format(
|
||||
conditions=conditions,
|
||||
limit=limit or "",
|
||||
@@ -1452,7 +1452,7 @@ def get_valuation_rate(
|
||||
AND valuation_rate >= 0
|
||||
AND is_cancelled = 0
|
||||
AND NOT (voucher_no = %s AND voucher_type = %s)
|
||||
order by posting_datetime desc, name desc limit 1""",
|
||||
order by posting_date desc, posting_time desc, name desc limit 1""",
|
||||
(item_code, warehouse, voucher_no, voucher_type),
|
||||
)
|
||||
|
||||
@@ -1701,7 +1701,8 @@ def get_future_sle_with_negative_qty(sle):
|
||||
& (SLE.is_cancelled == 0)
|
||||
& (SLE.qty_after_transaction < 0)
|
||||
)
|
||||
.orderby(SLE.posting_datetime)
|
||||
.orderby(SLE.posting_date)
|
||||
.orderby(SLE.posting_time)
|
||||
.limit(1)
|
||||
)
|
||||
|
||||
@@ -1717,14 +1718,14 @@ def get_future_sle_with_negative_batch_qty(args):
|
||||
with batch_ledger as (
|
||||
select
|
||||
posting_date, posting_time, posting_datetime, voucher_type, voucher_no,
|
||||
sum(actual_qty) over (order by posting_datetime, creation) as cumulative_total
|
||||
sum(actual_qty) over (order by posting_date, posting_time, creation) as cumulative_total
|
||||
from `tabStock Ledger Entry`
|
||||
where
|
||||
item_code = %(item_code)s
|
||||
and warehouse = %(warehouse)s
|
||||
and batch_no=%(batch_no)s
|
||||
and is_cancelled = 0
|
||||
order by posting_datetime, creation
|
||||
order by posting_date, posting_time, creation
|
||||
)
|
||||
select * from batch_ledger
|
||||
where
|
||||
|
||||
Reference in New Issue
Block a user