Automatic payment entry with server script getting error

I have written a server script which automatically creates payment voucher after purchase invoice is submitted.
Everything is working perfect but problem is coming sometimes and this error is coming.

error-screenshot :point_down:

We checked bank and cash account is set in company master,this error is coming only to the client sometimes and if we making enteriesd then the error does not come.

Can anyone help me how to solve this?

Also, can you one explain how shall I write logs statements in my server script so I will write them in our server script as well and will come to know why code is failing occassionaly.

server-script :point_down:

if doc.custom_advance_amount > 0:
    # Fetching the Purchase Invoice document
    purchase_invoice = frappe.get_doc("Purchase Invoice", doc.name)

    # Fetching the party account from the Purchase Invoice
    party_account = purchase_invoice.party_account

    # Assuming doc.custom_company is the field where the user selects the company
    company = frappe.get_doc("Company", doc.company)

    # Fetching the payment account based on the selected company
    paid_from_account_cash = company.default_cash_account

    payment = frappe.get_doc({
        "doctype": "Payment Entry",
        "payment_type": "Pay",
        "company": doc.company,
        "party_type": "Supplier",
        "party": doc.supplier,
        "paid_amount": doc.custom_advance_amount,
        "received_amount": doc.custom_advance_amount,
        # "source_exchange_rate": 0, 
        "mode_of_payment": doc.custom_payment_type,  # Assuming cash mode of payment
        "paid_from": paid_from_account_cash,
        "paid_to": party_account,  # Assigning party account directly
        "reference_no": "Auto",
        "references": [
            {
                "reference_doctype": "Purchase Invoice",
                "reference_name": doc.name,
                "allocated_amount": doc.custom_advance_amount
            }
        ]
    })

    payment.insert().submit()


if doc.custom_advance_amount_bank > 0:
    # Fetching the Purchase Invoice document
    purchase_invoice = frappe.get_doc("Purchase Invoice", doc.name)

    # Fetching the party account from the Purchase Invoice
    party_account = purchase_invoice.party_account

    # Assuming doc.custom_company is the field where the user selects the company
    company = frappe.get_doc("Company", doc.company)

    # Fetching the payment account based on the selected company
    paid_from_account_cash = company.default_bank_account

    payment = frappe.get_doc({
        "doctype": "Payment Entry",
        "payment_type": "Pay",
        "company": doc.company,
        "party_type": "Supplier",
        "party": doc.supplier,
        "paid_amount": doc.custom_advance_amount_bank,
        "received_amount": doc.custom_advance_amount_bank,
        # "source_exchange_rate": 0, 
        "mode_of_payment": doc.custom_payment_type_bank,  # Assuming cash mode of payment
        "paid_from": paid_from_account_cash,
        "paid_to": party_account,  # Assigning party account directly
        "reference_no": "Auto",
        "references": [
            {
                "reference_doctype": "Purchase Invoice",
                "reference_name": doc.name,
                "allocated_amount": doc.custom_advance_amount_bank
            }
        ]
    })

    payment.insert().submit()

frappe.log_error()

apps/erpnext/erpnext/controllers/accounts_controller.py

def calculate_paid_amount(self):
if hasattr(self, “is_pos”) or hasattr(self, “is_paid”):
is_paid = self.get(“is_pos”) or self.get(“is_paid”)

		if is_paid:
			if not self.cash_bank_account:
				# show message that the amount is not paid
				frappe.throw(
					_(
						"Note: Payment Entry will not be created since 'Cash or Bank Account' was not specified"
					)
				)

			if cint(self.is_return) and self.grand_total > self.paid_amount:
				self.paid_amount = flt(flt(self.grand_total), self.precision("paid_amount"))

			elif not flt(self.paid_amount) and flt(self.outstanding_amount) > 0:
				self.paid_amount = flt(flt(self.outstanding_amount), self.precision("paid_amount"))

			self.base_paid_amount = flt(
				self.paid_amount * self.conversion_rate, self.precision("base_paid_amount")
			)[quote="Jeel, post:3, topic:128238, full:true"]

(post deleted by author)
[/quote]

1 Like