Skip to content

Commit

Permalink
Merge pull request #57 from CristianoMafraJunior/fix_damdfe_seg
Browse files Browse the repository at this point in the history
DAMDFE: Correção informações do seguro
  • Loading branch information
antoniospneto authored Dec 11, 2024
2 parents cbfd911 + bcc6d8a commit 5917421
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 25 deletions.
87 changes: 68 additions & 19 deletions brazilfiscalreport/damdfe/damdfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, xml, config: DamdfeConfig = None):

root = ET.fromstring(xml)
self.inf_adic = root.find(f"{URL}infAdic")
self.inf_seg = root.find(f"{URL}infSeg")
self.inf_seg = root.findall(f"{URL}seg")
self.disp = root.find(f"{URL}disp")
self.inf_mdfe = root.find(f"{URL}infMDFe")
self.prot_mdfe = root.find(f"{URL}protMDFe")
Expand Down Expand Up @@ -85,6 +85,31 @@ def _build_chCTe_str(self):
self.chCTe_str.append(chCTe_value)
return self.chCTe_str

def _build_seg_str(self):
self.inf_seg_str = []
for seg in self.inf_seg:
xSeg = extract_text(seg.find(f"{URL}infSeg"), "xSeg")
infSeg_cnpj = extract_text(seg.find(f"{URL}infSeg"), "CNPJ")
nApol = extract_text(seg, "nApol")
"""
# TODO: criado uma lista para cada tag <seg>
para não conter duplicações da tag nAver
"""
nAver_list = []
for nAver in seg.findall(f"{URL}nAver"):
nAver_value = nAver.text.strip() if nAver.text else None
if nAver_value:
nAver_list.append(nAver_value)
self.inf_seg_str.append(
{
"nome": xSeg or "",
"cnpj": infSeg_cnpj or "",
"apolice": nApol if nApol else "",
"averbacoes": nAver_list if nAver_list else [""],
}
)
return self.inf_seg_str

def _build_chnfe_str(self):
self.chNFe_str = []
for chnfe in self.inf_mun_descarga:
Expand Down Expand Up @@ -1601,10 +1626,7 @@ def _draw_insurance_information(self):

self.fisco = extract_text(self.inf_adic, "infAdFisco")
self.obs = extract_text(self.inf_adic, "infCl")
self.seguradora_nome = extract_text(self.inf_seg, "xSeg")
self.cnpj_segurado = extract_text(self.inf_seg, "CNPJ")
self.n_apol = extract_text(self.inf_seg, "nApol")
self.nome_averbacao = extract_text(self.inf_seg, "nAver")
self._build_seg_str()

self.rect(
x=x_margin,
Expand All @@ -1621,24 +1643,51 @@ def _draw_insurance_information(self):
w=100, h=0, text="INFORMAÇÕES SOBRE OS SEGUROS", border=0, align="L"
)
self.set_font(self.default_font, "", 6)
self.set_xy(x=x_margin, y=y_middle)
if self.seguradora_nome:
self.multi_cell(
w=100,
h=0,
text=f"NOME: {self.seguradora_nome} CNPJ: {self.cnpj_segurado}",
border=0,
align="L",
)
self.set_xy(x=x_margin, y=y_middle + 4)
if self.n_apol:
y_position = self.get_y() + 2
for seg_data in self.inf_seg_str:
self.set_xy(x=x_margin, y=y_position)
self.multi_cell(
w=100,
h=0,
text=f"APÓLICE: {self.n_apol} AVERBAÇÃO: {self.nome_averbacao}",
w=190,
h=4,
text=(
f"NOME: {seg_data['nome']} "
f"CNPJ: {seg_data['cnpj']} "
f"APÓLICE: {seg_data['apolice']}"
),
border=0,
align="L",
)
y_position += 4
max_averbacoes_por_linha = 3
averbacao_linha = ""
averbacao_count = 0
for _, aver in enumerate(seg_data["averbacoes"]):
if averbacao_count < max_averbacoes_por_linha:
if aver:
averbacao_linha += f"AVERBAÇÃO: {aver} "
averbacao_count += 1
else:
self.set_xy(x=x_margin, y=y_position)
self.multi_cell(
w=190,
h=4,
text=averbacao_linha.strip(),
border=0,
align="L",
)
y_position += 4
averbacao_linha = f"AVERBAÇÃO: {aver} "
averbacao_count = 1
if averbacao_linha:
self.set_xy(x=x_margin, y=y_position)
self.multi_cell(
w=190,
h=4,
text=averbacao_linha.strip(),
border=0,
align="L",
)
y_position += 4

self.rect(
x=x_margin,
Expand Down
26 changes: 20 additions & 6 deletions tests/fixtures/mdf-e_test_1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,28 @@
<email>[email protected]</email>
</enderEmit>
</emit>
<infSeg>
<nfSeg>
<seg>
<respSeg>1</respSeg>
<CNPJ>00000000000000</CNPJ>
<infSeg>
<xSeg>SEGURADO TESTE</xSeg>
<CNPJ>12345678000199</CNPJ>
<nApol>12345678901234567890</nApol>
<nAver>1234567890123456789012345678901234567890</nAver>
</nfSeg>
</infSeg>
</infSeg>
<nApol>12345678901234567890</nApol>
</seg>
<seg>
<respSeg>1</respSeg>
<CNPJ>00000000000000</CNPJ>
<infSeg>
<xSeg>SEGURADO TESTE</xSeg>
<CNPJ>12345678000199</CNPJ>
</infSeg>
<nApol>12345678901234567890</nApol>
<nAver>1234567890123456789012345678901234567890</nAver>
<nAver>1234567890123456789012345678901234567890</nAver>
<nAver>1234567890123456789012345678901234567890</nAver>
<nAver>1234567890123456789012345678901234567890</nAver>
</seg>
<infModal versaoModal="3.00">
<rodo>
<infANTT>
Expand Down
Binary file modified tests/generated/damdfe/damdfe_default.pdf
Binary file not shown.
Binary file modified tests/generated/damdfe/damdfe_default_logo.pdf
Binary file not shown.
Binary file modified tests/generated/damdfe/damdfe_default_logo_margins.pdf
Binary file not shown.

0 comments on commit 5917421

Please sign in to comment.