Skip to content

Commit

Permalink
Merge PR #1382 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by simahawk
  • Loading branch information
OCA-git-bot committed Nov 11, 2024
2 parents 768ee19 + bb4e1b6 commit 8c9e135
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 0 deletions.
1 change: 1 addition & 0 deletions account_invoice_auto_send_by_email/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
17 changes: 17 additions & 0 deletions account_invoice_auto_send_by_email/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2023 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

{
"name": "Account Invoice Auto Send By Email",
"summary": "Invoice with the email transmit method are send automatically.",
"version": "14.0.1.0.0",
"category": "Accounting",
"author": "Camptocamp, Odoo Community Association (OCA)",
"license": "AGPL-3",
"depends": ["account", "account_invoice_transmit_method", "queue_job"],
"website": "https://github.com/OCA/account-invoicing",
"data": [
"data/ir_cron.xml",
],
"installable": True,
}
18 changes: 18 additions & 0 deletions account_invoice_auto_send_by_email/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 Camptocamp SA
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo noupdate="1">

<record model="ir.cron" id="cron_send_email_invoice">
<field name='name'>Account Invoice: Send email invoice</field>
<field name='interval_number'>1</field>
<field name='interval_type'>days</field>
<field name="numbercall">-1</field>
<field name="active" eval="True" />
<field name="nextcall" eval="datetime.now().replace(hour=23, minute=0, second=0)" />
<field name="model_id" ref="account.model_account_move" />
<field name="state">code</field>
<field name="code">model.cron_send_email_invoice()</field>
</record>

</odoo>
2 changes: 2 additions & 0 deletions account_invoice_auto_send_by_email/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import account_move
from . import account_invoice_send
15 changes: 15 additions & 0 deletions account_invoice_auto_send_by_email/models/account_invoice_send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
from odoo import models


class AccountInvoiceSend(models.TransientModel):
_inherit = "account.invoice.send"

def _get_ui_options(self):
return {
"is_print": self.is_print,
"is_email": self.is_email,
"template_id": self.template_id.id,
"composition_mode": "mass_mail",
}
36 changes: 36 additions & 0 deletions account_invoice_auto_send_by_email/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2023 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo import _, api, models


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

def cron_send_email_invoice(self):
invoices = self.search(self._email_invoice_to_send_domain())
for invoice in invoices:
description = "Send invoice {} by email".format(invoice.name)
invoice.with_delay(description=description)._execute_invoice_sent_wizard()

def _execute_invoice_sent_wizard(self, options=None):
self.ensure_one()
if self.is_move_sent:
return _("This invoice has already been sent.")
res = self.action_invoice_sent()
wiz_ctx = res["context"] or {}
wiz_ctx["active_model"] = self._name
wiz_ctx["active_ids"] = self.ids
wiz = self.env["account.invoice.send"].with_context(**wiz_ctx).create({})
wiz.write(wiz._get_ui_options())
return wiz.send_and_print_action()

@api.model
def _email_invoice_to_send_domain(self):
return [
("move_type", "in", ("out_invoice", "out_refund")),
("state", "=", "posted"),
("is_move_sent", "=", False),
("transmit_method_code", "=", "mail"),
("payment_state", "=", "not_paid"),
]
Empty file.
2 changes: 2 additions & 0 deletions account_invoice_auto_send_by_email/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module will on a daily basis send all invoices with `email` as transmit method.
That are posted and not yet send. The email is send with the use of queue job.
1 change: 1 addition & 0 deletions account_invoice_auto_send_by_email/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_account_invoice_auto_send_by_email
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Copyright 2023 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
import mock

from odoo.tests.common import SavepointCase


class TestAccountInvoiceAutoSendByEmail(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(
context=dict(
cls.env.context, tracking_disable=True, test_queue_job_no_delay=True
)
)

cls.AccountMove = cls.env["account.move"]
cls.AccountMove.search([]).write({"transmit_method_code": ""})
cls.company = cls.env.user.company_id
cls.transmit_method = cls.env.ref("account_invoice_transmit_method.mail")
cls.env["account.journal"].create(
{"name": "Test sale journal", "type": "sale", "code": "tsj"}
)
cls.customer = cls.env.ref("base.res_partner_1")
cls.receivable_account = cls.env["account.account"].search(
[
(
"user_type_id",
"=",
cls.env.ref("account.data_account_type_receivable").id,
),
("company_id", "=", cls.env.company.id),
],
limit=1,
)
cls.income_account = cls.env["account.account"].search(
[
(
"user_type_id",
"=",
cls.env.ref("account.data_account_type_current_liabilities").id,
),
("company_id", "=", cls.env.company.id),
],
limit=1,
)
cls.bank = cls.env.ref("base.res_bank_1")
cls.partner_bank = cls.env["res.partner.bank"].create(
{
"bank_id": cls.bank.id,
"acc_number": "300.300.300",
"acc_holder_name": "AccountHolderName",
"partner_id": cls.company.partner_id.id,
}
)
cls.invoice = cls.AccountMove.create(
{
"move_type": "out_invoice",
"partner_id": cls.customer.id,
"partner_bank_id": cls.partner_bank.id,
"transmit_method_id": cls.transmit_method.id,
"line_ids": [
(
0,
0,
{
"quantity": 3,
"price_unit": 4.0,
"debit": 12,
"credit": 0,
"name": "Some service",
"account_id": cls.receivable_account.id,
},
),
(
0,
0,
{
"debit": 0,
"credit": 12,
"name": "inv",
"account_id": cls.income_account.id,
},
),
],
}
)
cls.invoice.action_post()
cls.invoice.write({"payment_state": "not_paid"})

@mock.patch(
"odoo.addons.base.models.ir_actions_report.IrActionsReport._render_qweb_pdf"
)
def test_send_email_invoice_cron(self, mocked):
# We don't care about the content of the invoice report
mocked.return_value = (b"Whatever gets printed", "pdf")
moves = self.AccountMove.search(
self.AccountMove._email_invoice_to_send_domain()
)
self.assertEqual(len(moves), 1)
self.AccountMove.cron_send_email_invoice()
moves = self.AccountMove.search(
self.AccountMove._email_invoice_to_send_domain()
)
self.assertEqual(len(moves), 0)
self.assertTrue(self.invoice.is_move_sent)

def test_invoice_not_send_multiple_time(self):
self.invoice.is_move_sent = True
res = self.invoice._execute_invoice_sent_wizard()
self.assertEqual(res, "This invoice has already been sent.")
6 changes: 6 additions & 0 deletions setup/account_invoice_auto_send_by_email/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 8c9e135

Please sign in to comment.