Skip to content

Commit

Permalink
[IMP] l10n_br_sped_base: split sped by bloco opt
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalyi committed May 27, 2024
1 parent 5e20a18 commit d93d79c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
63 changes: 48 additions & 15 deletions l10n_br_sped_base/models/sped_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _get_default_dt_fin(self):
default=_get_default_dt_fin,
)

sped_attachment_id = fields.Many2one("ir.attachment", string="Sped Attachment")
split_sped_by_bloco = fields.Boolean()

@api.model
def _get_kind(self) -> str:
Expand Down Expand Up @@ -86,7 +86,7 @@ def button_populate_sped_from_odoo(self):
._get_top_registers(kind)
)
for register in top_registers:
register.pull_records_from_odoo(kind, level=2, log_msg=log_msg)
register._pull_records_from_odoo(kind, level=2, log_msg=log_msg)

Check warning on line 89 in l10n_br_sped_base/models/sped_declaration.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_sped_base/models/sped_declaration.py#L89

Added line #L89 was not covered by tests

self.message_post(body=log_msg.getvalue())

Check warning on line 91 in l10n_br_sped_base/models/sped_declaration.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_sped_base/models/sped_declaration.py#L91

Added line #L91 was not covered by tests

Expand All @@ -100,22 +100,54 @@ def button_done(self):
def button_draft(self):
self.state = "draft"

Check warning on line 101 in l10n_br_sped_base/models/sped_declaration.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_sped_base/models/sped_declaration.py#L101

Added line #L101 was not covered by tests

def button_create_sped_file(self):
def button_create_sped_files(self):
"""Generate and attach the SPED file."""
self.ensure_one()
sped_txt = self._generate_sped_text()

Check warning on line 106 in l10n_br_sped_base/models/sped_declaration.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_sped_base/models/sped_declaration.py#L105-L106

Added lines #L105 - L106 were not covered by tests

if self.split_sped_by_bloco:
attachment_vals = self._split_sped_text_by_bloco(sped_txt)

Check warning on line 109 in l10n_br_sped_base/models/sped_declaration.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_sped_base/models/sped_declaration.py#L109

Added line #L109 was not covered by tests
else:
attachment_vals = [self._create_sped_attachment(sped_txt)]
self.env["ir.attachment"].create(attachment_vals)

Check warning on line 112 in l10n_br_sped_base/models/sped_declaration.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_sped_base/models/sped_declaration.py#L111-L112

Added lines #L111 - L112 were not covered by tests

def _split_sped_text_by_bloco(self, sped_txt):
blocos = {}
# Split the text by bloco
current_bloco = None
for line in sped_txt.splitlines():
if line.startswith(("|0", "|9")):
continue
current_bloco = line[1]
if current_bloco not in blocos:
blocos[current_bloco] = []
if current_bloco:
blocos[current_bloco].append(line)

attachments_vals = []
for bloco, lines in blocos.items():
if len(lines) < 3: # empty bloco
continue

Check warning on line 130 in l10n_br_sped_base/models/sped_declaration.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_sped_base/models/sped_declaration.py#L130

Added line #L130 was not covered by tests
bloco_txt = "\n".join(lines)
attachments_vals.append(self._create_sped_attachment(bloco_txt, bloco))

return attachments_vals

def _create_sped_attachment(self, text, bloco=None):
kind = self._get_kind()
file_name = f"{kind}-{self.name_get()[0][1]}.txt"
self.sped_attachment_id = self.env["ir.attachment"].create(
{
"name": file_name,
"res_model": self._name,
"res_id": self.id,
"datas": base64.b64encode(sped_txt.encode()),
"mimetype": "application/txt",
"type": "binary",
}
)
if bloco:
file_name = f"{kind.upper()}-bloco_{bloco}-{self.name_get()[0][1]}.txt"
else:
file_name = f"{kind.upper()}-{self.name_get()[0][1]}.txt"

Check warning on line 141 in l10n_br_sped_base/models/sped_declaration.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_sped_base/models/sped_declaration.py#L141

Added line #L141 was not covered by tests

return {
"name": file_name,
"res_model": self._name,
"res_id": self.id,
"datas": base64.b64encode(text.encode()),
"mimetype": "application/txt",
"type": "binary",
}

@api.onchange("company_id")
def onchange_company_id(self):
Expand Down Expand Up @@ -175,7 +207,7 @@ def _append_view_header(self, form):
)
header.append(
E.button(
name="button_create_sped_file",
name="button_create_sped_files",
type="object",
states="done",
string="Generate SPED File",
Expand All @@ -202,6 +234,7 @@ def _append_view_footer(self, form):
def _append_top_view_elements(self, group, inline=False):
"""Append top-level elements to the form view."""
group.append(E.field(name="company_id"))
group.append(E.field(name="split_sped_by_bloco"))
group.append(E.separator(colspan="4"))

def _generate_sped_text(self, version=None):
Expand Down
1 change: 1 addition & 0 deletions l10n_br_sped_base/tests/test_sped_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def test_generate_sped(self):
target_content = f.read()
# print(sped)
self.assertEqual(sped.strip(), target_content.strip())
self.assertEqual(len(self.declaration._split_sped_text_by_bloco(sped)), 2)

def test_register_tree_view(self):
arch = self.env["l10n_br_sped.fake.i010"].fields_view_get(view_type="tree")[
Expand Down

0 comments on commit d93d79c

Please sign in to comment.