<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="hr_expense_form_view_inherit_sale_expense" model="ir.ui.view">
        <field name="name">hr.expense.form.inherit.sale.expense</field>
        <field name="model">hr.expense</field>
        <field name="inherit_id" ref="hr_expense.hr_expense_view_form"/>
        <field name="priority">30</field>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='analytic_distribution']" position="before">
                <field name="sale_order_id" groups="!sales_team.group_sale_salesman,!account.group_account_manager"
                    attrs="{'invisible': [('can_be_reinvoiced', '=', False)], 'readonly': [('sheet_is_editable', '=', False)]}"
                    options="{'no_create_edit': True, 'no_create': True, 'no_open': True}"
                    context="{'sale_show_partner_name': True, 'sale_expense_all_order': True}"
                    widget="sale_order_many2one"/>
                <field name="sale_order_id" groups="sales_team.group_sale_salesman,!account.group_account_manager"
                    attrs="{'invisible': [('can_be_reinvoiced', '=', False)], 'readonly': [('sheet_is_editable', '=', False)]}"
                    options="{'no_create_edit': True, 'no_create': True}"
                    context="{'sale_show_partner_name': True, 'sale_expense_all_order': True}"
                    widget="many2one"/>
                <field name="sale_order_id" groups="account.group_account_manager"
                    widget="many2one"
                    attrs="{'invisible':[['can_be_reinvoiced','=',False]],'readonly':['|', ['state','in',['done']], ['sheet_is_editable', '=', False]]}"
                    options="{'no_create_edit': True, 'no_create': True, 'no_open': True}"
                    context="{'sale_show_partner_name': True, 'sale_expense_all_order': True}"
                    />
                <field name="can_be_reinvoiced" invisible="1"/>
            </xpath>
        </field>
    </record>
    <record id="hr_expense_tree_view_inherit_sale_expense" model="ir.ui.view">
        <field name="name">hr.expense.tree.inherit.sale.expense</field>
        <field name="model">hr.expense</field>
        <field name="inherit_id" ref="hr_expense.view_expenses_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='reference']" position="after">
                <field name="sale_order_id" optional="hide" attrs="{'invisible': [('can_be_reinvoiced', '=', False)]}" options="{'no_create_edit': True, 'no_create': True, 'no_open': True}"  context="{'sale_show_partner_name': True, 'sale_expense_all_order': True}"/>
                <field name="can_be_reinvoiced" invisible="1" readonly="1"/>
            </xpath>
        </field>
    </record>

    <record id="hr_expense_split_view_inherit_sale_expense" model="ir.ui.view">
        <field name="name">hr.expense.split.view.inherit.sale.expense</field>
        <field name="model">hr.expense.split.wizard</field>
        <field name="inherit_id" ref="hr_expense.hr_expense_split"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='employee_id']" position="after">
                <field name="can_be_reinvoiced" invisible="1"/>
                <field name="sale_order_id" force_save="1" attrs="{'readonly': [('can_be_reinvoiced', '=', False)]}"/>
            </xpath>
        </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='state']" position="after">
                <field name="sale_order_id" attrs="{'invisible': [('can_be_reinvoiced', '=', False)]}" optional="show" options="{'no_create_edit': True, 'no_create': True, 'no_open': True}" context="{'sale_show_partner_name': True, 'sale_expense_all_order': True}"/>
                <field name="can_be_reinvoiced" invisible="1"/>
            </xpath>
        </field>
    </record>

    <record id="hr_expense_action_from_sale_order" model="ir.actions.act_window">
        <field name="name">Expenses</field>
        <field name="res_model">hr.expense</field>
        <field name="view_mode">tree,form</field>
        <field name="domain">[('sale_order_id', '=', active_id)]</field>
        <field name="context">{'default_sale_order_id': active_id}</field>
    </record>
</odoo>
