Skip to content

Commit

Permalink
fix: do not validate batch qty for LCV
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure committed Apr 12, 2024
1 parent 140fdd4 commit 6647815
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 0 deletions.
201 changes: 201 additions & 0 deletions erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,207 @@ def test_landed_cost_voucher_with_serial_batch_for_legacy_pr(self):
frappe.db.get_value("Serial and Batch Bundle", row.serial_and_batch_bundle, "avg_rate"),
)

def test_do_not_validate_landed_cost_voucher_with_serial_batch_for_legacy_pr(self):
from erpnext.stock.doctype.item.test_item import make_item
from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import get_auto_batch_nos

frappe.flags.ignore_serial_batch_bundle_validation = True
frappe.flags.use_serial_and_batch_fields = True
sn_item = "Test Don't Validate Landed Cost Voucher Serial NO for Legacy PR"
batch_item = "Test Don't Validate Landed Cost Voucher Batch NO for Legacy PR"
sn_item_doc = make_item(
sn_item,
{
"has_serial_no": 1,
"serial_no_series": "SN-TDVLCVSNO-.####",
"is_stock_item": 1,
},
)

batch_item_doc = make_item(
batch_item,
{
"has_batch_no": 1,
"batch_number_series": "BATCH-TDVLCVSNO-.####",
"create_new_batch": 1,
"is_stock_item": 1,
},
)

serial_nos = [
"SN-TDVLCVSNO-0001",
"SN-TDVLCVSNO-0002",
"SN-TDVLCVSNO-0003",
"SN-TDVLCVSNO-0004",
"SN-TDVLCVSNO-0005",
]

for sn in serial_nos:
if not frappe.db.exists("Serial No", sn):
sn_doc = frappe.get_doc(
{
"doctype": "Serial No",
"item_code": sn_item,
"serial_no": sn,
}
)
sn_doc.insert()

if not frappe.db.exists("Batch", "BATCH-TDVLCVSNO-0001"):
batch_doc = frappe.get_doc(
{
"doctype": "Batch",
"item": batch_item,
"batch_id": "BATCH-TDVLCVSNO-0001",
}
)
batch_doc.insert()

warehouse = "_Test Warehouse - _TC"
company = frappe.db.get_value("Warehouse", warehouse, "company")

pr = make_purchase_receipt(
company=company,
warehouse=warehouse,
item_code=sn_item,
qty=5,
rate=100,
uom=sn_item_doc.stock_uom,
stock_uom=sn_item_doc.stock_uom,
do_not_submit=True,
)

pr.append(
"items",
{
"item_code": batch_item,
"item_name": batch_item,
"description": "Test Batch Item",
"uom": batch_item_doc.stock_uom,
"stock_uom": batch_item_doc.stock_uom,
"qty": 5,
"rate": 100,
"warehouse": warehouse,
},
)

pr.submit()
pr.reload()

for sn in serial_nos:
sn_doc = frappe.get_doc("Serial No", sn)
sn_doc.db_set(
{
"warehouse": warehouse,
"status": "Active",
}
)

batch_doc.db_set(
{
"batch_qty": 5,
}
)

for row in pr.items:
if row.item_code == sn_item:
row.db_set("serial_no", ", ".join(serial_nos))
else:
row.db_set("batch_no", "BATCH-TDVLCVSNO-0001")

stock_ledger_entries = frappe.get_all("Stock Ledger Entry", filters={"voucher_no": pr.name})
for sle in stock_ledger_entries:
doc = frappe.get_doc("Stock Ledger Entry", sle.name)
if doc.item_code == sn_item:
doc.db_set("serial_no", ", ".join(serial_nos))
else:
doc.db_set("batch_no", "BATCH-TDVLCVSNO-0001")


dn = create_delivery_note(
company=company,
warehouse=warehouse,
item_code=sn_item,
qty=5,
rate=100,
uom=sn_item_doc.stock_uom,
stock_uom=sn_item_doc.stock_uom,
do_not_submit=True,
)

dn.append(
"items",
{
"item_code": batch_item,
"item_name": batch_item,
"description": "Test Batch Item",
"uom": batch_item_doc.stock_uom,
"stock_uom": batch_item_doc.stock_uom,
"qty": 5,
"rate": 100,
"warehouse": warehouse,
},
)

dn.submit()

stock_ledger_entries = frappe.get_all("Stock Ledger Entry", filters={"voucher_no": dn.name})
for sle in stock_ledger_entries:
doc = frappe.get_doc("Stock Ledger Entry", sle.name)
if doc.item_code == sn_item:
doc.db_set("serial_no", ", ".join(serial_nos))
else:
doc.db_set("batch_no", "BATCH-TDVLCVSNO-0001")

available_batches = get_auto_batch_nos(
frappe._dict(
{
"item_code": batch_item,
"warehouse": warehouse,
"batch_no": ["BATCH-TDVLCVSNO-0001"],
"consider_negative_batches": True,
}
)
)[0]

self.assertFalse(available_batches.get("qty"))

frappe.flags.ignore_serial_batch_bundle_validation = False
frappe.flags.use_serial_and_batch_fields = False

lcv = make_landed_cost_voucher(
company=pr.company,
receipt_document_type="Purchase Receipt",
receipt_document=pr.name,
charges=20,
distribute_charges_based_on="Qty",
do_not_save=True,
)

lcv.get_items_from_purchase_receipts()
lcv.save()
lcv.submit()

pr.reload()

for row in pr.items:
self.assertEqual(row.valuation_rate, 102)
self.assertTrue(row.serial_and_batch_bundle)
self.assertEqual(
row.valuation_rate,
frappe.db.get_value("Serial and Batch Bundle", row.serial_and_batch_bundle, "avg_rate"),
)

lcv.cancel()
pr.reload()

for row in pr.items:
self.assertEqual(row.valuation_rate, 100)
self.assertTrue(row.serial_and_batch_bundle)
self.assertEqual(
row.valuation_rate,
frappe.db.get_value("Serial and Batch Bundle", row.serial_and_batch_bundle, "avg_rate"),
)

def make_landed_cost_voucher(**args):
args = frappe._dict(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,12 @@ def validate_serial_and_batch_inventory(self):
self.validate_batch_inventory()

def validate_batch_inventory(self):
if (
self.voucher_type in ["Purchase Invoice", "Purchase Receipt"]
and frappe.db.get_value(self.voucher_type, self.voucher_no, "docstatus") == 1
):
return

if not self.has_batch_no:
return

Expand Down

0 comments on commit 6647815

Please sign in to comment.