diff options
| -rw-r--r-- | indoteknik_custom/models/account_move.py | 78 | ||||
| -rw-r--r-- | indoteknik_custom/views/mail_template_invoice_reminder.xml | 25 |
2 files changed, 65 insertions, 38 deletions
diff --git a/indoteknik_custom/models/account_move.py b/indoteknik_custom/models/account_move.py index df79b9f6..fd72d566 100644 --- a/indoteknik_custom/models/account_move.py +++ b/indoteknik_custom/models/account_move.py @@ -9,6 +9,7 @@ import os import re from terbilang import Terbilang from collections import defaultdict +from odoo.tools.misc import formatLang _logger = logging.getLogger(__name__) @@ -76,12 +77,8 @@ class AccountMove(models.Model): def send_due_invoice_reminder(self): today = fields.Date.today() - reminder_days = [-7, -3, 0, 3, 7] - target_dates = [today + timedelta(days=delta) for delta in reminder_days] - target_dates_str = [d.isoformat() for d in target_dates] - # Ganti nama partner untuk test jika perlu - partner = self.env['res.partner'].search([('name', 'ilike', 'PRIMA SEJAHTERA MARITIM')], limit=1) + partner = self.env['res.partner'].search([('name', 'ilike', 'TIRTA FRESINDO JAYA')], limit=1) if not partner: _logger.info("Partner tidak ditemukan.") return @@ -92,6 +89,7 @@ class AccountMove(models.Model): ('payment_state', 'not in', ['paid','in_payment', 'reversed']), ('invoice_date_due', '>=', today - timedelta(days=7)), ('invoice_date_due', '>=', today - timedelta(days=3)), + ('invoice_date_due', '=', today), ('invoice_date_due', '<=', today + timedelta(days=3)), ('invoice_date_due', '<=', today + timedelta(days=7)), ('partner_id', '=', partner.id), @@ -99,7 +97,6 @@ class AccountMove(models.Model): _logger.info(f"Invoices tahap 1: {invoices}") - # Filter berdasarkan term mengandung "tempo" invoices = invoices.filtered( lambda inv: inv.invoice_payment_term_id and 'tempo' in (inv.invoice_payment_term_id.name or '').lower() ) @@ -109,23 +106,64 @@ class AccountMove(models.Model): _logger.info(f"Tidak ada invoice yang due untuk partner: {partner.name}") return - # Pastikan field compute jalan - invoices._compute_invoice_day_to_due() + grouped = {} + for inv in invoices: + grouped.setdefault(inv.partner_id, []).append(inv) - # Ambil template template = self.env.ref('indoteknik_custom.mail_template_invoice_due_reminder') - for inv in invoices: - try: - # Untuk test: override ke email pribadi Anda - email_values = { - 'email_to': 'andrifebriyadiputra@gmail.com', - 'email_from': 'finance@indoteknik.co.id', - } - template.send_mail(inv.id, force_send=True, email_values=email_values) - _logger.info(f"Reminder terkirim: {inv.name} → {email_values['email_to']}") - except Exception as e: - _logger.error(f"Gagal kirim email untuk {inv.name}: {str(e)}") + for partner, invs in grouped.items(): + if not partner.email: + _logger.info(f"Partner {partner.name} tidak memiliki email") + continue + + invoice_table_rows = "" + for inv in invs: + days_to_due = (inv.invoice_date_due - today).days if inv.invoice_date_due else 0 + invoice_table_rows += f""" + <tr> + <td>{inv.name}</td> + <td>{fields.Date.to_string(inv.invoice_date) or '-'}</td> + <td>{fields.Date.to_string(inv.invoice_date_due) or '-'}</td> + <td>{days_to_due}</td> + <td>{formatLang(self.env, inv.amount_total, currency_obj=inv.currency_id)}</td> + <td>{inv.ref or '-'}</td> + </tr> + """ + + subject = f"Reminder Invoice Due - {partner.name}" + body_html = re.sub( + r"<tbody[^>]*>.*?</tbody>", + f"<tbody>{invoice_table_rows}</tbody>", + template.body_html, + flags=re.DOTALL + ).replace('${object.name}', partner.name) \ + .replace('${object.partner_id.name}', partner.name) \ + .replace('${object.email}', partner.email or '') + + values = { + 'subject': subject, + 'email_to': 'andrifebriyadiputra@gmail.com', # Ubah ke partner.email untuk produksi + 'email_from': 'finance@indoteknik.co.id', + 'body_html': body_html, + } + + _logger.info(f"VALUES: {values}") + + # self.env['mail.mail'].create(values).send() + # _logger.info(f"Reminder terkirim ke {partner.name} ({values['email_to']}) → {len(invs)} invoice") + + # for inv in invoices: + # try: + # # Untuk test: override ke email pribadi Anda + # email_values = { + # 'email_to': 'andrifebriyadiputra@gmail.com', + # 'email_from': 'finance@indoteknik.co.id', + # } + # template.send_mail(inv.id, force_send=True, email_values=email_values) + # _logger.info(f"Reminder terkirim: {inv.name} → {email_values['email_to']}") + # except Exception as e: + # _logger.error(f"Gagal kirim email untuk {inv.name}: {str(e)}") # def name_get(self): # result = [] diff --git a/indoteknik_custom/views/mail_template_invoice_reminder.xml b/indoteknik_custom/views/mail_template_invoice_reminder.xml index b19171b2..b39730b6 100644 --- a/indoteknik_custom/views/mail_template_invoice_reminder.xml +++ b/indoteknik_custom/views/mail_template_invoice_reminder.xml @@ -2,17 +2,16 @@ <odoo> <data noupdate="1"> <record id="mail_template_invoice_due_reminder" model="mail.template"> - <field name="name">Invoice Reminder: Due Date Notification</field> - <field name="model_id" ref="account.model_account_move"/> - <field name="subject">[Reminder] Invoice ${object.name} is Due Soon</field> + <field name="name">Invoice Reminder: Due Date Notification (Manual)</field> + <field name="model_id" ref="base.model_res_partner"/> + <field name="subject">[Reminder] Invoice Manual</field> <field name="email_from">finance@indoteknik.co.id</field> - <field name="reply_to">finance@indoteknik.co.id</field> - <field name="email_to">andrifebriyadiputra@gmail.com</field> + <field name="email_to">test@example.com</field> <field name="body_html" type="html"> <div style="font-family:Arial, sans-serif; font-size:14px;"> - <p>Dengan Hormat Bpk/Ibu ${object.partner_id.name},</p> + <p>Dengan Hormat Bpk/Ibu,</p> - <p>Berikut adalah detail invoice Anda yang sudah mendekati atau telah jatuh tempo:</p> + <p>Berikut adalah daftar invoice Anda yang mendekati atau telah jatuh tempo:</p> <table border="1" cellpadding="6" cellspacing="0" style="border-collapse: collapse; width: 100%;"> <thead> @@ -26,20 +25,10 @@ </tr> </thead> <tbody> - <tr> - <td>${object.name}</td> - <td>${format_date(object.invoice_date)}</td> - <td>${format_date(object.invoice_date_due)}</td> - <td>${object.invoice_day_to_due}</td> - <td>${format_amount(object.amount_total, object.currency_id)}</td> - <td>${object.ref or '-'}</td> - </tr> </tbody> </table> - <p>Mohon segera melakukan proses pembayaran sebelum jatuh tempo.</p> - - <p>Terima kasih atas perhatian dan kerjasamanya.</p> + <p>Mohon segera melakukan proses pembayaran untuk invoice-invoice tersebut.</p> <p> Hormat Kami,<br/> |
