{# ── Macro moeda BR ── #} {% macro br(val) %}R$ {{ '%.2f'|format(val|float) | replace('.', ',') }}{% endmacro %} {# ── Variáveis de apoio ── #} {% set _desconto_perc = ((desconto_valor / subtotal_venda * 100) if subtotal_venda > 0 and desconto_valor > 0 else 0) %} {% set _acrescimo_perc = ((acrescimo_valor / subtotal_venda * 100) if subtotal_venda > 0 and acrescimo_valor > 0 else 0) %} {% set _nome_empresa = (empresa.nome_fantasia or empresa.nome or empresa.razao_social or 'CAPESYSTEM') if empresa else 'CAPESYSTEM' %} {% set _razao = (empresa.razao_social or empresa.nome or '') if empresa else '' %} {% set _eh_fiscal = (venda.chave_documento and venda.chave_documento | length == 44) %} {# ══════════════════════════════════════════════════════ LOGO ══════════════════════════════════════════════════════ #} {% if empresa and empresa.logo %}
{% if empresa.logo.startswith('data:') %} {% else %} {% endif %}
{% endif %} {# ══════════════════════════════════════════════════════ CABEÇALHO DA EMPRESA ══════════════════════════════════════════════════════ #}
{{ _nome_empresa.upper() }}
{% if _eh_fiscal %} {# Venda fiscal: mostra razão social, endereço, CNPJ, fone #} {% if _razao and _razao.upper() != _nome_empresa.upper() %}
{{ _razao }}
{% endif %} {% if empresa %} {% set _end = (empresa.logradouro or '') %} {% set _num = (empresa.numero or '') %} {% set _bairro = (empresa.bairro or '') %} {% set _cidade = (empresa.municipio or '') %} {% set _uf = (empresa.uf or '') %} {% set _cep_raw = (empresa.cep or '')|replace('.','') | replace('-','') | replace(' ','') %} {% if _end %}
{{ _end }}{% if _num %}, {{ _num }}{% endif %}
{% endif %} {% if _bairro %}
{{ _bairro }}
{% endif %} {% if _cidade %}
{{ _cidade }}/{{ _uf }}{% if _cep_raw|length == 8 %} - CEP: {{ _cep_raw[:5] }}-{{ _cep_raw[5:] }}{% endif %}
{% endif %} {% if empresa.cnpj %}
CNPJ: {{ empresa.cnpj }}
{% endif %} {% if empresa.telefone %}
Fone: {{ empresa.telefone }}
{% endif %} {% endif %} {% endif %}
{# ══════════════════════════════════════════════════════ TÍTULO ══════════════════════════════════════════════════════ #}
DOCUMENTO AUXILIAR DE VENDA
{# ══════════════════════════════════════════════════════ IDENTIFICAÇÃO ══════════════════════════════════════════════════════ #}
Nº {{ venda.numero_pedido or venda.id }} {{ (venda.data_venda.strftime('%d/%m/%Y') if venda.data_venda else '') }}{% if venda.data_fechamento %} {{ venda.data_fechamento.strftime('%H:%M') }}{% elif venda.data_criacao %} {{ venda.data_criacao.strftime('%H:%M') }}{% endif %}
Caixa: {{ venda.caixa_pdv.numero_caixa if venda.caixa_pdv else '-' }} Operador: {{ current_user.username }}
{% if venda.vendedor_nome or (venda.vendedor and venda.vendedor.nome) %}
Vendedor: {{ venda.vendedor_nome or venda.vendedor.nome }}
{% endif %}
{# ══════════════════════════════════════════════════════ CLIENTE ══════════════════════════════════════════════════════ #}
{% set _cli_nome = venda.get_cliente_nome() if venda.get_cliente_nome else 'Consumidor Final' %} {% if _cli_nome and _cli_nome not in ('Consumidor Final', 'CONSUMIDOR', '', 'Cliente não informado', 'Cliente nao informado') %}
CLIENTE:
  {{ _cli_nome }}
{% if venda.pessoa and venda.pessoa.cpf %} {% set _cpf = venda.pessoa.cpf | replace('.','') | replace('-','') | replace('/','') %} {% if _cpf|length == 11 %}
  CPF: {{ _cpf[:3] }}.{{ _cpf[3:6] }}.{{ _cpf[6:9] }}-{{ _cpf[9:] }}
{% elif _cpf|length == 14 %}
  CNPJ: {{ _cpf[:2] }}.{{ _cpf[2:5] }}.{{ _cpf[5:8] }}/{{ _cpf[8:12] }}-{{ _cpf[12:] }}
{% endif %} {% endif %} {% if venda.pessoa and (venda.pessoa.telefone or venda.pessoa.celular) %}
  Fone: {{ venda.pessoa.telefone or venda.pessoa.celular }}
{% endif %} {% else %}
CLIENTE: Consumidor Final
{% endif %}
{# ══════════════════════════════════════════════════════ ITENS ══════════════════════════════════════════════════════ #}
{% for item in venda.itens %} {% endfor %}
COD DESCRIÇÃO QTD UNIT TOTAL
{{ (item.produto.codigo_produto if item.produto else '') or loop.index }} {{ item.descricao or (item.produto.nome if item.produto else 'Produto') }} {%- if item.quantidade == (item.quantidade | int) -%} {{ item.quantidade | int }} {%- else -%} {{ '%.3f'|format(item.quantidade) | replace('.', ',') }} {%- endif -%} {{ '%.2f'|format(item.valor_unitario) | replace('.', ',') }} {{ '%.2f'|format(item.valor_total) | replace('.', ',') }}
{% set _total_un = venda.itens | sum(attribute='quantidade') %} QTD ITENS: {{ venda.itens | length }}  |   TOTAL UN: {%- if _total_un == (_total_un | int) %} {{ _total_un | int }}{%- else %} {{ '%.3f'|format(_total_un) | replace('.', ',') }}{%- endif %}
{# ══════════════════════════════════════════════════════ TOTAIS ══════════════════════════════════════════════════════ #}
{% if desconto_valor > 0 or acrescimo_valor > 0 %}
Subtotal:{{ br(subtotal_venda) }}
{% endif %} {% if desconto_valor > 0 %}
(-) Desconto{% if _desconto_perc > 0 %} ({{ '%.1f'|format(_desconto_perc) | replace('.', ',') }}%){% endif %}: {{ br(desconto_valor) }}
{% endif %} {% if acrescimo_valor > 0 %}
(+) Acréscimo{% if _acrescimo_perc > 0 %} ({{ '%.1f'|format(_acrescimo_perc) | replace('.', ',') }}%){% endif %}: {{ br(acrescimo_valor) }}
{% endif %}
TOTAL {{ br(valor_total) }}
{# ══════════════════════════════════════════════════════ PAGAMENTO DETALHADO ══════════════════════════════════════════════════════ #}
PAGAMENTO DETALHADO
Subtotal da venda:{{ br(valor_total) }}
{% set ns = namespace(total_desc=0.0, total_acr=0.0, total_cobrado=0.0) %} {% for pg in pagamentos %} {% set _forma = pg.get('forma_nome') or pg.get('forma') or 'Pagamento' %} {% set _valor = (pg.get('valor') or 0) | float %} {% set _receb = (pg.get('valor_recebido') or pg.get('valor') or 0) | float %} {% set _troco = (pg.get('troco') or 0) | float %} {% set _desc_v = (pg.get('valor_desconto') or pg.get('desconto_valor') or 0) | float %} {% set _desc_p = (pg.get('desconto_percentual') or 0) | float %} {% set _acr_v = (pg.get('valor_acrescimo') or pg.get('acrescimo_valor') or 0) | float %} {% set _acr_p = (pg.get('acrescimo_percentual') or 0) | float %} {% set _bandei = pg.get('bandeira') or '' %} {% set _parc = (pg.get('parcelas_qtd') or pg.get('numero_parcelas') or 1) | int %} {% set _tipo = pg.get('tipo') or '' %} {% set _eh_cred = (_tipo|lower == 'credito') or ('credito' in (_forma|lower)) or (_bandei != '') or (_parc > 1) %} {% set _cobrado_raw = (pg.get('valor_pago') or 0) | float %} {% set _cobrado = _cobrado_raw if _cobrado_raw > 0 else (_valor - _desc_v + _acr_v) %} {% set ns.total_desc = ns.total_desc + _desc_v %} {% set ns.total_acr = ns.total_acr + _acr_v %} {% set ns.total_cobrado = ns.total_cobrado + _cobrado %}
{{ loop.index }}. {{ _forma }}
{% if _eh_cred %}
{% if _bandei %}Bandeira: {{ _bandei }}  |  {% endif %}Parcelas: {{ _parc }}x
{% endif %} {% set _detalhe = pg.get('detalhe_cupom_pagamento') or '' %} {% if _detalhe and not _eh_cred %}
↳ {{ _detalhe }}
{% endif %}
  Valor:{{ br(_valor) }}
{% if _desc_v > 0 %}
  Desconto{% if _desc_p > 0 %} ({{ _desc_p | int }}%){% endif %}: -{{ br(_desc_v) }}
{% endif %} {% if _acr_v > 0 %}
  Acréscimo{% if _acr_p > 0 %} ({{ _acr_p | int }}%){% endif %}: +{{ br(_acr_v) }}
{% endif %} {% if (_cobrado - _valor) | abs > 0.009 %}
  = Cobrado:{{ br(_cobrado) }}
{% endif %} {% if _troco > 0 %}
  Troco:{{ br(_troco) }}
{% endif %}
{% endfor %}
RESUMO:
{% if ns.total_desc > 0 %}
  Total Descontos:-{{ br(ns.total_desc) }}
{% endif %} {% if ns.total_acr > 0 %}
  Total Acréscimos:+{{ br(ns.total_acr) }}
{% endif %}
  Total Cobrado:{{ br(ns.total_cobrado) }}
{% if total_troco > 0 %}
TROCO {{ br(total_troco) }}
{% endif %}
{# ══════════════════════════════════════════════════════ COMPROVANTE PIX ══════════════════════════════════════════════════════ #} {% if comprovante_pix %}
COMPROVANTE PIX
Meio:{{ comprovante_pix.meio_pagamento_label }}
Gateway:{{ comprovante_pix.gateway_label }}
Status:{{ comprovante_pix.status_label }}
Valor pago:{{ br(comprovante_pix.valor_pago) }}
{% if comprovante_pix.data_confirmacao_fmt %}
Confirmado em:{{ comprovante_pix.data_confirmacao_fmt }}
{% endif %} {% if comprovante_pix.txid %}
TXID: {{ comprovante_pix.txid }}
{% endif %} {% if comprovante_pix.gateway_transacao_id %}
Transação: {{ comprovante_pix.gateway_transacao_id }}
{% endif %} {% if comprovante_pix.gateway_cobranca_id %}
Cobrança: {{ comprovante_pix.gateway_cobranca_id }}
{% endif %} {% if comprovante_pix.referencia_externa %}
Referência: {{ comprovante_pix.referencia_externa }}
{% endif %} {% if comprovante_pix.mensagem %}
{{ comprovante_pix.mensagem }}
{% endif %}
{% endif %} {# ══════════════════════════════════════════════════════ OBSERVAÇÕES ══════════════════════════════════════════════════════ #} {% if venda.observacoes %}
OBSERVAÇÕES:
{{ venda.observacoes }}
{% endif %} {# ══════════════════════════════════════════════════════ RODAPÉ ══════════════════════════════════════════════════════ #} {% if _eh_fiscal %}
CHAVE DE ACESSO
{{ venda.chave_documento }}
{% else %}
*** NÃO É DOCUMENTO FISCAL ***
{% endif %}