# -*- coding: utf-8 -*-
from odoo import api, fields, models


class SalaryStructure(models.Model):
    _name = 'c2c.salary.structure'
    _description = 'Salary Structure'
    _rec_name = 'name'

    name = fields.Char(string='Name', required=True)
    basic_percentage = fields.Float(
        string='Basic (%)', required=True, default=50.0,
        help='Percentage of gross salary allocated as Basic.',
    )
    hra_percentage = fields.Float(
        string='HRA (%)', required=True, default=20.0,
        help='Percentage of gross salary allocated as HRA.',
    )
    allowance_percentage = fields.Float(
        string='Allowances (%)', required=True, default=30.0,
        help='Percentage of gross salary allocated as Allowances.',
    )
    pf_percentage = fields.Float(
        string='PF (%)', required=True, default=12.0,
        help='Provident Fund deduction percentage on Basic salary.',
    )
    esi_percentage = fields.Float(
        string='ESI (%)', required=True, default=1.75,
        help='ESI deduction percentage on Gross salary.',
    )
    professional_tax_fixed = fields.Float(
        string='Professional Tax (Fixed)', default=200.0,
        help='Fixed professional tax amount deducted per month.',
    )
    company_id = fields.Many2one(
        'res.company', string='Company', required=True,
        default=lambda self: self.env.company,
    )
    active = fields.Boolean(default=True)

    @api.constrains('basic_percentage', 'hra_percentage', 'allowance_percentage')
    def _check_percentages(self):
        for rec in self:
            total = rec.basic_percentage + rec.hra_percentage + rec.allowance_percentage
            if abs(total - 100.0) > 0.01:
                raise models.ValidationError(
                    'Basic + HRA + Allowances percentages must equal 100%%. '
                    'Current total: %.2f%%' % total
                )
