diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2025-08-14 16:28:15 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2025-08-14 16:28:15 +0700 |
| commit | bfdaf12dc3e7abace11502fcf1cb6c87522f1a51 (patch) | |
| tree | 1b7d44f2b6a5ad2539f181effb66e9e3a59c9e9a /indoteknik_custom/models/account_move.py | |
| parent | ad6ccce331dc8c83f2cacb82b0e3c94467ff9d85 (diff) | |
| parent | db75081a2afcd369e8a0169f3fe2f080dbad0c4a (diff) | |
Merge branch 'odoo-backup' of bitbucket.org:altafixco/indoteknik-addons into odoo-backup
Diffstat (limited to 'indoteknik_custom/models/account_move.py')
| -rw-r--r-- | indoteknik_custom/models/account_move.py | 73 |
1 files changed, 32 insertions, 41 deletions
diff --git a/indoteknik_custom/models/account_move.py b/indoteknik_custom/models/account_move.py index fd08ed60..b0ffd8b9 100644 --- a/indoteknik_custom/models/account_move.py +++ b/indoteknik_custom/models/account_move.py @@ -97,6 +97,8 @@ class AccountMove(models.Model): payment_date = fields.Date(string="Payment Date", compute='_compute_payment_date') partial_payment = fields.Float(string="Partial Payment", compute='compute_partial_payment') + reminder_sent_date = fields.Date(string="Tanggal Reminder Terkirim") + def compute_partial_payment(self): for move in self: if move.amount_total_signed > 0 and move.amount_residual_signed > 0 and move.payment_state == 'partial': @@ -119,20 +121,6 @@ class AccountMove(models.Model): else: move.payment_date = False - # def name_get(self): - # result = [] - # for move in self: - # if move.move_type == 'entry': - # # Jika masih draft, tampilkan 'Draft CAB' - # if move.state == 'draft': - # label = 'Draft CAB' - # else: - # label = move.name - # result.append((move.id, label)) - # else: - # # Untuk invoice dan lainnya, pakai default - # result.append((move.id, move.display_name)) - # return result def send_due_invoice_reminder(self): today = fields.Date.today() @@ -144,31 +132,22 @@ class AccountMove(models.Model): today + timedelta(days=7), ] - # --- TESTING --- - # partner = self.env['res.partner'].search([('name', 'ilike', 'DIRGANTARA YUDHA ARTHA')], limit=1) - # if not partner: - # _logger.info("Partner tidak ditemukan.") - # return - # 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), - # ('partner_id', '=', partner.id), - # ]) + 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 tahap 1: {invoices}") + _logger.info(f"Invoices: {invoices}") 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}") + # _logger.info(f"Invoices tahap 2: {invoices}") if not invoices: _logger.info("Tidak ada invoice yang due") @@ -177,11 +156,18 @@ class AccountMove(models.Model): invoice_group = {} for inv in invoices: dtd = (inv.invoice_date_due - today).days if inv.invoice_date_due else 0 - invoice_group.setdefault((inv.partner_id, dtd), []).append(inv) + key = (inv.partner_id, dtd) + 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 + # Ambil child contact yang di-checklist reminder_invoices reminder_contacts = self.env['res.partner'].search([ ('parent_id', '=', partner.id), @@ -190,11 +176,16 @@ class AccountMove(models.Model): ]) _logger.info(f"Email Reminder Child {reminder_contacts}") - if not reminder_contacts: - _logger.info(f"Partner {partner.name} tidak memiliki email yang sudah ceklis reminder") - continue + # if not reminder_contacts: + # _logger.info(f"Partner {partner.name} tidak memiliki email yang sudah ceklis reminder") + # continue emails = list(filter(None, [partner.email])) + reminder_contacts.mapped('email') + if reminder_contacts: + _logger.info(f"Email Reminder Child {reminder_contacts}") + else: + _logger.info(f"Tidak ada child contact reminder, gunakan email utama partner") + if not emails: _logger.info(f"Partner {partner.name} tidak memiliki email yang bisa dikirimi") continue @@ -208,7 +199,7 @@ class AccountMove(models.Model): invoice_table_rows += f""" <tr> <td>{inv.partner_id.name}</td> - <td>{inv.purchase_order_id.name or '-'}</td> + <td>{inv.ref or '-'}</td> <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> @@ -220,9 +211,9 @@ class AccountMove(models.Model): days_to_due_message = "" closing_message = "" - if dtd < 0: + if dtd > 0: days_to_due_message = ( - f"Kami ingin mengingatkan bahwa tagihan anda akan jatuh tempo dalam {abs(dtd)} hari ke depan, " + f"Kami ingin mengingatkan bahwa tagihan anda akan jatuh tempo dalam {dtd} hari ke depan, " "dengan rincian sebagai berikut:" ) closing_message = ( @@ -244,9 +235,9 @@ class AccountMove(models.Model): "Terima kasih atas perhatian dan kerja samanya." ) - if dtd > 0: + if dtd < 0: days_to_due_message = ( - f"Kami ingin mengingatkan bahwa tagihan anda telah jatuh tempo selama {dtd} hari, " + f"Kami ingin mengingatkan bahwa tagihan anda telah jatuh tempo selama {abs(dtd)} hari, " "dengan rincian sebagai berikut:" ) closing_message = ( @@ -282,18 +273,18 @@ class AccountMove(models.Model): # 'email_to': 'andrifebriyadiputra@gmail.com', 'email_to': email_to, 'email_from': 'finance@indoteknik.co.id', - 'email_cc': ",".join(cc_list), + 'email_cc': ",".join(sorted(set(cc_list))), 'body_html': body_html, 'reply_to': 'finance@indoteknik.co.id', } - _logger.info(f"Mengirim email ke: {values['email_to']} CC: {values['email_cc']}") + _logger.info(f"Mengirim email ke: {values['email_to']} > email CC: {values['email_cc']}") template.send_mail(invs[0].id, force_send=True, email_values=values) - + # flag + 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 - for inv in invs: inv.message_post( subject=values['subject'], |
