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

    <!-- Sale Order form: add "Create PO" button and purchase count stat -->
    <record id="fusion_sale_order_form_inherit" model="ir.ui.view">
        <field name="name">fusion.sale.order.form.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//form//header//button[@name='action_quotation_send']" position="after">
                <button name="%(fusion_purchase_order_wizard_action)d" class="btn-primary" type="action" string="Create PO"/>
            </xpath>
            <xpath expr="//button[@name='action_view_invoice']" position="before">
                <button type="object" name="action_view_fusion_purchases" class="oe_stat_button" invisible="fusion_purchase_count == 0" icon="fa-file">
                    <field name="fusion_purchase_count" widget="statinfo" string="Purchase"/>
                </button>
            </xpath>
        </field>
    </record>

    <!-- Purchase Order form: add linked SOs, marked for, match button, stat -->
    <record id="fusion_purchase_order_form_inherit" model="ir.ui.view">
        <field name="name">fusion.purchase.order.form.inherit</field>
        <field name="model">purchase.order</field>
        <field name="inherit_id" ref="purchase.purchase_order_form"/>
        <field name="arch" type="xml">
            <!-- Match SO button in header -->
            <xpath expr="//header" position="inside">
                <button name="action_fusion_match_sale_order" type="object" string="Match Sale Order"
                    class="btn-secondary"
                    confirm="This will try to match this PO to a Sale Order based on the 'Marked For' field. Continue?"/>
            </xpath>
            <!-- Stat button for linked Sale Orders -->
            <xpath expr="//div[@name='button_box']" position="inside">
                <button type="object" name="action_view_fusion_sale_order" class="oe_stat_button"
                    invisible="fusion_sale_count == 0" icon="fa-shopping-cart">
                    <field name="fusion_sale_count" widget="statinfo" string="Sale Orders"/>
                </button>
            </xpath>
            <!-- Marked For (contacts) below Vendor Reference -->
            <xpath expr="//field[@name='partner_ref']" position="after">
                <field name="fusion_marked_for_ids" widget="many2many_tags" string="Marked For"
                    placeholder="Select customers..." options="{'no_create': False, 'no_quick_create': False}"/>
            </xpath>
            <!-- Sale Orders (many2many) before order date -->
            <xpath expr="//field[@name='date_order']" position="before">
                <field name="fusion_sale_ids" widget="many2many_tags" string="Sale Orders"
                    placeholder="Link sale orders..." options="{'no_create': True}"/>
            </xpath>
        </field>
    </record>

    <!-- Purchase Order list: add Marked For column next to Vendor -->
    <record id="fusion_purchase_order_list_inherit" model="ir.ui.view">
        <field name="name">fusion.purchase.order.list.inherit</field>
        <field name="model">purchase.order</field>
        <field name="inherit_id" ref="purchase.purchase_order_view_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='partner_id']" position="after">
                <field name="fusion_marked_for_ids" widget="many2many_tags" string="Marked For" optional="show"/>
            </xpath>
        </field>
    </record>

    <!-- RFQ list: add Marked For column next to Vendor -->
    <record id="fusion_rfq_list_inherit" model="ir.ui.view">
        <field name="name">fusion.rfq.list.inherit</field>
        <field name="model">purchase.order</field>
        <field name="inherit_id" ref="purchase.purchase_order_kpis_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='partner_id']" position="after">
                <field name="fusion_marked_for_ids" widget="many2many_tags" string="Marked For" optional="show"/>
            </xpath>
        </field>
    </record>

    </data>
</odoo>
