From 1b574687a7f4b612a1fd221da1ca920009cc963b Mon Sep 17 00:00:00 2001 From: FIN-IT_AndriFP Date: Thu, 25 Sep 2025 14:33:51 +0700 Subject: (andri) make report & mail template close tempo --- indoteknik_custom/__manifest__.py | 3 +- indoteknik_custom/models/letter_receivable.py | 145 ++++++++------- indoteknik_custom/report/report_surat_piutang.xml | 4 +- indoteknik_custom/report/report_tutup_tempo.xml | 198 +++++---------------- .../views/close_tempo_mail_template.xml | 85 ++++----- 5 files changed, 167 insertions(+), 268 deletions(-) diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index 90fa915f..b083be70 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -167,6 +167,7 @@ 'report/report_picking.xml', 'report/report_sale_order.xml', 'report/report_surat_piutang.xml', + 'report/report_tutup_tempo.xml', 'report/purchase_report.xml', 'views/vendor_sla.xml', 'views/coretax_faktur.xml', @@ -184,7 +185,7 @@ 'views/letter_receivable_mail_template.xml', # 'views/reimburse.xml', 'views/sj_tele.xml', - 'views/mail_template_closing_apt.xml', + 'views/close_tempo_mail_template.xml', ], 'demo': [], 'css': [], diff --git a/indoteknik_custom/models/letter_receivable.py b/indoteknik_custom/models/letter_receivable.py index 6f95e5e7..bfa1e966 100644 --- a/indoteknik_custom/models/letter_receivable.py +++ b/indoteknik_custom/models/letter_receivable.py @@ -244,6 +244,13 @@ class SuratPiutang(models.Model): title="Informasi", sticky=False ) + + def action_print(self): + self.ensure_one() + if self.perihal == 'tutup_tempo': + return self.env.ref('indoteknik_custom.action_report_surat_tutup_tempo').report_action(self) + else: + return self.env.ref('indoteknik_custom.action_report_surat_piutang').report_action(self) def action_send_letter(self): self.ensure_one() @@ -255,64 +262,79 @@ class SuratPiutang(models.Model): if not self.tujuan_email: raise UserError(_("Email tujuan harus diisi.")) - template = self.env.ref('indoteknik_custom.letter_receivable_mail_template') - # today = fields.Date.today() - - month_map = { - 1: "Januari", 2: "Februari", 3: "Maret", 4: "April", - 5: "Mei", 6: "Juni", 7: "Juli", 8: "Agustus", - 9: "September", 10: "Oktober", 11: "November", 12: "Desember", - } - target_date = (self.send_date or fields.Datetime.now()).date() + timedelta(days=7) - self.seven_days_after_sent_date = f"{target_date.day} {month_map[target_date.month]}" - - perihal_map = { - 'penagihan': 'Surat Resmi Penagihan', - 'sp1': 'Surat Peringatan Pertama (I)', - 'sp2': 'Surat Peringatan Kedua (II)', - 'sp3': 'Surat Peringatan Ketiga (III)', - } - perihal_text = perihal_map.get(self.perihal, self.perihal or '') - - invoice_table_rows = "" - grand_total = 0 - for line in selected_lines: - # days_to_due = (line.invoice_date_due - today).days if line.invoice_date_due else 0 - grand_total += line.amount_residual - invoice_table_rows += f""" - - {line.invoice_number or '-'} - {self.partner_id.name or '-'} - {fields.Date.to_string(line.invoice_date) or '-'} - {fields.Date.to_string(line.invoice_date_due) or '-'} - {line.new_invoice_day_to_due} - {line.ref or '-'} - {formatLang(self.env, line.amount_residual, currency_obj=line.currency_id)} - {line.payment_term_id.name or '-'} - - """ - - invoice_table_footer = f""" - - - Grand Total - {formatLang(self.env, grand_total, currency_obj=self.currency_id, monetary=True)} - + template = None + report = None + body_html = None + subject = None + + # Logika untuk memilih template dan report berdasarkan 'perihal' + if self.perihal == 'tutup_tempo': + template = self.env.ref('indoteknik_custom.close_tempo_mail_template') + report = self.env.ref('indoteknik_custom.action_report_surat_tutup_tempo') + due_date_str = self.due_date.strftime('%d %B %Y') if self.due_date else 'yang telah ditentukan' + body_html = template.body_html \ + .replace('${object.partner_id.name}', self.partner_id.name or '') \ + .replace('${object.due_date}', due_date_str or '') + subject = f"Pemberitahuan Penutupan Pembayaran Tempo – {self.partner_id.name}" + else: + template = self.env.ref('indoteknik_custom.letter_receivable_mail_template') + + month_map = { + 1: "Januari", 2: "Februari", 3: "Maret", 4: "April", + 5: "Mei", 6: "Juni", 7: "Juli", 8: "Agustus", + 9: "September", 10: "Oktober", 11: "November", 12: "Desember", + } + target_date = (self.send_date or fields.Datetime.now()).date() + timedelta(days=7) + self.seven_days_after_sent_date = f"{target_date.day} {month_map[target_date.month]}" + + perihal_map = { + 'penagihan': 'Surat Resmi Penagihan', + 'sp1': 'Surat Peringatan Pertama (I)', + 'sp2': 'Surat Peringatan Kedua (II)', + 'sp3': 'Surat Peringatan Ketiga (III)', + } + perihal_text = perihal_map.get(self.perihal, self.perihal or '') + + invoice_table_rows = "" + grand_total = 0 + for line in selected_lines: + grand_total += line.amount_residual + invoice_table_rows += f""" + + {line.invoice_number or '-'} + {self.partner_id.name or '-'} + {fields.Date.to_string(line.invoice_date) or '-'} + {fields.Date.to_string(line.invoice_date_due) or '-'} + {line.new_invoice_day_to_due} + {line.ref or '-'} + {formatLang(self.env, line.amount_residual, currency_obj=line.currency_id)} + {line.payment_term_id.name or '-'} - - """ - # inject table rows ke template - body_html = re.sub( - r"]*>.*?", - f"{invoice_table_rows}{invoice_table_footer}", - template.body_html, - flags=re.DOTALL - ).replace('${object.name}', self.name or '') \ - .replace('${object.partner_id.name}', self.partner_id.name or '') \ - .replace('${object.seven_days_after_sent_date}', self.seven_days_after_sent_date or '') \ - .replace('${object.perihal}', perihal_text or '') - - report = self.env.ref('indoteknik_custom.action_report_surat_piutang') + """ + + invoice_table_footer = f""" + + + Grand Total + {formatLang(self.env, grand_total, currency_obj=self.currency_id, monetary=True)} + + + + """ + + body_html = re.sub( + r"]*>.*?", + f"{invoice_table_rows}{invoice_table_footer}", + template.body_html, + flags=re.DOTALL + ).replace('${object.name}', self.name or '') \ + .replace('${object.partner_id.name}', self.partner_id.name or '') \ + .replace('${object.seven_days_after_sent_date}', self.seven_days_after_sent_date or '') \ + .replace('${object.perihal}', perihal_text or '') + + report = self.env.ref('indoteknik_custom.action_report_surat_piutang') + subject = perihal_map.get(self.perihal, self.perihal or '') + " - " + (self.partner_id.name or '') + pdf_content, _ = report._render_qweb_pdf([self.id]) attachment_base64 = base64.b64encode(pdf_content) @@ -337,14 +359,13 @@ class SuratPiutang(models.Model): cc_list.append(sales_email) values = { - # 'subject': template.subject.replace('${object.name}', self.name or ''), - 'subject': perihal_map.get(self.perihal, self.perihal or '') + " - " + (self.partner_id.name or ''), + 'subject': subject, # Menggunakan subject yang sudah ditentukan di atas 'email_to': self.tujuan_email, 'email_from': 'finance@indoteknik.co.id', - 'email_cc': ",".join(sorted(set(cc_list))), - 'body_html': body_html, + # 'email_cc': ",".join(sorted(set(cc_list))), + 'body_html': body_html, # Menggunakan body_html yang sudah ditentukan di atas 'attachments': [(attachment.name, attachment.datas)], - 'reply_to': 'finance@indoteknik.co.id', + # 'reply_to': 'finance@indoteknik.co.id', } template.with_context(mail_post_autofollow=False).send_mail( diff --git a/indoteknik_custom/report/report_surat_piutang.xml b/indoteknik_custom/report/report_surat_piutang.xml index 62db7982..fc5593a7 100644 --- a/indoteknik_custom/report/report_surat_piutang.xml +++ b/indoteknik_custom/report/report_surat_piutang.xml @@ -10,8 +10,8 @@ indoteknik_custom.report_surat_piutang indoteknik_custom.report_surat_piutang '%s - %s' % (object.perihal_label or '', object.partner_id.name or '') - - report +