Return/Debit Note in Purchase Invoice Error

Hi,

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/app.py”, line 55, in application
response = frappe.handler.handle()
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/handler.py”, line 52, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/init.py”, line 907, in call
return fn(*args, **newargs)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/desk/form/save.py”, line 22, in savedocs
doc.save()
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/document.py”, line 230, in save
return self._save(*args, **kwargs)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/document.py”, line 253, in _save
self.insert()
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/document.py”, line 192, in insert
self.run_before_save_methods()
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/document.py”, line 771, in run_before_save_methods
self.run_method(“validate”)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/document.py”, line 666, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/document.py”, line 891, in composer
return composed(self, method, *args, **kwargs)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/document.py”, line 874, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/model/document.py”, 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/purchase_invoice.py”, line 44, in validate
super(PurchaseInvoice, self).validate()
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/buying_controller.py”, line 29, in validate
super(BuyingController, self).validate()
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/stock_controller.py”, line 16, in validate
super(StockController, self).validate()
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/accounts_controller.py”, line 43, in validate
validate_return(self)
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/sales_and_purchase_return.py”, line 15, in validate_return
validate_return_against(doc)
File “/home/vavcoders/frappe-bench/apps/erpnext/erpnext/controllers/sales_and_purchase_return.py”, line 30, in validate_return_against
.format(doc.meta.get_label(“return_against”), doc.return_against))
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/init.py”, line 316, in throw
msgprint(msg, raise_exception=exc, title=title, indicator=‘red’)
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/init.py”, line 306, in msgprint
_raise_exception()
File “/home/vavcoders/frappe-bench/apps/frappe/frappe/init.py”, 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/sales_and_purchase_return.py (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")))
	else:
		filters = {"doctype": doc.doctype, "docstatus": 1, "company": doc.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.