From 940dfad233b503f800bd4888dfac8d1430289e9f Mon Sep 17 00:00:00 2001 From: rodh-odoo Date: Tue, 19 Aug 2025 14:54:21 +0530 Subject: [PATCH] [IMP] send_invoice_email: add cron job to send invoices automatically via email Purpose of this commit: - Added a scheduled action (cron) to automatically send invoices by email. - Allows invoices to be emailed without manual user action. - Uses config parameter `send_invoice_by_email` to enable/disable the email sending feature. --- send_invoice_email/__init__.py | 1 + send_invoice_email/__manifest__.py | 19 +++++++++++ send_invoice_email/models/__init__.py | 2 ++ send_invoice_email/models/account_move.py | 33 +++++++++++++++++++ .../models/res_config_settings.py | 7 ++++ send_invoice_email/views/ir_cron_data.xml | 12 +++++++ .../views/res_config_settings_view.xml | 19 +++++++++++ 7 files changed, 93 insertions(+) create mode 100644 send_invoice_email/__init__.py create mode 100644 send_invoice_email/__manifest__.py create mode 100644 send_invoice_email/models/__init__.py create mode 100644 send_invoice_email/models/account_move.py create mode 100644 send_invoice_email/models/res_config_settings.py create mode 100644 send_invoice_email/views/ir_cron_data.xml create mode 100644 send_invoice_email/views/res_config_settings_view.xml diff --git a/send_invoice_email/__init__.py b/send_invoice_email/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/send_invoice_email/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/send_invoice_email/__manifest__.py b/send_invoice_email/__manifest__.py new file mode 100644 index 0000000000..810400e638 --- /dev/null +++ b/send_invoice_email/__manifest__.py @@ -0,0 +1,19 @@ +{ + 'name': 'Send Invoice Email', + 'version': '1.0', + 'summary': 'Automatically send invoices by email after configurable days', + 'author': 'rodh', + 'category': 'Accounting', + "license": "LGPL-3", + 'depends': [ + 'account', + 'base', + ], + 'data': [ + 'views/res_config_settings_view.xml', + 'views/ir_cron_data.xml', + ], + 'installable': True, + 'application': False, + 'auto_install': False, +} diff --git a/send_invoice_email/models/__init__.py b/send_invoice_email/models/__init__.py new file mode 100644 index 0000000000..123c3f2514 --- /dev/null +++ b/send_invoice_email/models/__init__.py @@ -0,0 +1,2 @@ +from . import res_config_settings +from . import account_move diff --git a/send_invoice_email/models/account_move.py b/send_invoice_email/models/account_move.py new file mode 100644 index 0000000000..2cdfac4b4b --- /dev/null +++ b/send_invoice_email/models/account_move.py @@ -0,0 +1,33 @@ +from odoo import fields, models +from datetime import timedelta + + +class AccountMove(models.Model): + _inherit = 'account.move' + + def _send_email_invoice(self): + + hour = self.env['ir.config_parameter'].sudo().get_param('send_invoice_email.send_invoice_by_email') + + if hour is None: + return + + try: + hour = int(hour) + except ValueError: + return + + target_date = fields.Datetime.now() - timedelta(hours=hour) + + invoices = self.search([ + ('invoice_date', '<=', target_date.date()), + ('payment_state', '!=', 'paid'), + ('state', '=', 'posted') +]) + + template = self.env.ref('account.email_template_edi_invoice', raise_if_not_found=False) + if not template: + return + + for invoice in invoices: + template.send_mail(invoice.id, force_send=True) diff --git a/send_invoice_email/models/res_config_settings.py b/send_invoice_email/models/res_config_settings.py new file mode 100644 index 0000000000..7c4c1293b7 --- /dev/null +++ b/send_invoice_email/models/res_config_settings.py @@ -0,0 +1,7 @@ +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = 'res.config.settings' + + send_invoice_by_email = fields.Integer(string='Send invoice by email after (days)', config_parameter="send_invoice_email.send_invoice_by_email") diff --git a/send_invoice_email/views/ir_cron_data.xml b/send_invoice_email/views/ir_cron_data.xml new file mode 100644 index 0000000000..b8741e130b --- /dev/null +++ b/send_invoice_email/views/ir_cron_data.xml @@ -0,0 +1,12 @@ + + + + Send Invoice By Email + + code + model._send_email_invoice() + 1 + hours + True + + diff --git a/send_invoice_email/views/res_config_settings_view.xml b/send_invoice_email/views/res_config_settings_view.xml new file mode 100644 index 0000000000..05f0cfafcf --- /dev/null +++ b/send_invoice_email/views/res_config_settings_view.xml @@ -0,0 +1,19 @@ + + + + res.config.settings.inherit.view.form + res.config.settings + + + + + + + + +