<odoo>
    <record id="hr_expense_view_form" model="ir.ui.view">
        <field name="name">hr.expense.form</field>
        <field name="model">hr.expense</field>
        <field name="inherit_id" ref="hr_expense.hr_expense_view_form" />
        <field name="arch" type="xml">
            <field name="unit_amount" position="attributes">
                <attribute name="force_save">1</attribute>
            </field>
            <label for="quantity" position="attributes">
                <attribute
                    name="attrs"
                >{'invisible': [('invoice_id', '!=', False)]}</attribute>
            </label>
            <xpath
                expr='//field[@name="quantity"]/parent::div/parent::div'
                position="attributes"
            >
                <attribute
                    name="attrs"
                >{'invisible': [('invoice_id', '!=', False)]}</attribute>
            </xpath>
            <label for="tax_ids" position="attributes">
                <attribute
                    name="attrs"
                >{'invisible': [('invoice_id', '!=', False)]}</attribute>
            </label>
            <xpath
                expr='//field[@name="tax_ids"]/parent::div/parent::div'
                position="attributes"
            >
                <attribute
                    name="attrs"
                >{'invisible': [('invoice_id', '!=', False)]}</attribute>
            </xpath>
            <!-- Force saved to be sure they are stored when selecting the vendor bill in any state -->
            <field name="quantity" position="attributes">
                <attribute name="force_save">1</attribute>
            </field>
            <field name="reference" position="attributes">
                <attribute name="force_save">1</attribute>
            </field>
            <field name="date" position="attributes">
                <attribute name="force_save">1</attribute>
            </field>
            <field name="total_amount" position="attributes">
                <attribute name="force_save">1</attribute>
                <attribute
                    name="attrs"
                >{'readonly': [('invoice_id', '!=', False)]}</attribute>
            </field>
            <xpath
                expr="//field[@name='total_amount']/../field[@name='currency_id']"
                position="attributes"
            >
                <attribute
                    name="attrs"
                >{'readonly': [('invoice_id', '!=', False)]}</attribute>
            </xpath>
            <field name="reference" position="attributes">
                <attribute name="force_save">1</attribute>
                <attribute
                    name="attrs"
                >{'readonly': ['|', ('is_ref_editable', '=', False), ('invoice_id', '!=', False)], 'invisible': [('product_has_cost', '=', True)]}</attribute>
            </field>
            <field name="date" position="attributes">
                <attribute name="force_save">1</attribute>
                <attribute
                    name="attrs"
                >{'readonly': ['|', ('sheet_is_editable', '=', False), ('invoice_id', '!=', False)]}</attribute>
            </field>
            <field name="account_id" position="attributes">
                <attribute
                    name="attrs"
                >{'readonly': ['|', ('is_editable', '=', False), ('sheet_is_editable', '=', False)], 'invisible': [('invoice_id', '!=', False)]}</attribute>
            </field>
            <xpath
                expr="//field[@name='analytic_distribution']/../field[@name='company_id']"
                position="attributes"
            >
                <attribute
                    name="attrs"
                >{'readonly': [('invoice_id', '!=', False)]}</attribute>
            </xpath>
            <xpath expr='//field[@name="product_id"]/parent::div' position="after">
                <!-- Added double, one as invisible and w/o group, for those without the permission -->
                <field name="invoice_id" invisible="1" />
                <field
                    name="invoice_id"
                    groups="account.group_account_readonly"
                    context="{'default_type': 'in_invoice',
                            'type': 'in_invoice',
                            'journal_type': 'purchase',
                            'default_ref': reference,
                            'default_invoice_date': date,
                            'default_line_ids': [{'product_id': product_id,
                                                          'name': name,
                                                          'price_unit': unit_amount,
                                                          'quantity': quantity,
                                                          'date': date,
                                                          'account_id': account_id,
                                                          'tax_ids': tax_ids, }], }"
                />
            </xpath>
            <field name="tax_ids" position="attributes">
                <attribute
                    name="attrs"
                >{'invisible': [('invoice_id', '!=', False)]}</attribute>
            </field>
        </field>
    </record>
    <record id="hr_expense_sheet_form_view_inherit_sale_expense" model="ir.ui.view">
        <field name="name">hr.expense.sheet.form.inherit.sale.expense</field>
        <field name="model">hr.expense.sheet</field>
        <field name="inherit_id" ref="hr_expense.view_hr_expense_sheet_form" />
        <field name="arch" type="xml">
            <xpath
                expr="//field[@name='expense_line_ids']/tree/field[@name='name']"
                position="after"
            >
                <field name="invoice_id" groups="account.group_account_invoice" />
                <field name="quantity" invisible="1" />
            </xpath>
            <xpath
                expr="//field[@name='expense_line_ids']/tree/field[@name='total_amount']"
                position="after"
            >
                <field name="sheet_id_state" invisible="1" />
                <button
                    name="action_expense_create_invoice"
                    groups="account.group_account_invoice"
                    string="Create Vendor Bill"
                    type="object"
                    attrs="{'invisible': ['|', ('invoice_id', '!=', False),('sheet_id_state', 'not in', ('draft','approve'))]}"
                />
            </xpath>
            <div class="oe_button_box" position="inside">
                <button
                    class="oe_stat_button"
                    icon="fa-book"
                    name="action_view_invoices"
                    groups="account.group_account_invoice"
                    type="object"
                    attrs="{'invisible': [('invoice_count', '=', 0)]}"
                >
                    <field
                        name="invoice_count"
                        groups="account.group_account_invoice"
                        widget="statinfo"
                        string="Invoices"
                    />
                </button>
            </div>
        </field>
    </record>
</odoo>
