# -*- coding:  utf-8 -*-
##############################################################################
#
#    OpenERP,  Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http: //tiny.be > ).
#
#    This program is free software:  you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation,  either version 3 of the
#    License,  or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not,  see <http: //www.gnu.org/licenses/ > .
#
##############################################################################
# Generated by the OpenERP plugin for Dia !
# from osv import fields
from openerp.osv import fields, osv
# import datetime
from openerp import api


class hr_employee_category_index(osv.osv):
    _name = "hr.employee.category.index"
    _columns = {
        'categ_id': fields.many2one('hr.employee.category', 'Categorie'),
        'name': fields.integer('Echelon'),
        'index': fields.integer('Indice'),
        }
    _order = 'name'


class hr_category_rubriques(osv.osv):
    _name = "hr.category.rubriques"
    _description = "Ligne de rubriques par poste"

    _columns = {
        'name': fields.many2one('hr.payroll_ma.rubrique', 'Rubrique'),
        'categ_id': fields.many2one('hr.employee.category',
                                    u'Catégorie', select=True),
        'montant': fields.float('Montant'),
    }


class hr_employee_category(osv.osv):
    _inherit = "hr.employee.category"
    _columns = {
        # 'code' :  fields.char('Code'),
        'index_ids': fields.one2many('hr.employee.category.index',
                                     'categ_id', 'Indice'),
        'rubrique_ids': fields.one2many('hr.category.rubriques',
                                        'categ_id', 'Rubriques'),
    }
    _order = 'name'


class hr_job(osv.osv):

    _inherit = "hr.job"
    _description = "Postes"

    _columns = {
        'categ_id': fields.many2one('hr.employee.category',
                                    u'Catégorie Initiale'),
        'rubrique_ids': fields.float('Rubriques'),
        }


class hr_employee(osv.osv):
    _inherit = "hr.employee"
    _columns = {
        'salary_index': fields.related(
            'contract_id', 'salary_index',
            type='integer', string='Index de Salaire Actuel'),
        'salary_index_name': fields.related(
            'contract_id', 'index_name',
            type='integer', string='Echelon Actuel')
    }


class hr_contract(osv.osv):
    _inherit = "hr.contract"

    def _get_index(self, cr, uid, ids, field_name, args, context=None):
        res = {}
        obj_index = self.pool.get('hr.employee.category.index')
        for emp in self.browse(cr, uid, ids, context=context):
            index_ids = obj_index.search(
                cr, uid,
                [('categ_id', '=', emp.categ_id.id),
                 ('name', '=', emp.index_name)], context=context)
            indexes = obj_index.browse(cr, uid, index_ids)
            res[emp.id] = False
            for index in indexes:
                res[emp.id] = index.index
        return res

    _columns = {
        'categ_id': fields.many2one('hr.employee.category', u'Catégorie'),
        'index_name': fields.integer('Echelon'),
        'salary_index': fields.function(
            _get_index, method=True, string='Indice de salaire',
            type='integer'),
        }

    def copy_contract(self, cr, uid, ids, context=None):
        for rec in self.browse(cr, uid, ids, context):
            self.copy(cr, uid, rec.id, {}, context)
        return True

    @api.one
    def index_promote(self):
        param_obj = self.env['hr.payroll_ma.parametres']
        param_ids = param_obj.search([])
        for param in param_ids:
            if not param.salary_on_index:
                return False
            else:
                index_value = param.index
        self.index_name = self.index_name + 1
        index_names = []
        for index in self.job_id.categ_id.index_ids:
            index_names.append(index.name)
            if index.name == self.index_name:
                self.wage = index.index * 173.33 * index_value / 5
                break
        if self.index_name > max(index_names):
            emp_cat_obj = self.env['hr.employee.category']
            cat_ids = emp_cat_obj.search([('parent_id', '!=', False)])
            cat_found = False
            for cat_id in cat_ids:
                if cat_found:
                    # new_cat = cat_id.id
                    for index in cat_id.index_ids:
                        if index.index > self.salary_index:
                            self.index_name = index.name
                            self.salary_index = index.index
                            self.categ_id = cat_id.id
                            self.wage = index.index * 173.33 * index_value / 5
                            break
                    break
                if cat_id.name == self.categ_id.name:
                    cat_found = True

    @api.onchange('index_name')
    def change_index_name(self):
        param_obj = self.env['hr.payroll_ma.parametres']
        param_ids = param_obj.search([])
        for param in param_ids:
            if not param.salary_on_index:
                return False
            else:
                index_value = param.index

        for index in self.job_id.categ_id.index_ids:
            if index.name == self.index_name:
                self.salary_index = index.index
                self.wage = index.index * 173.33 * index_value / 5

    @api.onchange('job_id')
    def change_job(self):
        param_obj = self.env['hr.payroll_ma.parametres']
        param_ids = param_obj.search([])
        for param in param_ids:
            if not param.salary_on_index:
                return False
            else:
                index_value = param.index

        if not self.job_id:
            return False
        job_cat = self.job_id.categ_id.name
        salary_index = self.salary_index
        change_cat = True
        index_names = []
        for index in self.job_id.categ_id.index_ids:
            index_names.append(index.name)
            if index.index > salary_index:
                change_cat = False
                self.index_name = index.name
                self.categ_id = self.job_id.categ_id.id
                self.salary_index = index.index
                self.wage = index.index * 173.33 * index_value / 5
                break
        if change_cat:
            if self.index_name > max(index_names):
                emp_cat_obj = self.env['hr.employee.category']
                cat_ids = emp_cat_obj.search([('parent_id', '!=', False)])
                cat_found = False
                for cat_id in cat_ids:
                    if cat_found:
                        # new_cat = cat_id.id
                        for index in cat_id.index_ids:
                            if index.index > salary_index:
                                self.index_name = index.name
                                self.salary_index = index.index
                                self.categ_id = cat_id.id
                                indx_m = index.index * 173.33
                                self.wage = indx_m * index_value / 5
                                break
                        break
                    if cat_id.name == job_cat:
                        cat_found = True
