Skip to content

Commit

Permalink
[ADD] add new module intrastat_product_hscodes_import
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidderen-noviat committed Nov 7, 2023
1 parent 4466fa9 commit 7868803
Show file tree
Hide file tree
Showing 13 changed files with 39,151 additions and 0 deletions.
1 change: 1 addition & 0 deletions intrastat_product_hscodes_import/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import wizards
20 changes: 20 additions & 0 deletions intrastat_product_hscodes_import/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2009-2023 Noviat (http://www.noviat.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Intrastat Product - HS Codes Import",
"version": "15.0.1.0.0",
"category": "Intrastat",
"license": "AGPL-3",
"summary": "Module used to import HS Codes for Intrastat Product",
"author": "Noviat, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/intrastat-extrastat",
"depends": [
"intrastat_product",
],
"data": [
"security/ir.model.access.csv",
"wizards/intrastat_hscodes_import_installer_views.xml",
],
"installable": True,
}
3 changes: 3 additions & 0 deletions intrastat_product_hscodes_import/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_intrastat_hscodes_import_installer_admin,access_intrastat_hscodes_import_installer_admin,model_intrastat_hscodes_import_installer,base.group_system,1,1,1,1
access_intrastat_hscodes_import_installer_account_manager,access_intrastat_hscodes_import_installer_account_manager,model_intrastat_hscodes_import_installer,account.group_account_manager,1,1,1,0
9,741 changes: 9,741 additions & 0 deletions intrastat_product_hscodes_import/static/data/2022_de_intrastat_codes.csv

Large diffs are not rendered by default.

9,741 changes: 9,741 additions & 0 deletions intrastat_product_hscodes_import/static/data/2022_en_intrastat_codes.csv

Large diffs are not rendered by default.

9,741 changes: 9,741 additions & 0 deletions intrastat_product_hscodes_import/static/data/2022_fr_intrastat_codes.csv

Large diffs are not rendered by default.

9,741 changes: 9,741 additions & 0 deletions intrastat_product_hscodes_import/static/data/2022_nl_intrastat_codes.csv

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions intrastat_product_hscodes_import/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import intrastat_hscodes_import_installer
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Copyright 2009-2023 Noviat.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import csv
import io
import os

import odoo
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.osv.expression import OR


class IntrastatHSCodesImportInstaller(models.TransientModel):
_name = "intrastat.hscodes.import.installer"
_inherit = "res.config.installer"
_description = "Intrastat HS Codes Import Installer"

intrastat_file_year = fields.Selection(selection=[("2022", "2022")], default="2022")
intrastat_file_delimiter = fields.Selection(selection=[(";", ";")], default=";")
share_codes = fields.Boolean(
default=True,
help="Set this flag to share the Intrastat Codes between all "
"Legal Entities defined in this Odoo Database.\n",
)
company_id = fields.Many2one(
comodel_name="res.company",
string="Company",
)

def _hscodes_vals_domain(self):
domain = [("company_id", "=", False)]

Check warning on line 32 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L32

Added line #L32 was not covered by tests
if self.company_id:
domain = OR([domain, ("company_id", "=", self.company_id.id)])
return domain

Check warning on line 35 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L34-L35

Added lines #L34 - L35 were not covered by tests

@api.model
def _intrastat_file_available_langs(self):
return ["en", "fr", "fr", "de"]

Check warning on line 39 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L39

Added line #L39 was not covered by tests

@api.model
def _load_code(self, row, hs_codes):
company_id = self.company_id.id or False
vals = {

Check warning on line 44 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L43-L44

Added lines #L43 - L44 were not covered by tests
"description": row["description"],
"company_id": company_id,
}
intrastat_unit_id = row["unit_id"]

Check warning on line 48 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L48

Added line #L48 was not covered by tests
if intrastat_unit_id:
intrastat_unit_ref = "intrastat_product." + intrastat_unit_id
intrastat_unit = self.env.ref(intrastat_unit_ref)
vals["intrastat_unit_id"] = intrastat_unit.id
intrastat_code = row["code"]

Check warning on line 53 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L50-L53

Added lines #L50 - L53 were not covered by tests
hscode_rec = hs_codes.filtered(lambda h: h.local_code == intrastat_code)
if hscode_rec:
hscode_rec.write(vals)

Check warning on line 56 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L56

Added line #L56 was not covered by tests
else:
vals["local_code"] = intrastat_code
hscode_rec = self.env["hs.code"].create(vals)
hs_codes |= hscode_rec
return hs_codes

