Skip to content

Commit

Permalink
fix: purchase return entry issue (#44721)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure authored and mihir-kandoi committed Dec 17, 2024
1 parent f4612fb commit 75147af
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions erpnext/controllers/sales_and_purchase_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,13 @@ def validate_return_against(doc):
def validate_returned_items(doc):
valid_items = frappe._dict()

select_fields = "item_code, qty, stock_qty, rate, parenttype, conversion_factor"
select_fields = "item_code, qty, stock_qty, rate, parenttype, conversion_factor, name"
if doc.doctype != "Purchase Invoice":
select_fields += ",serial_no, batch_no"

if doc.doctype in ["Purchase Invoice", "Purchase Receipt", "Subcontracting Receipt"]:
select_fields += ",rejected_qty, received_qty"

if doc.doctype in ["Purchase Receipt", "Purchase Invoice"]:
select_fields += ",name"

for d in frappe.db.sql(
f"""select {select_fields} from `tab{doc.doctype} Item` where parent = %s""",
doc.return_against,
Expand Down Expand Up @@ -113,11 +110,13 @@ def validate_returned_items(doc):
for d in doc.get("items"):
key = d.item_code
raise_exception = False
if doc.doctype in ["Purchase Receipt", "Purchase Invoice"]:
if doc.doctype in ["Purchase Receipt", "Purchase Invoice", "Sales Invoice"]:
field = frappe.scrub(doc.doctype) + "_item"
if d.get(field):
key = (d.item_code, d.get(field))
raise_exception = True
elif doc.doctype == "Delivery Note":
key = (d.item_code, d.get("dn_detail"))

if d.item_code and (flt(d.qty) < 0 or flt(d.get("received_qty")) < 0):
if key not in valid_items:
Expand All @@ -129,7 +128,7 @@ def validate_returned_items(doc):
)
else:
ref = valid_items.get(key, frappe._dict())
validate_quantity(doc, d, ref, valid_items, already_returned_items)
validate_quantity(doc, key, d, ref, valid_items, already_returned_items)

if (
ref.rate
Expand Down Expand Up @@ -159,15 +158,15 @@ def validate_returned_items(doc):
frappe.throw(_("At least one item should be entered with negative quantity in return document"))


def validate_quantity(doc, args, ref, valid_items, already_returned_items):
def validate_quantity(doc, key, args, ref, valid_items, already_returned_items):
fields = ["stock_qty"]
if doc.doctype in ["Purchase Receipt", "Purchase Invoice", "Subcontracting Receipt"]:
if not args.get("return_qty_from_rejected_warehouse"):
fields.extend(["received_qty", "rejected_qty"])
else:
fields.extend(["received_qty"])

already_returned_data = already_returned_items.get(args.item_code) or {}
already_returned_data = already_returned_items.get(key) or {}

company_currency = erpnext.get_company_currency(doc.company)
stock_qty_precision = get_field_precision(
Expand Down Expand Up @@ -253,15 +252,20 @@ def get_already_returned_items(doc):
column += """, sum(abs(child.rejected_qty) * child.conversion_factor) as rejected_qty,
sum(abs(child.received_qty) * child.conversion_factor) as received_qty"""

field = (
frappe.scrub(doc.doctype) + "_item"
if doc.doctype in ["Purchase Invoice", "Purchase Receipt", "Sales Invoice"]
else "dn_detail"
)
data = frappe.db.sql(
f"""
select {column}
select {column}, {field}
from
`tab{doc.doctype} Item` child, `tab{doc.doctype}` par
where
child.parent = par.name and par.docstatus = 1
and par.is_return = 1 and par.return_against = %s
group by item_code
group by item_code, {field}
""",
doc.return_against,
as_dict=1,
Expand All @@ -271,7 +275,7 @@ def get_already_returned_items(doc):

for d in data:
items.setdefault(
d.item_code,
(d.item_code, d.get(field)),
frappe._dict(
{
"qty": d.get("qty"),
Expand Down

0 comments on commit 75147af

Please sign in to comment.