diff --git a/auto_invoice_email/__init__.py b/auto_invoice_email/__init__.py new file mode 100644 index 00000000000..d6210b1285d --- /dev/null +++ b/auto_invoice_email/__init__.py @@ -0,0 +1,3 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import models diff --git a/auto_invoice_email/__manifest__.py b/auto_invoice_email/__manifest__.py new file mode 100644 index 00000000000..1957455ec6f --- /dev/null +++ b/auto_invoice_email/__manifest__.py @@ -0,0 +1,14 @@ +{ + "name": "Auto Invoice Email", + "version": "1.0", + "summary": "Automatically send posted invoices by email after configured days", + "category": "Accounting", + "author": "Rohit", + "depends": ["account"], + "data": [ + "views/res_config_settings_views.xml", + "data/auto_send_cron.xml", + ], + "installable": True, + "license": "LGPL-3", +} diff --git a/auto_invoice_email/data/auto_send_cron.xml b/auto_invoice_email/data/auto_send_cron.xml new file mode 100644 index 00000000000..df3cd9ba4cd --- /dev/null +++ b/auto_invoice_email/data/auto_send_cron.xml @@ -0,0 +1,12 @@ + + + Auto Send Posted Invoices + + + code + model._auto_send_invoices() + 1 + days + + + diff --git a/auto_invoice_email/models/__init__.py b/auto_invoice_email/models/__init__.py new file mode 100644 index 00000000000..0c548851701 --- /dev/null +++ b/auto_invoice_email/models/__init__.py @@ -0,0 +1,4 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import res_config_settings +from . import account_move diff --git a/auto_invoice_email/models/account_move.py b/auto_invoice_email/models/account_move.py new file mode 100644 index 00000000000..b35e747ba2d --- /dev/null +++ b/auto_invoice_email/models/account_move.py @@ -0,0 +1,22 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models, api +from datetime import datetime, timedelta + + +class AccountMove(models.Model): + _inherit = "account.move" + + @api.model + def _auto_send_invoices(self): + # Using sudo to access system parameters that are not user-specific and may be restricted + days = int(self.env["ir.config_parameter"].sudo().get_param("auto_invoice_email.days", default=0)) + if days <= 0: + return + + target_date = datetime.today().date() - timedelta(days=days) + invoices = self.search([("state", "=", "posted"), ("invoice_date", "=", target_date), ("move_type", "in", ("out_invoice", "out_refund"))]) + template = self.env.ref("account.email_template_edi_invoice") + for invoice in invoices: + template.with_context(force_send=True).send_mail(invoice.id, force_send=True) + invoice.message_post(body="Invoice automatically sent by cron job.", message_type="comment") diff --git a/auto_invoice_email/models/res_config_settings.py b/auto_invoice_email/models/res_config_settings.py new file mode 100644 index 00000000000..b69152009d8 --- /dev/null +++ b/auto_invoice_email/models/res_config_settings.py @@ -0,0 +1,29 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models, fields, api + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + auto_send_invoice_days = fields.Integer(string="Send invoices after (days)") + + def set_values(self): + super().set_values() + self.env["ir.config_parameter"].sudo().set_param( + "auto_invoice_email.days", self.auto_send_invoice_days + ) + + @api.model + def get_values(self): + res = super().get_values() + res.update( + { + "auto_send_invoice_days": int( + self.env["ir.config_parameter"] + .sudo() + .get_param("auto_invoice_email.days", default=0) + ) + } + ) + return res diff --git a/auto_invoice_email/views/res_config_settings_views.xml b/auto_invoice_email/views/res_config_settings_views.xml new file mode 100644 index 00000000000..62d7d702fdb --- /dev/null +++ b/auto_invoice_email/views/res_config_settings_views.xml @@ -0,0 +1,14 @@ + + + res.config.settings.inherit.auto.invoice + res.config.settings + + + + + + + + + +