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

    <!-- Parameters -->
    <record id="rule_parameter_ee_afp" model="hr.rule.parameter">
        <field name="name">EE: AFP</field>
        <field name="code">ee_afp</field>
        <field name="country_id" ref="base.pe"/>
    </record>
    <record id="rule_parameter_ee_afp_2020" model="hr.rule.parameter.value">
        <field name="parameter_value">{
            # non-mixta, mixta_monthly, mixta_annually, iss, ley, maximum (iss wage base)
            'habitat':   (1.47, 0.38, 1.25, 1.35, 10.0, 9707.03),
            'integra':   (1.55, 0.00, 1.82, 1.35, 10.0, 9707.03),
            'prima':     (1.60, 0.18, 1.25, 1.35, 10.0, 9707.03),
            'profuturo': (1.69, 0.67, 1.20, 1.35, 10.0, 9707.03),
        }</field>
        <field name="rule_parameter_id" ref="rule_parameter_ee_afp"/>
        <field name="date_from" eval="datetime(2020, 1, 1).date()"/>
    </record>

    <!-- EE Rules -->
    <record id="hr_payroll_rule_ee_afp_pensiones" model="hr.salary.rule">
        <field name="sequence" eval="190"/>
        <field name="struct_id" ref="hr_payroll_structure"/>
        <field name="category_id" ref="hr_payroll_category_ee_pe_afp"/>
        <field name="name">EE: PE AFP Pensions</field>
        <field name="code">EE_PE_AFP_PENSIONES</field>
        <field name="condition_select">python</field>
        <field name="condition_python">result = categories.GROSS and contract.pe_payroll_config_value('retirement_type') == 'afp'</field>
        <field name="amount_select">code</field>
        <field name="amount_python_compute">result, result_rate = categories.GROSS, -payslip.rule_parameter('ee_afp')[contract.pe_payroll_config_value('afp_type')][4]</field>
        <field name="appears_on_payslip" eval="True"/>
        <field name="partner_id" ref="partner_afp"/>
    </record>

    <record id="hr_payroll_rule_ee_afp_seguro" model="hr.salary.rule">
        <field name="sequence" eval="191"/>
        <field name="struct_id" ref="hr_payroll_structure"/>
        <field name="category_id" ref="hr_payroll_category_ee_pe_afp"/>
        <field name="name">EE: PE AFP Insurance</field>
        <field name="code">EE_PE_AFP_SEGURO</field>
        <field name="condition_select">python</field>
        <field name="condition_python">result = categories.GROSS and contract.pe_payroll_config_value('retirement_type') == 'afp'</field>
        <field name="amount_select">code</field>
        <field name="amount_python_compute">
wage = categories.GROSS
# wage_max is monthly
wage_max = payslip.rule_parameter('ee_afp')[contract.pe_payroll_config_value('afp_type')][5]
# normalize wage_max to pay period
pay_periods_in_year = payslip.pay_periods_in_year
wage_max = (wage_max * 12.0) / pay_periods_in_year
eligible_wage = min(wage, wage_max)
rate = -payslip.rule_parameter('ee_afp')[contract.pe_payroll_config_value('afp_type')][3]
result, result_rate = eligible_wage, rate
</field>
        <field name="appears_on_payslip" eval="True"/>
        <field name="partner_id" ref="partner_afp"/>
    </record>

    <record id="hr_payroll_rule_ee_afp_comision_mixta" model="hr.salary.rule">
        <field name="sequence" eval="192"/>
        <field name="struct_id" ref="hr_payroll_structure"/>
        <field name="category_id" ref="hr_payroll_category_ee_pe_afp"/>
        <field name="name">EE: PE AFP Mixed Commission</field>
        <field name="code">EE_PE_AFP_COMISION_MIXTA</field>
        <field name="condition_select">python</field>
        <field name="condition_python">result = categories.GROSS and contract.pe_payroll_config_value('retirement_type') == 'afp' and contract.pe_payroll_config_value('afp_comision_type') == 'mixta'</field>
        <field name="amount_select">code</field>
        <field name="amount_python_compute">result, result_rate = categories.GROSS, -payslip.rule_parameter('ee_afp')[contract.pe_payroll_config_value('afp_type')][1]</field>
        <field name="appears_on_payslip" eval="True"/>
        <field name="partner_id" ref="partner_afp"/>
    </record>

    <record id="hr_payroll_rule_ee_afp_comision_non_mixta" model="hr.salary.rule">
        <field name="sequence" eval="192"/>
        <field name="struct_id" ref="hr_payroll_structure"/>
        <field name="category_id" ref="hr_payroll_category_ee_pe_afp"/>
        <field name="name">EE: PE AFP Comission (Non-Mixed)</field>
        <field name="code">EE_PE_AFP_COMISION_NON_MIXTA</field>
        <field name="condition_select">python</field>
        <field name="condition_python">result = categories.GROSS and contract.pe_payroll_config_value('retirement_type') == 'afp' and contract.pe_payroll_config_value('afp_comision_type') == 'non_mixta'</field>
        <field name="amount_select">code</field>
        <field name="amount_python_compute">result, result_rate = categories.GROSS, -payslip.rule_parameter('ee_afp')[contract.pe_payroll_config_value('afp_type')][0]</field>
        <field name="appears_on_payslip" eval="True"/>
        <field name="partner_id" ref="partner_afp"/>
    </record>

</odoo>
