<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright 2019 Akretion France (http://www.akretion.com)
  @author Alexis de Lattre <alexis.delattre@akretion.com>
  License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
-->

<odoo>

<menuitem id="commission_root" name="Commissions" parent="account.menu_finance" sequence="11"/>
<menuitem id="commission_config_root" name="Commissions" parent="account.menu_finance_configuration" sequence="110"/>

<!-- PROFILE -->
<record id="commission_profile_form" model="ir.ui.view">
    <field name="name">commission.profile.form</field>
    <field name="model">commission.profile</field>
    <field name="arch" type="xml">
        <form>
            <sheet>
                <widget name="web_ribbon" title="Archived" bg_color="bg-danger" attrs="{'invisible': [('active', '=', True)]}"/>
                <group name="main">
                    <field name="name"/>
                    <field name="active" invisible="1"/>
                    <field name="company_id" invisible="1"/>
                    <field name="company_id" groups="base.group_multi_company"/>
                    <field name="trigger_type"/>
                </group>
                <group name="lines" string="Rules">
                    <field name="line_ids" nolabel="1" colspan="2"/>
                </group>
            </sheet>
        </form>
    </field>
</record>

<record id="commission_profile_tree" model="ir.ui.view">
    <field name="name">commission.profile.tree</field>
    <field name="model">commission.profile</field>
    <field name="arch" type="xml">
        <tree>
            <field name="sequence" widget="handle"/>
            <field name="name" decoration-bf="1"/>
            <field name="trigger_type" optional="show"/>
            <field name="company_id" groups="base.group_multi_company"/>
        </tree>
    </field>
</record>

<record id="commission_profile_action" model="ir.actions.act_window">
    <field name="name">Commission Profiles</field>
    <field name="res_model">commission.profile</field>
    <field name="view_mode">tree,form</field>
</record>

<menuitem id="commission_profile_menu" action="commission_profile_action" parent="commission_config_root" sequence="18"/>


<!-- RULE -->
<record id="commission_rule_form" model="ir.ui.view">
    <field name="name">commission.rule.form</field>
    <field name="model">commission.rule</field>
    <field name="arch" type="xml">
        <form>
            <sheet>
                <group name="main">
                    <field name="profile_id" invisible="not context.get('commission_rule_main_view')"/>
                    <field name="company_id" groups="base.group_multi_company"/>
                    <field name="company_id" invisible="1"/>
                    <field name="applied_on" widget="radio"/>
                </group>
                <group name="match" string="Match">
                    <field name="partner_ids" attrs="{'invisible': [('applied_on', 'not in', ('0_customer_product', '1_customer_product_category'))], 'required': [('applied_on', 'in', ('0_customer_product', '1_customer_product_category'))]}"/>
                    <field name="product_categ_ids" attrs="{'invisible': [('applied_on', 'not in', ('1_customer_product_category', '3_product_category'))], 'required': [('applied_on', 'in', ('1_customer_product_category', '3_product_category'))]}"/>
                    <field name="product_ids" attrs="{'invisible': [('applied_on', 'not in', ('0_customer_product', '2_product'))], 'required': [('applied_on', 'in', ('0_customer_product', '2_product'))]}"/>
                    <field name="date_start"/>
                    <field name="date_end"/>
                </group>
                <group name="compute" string="Compute">
                    <label for="rate"/>
                    <div name="rate">
                        <field name="rate" class="oe_inline"/> %
                    </div>
                </group>
            </sheet>
        </form>
    </field>
</record>

<record id="commission_rule_tree" model="ir.ui.view">
    <field name="name">commission.rule.tree</field>
    <field name="model">commission.rule</field>
    <field name="arch" type="xml">
        <tree>
            <field name="profile_id" invisible="not context.get('commission_rule_main_view')"/>
            <field name="applied_on"/>
            <field name="date_start"/>
            <field name="date_end"/>
            <field name="rate" string="Rate (%)"/>
        </tree>
    </field>
</record>

<record id="commission_rule_search" model="ir.ui.view">
    <field name="name">commission.rule.search</field>
    <field name="model">commission.rule</field>
    <field name="arch" type="xml">
        <search>
            <filter string="Archived" name="inactive" domain="[('active', '=', False)]"/>
            <group name="groupby">
                <filter name="profile_groupby" string="Profile" context="{'group_by': 'profile_id'}"/>
            </group>
        </search>
    </field>
</record>


<record id="commission_rule_action" model="ir.actions.act_window">
    <field name="name">Commission Rules</field>
    <field name="res_model">commission.rule</field>
    <field name="view_mode">tree,form</field>
    <field name="context">{'commission_rule_main_view': True}</field>
</record>


<menuitem id="commission_rule_menu" action="commission_rule_action" parent="commission_config_root" sequence="20"/>

<!-- RESULT -->
<record id="commission_result_form" model="ir.ui.view">
    <field name="name">commission.result.form</field>
    <field name="model">commission.result</field>
    <field name="arch" type="xml">
        <form>
            <group name="main">
                <group name="main-left">
                    <field name="partner_id"/>
                    <field name="profile_id" groups="account.group_account_manager"/>
                    <field name="company_currency_id" invisible="1"/>
                    <field name="company_id" invisible="1"/>
                    <field name="company_id" groups="base.group_multi_company"/>
                    <field name="amount_total"/>
                </group>
                <group name="main-right">
                    <field name="date_range_id"/>
                    <field name="date_start"/>
                    <field name="date_end"/>
                </group>
            </group>
            <group name="lines" string="Commission Lines">
                <field nolabel="1" name="line_ids" colspan="2">
                    <tree>
                        <field name="move_id"/>
                        <field name="move_line_id"/>
                        <field name="base"/>
                        <field name="rate" string="Rate (%)"/>
                        <field name="amount" sum="1"/>
                        <field name="rule_id"/>
                        <field name="company_currency_id" invisible="1"/>
                    </tree>
                </field>
            </group>
        </form>
    </field>
</record>

<record id="commission_result_tree" model="ir.ui.view">
    <field name="name">commission.result.tree</field>
    <field name="model">commission.result</field>
    <field name="arch" type="xml">
        <tree>
            <field name="date_range_id" optional="show"/>
            <field name="date_start" optional="hide"/>
            <field name="date_end" optional="hide"/>
            <field name="partner_id"/>
            <field name="profile_id" groups="account.group_account_manager"/>
            <field name="company_currency_id" invisible="1"/>
            <field name="company_id" groups="base.group_multi_company"/>
            <field name="amount_total" sum="1" optional="show"/>
        </tree>
    </field>
</record>

<record id="commission_result_search" model="ir.ui.view">
    <field name="name">commission.result.search</field>
    <field name="model">commission.result</field>
    <field name="arch" type="xml">
        <search>
            <field name="partner_id"/>
            <field name="date_range_id"/>
            <group name="groupby">
                <filter name="partner_groupby" string="Salesman" context="{'group_by': 'partner_id'}"/>
                <filter name="date_range_groupby" string="Period" context="{'group_by': 'date_range_id'}"/>
            </group>
        </search>
    </field>
</record>

<record id="commission_result_action" model="ir.actions.act_window">
    <field name="name">Commissions</field>
    <field name="res_model">commission.result</field>
    <field name="view_mode">tree,form</field>
</record>

<menuitem id="commission_result_menu" action="commission_result_action" parent="commission_root" sequence="10"/>


</odoo>