Check warning on line 61 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L58-L61

Added lines #L58 - L61 were not covered by tests

def execute(self):
res = super().execute()

Check warning on line 64 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L64

Added line #L64 was not covered by tests
# get path for intrastat hs codes files
module = __name__.split("addons.")[1].split(".")[0]

Check warning on line 66 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L66

Added line #L66 was not covered by tests
for adp in odoo.addons.__path__:
module_path = adp + os.sep + module

Check warning on line 68 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L68

Added line #L68 was not covered by tests
if os.path.isdir(module_path):
break

Check warning on line 70 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L70

Added line #L70 was not covered by tests
# load existing intrastat codes
hs_codes = self.env["hs.code"].search(self._hscodes_vals_domain())

Check warning on line 72 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L72

Added line #L72 was not covered by tests
# load csv files from data
lang_found = False

Check warning on line 74 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L74

Added line #L74 was not covered by tests
for lang in self._intrastat_file_available_langs():
lang_recs = self.env["res.lang"].search([("code", "=like", lang + "_%")])

Check warning on line 76 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L76

Added line #L76 was not covered by tests
if not lang_recs:
continue
lang_found = True
intrastat_filename = (

Check warning on line 80 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L78-L80

Added lines #L78 - L80 were not covered by tests
self.intrastat_file_year + "_" + lang + "_intrastat_codes.csv"
)
intrastat_file_path = (

Check warning on line 83 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L83

Added line #L83 was not covered by tests
module_path + os.sep + "static/data" + os.sep + intrastat_filename
)
with io.open(

Check warning on line 86 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L86

Added line #L86 was not covered by tests
intrastat_file_path, mode="r", encoding="Windows-1252"
) as CN_file:
intrastat_codes = csv.DictReader(

Check warning on line 89 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L89

Added line #L89 was not covered by tests
CN_file, delimiter=self.intrastat_file_delimiter
)
for lang_rec in lang_recs:
hs_codes = hs_codes.with_context(lang=lang_rec.code)

Check warning on line 93 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L93

Added line #L93 was not covered by tests
for row in intrastat_codes:
hs_codes = self._load_code(row, hs_codes)

Check warning on line 95 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L95

Added line #L95 was not covered by tests
if not lang_found:
raise UserError(

Check warning on line 97 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L97

Added line #L97 was not covered by tests
_(
"Any Language active on your system is available in intrastat codes files [%s]"
)
% ",".join(self._intrastat_file_available_langs())
)
return res

Check warning on line 103 in intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py

View check run for this annotation

Codecov / codecov/patch

intrastat_product_hscodes_import/wizards/intrastat_hscodes_import_installer.py#L103

Added line #L103 was not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="intrastat_hscodes_import_installer_view_form" model="ir.ui.view">
<field name="name">intrastat.hscodes.import.installer.view</field>
<field name="model">intrastat.hscodes.import.installer</field>
<field name="inherit_id" ref="base.res_config_installer" />
<field name="arch" type="xml">
<form position="attributes">
<attribute name="string">Load Intrastat Codes</attribute>
</form>
<xpath expr="/form/separator[@colspan='4']" position="after">
<group position="inside">
<field name="share_codes" />
<field
name="company_id"
options="{'no_create': True, 'no_open': True}"
domain="[('id', 'in', allowed_company_ids)]"
groups="base.group_multi_company"
attrs="{'required': [('share_codes','=',False)],'invisible': [('share_codes','=',True)]}"
/>
<field name="intrastat_file_year" invisible="1" />
<field name="intrastat_file_delimiter" invisible="1" />
</group>
</xpath>
<xpath expr="/form/separator[@colspan='4']" position="attributes">
<attribute name="string">Load Intrastat Codes</attribute>
</xpath>
<xpath expr="//button[@name='action_next']" position="attributes">
<attribute name="string">Load</attribute>
</xpath>
</field>
</record>

<record
id="intrastat_hscodes_import_installer_action"
model="ir.actions.act_window"
>
<field name="name">Load Intrastat Codes</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">intrastat.hscodes.import.installer</field>
<field name="view_id" ref="intrastat_hscodes_import_installer_view_form" />
<field name="view_mode">form</field>
<field name="target">new</field>
</record>

<record id="intrastat_installer_todo" model="ir.actions.todo">
<field name="action_id" ref="intrastat_hscodes_import_installer_action" />
<field name="sequence">10</field>
</record>

</odoo>
6 changes: 6 additions & 0 deletions setup/intrastat_product_hscodes_import/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 7868803

Please sign in to comment.