Skip to content

Commit

Permalink
[RFC] l10n_br_cte: refactor dacte report
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelsavegnago committed Nov 25, 2024
1 parent ce8571e commit 5416185
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 26 deletions.
3 changes: 2 additions & 1 deletion l10n_br_cte/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import models
from .hooks import post_init_hook
from . import models
from . import report
16 changes: 13 additions & 3 deletions l10n_br_cte/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,27 @@
"spec_driven_model",
],
"data": [
"security/ir.model.access.csv",
# Data
"data/ir_config_parameter.xml",
# Security
"security/ir.model.access.csv",
# Views
"views/document.xml",
"views/cte_action.xml",
"views/cte_menu.xml",
"views/res_company.xml",
"views/modal/modal_rodoviario.xml",
"views/modal/modal_aquaviario.xml",
"views/modal/modal_ferroviario.xml",
"views/modal/modal_aereo.xml",
# Report
"report/reports.xml",
"report/danfe_cte.xml",
"report/danfe_report.xml",
# Wizards
"wizards/document_correction_wizard.xml",
# Actions
"views/cte_action.xml",
# Menus
"views/cte_menu.xml",
],
"demo": [
"demo/company_demo.xml",
Expand Down
33 changes: 11 additions & 22 deletions l10n_br_cte/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from datetime import datetime
from enum import Enum

from brazilfiscalreport.dacte import Dacte
from erpbrasil.base.fiscal import cnpj_cpf

# TODO: precisa tratar
Expand Down Expand Up @@ -1753,28 +1752,18 @@ def make_pdf(self):
if not self.filtered(filter_processador_edoc_cte):
return super().make_pdf()

Check warning on line 1753 in l10n_br_cte/models/document.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_cte/models/document.py#L1753

Added line #L1753 was not covered by tests

attachment_data = {

Check warning on line 1755 in l10n_br_cte/models/document.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_cte/models/document.py#L1755

Added line #L1755 was not covered by tests
"name": self.document_key + ".pdf",
"res_model": self._name,
"res_id": self.id,
"mimetype": "application/pdf",
"type": "binary",
}
report = self.env.ref("l10n_br_cte.report_dacte")
pdf_data = report._render_qweb_pdf(self.fiscal_line_ids.document_id.ids)
attachment_data["datas"] = base64.b64encode(pdf_data[0])
file_pdf = self.file_report_id
self.file_report_id = False
file_pdf.unlink()

Check warning on line 1767 in l10n_br_cte/models/document.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_cte/models/document.py#L1762-L1767

Added lines #L1762 - L1767 were not covered by tests

if self.authorization_file_id:
arquivo = self.authorization_file_id
xml_string = base64.b64decode(arquivo.datas).decode()
else:
arquivo = self.send_file_id
xml_string = base64.b64decode(arquivo.datas).decode()
# TODO: implementar temp_xml_autorizacao igual nfe ?
# xml_string = self.temp_xml_autorizacao(xml_string)

pdf = Dacte(xml=xml_string).output()

self.file_report_id = self.env["ir.attachment"].create(
{
"name": self.document_key + ".pdf",
"res_model": self._name,
"res_id": self.id,
"datas": base64.b64encode(pdf),
"mimetype": "application/pdf",
"type": "binary",
}
)
self.file_report_id = self.env["ir.attachment"].create(attachment_data)

Check warning on line 1769 in l10n_br_cte/models/document.py

View check run for this annotation

Codecov / codecov/patch

l10n_br_cte/models/document.py#L1769

Added line #L1769 was not covered by tests
22 changes: 22 additions & 0 deletions l10n_br_cte/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ class ResCompany(spec_models.SpecModel):

cte40_CRT = fields.Selection(related="tax_framework")

cte_authorize_accountant_download_xml = fields.Boolean(
string="Include Accountant Partner data in persons authorized to "
"download CTe XML",
default=False,
)

dacte_margin_top = fields.Integer(
default=5, help="Top margin in mm for the DACTE layout."
)

dacte_margin_right = fields.Integer(
default=5, help="Right margin in mm for the DACTE layout."
)

dacte_margin_bottom = fields.Integer(
default=5, help="Bottom margin in mm for the DACTE layout."
)

dacte_margin_left = fields.Integer(
default=5, help="Left margin in mm for the DACTE layout."
)

def _compute_cte_data(self):
# compute because a simple related field makes the match_record fail
for rec in self:
Expand Down
1 change: 1 addition & 0 deletions l10n_br_cte/report/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import ir_actions_report
165 changes: 165 additions & 0 deletions l10n_br_cte/report/danfe_cte.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<template id="report_dacte_cte">
<t t-call="web.html_container">
<div style="font-size: 12px; max-width: 90mm; margin-top: 2mm;">
<div style="text-align: center; margin: 0 auto">
<h2
style="text-transform: uppercase; font-size: 18px; font-weight: bold;"
><t t-esc="company_legal_name" /></h2>
<div style="max-width: 20.5em; text-align: center; margin: 0 auto;">
<p style="margin-bottom: 0;"><t t-esc="company_street" />, <t
t-esc="company_district"
/>, <t t-esc="company_city" /> - <t
t-esc="company_state"
/></p>
</div>
<div
style="display: flex; justify-content: space-between; max-width: 20em; margin: 0 auto;"
>
<p style="margin-bottom: 0; padding: 0;">CNPJ: <t
t-esc="company_cnpj"
/></p>
<p style="margin-bottom: 0; padding: 0;">IE: <t
t-esc="company_ie"
/></p>
</div>
<div>
<p
class="text-transform: uppercase; margin-bottom: 0;"
>Documento Auxiliar do Conhecimento de Transporte</p>
</div>
<t t-if="contingency">
<hr />
<div>
<p style="margin-bottom: 0;">Emitida em Contingência</p>
<p style="margin-bottom: 0;">Pendente de Autorização</p>
</div>
</t>
<hr />
<t t-if="homologation_environment">
<div style="text-transform: uppercase; ">
<p
style="margin-bottom: 0; font-size: 14px;"
>EMITIDA EM AMBIENTE DE HOMOLOGAÇÃO - SEM VALOR FISCAL</p>
</div>
<hr />
</t>
</div>
<div>
<p
style="font-size: 11px;"
># | COD | DESCRICAO | QUANTIDADE | UN | VL UNIT | VL TOTAL</p>
<t t-foreach="lines" t-as="line" t-key="line.id">
<p style="margin-bottom: 0;"><t
t-esc="line['product_index']"
/> <t t-esc="line['product_name']" /> <t
t-esc="line['product_quantity']"
/><t t-esc="line['product_uom']" /> X <t
t-esc="line['product_unit_value']"
/> <t t-esc="line['product_unit_total']" /> </p>
</t>
</div>
<hr />
<div>
<div
style="display: flex; justify-content: space-between; align-items: center;"
>
<p style="margin-bottom: 0;">Qtd total itens</p>
<p style="margin-bottom: 0;"><t
t-esc="total_product_quantity"
/></p>
</div>
<t t-if="unformatted_amount_discount_value">
<div
style="display: flex; justify-content: space-between; align-items: center;"
>
<p style="margin-bottom: 0;">Desconto</p>
<p style="margin-bottom: 0;"><t
t-esc="unformatted_amount_discount_value"
/></p>
</div>
</t>
<div
style="display: flex; justify-content: space-between; align-items: center;"
>
<p
style="text-transform: uppercase; font-weight: bold; margin-bottom: 0"
>Valor total</p>
<p style="margin-bottom: 0;"><t t-esc="amount_total" /></p>
</div>
</div>
<div
style="display: flex; justify-content: space-between; align-items: center;"
>
<t t-foreach="payments" t-as="payment">
<p style="margin-bottom: 0;"><t t-esc="payment['method']" /></p>
<p style="margin-bottom: 0;"><t t-esc="payment['value']" /></p>
</t>
</div>
<t t-if="amount_change">
<div
style="display: flex; justify-content: space-between; align-items: center;"
>
<p style="margin-bottom: 0;">TROCO</p>
<p style="margin-bottom: 0;"><t t-esc="amount_change" /></p>
</div>
</t>
<hr />
<div style="text-align: center;">
<p style="margin-bottom: 0;">Consulte pela chave de acesso em:</p>
<p style="margin-bottom: 0;"><t t-esc="nfce_url" /></p>
<p style="font-size: 13px; margin-bottom: 0;"><t
t-esc="document_key"
/></p>
</div>
<div
style="display: flex; flex-direction: row-reverse; align-items: center; justify-content: space-between; margin: 10px 0;"
>
<div>
<div>
<!-- TODO: Quando o consumidor for identificado deve haver o CPF ou CNPJ-->
<p
style="text-transform: uppercase; text-align: left; fonti-size: 18px; margin: 2px auto;"
>Consumidor Nao Identificado</p>
</div>

<div>
<p style="margin-bottom: 0;">Numero: <t
t-esc="document_number"
/></p>
<p style="margin-bottom: 0;">Serie: <t
t-esc="document_serie"
/></p>
<p style="margin-bottom: 0;">Emissao: <t
t-esc="document_date"
/></p>
</div>

<div>
<t t-if="contingency">
<p style="margin-bottom: 0;">Emitida em Contingencia</p>
<p style="margin-bottom: 0;">Pendente de Autorizacao</p>
</t>
<t t-else="">
<p style="margin-bottom: 0;">Protocolo: <t
t-esc="authorization_protocol"
/></p>
<p style="margin-bottom: 0;">Autorizacao: <t
t-esc="document_date"
/></p>
</t>
</div>
</div>

<div>
<t t-set="qr_code" t-value="document_qrcode" />
<img
t-att-src="'/report/barcode/?type=%s&amp;value=%s&amp;width=%s&amp;height=%s'%('QR', qr_code, 150, 150)"
/>
</div>
</div>
</div>
</t>
</template>
</odoo>
12 changes: 12 additions & 0 deletions l10n_br_cte/report/danfe_report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<odoo>
<record id="report_dacte" model="ir.actions.report">
<field name="name">DACTE</field>
<field name="model">l10n_br_fiscal.document</field>
<field name="report_type">qweb-pdf</field>
<field name="report_name">main_template_dacte</field>
<field name="report_file">main_template_dacte</field>
<field name="print_report_name">'%s' % (object.document_key)</field>
<field name="binding_model_id" ref="model_l10n_br_fiscal_document" />
<field name="binding_type">report</field>
</record>
</odoo>
Loading

0 comments on commit 5416185

Please sign in to comment.