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

    <record model="ir.ui.view" id="product_pricelist_item_view_search">
        <field name="name">product.pricelist.item.search</field>
        <field name="model">product.pricelist.item</field>
        <field name="arch" type="xml">
            <search string="Products Price Rules Search">
                <filter name="Product Rule" domain="[('applied_on', '=', '1_product')]"/>
                <filter name="Variant Rule" domain="[('applied_on', '=', '0_product_variant')]" groups="product.group_product_variant"/>
                <separator/>
                <field name="pricelist_id" groups="product.group_product_pricelist"/>
                <field name="company_id" groups="base.group_multi_company"/>
                <field name="currency_id" groups="base.group_multi_currency"/>
                <filter string="Active" name="visible" domain="[('pricelist_id.active', '=', True)]"/>
                <group>
                    <filter
                        string="Product"
                        name="groupby_product"
                        context="{'group_by': 'product_tmpl_id'}"
                    />
                    <filter
                        string="Variant"
                        name="groupby_product_variant"
                        domain="[('applied_on', '=', '0_product_variant')]"
                        context="{'group_by': 'product_id'}"
                        groups="product.group_product_variant"
                    />
                    <filter
                        string="Pricelist"
                        name="groupby_vendor"
                        context="{'group_by': 'pricelist_id'}"
                        groups="product.group_product_pricelist"
                    />
                </group>
            </search>
        </field>
    </record>

    <record id="product_pricelist_item_tree_view" model="ir.ui.view">
        <field name="name">product.pricelist.item.list</field>
        <field name="model">product.pricelist.item</field>
        <field name="priority">10</field>
        <field name="arch" type="xml">
            <list string="Price Rules">
                <field name="pricelist_id" groups="product.group_product_pricelist" required="is_pricelist_required"/>
                <field name="name" string="Applied On"/>
                <field name="price"/>
                <field name="min_quantity" colspan="4"/>
                <field name="date_start" optional="hide"/>
                <field name="date_end" optional="hide"/>
                <field name="company_id" groups="base.group_multi_company" optional="show"/>
            </list>
        </field>
    </record>

    <record id="product_pricelist_item_tree_view_from_product" model="ir.ui.view">
        <!-- Access and edit price rules from a given product/product variant -->
        <field name="name">product.pricelist.item.list</field>
        <field name="model">product.pricelist.item</field>
        <field name="priority">100</field>
        <field name="arch" type="xml">
            <list string="Pricelist Rules" editable="bottom">
                <!-- Scope = coming from a product/product template -->
                <field
                    name="pricelist_id"
                    options="{'no_create_edit': 1, 'no_open': 1}"
                    required="is_pricelist_required"
                    groups="product.group_product_pricelist"
                />
                <field
                    name="product_id"
                    groups="product.group_product_variant"
                    readonly="context.get('active_model') == 'product.product'"
                    column_invisible="context.get('product_without_variants', False)"
                    required="applied_on == '0_product_variant'"
                    domain="['|', '|',
                        ('id', '=', context.get('default_product_id', 0)),
                        ('product_tmpl_id', '=', context.get('default_product_tmpl_id', 0)),
                        ('categ_id', '=', context.get('default_categ_id', 0)), '|', ('company_id', '=', company_id), ('company_id', '=', False)
                    ]"
                    placeholder="All variants"
                    options="{'no_create_edit': 1, 'no_open': 1}"
                />
                <field name="company_id" column_invisible="True"/>
                <field name="categ_id" column_invisible="True"/>
                <field
                    name="product_tmpl_id"
                    column_invisible="context.get('active_model') != 'product.category'"
                    required="applied_on == '1_product'"
                    domain="[('categ_id', '=', context.get('default_categ_id', True)), '|', ('company_id', '=', company_id), ('company_id', '=', False)]"
                    options="{'no_create_edit': 1, 'no_open': 1}"
                />
                <field
                    name="fixed_price"
                    widget="monetary"
                    string="Price"
                    options="{'currency_field': 'currency_id', 'field_digits': True}"
                    required="1"
                />
                <field name="min_quantity" colspan="4"/>
                <field name="currency_id" column_invisible="True"/>
                <field name="date_start" optional="show"/>
                <field name="date_end" optional="show"/>
                <field name="applied_on" column_invisible="True"/>
                <field name="company_id" groups="base.group_multi_company" optional="hide" options="{'no_create': 1, 'no_open': 1}"/>
            </list>
        </field>
    </record>

    <record id="product_pricelist_item_form_view" model="ir.ui.view">
        <field name="name">product.pricelist.item.form</field>
        <field name="model">product.pricelist.item</field>
        <field name="arch" type="xml">
            <form string="Pricelist Rule">
                <sheet>
                    <field name="name" invisible="1"/>
                    <field name="company_id" invisible="1"/>
                    <field name="price" invisible="1"/>
                    <group name="pricelist_rule_computation">
                        <group name="pricelist_rule_method">
                            <field name="applied_on" invisible="1"/>
                            <field name="display_applied_on"
                                string="Apply To"
                                widget="radio"
                                options="{'horizontal': true}"/>
                            <field name="categ_id"
                                options="{'no_create': 1}"
                                invisible="display_applied_on != '2_product_category'"
                                placeholder="All categories"/>
                            <field name="product_tmpl_id"
                                options="{'no_create': 1}"
                                invisible="display_applied_on != '1_product'"
                                placeholder="All products"/>
                            <field name="product_variant_count" invisible="1"/>
                            <field name="product_id"
                                options="{'no_create': 1}"
                                invisible="product_variant_count &lt; 2"
                                placeholder="All variants"/>
                            <field name="compute_price"
                                string="Price Type"
                                widget="radio"
                                options="{'horizontal': true}"
                                invisible="display_applied_on == '1_product' and compute_price == 'fixed_price'"
                                groups="product.group_product_pricelist"
                            />
                            <label for="fixed_price" invisible="compute_price != 'fixed'"/>
                            <div class="o_row" invisible="compute_price != 'fixed'" style="width: 60% !important;">
                                <field name="fixed_price"
                                    widget="monetary"
                                    options="{'currency_field': 'currency_id', 'field_digits': True}"/>
                                <span class="d-flex gap-2 p-0" invisible="not product_uom_name">per<field
                                        name="product_uom_name"/></span>
                            </div>
                            <label for="percent_price" string="Discount" invisible="compute_price != 'percentage'"/>
                            <div class="o_row gap-3" invisible="compute_price != 'percentage'">
                                <field name="percent_price"/>
                                <span>%</span>
                                <t groups="product.group_product_pricelist">
                                    <span>on</span>
                                    <field name="base_pricelist_id" placeholder="sales price" domain="[('id', '!=', pricelist_id)]"/>
                                </t>
                            </div>
                        </group>
                        <group name="pricelist_rule_limits">
                            <field name="min_quantity" string="Min Qty"/>
                            <field name="date_start"
                                string="Validity Period"
                                widget="daterange"
                                options="{'end_date_field': 'date_end'}"/>
                            <field name="date_end" invisible="1"/>
                        </group>
                    </group>
                    <group name="pricelist_rule_base" invisible="compute_price != 'formula'">
                        <group>
                            <field name="base" string="Based price"/>
                            <field name="base_pricelist_id"
                                invisible="base != 'pricelist'"
                                readonly="base != 'pricelist'"
                                required="compute_price == 'formula' and base == 'pricelist'"
                                string="Other Pricelist"/>
                            <label for="price_discount" string="Discount"
                                invisible="base == 'standard_price'"/>
                            <div class="o_row" invisible="base == 'standard_price'" style="width: 40% !important;">
                                <field name="price_discount"/>
                                <span>%</span>
                            </div>
                            <label for="price_markup" string="Markup"
                                invisible="base != 'standard_price'"/>
                            <div class="o_row" invisible="base != 'standard_price'" style="width: 40% !important;">
                                <field name="price_markup"/>
                                <span>%</span>
                            </div>
                            <field name="price_round" string="Round off to"/>
                            <field name="price_surcharge"
                                widget="monetary"
                                options="{'currency_field': 'currency_id', 'field_digits': True}"/>
                            <label string="Margins" for="price_min_margin"
                                groups="base.group_no_one"/>
                            <div class="d-flex align-items-baseline" groups="base.group_no_one">
                                <field name="price_min_margin"
                                    string="Min. Margin"
                                    class="oe_inline"
                                    widget="monetary"
                                    options="{'field_digits': True}"/>
                                <i class="fa fa-long-arrow-right mx-2 oe_edit_only"
                                    aria-label="Arrow icon" title="Arrow"/>
                                <field name="price_max_margin"
                                    string="Max. Margin"
                                    class="oe_inline"
                                    widget="monetary"
                                    nolabel="1"
                                    options="{'field_digits': True}"/>
                            </div>
                        </group>
                        <div>
                            <div class="alert alert-info" role="alert" style="white-space: pre;">
                                <field name="rule_tip"/>
                            </div>
                            <div class="alert alert-info"
                                role="alert"
                                style="white-space: pre;">
                                <b>Tip: want to round at 9.99?</b>
                                <div>round off to 10.00 and set an extra at -0.01</div>
                            </div>
                        </div>
                    </group>

                    <group string="Company Settings" groups="base.group_no_one">
                        <group name="pricelist_rule_related">
                            <field name="pricelist_id" invisible="1"/>
                            <field name="currency_id" groups="base.group_multi_currency"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <record id="product_pricelist_item_product_template_form_view" model="ir.ui.view">
        <field name="name">product.pricelist.item.product.template.form.inherit</field>
        <field name="model">product.pricelist.item</field>
        <field name="inherit_id" ref="product.product_pricelist_item_form_view"/>
        <field name="mode">primary</field>
        <field name="arch" type="xml">
            <field name="display_applied_on" position="attributes">
                <attribute name="invisible">1</attribute>
            </field>
            <field name="product_tmpl_id" position="attributes">
                <attribute name="readonly">1</attribute>
                <attribute name="force_save">1</attribute>
            </field>
            <field name="date_start" position="after">
                <field name="pricelist_id" groups="product.group_product_pricelist"/>
            </field>
        </field>
    </record>

    <record id="product_pricelist_item_product_product_form_view" model="ir.ui.view">
        <field name="name">product.pricelist.item.product.product.form.inherit</field>
        <field name="model">product.pricelist.item</field>
        <field name="inherit_id" ref="product.product_pricelist_item_product_template_form_view"/>
        <field name="mode">primary</field>
        <field name="arch" type="xml">
            <field name="product_id" position="attributes">
                <attribute name="readonly">1</attribute>
                <attribute name="force_save">1</attribute>
            </field>
        </field>
    </record>

</odoo>
