Skip to content

Commit

Permalink
perf: data import for stock entries (#42711)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure authored Aug 19, 2024
1 parent d32fdaa commit 1511280
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
70 changes: 69 additions & 1 deletion erpnext/stock/doctype/stock_entry/test_stock_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from frappe.permissions import add_user_permission, remove_user_permission
from frappe.tests.utils import FrappeTestCase, change_settings
from frappe.utils import add_days, flt, nowtime, today
from frappe.utils import add_days, cstr, flt, get_time, getdate, nowtime, today

from erpnext.accounts.doctype.account.test_account import get_inventory_account
from erpnext.controllers.accounts_controller import InvalidQtyError
Expand Down Expand Up @@ -1789,6 +1789,74 @@ def test_serial_batch_bundle_type_of_transaction(self):
frappe.db.set_value("Serial and Batch Bundle", sbb, "type_of_transaction", "Inward")
self.assertRaises(frappe.ValidationError, se.submit)

def test_stock_entry_for_same_posting_date_and_time(self):
warehouse = "_Test Warehouse - _TC"
item_code = "Test Stock Entry For Same Posting Datetime 1"
make_item(item_code, {"is_stock_item": 1})
posting_date = nowdate()
posting_time = nowtime()

for index in range(25):
se = make_stock_entry(
item_code=item_code,
qty=1,
to_warehouse=warehouse,
posting_date=posting_date,
posting_time=posting_time,
do_not_submit=True,
purpose="Material Receipt",
basic_rate=100,
)

se.append(
"items",
{
"item_code": item_code,
"item_name": se.items[0].item_name,
"description": se.items[0].description,
"t_warehouse": se.items[0].t_warehouse,
"basic_rate": 100,
"qty": 1,
"stock_qty": 1,
"conversion_factor": 1,
"expense_account": se.items[0].expense_account,
"cost_center": se.items[0].cost_center,
"uom": se.items[0].uom,
"stock_uom": se.items[0].stock_uom,
},
)

se.remarks = f"The current number is {cstr(index)}"

se.submit()

sles = frappe.get_all(
"Stock Ledger Entry",
fields=[
"posting_date",
"posting_time",
"actual_qty",
"qty_after_transaction",
"incoming_rate",
"stock_value_difference",
"stock_value",
],
filters={"item_code": item_code, "warehouse": warehouse},
order_by="creation",
)

self.assertEqual(len(sles), 50)
i = 0
for sle in sles:
i += 1
self.assertEqual(getdate(sle.posting_date), getdate(posting_date))
self.assertEqual(get_time(sle.posting_time), get_time(posting_time))
self.assertEqual(sle.actual_qty, 1)
self.assertEqual(sle.qty_after_transaction, i)
self.assertEqual(sle.incoming_rate, 100)
self.assertEqual(sle.stock_value_difference, 100)
self.assertEqual(sle.stock_value, 100 * i)


def make_serialized_item(**args):
args = frappe._dict(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,8 @@ def _day(days):
self.assertEqual(50, _get_stock_credit(final_consumption))

def test_tie_breaking(self):
from erpnext.stock.doctype.repost_item_valuation.repost_item_valuation import repost_entries

frappe.flags.dont_execute_stock_reposts = True
self.addCleanup(frappe.flags.pop, "dont_execute_stock_reposts")

Expand Down Expand Up @@ -1085,6 +1087,7 @@ def ordered_qty_after_transaction():
self.assertEqual([10, 11], ordered_qty_after_transaction())

first.cancel()
repost_entries()
self.assertEqual([1], ordered_qty_after_transaction())

backdated = make_stock_entry(
Expand Down
6 changes: 6 additions & 0 deletions erpnext/stock/stock_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ def get_sle_against_current_voucher(self):
and (
posting_datetime = %(posting_datetime)s
)
and creation = %(creation)s
order by
creation ASC
for update
Expand Down Expand Up @@ -1527,6 +1528,11 @@ def get_previous_sle_of_current_voucher(args, operator="<", exclude_current_vouc
voucher_no = args.get("voucher_no")
voucher_condition = f"and voucher_no != '{voucher_no}'"

elif args.get("creation"):
creation = args.get("creation")
operator = "<="
voucher_condition = f"and creation < '{creation}'"

sle = frappe.db.sql(
f"""
select *, posting_datetime as "timestamp"
Expand Down

0 comments on commit 1511280

Please sign in to comment.