bbone/application/modules/Leave/controllers/Leave.php
2023-06-26 12:37:58 +05:30

397 lines
14 KiB
PHP

<?php defined('BASEPATH') or exit('No direct script access allowed');
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
date_default_timezone_set('Asia/Kolkata');
/**
* AMS
*
* @package HMVC
* @author Venba
* @copyright 2022 Venba
* @license https://venbainfotech.com/licenses/MIT MIT License
* @link <URI> (description)
* @version GIT: $Id$
* @since Version 0.0.1
* @filesource
*
*/
class Leave extends Admin_Controller
{
/**
* [__construct description]
*
* @method __construct
*/
public function __construct()
{
// To inherit directly the attributes of the parent class.
parent::__construct();
### Syntax : $this->load->model('Model Path' , 'Alias name');
### The second parameter (optional) is used to call the method.
$this->load->model('MY_Model','MY_Model');
$this->load->model('Leave_model','Leave_model');
$this->load->model('Employee/Employee_model','Emp_model');
$this->load->model('Audit/Audit_model','audit_model');
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->layout->set('users', "gowtham");
$this->layout->buffer('main_content', 'Asset/index');
$this->layout->render('Employee_Layout');
}
public function leaveList()
{
$business_id = user()->business_id;
$leave_list = $this->Leave_model->get_leave_with_emp_name(user()->business_id);
$decline_dropdown = $this->Leave_model->decline_dropdown($business_id);
$this->layout->set('tableData', $leave_list);
$this->layout->set('dropdown', $decline_dropdown);
$this->layout->buffer('main_content', 'Leave/leave_list');
$this->layout->render('Employee_Layout');
}
public function applyLeave()
{
$business_id = user()->business_id;
$user_id = user()->id;
$emp_leave_count = $this->Leave_model->get_count($user_id, $business_id, "emp_leave_days");
$emp = $this->Emp_model->get_employee(user()->business_id);
$leavetype = $this->Leave_model->getLeaveType($business_id);
//print_r($emp_leave_count); die();
$this->layout->set('emp', $emp);
$this->layout->set('count', $emp_leave_count);
$this->layout->set('leavetype', $leavetype);
$this->layout->buffer('main_content', 'Leave/apply_leave');
$this->layout->render('Employee_Layout');
}
public function get_emp_name()
{
$emp = $this->Leave_model->get_employee(user()->business_id);
$html = '<option>--select Employee--</option>';
foreach($emp as $val)
{
if($val->user_id != user()->id)
{
$html .='<option id="'.$val->manager.'" value="'.$val->user_id.'">'.$val->name.'</option>';
}
}
echo json_encode($html);
}
public function createLeave()
{
if($this->input->post('type') === 'Permission')
{
$perm_data = $this->input->post();
if(isset($perm_data['emp_id'])){
$emp_id =$this->input->post('emp_id');
}else{
$emp_id = user()->id;
}
$perm_data['emp_id'] = $emp_id;
if($data['manager']){
$manager = $this->input->post('manager');
}else{
$manager = employee()->manager;
}
$perm_data['manager'] = $manager;
$perm_data['business_id'] = user()->business_id;
$perm_data['status'] = 'pending';
$perm_data['created_date'] = date('d-m-Y');
$perm_data['created_time'] = date('H:i A');
$table="leave_list";
$create_permission = $this->MY_Model->insert($perm_data,$table);
$this->session->set_flashdata('Success', 'Submitted for Approval');
redirect('Leave/LeaveHistory');
}
$data = $this->input->post();
if(isset($data['emp_id'])){
$emp_id =$this->input->post('emp_id');
}else{
$emp_id = user()->id;
}
$data['emp_id'] = $emp_id;
if(!is_null($data['manager'])){
$manager = $this->input->post('manager');
}else{
$manager = employee()->manager;
}
$data['manager'] = $manager;
$data['business_id'] = user()->business_id;
$data['status'] = 'pending';
$data['created_date'] = date('d-m-Y');
$data['created_time'] = date('H:i A');
$emp_leave_count = $this->Leave_model->get_leave( $emp_id , user()->business_id , "emp_leave_days");
// count number of days between two dates
$days_between = ceil(abs($this->input->post('to_date') - $this->input->post('from_date')) / 86400);
$data['no_of_days'] = $days_between + 1;
$noofdays = $days_between + 1;
if (count($emp_leave_count) != 0)
{
$data['leave_bal'] = $emp_leave_count[0]->remaining_days;
$days['remaining_days'] = $emp_leave_count[0]->remaining_days;
$this->Leave_model->update($days , $emp_id , 'emp_leave_days');
}
else
{
$all_leave_count = $this->Leave_model->get_by_type( $this->input->post('type') , user()->business_id , "leave_master");
$data['leave_bal'] = $all_leave_count[0]->days;
$days['remaining_days'] = $all_leave_count[0]->days;
$days['business_id'] = user()->business_id;
$days['emp_id'] = $emp_id;
$days['total_days'] = $all_leave_count[0]->days;
$days['type'] = $all_leave_count[0]->id;
$this->MY_Model->insert($days,'emp_leave_days');
}
$table="leave_list";
$create_leave = $this->MY_Model->insert($data,$table);
//audit
if($create_leave)
{
//set id $ table name for audit history
$this->audit->set('id',$create_leave);
$this->audit->set('table',$table);
//fetch new_data after post data insert
$this->audit->fetch('new_data');
//audit history generation for insert
$test = $this->audit->create_history('insert');
}
$this->session->set_flashdata('Success', 'Submitted for Approval');
redirect('Leave/LeaveHistory');
}
public function edit_leave_function()
{
$id = $this->input->get('id');
$emp_leave_count = $this->Leave_model->get("emp_leave_days");
$emp = $this->Emp_model->get_employee(user()->business_id);
$business_id = user()->business_id;
$leavetype = $this->Leave_model->getLeaveType($business_id);
$editleave = $this->Leave_model->editleave($id);
$this->layout->set('emp', $emp);
$this->layout->set('editleave', $editleave);
$this->layout->set('count', $emp_leave_count);
$this->layout->set('leavetype', $leavetype);
$this->layout->buffer('main_content', 'Leave/edit_leave');
$this->layout->render('Employee_Layout');
}
public function editleave()
{
if($this->input->post('type') === 'Permission')
{
$action = $this->input->post();
unset($action['from_date']);
unset($action['to_date']);
$id = $this->input->post('id');
if(isset($action['emp_id'])){
$emp_id =$this->input->post('emp_id');
}else{
$emp_id = user()->id;
}
$action['emp_id'] = $emp_id;
if($action['manager']){
$manager = $this->input->post('manager');
}else{
$manager = employee()->manager;
}
$action['manager'] = $manager;
$action['business_id'] = user()->business_id;
$action['status'] = 'pending';
$action['created_date'] = date('d-m-Y');
$action['created_time'] = date('H:i A');
$table="leave_list";
$create_permission = $this->MY_Model->edit_option($action, $id, $table);
$this->session->set_flashdata('Success', 'Submitted for Approval');
redirect('Leave/LeaveHistory');
}
$action = $this->input->post();
$id = $this->input->post('id');
if(isset($action['emp_id'])){
$emp_id =$this->input->post('emp_id');
}else{
$emp_id = user()->id;
}
$action['emp_id'] = $emp_id;
$action['business_id'] = user()->business_id;
$action['status'] = 'pending';
$emp_leave_count = $this->Leave_model->get_leave( $emp_id , user()->business_id , "emp_leave_days");
// count number of days between two dates
$days_between = ceil(abs(strtotime($this->input->post('to_date')) - strtotime($this->input->post('from_date'))) / 86400);
$action['no_of_days'] = $days_between + 1;
$noofdays = $days_between + 1;
if (count($emp_leave_count) != 0)
{
$action['leave_bal'] = $emp_leave_count[0]->remaining_days;
$days['remaining_days'] = $emp_leave_count[0]->remaining_days;
$this->Leave_model->update($days , $emp_id , 'emp_leave_days');
}
else
{
$all_leave_count = $this->Leave_model->get_by_type( $this->input->post('type') , user()->business_id , "leave_master");
$action['leave_bal'] = $all_leave_count[0]->days;
$days['remaining_days'] = $all_leave_count[0]->days;
$days['business_id'] = user()->business_id;
$days['emp_id'] = $emp_id;
$days['total_days'] = $all_leave_count[0]->days;
$days['type'] = $all_leave_count[0]->id;
$this->MY_Model->update($days,'emp_leave_days');
}
$table="leave_list";
$update_leave = $this->MY_Model->edit_option($action,$id,$table);
$this->session->set_flashdata('Success', 'Leave Updated Successfully');
redirect('Leave/LeaveHistory');
}
public function get_leave_count($id)
{
$str = $id;
$res = preg_split('~[_]+~', $str, 0, PREG_SPLIT_NO_EMPTY);
$emp_leave_count = $this->Leave_model->get_leave_based_on_tpye($res[0] , $res[1] , user()->business_id , "emp_leave_days");
echo json_encode($emp_leave_count);
}
public function get_all_leave_count($id)
{
$all_leave_count = $this->Leave_model->get_id( $id , user()->business_id , "leave_master");
echo json_encode($all_leave_count);
}
public function ApproveLeave()
{
$id=$this->input->get('id');
$emp_id = $this->Leave_model->get_emp_id($id);
$no_of_days = $this->Leave_model->get_Leave_nodays($emp_id);
$remaining_days = $this->Leave_model->remaining_days($emp_id, user()->business_id);
$balance_days = $remaining_days[0]->remaining_days - $no_of_days->no_of_days;
$data['leave_bal'] = $balance_days;
$days['remaining_days'] = $balance_days;
$this->Leave_model->emp_leave_days_update($emp_id, $days);
$data['status'] = 'Approved';
$data['approved_by']= user()->name;
$data['approved_at'] = date('d-m-Y H:i:s');
$this->Leave_model->LeaveApprove($id, $data);
redirect('Leave/leaveList');
}
public function ManagerLeaveApprove()
{
$id=$this->input->get('id');
$emp_id = $this->Leave_model->get_emp_id($id);
$no_of_days = $this->Leave_model->get_Leave_nodays($emp_id);
$remaining_days = $this->Leave_model->remaining_days($emp_id, user()->business_id);
$balance_days = $remaining_days[0]->remaining_days - $no_of_days->no_of_days;
$data['leave_bal'] = $balance_days;
$days['remaining_days'] = $balance_days;
$this->Leave_model->emp_leave_days_update($emp_id, $days);
$data['status'] = 'Approved';
$data['approved_by']= user()->name;
$data['approved_at'] = date('d-m-Y H:i:s');
$this->Leave_model->LeaveApprove($id, $data);
redirect('Leave/manager_approvel');
}
public function Decline()
{
$id=$this->input->get('id');
$data['status'] = 'Declined';
$data['declined_by']= user()->name;
$data['declined_at'] = date('d-m-Y H:i:s');
$data['DReason'] = $this->input->get('reason');
$this->Leave_model->LeaveDecline($id, $data);
return true;
}
public function cancled()
{
$id=$this->input->get('id');
$data['status'] = 'canceled';
$data['cancled_at'] = date('d-m-Y H:i:s');
$this->Leave_model->Leavecancle($id, $data);
$this->session->set_flashdata('Success', 'Leave canceled Successfully');
redirect('Leave/LeaveHistory');
}
public function LeaveHistory()
{
$leave_list = $this->Leave_model->get_leave_with_emp_name(user()->business_id);
$this->layout->set('tableData', $leave_list);
$this->layout->buffer('main_content', 'Leave/leavehistory');
$this->layout->render('Employee_Layout');
}
public function manager_approvel()
{
$business_id = user()->business_id;
$user_id = user()->id;
$manager = $this->Leave_model->under_emp_manager($user_id, $business_id);
$get_delegation = $this->Leave_model->Get_delegation_id($business_id);
$decline_dropdown = $this->Leave_model->decline_dropdown($business_id);
$leave_list = $this->Leave_model->get_with_emp_name(user()->business_id);
$this->layout->set('tableData', $leave_list);
$this->layout->set('dropdown', $decline_dropdown);
$this->layout->set('get_delegation', $get_delegation);
$this->layout->set('manager', $manager);
$this->layout->buffer('main_content', 'Leave/manager_approvel');
$this->layout->render('Employee_Layout');
}
public function hide_leave_type()
{
$type_id = $this->input->get('type_id');
$emp_id = $this->input->get('emp_id');
$emp_leave_count = $this->Leave_model->get_emp_leave_count($type_id, $emp_id);
echo json_encode($emp_leave_count);
}
}