Skip to content

Commit 90f9b11

Browse files
committed
[ADD] auto_invoice_email: Auto-send posted invoices from the configured days
- Modified the cron logic to generate and attach the invoice PDF when sending email. - Ensured the email includes the invoice PDF using QWeb report. - Posted a message in the chatter with or without attachment, depending on availability. - Added fallback logic to handle missing attachments gracefully.
1 parent fbf9ee9 commit 90f9b11

File tree

7 files changed

+98
-0
lines changed

7 files changed

+98
-0
lines changed

auto_invoice_email/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Part of Odoo. See LICENSE file for full copyright and licensing details.
2+
3+
from . import models

auto_invoice_email/__manifest__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Auto Invoice Email",
3+
"version": "1.0",
4+
"summary": "Automatically send posted invoices by email after configured days",
5+
"category": "Accounting",
6+
"author": "Rohit",
7+
"depends": ["account"],
8+
"data": [
9+
"views/res_config_settings_views.xml",
10+
"data/auto_send_cron.xml",
11+
],
12+
"installable": True,
13+
"license": "LGPL-3",
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<odoo>
2+
<record id="ir_cron_auto_send_invoices" model="ir.cron">
3+
<field name="name">Auto Send Posted Invoices</field>
4+
<field name="model_id" ref="account.model_account_move"/>
5+
<field name="user_id" ref="base.user_root" />
6+
<field name="state">code</field>
7+
<field name="code">model._auto_send_invoices()</field>
8+
<field name="interval_number">1</field>
9+
<field name="interval_type">days</field>
10+
<field name="active" eval="True" />
11+
</record>
12+
</odoo>

auto_invoice_email/models/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Part of Odoo. See LICENSE file for full copyright and licensing details.
2+
3+
from . import res_config_settings
4+
from . import account_move
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Part of Odoo. See LICENSE file for full copyright and licensing details.
2+
3+
from odoo import models, api
4+
from datetime import datetime, timedelta
5+
6+
7+
class AccountMove(models.Model):
8+
_inherit = "account.move"
9+
10+
@api.model
11+
def _auto_send_invoices(self):
12+
# Using sudo to access system parameters that are not user-specific and may be restricted
13+
days = int(self.env["ir.config_parameter"].sudo().get_param("auto_invoice_email.days", default=0))
14+
if days <= 0:
15+
return
16+
17+
target_date = datetime.today().date() - timedelta(days=days)
18+
invoices = self.search([("state", "=", "posted"), ("invoice_date", "=", target_date), ("move_type", "in", ("out_invoice", "out_refund"))])
19+
template = self.env.ref("account.email_template_edi_invoice")
20+
for invoice in invoices:
21+
template.with_context(force_send=True).send_mail(invoice.id, force_send=True)
22+
invoice.message_post(body="Invoice automatically sent by cron job.", message_type="comment")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Part of Odoo. See LICENSE file for full copyright and licensing details.
2+
3+
from odoo import models, fields, api
4+
5+
6+
class ResConfigSettings(models.TransientModel):
7+
_inherit = "res.config.settings"
8+
9+
auto_send_invoice_days = fields.Integer(string="Send invoices after (days)")
10+
11+
def set_values(self):
12+
super().set_values()
13+
self.env["ir.config_parameter"].sudo().set_param(
14+
"auto_invoice_email.days", self.auto_send_invoice_days
15+
)
16+
17+
@api.model
18+
def get_values(self):
19+
res = super().get_values()
20+
res.update(
21+
{
22+
"auto_send_invoice_days": int(
23+
self.env["ir.config_parameter"]
24+
.sudo()
25+
.get_param("auto_invoice_email.days", default=0)
26+
)
27+
}
28+
)
29+
return res
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<odoo>
2+
<record id="view_res_config_settings_inherit_auto_invoice" model="ir.ui.view">
3+
<field name="name">res.config.settings.inherit.auto.invoice</field>
4+
<field name="model">res.config.settings</field>
5+
<field name="inherit_id" ref="account.res_config_settings_view_form"/>
6+
<field name="arch" type="xml">
7+
<xpath expr="//block[@id='invoicing_settings']" position="inside">
8+
<setting id="auto_send_invoice_setting" help="Send invoices automatically after X days">
9+
<field name="auto_send_invoice_days" placeholder="Enter days"/>
10+
</setting>
11+
</xpath>
12+
</field>
13+
</record>
14+
</odoo>

0 commit comments

Comments
 (0)