Skip to content

Commit 0e613c1

Browse files
committed
[ADD] account: Auto-send posted invoices older than configured days
The motivation behind this PR: - To automate the process of sending posted invoices via email for all invoices older than a configurable number of days. This ensures timely communication with customers without requiring manual intervention. The behavior before this PR: - Only invoices with an exact `invoice_date` equal to today's date minus the configured number of days (`= target_date`) were selected. Older invoices were skipped, even if they were still unsent. The desired behavior after this PR: - Now, the system selects all posted invoices where `invoice_date` is less than the calculated `target_date`, allowing pending older invoices to be sent automatically via the scheduled cron job.
1 parent fbf9ee9 commit 0e613c1

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

auto_invoice_email/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import res_config_settings
2+
from . import account_move
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from odoo import models, api
2+
from datetime import datetime, timedelta
3+
4+
5+
class AccountMove(models.Model):
6+
_inherit = "account.move"
7+
8+
@api.model
9+
def _auto_send_invoices(self):
10+
days = int(
11+
self.env["ir.config_parameter"]
12+
.sudo()
13+
.get_param("auto_invoice_email.days", default=0)
14+
)
15+
if days <= 0:
16+
return
17+
18+
target_date = datetime.today().date() - timedelta(days=days)
19+
invoices = self.search(
20+
[
21+
("state", "=", "posted"),
22+
("invoice_date", "=", target_date),
23+
("move_type", "in", ("out_invoice", "out_refund")),
24+
]
25+
)
26+
template = self.env.ref("account.email_template_edi_invoice")
27+
for invoice in invoices:
28+
template.with_context(force_send=True).send_mail(
29+
invoice.id, force_send=True
30+
)
31+
invoice.message_post(body="Invoice automatically sent by cron job.", message_type="comment")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from odoo import models, fields, api
2+
3+
4+
class ResConfigSettings(models.TransientModel):
5+
_inherit = "res.config.settings"
6+
7+
auto_send_invoice_days = fields.Integer(string="Send invoices after (days)")
8+
9+
def set_values(self):
10+
super().set_values()
11+
self.env["ir.config_parameter"].sudo().set_param(
12+
"auto_invoice_email.days", self.auto_send_invoice_days
13+
)
14+
15+
@api.model
16+
def get_values(self):
17+
res = super().get_values()
18+
res.update(
19+
{
20+
"auto_send_invoice_days": int(
21+
self.env["ir.config_parameter"]
22+
.sudo()
23+
.get_param("auto_invoice_email.days", default=0)
24+
)
25+
}
26+
)
27+
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)