Return/Debit Note in Purchase Invoice Error


When I’m trying to make a return/debit note in purchase invoice, it is throwing me the below error:

Invalid Return Against Purchase Invoice: PINV-00513

In Purchase receipt, return is working fine which actually takes care of stock. But, return has to be made in Sales Invoice as well which affects accounting.

Can you check for details error log in the browser’s console please? Is this issue occurring in all the Purchase Invoices? If not tried, please check once.

This is for all the Purchase Invoices. Below is the error log that I’m getting in the browser’s console.

Traceback (most recent call last):
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/”, line 55, in application
response = frappe.handler.handle()
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/”, line 21, in handle
data = execute_cmd(cmd)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/”, line 52, in execute_cmd
return, **frappe.form_dict)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/”, line 907, in call
return fn(*args, **newargs)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/desk/form/”, line 22, in savedocs
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/”, line 230, in save
return self._save(*args, **kwargs)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/”, line 253, in _save
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/”, line 192, in insert
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/”, line 771, in run_before_save_methods
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/”, line 666, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/”, line 891, in composer
return composed(self, method, *args, **kwargs)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/”, line 874, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/”, line 660, in
fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/accounts/doctype/purchase_invoice/”, line 44, in validate
super(PurchaseInvoice, self).validate()
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/”, line 29, in validate
super(BuyingController, self).validate()
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/”, line 16, in validate
super(StockController, self).validate()
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/”, line 43, in validate
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/”, line 15, in validate_return
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/”, line 30, in validate_return_against
.format(doc.meta.get_label(“return_against”), doc.return_against))
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/”, line 316, in throw
msgprint(msg, raise_exception=exc, title=title, indicator=‘red’)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/”, line 306, in msgprint
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/”, line 279, in _raise_exception
raise raise_exception, encode(msg)
ValidationError: Invalid Return Against Purchase Invoice: PINV-00513

Hi Team,

Can you please tell what could be the issue. This is important for us as we are not able to do partial returns

Hi @umair , Can you please help on this?

Not sure why this is happening… But while making ‘Return in Purchase Invoice’ it is going into ‘if’ block where customer is taken. In this case, as customer is None, it is breaking and throwing an error. Actually, if and elif conditions are becoming true here.

I added and doc.customer & and doc.supplier as shown below(Line-6 & Line-8 in below code) in erpnext/controllers/ (in validate_return_against() function)

def validate_return_against(doc):
	if not doc.return_against:
		frappe.throw(_("{0} is mandatory for Return").format(doc.meta.get_label("return_against")))
		filters = {"doctype": doc.doctype, "docstatus": 1, "company":}
		if doc.meta.get_field("customer") and doc.customer:
			filters["customer"] = doc.customer
		elif doc.meta.get_field("supplier") and doc.supplier:
			filters["supplier"] = doc.supplier
		if not frappe.db.exists(filters):
				frappe.throw(_("Invalid {0}: {1}")

This solved my problem.