[14.0][MIG] account_statement_import_camt54

This commit is contained in:
Iryna Vyshnevska
2020-12-06 19:13:40 +02:00
parent 673d4ccd52
commit 0bc11c2e1a
16 changed files with 15 additions and 53 deletions

View File

@@ -0,0 +1,2 @@
from . import account_statement_import
from . import parser

View File

@@ -0,0 +1,40 @@
# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import logging
from odoo import models
_logger = logging.getLogger(__name__)
class AccountStatementImport(models.TransientModel):
"""Add process_camt method to account.bank.statement.import."""
_inherit = "account.statement.import"
def _create_bank_statements(self, stmts_vals):
""" Set balance_end_real if not already provided by the file."""
statement_line_ids, notifications = super()._create_bank_statements(stmts_vals)
statements = self.env["account.bank.statement"].search(
[("line_ids", "in", statement_line_ids)]
)
for statement in statements:
if not statement.balance_end_real:
amount = sum(statement.line_ids.mapped("amount"))
statement.balance_end_real = statement.balance_start + amount
return statement_line_ids, notifications
def _complete_stmts_vals(self, stmts_vals, journal, account_number):
"""Search partner from partner reference"""
stmts_vals = super()._complete_stmts_vals(stmts_vals, journal, account_number)
for st_vals in stmts_vals:
for line_vals in st_vals["transactions"]:
if "partner_ref" in line_vals:
partner_ref = line_vals.pop("partner_ref")
partner = self.env["res.partner"].search(
[("ref", "=", partner_ref)], limit=1
)
line_vals["partner_id"] = partner.id
return stmts_vals

View File

@@ -0,0 +1,100 @@
# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, exceptions, models
class CamtParser(models.AbstractModel):
"""Parser for camt bank statement import files."""
_inherit = "account.statement.import.camt.parser"
def _get_partner_ref(self, isr):
ICP = self.env["ir.config_parameter"]
ref_format = ICP.sudo().get_param("isr_partner_ref")
if not ref_format:
return
config = ref_format.split(",")
if len(config) == 2:
start, size = config
elif len(config) == 1:
start = config[0]
size = 6
else:
raise exceptions.UserError(
_(
"Config parameter `isr_partner_ref` is wrong.\n"
"It must be in format `i[,n]` \n"
"where `i` is the position of the first digit and\n"
"`n` the number of digit in the reference,"
" by default 6.\n"
'e.g. "13,6"'
)
)
try:
start = int(start) - 1 # count from 1 instead of 0
size = int(size)
end = start + size
except ValueError:
raise exceptions.UserError(
_(
"Config parameter `isr_partner_ref` is wrong.\n"
"It must be in format `i[,n]` \n"
"`i` and `n` must be integers.\n"
'e.g. "13,6"'
)
)
return isr[start:end].lstrip("0")
def parse_transaction_details(self, ns, node, transaction):
"""Put ESR in label and add aditional information to label
if no esr is available
"""
super().parse_transaction_details(ns, node, transaction)
# put the esr in the label. odoo reconciles based on the label,
# if there is no esr it tries to use the information textfield
isr_number = node.xpath(
"./ns:RmtInf/ns:Strd/ns:CdtrRefInf/ns:Ref", namespaces={"ns": ns}
)
if len(isr_number):
transaction["name"] = isr_number[0].text
partner_ref = self._get_partner_ref(isr_number[0].text)
if partner_ref:
transaction["partner_ref"] = partner_ref
else:
xpath_exprs = [
"./ns:RmtInf/ns:Ustrd|./ns:RtrInf/ns:AddtlInf",
"./ns:AddtlNtryInf",
"/ns:Refs/ns:InstrId",
]
name = transaction["name"]
for xpath_expr in xpath_exprs:
found_node = node.xpath(xpath_expr, namespaces={"ns": ns})
if found_node:
name = found_node[0].text
break
trans_id_node = (
node.getparent()
.getparent()
.xpath("./ns:AcctSvcrRef", namespaces={"ns": ns})
)
if trans_id_node:
name = "{} ({})".format(name, trans_id_node[0].text)
if name:
transaction["name"] = name
# End add esr to the label.
# add transaction id to ref
self.add_value_from_node(
ns,
node,
[
"./../../ns:AcctSvcrRef",
"./ns:RmtInf/ns:Strd/ns:CdtrRefInf/ns:Ref",
"./ns:Refs/ns:EndToEndId",
],
transaction,
"ref",
)
return True