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

    <!-- ═══════════════════════════════════════════════════════════
         Stock Picking Form: SO/Invoice tracking + Serial Scanner
         ═══════════════════════════════════════════════════════════ -->

    <record id="view_picking_form_inherit_fi" model="ir.ui.view">
        <field name="name">stock.picking.form.fusion.inventory</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="stock.view_picking_form"/>
        <field name="arch" type="xml">

            <!-- ── Smart buttons: Sale Order ── -->
            <xpath expr="//div[@name='button_box']" position="inside">
                <button name="action_view_sale_order" type="object"
                        class="oe_stat_button" icon="fa-shopping-cart"
                        invisible="not x_fi_sale_order_id">
                    <field name="x_fi_sale_order_id" widget="statinfo"
                           string="Sale Order"/>
                </button>
                <button name="action_view_invoices" type="object"
                        class="oe_stat_button" icon="fa-file-text-o"
                        invisible="x_fi_invoice_count == 0">
                    <field name="x_fi_invoice_count" widget="statinfo"
                           string="Invoices"/>
                </button>
                <button name="action_view_purchase_order" type="object"
                        class="oe_stat_button" icon="fa-truck"
                        invisible="not x_fi_purchase_order_id">
                    <field name="x_fi_purchase_order_id" widget="statinfo"
                           string="Purchase Order"/>
                </button>
                <button name="action_view_bills" type="object"
                        class="oe_stat_button" icon="fa-credit-card"
                        invisible="x_fi_bill_count == 0">
                    <field name="x_fi_bill_count" widget="statinfo"
                           string="Bills"/>
                </button>
            </xpath>

            <!-- ── Status pills below header: Sale Order ── -->
            <xpath expr="//header" position="after">
                <div class="d-flex gap-2 px-3 pb-2"
                     invisible="not x_fi_sale_order_id">
                    <span class="text-muted">SO Status:</span>
                    <field name="x_fi_sale_order_state" widget="badge"
                           decoration-info="x_fi_sale_order_state == 'draft'"
                           decoration-primary="x_fi_sale_order_state == 'sale'"
                           decoration-success="x_fi_sale_order_state == 'done'"
                           decoration-danger="x_fi_sale_order_state == 'cancel'"/>
                    <span class="text-muted ms-3">Invoice:</span>
                    <field name="x_fi_invoice_status" widget="badge"
                           decoration-danger="x_fi_invoice_status == 'no'"
                           decoration-warning="x_fi_invoice_status == 'invoiced'"
                           decoration-success="x_fi_invoice_status == 'paid'"/>
                </div>
                <div class="d-flex gap-2 px-3 pb-2"
                     invisible="not x_fi_purchase_order_id">
                    <span class="text-muted">PO Status:</span>
                    <field name="x_fi_purchase_order_state" widget="badge"
                           decoration-info="x_fi_purchase_order_state == 'draft'"
                           decoration-primary="x_fi_purchase_order_state == 'purchase'"
                           decoration-success="x_fi_purchase_order_state == 'done'"
                           decoration-danger="x_fi_purchase_order_state == 'cancel'"/>
                    <span class="text-muted ms-3">Bill:</span>
                    <field name="x_fi_bill_status" widget="badge"
                           decoration-danger="x_fi_bill_status == 'no'"
                           decoration-warning="x_fi_bill_status == 'billed'"
                           decoration-success="x_fi_bill_status == 'paid'"/>
                </div>
            </xpath>

            <!-- ── Serial Number Scan button ── -->
            <xpath expr="//header" position="inside">
                <button name="action_scan_serial_numbers" type="object"
                        string="Scan Serial Numbers"
                        class="btn-secondary"
                        invisible="picking_type_code != 'outgoing'"/>
            </xpath>

        </field>
    </record>

    <!-- ═══════════════════════════════════════════════════════════
         Stock Picking List: Invoice status column + filters
         ═══════════════════════════════════════════════════════════ -->

    <record id="view_picking_list_inherit_fi" model="ir.ui.view">
        <field name="name">stock.picking.list.fusion.inventory</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="stock.vpicktree"/>
        <field name="arch" type="xml">
            <xpath expr="//list" position="inside">
                <field name="x_fi_invoice_status" string="Invoice Status"
                       widget="badge" optional="show"
                       decoration-danger="x_fi_invoice_status == 'no'"
                       decoration-warning="x_fi_invoice_status == 'invoiced'"
                       decoration-success="x_fi_invoice_status == 'paid'"/>
                <field name="x_fi_bill_status" string="Bill Status"
                       widget="badge" optional="show"
                       decoration-danger="x_fi_bill_status == 'no'"
                       decoration-warning="x_fi_bill_status == 'billed'"
                       decoration-success="x_fi_bill_status == 'paid'"/>
            </xpath>
        </field>
    </record>

    <!-- ═══════════════════════════════════════════════════════════
         Stock Picking Search: Filters for invoice status
         ═══════════════════════════════════════════════════════════ -->

    <record id="view_picking_search_inherit_fi" model="ir.ui.view">
        <field name="name">stock.picking.search.fusion.inventory</field>
        <field name="model">stock.picking</field>
        <field name="inherit_id" ref="stock.view_picking_internal_search"/>
        <field name="arch" type="xml">
            <xpath expr="//search" position="inside">
                <separator/>
                <filter name="fi_paid_not_delivered" string="Paid - Ready to Deliver"
                        domain="[('x_fi_invoice_status', '=', 'paid'), ('state', 'not in', ('done', 'cancel'))]"/>
                <filter name="fi_invoiced_not_delivered" string="Invoiced - Not Delivered"
                        domain="[('x_fi_invoice_status', '=', 'invoiced'), ('state', 'not in', ('done', 'cancel'))]"/>
                <filter name="fi_not_invoiced" string="Not Invoiced"
                        domain="[('x_fi_invoice_status', '=', 'no'), ('picking_type_code', '=', 'outgoing')]"/>
                <separator/>
                <filter name="fi_bill_paid" string="Bill Paid"
                        domain="[('x_fi_bill_status', '=', 'paid'), ('picking_type_code', '=', 'incoming')]"/>
                <filter name="fi_billed_not_paid" string="Billed - Not Paid"
                        domain="[('x_fi_bill_status', '=', 'billed'), ('picking_type_code', '=', 'incoming')]"/>
                <filter name="fi_not_billed" string="Not Billed"
                        domain="[('x_fi_bill_status', '=', 'no'), ('picking_type_code', '=', 'incoming')]"/>
            </xpath>
        </field>
    </record>

</odoo>
