コンテンツへスキップ
メニュー
この質問にフラグが付けられました
2 返信
1724 ビュー

Hello, 

I want to send an email for the customer after the payment is created,

I tried to use this but not worked

class AccountPaymentRegisterInherit(models.TransientModel):
_inherit = 'account.payment.register'

def action_create_payments(self):

payment_records = super(AccountPaymentRegisterInherit, self).action_create_payments()

payments = self.env['account.payment'].search([('id', 'in', payment_records.ids)])

for payment in payments:
email_template = self.env.ref('plnx_membership.email_template_register_payment', raise_if_not_found=False)
if email_template:
email_template.send_mail(payment.id, force_send=True)

return payment_records
def write(self, vals):
result = super(AccountMove, self).write(vals)
for record in self:
if vals.get('payment_state') == 'paid' and record.payment_state == 'paid':


email_template = self.env.ref(
'plnx_membership.email_template_payment_registered')
if email_template:
email_template.send_mail(record.id, force_send=True)

return result
アバター
破棄
最善の回答

Hi,

Your code has a few issues:

  • In the account.payment.register inherit, action_create_payments() returns a recordset of account.payment (not a dict with ids), so payment_records.ids may fail if it's a single record—use payment_records directly.
  • The template 'plnx_membership.email_template_register_payment' may not exist or be configured for account.payment (ensure it's defined for the model and has partner_to_set for the customer).
  • In the AccountMove.write, the condition vals.get('payment_state') == 'paid' and record.payment_state == 'paid' is redundant and won't trigger reliably, as payment_state is a computed field (not stored) and only updates after the write completes. Use @api.depends or a post-write hook for reactivity.

Recommended Fix: Inherit account.payment and Override action_post() This triggers after the payment is posted (confirmed), which is when it's "created" and reconciles with the invoice, updating the invoice to 'paid'. Send the email to the invoice's partner (customer).

python

from odoo import models, api

class AccountPayment(models.Model):
    _inherit = 'account.payment'

    def action_post(self):
        res = super().action_post()
        for payment in self:
            # Get the reconciled invoice (assuming customer payment)
            reconciled_invoices = payment.reconciled_invoice_ids
            if reconciled_invoices:
                invoice = reconciled_invoices[0]  # Or loop if multiple
                email_template = self.env.ref('plnx_membership.email_template_payment_registered', raise_if_not_found=False)
                if email_template:
                    # Set context for customer email
                    email_template.with_context(default_partner_id=invoice.partner_id.id).send_mail(
                        invoice.id, force_send=True
                    )
        return res
  • Template Setup: Ensure email_template_payment_registered is for account.move (invoice model), with partner_to_set: object.partner_id in the template to email the customer.
  • Why action_post?: Payments are created as draft; posting confirms them and links to invoices.


Hope it helps

アバター
破棄
最善の回答
class AccountPayment(models.Model):
_inherit = "account.payment"

def action_post(self):
res = super(AccountPayment, self).action_post()

for payment in self:

# Use email code here

return res


Hi, better to use model account.payment to send automated email instead of account.payment.register model

アバター
破棄
関連投稿 返信 ビュー 活動
1
5月 25
1372
0
1月 25
1090
1
6月 25
578
1
6月 25
1517
2
4月 25
1125