Successfully automated supplier email notification on Payment Entry. Sharing the code if anyone finds it helpful.
def send_payment_notification(doc, method):
if enable_supplier_email ==1:
from erpnext.accounts.utils import get_balance_on
from frappe.core.doctype.communication.email import make
if doc.payment_type == "Pay" and doc.party_type == "Supplier" and doc.payment_order:
from erpnext.accounts.party import get_party_details
party_details = get_party_details(party_type=doc.party_type, party=doc.party)
supplier_email = party_details["contact_email"]
cc = frappe.db.get_single_value('AP Automation Settings', 'cc')
supplier_balance = get_balance_on(party_type="Supplier",party=doc.party)
if supplier_email:
try:
recipients = [supplier_email]
cc = [cc]
bcc = []
import locale
locale.setlocale(locale.LC_MONETARY, 'en_IN')
file = Path(__file__).with_name("email_supplier_payment_notification.html").absolute()
html = file.read_text()
email_template = html.format(
party_name = doc.party_name,
mode_of_payment = doc.mode_of_payment,
reference_no = doc.reference_no,
reference_date = doc.reference_date,
paid_amount = locale.currency(doc.paid_amount, grouping=True),
supplier_balance = locale.currency(supplier_balance, grouping=True),
)
subject = "Payment of {paid_amount} successful from Zarnik".format(
party_name = doc.party_name,
mode_of_payment = doc.mode_of_payment,
reference_no = doc.reference_no,
reference_date = doc.reference_date,
paid_amount = locale.currency(doc.paid_amount, grouping=True),
supplier_balance = locale.currency(supplier_balance, grouping=True),
)
from zarnik.utils import pdf_account_statement
attachments = pdf_account_statement(party_type=doc.party_type, party=doc.party, party_name=doc.party_name)
make(
doctype="Payment Entry",
name=doc.name,
content=email_template,
subject=subject,
recipients=recipients,
communication_medium="Email",
send_email=True,
attachments=attachments,
cc=cc,
bcc=bcc,
read_receipt=True,
)
except Exception as e:
frappe.log_error(message=e,title="Error send_payment_notification")
Account statement template
<head>
<style>
table.statement td {
border: 1px solid #cbdbdb;
border-collapse: collapse;
font-size: 12px;
}
table.statement th {
border: 1px solid #cbdbdb;
border-collapse: collapse;
background-color: #e9f0f0;
font-size: 12px;
}
</style>
</head>
<body>
<table style="width: 100%;">
<tbody>
<tr>
<td style="width: 80%; margin-top: 20px;">
<p style="text-align:left;"><span style="font-size: 16px; font-weight: bold;">Statement of Accounts</span><br>
<span style="font-size: 12px;">{{party_name}} | {{ frappe.format(from_date, {'fieldtype': 'Date'}) }} to {{ frappe.format(to_date, {'fieldtype': 'Date'}) }}</span></p>
</td>
<td style="width: 20%;">
<img src="/files/zarnik-logo-320x77-gsuite.png" width="130px" align="right">
</td>
</tr>
</tbody>
</table>
{% set gl = frappe.get_all('GL Entry', filters={'is_cancelled': 0, 'party': party, 'posting_date': ['>', from_date] }, fields=['posting_date', 'voucher_type', 'voucher_no', 'debit', 'credit'], order_by='posting_date asc') %}
<table class="statement" width="100%" cellpadding="4" cellspacing="0" style="margin-top: 20px; margin-bottom: 20px;">
<colgroup>
<col style="text-align: left; width: 15%;">
<col style="text-align: left; width: 25%;">
<col style="text-align: left; width: 30%;">
<col style="text-align: right; width: 15%;">
<col style="text-align: right; width: 15%;">
</colgroup>
<thead>
<tr>
<th style="text-align: left;">Date</th>
<th style="text-align: left;">Document</th>
<th style="text-align: left;">Reference</th>
<th style="text-align: center;">Debit</th>
<th style="text-align: center;">Credit</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>Opening Balance</td>
<td></td>
<td style="text-align: right;">
{% if opening_debit_balance != 0 %}
{{ frappe.format(opening_debit_balance, {'fieldtype': 'Currency'}) }}
{% else %} -
{%- endif %}
</td>
<td style="text-align: right;">
{% if opening_credit_balance != 0 %}
{{ frappe.format(opening_credit_balance, {'fieldtype': 'Currency'}) }}
{% else %} -
{%- endif %}
</td>
</tr>
{% for row in gl %}
<tr>
<td>{{ frappe.format(row.posting_date, {'fieldtype': 'Date'}) }}</td>
<td>{{row.voucher_type}} <br><small>{{row.voucher_no}}</small> </td>
<td>
{% if row.voucher_type == 'Payment Entry' %}
{% set ref = frappe.db.get_value(row.voucher_type, row.voucher_no, 'reference_no') %}
{% elif row.voucher_type == 'Journal Entry' %}
{% set ref = frappe.db.get_value(row.voucher_type, row.voucher_no,'cheque_no') %}
{% if ref is none %}
{% set ref = frappe.db.get_value(row.voucher_type, row.voucher_no,'user_remark') %}
{%- endif %}
{% elif row.voucher_type == 'Purchase Invoice' %}
{% set ref = frappe.db.get_value(row.voucher_type, row.voucher_no,'bill_no') %}
{% elif row.voucher_type == 'Sales Invoice' %}
{% set ref = frappe.db.get_value(row.voucher_type, row.voucher_no,'name') %}
{% else %}
{% set ref = ' ' %}
{%- endif %}
{% if ref is none %}
{% set ref = ' ' %}
{%- endif %}
{{ref}}
</td>
<td style="text-align: right;">
{% if row.debit != 0 %}
{{ frappe.format(row.debit, {'fieldtype': 'Currency'}) }}
{% else %} -
{%- endif %}
</td>
<td style="text-align: right;">
{% if row.credit != 0 %}
{{ frappe.format(row.credit, {'fieldtype': 'Currency'}) }}
{% else %} -
{%- endif %}
</td>
</tr>
{% endfor %}
<tr>
<td></td>
<td>Closing Balance</td>
<td></td>
<td style="text-align: right;">
{% if closing_debit_balance != 0 %}
{{ frappe.format(closing_debit_balance, {'fieldtype': 'Currency'}) }}
{% else %} -
{%- endif %}
</td>
<td style="text-align: right;">
{% if closing_credit_balance != 0 %}
{{ frappe.format(closing_credit_balance, {'fieldtype': 'Currency'}) }}
{% else %} -
{%- endif %}
</td>
</tr>
</tbody>
</table>
</body>
Email Template
Dear {party_name}, <br>
<br>
We have made a payment to your account. Following are the details of the transaction.<br>
<br>
<table cellpadding="5" cellspacing="0">
<colgroup>
<col style="font-weight: bold; width: 150px;">
</colgroup>
<tbody>
<tr>
<th align="left">Mode of Payment</th>
<td>:</td>
<td>{mode_of_payment}</td>
</tr>
<tr>
<th align="left">Reference No.</th>
<td>:</td>
<td>{reference_no}</td>
</tr>
<tr>
<th align="left">Reference Date</th>
<td>:</td>
<td>{reference_date}</td>
</tr>
<tr>
<th align="left">Amount Paid</th>
<td>:</td>
<td>{paid_amount}</td>
</tr>
</tbody>
</table>
<br>
Your account balance with us is {supplier_balance}<br>
<br>
If you have any clarifications regarding the above, please contact us at accounts@zarnik.com or +91-8129278080.<br>
<br>
Thanks,<br>
Team Zarnik<br>
<img style="width: 130px; margin-top: 30px;" src="https://ik.imagekit.io/zarnik/email/zarnik-logo-email.png" alt="Zarnik Email Signature Logo">