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
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
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()