<?xml version="1.0" encoding="utf-8" ?>
<odoo>
    <record id="purchase_order_view_form_inherit" model="ir.ui.view">
        <field name="name">purchase.order.form</field>
        <field name="model">purchase.order</field>
        <field
            name="inherit_id"
            ref="purchase_stock.purchase_order_view_form_inherit"
        />
        <field name="arch" type="xml">
            <button name="action_create_invoice" position="after">
                <button
                    name="action_create_invoice_refund"
                    string="Create Refund"
                    type="object"
                    context="{'create_refund':True, 'default_move_type': 'in_refund'}"
                    invisible="state not in ('purchase', 'done') or invoice_status in ('no', 'invoiced')"
                />
            </button>
            <xpath
                expr="//div[hasclass('oe_button_box')]/button[@name='action_view_invoice']"
                position="after"
            >
                <button
                    type="object"
                    name="action_view_invoice_refund"
                    class="oe_stat_button"
                    icon="fa-pencil-square-o"
                    invisible="invoice_refund_count == 0 or state in ('draft','sent','to approve')"
                >
                    <field
                        name="invoice_refund_count"
                        widget="statinfo"
                        string="Refunds"
                    />
                    <field name='invoice_ids' invisible="1" />
                </button>
            </xpath>
            <xpath
                expr="//field[@name='order_line']/list/field[@name='qty_received']"
                position="after"
            >
                <field
                    name="qty_returned"
                    column_invisible="parent.state not in ('purchase', 'done')"
                />
            </xpath>
            <xpath
                expr="//field[@name='order_line']/list/field[@name='qty_invoiced']"
                position="after"
            >
                <field
                    name="qty_refunded"
                    column_invisible="parent.state not in ('purchase', 'done')"
                />
            </xpath>
            <field name="move_ids" position="attributes">
                <attribute
                    name="context"
                >{'list_view_ref':'stock.view_move_tree'}</attribute>
            </field>
        </field>
    </record>
</odoo>
