From e65ade6ac2cb50c8ac09c05271a3b2cd227baae8 Mon Sep 17 00:00:00 2001 From: abdurrahman-saber <152967039+abdurrahman-saber@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:56:20 +0300 Subject: [PATCH] Revert "[IMP] hr_holidays_public: handle multi company configuration" --- .../hr_holidays_public/models/hr_holidays.py | 984 +++++++++--------- 1 file changed, 491 insertions(+), 493 deletions(-) diff --git a/odex25_hr/hr_holidays_public/models/hr_holidays.py b/odex25_hr/hr_holidays_public/models/hr_holidays.py index 5d25e58a0..2dd06cf27 100644 --- a/odex25_hr/hr_holidays_public/models/hr_holidays.py +++ b/odex25_hr/hr_holidays_public/models/hr_holidays.py @@ -572,159 +572,298 @@ class HRHolidays(models.Model): already_exist.write({'remaining_leaves': dutartion}) @api.model def process_holidays_scheduler_queue(self): - companies = self.env['res.company'].search([]) - for company in companies: - current_date_run = (datetime.utcnow() + timedelta(hours=3)).date() - employees = self.env['hr.employee'].search( - [('state', '=', 'open'), ('company_id', '=', company.id)] - ).filtered( - lambda emp: emp.first_hiring_date - and emp.first_hiring_date <= current_date_run - and emp.contract_id - and emp.contract_id.emp_type in ('other', 'saudi', 'displaced', 'external', 'external2') - and (not emp.contract_id.date_end or emp.contract_id.date_end and emp.contract_id.date_end >= current_date_run)) - holiday_status = self.env['hr.holidays.status'].search([('company_id', '=', company.id)]) - for emp in employees: - already_exist = None - current_date = (datetime.utcnow() + timedelta(hours=3)).date() + current_date_run = (datetime.utcnow() + timedelta(hours=3)).date() + employees = self.env['hr.employee'].search( + [('state', '=', 'open'), ('company_id', '=', self.env.user.company_id.id)] + ).filtered( + lambda emp: emp.first_hiring_date + and emp.first_hiring_date <= current_date_run + and emp.contract_id + and emp.contract_id.emp_type in ('other', 'saudi', 'displaced', 'external', 'external2') + and (not emp.contract_id.date_end or emp.contract_id.date_end and emp.contract_id.date_end >= current_date_run)) + holiday_status = self.env['hr.holidays.status'].search([('company_id', '=', self.env.user.company_id.id)]) + for emp in employees: + already_exist = None + current_date = (datetime.utcnow() + timedelta(hours=3)).date() + # current_date = datetime(2025, 12, 31).date() + first_hiring_date = datetime.strptime(str(emp.first_hiring_date), "%Y-%m-%d").date() + days = (current_date - first_hiring_date).days + 1 + for item in holiday_status: # current_date = datetime(2025, 12, 31).date() - first_hiring_date = datetime.strptime(str(emp.first_hiring_date), "%Y-%m-%d").date() - days = (current_date - first_hiring_date).days + 1 - for item in holiday_status: - # current_date = datetime(2025, 12, 31).date() - if days < item.number_of_days: - continue - is_years , is_month = False , False - if item.balance_type == 'monthly': - is_month = True - if item.balance_type == 'yearly': - is_years = True # True - self.send_email_holiday(item, emp) - if item.leave_type == 'annual' and (item.emp_type == 'all' or item.emp_type == emp.contract_id.emp_type): - if item.leave_annual_type == 'open_balance': # or item.leave_annual_type == 'save_annual_year' and item.number_of_holidays_save_years <= 0: - already_exist = self.env['hr.holidays'].search([('employee_id', '=', emp.id), - ('holiday_status_id', '=', item.id), - ('type', '=', 'add'), - ('check_allocation_view', '=', 'balance') - ], limit=1, order="id desc") - create_open_balance = self.env['hr.holidays'].search([('holiday_status_id', '=', item.id), - ('type', '=', 'add'), - ('check_allocation_view', '=', 'balance') - ], order="id desc") - if is_years: - current_date = current_date.replace(year=current_date.year , month=12, day=31) + if days < item.number_of_days: + continue + is_years , is_month = False , False + if item.balance_type == 'monthly': + is_month = True + if item.balance_type == 'yearly': + is_years = True # True + self.send_email_holiday(item, emp) + if item.leave_type == 'annual' and (item.emp_type == 'all' or item.emp_type == emp.contract_id.emp_type): + if item.leave_annual_type == 'open_balance': # or item.leave_annual_type == 'save_annual_year' and item.number_of_holidays_save_years <= 0: + already_exist = self.env['hr.holidays'].search([('employee_id', '=', emp.id), + ('holiday_status_id', '=', item.id), + ('type', '=', 'add'), + ('check_allocation_view', '=', 'balance') + ], limit=1, order="id desc") + create_open_balance = self.env['hr.holidays'].search([('holiday_status_id', '=', item.id), + ('type', '=', 'add'), + ('check_allocation_view', '=', 'balance') + ], order="id desc") + if is_years: + current_date = current_date.replace(year=current_date.year , month=12, day=31) + current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() + if is_month: + if self.is_last_day_of_month(current_date): + current_date = current_date + else: current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() - if is_month: - if self.is_last_day_of_month(current_date): - current_date = current_date - else: - current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() - current_date = current_date.replace(day=1) - timedelta(days=1) - if not already_exist and len(create_open_balance) > 1 and item.leave_annual_type == 'open_balance': - # if not already_exist and item.leave_annual_type == 'open_balance': - balance_leaves = self.remaining_leaves_of_day_by_date(emp, str(first_hiring_date), item , is_month=False, is_years=False) - already_exist = self.env['hr.holidays'].create({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'employee_id': emp.id, - 'holiday_status_id': item.id, - 'check_allocation_view': 'balance', - 'type': 'add', - 'remaining_leaves': balance_leaves, + current_date = current_date.replace(day=1) - timedelta(days=1) + if not already_exist and len(create_open_balance) > 1 and item.leave_annual_type == 'open_balance': + # if not already_exist and item.leave_annual_type == 'open_balance': + balance_leaves = self.remaining_leaves_of_day_by_date(emp, str(first_hiring_date), item , is_month=False, is_years=False) + already_exist = self.env['hr.holidays'].create({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'employee_id': emp.id, + 'holiday_status_id': item.id, + 'check_allocation_view': 'balance', + 'type': 'add', + 'remaining_leaves': balance_leaves, + }) + self.env['hr.inverse.holidays'].create( + {'holiday_id': already_exist.id, 'cron_run_date': first_hiring_date, 'balance_leaves':balance_leaves }) + if already_exist: + if not already_exist.holiday_ids: + not_balance = 0 + not_no_balance = 0 + if not is_years and not is_month: + not_balance = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) + not_no_balance = self.calc_not_balance_annual_leave(emp, current_date) * not_balance + already_exist.write({ + 'remaining_leaves': already_exist.remaining_leaves + not_balance - not_no_balance, }) - self.env['hr.inverse.holidays'].create( - {'holiday_id': already_exist.id, 'cron_run_date': first_hiring_date, 'balance_leaves':balance_leaves }) + self.env['hr.inverse.holidays'].create({'holiday_id': already_exist.id, 'cron_run_date': current_date, 'balance_leaves':not_balance, 'no_balance_leaves':not_no_balance }) + holiday = already_exist.holiday_ids[-1] + if not holiday.cron_run_date: + not_balance = 0 + not_no_balance = 0 + if not is_years and not is_month: + not_balance = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) + not_no_balance = self.calc_not_balance_annual_leave(emp, current_date) * not_balance + already_exist.write({ + 'remaining_leaves': already_exist.remaining_leaves + not_balance - not_no_balance, + }) + holiday.write({'cron_run_date': current_date}) + holiday_cron_run_date = datetime.strptime(str(holiday.cron_run_date ), "%Y-%m-%d").date() + if current_date > holiday_cron_run_date or is_years: + holiday_cron_run_date = datetime.strptime(str(holiday.cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() + current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() + if holiday_cron_run_date >= current_date: + continue + delta = current_date - holiday_cron_run_date # timedelta + months_date = holiday_cron_run_date + months = (current_date.year - months_date.year) * 12 + (current_date.month - months_date.month) + 1 + item_values = [] + year = current_date.year - holiday_cron_run_date.year + range_val = [] + range_val = range(1, delta.days + 1) + if is_years: + range_val = range(1, year + 1) + end_day = (holiday_cron_run_date.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) + end_date_cron = (end_day - holiday_cron_run_date).days + if end_date_cron > 0: + no_year_balance , year_balance = 0, 0 + for i in range(1, end_date_cron + 1): + cala_date = holiday_cron_run_date + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + already_exist.write({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'remaining_leaves': already_exist.remaining_leaves + year_balance - no_year_balance, + }) + already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) + complete_month = datetime.strptime(str(already_exist.holiday_ids[-1].cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() + cron_run_date_y = 12 - complete_month.month + for x in range(1 , cron_run_date_y + 1): + no_year_balance , year_balance = 0, 0 + end_day = (complete_month.replace(day=1) + relativedelta.relativedelta(months=x + 1)) - timedelta(days=1) + y = end_day.replace(day=1) + for i in range(0, (end_day - y).days + 1): + cala_date = y + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + already_exist.write({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'remaining_leaves': (already_exist.remaining_leaves + year_balance) - no_year_balance, + }) + already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) + if is_month: + end_day = (holiday_cron_run_date.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) + end_date_cron = (end_day - holiday_cron_run_date).days + if end_date_cron > 0: + no_year_balance , year_balance = 0, 0 + for i in range(1, end_date_cron + 1): + cala_date = holiday_cron_run_date + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + already_exist.write({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'remaining_leaves': already_exist.remaining_leaves + (year_balance - no_year_balance), + }) + already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) + range_val = range(1, months) + for i in range_val: + no_year_balance , year_balance = 0, 0 + cron_date = holiday_cron_run_date + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cron_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cron_date) * balance) + year_balance = year_balance + balance + if is_years: + no_year_balance , year_balance = 0, 0 + cron_date = holiday_cron_run_date.replace(year=holiday_cron_run_date.year + i , month=12, day=31) + y = cron_date.replace(year=cron_date.year - 1 , month=12, day=31) + for i in range(1, (cron_date - y).days + 1): + cala_date = y + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + if is_month: + cron_date = (holiday_cron_run_date.replace(day=1) + relativedelta.relativedelta(months=i + 1)) - timedelta(days=1) + y = cron_date.replace(day=1) + no_year_balance , year_balance = 0, 0 + for i in range(0, (cron_date - y).days + 1): + cala_date = y + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + item_values.append({'cron_run_date': cron_date, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance}) + already_exist.write({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'remaining_leaves':(already_exist.remaining_leaves + year_balance) - no_year_balance, + }) + already_exist.write({'holiday_ids': [(0, 0, item) for item in item_values]}) + # TO DO cala dutartion if val days_save == 0 or set days_save BY ABUZAR + # DONE call end method --- + # self.reset_remaining_leaves(current_date, item, emp, already_exist,is_years) + elif item.leave_annual_type == 'save_annual_year': + # and item.number_of_holidays_save_years > 0: + current_date = (datetime.utcnow() + timedelta(hours=3)).date() # replace with: current_date_time++ + # current_date = datetime(2025, 12, 31).date() + current_date_val = current_date.year + old_year_date_val = current_date.year - 1 # previous year + end_of_save_year = datetime.strftime(current_date, "{0}-12-31 23:59:59".format(old_year_date_val)) # previous_year_end + end_of_save_year_value = datetime.strptime(str(end_of_save_year), "%Y-%m-%d 23:59:59").date() # previous_year_end Date + start_of_new_year = datetime.strftime(first_hiring_date, "{0}-01-01".format(current_date_val)) + start_of_save_year_value = datetime.strptime(str(start_of_new_year), "%Y-%m-%d").date() # start of new year + working_days_old = (end_of_save_year_value - first_hiring_date).days + 1 # working days till end prv year عدد ايام عمل الموظف الي السنة الحالية + employee_working_years = item.number_of_holidays_save_years * 365 # balance of prev years to give? + + if working_days_old >= employee_working_years: + + remaining_leaves_of_day = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) + already_exist = self.env['hr.holidays'].search([('employee_id', '=', emp.id), + ('holiday_status_id', '=', item.id), + ('type', '=', 'add'), + ('check_allocation_view', '=', 'balance') + ], limit=1, order="id desc") if already_exist: - if not already_exist.holiday_ids: - not_balance = 0 - not_no_balance = 0 - if not is_years and not is_month: - not_balance = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) - not_no_balance = self.calc_not_balance_annual_leave(emp, current_date) * not_balance - already_exist.write({ - 'remaining_leaves': already_exist.remaining_leaves + not_balance - not_no_balance, - }) - self.env['hr.inverse.holidays'].create({'holiday_id': already_exist.id, 'cron_run_date': current_date, 'balance_leaves':not_balance, 'no_balance_leaves':not_no_balance }) - holiday = already_exist.holiday_ids[-1] - if not holiday.cron_run_date: - not_balance = 0 - not_no_balance = 0 - if not is_years and not is_month: - not_balance = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) - not_no_balance = self.calc_not_balance_annual_leave(emp, current_date) * not_balance - already_exist.write({ - 'remaining_leaves': already_exist.remaining_leaves + not_balance - not_no_balance, - }) - holiday.write({'cron_run_date': current_date}) - holiday_cron_run_date = datetime.strptime(str(holiday.cron_run_date ), "%Y-%m-%d").date() - if current_date > holiday_cron_run_date or is_years: - holiday_cron_run_date = datetime.strptime(str(holiday.cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() + """ + Important note: + we are no longer saving years but carrying balance from year to the next only; + according to Bakri Request done by Yousra. + """ + if is_years: + current_date = current_date.replace(year=current_date.year , month=12, day=31) current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() - if holiday_cron_run_date >= current_date: + if is_month: + if self.is_last_day_of_month(current_date): + current_date = current_date + else: + current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() + current_date = current_date.replace(day=1) - timedelta(days=1) + if not already_exist.holiday_ids: + self.env['hr.inverse.holidays'].create({'holiday_id': already_exist.id, 'cron_run_date': current_date, }) + holiday = already_exist.holiday_ids[-1] + if holiday.cron_run_date: + cron_run_date1 = datetime.strptime(str(holiday.cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() + current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() + if cron_run_date1 > current_date: continue - delta = current_date - holiday_cron_run_date # timedelta - months_date = holiday_cron_run_date + delta = current_date - cron_run_date1 # timedelta + months_date = cron_run_date1 + year = current_date.year - cron_run_date1.year months = (current_date.year - months_date.year) * 12 + (current_date.month - months_date.month) + 1 item_values = [] - year = current_date.year - holiday_cron_run_date.year range_val = [] range_val = range(1, delta.days + 1) if is_years: - range_val = range(1, year + 1) - end_day = (holiday_cron_run_date.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) - end_date_cron = (end_day - holiday_cron_run_date).days - if end_date_cron > 0: - no_year_balance , year_balance = 0, 0 - for i in range(1, end_date_cron + 1): - cala_date = holiday_cron_run_date + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance - already_exist.write({ + if year <= item.number_of_holidays_save_years: + range_val = range(1, year + 1) + end_day = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) + end_date_cron = (end_day - cron_run_date1).days + if end_date_cron > 0: + no_year_balance , year_balance = 0, 0 + for i in range(1, end_date_cron + 1): + cala_date = cron_run_date1 + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + already_exist.write({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'remaining_leaves': already_exist.remaining_leaves + year_balance - no_year_balance, + }) + already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) + + complete_month = datetime.strptime(str(already_exist.holiday_ids[-1].cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() + cron_run_date_y = 12 - complete_month.month + for x in range(1, cron_run_date_y + 1): + no_year_balance , year_balance = 0, 0 + end_day = (complete_month.replace(day=1) + relativedelta.relativedelta(months=x + 1)) - timedelta(days=1) + y = end_day.replace(day=1) + for i in range(0, (end_day - y).days + 1): + cala_date = y + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + already_exist.write({ 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + year_balance - no_year_balance, - }) - already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) - complete_month = datetime.strptime(str(already_exist.holiday_ids[-1].cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() - cron_run_date_y = 12 - complete_month.month - for x in range(1 , cron_run_date_y + 1): - no_year_balance , year_balance = 0, 0 - end_day = (complete_month.replace(day=1) + relativedelta.relativedelta(months=x + 1)) - timedelta(days=1) - y = end_day.replace(day=1) - for i in range(0, (end_day - y).days + 1): - cala_date = y + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance - already_exist.write({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': (already_exist.remaining_leaves + year_balance) - no_year_balance, - }) - already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) + 'remaining_leaves': (already_exist.remaining_leaves + year_balance) - no_year_balance, + }) + already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) + else: + cron_run_date1 = current_date.replace(year=current_date.year - item.number_of_holidays_save_years, month=12, day=31) + range_val = range(0, item.number_of_holidays_save_years + 1) if is_month: - end_day = (holiday_cron_run_date.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) - end_date_cron = (end_day - holiday_cron_run_date).days - if end_date_cron > 0: - no_year_balance , year_balance = 0, 0 - for i in range(1, end_date_cron + 1): - cala_date = holiday_cron_run_date + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance - already_exist.write({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + (year_balance - no_year_balance), - }) - already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) - range_val = range(1, months) + if year <= item.number_of_holidays_save_years: + end_day = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) + end_date_cron = (end_day - cron_run_date1).days + if end_date_cron > 0: + no_year_balance , year_balance = 0, 0 + for i in range(1, end_date_cron + 1): + cala_date = cron_run_date1 + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + already_exist.write({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'remaining_leaves': already_exist.remaining_leaves + (year_balance - no_year_balance), + }) + already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) + range_val = range(1, months) + else: + cron_run_date1 = current_date.replace(year=current_date.year - item.number_of_holidays_save_years , month=1, day=31) + range_val = range(0, (item.number_of_holidays_save_years) * 12 + (current_date.month - cron_run_date1.month) + 1) for i in range_val: no_year_balance , year_balance = 0, 0 - cron_date = holiday_cron_run_date + timedelta(days=i) + cron_date = cron_run_date1 + timedelta(days=i) balance = self.remaining_leaves_of_day_by_date(emp, str(cron_date), item , is_month=False, is_years=False) no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cron_date) * balance) year_balance = year_balance + balance if is_years: no_year_balance , year_balance = 0, 0 - cron_date = holiday_cron_run_date.replace(year=holiday_cron_run_date.year + i , month=12, day=31) + cron_date = cron_run_date1.replace(year=cron_run_date1.year + i , month=12, day=31) y = cron_date.replace(year=cron_date.year - 1 , month=12, day=31) for i in range(1, (cron_date - y).days + 1): cala_date = y + timedelta(days=i) @@ -732,7 +871,7 @@ class HRHolidays(models.Model): no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) year_balance = year_balance + balance if is_month: - cron_date = (holiday_cron_run_date.replace(day=1) + relativedelta.relativedelta(months=i + 1)) - timedelta(days=1) + cron_date = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=i + 1)) - timedelta(days=1) y = cron_date.replace(day=1) no_year_balance , year_balance = 0, 0 for i in range(0, (cron_date - y).days + 1): @@ -746,399 +885,258 @@ class HRHolidays(models.Model): 'remaining_leaves':(already_exist.remaining_leaves + year_balance) - no_year_balance, }) already_exist.write({'holiday_ids': [(0, 0, item) for item in item_values]}) - # TO DO cala dutartion if val days_save == 0 or set days_save BY ABUZAR - # DONE call end method --- - # self.reset_remaining_leaves(current_date, item, emp, already_exist,is_years) - elif item.leave_annual_type == 'save_annual_year': - # and item.number_of_holidays_save_years > 0: - current_date = (datetime.utcnow() + timedelta(hours=3)).date() # replace with: current_date_time++ - # current_date = datetime(2025, 12, 31).date() - current_date_val = current_date.year - old_year_date_val = current_date.year - 1 # previous year - end_of_save_year = datetime.strftime(current_date, "{0}-12-31 23:59:59".format(old_year_date_val)) # previous_year_end - end_of_save_year_value = datetime.strptime(str(end_of_save_year), "%Y-%m-%d 23:59:59").date() # previous_year_end Date - start_of_new_year = datetime.strftime(first_hiring_date, "{0}-01-01".format(current_date_val)) - start_of_save_year_value = datetime.strptime(str(start_of_new_year), "%Y-%m-%d").date() # start of new year - working_days_old = (end_of_save_year_value - first_hiring_date).days + 1 # working days till end prv year عدد ايام عمل الموظف الي السنة الحالية - employee_working_years = item.number_of_holidays_save_years * 365 # balance of prev years to give? - - if working_days_old >= employee_working_years: - - remaining_leaves_of_day = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) + else: + holiday.write({'cron_run_date': current_date, 'balance_leaves':remaining_leaves_of_day }) + already_exist.write({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'remaining_leaves': already_exist.remaining_leaves + float(remaining_leaves_of_day), + }) + else: + year_balance = 0 + no_year_balance = 0 + start_of_save_year_value = start_of_save_year_value.replace(year=(current_date.year - item.number_of_holidays_save_years)) + working_days_new = ((current_date - start_of_save_year_value).days + 1) + if not is_years and not is_month: + for index in range(0, working_days_new): + current_date = start_of_save_year_value + timedelta(days=index) + balance = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, current_date) * balance) + year_balance = year_balance + balance + if is_years: + year_balance = 0 + remaining_leaves_of_day = 0 + current_date = current_date.replace(year=current_date.year , month=12, day=31) + year_new = (current_date.year - first_hiring_date.year) + year_new = min(year_new, item.number_of_holidays_save_years) + 1 + y = current_date.replace(year=current_date.year - year_new , month=12, day=31) + for i in range(1, (current_date - y).days + 1): + cala_date = y + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + if is_month: + year_balance = 0 + remaining_leaves_of_day = 0 + current_date = (current_date.replace(day=1) + relativedelta.relativedelta(months=0)) - timedelta(days=1) + first_hiring_date = datetime.strptime(str(emp.first_hiring_date), "%Y-%m-%d").date() + remaining_leaves_of_day = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=is_month, is_years=is_years) + months_date = first_hiring_date + months_new = (current_date.year - months_date.year) * 12 + current_date.month - months_date.month + save_month = item.number_of_holidays_save_years * 12 + min_months = min(months_new, save_month) + if months_new > save_month: + last_date = current_date.replace(year=current_date.year - 1, month=12, day=31) + open_years_month = (current_date.year - last_date.year) * 12 + current_date.month - last_date.month + min_months = min_months + open_years_month + y = (current_date - relativedelta.relativedelta(months=min_months - 1)).replace(day=1) + for i in range(0, (current_date - y).days + 1): + cala_date = y + timedelta(days=i) + balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) + year_balance = year_balance + balance + already_exist = self.env['hr.holidays'].create({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'employee_id': emp.id, + 'holiday_status_id': item.id, + 'check_allocation_view': 'balance', + 'type': 'add', + 'remaining_leaves': year_balance - no_year_balance, + }) + self.env['hr.inverse.holidays'].create({'holiday_id': already_exist.id, 'cron_run_date': current_date, 'balance_leaves':year_balance , 'no_balance_leaves':no_year_balance }) + else: + if is_years: + current_date = current_date.replace(year=current_date.year , month=12, day=31) + current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() + if is_month: + current_date = (current_date.replace(day=1) + relativedelta.relativedelta(months=0)) - timedelta(days=1) + days_of_new_employee = (current_date - first_hiring_date).days + 1 + if days_of_new_employee >= item.number_of_days: already_exist = self.env['hr.holidays'].search([('employee_id', '=', emp.id), - ('holiday_status_id', '=', item.id), - ('type', '=', 'add'), - ('check_allocation_view', '=', 'balance') - ], limit=1, order="id desc") + ('holiday_status_id', '=', item.id), + ('type', '=', 'add'), + ('check_allocation_view', '=', 'balance') + ], limit=1, order="id desc") + if not already_exist: + balance_leaves = self.remaining_leaves_of_day_by_date(emp, str(first_hiring_date), item , is_month=False, is_years=False) + already_exist = self.env['hr.holidays'].create({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'employee_id': emp.id, + 'holiday_status_id': item.id, + 'check_allocation_view': 'balance', + 'type': 'add', + 'remaining_leaves': balance_leaves, + }) + self.env['hr.inverse.holidays'].create( + {'holiday_id': already_exist.id, 'cron_run_date': first_hiring_date, 'balance_leaves':balance_leaves }) + if not is_years and not is_month: + working_days_new = ((current_date - first_hiring_date).days) + 1 + year_balance = already_exist.remaining_leaves or 0 + no_balance = 0 + for index in range(1, working_days_new): + current_date = first_hiring_date + timedelta(days=index) + balance = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) + no_balance = no_balance + self.calc_not_balance_annual_leave(emp, current_date) * balance + year_balance = year_balance + balance + already_exist.holiday_ids[-1].cron_run_date = current_date + already_exist.holiday_ids[-1].balance_leaves = year_balance + already_exist.holiday_ids[-1].no_balance_leaves = no_balance + already_exist.remaining_leaves = year_balance - no_balance if already_exist: - """ - Important note: - we are no longer saving years but carrying balance from year to the next only; - according to Bakri Request done by Yousra. - """ - if is_years: - current_date = current_date.replace(year=current_date.year , month=12, day=31) - current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() - if is_month: - if self.is_last_day_of_month(current_date): - current_date = current_date - else: - current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() - current_date = current_date.replace(day=1) - timedelta(days=1) if not already_exist.holiday_ids: - self.env['hr.inverse.holidays'].create({'holiday_id': already_exist.id, 'cron_run_date': current_date, }) - holiday = already_exist.holiday_ids[-1] - if holiday.cron_run_date: - cron_run_date1 = datetime.strptime(str(holiday.cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() - current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() - if cron_run_date1 > current_date: - continue - delta = current_date - cron_run_date1 # timedelta - months_date = cron_run_date1 - year = current_date.year - cron_run_date1.year - months = (current_date.year - months_date.year) * 12 + (current_date.month - months_date.month) + 1 - item_values = [] - range_val = [] - range_val = range(1, delta.days + 1) - if is_years: - if year <= item.number_of_holidays_save_years: + current_date_new = (datetime.utcnow() + timedelta(hours=3)).date() + balance_leaves = self.remaining_leaves_of_day_by_date(emp, str(current_date_new), item , is_month=False, is_years=False) + self.env['hr.inverse.holidays'].create({'holiday_id': already_exist.id, 'cron_run_date': current_date_new, 'balance_leaves':balance_leaves }) + holiday.write({'cron_run_date': current_date, }) + already_exist.write({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'remaining_leaves': already_exist.remaining_leaves + balance_leaves, + }) + else: + holiday = already_exist.holiday_ids[-1] + if holiday.cron_run_date: + cron_run_date1 = datetime.strptime(str(holiday.cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() + if cron_run_date1 >= current_date: + continue + current_date1 = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() + delta = current_date1 - cron_run_date1 # timedelta + months_date = cron_run_date1 + months = (current_date1.year - months_date.year) * 12 + current_date1.month - months_date.month + year = current_date1.year - cron_run_date1.year + item_values = [] + range_val = [] + range_val = range(1, delta.days + 1) + if is_years: range_val = range(1, year + 1) end_day = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) end_date_cron = (end_day - cron_run_date1).days if end_date_cron > 0: - no_year_balance , year_balance = 0, 0 - for i in range(1, end_date_cron + 1): - cala_date = cron_run_date1 + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance + balance_leaves = 0 + no_year_balance = 0 + start_date = (end_day - cron_run_date1).days + 1 + for index in range(1, start_date): + calc_date = cron_run_date1 + timedelta(days=index) + balance = self.remaining_leaves_of_day_by_date(emp, str(calc_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, calc_date) * balance) + balance_leaves = balance_leaves + balance + # balance_leaves = balance_leaves - no_year_balance already_exist.write({ 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + year_balance - no_year_balance, + 'remaining_leaves': already_exist.remaining_leaves + balance_leaves - no_year_balance, }) - already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) - + already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':balance_leaves, 'no_balance_leaves':no_year_balance})]}) complete_month = datetime.strptime(str(already_exist.holiday_ids[-1].cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() cron_run_date_y = 12 - complete_month.month for x in range(1, cron_run_date_y + 1): - no_year_balance , year_balance = 0, 0 end_day = (complete_month.replace(day=1) + relativedelta.relativedelta(months=x + 1)) - timedelta(days=1) - y = end_day.replace(day=1) - for i in range(0, (end_day - y).days + 1): - cala_date = y + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance + balance_leaves = 0 + no_year_balance = 0 + start_date = (end_day - end_day.replace(day=1)).days + 1 + for index in range(0, start_date): + calc_date = end_day.replace(day=1) + timedelta(days=index) + balance = self.remaining_leaves_of_day_by_date(emp, str(calc_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, calc_date) * balance) + balance_leaves = balance_leaves + balance + # balance_leaves = balance_leaves - no_year_balance already_exist.write({ 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': (already_exist.remaining_leaves + year_balance) - no_year_balance, + 'remaining_leaves': already_exist.remaining_leaves + balance_leaves - no_year_balance }) - already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) - else: - cron_run_date1 = current_date.replace(year=current_date.year - item.number_of_holidays_save_years, month=12, day=31) - range_val = range(0, item.number_of_holidays_save_years + 1) - if is_month: - if year <= item.number_of_holidays_save_years: + already_exist.write({'holiday_ids': [(0, 0, {\ + 'cron_run_date': end_day, \ + 'balance_leaves':balance_leaves, \ + 'no_balance_leaves':no_year_balance})]}) + if is_month: + balance_leaves = 0 + no_year_balance = 0 end_day = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) end_date_cron = (end_day - cron_run_date1).days if end_date_cron > 0: - no_year_balance , year_balance = 0, 0 - for i in range(1, end_date_cron + 1): - cala_date = cron_run_date1 + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance + for index in range(1, end_date_cron + 1): + calc_date = cron_run_date1 + timedelta(days=index) + balance = self.remaining_leaves_of_day_by_date(emp, str(calc_date), item , is_month=False, is_years=False) + no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, calc_date) * balance) + balance_leaves = balance_leaves + balance + # balance_leaves = balance_leaves - no_year_balance already_exist.write({ 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + (year_balance - no_year_balance), + 'remaining_leaves': already_exist.remaining_leaves + balance_leaves - no_year_balance, }) - already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance})]}) - range_val = range(1, months) - else: - cron_run_date1 = current_date.replace(year=current_date.year - item.number_of_holidays_save_years , month=1, day=31) - range_val = range(0, (item.number_of_holidays_save_years) * 12 + (current_date.month - cron_run_date1.month) + 1) - for i in range_val: - no_year_balance , year_balance = 0, 0 - cron_date = cron_run_date1 + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cron_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cron_date) * balance) - year_balance = year_balance + balance - if is_years: - no_year_balance , year_balance = 0, 0 - cron_date = cron_run_date1.replace(year=cron_run_date1.year + i , month=12, day=31) - y = cron_date.replace(year=cron_date.year - 1 , month=12, day=31) - for i in range(1, (cron_date - y).days + 1): - cala_date = y + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance - if is_month: - cron_date = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=i + 1)) - timedelta(days=1) - y = cron_date.replace(day=1) - no_year_balance , year_balance = 0, 0 - for i in range(0, (cron_date - y).days + 1): - cala_date = y + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance - item_values.append({'cron_run_date': cron_date, 'balance_leaves':year_balance, 'no_balance_leaves':no_year_balance}) - already_exist.write({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves':(already_exist.remaining_leaves + year_balance) - no_year_balance, - }) - already_exist.write({'holiday_ids': [(0, 0, item) for item in item_values]}) - else: - holiday.write({'cron_run_date': current_date, 'balance_leaves':remaining_leaves_of_day }) - already_exist.write({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + float(remaining_leaves_of_day), - }) - else: - year_balance = 0 - no_year_balance = 0 - start_of_save_year_value = start_of_save_year_value.replace(year=(current_date.year - item.number_of_holidays_save_years)) - working_days_new = ((current_date - start_of_save_year_value).days + 1) - if not is_years and not is_month: - for index in range(0, working_days_new): - current_date = start_of_save_year_value + timedelta(days=index) - balance = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, current_date) * balance) - year_balance = year_balance + balance - if is_years: - year_balance = 0 - remaining_leaves_of_day = 0 - current_date = current_date.replace(year=current_date.year , month=12, day=31) - year_new = (current_date.year - first_hiring_date.year) - year_new = min(year_new, item.number_of_holidays_save_years) + 1 - y = current_date.replace(year=current_date.year - year_new , month=12, day=31) - for i in range(1, (current_date - y).days + 1): - cala_date = y + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance - if is_month: - year_balance = 0 - remaining_leaves_of_day = 0 - current_date = (current_date.replace(day=1) + relativedelta.relativedelta(months=0)) - timedelta(days=1) - first_hiring_date = datetime.strptime(str(emp.first_hiring_date), "%Y-%m-%d").date() - remaining_leaves_of_day = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=is_month, is_years=is_years) - months_date = first_hiring_date - months_new = (current_date.year - months_date.year) * 12 + current_date.month - months_date.month - save_month = item.number_of_holidays_save_years * 12 - min_months = min(months_new, save_month) - if months_new > save_month: - last_date = current_date.replace(year=current_date.year - 1, month=12, day=31) - open_years_month = (current_date.year - last_date.year) * 12 + current_date.month - last_date.month - min_months = min_months + open_years_month - y = (current_date - relativedelta.relativedelta(months=min_months - 1)).replace(day=1) - for i in range(0, (current_date - y).days + 1): - cala_date = y + timedelta(days=i) - balance = self.remaining_leaves_of_day_by_date(emp, str(cala_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, cala_date) * balance) - year_balance = year_balance + balance - already_exist = self.env['hr.holidays'].create({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'employee_id': emp.id, - 'holiday_status_id': item.id, - 'check_allocation_view': 'balance', - 'type': 'add', - 'remaining_leaves': year_balance - no_year_balance, - }) - self.env['hr.inverse.holidays'].create({'holiday_id': already_exist.id, 'cron_run_date': current_date, 'balance_leaves':year_balance , 'no_balance_leaves':no_year_balance }) - else: - if is_years: - current_date = current_date.replace(year=current_date.year , month=12, day=31) - current_date = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() - if is_month: - current_date = (current_date.replace(day=1) + relativedelta.relativedelta(months=0)) - timedelta(days=1) - days_of_new_employee = (current_date - first_hiring_date).days + 1 - if days_of_new_employee >= item.number_of_days: - already_exist = self.env['hr.holidays'].search([('employee_id', '=', emp.id), - ('holiday_status_id', '=', item.id), - ('type', '=', 'add'), - ('check_allocation_view', '=', 'balance') - ], limit=1, order="id desc") - if not already_exist: - balance_leaves = self.remaining_leaves_of_day_by_date(emp, str(first_hiring_date), item , is_month=False, is_years=False) - already_exist = self.env['hr.holidays'].create({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'employee_id': emp.id, - 'holiday_status_id': item.id, - 'check_allocation_view': 'balance', - 'type': 'add', - 'remaining_leaves': balance_leaves, - }) - self.env['hr.inverse.holidays'].create( - {'holiday_id': already_exist.id, 'cron_run_date': first_hiring_date, 'balance_leaves':balance_leaves }) - if not is_years and not is_month: - working_days_new = ((current_date - first_hiring_date).days) + 1 - year_balance = already_exist.remaining_leaves or 0 - no_balance = 0 - for index in range(1, working_days_new): - current_date = first_hiring_date + timedelta(days=index) - balance = self.remaining_leaves_of_day_by_date(emp, str(current_date), item , is_month=False, is_years=False) - no_balance = no_balance + self.calc_not_balance_annual_leave(emp, current_date) * balance - year_balance = year_balance + balance - already_exist.holiday_ids[-1].cron_run_date = current_date - already_exist.holiday_ids[-1].balance_leaves = year_balance - already_exist.holiday_ids[-1].no_balance_leaves = no_balance - already_exist.remaining_leaves = year_balance - no_balance - if already_exist: - if not already_exist.holiday_ids: - current_date_new = (datetime.utcnow() + timedelta(hours=3)).date() - balance_leaves = self.remaining_leaves_of_day_by_date(emp, str(current_date_new), item , is_month=False, is_years=False) - self.env['hr.inverse.holidays'].create({'holiday_id': already_exist.id, 'cron_run_date': current_date_new, 'balance_leaves':balance_leaves }) - holiday.write({'cron_run_date': current_date, }) - already_exist.write({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + balance_leaves, - }) - else: - holiday = already_exist.holiday_ids[-1] - if holiday.cron_run_date: - cron_run_date1 = datetime.strptime(str(holiday.cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() - if cron_run_date1 >= current_date: - continue - current_date1 = datetime.strptime(str(current_date), DEFAULT_SERVER_DATE_FORMAT).date() - delta = current_date1 - cron_run_date1 # timedelta - months_date = cron_run_date1 - months = (current_date1.year - months_date.year) * 12 + current_date1.month - months_date.month - year = current_date1.year - cron_run_date1.year - item_values = [] - range_val = [] - range_val = range(1, delta.days + 1) + already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':balance_leaves, 'no_balance_leaves':no_year_balance})]}) + range_val = range(1, months + 1) + for i in range_val: + cron_date = cron_run_date1 + timedelta(days=i) + balance_leaves = self.remaining_leaves_of_day_by_date(emp, str(cron_date), item , is_month=False, is_years=False) + no_year_balance = self.calc_not_balance_annual_leave(emp, cron_date) * balance_leaves if is_years: - range_val = range(1, year + 1) - end_day = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) - end_date_cron = (end_day - cron_run_date1).days - if end_date_cron > 0: + cron_date = cron_run_date1.replace(year=cron_run_date1.year + i , month=12, day=31) + if is_month: + cron_date = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=i + 1)) - timedelta(days=1) + if current_date >= cron_date: + if is_month: balance_leaves = 0 no_year_balance = 0 - start_date = (end_day - cron_run_date1).days + 1 - for index in range(1, start_date): - calc_date = cron_run_date1 + timedelta(days=index) + start_date = cron_date.replace(day=1) + start_days = (cron_date - cron_date.replace(day=1)).days + 1 + for index in range(0, start_days): + calc_date = start_date + timedelta(days=index) balance = self.remaining_leaves_of_day_by_date(emp, str(calc_date), item , is_month=False, is_years=False) no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, calc_date) * balance) balance_leaves = balance_leaves + balance - # balance_leaves = balance_leaves - no_year_balance - already_exist.write({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + balance_leaves - no_year_balance, - }) - already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':balance_leaves, 'no_balance_leaves':no_year_balance})]}) - complete_month = datetime.strptime(str(already_exist.holiday_ids[-1].cron_run_date), DEFAULT_SERVER_DATE_FORMAT).date() - cron_run_date_y = 12 - complete_month.month - for x in range(1, cron_run_date_y + 1): - end_day = (complete_month.replace(day=1) + relativedelta.relativedelta(months=x + 1)) - timedelta(days=1) + if is_years: balance_leaves = 0 no_year_balance = 0 - start_date = (end_day - end_day.replace(day=1)).days + 1 - for index in range(0, start_date): - calc_date = end_day.replace(day=1) + timedelta(days=index) + start_date = cron_date.replace(year=cron_date.year - 1) + start_date_number = (cron_date - start_date).days + 1 + for index in range(1, start_date_number): + calc_date = start_date + timedelta(days=index) balance = self.remaining_leaves_of_day_by_date(emp, str(calc_date), item , is_month=False, is_years=False) no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, calc_date) * balance) - balance_leaves = balance_leaves + balance - # balance_leaves = balance_leaves - no_year_balance - already_exist.write({ + balance_leaves = balance_leaves + balance + remaining_leaves_of_day = balance_leaves - no_year_balance + item_values.append({ 'cron_run_date': cron_date, 'balance_leaves':balance_leaves, 'no_balance_leaves':no_year_balance}) + already_exist.write({ 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + balance_leaves - no_year_balance - }) - already_exist.write({'holiday_ids': [(0, 0, {\ - 'cron_run_date': end_day, \ - 'balance_leaves':balance_leaves, \ - 'no_balance_leaves':no_year_balance})]}) - if is_month: - balance_leaves = 0 - no_year_balance = 0 - end_day = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=1)) - timedelta(days=1) - end_date_cron = (end_day - cron_run_date1).days - if end_date_cron > 0: - for index in range(1, end_date_cron + 1): - calc_date = cron_run_date1 + timedelta(days=index) - balance = self.remaining_leaves_of_day_by_date(emp, str(calc_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, calc_date) * balance) - balance_leaves = balance_leaves + balance - # balance_leaves = balance_leaves - no_year_balance - already_exist.write({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + balance_leaves - no_year_balance, - }) - already_exist.write({'holiday_ids': [(0, 0, {'cron_run_date': end_day, 'balance_leaves':balance_leaves, 'no_balance_leaves':no_year_balance})]}) - range_val = range(1, months + 1) - for i in range_val: - cron_date = cron_run_date1 + timedelta(days=i) - balance_leaves = self.remaining_leaves_of_day_by_date(emp, str(cron_date), item , is_month=False, is_years=False) - no_year_balance = self.calc_not_balance_annual_leave(emp, cron_date) * balance_leaves - if is_years: - cron_date = cron_run_date1.replace(year=cron_run_date1.year + i , month=12, day=31) - if is_month: - cron_date = (cron_run_date1.replace(day=1) + relativedelta.relativedelta(months=i + 1)) - timedelta(days=1) - if current_date >= cron_date: - if is_month: - balance_leaves = 0 - no_year_balance = 0 - start_date = cron_date.replace(day=1) - start_days = (cron_date - cron_date.replace(day=1)).days + 1 - for index in range(0, start_days): - calc_date = start_date + timedelta(days=index) - balance = self.remaining_leaves_of_day_by_date(emp, str(calc_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, calc_date) * balance) - balance_leaves = balance_leaves + balance - if is_years: - balance_leaves = 0 - no_year_balance = 0 - start_date = cron_date.replace(year=cron_date.year - 1) - start_date_number = (cron_date - start_date).days + 1 - for index in range(1, start_date_number): - calc_date = start_date + timedelta(days=index) - balance = self.remaining_leaves_of_day_by_date(emp, str(calc_date), item , is_month=False, is_years=False) - no_year_balance = no_year_balance + (self.calc_not_balance_annual_leave(emp, calc_date) * balance) - balance_leaves = balance_leaves + balance - remaining_leaves_of_day = balance_leaves - no_year_balance - item_values.append({ 'cron_run_date': cron_date, 'balance_leaves':balance_leaves, 'no_balance_leaves':no_year_balance}) - already_exist.write({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'remaining_leaves': already_exist.remaining_leaves + float(balance_leaves - no_year_balance), - }) - already_exist.write({'holiday_ids': [(0, 0, item) for item in item_values]}) - else: - current_date_new = (datetime.utcnow() + timedelta(hours=3)).date() - holiday.write({'cron_run_date': current_date_new}) - self.reset_remaining_leaves(current_date, item, emp, already_exist, is_years) - - elif item.leave_type != 'annual' and (item.gender == 'both' or item.gender == emp.gender): - if days >= item.number_of_days: - current_date = (datetime.utcnow() + timedelta(hours=3)).date() - already_exist = self.env['hr.holidays'].search([('employee_id', '=', emp.id), - ('holiday_status_id', '=', item.id), - ('type', '=', 'add'), - ('check_allocation_view', '=', 'balance') - ], limit=1, order="id desc") - remaining_leaves = item.duration - if already_exist: - # Renewal Balance of Sick Leave - self.renewal_balance_sick_leave(emp, item, already_exist, current_date) - # Renewal Balance of Unpaid Leave - self.renewal_balance_unpaid_leave(emp, item, already_exist, current_date) - # Renewal Balance of Last Day OF Year - # leave_type = ['exam','death'] # Just set type leave in list add new Leave - self.renewal_balance_leaves_last_day_of_year(current_date, item, already_exist , emp) - # Renewal Balance of daily - #renewal_daily_leave_type = ['death', 'new_baby'] # Just set type leave in list add new Leave - self.renewal_balance_leaves_daily(current_date, item, already_exist ) - else: - hr_holidays = self.env['hr.holidays'].create({ - 'name': 'Yearly Allocation of ' + item.name + ' Leaves', - 'employee_id': emp.id, - 'holiday_status_id': item.id, - 'remaining_leaves': remaining_leaves, - 'check_allocation_view': 'balance', - 'type': 'add' - }) - self.env['hr.inverse.holidays'].create({'holiday_id': hr_holidays.id, - 'cron_run_date': current_date, - 'balance_leaves':remaining_leaves}) - + 'remaining_leaves': already_exist.remaining_leaves + float(balance_leaves - no_year_balance), + }) + already_exist.write({'holiday_ids': [(0, 0, item) for item in item_values]}) + else: + current_date_new = (datetime.utcnow() + timedelta(hours=3)).date() + holiday.write({'cron_run_date': current_date_new}) + self.reset_remaining_leaves(current_date, item, emp, already_exist, is_years) + + elif item.leave_type != 'annual' and (item.gender == 'both' or item.gender == emp.gender): + if days >= item.number_of_days: + current_date = (datetime.utcnow() + timedelta(hours=3)).date() + already_exist = self.env['hr.holidays'].search([('employee_id', '=', emp.id), + ('holiday_status_id', '=', item.id), + ('type', '=', 'add'), + ('check_allocation_view', '=', 'balance') + ], limit=1, order="id desc") + remaining_leaves = item.duration + if already_exist: + # Renewal Balance of Sick Leave + self.renewal_balance_sick_leave(emp, item, already_exist, current_date) + # Renewal Balance of Unpaid Leave + self.renewal_balance_unpaid_leave(emp, item, already_exist, current_date) + # Renewal Balance of Last Day OF Year + # leave_type = ['exam','death'] # Just set type leave in list add new Leave + self.renewal_balance_leaves_last_day_of_year(current_date, item, already_exist , emp) + # Renewal Balance of daily + #renewal_daily_leave_type = ['death', 'new_baby'] # Just set type leave in list add new Leave + self.renewal_balance_leaves_daily(current_date, item, already_exist ) + else: + hr_holidays = self.env['hr.holidays'].create({ + 'name': 'Yearly Allocation of ' + item.name + ' Leaves', + 'employee_id': emp.id, + 'holiday_status_id': item.id, + 'remaining_leaves': remaining_leaves, + 'check_allocation_view': 'balance', + 'type': 'add' + }) + self.env['hr.inverse.holidays'].create({'holiday_id': hr_holidays.id, + 'cron_run_date': current_date, + 'balance_leaves':remaining_leaves}) + def is_last_day_of_month(self, date): # Get the last day of the month for the given date last_day_of_month = (date.replace(day=1) \