Skip to content

Commit

Permalink
[MIG] l10n_ch_import_cresus: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
em230418 committed Jul 5, 2024
1 parent f05c9a3 commit d63dffb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 37 deletions.
3 changes: 2 additions & 1 deletion l10n_ch_import_cresus/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"application": False,
"installable": True,
"depends": [
"account_accountant",
"account",
],
"data": [
"security/ir.model.access.csv",
"wizard/l10n_ch_import_cresus_view.xml",
"views/account_tax_view.xml",
"views/menu.xml",
Expand Down
2 changes: 2 additions & 0 deletions l10n_ch_import_cresus/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_account_cresus_import,access_account_cresus_import,model_account_cresus_import,account.group_account_manager,1,1,1,1
27 changes: 10 additions & 17 deletions l10n_ch_import_cresus/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import logging
import tempfile

import openerp.tests.common as common
from StringIO import StringIO
import odoo.tests.common as common
from io import BytesIO

from odoo.modules import get_resource_path

Expand All @@ -22,9 +22,6 @@ def setUp(self):
account_obj = self.env["account.account"]
analytic_account_obj = self.env["account.analytic.account"]

user_type = self.env["account.account.type"].search(
[("include_initial_balance", "=", False)], limit=1
)
for code in [
"1000",
"1010",
Expand All @@ -40,13 +37,13 @@ def setUp(self):
]:
found = account_obj.search([("code", "=", code)])
if found: # patch it within the transaction
found.user_type_id = user_type.id
found.account_type = "income"
else:
account_obj.create(
{
"name": "dummy %s" % code,
"code": code,
"user_type_id": user_type.id,
"account_type": "income",
"reconcile": True,
}
)
Expand All @@ -58,7 +55,6 @@ def setUp(self):
"tax_cresus_mapping": "VAT",
}
)
self.reserve = analytic_account_obj.create({"name": "Fortune", "code": "10"})

def test_import(self):
journal_obj = self.env["account.journal"]
Expand All @@ -72,26 +68,23 @@ def get_path(filename):
res = get_resource_path("l10n_ch_import_cresus", "tests", filename)
return res

with open(get_path("input.csv")) as src:
buf = StringIO()
base64.encode(src, buf)
contents = buf.getvalue()
buf.close()
with open(get_path("input.csv"), "rb") as src:
contents = base64.b64encode(src.read()).decode("utf-8")

wizard = self.env["account.cresus.import"].create(
{"journal_id": misc.id, "file": contents}
)
wizard._import_file()

res = wizard.imported_move_ids
res.assert_balanced()
res._check_balanced({"records": res, "self": res})

gold = open(get_path("golden-output.txt"))
temp = tempfile.NamedTemporaryFile(prefix="odoo-l10n_ch_import_cresus")
gold = open(get_path("golden-output.txt"), "r")
temp = tempfile.NamedTemporaryFile("w+", prefix="odoo-l10n_ch_import_cresus")

# Get a predictable representation that can be compared across runs
def p(u):
temp.write(u.encode("utf-8"))
temp.write(u)
temp.write("\n")

first = True
Expand Down
4 changes: 2 additions & 2 deletions l10n_ch_import_cresus/views/account_tax_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<field name="model">account.tax</field>
<field name="inherit_id" ref="account.view_tax_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='refund_account_id']" position="after">
<xpath expr="//field[@name='refund_repartition_line_ids']/.." position="after">
<group string="Crésus" name="Crésus">
<field name="tax_cresus_mapping" />
<field name="tax_cresus_mapping" nolabel="1" colspan="2"/>
</group>
</xpath>
</field>
Expand Down
2 changes: 0 additions & 2 deletions l10n_ch_import_cresus/views/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
<field name="res_model">account.cresus.import</field>
<field name="domain" />
<field name="target">new</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="cresus_move_line_importer_form" />
</record>
<menuitem
icon="STOCK_EXECUTE"
name="Accounting Cresus Import"
action="action_account_cresus_import"
id="menu_account_cresus_import"
Expand Down
35 changes: 20 additions & 15 deletions l10n_ch_import_cresus/wizard/l10n_ch_import_cresus.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import base64
import csv
import tempfile
from babel.numbers import parse_decimal, NumberFormatError
from datetime import datetime

from odoo import api, exceptions, fields, models
Expand All @@ -27,6 +28,7 @@ class AccountCresusImport(models.TransientModel):
imported_move_ids = fields.Many2many(
"account.move", "import_cresus_move_rel", string="Imported moves"
)
index = fields.Integer()

HEAD_CRESUS = [
"date",
Expand Down Expand Up @@ -85,7 +87,7 @@ def prepare_line(
analytic_account = analytic_account_obj.search(
[("code", "=", analytic_account_code)], limit=1
)
line["analytic_account_id"] = analytic_account.id
line["analytic_distribution"] = {str(analytic_account.id): 100.00}

if tax_ids:
line["tax_ids"] = [(4, id, 0) for id in tax_ids]
Expand All @@ -99,15 +101,16 @@ def _parse_csv(self):
:returns: generator
"""
# We use tempfile in order to avoid memory error with large files
with tempfile.TemporaryFile() as src:
content = self.file
delimiter = "\t"
src.write(content)
with tempfile.TemporaryFile() as decoded:
src.seek(0)
base64.decode(src, decoded)
decoded.seek(0)
Attachment = self.env["ir.attachment"]
csv_attachment = Attachment.search([
("res_model", "=", self._name),
("res_id", "=", self.id),
("res_field", '=', 'file'),
])
delimiter = "\t"
csv_filepath = Attachment._full_path(csv_attachment.store_fname)
for x in range(1):
with open(csv_filepath) as decoded:
try:
data = csv.DictReader(
decoded, fieldnames=self.HEAD_CRESUS, delimiter=delimiter
Expand Down Expand Up @@ -144,7 +147,6 @@ def _parse_date(self, date_string):
raise exceptions.ValidationError(_("Can't parse date '%s'") % date_string)
return fields.Date.to_string(dt)

@api.multi
def _standardise_data(self, data):
"""split accounting lines where needed
Expand All @@ -167,9 +169,13 @@ def _standardise_data(self, data):
previous_pce = line_cresus["pce"]
previous_date = line_cresus["date"]

from babel.numbers import parse_decimal
try:
recto_amount_decimal = parse_decimal(line_cresus["amount"], locale="de_CH")
except NumberFormatError:
# replacing old version of group separator
recto_amount_decimal = parse_decimal(line_cresus["amount"].replace("'", "’"), locale="de_CH")
recto_amount = float(recto_amount_decimal)

recto_amount = float(parse_decimal(line_cresus["amount"], locale="de_CH"))
verso_amount = 0.0
if recto_amount < 0:
recto_amount, verso_amount = 0.0, -recto_amount
Expand Down Expand Up @@ -208,7 +214,6 @@ def _standardise_data(self, data):
lines, date=line_cresus["date"], ref=previous_pce, journal_id=journal_id
)

@api.multi
def _import_file(self):
self.index = 0
data = self._parse_csv()
Expand All @@ -217,8 +222,8 @@ def _import_file(self):
self.with_context(dont_create_taxes=True).write(
{"imported_move_ids": [(0, False, mv)]}
)
self.invalidate_cache(fnames=["imported_move_ids"])

@api.multi
def import_file(self):
try:
self._import_file()
Expand Down

0 comments on commit d63dffb

Please sign in to comment.