siddharth_application/application/controllers/user.php
2017-12-15 12:20:46 +05:30

786 lines
20 KiB
PHP
Executable File

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/libraries/BaseController.php';
/**
* Class : User (UserController)
* User Class to control all user related operations.
* @author : Kishor Mali
* @version : 1.1
* @since : 15 November 2016
*/
class user extends BaseController
{
/**
* This is default constructor of the class
*/
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
$this->load->model('dahsboard_Model');
$this->load->library('form_validation');
$this->isLoggedIn();
}
/**
* This function used to load the first screen of the user
*/
public function index()
{
$this->global['pageTitle'] = 'Siddharth : Dashboard';
$this->load->model('employeedetails_model');
$this->load->model('dahsboard_Model');
$data['EmpCount'] = $this->employeedetails_model->getEmployeeCount();
$data['totalpurchaseorder']=$this->dahsboard_Model->totalpurchaseorder();
//print_r( $data['totalpurchaseorder']);
$data['totalordervalue']=$this->dahsboard_Model->totalordervalue();
$data['pendingpo']=$this->dahsboard_Model->pendingpo();
// $data['totalserviceamount']=$this->dahsboard_Model->totalserviceamount();
// $data['totalcapitalamount']=$this->dahsboard_Model->totalcapitalamount();
// $data['totalimportamount']=$this->dahsboard_Model->totalimportamount();
// $data['totalrevenueamount']=$this->dahsboard_Model->totalrevenueamount();
// $data['totalservicepo']=$this->dahsboard_Model->totalservicepo();
// $data['totalrevenuepo']=$this->dahsboard_Model->totalrevenuepo();
$data['totalimportpo']=$this->dahsboard_Model->totalimportpo();
//$data['totalcapitalpo']=$this->dahsboard_Model->totalcapitalpo();
$data['details']=$this->dahsboard_Model->reqdetail();
$data['pending_details']=$this->dahsboard_Model->req_pending();
$data['list']=$this->dahsboard_Model->req_list();
$data['serviceprogress']=$this->dahsboard_Model->serviceprogress();
$data['revenueprogress']=$this->dahsboard_Model->revenueprogress();
$data['importprogress']=$this->dahsboard_Model->importprogress();
$data['capitalprogress']=$this->dahsboard_Model->capitalprogress();
//month-wise area chart
$data['getTotalServicePoCount'] = $this->dahsboard_Model->getTotalServicePoCount();
$data['getTotalimportPoCount'] = $this->dahsboard_Model->getTotalimportPoCount();
$data['getTotalcapitalPoCount'] = $this->dahsboard_Model->getTotalcapitalPoCount();
$data['getTotalrevenuePoCount'] = $this->dahsboard_Model->getTotalrevenuePoCount();
//dashboard pie chart
$data['importbudgt'] = $this->dahsboard_Model->importbudgt();
$data['importbal'] = $this->dahsboard_Model->importbal();
$data['servicebudgt'] = $this->dahsboard_Model->servicebudgt();
$data['servicebal'] = $this->dahsboard_Model->servicebal();
$data['revenuebudgt'] = $this->dahsboard_Model->revenuebudgt();
$data['revenuebal'] = $this->dahsboard_Model->revenuebal();
$data['capitalbud'] = $this->dahsboard_Model->capitalbud();
$data['capitalbal'] = $this->dahsboard_Model->capitalbal();
$data['totemp']=$this->dahsboard_Model->totemp();
$data['totloan']=$this->dahsboard_Model->totloan();
$data['totpay']=$this->dahsboard_Model->totpay();
$data['totrec']=$this->dahsboard_Model->totrec();
$data['emplist']=$this->dahsboard_Model->emplist();
/* yesterday absent list count in employee*/
$dates = date('Y-m');
$date = $dates.'-01';
$dat = date('d',strtotime("-1 days"));
//echo $date;
$WH = 'WH'.date('j',strtotime("-1 days"));
//echo($WH);die;
$yesterday = $this->dahsboard_Model->attyesterday($WH,$date);
//print_r($yesterday);die;
$values=0;
foreach ($yesterday as $weekda => $value) {
if($value[$WH] == 0){
$values++;
}
}
$data['values'] = $values;
/*Today absent list count in employee*/
$dates = date('Y-m');
//echo $date;
$date = $dates.'-01';
$dat = date('d');
//echo $date;
$WH = 'WH'.date('j');
//echo $WH;die;
$yesterday = $this->dahsboard_Model->attyesterday($WH,$date);
// print_r($yesterday);die;
$values=0;
foreach ($yesterday as $weekda => $value) {
if($value[$WH] == 0){
$values++;
}
}
$data['today'] = $values;
//print_r($data['today']);die;
$this->loadViews("dashboard", $this->global, $data , NULL);
}
function attendancemonth()
{
// $EmpID='';
$date = date('Y-m');
//echo $date;
$dates = $date.'-01';
$EmpID= $this->input->post('id');
//echo $dates;
//echo $weekabs;die;
$monthabs = $this->dahsboard_Model->attendance($EmpID,$dates);
//print_r($data['monthabs']);
//$todate = $this->input->get('todate');
$persent_month = date('t');
//$current_month_day = cal_days_in_month(CAL_GREGORIAN,$persent_month,$year);
//$current_month_day;die;
$values=0;
//die();
$weekdays = array();
$date = date('d');
//echo $date;
for($i=1;$i<=$persent_month;$i++)
{
//if($date > 0 ){
$weekdays[] = 'WH'.$i;
//print_r($weekdays);
}
//print_r($weekdays);die;
foreach ($monthabs as $weekda ) {
//print_r($weekda);
foreach ($weekda as $key => $value) {
//print_r($key);die;
//print_r($value);
foreach ($weekdays as $weeks) {
//print_r($weeks);
//print_r($key);
if($weeks == $key)
{
if($value != 0){
// echo $value;
$values++;
// echo $values;
}
}
}
}
}
//echo $values;die;
//print_r($week);
$year = date('Y');
$current_month = date('m');
//echo $current_month;die;
//$current_month_days = cal_days_in_month(CAL_GREGORIAN,$current_month,$year);
//echo $current_month_days;die;
$CUR_month = date('t');
//echo $current_month;die;
// $total_days_percentage = ($values/$date);
//print_r($total_days_percentage);die;
$data['values'] = $values;
$data['present_percentage'] = round(($values/$date)*100);
$data['CUR_month'] = $date;
echo json_encode($data);
}
public function getFinancialStartDate() {
$CURMONTH = date('n');
$CURYEAR = date('Y');
$FINYEAR = 0;
if($CURMONTH >= 4 ) {
$FINYEAR = $CURYEAR;
}else {
$FINYEAR = $CURYEAR-1;
}
$dateInString = $FINYEAR.'-4-1';
$result = date_create($dateInString);
return $result;
}
public function getFinancialEndDate() {
$CURMONTH = date('n');
$CURYEAR = date('Y');
$FINYEAR = 0;
if($CURMONTH < 4 ) {
$FINYEAR = $CURYEAR;
}else {
$FINYEAR = $CURYEAR+1;
}
$dateInString = $FINYEAR.'-3-31';
$result = date_create($dateInString);
return $result;
}
function attendanceyear()
{
$CURMONTH = date('m');
$CURDATE = date('d');
//$dates = $start_date.'-04-01';
$EmpID= $this->input->post('id');
$day = array();
$datejoin=$this->dahsboard_Model->joindate($EmpID);
$DateofJoiningInString = $datejoin[0]->DateofJoining;
$DateofJoining = date_create($DateofJoiningInString);
$finStart = $this->getFinancialStartDate();
$finEnd = $this->getFinancialEndDate();
$JoinedMonth = date_format($DateofJoining,"n");
if($DateofJoining >= $finStart && $DateofJoining <= $finEnd) {
$JoinedMonthAttendance = $this->dahsboard_Model->getAllAttendance($EmpID,$JoinedMonth);
$doj_month =date_format($DateofJoining,"n");
$doj_year= date_format($DateofJoining,"Y");
$doj_day= date_format($DateofJoining,"j");
$joinedMonthDaysCount = cal_days_in_month(CAL_GREGORIAN, $doj_month, $doj_year);
$joinedMonthWorkingDayCount = $joinedMonthDaysCount - $doj_day;
$joinedMonthDays =0;
for($j=$doj_day;$j<=$joinedMonthDaysCount;$j++)
{
$weekdays[] = 'WH'.$j;
}
foreach ($JoinedMonthAttendance as $day ) {
//print_r($weekda);
foreach ($day as $key => $value) {
foreach ($weekdays as $weeks) {
if($weeks == $key)
{
if($value!= 0){
$joinedMonthDays++;
}
}
}
}
}
$result = $this->dahsboard_Model->attendanceyear($EmpID, $JoinedMonth + 1);
$yearabs = $this->dahsboard_Model->getAllAttendance($EmpID,$CURMONTH);
//print_r($yearabs);exit();
$TotalNoofDays = $result[0]->TotalWorkingDays;
$DaysWorked = $result[0]->DaysWorked;
$values=0;
$weekdays = array();
$date = date('d');
$Work= array_sum($weekdays);
for($j=1;$j<=$date;$j++)
{
$weekdays[] = 'WH'.$j;
}
foreach ($yearabs as $day ) {
//print_r($weekda);
foreach ($day as $key => $value) {
foreach ($weekdays as $weeks) {
if($weeks == $key)
{
if($value != 0){
$values++;
}
}
}
}
}
$TotalNoofDays = $TotalNoofDays+$CURDATE + $joinedMonthWorkingDayCount ;
$DaysWorked = $DaysWorked+$values+$joinedMonthDays;
//echo $TotalNoofDays.'-'.$DaysWorked;
$final_res = round(($DaysWorked /$TotalNoofDays) * 100);
// echo "------";
//echo $final_res;exit();
$data['workeds']=$DaysWorked;
$data['yeardata'] = $final_res;
$data['totaldays'] = $TotalNoofDays;
// if(!empty($working)){
// $data['total_working_days'] = round($worked/$dayswork);
// }else{
// $data['total_working_days'] = 0;
// }
echo json_encode($data);
}else {
$result = $this->dahsboard_Model->attendanceyear($EmpID, 4);
$yearabs = $this->dahsboard_Model->getAllAttendance($EmpID,$CURMONTH);
//print_r($yearabs);exit();
$TotalNoofDays = $result[0]->TotalWorkingDays;
$DaysWorked = $result[0]->DaysWorked;
$values=0;
$weekdays = array();
$date = date('d');
$Work= array_sum($weekdays);
for($j=1;$j<=$date;$j++)
{
$weekdays[] = 'WH'.$j;
}
foreach ($yearabs as $day ) {
//print_r($weekda);
foreach ($day as $key => $value) {
foreach ($weekdays as $weeks) {
if($weeks == $key)
{
if($value != 0){
$values++;
}
}
}
}
}
$TotalNoofDays = $TotalNoofDays+$CURDATE;
$DaysWorked = $DaysWorked+$values;
//echo $TotalNoofDays.'-'.$DaysWorked;
$final_res = round(($DaysWorked /$TotalNoofDays) * 100);
// echo "------";
//echo $final_res;exit();
$data['workeds']=$DaysWorked;
$data['yeardata'] = $final_res;
$data['totaldays'] = $TotalNoofDays;
// if(!empty($working)){
// $data['total_working_days'] = round($worked/$dayswork);
// }else{
// $data['total_working_days'] = 0;
// }
echo json_encode($data);
}
}
/**
* This function is used to load the user list
*/
function userListing()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$data['userRecords'] = $this->user_model->userListing();
$this->global['pageTitle'] = 'Siddharth : User Listing';
$this->loadViews("users", $this->global, $data, NULL);
}
}
/**
* This function is used to load the add new form
*/
function addNew()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$data['EmpList'] = $this->user_model->getAllEmployees();
$data['roles'] = $this->user_model->getUserRoles();
$this->global['pageTitle'] = 'Siddharth : Add New User';
$this->loadViews("addNew", $this->global, $data, NULL);
}
}
/**
* This function is used to check whether email already exist or not
*/
function checkEmailExists()
{
$userId = $this->input->post("userId");
$email = $this->input->post("email");
if(empty($userId)){
$result = $this->user_model->checkEmailExists($email);
} else {
$result = $this->user_model->checkEmailExists($email, $userId);
}
if(empty($result)){ echo("true"); }
else { echo("false"); }
}
/* Validation for Employee Dropdown
*/
function Employee_validate($selectValue)
{
//echo 'select_validate method called';
// 'none' is the first option and the text says something like "-Choose one-"
if($selectValue == '0')
{
$this->form_validation->set_message('Employee_validate', 'Please Select Employee.');
return false;
}
else // user picked something
{
return true;
}
}
/* Validation for Employee Dropdown
*/
function Role_validate($selectValue)
{
//echo 'select_validate method called';
// 'none' is the first option and the text says something like "-Choose one-"
if($selectValue == '0')
{
$this->form_validation->set_message('Role_validate', 'Please Select Role.');
return false;
}
else // user picked something
{
return true;
}
}
/**
* This function is used to add new user to the system
*/
function addNewUser()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$this->form_validation->set_rules('EmpList','EmpList','trim|callback_Employee_validateS');
$this->form_validation->set_rules('role','role','trim|callback_Role_validate');
$this->form_validation->set_rules('password','Password','required|max_length[20]');
$this->form_validation->set_rules('cpassword','Confirm Password','trim|required|matches[password]|max_length[20]');
if($this->form_validation->run() == FALSE)
{
$this->addNew();
}
else
{
$EmpID = $this->input->post('EmpList');
$roleId = $this->input->post('role');
$password = $this->input->post('password');
$dt = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
$Createddt = $dt->format('Y-m-d H:i:s');
$email = $this->input->post('MailID');
$userInfo = array('email'=>$email,'EmpID'=>$EmpID,'password'=>getHashedPassword($password),'roleId'=>$roleId,'createdBy'=>$this->vendorId,'createdDtm'=>$Createddt);
$result = $this->user_model->addNewUser($userInfo);
if($result > 0)
{
redirect('userListing','refresh');
echo "<script>alert('New User Created successfully!');</script>";
}
else
{
redirect('userListing','refresh');
echo "<script>alert('Failiure User Creation!');</script>";
}
}
}
}
/**
* This function is used load user edit information
* @param number $userId : Optional : This is user id
*/
function editOld($UserId = '')
{
if($this->isAdmin() == TRUE )
{
$this->loadThis();
}
else
{
if($UserId == '')
{
// redirect('userListing');
}
$Uid = $_GET['UID'];
$data['roles'] = $this->user_model->getUserRoles();
//print_r($data);
$data['EmpList'] = $this->user_model->GetEmployeeDetails($Uid);
//print_r($data);
$data['EmpRole'] = $this->user_model->GetRoleNameByUserID($Uid);
// $data['userInfo'] = $this->user_model->getUserInfo($userId);
$this->global['pageTitle'] = 'Siddharth : Edit User';
$this->loadViews("editOld", $this->global, $data, NULL);
}
}
/**
* This function is used to edit the user information
*/
function editUser()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$userId = $this->input->post('EmpList');
$this->form_validation->set_rules('EmpList','EmpList','trim|callback_Employee_validateS');
$this->form_validation->set_rules('role','role','trim|callback_Role_validate');
$this->form_validation->set_rules('password','Password','required|max_length[20]');
$this->form_validation->set_rules('cpassword','Confirm Password','trim|required|matches[password]|max_length[20]');
if($this->form_validation->run() == FALSE)
{
$this->editOld($userId);
}
else
{
$EmpID = $this->input->post('EmpList');
$role = $this->input->post('role');
$password = $this->input->post('password');
$dt = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
$updatedDate = $dt->format('Y-m-d H:i:s');
// echo $EmpID;
$userInfo = array();
$userInfo = array('password'=>getHashedPassword($password),'roleId'=>$role,'EmpId'=>$EmpID,'updatedDtm'=>$updatedDate);
$result = $this->user_model->editUser($userInfo, $EmpID);
if($result == true)
{
$this->session->set_flashdata('success', 'User updated successfully');
}
else
{
$this->session->set_flashdata('error', 'User updation failed');
}
redirect('userListing');
}
}
}
/**
* This function is used to delete the user using userId
* @return boolean $result : TRUE / FALSE
*/
function deleteUser()
{
echo 'Delete';
$Uid = $this->input->post('id');
$dt = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
$updatedDate = $dt->format('Y-m-d H:i:s');
$userInfo = array('isDeleted'=>1,'updatedBy'=>$this->vendorId, 'updatedDtm'=>$updatedDate);
$result = $this->user_model->deleteUser($Uid, $userInfo);
echo 'Succssfully change the user status to InAcive';
}
/**
* This function is used to load the change password screen
*/
function loadChangePass()
{
$this->global['pageTitle'] = 'Siddharth : Change Password';
$this->loadViews("changePassword", $this->global, NULL, NULL);
}
/**
* This function is used to change the password of the user
*/
function changePassword()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('oldPassword','Old password','required|max_length[20]');
$this->form_validation->set_rules('newPassword','New password','required|max_length[20]');
$this->form_validation->set_rules('cNewPassword','Confirm new password','required|matches[newPassword]|max_length[20]');
if($this->form_validation->run() == FALSE)
{
$this->loadChangePass();
}
else
{
$oldPassword = $this->input->post('oldPassword');
$newPassword = $this->input->post('newPassword');
$resultPas = $this->user_model->matchOldPassword($this->vendorId, $oldPassword);
if(empty($resultPas))
{
$this->session->set_flashdata('nomatch', 'Your old password not correct');
redirect('loadChangePass');
}
else
{
$usersData = array('password'=>getHashedPassword($newPassword), 'updatedBy'=>$this->vendorId,
'updatedDtm'=>date('Y-m-d H:i:sa'));
$result = $this->user_model->changePassword($this->vendorId, $usersData);
if($result > 0) { $this->session->set_flashdata('success', 'Password updation successful'); }
else { $this->session->set_flashdata('error', 'Password updation failed'); }
redirect('loadChangePass');
}
}
}
function pageNotFound()
{
$this->global['pageTitle'] = 'Siddharth : 404 - Page Not Found';
$this->loadViews("404", $this->global, NULL, NULL);
}
}
?>