bbone/application/modules/Leave/models/Leave_model.php
2024-03-05 14:10:53 +05:30

639 lines
22 KiB
PHP

<?php
class Leave_model extends MY_Model {
public function get_all_leave($business_id)
{
$this->db->select('A.* ,U1.name as emp_name, U2.name as ApproveBy, l1.type as leavetype');
$this->db->from('leave_list A');
$this->db->join('users U1', 'U1.id = A.emp_id', 'left');
$this->db->join('users U2', 'U2.id = A.approved_by', 'left');
$this->db->join('leave_master l1', 'l1.id = A.type', 'left');
$this->db->where('A.business_id', $business_id);
$this->db->where('A.is_active', 1);
$this->db->where('A.approved_by !=', '');
$this->db->order_by('A.id','DESC');
// $this->db->group_by('A.type,A.emp_id');
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_leave_with_emp_name($business_id)
{
$this->db->select('A.* ,U1.name as emp_name, U2.name as ApproveBy, U3.name as DeclineBy, U4.name as Risedby');
$this->db->from('leave_list A');
$this->db->join('users U1', 'U1.id = A.emp_id', 'left');
$this->db->join('users U2', 'U2.id = A.approved_by', 'left');
$this->db->join('users U3', 'U3.id = A.declined_by', 'left');
$this->db->join('users U4', 'U4.id = A.risedby', 'left');
$this->db->where('A.business_id', $business_id);
$this->db->order_by('A.id','DESC');
$this->db->where('A.is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_leave_data_using_leave_id($leave_id)
{
$this->db->select('A.* ,U1.name as emp_name, U1.email as emp_email, U2.name as ApproveBy, U3.name as DeclineBy, U4.name as Risedby, E.manager as ManagerId, LM.code as leave_code, LM.type as leave_type');
$this->db->from('leave_list A');
$this->db->join('users U1', 'U1.id = A.emp_id', 'left');
$this->db->join('users U2', 'U2.id = A.approved_by', 'left');
$this->db->join('users U3', 'U3.id = A.declined_by', 'left');
$this->db->join('users U4', 'U4.id = A.risedby', 'left');
$this->db->join('employees E', 'E.user_id = A.emp_id', 'left');
$this->db->join('leave_master LM', 'LM.id = A.type', 'left');
$this->db->where('A.id', $leave_id);
$this->db->order_by('A.id','DESC');
$this->db->where('A.is_active', 1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $row = $query->row();
} else {
return false;
}
}
public function get_template_data_using_code($code, $type){
$this->db->select('*');
$this->db->from('templates');
$this->db->where('templet_code', $code);
$this->db->where('templet_type', $type);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
} else {
return false;
}
}
public function get_user_name($user_id){
$this->db->select('name');
$this->db->from('users');
$this->db->where('id', $user_id);
$query = $this->db->get();
// $query = $query->result();
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->name;
} else {
return false;
}
// return $query;
}
public function get_all_leave_request_by_manager_id($business_id, $user_id)
{
$this->db->select('A.* ,U1.name as emp_name, U2.name as ApproveBy, U3.name as DeclineBy, U4.name as Risedby, E.manager as manager_id, LM.type as leave_type');
$this->db->from('leave_list A');
$this->db->join('users U1', 'U1.id = A.emp_id', 'left');
$this->db->join('users U2', 'U2.id = A.approved_by', 'left');
$this->db->join('users U3', 'U3.id = A.declined_by', 'left');
$this->db->join('users U4', 'U4.id = A.risedby', 'left');
$this->db->join('employees E', 'E.user_id = A.emp_id', 'left');
$this->db->join('leave_master LM', 'LM.id = A.type', 'left');
$this->db->where('A.business_id', $business_id);
$this->db->where('E.manager', $user_id);
$this->db->order_by('A.id','DESC');
$this->db->where('A.is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_delegated_user_id($business_id, $user_id){
$this->db->select('emp_id');
$this->db->from('delegation');
$this->db->where('business_id', $business_id);
$this->db->where('delegated_to', $user_id);
$this->db->where('is_active', 1);
$query = $this->db->get();
$result_array = $query->result_array();
$emp_ids = array_column($result_array, 'emp_id');
return $emp_ids;
}
public function get_all_leave_request_by_manager_id_with_delegation($business_id, $loggeduserid)
{
$delegated_users_id_list = $this->get_delegated_user_id($business_id, $loggeduserid);
$count = count($delegated_users_id_list);
if($count > 0)
{
foreach ($delegated_users_id_list as $user_id) {
$this->db->select('A.* ,U1.name as emp_name, U2.name as ApproveBy, U3.name as DeclineBy, U4.name as Risedby, E.manager as manager_id, LM.type as leave_type, D.from_date as delegate_f_date, D.to_date as delegate_t_date, D.delegated_to, D.emp_id');
$this->db->from('leave_list A');
$this->db->join('users U1', 'U1.id = A.emp_id', 'left');
$this->db->join('users U2', 'U2.id = A.approved_by', 'left');
$this->db->join('users U3', 'U3.id = A.declined_by', 'left');
$this->db->join('users U4', 'U4.id = A.risedby', 'left');
$this->db->join('employees E', 'E.user_id = A.emp_id', 'left');
$this->db->join('delegation D', 'D.business_id = A.business_id', 'left');
$this->db->join('leave_master LM', 'LM.id = A.type', 'left');
$this->db->where('A.business_id', $business_id);
$this->db->where('E.manager', $user_id); //User ID as Employee ID
// $this->db->where('D.emp_id', $user_id);
$this->db->order_by('A.id','DESC');
$this->db->where('A.is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
}
}
public function get_count($emp_id, $business_id, $type_id)
{
$this->db->select('*');
$this->db->from('emp_leave_days');
$this->db->where('business_id', $business_id);
$this->db->where('emp_id', $emp_id);
$this->db->where('type', $type_id);
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function remaining_days($emp_id, $business_id, $leave_year, $type)
{
$this->db->select('*');
$this->db->from('emp_leave_days');
$this->db->where('emp_id', $emp_id);
$this->db->where('business_id', $business_id);
$this->db->where('year', $leave_year);
$this->db->where('type', $type);
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->result();
// print_r($this->db->last_query());
return $query;
}
// public function remaining_days_2($emp_id, $business_id, $next_year, $type)
// {
// $this->db->select('*');
// $this->db->from('emp_leave_days');
// $this->db->where('emp_id', $emp_id);
// $this->db->where('business_id', $business_id);
// $this->db->where('year', $next_year);
// $this->db->where('type', $type);
// $this->db->where('is_active', 1);
// $query = $this->db->get();
// $query = $query->row();
// return $query;
// }
function get_id($id,$business_id,$table)
{
$this->db->select('*');
$this->db->from($table);
$this->db->where('id', $id);
$this->db->where('business_id', $business_id);
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
function get_by_type($type,$business_id,$table)
{
$this->db->select();
$this->db->from($table);
$this->db->where('id', $type);
$this->db->where('business_id', $business_id);
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_employee($business_id)
{
$this->db->select('users.name, employees.user_id, employees.manager');
$this->db->from('users');
$this->db->join('employees', 'employees.user_id = users.id');
$this->db->where('users.business_id', $business_id);
$this->db->where('users.is_active', 1);
$this->db->where('employees.is_active', 1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return;
}
}
public function get_leave($id , $business_id , $table, $leave_year, $type)
{
$this->db->select('*');
$this->db->from($table);
$this->db->where('business_id', $business_id);
$this->db->where('emp_id', $id);
$this->db->where('type', $type);
$this->db->where('year', $leave_year);
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_leave_based_on_tpye($id , $type , $business_id , $table, $year)
{
$this->db->select('*');
$this->db->from($table);
$this->db->where('business_id', $business_id);
$this->db->where('emp_id', $id);
$this->db->where('type', $type);
$this->db->where('year', $year);
$this->db->order_by('id','DESC');
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_leave_by_md5_id($id,$business_id,$table)
{
$this->db->select('*');
$this->db->from($table);
$this->db->where('business_id', $business_id);
$this->db->where('md5(id)', $id);
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->row();
return $query;
}
function update_leave($action,$id,$table,$current_year,$type_id){
$this->db->where('emp_id',$id);
$this->db->where('type', $type_id);
$this->db->where('year', $current_year);
$this->db->update($table,$action);
return;
}
public function LeaveApprove($id, $data)
{
$this->db->where('id',$id);
$this->db->update('leave_list', $data);
return true;
}
public function LeavewithdrawDecline($id, $data)
{
$this->db->where('id',$id);
// $this->db->update('emp_leave_days', $data);
$this->db->update('leave_list', $data);
return true;
}
public function LeavewithdrawApproved($id, $data)
{
$this->db->where('id',$id);
$this->db->update('leave_list', $data);
return true;
}
public function LeaveDecline($id, $data)
{
$this->db->where('id',$id);
$this->db->where('status', 'pending');
$this->db->update('leave_list', $data);
return true;
}
public function Leavecancle($id, $data)
{
$this->db->where('md5(id)',$id);
$this->db->where('status', 'pending');
$this->db->update('leave_list', $data);
return;
}
public function Leavecanceaprove($id, $data)
{
$this->db->where('md5(id)',$id);
$this->db->where('status', 'Approved');
$this->db->update('leave_list', $data);
return;
}
public function getLeaveType($business_id)
{
$this->db->select('*');
$this->db->from('leave_master');
$this->db->where('business_id', $business_id);
$this->db->where('is_active', 1);
return $this->db->get()->result();
}
public function get_Leave_nodays($id)
{
$this->db->select('*');
$this->db->from('leave_list');
$this->db->where('id',$id);
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->row();
return $query;
}
public function emp_leave_days_update($id, $days, $current_year, $business_id, $type)
{
$this->db->where('emp_id',$id);
$this->db->where('year', $current_year);
$this->db->where('business_id', $business_id);
$this->db->where('type', $type);
$this->db->update('emp_leave_days', $days);
}
// public function emp_leave_days_update_2($id, $days, $next_year, $business_id, $type)
// {
// $this->db->where('emp_id',$id);
// $this->db->where('year', $next_year);
// $this->db->where('business_id', $business_id);
// $this->db->where('type', $type);
// $this->db->update('emp_leave_days', $days);
// }
public function get_emp_id($id)
{
$this->db->select('emp_id');
$this->db->where('id', $id); // Assuming $leaveListId is the ID to match
$query = $this->db->get('leave_list');
if ($query->num_rows() > 0) {
$row = $query->row();
$empId = $row->emp_id;
return $empId;
}
}
public function under_emp_manager($user_id, $business_id)
{
$this->db->select('manager');
$this->db->from('employees');
$this->db->where('business_id', $business_id);
$query = $this->db->get();
return $query->result();
}
public function editleave($id)
{
$this->db->select('*');
$this->db->from('leave_list');
$this->db->where('md5(id)', $id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return;
}
}
public function decline_dropdown($business_id)
{
$this->db->select('*');
$this->db->from('enum');
$this->db->where('enum_type', 'decline');
$this->db->where('business_id', $business_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return;
}
}
public function Get_delegation_id($business_id)
{
$this->db->select('*');
$this->db->from('delegation');
$this->db->where('business_id', $business_id);
$this->db->where('is_active', 1);
$query = $this->db->get();
return $query->result();
}
public function get_emp_leave_count($type_id, $emp_id)
{
$this->db->select('remaining_days');
$this->db->from('emp_leave_days');
$this->db->where('emp_id', $emp_id);
$this->db->where('type', $type_id);
$this->db->where('is_active', 1);
$query = $this->db->get();
return $query->result();
}
public function get_leave_master($business_id)
{
$this->db->select('*');
$this->db->from('leave_master');
$this->db->where('(business_id = 0 OR business_id = ' . $business_id . ')');
$this->db->where('is_active', 1);
$query = $this->db->get();
$result = $query->result();
return $result;
}
public function get_leave_master_count($type_id, $business_id)
{
$this->db->select('*');
$this->db->from('leave_master');
$this->db->where('business_id', $business_id);
$this->db->where('id', $type_id);
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_emp_joing_year_month($emp_id, $business_id)
{
$this->db->select('A.*, name', 'users.name');
$this->db->from('employees A');
$this->db->join('users', 'users.id = A.user_id', 'left');
$this->db->where('A.business_id', $business_id);
$this->db->where('A.user_id', $emp_id);
$this->db->where('A.is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_leave_type($business_id)
{
$this->db->select('*');
$this->db->from('leave_master');
$this->db->where('business_id', $business_id);
$this->db->where('is_active', 1);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_from_date($emp_id, $from_date, $to_date)
{
$this->db->select('*');
$this->db->from('leave_list');
$this->db->where('emp_id', $emp_id);
$this->db->where('from_date_new >=', $from_date);
$this->db->where('to_date_new <=', $to_date);
$query = $this->db->count_all_results();
return $query;
}
public function get_emp_join_date($emp_id, $from_date, $to_date)
{
$this->db->select('*');
$this->db->from('employees');
$this->db->where('user_id', $emp_id);
$this->db->where('j_date >', $from_date);
$query = $this->db->count_all_results();
return $query;
}
public function get_emp_data_by_emp_id($emp_id){
$this->db->select('*');
$this->db->from('employees');
$this->db->where('user_id', $emp_id);
$query = $this->db->get();
$query = $query->result();
return $query;
}
public function get_approved_leaves_cur_week()
{
$currentWeekStart = date('Y-m-d', strtotime('monday this week'));
$currentWeekEnd = date('Y-m-d', strtotime('sunday this week'));
$this->db->select('leave_list.*, users.name as emp_name');
$this->db->from('leave_list');
$this->db->join('users', 'users.id = leave_list.emp_id', 'left');
$this->db->group_start();
$this->db->where("STR_TO_DATE(from_date_new, '%d-%m-%Y') <= '$currentWeekEnd'");
$this->db->where("STR_TO_DATE(to_date_new, '%d-%m-%Y') >= '$currentWeekStart'");
$this->db->or_where("STR_TO_DATE(perm_date, '%d-%m-%Y') <= '$currentWeekEnd'");
$this->db->or_where("STR_TO_DATE(perm_date, '%d-%m-%Y') >= '$currentWeekStart'");
$this->db->group_end();
$this->db->where('leave_list.status', 'approved');
$query = $this->db->get();
$approvedLeaveRecords = $query->result();
// Organize approved leave records into columns based on days of the week
$weekData = [];
foreach ($approvedLeaveRecords as $leave) {
$leaveStartDate = ($leave->from_date_new) ? date('Y-m-d', strtotime($leave->from_date_new)) : date('Y-m-d', strtotime($leave->perm_date));
$leaveEndDate = ($leave->to_date_new) ? date('Y-m-d', strtotime($leave->to_date_new)) : date('Y-m-d', strtotime($leave->perm_date));
// Use from_time and to_time if available
$fromTime = ($leave->from_time) ? ' ' . $leave->from_time : '';
$toTime = ($leave->to_time) ? ' ' . $leave->to_time : '';
// Iterate over each day in the leave period
$currentDate = $leaveStartDate;
while ($currentDate <= $leaveEndDate) {
$dayOfWeek = date('l', strtotime($currentDate));
$weekData[$dayOfWeek][] = [
'emp_id' => $leave->emp_id,
'emp_name' => $leave->emp_name,
'from_time' => $fromTime,
'to_time' => $toTime,
];
$currentDate = date('Y-m-d', strtotime($currentDate . ' +1 day'));
}
}
// print_r($weekData);
return $weekData;
}
public function get_week_leave_count($business_id)
{
$startOfWeek = date('Y-m-d', strtotime('monday this week'));
$endOfWeek = date('Y-m-d', strtotime('sunday this week'));
// Query to get leave records for the specified employee within the current week
$this->db->select('A.*, U1.name as employee_name');
$this->db->from('leave_list A');
$this->db->where('A.business_id', $business_id);
$this->db->where("(A.from_date_new BETWEEN '$startOfWeek' AND '$endOfWeek' OR A.to_date_new BETWEEN '$startOfWeek' AND '$endOfWeek' OR (A.from_date_new < '$startOfWeek' AND A.to_date_new > '$endOfWeek'))");
$this->db->join('users U1', 'U1.id = A.emp_id', 'left');
$query = $this->db->get();
$leaveRecords = $query->result();
// echo json_encode($leaveRecords);die;
// Initialize an array to store leave counts for each day of the week
$weeklyLeaveCounts = array(
'sunday' => [],
'monday' => [],
'tuesday' => [],
'wednesday' => [],
'thursday' => [],
'friday' => [],
'saturday' => []
);
// Iterate over the leave records to count leaves for each day of the week
foreach ($leaveRecords as $key => $record) {
if($record->from_date_new < $startOfWeek)
{
$record->from_date_new = $startOfWeek;
}
if($record->to_date_new > $endOfWeek)
{
$record->to_date_new = $endOfWeek;
}
$startDate = date('l', strtotime($record->from_date_new));
$endDate = date('l', strtotime($record->to_date_new));
// Increment leave counts for each day within the leave period
while ($startDate !== $endDate) {
array_push($weeklyLeaveCounts[strtolower($startDate)], $record->employee_name);
$startDate = date('l', strtotime($startDate . ' +1 day'));
}
array_push($weeklyLeaveCounts[strtolower($startDate)], $record->employee_name); // Increment for the last day
}
// Return the array with leave counts for each day of the week
return $weeklyLeaveCounts;
}
}