<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="account_nilvera_view_move_form" model="ir.ui.view">
        <field name="name">account.nilvera.view.move.form</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="arch" type="xml">
            <xpath expr="//group[@name='accounting_info_group']/field[@name='invoice_incoterm_id']" position="attributes">
                <attribute name="required" add="l10n_tr_is_export_invoice" separator=" or "/>
            </xpath>
            <xpath expr="//header" position="after">
                <div class="alert alert-info" role="alert" invisible="state != 'draft' or l10n_tr_nilvera_send_status != 'error' or not l10n_tr_nilvera_uuid">
                    To preserve accounting integrity and comply with legal requirements, invoices cannot be reused once an error occurs. Please create a new invoice to continue.
                </div>
            </xpath>
            <xpath expr="//button[@name='action_post'][2]" position="attributes">
                <attribute name="invisible" separator=" or " add="l10n_tr_nilvera_send_status == 'error' and l10n_tr_nilvera_uuid"/>
            </xpath>
            <xpath expr="//div[@name='journal_div']" position="after">
                <label for="l10n_tr_nilvera_send_status" invisible="country_code != 'TR' or state == 'draft' or move_type not in ['out_invoice', 'out_refund']"/>
                <div name="nilvera_div"
                     class="d-flex"
                     invisible="country_code != 'TR' or state == 'draft' or move_type not in ['out_invoice', 'out_refund']">
                    <field name="l10n_tr_nilvera_send_status" class="oe_inline"/>
                </div>
                <field name="l10n_tr_is_export_invoice"
                       invisible="l10n_tr_nilvera_customer_status != 'earchive'"
                       readonly="state in ['cancel', 'posted']"/>
                <div class="o_td_label"
                     invisible="l10n_tr_is_export_invoice
                                or country_code != 'TR'
                                or move_type not in ['out_invoice', 'out_refund']
                                or l10n_tr_nilvera_customer_status not in ['einvoice', 'earchive']">
                    <label for="l10n_tr_gib_invoice_scenario" invisible="l10n_tr_nilvera_customer_status != 'einvoice'" />
                    <label string="Invoice Type"
                           for="l10n_tr_gib_invoice_type"
                           invisible="l10n_tr_nilvera_customer_status != 'earchive'"/>
                </div>
                <div class="d-flex"
                     invisible="l10n_tr_is_export_invoice
                                or country_code != 'TR'
                                or move_type not in ['out_invoice', 'out_refund']
                                or l10n_tr_nilvera_customer_status not in ['einvoice', 'earchive']">
                    <field name="l10n_tr_gib_invoice_scenario"
                           invisible="l10n_tr_nilvera_customer_status != 'einvoice'"
                           readonly="state in ['cancel', 'posted']"/>
                    <span class="o_form_label mx-3" invisible="l10n_tr_nilvera_customer_status != 'einvoice'">is</span>
                    <field name="l10n_tr_gib_invoice_type"
                           required="l10n_tr_nilvera_customer_status == 'einvoice' and l10n_tr_gib_invoice_scenario"
                           invisible="l10n_tr_nilvera_customer_status not in ['einvoice', 'earchive']"
                           readonly="state in ['cancel', 'posted']"/>
                </div>
                <field name="l10n_tr_exemption_code_id"
                       domain="[('code_type', 'in', l10n_tr_exemption_code_domain_list or [])]"
                       invisible="not l10n_tr_exemption_code_domain_list
                                  or l10n_tr_nilvera_customer_status not in ['einvoice', 'earchive']"
                       required="l10n_tr_is_export_invoice or l10n_tr_gib_invoice_type in ('IHRACKAYITLI', 'ISTISNA')"
                       readonly="state in ['cancel', 'posted']"/>
                <field name="l10n_tr_shipping_type"
                       required="l10n_tr_is_export_invoice"
                       invisible="not l10n_tr_exemption_code_domain_list
                                  or l10n_tr_nilvera_customer_status != 'earchive'
                                  or not l10n_tr_is_export_invoice"
                       readonly="state in ['cancel', 'posted']"/>
            </xpath>
            <xpath expr="//field[@name='invoice_line_ids']//list//field[@name='tax_ids']" position="after">
                <field name="l10n_tr_ctsp_number" optional="hide"/>
                <field name="l10n_tr_seller_line_code" optional="hide"/>
                <field name="l10n_tr_customer_line_code" optional="hide"/>
                <field name="l10n_tr_original_quantity"
                       required="parent.l10n_tr_gib_invoice_type == 'TEVKIFATIADE'"
                       column_invisible="parent.l10n_tr_gib_invoice_type != 'TEVKIFATIADE'"/>
                <field name="l10n_tr_original_tax_without_withholding"
                       required="parent.l10n_tr_gib_invoice_type == 'TEVKIFATIADE'"
                       column_invisible="parent.l10n_tr_gib_invoice_type != 'TEVKIFATIADE'"/>
            </xpath>
            <xpath expr="//header/field[@name='state']" position="before">
                <button name="l10n_tr_nilvera_get_pdf"
                        string="Fetch Nilvera PDF"
                        type="object"
                        invisible="move_type not in ['out_invoice', 'out_refund'] or l10n_tr_nilvera_send_status != 'succeed'"/>
            </xpath>
            <xpath expr="//group[@name='sale_info_group']/field[@name='ref']" position="attributes">
                <attribute name="required">country_code == 'TR' and not reversed_entry_id and move_type == 'out_refund'</attribute>
            </xpath>
            <xpath expr="//group[@name='sale_info_group']/field[@name='ref']" position="after">
                <field name="l10n_tr_original_invoice_date" string="Original Invoice Date" invisible="country_code != 'TR' or move_type != 'out_refund' or reversed_entry_id" required="country_code == 'TR' and not reversed_entry_id and move_type == 'out_refund'"/>
            </xpath>
        </field>
    </record>

    <record id="account_nilvera_view_account_invoice_filter" model="ir.ui.view">
        <field name="name">account.nilvera.invoice.select</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_account_invoice_filter"/>
        <field name="arch" type="xml">
            <xpath expr="//filter[@name='accounting_date']" position="after">
                <filter string="Nilvera Status" name="nilvera_status" context="{'group_by': 'l10n_tr_nilvera_send_status'}"/>
            </xpath>
        </field>
    </record>

    <record id="account_nilvera_view_invoice_tree" model="ir.ui.view">
        <field name="name">account.nilvera.invoice.list</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_invoice_tree"/>
        <field name="arch" type="xml">
            <button name="action_force_register_payment" position="after">
                <button name="l10n_tr_nilvera_get_pdf" type="object" string="Fetch Nilvera PDF"/>
            </button>
            <xpath expr="//field[@name='abnormal_date_warning']" position="after">
                <field name="l10n_tr_nilvera_send_status"
                    string="Nilvera Status"
                    widget="badge"
                    decoration-danger="l10n_tr_nilvera_send_status == 'error'"
                    decoration-warning="l10n_tr_nilvera_send_status in ('sent', 'waiting')"
                    decoration-success="l10n_tr_nilvera_send_status == 'succeed'"
                    optional="hide"/>
            </xpath>
        </field>
    </record>
</odoo>
