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

    <!-- ================================================================= -->
    <!-- Inter-Company Configuration in Settings                           -->
    <!-- ================================================================= -->

    <record id="res_config_settings_intercompany_view" model="ir.ui.view">
        <field name="name">res.config.settings.view.form.fusion.intercompany</field>
        <field name="model">res.config.settings</field>
        <field name="inherit_id" ref="account.res_config_settings_view_form"/>
        <field name="priority" eval="55"/>
        <field name="arch" type="xml">
            <block id="invoicing_settings" position="inside">
                <setting string="Inter-Company Invoice Sync" company_dependent="1"
                         help="Automatically create matching bills/invoices between companies">
                    <field name="fusion_intercompany_invoice_enabled"/>
                    <div class="content-group" invisible="not fusion_intercompany_invoice_enabled">
                        <div class="row mt8">
                            <label for="fusion_intercompany_invoice_journal_id"
                                   class="col-lg-5 o_light_label"
                                   string="Default Journal"/>
                            <field name="fusion_intercompany_invoice_journal_id"
                                   domain="[('type', 'in', ('sale', 'purchase'))]"/>
                        </div>
                        <div class="text-muted mt8">
                            When posting an invoice to a partner that represents another
                            company, a corresponding bill (or invoice) will be created
                            automatically in the target company.
                        </div>
                    </div>
                </setting>
            </block>
        </field>
    </record>

    <!-- ================================================================= -->
    <!-- 3-Way Match Status on Invoice Form                                -->
    <!-- ================================================================= -->

    <record id="view_move_form_3way_match" model="ir.ui.view">
        <field name="name">account.move.form.fusion.3way.match</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority" eval="60"/>
        <field name="arch" type="xml">
            <!-- Add 3-Way Match fields to the Other Info tab -->
            <xpath expr="//page[@id='other_tab']//group[@name='accounting_info_group']" position="inside">
                <field name="fusion_3way_match_status"
                       invisible="move_type not in ('in_invoice', 'in_refund')"
                       widget="badge"
                       decoration-success="fusion_3way_match_status == 'matched'"
                       decoration-warning="fusion_3way_match_status == 'partial'"
                       decoration-danger="fusion_3way_match_status == 'unmatched'"
                       decoration-muted="fusion_3way_match_status == 'not_applicable'"/>
            </xpath>

            <!-- Add 3-Way Match amounts group -->
            <xpath expr="//page[@id='other_tab']//group[@name='accounting_info_group']" position="after">
                <group string="3-Way Matching Details"
                       invisible="fusion_3way_match_status == 'not_applicable' or move_type not in ('in_invoice', 'in_refund')">
                    <group>
                        <field name="fusion_3way_po_amount"/>
                        <field name="fusion_3way_received_qty_value"/>
                        <field name="fusion_3way_bill_amount"/>
                    </group>
                    <group>
                        <button name="action_refresh_3way_match"
                                type="object"
                                string="Refresh Match"
                                class="btn-secondary"
                                icon="fa-refresh"
                                invisible="fusion_3way_match_status == 'not_applicable'"/>
                        <!-- Purchase order view button removed: requires purchase module -->
                    </group>
                </group>
            </xpath>

            <!-- Add 3-Way Match badge to the header area -->
            <xpath expr="//div[hasclass('oe_title')]" position="after">
                <div class="d-flex align-items-center gap-2 mb-2"
                     invisible="fusion_3way_match_status in ('not_applicable', False) or move_type not in ('in_invoice', 'in_refund')">
                    <field name="fusion_3way_match_status"
                           widget="badge"
                           decoration-success="fusion_3way_match_status == 'matched'"
                           decoration-warning="fusion_3way_match_status == 'partial'"
                           decoration-danger="fusion_3way_match_status == 'unmatched'"/>
                </div>
            </xpath>
        </field>
    </record>

    <!-- ================================================================= -->
    <!-- Debit Note Button on Invoice Form                                 -->
    <!-- ================================================================= -->

    <record id="view_move_form_debit_note" model="ir.ui.view">
        <field name="name">account.move.form.fusion.debit.note</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority" eval="61"/>
        <field name="arch" type="xml">
            <!-- Add Debit Note button in the button box -->
            <xpath expr="//div[hasclass('oe_button_box')]" position="inside">
                <field name="fusion_debit_note_count" invisible="1"/>
                <button name="action_view_debit_notes"
                        class="oe_stat_button"
                        icon="fa-file-text-o"
                        type="object"
                        invisible="fusion_debit_note_count == 0">
                    <field name="fusion_debit_note_count" string="Debit Notes" widget="statinfo"/>
                </button>
            </xpath>

            <!-- Add Create Debit Note action button -->
            <xpath expr="//button[@name='action_register_payment']" position="after">
                <button name="action_create_debit_note"
                        type="object"
                        string="Create Debit Note"
                        class="btn-secondary"
                        groups="account.group_account_user"
                        invisible="state != 'posted' or move_type not in ('out_invoice', 'in_invoice')"
                        data-hotkey="shift+d"/>
            </xpath>

            <!-- Show origin debit note link in Other Info -->
            <xpath expr="//page[@id='other_tab']//group[@name='accounting_info_group']" position="inside">
                <field name="fusion_debit_note_origin_id"
                       invisible="not fusion_debit_note_origin_id"
                       readonly="1"/>
            </xpath>
        </field>
    </record>

    <!-- ================================================================= -->
    <!-- Inter-Company Counter-Document Links on Invoice Form              -->
    <!-- ================================================================= -->

    <record id="view_move_form_intercompany_links" model="ir.ui.view">
        <field name="name">account.move.form.fusion.intercompany</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority" eval="62"/>
        <field name="arch" type="xml">
            <xpath expr="//page[@id='other_tab']//group[@name='accounting_info_group']" position="inside">
                <field name="fusion_intercompany_move_id"
                       invisible="not fusion_intercompany_move_id"
                       readonly="1"
                       string="IC Counter-Document"/>
                <field name="fusion_intercompany_source_id"
                       invisible="not fusion_intercompany_source_id"
                       readonly="1"
                       string="IC Source Document"/>
            </xpath>
        </field>
    </record>

    <!-- ================================================================= -->
    <!-- Account Transfer Action (menu entry)                              -->
    <!-- ================================================================= -->

    <record id="action_fusion_account_transfer" model="ir.actions.act_window">
        <field name="name">Account Transfer</field>
        <field name="res_model">fusion.account.transfer</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
        <field name="context">{}</field>
    </record>

    <menuitem id="menu_fusion_account_transfer"
              name="Account Transfer"
              parent="account.menu_finance_entries"
              action="action_fusion_account_transfer"
              sequence="50"
              groups="account.group_account_user"/>

</odoo>
