Skip to content

Commit c241262

Browse files
fix: not able to make purchase receipt from SCR (backport frappe#44919) (frappe#44925)
* fix: not able to make purchase receipt from SCR (frappe#44919) (cherry picked from commit ab1cca0) * chore: fix test case --------- Co-authored-by: rohitwaghchaure <[email protected]>
1 parent 9060e4c commit c241262

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

erpnext/subcontracting/doctype/subcontracting_receipt/subcontracting_receipt.py

+29
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,9 @@ def post_process(source, target):
775775
postprocess=post_process,
776776
)
777777

778+
if not target_doc.get("items"):
779+
add_po_items_to_pr(source_doc, target_doc)
780+
778781
if (save or submit) and frappe.has_permission(target_doc.doctype, "create"):
779782
target_doc.save()
780783

@@ -794,3 +797,29 @@ def post_process(source, target):
794797
)
795798

796799
return target_doc
800+
801+
802+
def add_po_items_to_pr(scr_doc, target_doc):
803+
fg_items = {(item.item_code, item.purchase_order): item.qty for item in scr_doc.items}
804+
805+
for (item_code, po_name), fg_qty in fg_items.items():
806+
po_doc = frappe.get_doc("Purchase Order", po_name)
807+
for item in po_doc.items:
808+
if item.fg_item != item_code:
809+
continue
810+
811+
qty = (item.stock_qty - item.received_qty) * fg_qty / item.fg_item_qty
812+
if qty:
813+
target_doc.append(
814+
"items",
815+
{
816+
"item_code": item.item_code,
817+
"item_name": item.item_name,
818+
"description": item.description,
819+
"qty": qty,
820+
"rate": item.rate,
821+
"warehouse": item.warehouse,
822+
"purchase_order": item.parent,
823+
"purchase_order_item": item.name,
824+
},
825+
)

erpnext/subcontracting/doctype/subcontracting_receipt/test_subcontracting_receipt.py

+74
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,80 @@ def test_auto_create_purchase_receipt(self):
11371137

11381138
self.assertEqual(pr_details[0]["total_taxes_and_charges"], 60)
11391139

1140+
@change_settings("Buying Settings", {"auto_create_purchase_receipt": 1})
1141+
def test_auto_create_purchase_receipt_with_no_reference_of_po_item(self):
1142+
from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order
1143+
1144+
fg_item = "Subcontracted Item SA1"
1145+
service_items = [
1146+
{
1147+
"warehouse": "_Test Warehouse - _TC",
1148+
"item_code": "Subcontracted Service Item 1",
1149+
"qty": 10,
1150+
"rate": 100,
1151+
"fg_item": fg_item,
1152+
"fg_item_qty": 5,
1153+
},
1154+
]
1155+
1156+
po = create_purchase_order(
1157+
rm_items=service_items,
1158+
is_subcontracted=1,
1159+
supplier_warehouse="_Test Warehouse 1 - _TC",
1160+
do_not_submit=True,
1161+
)
1162+
po.append(
1163+
"taxes",
1164+
{
1165+
"account_head": "_Test Account Excise Duty - _TC",
1166+
"charge_type": "On Net Total",
1167+
"cost_center": "_Test Cost Center - _TC",
1168+
"description": "Excise Duty",
1169+
"doctype": "Purchase Taxes and Charges",
1170+
"rate": 10,
1171+
},
1172+
)
1173+
po.save()
1174+
po.submit()
1175+
1176+
sco = get_subcontracting_order(po_name=po.name)
1177+
for row in sco.items:
1178+
row.db_set("purchase_order_item", None)
1179+
1180+
sco.reload()
1181+
1182+
for row in sco.items:
1183+
self.assertFalse(row.purchase_order_item)
1184+
1185+
rm_items = get_rm_items(sco.supplied_items)
1186+
itemwise_details = make_stock_in_entry(rm_items=rm_items)
1187+
make_stock_transfer_entry(
1188+
sco_no=sco.name,
1189+
rm_items=rm_items,
1190+
itemwise_details=copy.deepcopy(itemwise_details),
1191+
)
1192+
1193+
scr = make_subcontracting_receipt(sco.name)
1194+
for row in scr.items:
1195+
self.assertFalse(row.purchase_order_item)
1196+
1197+
scr.items[0].qty = 3
1198+
scr.save()
1199+
scr.submit()
1200+
1201+
pr_details = frappe.get_all(
1202+
"Purchase Receipt",
1203+
filters={"subcontracting_receipt": scr.name},
1204+
fields=["name", "total_taxes_and_charges"],
1205+
)
1206+
1207+
self.assertTrue(pr_details)
1208+
1209+
pr_qty = frappe.db.get_value("Purchase Receipt Item", {"parent": pr_details[0]["name"]}, "qty")
1210+
self.assertEqual(pr_qty, 6)
1211+
1212+
self.assertEqual(pr_details[0]["total_taxes_and_charges"], 60)
1213+
11401214
def test_use_serial_batch_fields_for_subcontracting_receipt(self):
11411215
fg_item = make_item(
11421216
"Test Subcontracted Item With Batch No",

0 commit comments

Comments
 (0)