Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: same posting date and time causing incorrect valuation rate #42351

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions erpnext/stock/doctype/delivery_note/test_delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,93 @@ def test_sales_return_serial_no_for_serial_item_in_dn(self):
returned_serial_nos = get_serial_nos_from_bundle(dn_return.items[0].serial_and_batch_bundle)
self.assertEqual(serial_nos, returned_serial_nos)

def test_same_posting_date_and_posting_time(self):
item_code = make_item(
"Test Same Posting Datetime Item",
properties={
"has_batch_no": 1,
"create_new_batch": 1,
"batch_number_series": "SS-ART11-TESTBATCH.#####",
"is_stock_item": 1,
},
).name

se = make_stock_entry(
item_code=item_code,
target="_Test Warehouse - _TC",
qty=100,
basic_rate=50,
posting_date=add_days(nowdate(), -1),
)

batch_no = get_batch_from_bundle(se.items[0].serial_and_batch_bundle)

posting_date = today()
posting_time = nowtime()
dn1 = create_delivery_note(
posting_date=posting_date,
posting_time=posting_time,
item_code=item_code,
rate=300,
qty=25,
batch_no=batch_no,
use_serial_batch_fields=1,
)

dn2 = create_delivery_note(
posting_date=posting_date,
posting_time=posting_time,
item_code=item_code,
rate=300,
qty=25,
batch_no=batch_no,
use_serial_batch_fields=1,
)

dn3 = create_delivery_note(
posting_date=posting_date,
posting_time=posting_time,
item_code=item_code,
rate=300,
qty=25,
batch_no=batch_no,
use_serial_batch_fields=1,
)

dn4 = create_delivery_note(
posting_date=posting_date,
posting_time=posting_time,
item_code=item_code,
rate=300,
qty=25,
batch_no=batch_no,
use_serial_batch_fields=1,
)

for dn in [dn1, dn2, dn3, dn4]:
sles = frappe.get_all(
"Stock Ledger Entry",
fields=["stock_value_difference", "actual_qty"],
filters={"is_cancelled": 0, "voucher_no": dn.name, "docstatus": 1},
)

for sle in sles:
self.assertEqual(sle.actual_qty, 25.0 * -1)
self.assertEqual(sle.stock_value_difference, 25.0 * 50 * -1)

dn5 = create_delivery_note(
posting_date=posting_date,
posting_time=posting_time,
item_code=item_code,
rate=300,
qty=25,
batch_no=batch_no,
use_serial_batch_fields=1,
do_not_submit=True,
)

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


def create_delivery_note(**args):
dn = frappe.new_doc("Delivery Note")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def get_sle_for_outward_transaction(self):
"batch_nos": {row.batch_no: row for row in self.entries if row.batch_no},
"voucher_type": self.voucher_type,
"voucher_detail_no": self.voucher_detail_no,
"creation": self.creation,
}
)

Expand Down
12 changes: 9 additions & 3 deletions erpnext/stock/serial_batch_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,15 @@ def get_batch_no_ledgers(self) -> list[dict]:

timestamp_condition = ""
if self.sle.posting_date and self.sle.posting_time:
timestamp_condition = CombineDatetime(
parent.posting_date, parent.posting_time
) <= CombineDatetime(self.sle.posting_date, self.sle.posting_time)
timestamp_condition = CombineDatetime(parent.posting_date, parent.posting_time) < CombineDatetime(
self.sle.posting_date, self.sle.posting_time
)

if self.sle.creation:
timestamp_condition |= (
CombineDatetime(parent.posting_date, parent.posting_time)
== CombineDatetime(self.sle.posting_date, self.sle.posting_time)
) & (parent.creation < self.sle.creation)

query = (
frappe.qb.from_(parent)
Expand Down
Loading