diff --git a/addons/account/views/account_invoice_view.xml b/addons/account/views/account_invoice_view.xml
index d09d1d7cd01..41f7de14856 100644
--- a/addons/account/views/account_invoice_view.xml
+++ b/addons/account/views/account_invoice_view.xml
@@ -352,7 +352,7 @@
-
+
diff --git a/addons/l10n_ch/__init__.py b/addons/l10n_ch/__init__.py
index 9360fc1f834..b12f4e83744 100644
--- a/addons/l10n_ch/__init__.py
+++ b/addons/l10n_ch/__init__.py
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
+import models
+
from odoo import api, SUPERUSER_ID
def load_translations(cr, registry):
diff --git a/addons/l10n_ch/models/__init__.py b/addons/l10n_ch/models/__init__.py
index 4ff6e00d8ce..fe19ba43cfb 100644
--- a/addons/l10n_ch/models/__init__.py
+++ b/addons/l10n_ch/models/__init__.py
@@ -2,6 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import account_invoice
+import mail_compose_message
import mail_template
import res_bank
import res_company
diff --git a/addons/l10n_ch/models/account_invoice.py b/addons/l10n_ch/models/account_invoice.py
index b1807906dae..51bad251178 100644
--- a/addons/l10n_ch/models/account_invoice.py
+++ b/addons/l10n_ch/models/account_invoice.py
@@ -3,7 +3,8 @@
import re
-from odoo import models, fields, api
+from odoo import models, fields, api, _
+from odoo.exceptions import ValidationError
from odoo.tools.float_utils import float_split_str
from odoo.tools.misc import mod10r
@@ -24,6 +25,9 @@ class AccountInvoice(models.Model):
l10n_ch_isr_valid = fields.Boolean(compute='_compute_l10n_ch_isr_valid', help='Boolean value. True iff all the data required to generate the ISR are present')
+ l10n_ch_isr_sent = fields.Boolean(defaut=False, help="Boolean value telling whether or not the ISR corresponding to this invoice has already been printed or sent by mail.")
+ l10n_ch_currency_name = fields.Char(related='currency_id.name', help="The name of this invoice's currency") #This field is used in the "invisible" condition field of the 'Print ISR' button.
+
@api.depends('partner_bank_id.bank_id.l10n_ch_postal_eur', 'partner_bank_id.bank_id.l10n_ch_postal_chf')
def _compute_l10n_ch_isr_postal(self):
""" Computes the postal reference identifying the bank managing this ISR and formats it accordingly"""
@@ -125,15 +129,40 @@ class AccountInvoice(models.Model):
record.l10n_ch_isr_postal and \
record.partner_bank_id and \
record.partner_bank_id.l10n_ch_postal and \
- record.currency_id.name in ['EUR', 'CHF']
+ record.l10n_ch_currency_name in ['EUR', 'CHF']
- def invoice_print(self):
- """ Triggered by the 'print invoice' button. When printing the invoice, also print
- the ISR if the invoice is compatible.
+ def split_total_amount(self):
+ """ Splits the total amount of this invoice in two parts, using the dot as
+ a separator, and taking two precision digits (always displayed).
+ These two parts are returned as the two elements of a tuple, as strings
+ to print in the report.
+
+ This function is needed on the model, as it must be called in the report
+ template, which cannot reference static functions
+ """
+ return float_split_str(self.amount_total, 2)
+
+ def isr_print(self):
+ """ Triggered by the 'Print ISR' button.
"""
self.ensure_one()
- invoice_report = super(AccountInvoice, self).invoice_print()
if self.l10n_ch_isr_valid:
- isr_report = self.env['report'].get_action(self, 'l10n_ch.isr_report_main')
- invoice_report['next_report_to_generate'] = isr_report
- return invoice_report
+ self.l10n_ch_isr_sent = True
+ return self.env['report'].get_action(self, 'l10n_ch.isr_report_main')
+ else:
+ raise ValidationError(_("""You cannot generate an ISR yet.\n
+ For this, you need to :\n
+ - set a valid postal account number (or an IBAN referencing one) for your company\n
+ - define its bank\n
+ - associate this bank with a postal reference for the currency used in this invoice\n
+ - fill the 'bank account' field of the invoice with the postal to be used to receive the related payment. A default account will be automatically set for all invoices created after you defined a postal account for your company."""))
+
+ def action_invoice_sent(self):
+ """ Overridden. Triggered by the 'send by mail' button.
+ """
+ rslt = super(AccountInvoice, self).action_invoice_sent()
+
+ if self.l10n_ch_isr_valid:
+ rslt['context']['l10n_ch_mark_isr_as_sent'] = True
+
+ return rslt
diff --git a/addons/l10n_ch/models/mail_compose_message.py b/addons/l10n_ch/models/mail_compose_message.py
new file mode 100644
index 00000000000..652571492a7
--- /dev/null
+++ b/addons/l10n_ch/models/mail_compose_message.py
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+
+
+class MailComposer(models.TransientModel):
+ _inherit = 'mail.compose.message'
+
+ @api.multi
+ def send_mail(self, auto_commit=False):
+ """ Method overriden to mark ISR as sent once a mail containing
+ it in attachment has been sent.
+ """
+ context = self._context
+ if context.get('default_model') == 'account.invoice' and \
+ context.get('default_res_id') and \
+ context.get('l10n_ch_mark_isr_as_sent', False):
+
+ invoice = self.env['account.invoice'].browse(context['default_res_id'])
+ invoice = invoice.with_context(mail_post_autofollow=True)
+ invoice.l10n_ch_isr_sent = True
+ invoice.message_post(body=_("ISR sent"))
+
+ return super(MailComposer, self).send_mail(auto_commit=auto_commit)
\ No newline at end of file
diff --git a/addons/l10n_ch/tests/test_l10n_ch_isr.py b/addons/l10n_ch/tests/test_l10n_ch_isr.py
index b646454e3a8..c7722e1a7d0 100644
--- a/addons/l10n_ch/tests/test_l10n_ch_isr.py
+++ b/addons/l10n_ch/tests/test_l10n_ch_isr.py
@@ -4,6 +4,7 @@
import time
from odoo.addons.account.tests.account_test_classes import AccountingTestCase
+from odoo.exceptions import ValidationError
class ISRTest(AccountingTestCase):
@@ -45,23 +46,20 @@ class ISRTest(AccountingTestCase):
'acc_number': number
})
+ def print_isr(self, invoice):
+ try:
+ invoice.isr_print()
+ return True
+ except ValidationError:
+ return False
+
def isr_not_generated(self, invoice):
""" Prints the given invoice and tests that no ISR generation is triggered. """
- next_action = invoice.invoice_print()
- iterated_actions = []
- while next_action and next_action not in iterated_actions:
- self.assertNotEqual(next_action['report_name'], 'l10n_ch.isr_report_main', 'No ISR should be generated for this invoice')
- next_action = next_action.get('next_report_to_generate', None)
+ self.assertFalse(self.print_isr(invoice), 'No ISR should be generated for this invoice')
def isr_generated(self, invoice):
""" Prints the given invoice and tests that an ISR generation is triggered. """
- next_action = invoice.invoice_print()
- iterated_actions = []
- isr_found = False
- while next_action and next_action not in iterated_actions and not isr_found:
- isr_found = next_action['report_name'] == 'l10n_ch.isr_report_main'
- next_action = next_action.get('next_report_to_generate', None)
- self.assertTrue(isr_found, 'An ISR should have been generated')
+ self.assertTrue(self.print_isr(invoice), 'An ISR should have been generated')
def test_l10n_ch_postals(self):
#An account whose number is set to a valid postal number becomes a 'postal'
diff --git a/addons/l10n_ch/views/account_invoice_view.xml b/addons/l10n_ch/views/account_invoice_view.xml
index af02687f4de..e550670de85 100644
--- a/addons/l10n_ch/views/account_invoice_view.xml
+++ b/addons/l10n_ch/views/account_invoice_view.xml
@@ -9,6 +9,36 @@
0
+
+
+
+
+
+
+
+
+
+
+
+
+