# -*- coding: utf-8 -*-

import datetime
from datetime import datetime as dt
from datetime import timedelta
from dateutil import relativedelta
from hijridate import convert
# from googletrans import Translator

from odoo import models, fields, api, _
from odoo.exceptions import UserError

# translator = Translator()


# Contract
class Contract(models.Model):
    _inherit = 'hr.contract'

    name = fields.Char(related="employee_id.emp_no", readonly=True, string='Employee Number')
    state = fields.Selection(selection_add=[
        ('employeed_aproval', 'Employeed Approval'),
        ('hr_head_approval', 'HR Head Approval'),
        ('program_directory', 'Executive Approval'),
        ('end_contract', 'End Contract')
    ])

    active = fields.Boolean(default=True)
    employee_name = fields.Char(related="employee_id.name", readonly=True)
    employee_type = fields.Selection(selection=[('saudi', 'Saudi'), ('foreign', 'Foreign'),
                                                ('displaced', 'Displaced Tribes'),
                                                ('external', 'Outsource1'), ('external2', 'Outsource2')],
                                     default='saudi',
                                     tracking=True)
    contract_status = fields.Selection(selection=[('single', 'single contract'),
                                                  ('marriage', 'marriage contract')], default='single',
                                       tracking=True)
    contract_duration = fields.Selection(selection=[('3_months', '3 Months'),
                                                    ('6_months', '6 Months'),
                                                    ('9_months', '9 Months'),
                                                    ('12_months', '12 Months'),
                                                    ('24_months', '24 Months'), ('36_months', '36 Months'),
                                                    ('none', 'None')], default='12_months')
    experience_year = fields.Integer()
    has_end_service_benefit = fields.Boolean(string='Has end service benefits')

    # fields on salary information page
    suspended = fields.Boolean(string='Suspended')
    social_insurance = fields.Boolean(string='Social Insurance')
    salary = fields.Float(string='Base Salary', tracking=True)

    # fields of information page
    wage = fields.Float()
    advatages = fields.Text()
    trial_date_start = fields.Date(tracking=True)
    trial_date_end = fields.Date(tracking=True)
    date_start = fields.Date(tracking=True)
    date_end = fields.Date(tracking=True)
    note = fields.Text()
    # fields of dependent page
    employee_code = fields.Char()
    allow_mbl = fields.Boolean(string='Allow Mobile Allowance')
    sign_bonous = fields.Boolean(string='Sign on Bounus')
    loan_allow = fields.Boolean(string='Allow Loan Allowance')
    air_allow = fields.Boolean(string='Air Allowance')
    adults = fields.Integer(string='Adult(s)')
    # children = fields.Integer()
    infants = fields.Integer()
    package = fields.Float()
    gosi = fields.Boolean(string='GOSI')
    # vehicle_attendance = fields.Integer(string='Vehicle Attendance')
    # system_attendance = fields.Integer(string='System Attendance')
    # line_manager_attendance = fields.Integer(string='Line Manager Attendance')
    # expense_claim = fields.Float(string='Expense Claim')
    # hr_visa_ticket = fields.Float(string='HR Visa/Ticket')
    # other_allowances = fields.Float(string='Other Allowances')
    # advance_salary = fields.Float(string='Advance AGT Salary')
    hr_expense = fields.Float(string='Hr Expense')
    cash_sales = fields.Float(string='Cash Sales')
    traffic_fine = fields.Float(string='Traffic Fine')
    bk_balance = fields.Float(string='Bank Balance')
    other_deductions = fields.Float(string='Other Deductions')

    fn = fields.Char(string="First Name")
    mn = fields.Char("Middle Name")
    ln = fields.Char(string="Last Name")
    dn = fields.Char(string="Display Name")
    e_date = fields.Date(string="Effective Date")

    status = fields.Selection(selection=[('bachelor', 'Bachelor'), ('family', 'family')], string='Status')
    hra = fields.Char("HRA")
    t_allow = fields.Float(string="Transport Allowance")
    f_allow = fields.Float(string="Food Allowance")
    f_ot = fields.Float(string="Fixed OT")
    departure = fields.Char(string="Departure Air Port")
    destination = fields.Char(string="Destination Air Port")
    medical = fields.Selection(selection=[('yes', 'Yes'), ('no', 'No')], string='Medical')

    c_accommodation = fields.Selection(selection=[(
        'yes', 'Yes'),
        ('no', 'No')], string='Company Accommodation')

    c_vehicle = fields.Selection(selection=[(
        'yes', 'Yes'),
        ('no', 'No')], string='Company Vehicle')

    c_vacation = fields.Selection(selection=[(
        '12', '12 Months'),
        ('18', '18 Months'),
        ('24', '24 Months')], string='Contractual Vacation')

    nod = fields.Selection(selection=[(
        '12', '12 Months'),
        ('18', '18 Months'),
        ('24', '24 Months')], string='Number of days')

    probation = fields.Selection(selection=[(
        '3', '3 Months'),
        ('6', '6 Months')], string='Probation')

    dependent = fields.Selection(selection=[(
        '1', '1+1 '),
        ('2', '1+2 '),
        ('3', '1+3 '),
        ('all', 'All ')], string='Dependent')

    incentive = fields.Selection(selection=[(
        'yes', 'Yes'),
        ('no', 'No')], string='Incentive')

    monthly_salary = fields.Float(string='Monthly Salary', compute='_compute_monthly_salary')
    saudi_emp_type = fields.Selection([('saudi-contract', 'Saudi Contracting'),
                                       ('saudi-non', 'Saudi Non-Contracting')], string='Saudi Employee Type',
                                      default='saudi-contract')

    contract_type = fields.Selection([('local', 'Local'), ('international', 'International')], string='Contract Type')

    contract_description = fields.Selection([('locum', 'Temporary'), ('permanent', 'Permanent')],
                                            string='Contract Description', default='permanent')

    house_allowance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                            string='House Allowance Type', default='none')
    house_allowance = fields.Float('House Allowance')
    salary_insurnce = fields.Float(string='Salary Insurnce')
    overtime_eligible = fields.Selection([('yes', 'Yes'), ('no', 'No')], string='Overtime Eligibility', default='no')
    overtime_eligible_float = fields.Float('Overtime Eligibility Amount')
    exit_and_return = fields.Selection([('yes', 'Yes'), ('no', 'No')], string='Exit and Return', default='no')
    exit_and_return_amount = fields.Float('Exit and Return Amount', default=200)

    air_ticket_eligible = fields.Selection([('yes', 'Yes'), ('no', 'No')],
                                           string='Air Ticket Eligible', default='no')
    annual_leave = fields.Selection([('yes', 'Yes'), ('no', 'No')], string='Annual Leave', default="no")
    annual_leave_days = fields.Float(string='Annual Leave In Days')
    transport_allowance_type = fields.Selection(
        [('none', 'None'), ('perc', 'Percentage'), ('num', 'Number'), ('company', 'By Company')],
        string='Transportation Allowance Type', default='none')
    transport_allowance = fields.Float('Transportation Allowance')

    transport_allowance_temp = fields.Float(string='Transportation Allowance', compute='_get_amount')

    field_allowance_type = fields.Selection(
        [('none', 'None'), ('perc', 'Percentage'), ('num', 'Number'), ('company', 'By Company')],
        string='Field Allowance Type', default='none')
    field_allowance = fields.Float('Field Allowance')

    field_allowance_temp = fields.Float(string='Field Allowance', compute='_get_amount')

    special_allowance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                              string='Special Allowance Type', default='none')
    special_allowance = fields.Float('Special Allowance')
    special_allowance_temp = fields.Float('Special Allowance', compute='_get_amount')

    other_allowance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                            string='Other Allowances Type', default='none')
    other_allowance = fields.Float('Other Allowances')
    other_allowance_temp = fields.Float('Other Allowances', compute='_get_amount')

    travel_allowance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                             string='Travel Allowance Type', default='none')
    travel_allowance = fields.Float('Travel Allowance')
    travel_allowance_temp = fields.Float('Travel Allowance', compute='_get_amount')
    education_allowance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                                string='Education Allowance Type', default='none')
    education_allowance = fields.Float('Education Allowance')
    education_allowance_temp = fields.Float('Education Allowance', compute='_get_amount')

    food_allowance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                           string='Food Allowance Type', default='none')
    food_allowance2 = fields.Float('Food Allowance')
    food_allowance2_temp = fields.Float('Food Allowance', compute='_get_amount')

    security_allowance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                               string='Security Allowance Type', default='none')
    security_allowance = fields.Float('Security Allowance')
    security_allowance_temp = fields.Float('Security Allowance', compute='_get_amount')
    communication_allowance_type = fields.Selection(
        [('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
        string='Communication Allowance Type', default='none')
    communication_allowance = fields.Float('Communication Allowance')
    communication_allowance_temp = fields.Float('Communication Allowance', compute='_get_amount')

    retire_allowance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                             string='Retire Allowance Type', default='none')
    retire_allowance = fields.Float('Retirement Allowance')

    infect_allowance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                             string='Infection Allowance Type', default='none')
    infect_allowance = fields.Float('Infection Allowance')
    supervision_allowance_type = fields.Selection(
        [('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
        string='Supervision Allowance Type', default='none')
    supervision_allowance = fields.Float('Supervision Allowance')
    insurance_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                      string='Insurance Type', default='none')
    insurance = fields.Float('Insurance')
    other_deduction_type = fields.Selection([('none', 'None'), ('perc', 'Percentage'), ('num', 'Number')],
                                            string='Other Deductions Type', default='none')
    other_deduction = fields.Float('Other Deductions')
    gosi_deduction = fields.Float(compute="_calculate_gosi", string='Gosi (Employee Percentage)')
    gosi_employer_deduction = fields.Float(compute="_calculate_gosi", string='Gosi (Employer Percentage)')
    total_gosi = fields.Float(compute="_calculate_gosi", string='Total')
    is_gosi_deducted = fields.Selection([('yes', 'Yes'), ('no', 'No')], default='yes')
    blood_type = fields.Selection(
        [('O-', 'O−'), ('O+', 'O+'), ('A-', 'A−'), ('A+', 'A+'), ('B-', 'B−'), ('B+', 'B+'), ('AB-', 'AB−'),
         ('AB+', 'AB+')], 'Blood Type')

    religion = fields.Selection([('muslim', 'Muslim'), ('christian', 'Christian'), ('other', 'Other')],
                                string='Religion')
    gender = fields.Selection([('male', 'Male'), ('female', 'Female')],
                              string='Gender')

    birth_place = fields.Char('Birth Place')

    point_of_hire = fields.Char('Point of hire')
    city = fields.Char('City Hired From')
    country = fields.Char('Country Hired From')
    contact_address = fields.Char('Contact address', size=512)

    date_of_birth = fields.Date('Date Of Birth')
    marital = fields.Selection(
        [('single', 'Single'), ('married', 'Married'), ('widower', 'Widower'), ('divorced', 'Divorced')],
        string='Marital Status', default='single')
    mobile_no = fields.Char('Mobile No')
    p_o_box_no = fields.Char('P. O. Box')
    zip_code = fields.Char('Zip Code')
    saudi_id_iqama = fields.Char('Saudi ID / Identity No.')
    saudi_id_iqama_date = fields.Date('Saudi ID / Identity Issue Date')
    saudi_id_iqama_expiry = fields.Date('Saudi ID / Identity Expiry Date')
    passport_number = fields.Char('Passport number')
    passport_issue_date = fields.Date('Passport Issue Date')
    passport_expiry_date = fields.Date('Passport Expiry Date')
    passport_issue_place = fields.Char('Passport Issue Place')
    saudi_com_number = fields.Char('Saudi Commission Number')
    saudi_com_date = fields.Date('Saudi Commission Issue Date')
    saudi_com_expiry_date = fields.Date('Saudi Commission Expiry Date')
    bls_date = fields.Date('BLS Date')
    acls_date = fields.Date('ACLS Date')
    insurance_date = fields.Date('Insurance Date')
    specialty = fields.Char('Specialty')
    category = fields.Char('Category')
    effective_from = fields.Date('Effective From')
    to_contact = fields.Text('To contact in case of Emergency')

    emp_type = fields.Selection([('saudi', 'Holiday1'),
                                 ('other', 'Holiday2'),
                                 ('displaced', 'Holiday3'),
                                 ('external', 'Holiday4'),
                                 ('external2', 'Other')], string='Annual Leave Entitlement', tracking=True)
    appraisal = fields.Boolean('Appraisal')
    re_contract = fields.Boolean('re contract')
    contract_draft = fields.Boolean('Contract Draft')
    breakdown_allowance = fields.Float(compute="_cal_allowance", string='Breakdown Allowance')
    car_allowance = fields.Float('Car Allowance')
    ticket_allowance = fields.Float('Ticket Allowance')
    medical_ins_allowance = fields.Float('Medical Insurance Allowance')
    medical_ins_issue_date = fields.Date('Medical Insurance Issue Date')
    medical_ins_exp_date = fields.Date('Medical Insurance Expiry Date')
    join_date = fields.Date('Join Date')
    driving_lic_issue_date = fields.Date('Driving License Issue Date')
    driving_lic_exp_date = fields.Date('Driving License Expiry Date')
    driving_lic_issue_place = fields.Char('Driving License Issue Place')

    # Work permit fields
    visa_no = fields.Char('Visa Number')
    visa_expire = fields.Date('Visa Expiry Date')
    permit_no = fields.Char('Work Permit Number')
    dependants_ticket_amount = fields.Float(string='Dependants Ticket Amount', compute='_get_dependants_ticket_amount')
    air_ticket_amount = fields.Float(string='Air Ticket Amount')
    air_ticket_number = fields.Integer(string='Air Ticket No.')
    total_air_ticket_amount = fields.Float(string='Total Air Ticket Amount', compute='_get_total_ticket_amount')
    trial_duration = fields.Float(string='Trail Duration', compute='_compute_contract_duration')
    contract_duration_cal = fields.Float(string='Contract Duration', compute='_compute_contract_duration')

    # Relational fields
    job_id = fields.Many2one(related="employee_id.job_id", readonly=True, tracking=True)
    working_hours = fields.Many2one(related='employee_id.resource_calendar_id')
    analytic_account_id = fields.Many2one(comodel_name='account.analytic.account')
    journal_id = fields.Many2one(comodel_name='account.journal')
    vac_des = fields.Many2one(comodel_name='hr.vacation.dest', string='Vacation Destination')
    employee_dependent_ids = fields.One2many(comodel_name='hr.employee.dependent', inverse_name='contract_id')
    employee_dependant = fields.One2many('hr.employee.dependent', 'contract_id', 'Employee Dependants')
    children_allowance = fields.One2many('hr.children.allowance', 'contract_id', 'Children Allowance')
    nationality = fields.Many2one('res.country', related='employee_id.country_id', readonly=True)
    type_id = fields.Many2one(related='employee_id.employee_type_id', string="Contractor Type", tracking=True)
    contractor_type = fields.Many2one(related='employee_id.employee_type_id', string="Contractor Type", required=False, tracking=True, store=True)
    employee_id = fields.Many2one('hr.employee')
    department_id = fields.Many2one('hr.department', 'Department Name', related='employee_id.department_id',
                                    readonly=True)
    hiring_date = fields.Date(related='employee_id.first_hiring_date', string="Hiring Date", readonly=True)
    all_exper_year = fields.Integer(compute='_compute_all_experience', store=True)
    all_exper_month = fields.Integer(compute='_compute_all_experience', store=True)
    all_exper_day = fields.Integer(compute='_compute_all_experience', store=True)

    previous_contract_id = fields.Many2one('hr.contract', store=True, string='Previous Contract',
                                           help='The Previous Contract Of The Employee')

    #########################  consultant #####################new /:19/10
    consultants = fields.Boolean(default=False)
    consultant_salary = fields.Float(string='Consultant Salary')
    consultant_hour = fields.Float(string='Consultant Hour')
    max_consultant_hour = fields.Float(string='Max Consultant Hour')

    salary_status = fields.Selection([('in', 'IN'), ('out', 'OUT')],
                                     string='Salary Status')
    recruited_talent = fields.Selection([('billable', 'Billable'), ('un_billable', 'Un Billable')],
                                        string='Recruited Talent')
    class_contract = fields.Selection([('employees', 'Employees'), ('collect_money', 'Collect Money'),
                                                ('endowments', 'Endowments'),('researchers', 'Researchers')],
                                   string='Classification Contracts', tracking=True)
    period_ticket = fields.Integer()
    dependant_count = fields.Integer(compute='_get_employee_dependant_count', store=True)

    has_transportation = fields.Boolean(string='Has Transportation?', default=True)
    has_housing = fields.Boolean(string='Has Housing?', default=True)

    check_nationality = fields.Boolean(related='employee_id.check_nationality', store=True, string="Saudi?")

    last_salary_gosi = fields.Float(string='Last Salary Gosi', help='The Last Salary Gosi After The Age 50 Years.')


    def _get_employee_dependant_count(self):
        for rec in self:
            rec.dependant_count = len(
                self.env['hr.employee.dependent'].search([('contract_id', '=', rec.id)]))

    @api.depends('employee_id')
    def _compute_all_experience(self):
        for item in self:
            if item.employee_id:
                item.all_exper_year = item.employee_id.experience_year + item.employee_id.service_year
                item.all_exper_month = item.employee_id.experience_month + item.employee_id.service_month
                item.all_exper_day = item.employee_id.experience_day + item.employee_id.service_day

                if item.all_exper_month > 11:
                    item.all_exper_year = item.all_exper_year + 1
                    item.all_exper_month = item.all_exper_month - 12
                if item.all_exper_day > 30:
                    item.all_exper_month = item.all_exper_month + 1
                    item.all_exper_day = item.all_exper_day - 30

    @api.onchange('contractor_type')
    def onchange_contractor_type(self):
        if self.contractor_type:
            self.type_id = self.contractor_type
            self.consultants = self.contractor_type.consultants
            self.employee_id.employee_type_id = self.contractor_type

    @api.onchange('date_start', 'hiring_date')
    def get_trial_date_field(self):
        for rec in self:
            # rec.trial_date_start = False
            # rec.trial_date_end = False
            if rec.hiring_date and rec.date_start:
                date_start = datetime.datetime.strptime(str(rec.hiring_date), '%Y-%m-%d')
                rec.trial_date_start = rec.hiring_date
                rec.trial_date_end = date_start + relativedelta.relativedelta(months=3)
            elif rec.date_start and not rec.hiring_date:
                date_start = datetime.datetime.strptime(str(rec.date_start), '%Y-%m-%d')
                rec.trial_date_start = rec.date_start
                rec.trial_date_end = date_start + relativedelta.relativedelta(months=3)

    @api.onchange('employee_id')
    def _emp_type_employee(self):
        for item in self:
            previous_contract = self.search([('employee_id', '=', item.employee_id.id),
                                             ('active', '=', True), ], limit=1)
            if previous_contract:
                raise UserError(_('Sorry, Can Not Create More than One contract for an Employeet %s') %
                                         item.employee_id.name)

            if item.employee_id:
                #if item.employee_id.check_nationality:
                #    item.emp_type = 'saudi'
                #else:
                #    item.emp_type = 'other'
                if item.employee_id.marital == 'single':
                    item.contract_status = 'single'
                else:
                    item.contract_status = 'marriage'
                item.job_id = item.employee_id.job_id
                item.department_id = item.employee_id.department_id
                item.employee_code = item.employee_id.employee_code
                item.fn = item.employee_id.fn
                item.mn = item.employee_id.mn
                item.ln = item.employee_id.ln
                item.dn = item.employee_id.name

    '''@api.onchange('emp_type')
    def chick_saudi_percentage(self):
        for item in self:
            Saudization_percen = item.env.user.company_id.saudi_percentage
            if Saudization_percen > 0:
                saudi = len(item.search([('active', '=', True), ('emp_type', 'in', ('saudi', 'displaced')),
                                         ('state', '=', 'program_directory')]).ids)
                all_emp = len(item.search([('active', '=', True), ('state', '!=', 'end_contract')]).ids) + 1
                saudi_percen = (saudi / all_emp) * 100
                if saudi_percen > Saudization_percen and item.emp_type in ('saudi', 'displaced'):
                    raise UserError(
                        _('The Saudization percentage should not exceed  Percentage %s') % Saudization_percen)

                none_saudi = len(item.search(
                    [('active', '=', True), ('emp_type', '=', 'other'), ('state', '=', 'program_directory')]).ids)
                none_Saudization_percen = (100 - Saudization_percen)
                none_percen = (none_saudi / all_emp) * 100
                if none_percen > none_Saudization_percen and item.emp_type == 'other':
                    raise UserError(
                        _('The None Saudization percentage should not exceed Percentage %s') % none_Saudization_percen)'''

    def change_current_date_hijri(self, date):
        date = datetime.datetime.strptime(str(date), '%Y-%m-%d')
        year = date.year
        day = date.day
        month = date.month
        hijri_date = convert.Gregorian(year, month, day).to_hijri()
        return hijri_date

    def translate_to_eng(self, text):
        if text:
            eng_text = text
            ln = translator.detect(text)
            if ln.lang != 'en':
                eng_text = translator.translate(text, dest='en')
            return eng_text
        else:
            return ' '

    ###############>>send email end contract and trial peroid<<<##########
    @api.model
    def contract_mail_reminder(self):
        now = dt.now() + timedelta(days=1)
        date_now = now.date()
        match = self.search([('state', '!=', 'end_contract'),
                             ('contract_duration', 'not in',( '3_months','6_months'))])
        # trial_days_send_email=5
        cont_end_reminder = self.env.user.company_id.contract_end_reminder
        cont_trial_reminder = self.env.user.company_id.contract_trial_reminder
        for i in match:
            if i.date_end:
                exp_date = fields.Date.from_string(i.date_end) - timedelta(days=cont_end_reminder)
                if date_now >= exp_date:
                   self.env.ref('hr_contract_custom.email_template_contract_period_expiration_reminder', False).send_mail(i.id)

            if i.trial_date_end:
                exp_date = fields.Date.from_string(i.trial_date_end)
                exp_date1 = fields.Date.from_string(i.trial_date_end) - timedelta(days=cont_trial_reminder)
                # if date_now >= exp_date :
                if exp_date >= date_now and date_now >= exp_date1:
                   self.env.ref('hr_contract_custom.email_template_contract_trial_period_expiration_reminder', False).send_mail(i.id)

    ##########################################################################
    @api.onchange('contract_duration', 'date_start')
    def get_contract_end_date(self):
        if self.date_start and self.contract_description == 'locum':

            date_start = datetime.datetime.strptime(str(self.date_start), '%Y-%m-%d')

            if self.contract_duration == '3_months':
                self.date_end = date_start + relativedelta.relativedelta(months=3) - timedelta(days=1)
            elif self.contract_duration == '6_months':
                self.date_end = date_start + relativedelta.relativedelta(months=6) - timedelta(days=1)
            elif self.contract_duration == '9_months':
                self.date_end = date_start + relativedelta.relativedelta(months=9) - timedelta(days=1)
            elif self.contract_duration == '12_months':
                self.date_end = date_start + relativedelta.relativedelta(months=12) - timedelta(days=1)
            elif self.contract_duration == '24_months':
                self.date_end = date_start + relativedelta.relativedelta(months=24) - timedelta(days=1)
            elif self.contract_duration == '36_months':
                self.date_end = date_start + relativedelta.relativedelta(months=36) - timedelta(days=1)
            else:
                self.date_end = False

    # get salary amount form salary degree
    @api.onchange('salary_degree')
    def onchange_salary_degree(self):
        if self.salary_degree:
            self.salary = self.salary_degree.base_salary

    # Get Salary Insurnce from Salary amount
    @api.onchange('salary')
    def onchange_base_salary_insurance(self):
        if self.salary:
            self.salary_insurnce = self.salary

    # update  to control on date constrains
    @api.onchange('trial_date_start', 'trial_date_end', 'date_start', 'date_end')
    def onchange_dates(self):
        if self.trial_date_start:
            if self.trial_date_end:
                if self.date_start:
                    if self.date_end:
                        start_date_1 = dt.strptime(str(self.date_start), "%Y-%m-%d")
                        end_date_1 = dt.strptime(str(self.date_end), "%Y-%m-%d")
                        trial_start_date_1 = dt.strptime(str(self.trial_date_start), "%Y-%m-%d")
                        trial_end_date_1 = dt.strptime(str(self.trial_date_end), "%Y-%m-%d")

                        if trial_end_date_1 < trial_start_date_1:
                            raise UserError(_('trial End Date  must be greater than Trial Start date'))
                        if end_date_1 < start_date_1:
                            raise UserError(_('End  date must be greater than Start date'))

    @api.onchange('contract_description')
    def _contract_duration_change_state(self):
        if self.contract_description == 'permanent':
            self.contract_duration = 'none'
            self.date_end = ''

    @api.depends('wage', 'house_allowance', 'transport_allowance', 'communication_allowance')
    def _compute_monthly_salary(self):
        for rec in self:
            rec.monthly_salary = rec.wage + rec.house_allowance_temp + rec.transport_allowance_temp + \
                              rec.communication_allowance_temp + rec.field_allowance_temp + \
                              rec.special_allowance_temp + rec.other_allowance_temp

    @api.depends()
    def _cal_allowance(self):
        allowance = 0.0
        if self.employee_id.country_id.code == 'SA':
            allowance = self.salary * 1 / 100
        self.breakdown_allowance = allowance

    @api.depends()
    def _calculate_gosi(self):
        saudi_gosi = self.env.user.company_id.saudi_gosi
        company_gosi = self.env.user.company_id.company_gosi
        none_saudi_gosi = self.env.user.company_id.none_saudi_gosi

        max_saudi_gosi = self.env.user.company_id.max_amount_saudi_gosi
        max_company_gosi = self.env.user.company_id.max_amount_company_gosi

        Sanad_saudi_gosi = self.env.user.company_id.Sanad_saudi_gosi
        Sanad_company_gosi = self.env.user.company_id.Sanad_company_gosi

        for record in self:
            #if (record.emp_type == 'saudi' or record.emp_type == 'displaced') and record.is_gosi_deducted == "yes":
            record.employee_id._compute_employee_age()
            sanad_age = record.salary_scale.retirement_age
            emp_age = record.employee_id.employee_age

            if record.check_nationality==True and record.is_gosi_deducted == "yes":
                if emp_age >= sanad_age and sanad_age > 0 :
                   employee_gosi = (record.salary_insurnce + record.house_allowance_temp) * (saudi_gosi - Sanad_saudi_gosi) / 100
                   employer_gosi = (record.salary_insurnce + record.house_allowance_temp) * (company_gosi - Sanad_company_gosi)/ 100
                else:
                   employee_gosi = (record.salary_insurnce + record.house_allowance_temp) * saudi_gosi / 100
                   employer_gosi = (record.salary_insurnce + record.house_allowance_temp) * company_gosi / 100

                if employee_gosi <= max_saudi_gosi:
                   record.gosi_deduction = employee_gosi
                else:
                   record.gosi_deduction = max_saudi_gosi
                if employer_gosi <= max_company_gosi:
                   record.gosi_employer_deduction = employer_gosi
                else:
                   record.gosi_employer_deduction = max_company_gosi
                record.total_gosi = record.gosi_deduction + record.gosi_employer_deduction

            #elif (record.emp_type == 'saudi' or record.emp_type == 'displaced') and record.is_gosi_deducted == "no":
            elif record.check_nationality==True and record.is_gosi_deducted == "no":
                if emp_age >= sanad_age and sanad_age > 0 :
                   employee_gosi = (record.salary_insurnce + record.house_allowance_temp) * (saudi_gosi - Sanad_saudi_gosi) / 100
                   employer_gosi = (record.salary_insurnce + record.house_allowance_temp) * (company_gosi - Sanad_company_gosi) / 100
                else:
                   employee_gosi = (record.salary_insurnce + record.house_allowance_temp) * saudi_gosi / 100
                   employer_gosi = (record.salary_insurnce + record.house_allowance_temp) * company_gosi / 100

                if employee_gosi <= max_saudi_gosi:
                   employee_gosi = employee_gosi
                else:
                   employee_gosi = max_saudi_gosi
                if employer_gosi <= max_company_gosi:
                   employer_gosi = employer_gosi
                else:
                   employer_gosi = max_company_gosi

                record.gosi_deduction = 0.0
                record.gosi_employer_deduction = employee_gosi + employer_gosi
                record.total_gosi = employee_gosi + employer_gosi

            else:
                # pass
                employer_gosi = (record.salary_insurnce + record.house_allowance_temp) * none_saudi_gosi / 100

                record.gosi_deduction = 0.0
                record.gosi_employer_deduction = employer_gosi
                record.total_gosi = employer_gosi

            #if (record.emp_type == 'saudi' or record.emp_type == 'displaced') and record.saudi_emp_type == 'saudi-non':
            if record.check_nationality==True and record.saudi_emp_type == 'saudi-non':
                record.gosi_deduction = 0.0
                record.gosi_employer_deduction = 0.0
                record.total_gosi = 0.0

    @api.depends('date_start', 'date_end', 'trial_date_start', 'trial_date_end')
    def _compute_contract_duration(self):
        for item in self:
            item.contract_duration_cal = 0
            item.trial_duration = 0
            if item.date_start and item.date_end:
                date_start = datetime.datetime.strptime(str(item.date_start), '%Y-%m-%d').date()
                date_end = datetime.datetime.strptime(str(item.date_end), '%Y-%m-%d').date()
                item.contract_duration_cal = relativedelta.relativedelta(date_end, date_start).years

            if item.trial_date_start and item.trial_date_end:
                date_start = datetime.datetime.strptime(str(item.trial_date_start), '%Y-%m-%d').date()
                date_end = datetime.datetime.strptime(str(item.trial_date_end), '%Y-%m-%d').date()
                item.trial_duration = relativedelta.relativedelta(date_end, date_start).months
                if item.trial_duration > 6:
                    raise UserError(_('The period of trail duration must be not more than 6 months'))

    '''@api.onchange('date_start')
    def _compute_trial_period(self):
        if self.date_start and self.date_end:
            date_start = datetime.datetime.strptime(str(self.date_start), '%Y-%m-%d').date()
            self.trial_date_start = self.date_start
            self.trial_date_end = date_start + relativedelta.relativedelta(months=3) - timedelta(days=1)'''

    @api.depends('air_ticket_amount')
    def _get_total_ticket_amount(self):
        self.total_air_ticket_amount = self.air_ticket_amount * self.air_ticket_number

    @api.onchange('employee_id')
    def _onchange_employee_id(self):
        if self.employee_id:
            self.job_id = self.employee_id.job_id
            self.department_id = self.employee_id.department_id
            self.employee_code = self.employee_id.employee_code
            self.fn = self.employee_id.fn
            self.mn = self.employee_id.mn
            self.ln = self.employee_id.ln
            self.dn = self.employee_id.name

    @api.model_create_multi
    def create(self, vals_list):
        contracts = super(Contract, self).create(vals_list)
        for contract in contracts:
            contract.employee_id.contract_id = contract.id
        return contracts

    def draft_state(self):
        self.state = "draft"

    def employeed_aproval(self):
        #self.chick_saudi_percentage()
        self.state = "employeed_aproval"

    def hr_head_approval(self):
        #self.chick_saudi_percentage()
        self.state = "hr_head_approval"

    def end_contract_state(self):
        if self.date_end == False:
            raise UserError(_('The contract End Date Must Be Entered'))
        else:
            self.state = "end_contract"

    def program_directory(self):
        #self.chick_saudi_percentage()
        self.employee_id.contract_id = self.id
        hiring_date = self.employee_id.first_hiring_date
        effective = self.env['employee.effective.form'].search([('contract_id', '=', self.id)])
        if not effective and not hiring_date:
           self.env['employee.effective.form'].create({
                'employee_id': self.employee_id.id,
                'contract_id': self.id,
                'department_id': self.department_id.id,
                'contract_start': self.date_start,
                'effective_form_date': self.date_start,
                'state': 'draft'})
        self.state = "program_directory"

    def unlink(self):
        for i in self:
            if i.state != 'draft':
                raise UserError(_('You can not delete record in state not in draft'))
            # if i.hiring_date:
            # raise UserError(_('You can not delete record has Hiring date'))
        return super(Contract, self).unlink()

    @api.onchange('working_hours')
    def _onchange_working_hours(self):
        if self.employee_id.contract_id.id == self._origin.id:
            self.env['resource.resource'].browse([self.employee_id.resource_id.id]).write(
                {'calendar_id': self.working_hours.id})


    # Smart Button to access effective form

    def action_view_effective_frm(self):
        effective_id = self.env['employee.effective.form'].search([('employee_id', '=', self.employee_id.id)])
        return {
            'name': _('Employee Effective Form'),
            'domain': [('id', '=', effective_id.id)],
            'view_mode': 'list,form',
            'res_model': 'employee.effective.form',
            'view_id': False,
            'type': 'ir.actions.act_window',
        }


class VacationDest(models.Model):
    _name = 'hr.vacation.dest'

    _rec_name = 'name'
    name = fields.Char(required=True)


class EmployeeChildAllowance(models.Model):
    _name = 'hr.children.allowance'

    name = fields.Char('Children Name')
    age = fields.Integer('Age')
    fees = fields.Float('Educational Fees')
    remarks = fields.Text('Remarks')

    # Relational fields
    contract_id = fields.Many2one('hr.contract', 'Contract')


class ContractType(models.Model):
    _name = "hr.contract.type"
    _description = "Contract Type"
    _order = "sequence, id"

    name = fields.Char(string="Contract Type", required=True)
    sequence = fields.Integer(help="Gives the sequence when displaying a list of Contract.", default=10)
    salary_type = fields.Selection([
        ("amount", "Amount"),
        ("scale", "Scale")
    ], string="Salary Type")
    code = fields.Char(string='Code')
    consultants = fields.Boolean(default=False, string='Consultants')
    country_id = fields.Many2one('res.country', string='Country')

    company_id = fields.Many2one('res.company', string='Company', default=lambda self: self.env.company)

