ria/app/Models/Monthlypay_model.php

707 lines
22 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
class Monthlypay_model extends Model
{
/**
* This function is used to get the user listing count
* @param string $searchText : This is optional search text
* @param number $page : This is pagination offset
* @param number $segment : This is pagination limit
* @return array $result : This is result
*/
function monthlyListing($current = "")
{
$month = $current;
if (strlen($month) == 6) {
$month = '0' . $month;
}
//echo $month;
$curr_split = explode('-', $current);
$month_val = $curr_split[0];
$year_val = $curr_split[1];
$date_val = cal_days_in_month(CAL_GREGORIAN, $month_val, $year_val);
$lastdate = $year_val . '-' . $month_val . '-' . $date_val;
//echo $lastdate ;
// ->select('t_monthly_pay_inputs.*,t_employee_details.FirstName,t_employee_details.LastName,t_payroll.Key,t_loan_master.Loan_ID');
// $builder = $this->db->table('t_monthly_pay_inputs');
// ->join('t_employee_details','t_monthly_pay_inputs.EmpID=t_employee_details.EmpID');
// ->join('t_payroll','t_monthly_pay_inputs.EmpID=t_payroll.EmpID','left');
// ->join('t_loan_master','t_monthly_pay_inputs.EmpID=t_loan_master.EmpID','left');
// $this->db->orderBy('t_employee_details.EmpID','asc');
// $this->db->where('t_employee_details.DateofJoining <=',$lastdate);
// $this->db->where('Month_Year',$current);
$sql = "select t_monthly_pay_inputs.*,t_employee_details.FirstName,t_employee_details.LastName,t_loan_master.Loan_ID,t_loan_master.Monthly_Due,t_payroll.Id,t_employee_details.is_driver
from t_monthly_pay_inputs
left join t_employee_details on t_monthly_pay_inputs.EmpID=t_employee_details.EmpID and t_employee_details.DateofJoining <= ?
left join t_payroll on t_monthly_pay_inputs.EmpID=t_payroll.EmpID
left join t_loan_master on t_monthly_pay_inputs.EmpID=t_loan_master.EmpID and t_loan_master.is_Active = 1
where t_employee_details.is_driver = 0 and Month_Year = ?
order by t_employee_details.EmpID asc";
$query = $this->db->query($sql, array($lastdate, $current));
// print_r($query-getResult());
$result = $query->getResult();
return $result;
}
function getEmpPayDetails($current = "", $s = "")
{
$month_val = date('m', strtotime($current));
$year_val = date('Y', strtotime($current));
$date_val = cal_days_in_month(CAL_GREGORIAN, $month_val, $year_val);
//$monthyear_val = date('Y-m',strtotime($current));
$lastdate = $year_val . '-' . $month_val . '-' . $date_val;
$builder = $this->db->table('t_employee_details')
->select('t_employee_details.EmpID,t_employee_details.FirstName,t_employee_details.LastName,t_employee_details.DateofJoining,t_employee_details.is_driver')
//->join('t_emp_pay_data','t_emp_pay_data.EmpID=t_employee_details.EmpID','Left')
->where('t_employee_details.IsActive', 1)
->where('t_employee_details.is_driver', 0)
->where('t_employee_details.DateofJoining <=', $lastdate)
->orderBy('EmpID', 'asc');
$query = $builder->get();
$result = $query->getResult();
// $last_query = $this->db->getLastQuery();
// echo $last_query;die;
return $result;
}
function getEmpPayDetailsforMonthInputs($current = "")
{
//$month = '0'.$current;
$month = $current;
if (strlen($month) == 6) {
$month = '0' . $month;
}
$current = '01-' . $current;
//echo $current;
$current = format_date($current);//3rd
//echo 'Final'.$current;
$month_val = date('m', strtotime($current));
$year_val = date('Y', strtotime($current));
$date_val = cal_days_in_month(CAL_GREGORIAN, $month_val, $year_val);
//$monthyear_val = date('Y-m',strtotime($current));
$lastdate = $year_val . '-' . $month_val . '-' . $date_val;
//echo $lastdate;
//left join t_payroll on t_employee_details.EmpID = t_payroll.EmpID and month(t_payroll.PayOn) = month(?) and year(t_payroll.PayOn) = year(?)
$sql = "select t_emp_pay_data.EmpID,t_emp_pay_data.*,t_employee_details.work_location,t_employee_details.tds_applicable,t_employee_details.FirstName,t_employee_details.DateofJoining,t_employee_details.Designation,t_employee_details.esi_applicable,t_employee_details.pf_applicable,t_employee_details.is_management_team,t_attendance.Days_Worked as Days_worked,t_attendance.Absent as LOP_Days,t_attendance.Paid_Leave as Paid_leave,t_attendance.Total_OTHrs as OT_Hrs_Worked,t_attendance.NoofDays,t_attendance.sunday_count,t_loan_master.Loan_ID,t_loan_master.Monthly_Due,t_loan_master.Due_Start_Date as DueDate,t_loan_master.Loan_Amount,t_loan_master.Paid_Amount,t_payroll.Id,t_monthly_pay_inputs.Loan_Recovered,t_monthly_pay_inputs.Estimate,t_monthly_pay_inputs.Festival_Bonus,t_monthly_pay_inputs.Other_Deductions,t_monthly_pay_inputs.is_payslip_generated,t_loan_history .Balance_Amount,t_employee_details.is_driver
from t_emp_pay_data
left join t_employee_details on t_employee_details.EmpID = t_emp_pay_data.EmpID
left join t_payroll on t_employee_details.EmpID = t_payroll.EmpID and t_payroll.PayOn = ?
left join t_attendance on t_emp_pay_data.EmpID = t_attendance.EmpID
left join t_monthly_pay_inputs on t_monthly_pay_inputs.EmpID = t_emp_pay_data.EmpID and t_monthly_pay_inputs.Month_Year = ?
left join t_loan_master on t_emp_pay_data.EmpID = t_loan_master.EmpID and t_loan_master.Remaining_Due != 0
left join t_loan_history on t_loan_history .Loan_ID = t_loan_master.Loan_ID
where t_employee_details.DateofJoining <= ?
and month(t_attendance.Month_Year) = month(?) and year(t_attendance.Month_Year) = year(?)
and t_employee_details.is_driver = 0
order by t_employee_details.EmpID";
$query = $this->db->query($sql, array($month, $month, $lastdate, $current, $current));
// Get the last executed query
// $last_query = $this->db->getLastQuery();
// echo $last_query;die;
return $query->getResult();
}
function getPublicHoldays($current = "")
{
if (!empty($current)) {
$sql = "select * from t_public_holidays where month(H_Date) = month(?) and year(H_Date) = year(?)";
$query = $this->db->query($sql, array($current, $current));
return $query->getResult();
} else {
$sql = "select * from t_public_holidays where year(H_Date)=year(CURRENT_DATE) order by H_Date";
$query = $this->db->query($sql);
return $query->getResult();
}
}
function checkHoliday($Date)
{
$Date = format_date($Date);
//echo $Date;die();[
$sql = "select count(*) as count from t_public_holidays where H_Date = ?";
$query = $this->db->query($sql, array($Date));
return $query->getResultArray();
}
function saveHolidays($data, $string)
{
//print_r($data);
if ($string == 'i') {
$builder = $this->db->table('t_public_holidays');
$builder->insert($data);
$i = $this->db->affectedRows();
return $i;
} else if ($string == 'u') {
$this->db->table('t_public_holidays')
->where('H_date', $data['H_Date'])
->update($data);
$i = $this->db->affectedRows();
return $i;
}
}
function deleteHoliday($d)
{
$d = format_date($d);//3rd
$this->db->table('t_public_holidays')->delete(['H_date' => $d]);
return $this->db->affectedRows();
}
function monthlyAttendance($current = "")
{
$month_val = date('m', strtotime($current));
$year_val = date('Y', strtotime($current));
$date_val = cal_days_in_month(CAL_GREGORIAN, $month_val, $year_val);
$lastdate = $year_val . '-' . $month_val . '-' . $date_val;
// echo $current;
// echo "last date - ".$lastdate;die;
$builder = $this->db->table('t_attendance')
->select('t_attendance.*,t_employee_details.FirstName,t_employee_details.LastName')
->join('t_employee_details', 't_attendance.EmpID=t_employee_details.EmpID' , 'left')
// ->where('year(t_employee_details.DateofJoining) <= ',$year_val)
// ->where('month(t_employee_details.DateofJoining) <= ',$month_val)
->where('t_employee_details.DateofJoining <= ', $lastdate)
->where('t_attendance.Month_Year', $current)
->where('t_employee_details.is_driver', 0)
->where('t_employee_details.IsActive',1)
->orderBy('t_attendance.EmpID', 'asc');
$query = $builder->get();
$result = $query->getResult();
// $last_query = $this->db->getLastQuery();
// echo $last_query;die;
return $result;
}
function saveMonthlyData_model($data)
{
$month = $data['Month_Year'];
$empid = $data['EmpID'];
$count = 0;
$builder = $this->db->table('t_monthly_pay_inputs')->select('count(*) as count')
->where('Month_Year', $month)
->where('EmpID', $empid);
$isExits = $builder->get();
$res = $isExits->getResultArray();
if (!empty($res)) {
$count = $res[0]['count'];
}
if ($count == 0) {
$builder = $this->db->table('t_monthly_pay_inputs');
$builder->insert($data);
$i = $this->db->affectedRows();
return $i;
} elseif ($count == 1) {
$this->db->table('t_monthly_pay_inputs')
->where('EmpID', $data['EmpID'])
->where('Month_Year', $data['Month_Year'])
->update($data);
$i = $this->db->affectedRows();
return $i;
}
}
function saveAttendance($monthattendance, $Month_Year, $EmpID)
{
$builder = $this->db->table('t_attendance')->select('count(*) as c')
->where('Month_Year', $Month_Year)
->where('EmpID', $EmpID);
$isExist = $builder->get();
$isExist = $isExist->getResultArray();
$ans = $isExist[0]['c'];
if ($ans == 0) {
//array_pop();
$builder = $this->db->table('t_attendance');
$builder->insert($monthattendance);
return $this->db->affectedRows();
//echo "INS";
// if($this->db->affected_rows() > 0)
// {
// return true;
// }
// else
// {
// return false;
// }
} else {
$this->db->table('t_attendance')
->where('Month_Year', $Month_Year)
->where('EmpID', $EmpID)
->update($monthattendance);
//echo "UPDATE";
$i = $this->db->affectedRows();
return $i;
}
}
function addNewmonthlisting($m_list)
{
$this->db->transStart();
$builder = $this->db->table('t_monthly_pay_inputs');
$builder->insert($m_list);
$Asset_Code = $this->db->insertId();
$this->db->transComplete();
return $m_list;
}
function getUserInfo($EmpID, $month_year)
{
$builder = $this->db->table('t_monthly_pay_inputs')
->select('t_monthly_pay_inputs.*,t_employee_details.FirstName,t_employee_details.LastName')
->join('t_employee_details', 't_monthly_pay_inputs.EmpID=t_employee_details.EmpID')
->where('t_monthly_pay_inputs.EmpID', $EmpID)
->where('t_monthly_pay_inputs.Month_Year', $month_year);
$query = $builder->get();
return $query->getResult();
}
function editmonth($monthdata, $EmpID)
{
$monthyear = $monthdata['Month_Year'];
$this->db->table('t_monthly_pay_inputs')
->where('EmpID', $EmpID)
->where('Month_Year', $monthyear)
->update($monthdata);
return TRUE;
}
function viewmonth($monthdata, $employeeid)
{
$this->db->table('t_monthly_pay_inputs')
->orderBy('Month_Year')
->where('EmpID', $employeeid)
->update($monthdata);
return TRUE;
}
/*permission form model */
function emplist()
{
$sql = "select Emp.EmpID,CONCAT(FirstName,' ',LastName)as name ,Emp.Departmentcode, Dep.DepartmentName FROM t_employee_details as Emp
join t_departmentdetails as Dep on Dep.DEPCode=Emp.Departmentcode where Emp.IsActive = 1;";
$query = $this->db->query($sql);
return $query->getResult();
}
function addper($Permissiondata)
{
//if(!empty($Permissiondata){
$this->db->transStart();
$builder = $this->db->table('t_permission');
$builder->insert($Permissiondata);
//$Asset_Code = $this->db->insert_id();
$Permission = $this->db->affectedRows();
$this->db->transComplete();
return $Permission;
}
function perlist()
{
$builder = $this->db->table('t_permission')->select('*');
$query = $builder->get();
return $query->getResult();
}
function pdf($SNO)
{
$builder = $this->db->table('t_permission per')->select('per.*,emp.FirstName,LastName,dep.DepartmentName')
->join('t_employee_details emp', 'emp.EmpID = per.EmpID')
->join('t_departmentdetails dep', 'dep.DEPCode = emp.Departmentcode')
->where('per.SNO', $SNO);
$query = $builder->get();
return $query->getResult();
}
function reportvalue($fin_year)
{
if (empty($fin_year)) {
return [];
} else {
$curr_split = explode('-', $fin_year);
$pre_year = $curr_split[0];
$curr_year = $curr_split[1];
$sql = "select t_monthly_pay_inputs.EmpID,t_employee_details.FirstName,t_employee_details.LastName,sum(Festival_Bonus) as Bonus
from t_monthly_pay_inputs
join t_employee_details on t_monthly_pay_inputs.EmpID = t_employee_details.EmpID
where
substr(t_monthly_pay_inputs.Month_Year,4) = ?
and substr(t_monthly_pay_inputs.Month_Year,1,2) >= 4
or substr(t_monthly_pay_inputs.Month_Year,4) = ?
and substr(t_monthly_pay_inputs.Month_Year,1,2) <= 3
group by t_monthly_pay_inputs.EmpID";
$query = $this->db->query($sql, array($pre_year, $curr_year));
return $query->getResult();
}
}
function Monthwisereportvalue($Fin_year, $EmployeeID)
{
$curr_split = explode('-', $Fin_year);
$pre_year = $curr_split[0];
$curr_year = $curr_split[1];
$sql = "select t_monthly_pay_inputs.EmpID,t_monthly_pay_inputs.Month_Year,t_employee_details.FirstName,t_employee_details.LastName,sum(Festival_Bonus) as Bonus
from t_monthly_pay_inputs
join t_employee_details on t_monthly_pay_inputs.EmpID = t_employee_details.EmpID
and t_monthly_pay_inputs.EmpID = ?
where substr(t_monthly_pay_inputs.Month_Year,4) = ? and substr(t_monthly_pay_inputs.Month_Year,1,2) >= 4
or substr(t_monthly_pay_inputs.Month_Year,4) = ? and substr(t_monthly_pay_inputs.Month_Year,1,2) <= 3
group by t_monthly_pay_inputs.Month_Year";
$query = $this->db->query($sql, array($EmployeeID, $pre_year, $curr_year));
return $query->getResult();
}
//this function Production Salary cost details
function productionsalarycosts($productions)
{
$builder = $this->db->table('T_ProductionSalaryCost');
$builder->insert($productions);
$r = $this->db->affectedRows();
return $r;
}
function updateproduction($productions, $sno)
{
$builder = $this->db->table('T_ProductionSalaryCost')
->where('sno', $sno)
->update($productions);
$r = $this->db->affectedRows();
return $r;
}
function productionsalarycost()
{
$builder = $this->db->table('T_ProductionSalaryCost')->select('*');
$query = $builder->get();
return $query->getResult();
}
function attyesterday($WH, $daydate)
{
$sql = "select att.EmpID," . $WH . ",a.FirstName from t_attendance as att
join t_employee_details as a on a.EmpID= att.EmpID
where Month_Year = ? and att.EmpID in (select EmpID from t_attendance)";
$query = $this->db->query($sql, array($daydate));
return $query->getResultArray();
}
function dayAllPersentEmpSalary($WH, $OT, $date)
{
$sql = "select EmpID," . $WH . "," . $OT . " from t_attendance where Month_Year = ? and EmpID in (select EmpID from t_attendance)";
$query = $this->db->query($sql, array($date));
return $query->getResultArray();
}
function persentEmployeeDetails($EmpID)
{
$sql = "select * FROM t_emp_pay_data where EmpID = ?;";
$query = $this->db->query($sql, array($EmpID));
return $query->getResult();
}
// function cost($ab)
// {
// $sql = "select * from T_ProductionSalaryCost where month(date)=month('".$ab."')";
// //echo $sql;die;
// $query = $this->db->query($sql);
// return $query->getResult();
// }
function cost($ab, $from, $to)
{
// echo $ab;echo $from;echo $to;
// die;
$sql = "select * from T_ProductionSalaryCost where
month(date)= month('" . $ab . "')";
if ($from and $to != '') {
$sql .= "or date >= '" . $from . "'
and date <= '" . $to . "'";
}
// echo $sql;
$query = $this->db->query($sql);
return $query->getResult();
}
public function getheringleaveApplicationForm($leave_id = null ){
$builder = $this->db->table("t_employee_leave_application_form as leavetable")
->select("leavetable.* ,Dep.DepartmentName as department,Emp.Designation as designation")
->select("CONCAT_WS(' ', Emp.FirstName, Emp.LastName) AS employeeFullName")
->select("CONCAT_WS(' ', Emp2.FirstName, Emp2.LastName) AS creatorFullName")
->join("tbl_users as user", "user.userId = leavetable.created_by", "left")
->join("t_employee_details as Emp", "Emp.EmpID = leavetable.emp_id", "left")
->join("t_employee_details as Emp2", "Emp2.EmpID = user.EmpID", "left")
->join("t_departmentdetails as Dep", "Emp.Departmentcode = Dep.DEPCode", "left");
if (!empty($leave_id)) {
$builder->where('leavetable.leave_application_id', $leave_id);
}
$builder->where('leavetable.isactive', 1);
$query = $builder->get();
return $query->getResultArray();
}
public function getheringemployeeinformation(){ //active employee only no driver also.
$builder = $this->db->table("t_employee_details as Emp")
->select("Emp.EmpID,Emp.Designation,Emp.Departmentcode,Dep.DEPCode,Emp.Designation,Emp.Departmentcode,Dep.DEPCode,Dep.DepartmentName as DepartmentNamee,Dep.DepartmentName as DepartmentName")
->select("CONCAT_WS(' ', Emp.FirstName, Emp.LastName) AS FullName")
->join("t_departmentdetails as Dep", "Emp.Departmentcode = Dep.DEPCode", "left")
->where('Emp.is_driver != ', 1)
->where('Emp.IsActive', 1)
->orderBy("Emp.EmpID", "asc");
$query = $builder->get();
$result = $query->getResult();
return $result;
}
function getCompanyInformation()
{
$subQuery = "select CompanyName,replace(Address,'<br>','') as Address,GSTNO,PAN,ContactNumber,EmailAddress,AlternateContactNumber,companyWebsite,StateCode
from t_company_details ";
$query = $this->db->query($subQuery);
return $query->getResult();
}
public function saveleaveApplicationForm($data,$id){
if ($id == 0) {
$builder = $this->db->table('t_employee_leave_application_form');
$builder->insert($data);
return $this->db->affectedRows();
} else if ($id != 0) {
$this->db->table('t_employee_leave_application_form')
->where('leave_application_id', $id)
->update($data);
return $this->db->affectedRows();
}
}
public function validateDateRange($emp_id,$start_date,$end_date){
$array = $this->db->table('t_employee_leave_application_form')
->where('emp_id', $emp_id)
->where('status', 'Approved')
->groupStart() // Start grouping OR conditions
->where("'$start_date' BETWEEN start_date AND end_date", null, false)
->orWhere("'$end_date' BETWEEN start_date AND end_date", null, false)
->orWhere("start_date BETWEEN '$start_date' AND '$end_date'", null, false)
->orWhere("end_date BETWEEN '$start_date' AND '$end_date'", null, false)
->groupEnd() // End grouping
->get()
->getRowArray();
// $last_query = $this->db->getLastQuery();
// echo $last_query;
return $array;
}
function getEmpPayDetailsforOTListing($current = "")
{
//$month = '0'.$current;
$month = $current;
if (strlen($month) == 6) {
$month = '0' . $month;
}
$current = '01-' . $current;
//echo $current;
$current = format_date($current);//3rd
//echo 'Final'.$current;
$month_val = date('m', strtotime($current));
$year_val = date('Y', strtotime($current));
$date_val = cal_days_in_month(CAL_GREGORIAN, $month_val, $year_val);
//$monthyear_val = date('Y-m',strtotime($current));
$lastdate = $year_val . '-' . $month_val . '-' . $date_val;
//echo $lastdate;
//left join t_payroll on t_employee_details.EmpID = t_payroll.EmpID and month(t_payroll.PayOn) = month(?) and year(t_payroll.PayOn) = year(?)
$sql = "select t_emp_pay_data.EmpID,t_emp_pay_data.*,t_employee_details.FirstName,t_attendance.Days_Worked as Days_worked,t_attendance.Absent as LOP_Days,t_attendance.Paid_Leave as Paid_leave,t_attendance.Total_OTHrs as OT_Hrs_Worked,t_attendance.NoofDays,t_employee_details.is_management_team
from t_emp_pay_data
left join t_employee_details on t_employee_details.EmpID = t_emp_pay_data.EmpID
left join t_attendance on t_emp_pay_data.EmpID = t_attendance.EmpID
left join t_monthly_pay_inputs on t_monthly_pay_inputs.EmpID = t_emp_pay_data.EmpID and t_monthly_pay_inputs.Month_Year = ?
where t_employee_details.DateofJoining <= ?
and month(t_attendance.Month_Year) = month(?) and year(t_attendance.Month_Year) = year(?)
and t_attendance.Total_OTHrs != '0.00'
and t_employee_details.is_driver = 0
order by t_employee_details.EmpID";
$query = $this->db->query($sql, array($month, $lastdate, $current, $current));
// Get the last executed query
// $last_query = $this->db->getLastQuery();
// echo $last_query;
// die;
return $query->getResult();
}
function employeeLeaveDates($current){
$current =get_date_time_dynamical_format('m-Y','Y-m',"$current");
$array = $this->db->table('t_employee_leave_application_form')
->select("emp_id,start_date,end_date,status")
->where('status', 'Approved')
->groupStart() // Start grouping OR conditions
->where("DATE_FORMAT(start_date, '%Y-%m') = '$current'", null, false)
->orWhere("DATE_FORMAT(end_date, '%Y-%m') = '$current'", null, false)
->orWhere("DATE_FORMAT(start_date, '%Y-%m') <= '$current' AND DATE_FORMAT(end_date, '%Y-%m') >= '$current'", null, false)
->groupEnd() // End grouping
->get()
->getResultArray();
// $last_query = $this->db->getLastQuery();
// echo $last_query;
return $array;
// return [$current];
}
function getActiveEmployee()
{
$array = $this->db->table('t_employee_details')
->select("FirstName,LastName,Gender,EmpID")
->where('IsActive', 1)
->get()
->getResultArray();
return $array;
}
function leave_report($from_date, $to_date, $empID)
{
$builder = $this->db->table('t_monthly_pay_inputs MPI');
$builder->selectSum('MPI.LOP_Days');
$builder->selectSum('MPI.Paid_leave');
$builder->selectSum('MPI.OT_Hrs_Worked');
$builder->selectSum('MPI.OTSalary');
$builder->select('MPI.EmpID, ED.FirstName , ED.LastName');
$builder->join('t_employee_details ED', 'MPI.EmpID = ED.EmpID', 'left');
// Filter by EmpID if set
if (!empty($empID)) {
$builder->where('MPI.EmpID', $empID);
}
// Filter by date range if both dates are provided
if (!empty($from_date) && !empty($to_date)) {
$builder->where("MPI.Month_Year >=", $from_date);
$builder->where("MPI.Month_Year <=", $to_date);
}
$builder->groupBy('MPI.EmpID');
$query = $builder->get();
return $query->getResultArray();
}
}