<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="action_facturas_de_clientes_print_report" model="ir.actions.report">
        <field name="name">Factura</field>
        <field name="model">ir.attachment</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">l10n_mx_sat_sync_itadmin_ee.facturas_de_clientes</field>
        <field name="report_file">l10n_mx_sat_sync_itadmin_ee.facturas_de_clientes</field>
        <field name="binding_model_id" ref="model_ir_attachment"/>
        <field name="binding_type">report</field>
    </record>

	 <template id="facturas_de_clientes">
		    <t t-call="web.html_container">
		        <t t-foreach="docs" t-as="o">
		        	<t t-set="lang" t-value="o.env.user.lang"/>
		            <t t-call="l10n_mx_sat_sync_itadmin_ee.report_facturas_de_clientes_document" t-lang="lang"/>
		        </t>
		    </t>
	</template>
	<template id="report_facturas_de_clientes_document">
        <t t-call="web.external_layout">
            <t t-set="o" t-value="o.with_context(lang=lang)" />
            <t t-set="xml" t-value="l10n_mx_edi_get_xml_etree(o.datas)"/>
            <t t-set="tfd" t-value="l10n_mx_edi_get_tfd_etree(xml)"/>
            <t t-set="tfd_original_string" t-value="_get_l10n_mx_edi_cadena(o.datas)"/>
            <t t-set="supplier_rfc" t-value="xml.Receptor.get('Rfc', xml.Receptor.get('rfc'))"/>
            <t t-set="customer_rfc" t-value="xml.Emisor.get('Rfc', xml.Emisor.get('rfc'))"/>    
            <t t-set="address">
                <strong> Emisor </strong> <br/>
              	<address t-esc="xml.Emisor.get('Nombre', '')" />
             	<div class="mt16"><t t-esc="o.company_id.country_id.vat_label or 'Tax ID'"/>: <span t-esc="xml.Emisor.get('Rfc', '')"/></div>

                <t t-if="o.cfdi_type=='I'">
                	<t t-set="supplier_rfc" t-value="xml.Receptor.get('Rfc', xml.Receptor.get('rfc'))"/>
                	<t t-set="customer_rfc" t-value="xml.Emisor.get('Rfc', xml.Emisor.get('rfc'))"/> 
                </t>
                <t t-if="o.cfdi_type=='SI'">
                	<!--<address t-esc="xml.Receptor.get('Nombre', '')" />
                	<div class="mt16"><t t-esc="o.company_id.country_id.vat_label or 'Tax ID'"/>: <span t-esc="xml.Receptor.get('Rfc', '')"/></div> -->
                	<t t-set="supplier_rfc" t-value="xml.Emisor.get('Rfc', xml.Emisor.get('rfc'))"/>
                	<t t-set="customer_rfc" t-value="xml.Receptor.get('Rfc', xml.Receptor.get('rfc'))"/>
                </t>
                <!--<span t-if="o.company_id.vat != supplier_rfc">XML VAT: <span t-esc="supplier_rfc"></span></span>-->
	            
            </t>
            <div class="page">
                <h2>
                    <span t-if="o.cfdi_type=='I'">Factura</span>
                    <!-- <span t-if="o.type == 'out_invoice' and o.state == 'draft'">Draft Invoice</span>
                    <span t-if="o.type == 'out_invoice' and o.state == 'cancel'">Cancelled Invoice</span>
                    <span t-if="o.type == 'out_refund'">Credit Note</span>
                    <span t-if="o.type == 'in_refund'">Vendor Credit Note</span> -->
                    <span t-if="o.cfdi_type=='SI'">Proveedor</span>
                    <span t-esc="xml.get('Serie','')"/>
	    	    <span t-esc="xml.get('Folio','')"/>
                </h2>

                <div id="informations" class="row mt32 mb32">
                    <!-- <div class="col-auto mw-100 mb-2" t-if="o.name" name="description">
                        <strong>Description:</strong>
                        <p class="m-0" t-field="o.name"/>
                    </div> -->
                    <div class="col-auto mw-100 mb-2" name="invoice_date">
                        <strong>Invoice Date:</strong>
                        <p class="m-0" t-esc="xml.get('Fecha','')"/>
                    </div>
                    <!-- <div class="col-auto mw-100 mb-2" t-if="o.date_due and o.type == 'out_invoice' and o.state in ('open', 'in_payment', 'paid')" name="due_date">
                        <strong>Due Date:</strong>
                        <p class="m-0" t-field="o.date_due"/>
                    </div>
                    <div class="col-auto mw-100 mb-2" t-if="o.origin" name="origin">
                        <strong>Source:</strong>
                        <p class="m-0" t-field="o.origin"/>
                    </div>
                    <div class="col-auto mw-100 mb-2" t-if="o.partner_id.ref" name="customer_code">
                        <strong>Customer Code:</strong>
                        <p class="m-0" t-field="o.partner_id.ref"/>
                    </div> -->
                  <!--  <div name="reference" class="col-auto mw-100 mb-2">
                        <strong>Reference:</strong>
                        <p class="m-0" t-esc="xml.get('Serie','')"/>
                    </div>
                    -->
                    <t t-if="xml.Receptor.get('UsoCFDI', False)">
	                    <t t-set="usage" t-value="xml.Receptor.get('UsoCFDI')"/>
	                    <div name="usage" t-att-class="o_grid_layout">
	                        <strong>Uso:</strong>
	                        <p><span t-esc="usage"/> <!-- - <span t-field="o.l10n_mx_edi_usage"/> --></p>
	                    </div>
	                </t>
	                <!-- <t t-if="o.l10n_mx_edi_payment_method_id">
	                    <div t-att-class="o_grid_layout">
	                        <strong>Payment Way:</strong>
	                        <p t-esc="' - '.join([o.l10n_mx_edi_payment_method_id.code, o.l10n_mx_edi_payment_method_id.name])"/>
	                    </div>
	                </t> -->
	                <div t-att-class="o_grid_layout">
	                    <strong>Método de pago:</strong>
	                    <p t-esc="xml.get('formaDePago', xml.get('MetodoPago'))"/>
	                </div>
	                <div t-att-class="o_grid_layout">
	                    <strong>Forma de pago:</strong>
	                    <p t-esc="xml.get('formaDePago', xml.get('FormaPago'))"/>
	                </div>
			<div t-att-class="o_grid_layout">
			    <strong>Tipo de comprobante:</strong>
			    <p t-esc="xml.get('TipoDeComprobante')"/>
			</div>
			<div t-att-class="o_grid_layout">
			    <strong>Moneda:</strong>
			    <p t-esc="xml.get('Moneda')"/>
			</div>


	                <div t-att-class="o_grid_layout" t-if="xml.get('NumCtaPago')">
	                    <strong>Bank Account:</strong>
	                    <p t-esc="xml.get('NumCtaPago')"/>
	                </div>
                </div>

                <!-- <t t-set="display_discount" t-value="any([l.discount for l in o.invoice_line_ids])"/> -->

                <table class="table table-sm o_main_table" name="invoice_line_table">
                    <thead>
                        <tr>
                            <!-- TODO: remove in master -->
                            <t t-set="colspan" t-value="5"/>
                            <th t-if="xml.get('Version')"><span>Código</span></th>
                            <th class="text-left"><span>Descripción</span></th>
                            <!-- <th class="d-none text-left"><span>Source Document</span></th> -->
                            <th class="text-right"><span>Cantidad</span></th>
                            <th class="text-right" t-if="xml.get('Version')"><span>Código unidad</span></th>
                            <th t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}"><span>Precio unitario</span></th>
                            <!-- <th t-if="display_discount" t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                <span>Disc.(%)</span>
                                <t t-set="colspan" t-value="colspan+1"/>
                            </th> -->
                            <th t-attf-class="text-left {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}"><span>Impuestos</span></th>
                            <th class="text-right">
                                <span groups="account.group_show_line_subtotals_tax_excluded">Cantidad</span>
                                <span groups="account.group_show_line_subtotals_tax_included">Precio total</span>
                            </th>
                        </tr>
                    </thead>
                    <tbody class="invoice_tbody">
                        <t t-set="current_subtotal" t-value="0"/>

                        <t t-foreach="xml.Conceptos.Concepto" t-as="line">
							<t t-set="tax_per_amount" t-value="get_tax_amount(line)" />
							<t t-set="price_subtotal" t-value="round(float(line.get('ValorUnitario','0.0'))*float(line.get('Cantidad','0.0')),2)" />
							<t t-set="price_total" t-value="round(float(line.get('ValorUnitario','0.0'))*float(line.get('Cantidad','0.0')),2) + tax_per_amount[1]" />
							
                            <t t-set="current_subtotal" t-value="current_subtotal + price_subtotal" groups="account.group_show_line_subtotals_tax_excluded"/>
                            <t t-set="current_subtotal" t-value="current_subtotal + price_total" groups="account.group_show_line_subtotals_tax_included"/>

                            <tr> <!-- t-att-class="'bg-200 font-weight-bold o_line_section' if line.display_type == 'line_section' else 'font-italic o_line_note' if line.display_type == 'line_note' else ''" -->
                                <!-- <t t-if="not line.display_type" name="account_invoice_line_accountable"> -->
                                <td t-if="xml.get('Version')"><span t-esc="line.get('ClaveProdServ','')"/></td>
                                <td name="account_invoice_line_name"><span t-esc="line.get('Descripcion','')"/></td>
                                <!-- <td class="d-none"><span t-field="line.origin"/></td> -->
                                <td class="text-right">
                                    <span t-esc="line.get('Cantidad','')"/>
                                    <span t-esc="line.get('Unidad','')"  groups="uom.group_uom"/>
                                </td>
                                <td class="text-right" t-if="xml.get('Version')"><span t-esc="line.get('ClaveUnidad','')"/></td>
                                <td t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                    <span t-esc="line.get('ValorUnitario','0.0')"/>
                                </td>
                                <!-- <td t-if="display_discount" t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                    <span t-field="line.discount"/>
                                </td> -->
                                <td t-attf-class="text-left {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                    <!-- <span t-esc="', '.join(map(lambda x: (x.description or x.name), line.invoice_line_tax_ids))"/> -->
                                    <span t-esc="tax_per_amount[0]"/>
                                </td>
                                <td class="text-right o_price_total">
                                    <span t-esc="price_subtotal" groups="account.group_show_line_subtotals_tax_excluded"/>
                                    <span t-esc="price_total" groups="account.group_show_line_subtotals_tax_included"/>
                                </td>
                                <!-- </t>
                                <t t-if="line.display_type == 'line_section'">
                                    <td colspan="99">
                                        <span t-field="line.name"/>
                                    </td>
                                    <t t-set="current_section" t-value="line"/>
                                    <t t-set="current_subtotal" t-value="0"/>
                                </t>
                                <t t-if="line.display_type == 'line_note'">
                                    <td colspan="99">
                                        <span t-field="line.name"/>
                                    </td>
                                </t> -->
                            </tr>

                            <!-- <t t-if="current_section and (line_last or o.invoice_line_ids[line_index+1].display_type == 'line_section')">
                                <tr class="is-subtotal text-right">
                                    <td colspan="99">
                                        <strong class="mr16">Subtotal</strong>
                                        <span
                                            t-esc="current_subtotal"
                                            t-options='{"widget": "monetary", "display_currency": o.currency_id}'
                                        />
                                    </td>
                                </tr>
                            </t> -->
                        </t>
                    </tbody>
                </table>

                <div class="clearfix">
                    <div id="total" class="row" style="font-size:12px;">
                        <div t-attf-class="#{'col-4' if report_type != 'html' else 'col-sm-7 col-md-5'} ml-auto">
                            <table class="table table-sm;page-break-inside: avoid;">
                                <tr class="border-black o_subtotal" style="font-size:12px;">
                                    <td><strong>Subtotal</strong></td>
                                    <td class="text-right">
                                        <span t-esc="xml.get('SubTotal')"/>
                                    </td>
                                </tr>
                                <t t-set="tax_amount_by_per" t-value="get_tax_amount_by_percent(xml.Conceptos.Concepto)" />
                                <t t-foreach="tax_amount_by_per" t-as="amount_by_group">
                                    <tr style="">
                                        <td><span t-esc="amount_by_group"/></td>
                                        <td class="text-right o_price_total">
                                            <span t-esc="round(tax_amount_by_per[amount_by_group],2)" />
                                        </td>
                                    </tr>
                                </t>
                                <tr class="border-black o_total" style="font-size:12px;">
                                    <td><strong>Total</strong></td>
                                    <td class="text-right">
                                        <span t-esc="xml.get('Total')"/>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>

                <p>
                    <span t-esc="l10n_mx_edi_amount_to_text(xml.get('Moneda','MXN'), float(xml.get('Total')))"/>
                </p>
	            
                <!-- <p t-if="o.comment" name="comment">
                    <span t-field="o.comment"/>
                </p>
                <p t-if="o.payment_term_id" name="payment_term">
                    <span t-field="o.payment_term_id.note"/>
                </p>
                <p t-if="o.fiscal_position_id.note" name="note">
                    <span t-field="o.fiscal_position_id.note"/>
                </p>
                <div id="qrcode" t-if="(o.company_id.qr_code) and (o.currency_id.name == 'EUR') and (o.partner_bank_id.acc_number != False)">
                    <p t-if="(o.partner_bank_id.qr_code_valid)">
                        <strong class="text-center">Scan me with your banking app.</strong><br /><br />
                        <img class="border border-dark rounded" t-att-src="o.partner_bank_id.build_qr_code_url(o.residual,(o.reference) if (o.reference) else o.number)"/>
                    </p>
                    <p t-if="(o.partner_bank_id.qr_code_valid == False)">
                        <strong class="text-center">The SEPA QR Code informations are not set correctly.</strong><br />
                    </p>
                </div> -->
                
                <div class="row" id='complement'>
                    <div class="barcode col-3">
                        <t t-set="sello" t-value="xml.get('Sello', 'No identificado')[-8:]"/>
                        <img alt="Barcode" t-att-src="'/report/barcode/?type=QR&amp;value=%s&amp;width=180&amp;height=180' % quote_plus(
                            'https://verificacfdi.facturaelectronica.sat.gob.mx/default.aspx?' + keep_query(
                                re=supplier_rfc, rr=customer_rfc,
                                tt=float(xml.get('Total')), id=tfd.get('UUID'))
                                + '&amp;fe=%s' % quote_plus(
                                    sello, 'utf-8', 'strict', '=/').replace('%2B', '+'))"/>
                    </div>
                    <div class="complement-details col-9">
                        <div class="digital-stamp">
                            <span>Digital stamp of the emitter</span>
                        </div>
                        <div class="digital-stamp-content">
                            <span t-esc="xml.get('sello', xml.get('Sello', 'No identificado'))"/>
                        </div>
                        <div class="digital-stamp">
                            <span>Digital stamp SAT</span>
                        </div>
                        <div class="digital-stamp-content">
                            <span t-esc="tfd.get('selloSAT', tfd.get('SelloSAT', 'No identificado'))"/>
                        </div>
                        <div class="digital-stamp">
                            <span>Original chain complement of digital certification SAT</span>
                        </div>
                        <div class="digital-stamp-content">
                            <span class="nowrap" t-esc="tfd_original_string"/>
                        </div>
                        <div t-if="xml.Emisor.xpath('cfdi:ExpedidoEn', namespaces=xml.nsmap)" class="digital-stamp">
                            <span>Issued from</span>
                        </div>
                        <div t-if="xml.Emisor.xpath('cfdi:ExpedidoEn', namespaces=xml.nsmap)" class="digital-stamp-content">
                            <span t-esc="' | '.join([ '%s: %s' % (key, value) for key, value in xml.Emisor.ExpedidoEn.items()])"/>
                        </div>
                        <div class="digital-stamp">
                            <span>Extra Info</span>
                        </div>
                        <div class="digital-stamp-content">
                            <span> | Certificado de emisor:</span> <span t-esc="xml.get('noCertificado', xml.get('NoCertificado'))"/>
                            <span> | Certificado SAT:</span> <span t-esc="tfd.get('NoCertificadoSAT')"/>
                            <span> | Lugar de expedición:</span> <span t-esc="xml.get('LugarExpedicion')"/>
                            <span> | Regimen Fiscal:</span><span t-esc="xml.Emisor.get('RegimenFiscal', '')"/>
                            <span> | Fecha de emisión:</span> <span t-esc="xml.get('fecha', xml.get('Fecha', '')).replace('T', ' ')"/>
                            <span> | Fecha de certificación:</span> <span t-esc="tfd.get('FechaTimbrado', '').replace('T', ' ')"/>
                            <span> | Folio fiscal:</span> <span t-esc="tfd.get('UUID')"/>
                        </div>
                        <div class="digital-stamp-content text-center">
                            <strong>Este documento es una representación impresa de un CFDI</strong>
                        </div>
                    </div>
                </div>
            </div>
        </t>
    </template>
    
   <!-- <template id="report_facturas_de_clientes_document">
		<t t-call="web.html_container">
       	 	<t t-call="web.external_layout">
				<div class="page"> 		
        	    			<table class="table table-sm" >
			                   <tbody>
			                    <thead>
			                        <tr>
			                           <th class="text-right"><span>Clave Producto</span></th>
			                            <th class="text-right"><span>Cantidad</span></th>
			                            <th class="text-right"><span>Unidad</span></th>
			                            <th class="text-right"><span>Descripcion</span></th>
			                            <th class="text-right"><span>Precio Unitario</span></th>
			                            <th class="text-right"><span>Importe</span></th>
			                            <th class="text-right"><span>Impuesto</span></th>
			                            <th class="text-right"><span>Tipo Factor</span></th>
			                            <th class="text-right"><span>Tasa Cuota</span></th>
			                            <th class="text-right"><span>Importe Impuesto</span></th>
			                                                      
			                        </tr>
			                    </thead>
                   		 </tbody>
                </table>
    	</div>
    	</t>
    	</t>
    	
    	</template>	 -->

</odoo>
