Skip to content

Commit

Permalink
FIX acc_fact_eurof: define facto_settings field
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Aug 20, 2024
1 parent cc30f08 commit 3ec3896
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ def _compute_display_name(self):

@api.model
def _get_domain_for_factor(self):
journal = self.factor_journal_id
# journal = self.factor_journal_id
factor_journal = self.factor_journal_id
currency = journal.currency_id
bank_journal = self._get_bank_journal(self.factor_type, currency=currency)
# currency = journal.currency_id
# bank_journal = self._get_bank_journal(self.factor_type, currency=currency)
domain = [
("date", "<=", self.target_date),
("company_id", "=", self._get_company_id()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"bealdav",
],
"depends": [
"account_factoring_receivable_balance",
"account_factoring_receivable_balance",
"l10n_fr",
"partner_identification",
],
"data": [
"views/partner.xml",
"views/account_journal.xml",
"data/data.xml",
],
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# © 2024 David BEAL @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models
from odoo import api, exceptions, fields, models


def ini_format_to_dict(multiline_text):
vals = {}
for row in multiline_text.strip().split("\n"):
if "=" in row and row[0] != "#":
key, val = row.split("=")
if "#" in val:
val, __ = val.split("#")
vals[key.strip(" ")] = val.strip(" ")
return vals


class AccountJournal(models.Model):
Expand All @@ -11,6 +22,18 @@ class AccountJournal(models.Model):
selection_add=[("eurof", "Eurofactor")],
ondelete={"eurof": "set null"},
)
factor_code2 = fields.Char(
help="Numéro attribué par notre système à l’enregistrement de votre contrat",
factor_data = fields.Text(
default="\nkey1 = value1 \nkey2 = value2 # comment",
help="A saisir dans ce champ des clés / valeurs séparées par des =",
)
factor_settings = fields.Char(compute="_compute_factor_settings")

@api.depends("factor_data")
def _compute_factor_settings(self):
for rec in self:
try:
rec.factor_settings = ini_format_to_dict(rec.factor_data)
except Exception:
raise exceptions.ValidationError(
f"Le format des data\n{rec.factor_data}\nn'est pas conforme"
) from None
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ def _configure_eurof_factoring(self):
"type": "general",
"factor_type": FACTO_TYPE,
"code": FACTO_TYPE.upper(),
"factor_code": "12345",
"factor_code2": "12345",
"factor_data": self._populate_eurof_settings(),
"company_id": company.id,
"factoring_receivable_account_id": acc[f"4115{suffix}"].id,
"factoring_current_account_id": acc[f"4671{suffix}"].id,
Expand All @@ -156,6 +155,11 @@ def _configure_eurof_factoring(self):
)
return company

def _populate_eurof_settings(self):
return (
"contrat = 123456\nclient = 45678\nemetteurD = 54321\nemetteurE = 12345\n"
)

def _get_factor_shortname(self):
"""Allow to customze account name
CA : Crédit agricole
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

from odoo import fields, models
from odoo.exceptions import ValidationError
from odoo.tools.safe_eval import safe_eval

from .account_journal import ini_format_to_dict

RETURN = "\r\n"

Expand All @@ -15,9 +18,22 @@ class SubrogationReceipt(models.Model):
_inherit = "subrogation.receipt"

def _prepare_factor_file_eurof(self):
"Called from generic module"
self.ensure_one()
settings = safe_eval(self.factor_journal_id.factor_settings)
missing_keys = []
for key in ini_format_to_dict(
self.env["res.company"]._populate_eurof_settings()
):
if not settings.get(key):
missing_keys.append(key)
if missing_keys:
raise ValidationError(
f"Le journal doit comporter les clés suivantes {missing_keys} "
"avec des valeurs correctes"
)
name = "FAA{}_{}_{}_{}.txt".format(
self.factor_journal_id.factor_code,
settings["client"],
self._sanitize_filepath(f"{fields.Date.today()}"),
self.id,
self._sanitize_filepath(self.company_id.name),
Expand All @@ -26,15 +42,15 @@ def _prepare_factor_file_eurof(self):
"name": name,
"res_id": self.id,
"res_model": self._name,
"datas": self._prepare_factor_file_data_eurof(),
"datas": self._prepare_factor_file_data_eurof(settings),
}

def _prepare_factor_file_data_eurof(self):
def _prepare_factor_file_data_eurof(self, settings):
self.ensure_one()
if not self.statement_date:
# pylint: disable=C8107
raise ValidationError("Vous devez spécifier la date du dernier relevé")
data, max_row, balance = self._get_eurof_body()
data, max_row, balance = self._get_eurof_body(settings)
if data:
# raw_data = (f"{main}{RETURN}").replace("False", " ")
# data = clean_string(raw_data)
Expand All @@ -60,7 +76,7 @@ def _prepare_factor_file_data_eurof(self):
return base64.b64encode(data)
return False

def _get_eurof_body(self):
def _get_eurof_body(self, settings):
errors = []

def size(size, data, info=None):
Expand All @@ -84,11 +100,9 @@ def size(size, data, info=None):
if res:
errors.append(res)
partner_mapping = partners._get_partner_eurof_mapping()
main = {
"code": size(5, "factor_code", self.factor_journal_id),
"code2": size(5, "factor_code2", self.factor_journal_id),
"date": eurof_date(fields.Date.today()),
}
size(5, settings["client"], "client")
size(5, settings["emetteurD"], "emetteurD")
size(5, settings["emetteurE"], "emetteurE")
for line in self.line_ids:
move = line.move_id
partner = line.move_id.partner_id.commercial_partner_id
Expand All @@ -104,18 +118,17 @@ def size(size, data, info=None):
sequence += 1
p_type = get_type_piece(move)
total = move.amount_total_in_currency_signed
activity = "E"
if partner.country_id == self.env.ref("base.fr"):
activity = "D"
info = {
"code": main["code"],
"code2": main["code2"],
"file_date": main["date"],
"activity": "D"
# TODO check dom tom
if partner.country_id == self.env.ref("base.fr")
else "E",
"afc": "788"
# TODO check contrat can be 788
if partner.country_id == self.env.ref("base.fr")
else "999",
"emetteur": settings["emetteurD"]
if activity == "D"
else settings["emetteurE"],
"client": settings["client"],
"file_date": eurof_date(fields.Date.today()),
"activity": activity,
"afc": "711" if activity == "D" else "999",
"p_type": p_type,
"devise": move.currency_id.name,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>


<record id="view_account_journal_form" model="ir.ui.view">
<field name="model">account.journal</field>
<field
name="inherit_id"
ref="account_factoring_receivable_balance.view_account_journal_form"
/>
<field name="arch" type="xml">
<xpath expr="//field[@name='factor_code']" position="after">
<field name="factor_data" />
<field name="factor_settings" />
</xpath>
</field>
</record>


</odoo>

0 comments on commit 3ec3896

Please sign in to comment.