intrastat_base: Remove intrastat.common class

This commit is contained in:
Alexis de Lattre
2021-12-15 12:49:02 +01:00
parent 2c384b6761
commit aecf24f99f
9 changed files with 180 additions and 3 deletions

View File

@@ -1,2 +1,3 @@
from . import models
from . import report
from . import wizards

View File

@@ -35,6 +35,7 @@
"views/account_move.xml",
"views/sale_order.xml",
"views/stock_warehouse.xml",
"wizards/intrastat_result_view.xml",
"data/intrastat_transport_mode.xml",
"data/intrastat_unit.xml",
],

View File

@@ -20,7 +20,7 @@ class IntrastatProductDeclaration(models.Model):
_name = "intrastat.product.declaration"
_description = "Intrastat Product Report Base Object"
_rec_name = "year_month"
_inherit = ["mail.thread", "mail.activity.mixin", "intrastat.common"]
_inherit = ["mail.thread", "mail.activity.mixin"]
_order = "year_month desc, declaration_type, revision"
_sql_constraints = [
(
@@ -41,6 +41,62 @@ class IntrastatProductDeclaration(models.Model):
)
return res
company_id = fields.Many2one(
comodel_name="res.company",
string="Company",
required=True,
states={"done": [("readonly", True)]},
default=lambda self: self.env.company,
)
company_country_code = fields.Char(
compute="_compute_company_country_code",
string="Company Country Code",
readonly=True,
store=True,
)
state = fields.Selection(
selection=[("draft", "Draft"), ("done", "Done")],
string="State",
readonly=True,
tracking=True,
copy=False,
default="draft",
help="State of the declaration. When the state is set to 'Done', "
"the parameters become read-only.",
)
note = fields.Text(
string="Notes", help="You can add some comments here if you want."
)
year = fields.Char(
string="Year", required=True, states={"done": [("readonly", True)]}
)
month = fields.Selection(
selection=[
("01", "01"),
("02", "02"),
("03", "03"),
("04", "04"),
("05", "05"),
("06", "06"),
("07", "07"),
("08", "08"),
("09", "09"),
("10", "10"),
("11", "11"),
("12", "12"),
],
string="Month",
required=True,
states={"done": [("readonly", True)]},
)
year_month = fields.Char(
compute="_compute_year_month",
string="Period",
readonly=True,
tracking=True,
store=True,
help="Year and month of the declaration.",
)
declaration_type = fields.Selection(
selection="_get_declaration_type",
string="Type",
@@ -127,12 +183,32 @@ class IntrastatProductDeclaration(models.Model):
("nihil", _("Nihil")),
]
@api.depends("company_id")
def _compute_company_country_code(self):
for this in self:
if this.company_id:
if not this.company_id.country_id:
raise ValidationError(_("You must set company's country !"))
this.company_country_code = this.company_id.country_id.code.lower()
@api.depends("year", "month")
def _compute_year_month(self):
for this in self:
if this.year and this.month:
this.year_month = "-".join([this.year, this.month])
@api.depends("month")
def _compute_check_validity(self):
""" TO DO: logic based upon computation lines """
for this in self:
this.valid = True
@api.constrains("year")
def _check_year(self):
for this in self:
if len(this.year) != 4 or this.year[0] != "2":
raise ValidationError(_("Invalid Year!"))
@api.onchange("declaration_type")
def _onchange_declaration_type(self):
if self.declaration_type == "arrivals":
@@ -160,6 +236,36 @@ class IntrastatProductDeclaration(models.Model):
msg, action.id, _("Go to Accounting Configuration Settings screen")
)
def _attach_xml_file(self, xml_bytes, declaration_name):
"""Attach the XML file to the report_intrastat_product/service
object"""
self.ensure_one()
filename = "{}_{}.xml".format(self.year_month, declaration_name)
attach = self.env["ir.attachment"].create(
{
"name": filename,
"res_id": self.id,
"res_model": self._name,
"raw": xml_bytes,
}
)
return attach.id
def _unlink_attachments(self):
atts = self.env["ir.attachment"].search(
[("res_model", "=", self._name), ("res_id", "=", self.id)]
)
atts.unlink()
def unlink(self):
for this in self:
if this.state == "done":
raise UserError(
_("Cannot delete the declaration %s because it is in Done state.")
% this.display_name
)
return super().unlink()
def _get_partner_country(self, inv_line, notedict, eu_countries):
inv = inv_line.move_id
country = inv.src_dest_country_id or inv.partner_id.country_id
@@ -712,7 +818,7 @@ class IntrastatProductDeclaration(models.Model):
self.write(vals)
if vals["note"]:
result_view = self.env.ref("intrastat_base.intrastat_result_view_form")
result_view = self.env.ref("intrastat_product.intrastat_result_view_form")
return {
"name": _("Generate lines from invoices: results"),
"view_type": "form",
@@ -809,6 +915,14 @@ class IntrastatProductDeclaration(models.Model):
cl.write({"declaration_line_id": declaration_line.id})
return True
def _check_generate_xml(self):
self.ensure_one()
if not self.company_id.partner_id.vat:
raise UserError(
_("The VAT number is not set for the partner '%s'.")
% self.company_id.partner_id.display_name
)
def generate_xml(self):
""" generate the INTRASTAT Declaration XML file """
self.ensure_one()

View File

@@ -12,3 +12,4 @@ access_account_move_intrastat_line,Full access on Invoice Intrastat Lines,model_
access_intrastat_product_declaration,Full access on Intrastat Product Declarations to Accountant,model_intrastat_product_declaration,account.group_account_user,1,1,1,1
access_intrastat_product_computation_line,Full access on Intrastat Product Computation Lines to Accountant,model_intrastat_product_computation_line,account.group_account_user,1,1,1,1
access_intrastat_product_declaration_line,Full access on Intrastat Product Declaration Lines to Accountant,model_intrastat_product_declaration_line,account.group_account_user,1,1,1,1
access_intrastat_result_view,Access on intrastat.result.view,model_intrastat_result_view,account.group_account_user,1,1,1,0
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
12 access_intrastat_product_declaration Full access on Intrastat Product Declarations to Accountant model_intrastat_product_declaration account.group_account_user 1 1 1 1
13 access_intrastat_product_computation_line Full access on Intrastat Product Computation Lines to Accountant model_intrastat_product_computation_line account.group_account_user 1 1 1 1
14 access_intrastat_product_declaration_line Full access on Intrastat Product Declaration Lines to Accountant model_intrastat_product_declaration_line account.group_account_user 1 1 1 1
15 access_intrastat_result_view Access on intrastat.result.view model_intrastat_result_view account.group_account_user 1 1 1 0

View File

@@ -107,6 +107,7 @@ class IntrastatProductCommon(IntrastatCommon):
def _create_declaration(cls, vals=None):
values = {
"company_id": cls.env.company.id,
"declaration_type": "dispatches",
}
if vals is not None:
values.update(vals)

View File

@@ -4,7 +4,7 @@ from psycopg2 import IntegrityError
from odoo.tests.common import SavepointCase
from odoo.tools import mute_logger
from odoo.exceptions import UserError, ValidationError
from .common import IntrastatProductCommon
@@ -39,6 +39,28 @@ class TestIntrastatProduct(IntrastatProductCommon):
decl_copy = self.declaration.copy()
self.assertEqual(self.declaration.revision + 1, decl_copy.revision)
def test_declaration_no_country(self):
self.demo_company.country_id = False
with self.assertRaises(ValidationError):
self._create_declaration()
self.declaration.flush()
def test_declaration_no_vat(self):
self.demo_company.partner_id.vat = False
with self.assertRaises(UserError):
self._create_declaration()
self.declaration.flush()
self.declaration._check_generate_xml()
def test_declaration_state(self):
self._create_declaration()
self.declaration.unlink()
self._create_declaration()
self.declaration.state = "done"
with self.assertRaises(UserError):
self.declaration.unlink()
class TestIntrastatProductCase(TestIntrastatProduct, SavepointCase):
""" Test Intrastat Product """

View File

@@ -0,0 +1 @@
from . import intrastat_result_view

View File

@@ -0,0 +1,14 @@
# Copyright 2010-2021 Akretion (<alexis.delattre@akretion.com>)
# Copyright 2009-2021 Noviat (http://www.noviat.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import fields, models
class IntrastatResultView(models.TransientModel):
_name = "intrastat.result.view"
_description = "Pop-up to show errors on intrastat report generation"
note = fields.Text(
string="Notes", readonly=True, default=lambda self: self._context.get("note")
)

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2011-2021 Akretion France (http://www.akretion.com/)
Copyright 2015-2021 Noviat (http://www.noviat.com/)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<odoo>
<record id="intrastat_result_view_form" model="ir.ui.view">
<field name="name">intrastat.result_view_form</field>
<field name="model">intrastat.result.view</field>
<field name="arch" type="xml">
<form string="Intrastat Result View">
<group name="main">
<field name="note" nolabel="1" />
</group>
<footer>
<button string="Ok" class="btn-primary" special="cancel" />
</footer>
</form>
</field>
</record>
</odoo>