<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>

        <!-- New report paperformat for din5008 format -->
        <record id="paperformat_euro_din_a" model="report.paperformat">
            <field name="name">European A4 for DIN5008 Type A</field>
            <field name="default" eval="False" />
            <field name="format">A4</field>
            <field name="orientation">Portrait</field>
            <field name="margin_top">27</field>
            <field name="margin_bottom">40</field>
            <field name="margin_left">20</field>
            <field name="margin_right">10</field>
            <field name="dpi">70</field>
            <field name="header_line" eval="False" />
            <field name="header_spacing">27</field>
        </record>

        <record id="paperformat_euro_din" model="report.paperformat">
            <field name="name">European A4 for DIN5008 Type B</field>
            <field name="default" eval="False" />
            <field name="format">A4</field>
            <field name="orientation">Portrait</field>
            <field name="margin_top">45</field>
            <field name="margin_bottom">40</field>
            <field name="margin_left">20</field>
            <field name="margin_right">10</field>
            <field name="dpi">70</field>
            <field name="header_line" eval="False" />
            <field name="header_spacing">45</field>
        </record>

        <!-- New report layout for din5008 format -->
        <template id="external_layout_din5008">
            <div>
                <div t-attf-class="header din_page o_company_#{company.id}_layout #{'din_page_pdf' if report_type == 'pdf' else ''}">
                    <table class="company_header table-borderless" t-att-style="'height: %dmm;' % (din_header_spacing or 27)">
                        <tr>
                            <td><div class="h3 mt0" t-field="company.report_header"/></td>
                            <td><img t-if="company.logo" t-att-src="image_data_uri(company.logo)" t-att-style="'max-height: %dmm;' % (din_header_spacing or 27)"/></td>
                        </tr>
                    </table>
                </div>

                <div t-attf-class="din_page invoice_note article o_company_#{company.id}_layout {{'o_report_layout_background' if company.layout_background in ['Geometric', 'Custom']  else  ''}} #{'din_page_pdf' if report_type == 'pdf' else ''}"
                     t-attf-style="background-image: url({{ 'data:image/png;base64,%s' % company.layout_background_image.decode('utf-8') if company.layout_background_image and company.layout_background == 'Custom' else '/base/static/img/bg_background_template.jpg' if company.layout_background == 'Geometric' else ''}});"
                     t-att-data-oe-model="o and o._name"
                     t-att-data-oe-id="o and o.id">
                    <table class="din_company_info table-borderless" t-if="not skip_headers">
                        <tr>
                            <td>
                                <div class="address">
                                    <t t-if="company.name">
                                        <span t-field="company.name"/>
                                    </t>
                                    <t t-if="company.street">
                                        <span>|</span> <span t-field="company.street"/>
                                    </t>
                                    <t t-if="company.street2">
                                        <span>|</span> <span t-field="company.street2"/>
                                    </t>
                                    <t t-if="company.zip">
                                        <span>|</span> <span t-field="company.zip"/>
                                    </t>
                                    <t t-if="company.city">
                                        <span t-if="not company.zip">|</span> <span t-field="company.city"/>
                                    </t>
                                    <t t-if="company.country_id">
                                        <span>|</span> <span t-field="company.country_id.name"/>
                                    </t>
                                    <hr class="company_invoice_line" />
                                    <t t-if="o and 'l10n_din5008_addresses' in o" t-set="address">
                                        <address class="mb-0" t-field="o.partner_id.commercial_partner_id" t-options='{"widget": "contact", "fields": ["address", "name", "phone"], "no_marker": True}'/>
                                        <div t-if="o.partner_id.commercial_partner_id.vat" id="partner_vat_address_same_as_shipping">
                                            <t t-if="o.company_id.account_fiscal_country_id.vat_label" t-out="o.company_id.account_fiscal_country_id.vat_label" id="inv_tax_id_label"/>
                                            <t t-else="">Tax ID</t>: <span t-field="o.partner_id.commercial_partner_id.vat"/>
                                        </div>
                                    </t>
                                    <div t-if="address">
                                        <t t-out="address"/>
                                    </div>
                                    <div t-else="fallback_address">
                                        <t t-out="fallback_address"
                                           t-options='{"widget": "contact", "fields": ["address", "name"], "no_marker": True}' />
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="information_block">
                                    <t t-if="'l10n_din5008_template_data' in company" t-set="template_data" t-value="company.l10n_din5008_template_data"/>
                                    <t t-if="o and 'l10n_din5008_template_data' in o" t-set="template_data" t-value="o.l10n_din5008_template_data"/>
                                    <table>
                                        <t t-foreach="template_data" t-as="row">
                                            <tr><td><t t-out="row[0]"/></td><td><t t-out="row[1]"/></td></tr>
                                        </t>
                                    </table>
                                </div>
                            </td>
                        </tr>
                        <div>
                            <tr t-if="o and 'l10n_din5008_addresses' in o">
                                <t t-foreach="o.l10n_din5008_addresses" t-as="doc_address">
                                    <td>
                                        <div class="shipping_address">
                                            <strong><t t-esc="doc_address[0]"/></strong>
                                            <div t-esc="doc_address[1]" t-options='{"widget": "contact", "fields": ["address", "name", "phone"], "no_marker": True}'/>
                                        </div>
                                    </td>
                                </t>
                            </tr>
                        </div>
                    </table>
                    <h2 t-if="not skip_headers">
                        <span t-if="not o and not docs"><t t-out="company.l10n_din5008_document_title"/></span>
                        <span t-else="">
                            <t t-set="o" t-value="docs[0]" t-if="not o" />
                            <span t-if="'l10n_din5008_document_title' in o"><t t-out="o.l10n_din5008_document_title"/></span>
                            <span t-elif="'name' in o" t-field="o.name"/>
                        </span>
                    </h2>
                    <t t-out="0"/>
                </div>

                <div t-attf-class="din_page footer o_company_#{company.id}_layout #{'din_page_pdf' if report_type == 'pdf' else ''}">
                    <div class="text-end page_number">
                        <div class="text-muted">
                            Page: <span class="page"/> of <span class="topage"/>
                        </div>
                    </div>
                    <div class="company_details">
                        <table class="table-borderless">
                            <tr>
                                <td>
                                    <ul class="list-inline text-nowrap">
                                        <li t-if="company.company_details"><span t-field="company.company_details"/></li>
                                    </ul>
                                </td>
                                <td>
                                    <ul class="list-inline">
                                        <li t-if="company.report_footer"><span t-field="company.report_footer"/></li>
                                    </ul>
                                </td>
                                <td>
                                    <ul class="list-inline">
                                        <li t-if="company.vat"><t t-out="company.account_fiscal_country_id.vat_label or 'Tax ID'"/>:
                                            <span t-if="forced_vat" t-out="forced_vat"/>
                                            <span t-else="" t-field="company.vat"/>
                                        </li>
                                        <li>HRB Nr: <span t-field="company.company_registry"/></li>
                                    </ul>
                                </td>
                                <td t-if="company.partner_id.bank_ids">
                                    <ul class="list-inline">
                                        <t t-foreach="company.partner_id.bank_ids[:2]" t-as="bank">
                                            <li><span t-field="bank.bank_id.name"/></li>
                                            <li>IBAN: <span t-field="bank.acc_number"/></li>
                                            <li>BIC: <span t-field="bank.bank_id.bic"/></li>
                                        </t>
                                    </ul>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </template>

        <template id="din5008_css" inherit_id="web.styles_company_report">
            <xpath expr="//t[@t-elif]" position="before">
                <t t-elif="layout == 'l10n_din5008.external_layout_din5008'">
                    &amp;.din_page {
                        &amp;.header {
                            .company_header {
                                .name_container {
                                    color: <t t-out='primary'/>;
                                }
                            }
                        }
                        &amp;.invoice_note {
                            td {
                                .address {
                                    > span {
                                        color: <t t-out='secondary'/>;
                                    }
                                }
                            }
                            h2 {
                                color: <t t-out='primary'/>;
                            }
                            .page {
                                [name=invoice_line_table], [name=stock_move_table], .o_main_table {
                                    th {
                                        color: <t t-out='secondary'/>;
                                    }
                                }
                            }
                        }
                    }
                </t>
            </xpath>
        </template>
    </data>
</odoo>
