odoo_account_ebics/account_ebics/models/ebics_config.py

266 lines
8.9 KiB
Python
Raw Normal View History

2022-05-10 19:40:54 +00:00
# Copyright 2009-2022 Noviat.
2019-12-22 09:54:00 +00:00
# License LGPL-3 or later (http://www.gnu.org/licenses/lpgl).
2018-08-14 15:04:39 +00:00
import logging
import os
2022-05-10 19:40:54 +00:00
import re
2018-08-14 15:04:39 +00:00
from odoo import _, api, fields, models
2018-08-14 15:04:39 +00:00
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
class EbicsConfig(models.Model):
"""
EBICS configuration is stored in a separate object in order to
allow extra security policies on this object.
"""
2022-05-10 19:40:54 +00:00
_name = "ebics.config"
_description = "EBICS Configuration"
_order = "name"
2018-08-14 15:04:39 +00:00
name = fields.Char(
2022-05-10 19:40:54 +00:00
readonly=True,
states={"draft": [("readonly", False)]},
required=True,
)
journal_ids = fields.Many2many(
2022-05-10 19:40:54 +00:00
comodel_name="account.journal",
2022-12-18 19:55:23 +00:00
relation="account_journal_ebics_config_rel",
2022-05-10 19:40:54 +00:00
readonly=True,
states={"draft": [("readonly", False)]},
string="Bank Accounts",
domain="[('type', '=', 'bank')]",
2022-05-10 19:40:54 +00:00
)
2018-08-14 15:04:39 +00:00
ebics_host = fields.Char(
2022-05-10 19:40:54 +00:00
string="EBICS HostID",
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
2018-08-14 15:04:39 +00:00
help="Contact your bank to get the EBICS HostID."
2022-05-10 19:40:54 +00:00
"\nIn France the BIC is usually allocated to the HostID "
"whereas in Germany it tends to be an institute specific string "
"of 8 characters.",
)
2018-08-14 15:04:39 +00:00
ebics_url = fields.Char(
2022-05-10 19:40:54 +00:00
string="EBICS URL",
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
help="Contact your bank to get the EBICS URL.",
)
2018-08-14 15:04:39 +00:00
ebics_version = fields.Selection(
2022-12-18 19:55:23 +00:00
selection=[
("H003", "H003 (2.4)"),
("H004", "H004 (2.5)"),
("H005", "H005 (3.0)"),
],
2022-05-10 19:40:54 +00:00
string="EBICS protocol version",
readonly=True,
states={"draft": [("readonly", False)]},
required=True,
default="H004",
)
2018-08-14 15:04:39 +00:00
ebics_partner = fields.Char(
2022-05-10 19:40:54 +00:00
string="EBICS PartnerID",
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
2018-08-14 15:04:39 +00:00
help="Organizational unit (company or individual) "
2022-05-10 19:40:54 +00:00
"that concludes a contract with the bank. "
"\nIn this contract it will be agreed which order types "
"(file formats) are used, which accounts are concerned, "
"which of the customer's users (subscribers) "
"communicate with the EBICS bank server and the authorisations "
"that these users will possess. "
"\nIt is identified by the PartnerID.",
)
ebics_userid_ids = fields.One2many(
2022-05-10 19:40:54 +00:00
comodel_name="ebics.userid",
inverse_name="ebics_config_id",
readonly=True,
states={"draft": [("readonly", False)]},
2018-08-14 15:04:39 +00:00
help="Human users or a technical system that is/are "
2022-05-10 19:40:54 +00:00
"assigned to a customer. "
"\nOn the EBICS bank server it is identified "
"by the combination of UserID and PartnerID. "
"The technical subscriber serves only for the data exchange "
"between customer and financial institution. "
"The human user also can authorise orders.",
)
2018-08-14 15:04:39 +00:00
ebics_files = fields.Char(
2022-05-10 19:40:54 +00:00
string="EBICS Files Root",
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
2018-08-14 15:04:39 +00:00
default=lambda self: self._default_ebics_files(),
2022-05-10 19:40:54 +00:00
help="Root Directory for EBICS File Transfer Folders.",
)
2018-08-14 15:04:39 +00:00
# We store the EBICS keys in a separate directory in the file system.
# This directory requires special protection to reduce fraude.
ebics_keys = fields.Char(
2022-05-10 19:40:54 +00:00
string="EBICS Keys Root",
required=True,
readonly=True,
states={"draft": [("readonly", False)]},
2018-08-14 15:04:39 +00:00
default=lambda self: self._default_ebics_keys(),
2022-05-10 19:40:54 +00:00
help="Root Directory for storing the EBICS Keys.",
)
2018-08-14 15:04:39 +00:00
ebics_key_version = fields.Selection(
2022-05-10 19:40:54 +00:00
selection=[("A005", "A005 (RSASSA-PKCS1-v1_5)"), ("A006", "A006 (RSASSA-PSS)")],
string="EBICS key version",
default="A006",
readonly=True,
states={"draft": [("readonly", False)]},
help="The key version of the electronic signature.",
)
2018-08-14 15:04:39 +00:00
ebics_key_bitlength = fields.Integer(
2022-05-10 19:40:54 +00:00
string="EBICS key bitlength",
2018-08-14 15:04:39 +00:00
default=2048,
2022-05-10 19:40:54 +00:00
readonly=True,
states={"draft": [("readonly", False)]},
2018-08-14 15:04:39 +00:00
help="The bit length of the generated keys. "
2022-05-10 19:40:54 +00:00
"\nThe value must be between 1536 and 4096.",
)
2018-08-14 15:04:39 +00:00
ebics_file_format_ids = fields.Many2many(
2022-05-10 19:40:54 +00:00
comodel_name="ebics.file.format",
column1="config_id",
column2="format_id",
string="EBICS File Formats",
readonly=True,
states={"draft": [("readonly", False)]},
2018-08-14 15:04:39 +00:00
)
state = fields.Selection(
2022-05-10 19:40:54 +00:00
[("draft", "Draft"), ("confirm", "Confirmed")],
default="draft",
required=True,
readonly=True,
)
2018-08-14 15:04:39 +00:00
order_number = fields.Char(
2022-05-10 19:40:54 +00:00
size=4,
readonly=True,
states={"draft": [("readonly", False)]},
2018-08-14 15:04:39 +00:00
help="Specify the number for the next order."
2022-05-10 19:40:54 +00:00
"\nThis number should match the following pattern : "
"[A-Z]{1}[A-Z0-9]{3}",
)
2022-12-18 19:55:23 +00:00
active = fields.Boolean(default=True)
company_ids = fields.Many2many(
2022-05-10 19:40:54 +00:00
comodel_name="res.company",
2022-12-18 19:55:23 +00:00
relation="ebics_config_res_company_rel",
2022-05-10 19:40:54 +00:00
string="Companies",
2022-12-18 19:55:23 +00:00
readonly=True,
2022-05-10 19:40:54 +00:00
help="Companies sharing this EBICS contract.",
)
2018-08-14 15:04:39 +00:00
@api.model
def _default_ebics_files(self):
2022-05-10 19:40:54 +00:00
return "/".join(["/home/odoo/ebics_files", self._cr.dbname])
2018-08-14 15:04:39 +00:00
@api.model
def _default_ebics_keys(self):
2022-05-10 19:40:54 +00:00
return "/".join(["/etc/odoo/ebics_keys", self._cr.dbname])
2020-06-21 11:38:59 +00:00
@api.constrains("ebics_key_bitlength")
def _check_ebics_key_bitlength(self):
for cfg in self:
if cfg.ebics_version == "H005" and cfg.ebics_key_bitlength < 2048:
raise UserError(_("EBICS key bitlength must be >= 2048."))
2022-05-10 19:40:54 +00:00
@api.constrains("order_number")
2018-08-14 15:04:39 +00:00
def _check_order_number(self):
for cfg in self:
nbr = cfg.order_number
ok = True
if nbr:
if len(nbr) != 4:
ok = False
else:
pattern = re.compile("[A-Z]{1}[A-Z0-9]{3}")
if not pattern.match(nbr):
ok = False
if not ok:
2022-05-10 19:40:54 +00:00
raise UserError(
_(
"Order Number should comply with the following pattern:"
"\n[A-Z]{1}[A-Z0-9]{3}"
)
)
2022-12-18 19:55:23 +00:00
def write(self, vals):
"""
Due to the multi-company nature of the EBICS config we
need to adapt the company_ids in the write method.
"""
if "journal_ids" not in vals:
return super().write(vals)
for rec in self:
old_company_ids = rec.journal_ids.mapped("company_id").ids
super(EbicsConfig, rec).write(vals)
new_company_ids = rec.journal_ids.mapped("company_id").ids
updates = []
for cid in new_company_ids:
if cid in old_company_ids:
old_company_ids.remove(cid)
else:
updates += [(4, cid)]
updates += [(3, x) for x in old_company_ids]
super(EbicsConfig, rec).write({"company_ids": updates})
return True
2018-08-14 15:04:39 +00:00
def unlink(self):
for ebics_config in self:
2022-05-10 19:40:54 +00:00
if ebics_config.state == "active":
raise UserError(_("You cannot remove active EBICS configurations."))
2018-08-14 15:04:39 +00:00
return super(EbicsConfig, self).unlink()
def set_to_draft(self):
2022-05-10 19:40:54 +00:00
return self.write({"state": "draft"})
2018-08-14 15:04:39 +00:00
def set_to_confirm(self):
2022-05-10 19:40:54 +00:00
return self.write({"state": "confirm"})
2018-08-14 15:04:39 +00:00
def _get_order_number(self):
return self.order_number
def _update_order_number(self, OrderID):
o_list = list(OrderID)
for i, c in enumerate(reversed(o_list), start=1):
2022-05-10 19:40:54 +00:00
if c == "9":
o_list[-i] = "A"
2018-08-14 15:04:39 +00:00
break
2022-05-10 19:40:54 +00:00
if c == "Z":
o_list[-i] = "0"
2018-08-14 15:04:39 +00:00
continue
else:
o_list[-i] = chr(ord(c) + 1)
break
2022-05-10 19:40:54 +00:00
next_order_number = "".join(o_list)
if next_order_number == "ZZZZ":
next_order_number = "A000"
self.order_number = next_order_number
2018-08-14 15:04:39 +00:00
def _check_ebics_keys(self):
2022-05-10 19:40:54 +00:00
dirname = self.ebics_keys or ""
if not os.path.exists(dirname):
2022-05-10 19:40:54 +00:00
raise UserError(
_(
"EBICS Keys Root Directory %s is not available."
"\nPlease contact your system administrator."
)
% dirname
)
2018-08-14 15:04:39 +00:00
def _check_ebics_files(self):
2022-05-10 19:40:54 +00:00
dirname = self.ebics_files or ""
2018-08-14 15:04:39 +00:00
if not os.path.exists(dirname):
2022-05-10 19:40:54 +00:00
raise UserError(
_(
"EBICS Files Root Directory %s is not available."
"\nPlease contact your system administrator."
)
% dirname
)