Skip to content

Commit

Permalink
Merge PR #179 into 13.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Aug 9, 2022
2 parents c1bc3c4 + 446dcb3 commit 0efb503
Show file tree
Hide file tree
Showing 16 changed files with 628 additions and 94 deletions.
1 change: 1 addition & 0 deletions intrastat_base/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import res_company
from . import res_config_settings
from . import intrastat_common
from . import res_partner
67 changes: 67 additions & 0 deletions intrastat_base/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2022 Noviat.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo.exceptions import UserError

XI_COUNTY_NAMES = [
"antrim",
"armagh",
"down",
"fermanagh",
"londonderry",
"tyrone",
"northern ireland",
]

XI_COUNTIES = [
"base.state_uk18", # County Antrim
"base.state_uk19", # County Armagh
"base.state_uk20", # County Down
"base.state_uk22", # County Fermanagh
"base.state_uk23", # County Londonderry
"base.state_uk24", # County Tyrone
"base.state_ie_27", # Antrim
"base.state_ie_28", # Armagh
"base.state_ie_29", # Down
"base.state_ie_30", # Fermanagh
"base.state_ie_31", # Londonderry
"base.state_ie_32", # Tyrone
]


class ResPartner(models.Model):
_inherit = "res.partner"

@api.model
def _get_xi_counties(self):
return [self.env.ref(x) for x in XI_COUNTIES]

@api.model
def _get_xu_counties(self):
uk_counties = self.env.ref("base.uk").state_ids
xu_counties = uk_counties.filtered(lambda r: r not in self._get_xi_counties())
return xu_counties

def _get_intrastat_country_code(self, country=None, state=None):
if self:
self.ensure_one()
country = self.country_id
state = self.state_id
else:
state = state or self.env["res.country.state"]
country = country or state.country_id
if not country:
raise UserError(
_("Programming Error when calling '_get_intrastat_country_code()")
)
cc = country.code
if cc == "GB":
cc = "XU"
if state and cc in ["XU", "IE"]:
if (
state in self._get_xi_counties()
or state.name.lower().strip() in XI_COUNTY_NAMES
):
cc = "XI"
return cc
16 changes: 16 additions & 0 deletions intrastat_base/tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2022 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


class IntrastatCommon(object):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.chart_template_obj = cls.env["account.chart.template"]
cls.mail_obj = cls.env["mail.mail"]

cls.demo_user = cls.env.ref("base.user_demo")
cls.demo_company = cls.env.ref("base.main_company")

cls.shipping_cost = cls.env.ref("intrastat_base.shipping_costs_exclude")
5 changes: 5 additions & 0 deletions intrastat_product/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ Contributors
* Alexis de Lattre, Akretion <alexis.delattre@akretion.com>
* Luc De Meyer, Noviat <info@noviat.com>


* `Tecnativa <https://www.tecnativa.com>`_:

* Víctor Martínez

Maintainers
~~~~~~~~~~~

Expand Down
46 changes: 43 additions & 3 deletions intrastat_product/models/account_move.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2011-2017 Akretion France (http://www.akretion.com)
# Copyright 2009-2020 Noviat (http://www.noviat.com)
# Copyright 2011-2020 Akretion France (http://www.akretion.com)
# Copyright 2009-2022 Noviat (http://www.noviat.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# @author Luc de Meyer <info@noviat.com>

Expand Down Expand Up @@ -84,19 +84,32 @@ def compute_intrastat_lines(self):

def _get_intrastat_line_vals(self, line):
vals = {}
notedict = {
"note": "",
"line_nbr": 0,
}
decl_model = self.env["intrastat.product.declaration"]
if decl_model._is_product(line):
hs_code = line.product_id.get_hs_code_recursively()
if not hs_code:
return vals
weight, qty = decl_model._get_weight_and_supplunits(line, hs_code)
weight, qty = decl_model._get_weight_and_supplunits(line, hs_code, notedict)
product_country = line.product_id.origin_country_id
product_state = line.product_id.origin_state_id
country = product_country or product_state.country_id
product_origin_country_code = "QU"
if country:
product_origin_country_code = self.env[
"res.partner"
]._get_intrastat_country_code(product_country, product_state)
vals.update(
{
"invoice_line_id": line.id,
"hs_code_id": hs_code.id,
"transaction_weight": int(weight),
"transaction_suppl_unit_qty": qty,
"product_origin_country_id": line.product_id.origin_country_id.id,
"product_origin_country_code": product_origin_country_code,
}
)
return vals
Expand Down Expand Up @@ -158,11 +171,23 @@ class AccountMoveIntrastatLine(models.Model):
transaction_weight = fields.Integer(
help="Transaction weight in Kg: Quantity x Product Weight"
)
# product_origin_country_id is replaced by product_origin_country_code
# this field should be dropped once the localisation modules have been
# adapted accordingly
product_origin_country_id = fields.Many2one(
comodel_name="res.country",
string="Country of Origin",
help="Country of origin of the product i.e. product " "'made in ____'.",
)
product_origin_country_code = fields.Char(
string="Country of Origin of the Product",
size=2,
required=True,
default="QU",
help="2 digit code of country of origin of the product except for the UK.\n"
"Specify 'XI' for UK Northern Ireland and 'XU' for rest of the UK.\n"
"Specify 'QU' when the country is unknown.\n",
)

@api.onchange("invoice_line_id")
def _onchange_move_id(self):
Expand All @@ -174,3 +199,18 @@ def _onchange_move_id(self):
("id", "not in", moves.mapped("intrastat_line_ids.invoice_line_id").ids),
]
return {"domain": {"invoice_line_id": dom}}

@api.model
def create(self, vals):
self._format_vals(vals)
return super().create(vals)

def write(self, vals):
self._format_vals(vals)
return super().write(vals)

def _format_vals(self, vals):
if "product_origin_country_code" in vals:
vals["product_origin_country_code"] = (
vals["product_origin_country_code"].upper().strip()
)
Loading

0 comments on commit 0efb503

Please sign in to comment.