Skip to content

[ADD] invoice_send_automatic: create automated action for sending mail #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 18.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions invoice_send_automatic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
9 changes: 9 additions & 0 deletions invoice_send_automatic/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Automated Action Send Invoice Sales",
"version": "1.0",
"depends": ["base", "sale_management", "accountant"],
"data": ["views/res_config_settings_views.xml", "data/ir_cron.xml"],
"sequence": 1,
"application": True,
"license": "OEEL-1",
}
13 changes: 13 additions & 0 deletions invoice_send_automatic/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="ir_cron_automatic_invoice" model="ir.cron">
<field name="name">Automatic Send Invoice after due days</field>
<field name="model_id" ref="account.model_account_move" />
<field name="user_id" ref="base.user_root" />
<field name="state">code</field>
<field name="code">model.method_to_send_invoice_automatically()</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="active" eval="True" />
</record>
</odoo>
2 changes: 2 additions & 0 deletions invoice_send_automatic/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import res_config_settings
from . import account_move
32 changes: 32 additions & 0 deletions invoice_send_automatic/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from odoo import api, models
from datetime import date, timedelta


class AccountMove(models.Model):
_inherit = "account.move"

def _send_invoice_email(self):
self.ensure_one()

template = self.env.ref("account.email_template_edi_invoice")
template.send_mail(self.id)
self.message_post(body="Invoice automatically sent by cron job.")

@api.model
def method_to_send_invoice_automatically(self):
automatic_send_invoice_days = int(
self.env["ir.config_parameter"]
.sudo()
.get_param("invoice_send_automatic.automatic_send_invoice_days")
)
target_invoice_date = date.today() - timedelta(days=automatic_send_invoice_days)

invoices_to_send = self.search(
[
("state", "=", "posted"),
("move_type", "in", ("out_invoice", "out_refund")),
("invoice_date", "=", target_invoice_date),
]
)
for invoice in invoices_to_send:
invoice._send_invoice_email()
11 changes: 11 additions & 0 deletions invoice_send_automatic/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

automatic_send_invoice_days = fields.Integer(
related="company_id.account_tax_periodicity_reminder_day",
readonly=False,
config_parameter="invoice_send_automatic.automatic_send_invoice_days",
)
21 changes: 21 additions & 0 deletions invoice_send_automatic/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record id="res_config_settings_view_form_inherit" model="ir.ui.view">
<field name="name">res.config.settings.inherit.view.form</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="account.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//block//setting[@id='total_amount_words']"
position="after">
<setting id="automatic_send_invoice" help="Duration in days after the invoice" string="Send invoice by email ">
<div>
<field name="automatic_send_invoice_days" />
<span>days</span>
</div>
</setting>
</xpath>
</field>
</record>

</odoo>