Skip to content

Commit

Permalink
IMP account_facto_receivable_bal: support multi attachm
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Sep 7, 2024
1 parent f185982 commit 8366854
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 34 deletions.
24 changes: 18 additions & 6 deletions account_factoring_receivable_balance/models/subrogation_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class SubrogationReceipt(models.Model):
required=True,
)
comment = fields.Text()
instruction = fields.Text(store=False, compute="_compute_instruction")
line_ids = fields.One2many(
comodel_name="account.move.line",
inverse_name="subrogation_id",
Expand Down Expand Up @@ -138,7 +139,7 @@ def _get_domain_for_factor(self):
# ),
# )
# ]
domain.extend(self.factor_journal_id._get_factor_domain())
domain.extend(self.factor_journal_id._get_domain_for_factor())
return domain

@api.model
Expand Down Expand Up @@ -209,11 +210,19 @@ def action_confirm(self):
for rec in self:
if rec.state == "draft":
data = self._prepare_factor_file(rec.factor_type)
if data["datas"]:
rec.state = "confirmed"
rec.date = fields.Date.today()
rec.warn = False
self.env["ir.attachment"].create(data)
# We support multi/single attachment(s)
if isinstance(data, dict):
data_ = [data]
else:
data_ = data
attach_list = []
for datum in data_:
if datum["datas"]:
attach_list.append(datum)
self.env["ir.attachment"].create(attach_list)
rec.date = fields.Date.today()
rec.warn = False
rec.state = "confirmed"

def action_post(self):
for rec in self:
Expand Down Expand Up @@ -250,3 +259,6 @@ def unlink(self):

def _get_company_id(self):
return self.env.company.id

def _compute_instruction(self):
pass
13 changes: 10 additions & 3 deletions account_factoring_receivable_balance/views/subrogation_receipt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@
<field name="expense_tax_amount" string="Tax Amount" />
</group>
<group colspan="4">
<separator string="Comment" colspan="4" />
<field name="comment" nolabel="1" colspan="4" />
<separator
string="Comment"
attrs="{'invisible': [('comment', '!=', '')]}"
string="Instructions"
attrs="{'invisible': [('instruction', '=', False)]}"
colspan="4"
/>
<field name="comment" nolabel="1" colspan="4" />
<field
name="instruction"
nolabel="1"
colspan="4"
attrs="{'invisible': [('instruction', '=', False)]}"
/>
<separator
string="Warning"
attrs="{'invisible': [('warn', '=', False)]}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def _configure_eurof_factoring(self):
return company

def _populate_eurof_settings(self):
return "client = 45678\nemetteurD = 54321\nemetteurE = 12345\n"
return "client = 45678\nemetteurD = 54321\nemetteurE = 12345\nmail_prod = \n"

def _get_factor_shortname(self):
"""Allow to customze account name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class SubrogationReceipt(models.Model):
def _prepare_factor_file_eurof(self):
"Called from generic module"
self.ensure_one()
if not self.statement_date:
# pylint: disable=C8107
raise ValidationError("Vous devez spécifier la date du dernier relevé")
settings = safe_eval(self.factor_journal_id.factor_settings)
missing_keys = []
for key in ini_format_to_dict(
Expand All @@ -32,29 +35,33 @@ def _prepare_factor_file_eurof(self):
f"Le journal doit comporter les clés suivantes {missing_keys} "
"avec des valeurs correctes"
)
name = "FAA{}_{}_{}_{}.txt".format(
settings["emetteurD"],
self._sanitize_filepath(f"{fields.Date.today()}"),
self.id,
self._sanitize_filepath(self.company_id.name),
)
return {
"name": name,
"res_id": self.id,
"res_model": self._name,
"datas": self._prepare_factor_file_data_eurof(settings),
}

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(settings)
if data:
data = bytes(data, "ascii", "replace").replace(b"?", b" ")
return base64.b64encode(data)
return False
data = []
lines, max_row, balance = self._get_eurof_body(settings)
file_date = self._sanitize_filepath(f"{fields.Date.today()}")
company_ = self._sanitize_filepath(self.company_id.name)
# On peut avoir 2 fichiers
my_lines = {"emetteurD": [], "emetteurE": []}
for emetteur in my_lines.keys():
for line in lines:
if settings[emetteur] in line:
my_lines[emetteur].append(line)
file_data = bytes(RETURN.join(my_lines[emetteur]), "ascii", "replace")
name = f"FAA{settings[emetteur]}_{file_date}_{company_}_{self.id}.txt"
data.append(
{
"name": name,
"res_id": self.id,
"res_model": self._name,
"datas": base64.b64encode(file_data.replace(b"?", b" ")),
}
)
return data

def _sanitize_filepath(self, string):
string = super()._sanitize_filepath(string)
if self.factor_type == "eurof":
string = string.replace("-", "_")
return string

def _get_eurof_body(self, settings):
errors = []
Expand Down Expand Up @@ -142,7 +149,23 @@ def size(size, data, info=None):
if errors:
self.warn = "\n%s" % "\n".join(errors)
return False, False, False
return (RETURN.join(rows), len(rows), balance)
return (rows, len(rows), balance)

def _compute_instruction(self):
"""Display mail where send file"""
res = super()._compute_instruction()
for rec in self:
instruction = ""
if rec.factor_type == "eurof":
settings = safe_eval(self.factor_journal_id.factor_settings)
mail_prod = settings.get("mail_prod")
if mail_prod:
instruction = (
"Le fichier de quittance est à joindre "
f"à l'adresse ' {mail_prod} '"
)
rec.instruction = rec.instruction or instruction
return res


def get_piece_factor(name, p_type):
Expand Down

0 comments on commit 8366854

Please sign in to comment.