Skip to content

Commit ac90975

Browse files
rohitwaghchauremergify[bot]
authored andcommitted
fix: cancel SABB if SLE cancelled from LCV
(cherry picked from commit f23a49a)
1 parent de5e8a6 commit ac90975

3 files changed

Lines changed: 118 additions & 2 deletions

File tree

erpnext/controllers/buying_controller.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,9 @@ def update_stock_ledger(self, allow_negative_stock=False, via_landed_cost_vouche
783783
or self.is_return
784784
or (self.is_internal_transfer() and self.docstatus == 2)
785785
else self.get_package_for_target_warehouse(
786-
d, type_of_transaction=type_of_transaction
786+
d,
787+
type_of_transaction=type_of_transaction,
788+
via_landed_cost_voucher=via_landed_cost_voucher,
787789
)
788790
),
789791
},
@@ -871,7 +873,22 @@ def update_stock_ledger(self, allow_negative_stock=False, via_landed_cost_vouche
871873
via_landed_cost_voucher=via_landed_cost_voucher,
872874
)
873875

874-
def get_package_for_target_warehouse(self, item, warehouse=None, type_of_transaction=None) -> str:
876+
def get_package_for_target_warehouse(
877+
self, item, warehouse=None, type_of_transaction=None, via_landed_cost_voucher=None
878+
) -> str:
879+
if via_landed_cost_voucher and item.get("warehouse"):
880+
if sabb := frappe.db.get_value(
881+
"Serial and Batch Bundle",
882+
{
883+
"voucher_detail_no": item.name,
884+
"warehouse": item.get("warehouse"),
885+
"docstatus": 1,
886+
"is_cancelled": 0,
887+
},
888+
"name",
889+
):
890+
return sabb
891+
875892
if not item.serial_and_batch_bundle:
876893
return ""
877894

erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,102 @@ def test_stock_transfer_from_purchase_receipt(self):
10171017

10181018
pr.cancel()
10191019

1020+
def test_lcv_for_internal_transfer(self):
1021+
from erpnext.stock.doctype.delivery_note.delivery_note import make_inter_company_purchase_receipt
1022+
from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
1023+
from erpnext.stock.doctype.landed_cost_voucher.test_landed_cost_voucher import (
1024+
make_landed_cost_voucher,
1025+
)
1026+
1027+
prepare_data_for_internal_transfer()
1028+
1029+
customer = "_Test Internal Customer 2"
1030+
company = "_Test Company with perpetual inventory"
1031+
1032+
item_code = make_item(
1033+
"Test Item For LCV in Internal Transfer",
1034+
{"has_batch_no": 1, "create_new_batch": 1, "batch_naming_series": "TEST-SBATCH.###"},
1035+
).name
1036+
1037+
pr1 = make_purchase_receipt(
1038+
item_code=item_code,
1039+
qty=10,
1040+
rate=100,
1041+
warehouse="Stores - TCP1",
1042+
company="_Test Company with perpetual inventory",
1043+
)
1044+
1045+
dn1 = create_delivery_note(
1046+
item_code=pr1.items[0].item_code,
1047+
company=company,
1048+
customer=customer,
1049+
cost_center="Main - TCP1",
1050+
expense_account="Cost of Goods Sold - TCP1",
1051+
qty=10,
1052+
rate=500,
1053+
warehouse="Stores - TCP1",
1054+
target_warehouse="Work In Progress - TCP1",
1055+
)
1056+
1057+
pr = make_inter_company_purchase_receipt(dn1.name)
1058+
pr.items[0].from_warehouse = "Work In Progress - TCP1"
1059+
pr.items[0].warehouse = "Stores - TCP1"
1060+
pr.submit()
1061+
1062+
sle_entries = frappe.get_all(
1063+
"Stock Ledger Entry",
1064+
filters={"voucher_type": "Purchase Receipt", "voucher_no": pr.name},
1065+
fields=["serial_and_batch_bundle", "actual_qty"],
1066+
)
1067+
self.assertEqual(len(sle_entries), 2)
1068+
1069+
inward_sabb = frappe.get_all(
1070+
"Serial and Batch Bundle",
1071+
filters={
1072+
"voucher_type": "Purchase Receipt",
1073+
"voucher_no": pr.name,
1074+
"total_qty": (">", 0),
1075+
"docstatus": 1,
1076+
},
1077+
pluck="name",
1078+
)
1079+
self.assertEqual(len(inward_sabb), 1)
1080+
1081+
original_cost = frappe.db.get_value("Serial and Batch Bundle", inward_sabb[0], "total_amount")
1082+
1083+
make_landed_cost_voucher(
1084+
company=pr.company,
1085+
receipt_document_type="Purchase Receipt",
1086+
receipt_document=pr.name,
1087+
charges=100,
1088+
distribute_charges_based_on="Qty",
1089+
expense_account="Expenses Included In Valuation - TCP1",
1090+
)
1091+
1092+
sle_entries = frappe.get_all(
1093+
"Stock Ledger Entry",
1094+
filters={"voucher_type": "Purchase Receipt", "voucher_no": pr.name, "is_cancelled": 0},
1095+
fields=["serial_and_batch_bundle", "actual_qty"],
1096+
)
1097+
self.assertEqual(len(sle_entries), 2)
1098+
1099+
new_inward_sabb = frappe.get_all(
1100+
"Serial and Batch Bundle",
1101+
filters={
1102+
"voucher_type": "Purchase Receipt",
1103+
"voucher_no": pr.name,
1104+
"total_qty": (">", 0),
1105+
"docstatus": 1,
1106+
},
1107+
pluck="name",
1108+
)
1109+
self.assertEqual(len(new_inward_sabb), 1)
1110+
1111+
new_cost = frappe.db.get_value("Serial and Batch Bundle", new_inward_sabb[0], "total_amount")
1112+
self.assertEqual(new_cost, original_cost + 100)
1113+
1114+
self.assertTrue(new_inward_sabb[0] == inward_sabb[0])
1115+
10201116
def test_stock_transfer_from_purchase_receipt_with_valuation(self):
10211117
from erpnext.stock.doctype.delivery_note.delivery_note import make_inter_company_purchase_receipt
10221118
from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note

erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,9 @@ def get_available_qty_from_sabb(self):
15701570
return query.run(as_dict=True)
15711571

15721572
def validate_voucher_no_docstatus(self):
1573+
if self.is_cancelled:
1574+
return
1575+
15731576
if self.voucher_type == "POS Invoice":
15741577
return
15751578

0 commit comments

Comments
 (0)