summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2025-09-17 11:14:45 +0000
committerIT Fixcomart <it@fixcomart.co.id>2025-09-17 11:14:45 +0000
commit23d713fc686d56ef5b5e8004b91b3f4fe54117e6 (patch)
tree6d9ccb0905c1530adf1f495298692b80dbeab012
parent61b54c404d0c104a5751a17eed94c5934805a050 (diff)
parentd37525b0dbdec49d2e812fc1945fb6a81a570e94 (diff)
Merged in reminder-tempo-v3 (pull request #421)
Reminder tempo v3
-rw-r--r--indoteknik_custom/models/account_move.py116
-rw-r--r--indoteknik_custom/views/mail_template_invoice_reminder.xml1
2 files changed, 64 insertions, 53 deletions
diff --git a/indoteknik_custom/models/account_move.py b/indoteknik_custom/models/account_move.py
index 70cd07e4..f10ca23f 100644
--- a/indoteknik_custom/models/account_move.py
+++ b/indoteknik_custom/models/account_move.py
@@ -192,49 +192,57 @@ class AccountMove(models.Model):
def send_due_invoice_reminder(self):
today = fields.Date.today()
target_dates = [
- today - timedelta(days=7),
- today - timedelta(days=3),
- today,
- today + timedelta(days=3),
today + timedelta(days=7),
+ today + timedelta(days=3),
+ today,
]
-
- for days_after_due in range(14, 181, 7):
- target_dates.append(today - timedelta(days=days_after_due))
-
invoices = self.env['account.move'].search([
('move_type', '=', 'out_invoice'),
('state', '=', 'posted'),
('payment_state', 'not in', ['paid', 'in_payment', 'reversed']),
('invoice_date_due', 'in', target_dates),
- ('date_terima_tukar_faktur', '!=', False)
- ])
- _logger.info(f"Invoices: {invoices}")
+ ('date_terima_tukar_faktur', '!=', False),
+ ('invoice_payment_term_id.name', 'ilike', 'tempo')])
+ _logger.info(f"Found {len(invoices)} invoices due for reminder {invoices}.")
+ if not invoices:
+ _logger.info("Tidak ada invoice yang due")
+ return
- invoices = invoices.filtered(
- lambda inv: inv.invoice_payment_term_id and 'tempo' in (inv.invoice_payment_term_id.name or '').lower()
- )
- # _logger.info(f"Invoices tahap 2: {invoices}")
+ self._send_invoice_reminders(invoices, mode='due')
+ def send_overdue_invoice_reminder(self):
+ today = fields.Date.today()
+ invoices = self.env['account.move'].search([
+ ('move_type', '=', 'out_invoice'),
+ ('state', '=', 'posted'),
+ ('payment_state', 'not in', ['paid', 'in_payment', 'reversed']),
+ ('invoice_date_due', '<', today),
+ ('date_terima_tukar_faktur', '!=', False),
+ ('invoice_payment_term_id.name', 'ilike', 'tempo'),
+ ('partner_id', 'in', [94603])])
+ _logger.info(f"Found {len(invoices)} invoices overdue for reminder {invoices}.")
if not invoices:
- _logger.info("Tidak ada invoice yang due")
+ _logger.info("Tidak ada invoice yang overdue")
return
+ self._send_invoice_reminders(invoices, mode='overdue')
+
+ def _send_invoice_reminders(self, invoices, mode):
+ today = fields.Date.today()
+ template = self.env.ref('indoteknik_custom.mail_template_invoice_due_reminder')
invoice_group = {}
for inv in invoices:
dtd = (inv.invoice_date_due - today).days if inv.invoice_date_due else 0
- key = (inv.partner_id, dtd)
+ key = (inv.partner_id, dtd if mode == 'due' else "overdue")
if key not in invoice_group:
invoice_group[key] = self.env['account.move'] # recordset kosong
invoice_group[key] |= inv # gabung recordset
- template = self.env.ref('indoteknik_custom.mail_template_invoice_due_reminder')
-
for (partner, dtd), invs in invoice_group.items():
if all(inv.reminder_sent_date == today for inv in invs):
_logger.info(f"Reminder untuk {partner.name} sudah terkirim hari ini, skip.")
continue
-
+
promise_dates = [inv.customer_promise_date for inv in invs if inv.customer_promise_date]
if promise_dates:
earliest_promise = min(promise_dates) # ambil janji paling awal
@@ -276,11 +284,12 @@ class AccountMove(models.Model):
invoice_table_rows = ""
grand_total = 0
- for inv in invs:
+ for idx, inv in enumerate(invs, start=1): # numbering
days_to_due = (inv.invoice_date_due - today).days if inv.invoice_date_due else 0
grand_total += inv.amount_total
invoice_table_rows += f"""
<tr>
+ <td>{idx}</td>
<td>{inv.partner_id.name}</td>
<td>{inv.ref or '-'}</td>
<td>{inv.name}</td>
@@ -291,6 +300,7 @@ class AccountMove(models.Model):
<td>{days_to_due}</td>
</tr>
"""
+
invoice_table_footer = f"""
<tfoot>
<tr style="font-weight:bold; background-color:#f9f9f9;">
@@ -354,33 +364,33 @@ class AccountMove(models.Model):
days_to_due_message = ""
closing_message = ""
- if dtd > 0:
- days_to_due_message = (
- f"Kami ingin mengingatkan bahwa tagihan anda akan jatuh tempo dalam {dtd} hari ke depan, "
- "dengan rincian sebagai berikut:"
- )
- closing_message = (
- "Kami mengharapkan pembayaran dapat dilakukan tepat waktu untuk mendukung kelancaran "
- "hubungan kerja sama yang baik antara kedua belah pihak.<br/>"
- "Mohon konfirmasi apabila pembayaran telah dijadwalkan. "
- "Terima kasih atas perhatian dan kerja samanya."
- )
-
- if dtd == 0:
- days_to_due_message = (
- "Kami ingin mengingatkan bahwa tagihan anda telah memasuki tanggal jatuh tempo pada hari ini, "
- "dengan rincian sebagai berikut:"
- )
- closing_message = (
- "Mohon kesediaannya untuk segera melakukan pembayaran tepat waktu guna menghindari status "
- "keterlambatan dan menjaga kelancaran hubungan kerja sama yang telah terjalin dengan baik.<br/>"
- "Apabila pembayaran telah dijadwalkan atau diproses, mohon dapat dikonfirmasi kepada kami. "
- "Terima kasih atas perhatian dan kerja samanya."
- )
+ if mode == "due":
+ if dtd > 0:
+ days_to_due_message = (
+ f"Kami ingin mengingatkan bahwa tagihan anda akan jatuh tempo dalam {dtd} hari ke depan, "
+ "dengan rincian sebagai berikut:"
+ )
+ closing_message = (
+ "Kami mengharapkan pembayaran dapat dilakukan tepat waktu untuk mendukung kelancaran "
+ "hubungan kerja sama yang baik antara kedua belah pihak.<br/>"
+ "Mohon konfirmasi apabila pembayaran telah dijadwalkan. "
+ "Terima kasih atas perhatian dan kerja samanya."
+ )
- if dtd < 0:
+ elif dtd == 0:
+ days_to_due_message = (
+ "Kami ingin mengingatkan bahwa tagihan anda telah memasuki tanggal jatuh tempo pada hari ini, "
+ "dengan rincian sebagai berikut:"
+ )
+ closing_message = (
+ "Mohon kesediaannya untuk segera melakukan pembayaran tepat waktu guna menghindari status "
+ "keterlambatan dan menjaga kelancaran hubungan kerja sama yang telah terjalin dengan baik.<br/>"
+ "Apabila pembayaran telah dijadwalkan atau diproses, mohon dapat dikonfirmasi kepada kami. "
+ "Terima kasih atas perhatian dan kerja samanya."
+ )
+ else: # mode overdue
days_to_due_message = (
- f"Kami ingin mengingatkan bahwa tagihan anda telah jatuh tempo selama {abs(dtd)} hari, "
+ f"Kami ingin mengingatkan bahwa beberapa tagihan anda telah jatuh tempo, "
"dengan rincian sebagai berikut:"
)
closing_message = (
@@ -413,20 +423,20 @@ class AccountMove(models.Model):
# Siapkan email values
values = {
- 'subject': f"Reminder Invoice Due - {partner.name}",
- # 'email_to': 'andrifebriyadiputra@gmail.com',
- 'email_to': email_to,
+ 'subject': f"Reminder Invoice Due Test - {partner.name}",
+ 'email_to': 'andrifebriyadiputra@gmail.com',
+ # 'email_to': email_to,
'email_from': 'finance@indoteknik.co.id',
- 'email_cc': ",".join(sorted(set(cc_list))),
+ # 'email_cc': ",".join(sorted(set(cc_list))),
'body_html': body_html,
- 'reply_to': 'finance@indoteknik.co.id',
+ # 'reply_to': 'finance@indoteknik.co.id',
}
template.send_mail(invs[0].id, force_send=True, email_values=values)
- _logger.info(f"Mengirim email ke: {values['email_to']} > email CC: {values['email_cc']}")
+ # _logger.info(f"Mengirim email ke: {values['email_to']} > email CC: {values['email_cc']}")
_logger.info(f"Reminder terkirim ke {partner.name} ({values['email_to']}) → {len(invs)} invoice (dtd = {dtd})")
# flag
- invs.write({'reminder_sent_date': today})
+ # invs.write({'reminder_sent_date': today})
# Post ke chatter
user_system = self.env['res.users'].browse(25)
system_id = user_system.partner_id.id if user_system else False
diff --git a/indoteknik_custom/views/mail_template_invoice_reminder.xml b/indoteknik_custom/views/mail_template_invoice_reminder.xml
index 13c02a08..3534f7f6 100644
--- a/indoteknik_custom/views/mail_template_invoice_reminder.xml
+++ b/indoteknik_custom/views/mail_template_invoice_reminder.xml
@@ -17,6 +17,7 @@
style="border-collapse:collapse; font-size:12px; border:1px solid #ddd;">
<thead>
<tr style="background-color:#f2f2f2; text-align:left;">
+ <th style="border:1px solid #ddd;">No.</th>
<th style="border:1px solid #ddd;">Customer</th>
<th style="border:1px solid #ddd;">No. PO</th>
<th style="border:1px solid #ddd;">Invoice Number</th>