448 lines
15 KiB
PHP
Executable File
448 lines
15 KiB
PHP
Executable File
<?php
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class Costcenter_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 CostListing()
|
|
{
|
|
$builder = $this->db->table('t_costcenter_master as Cost')
|
|
->select('Cost.id as id, Cost.CostCenterCode as code,Cost.CostCenterName as CostCenterName , Cost.ApprovedBy as ApprovedBy,Emp.FirstName as FirstName,Emp.Designation as Designation ,Cost.CreatedDate,Cost.IsActive,Cost.DeletedBy,Cost.DeletedBy')
|
|
|
|
->join('t_employee_details as Emp', 'Emp.EmpID=Cost.ApprovedBy')
|
|
->orderBy('Cost.CreatedDate', 'desc');
|
|
|
|
$query =$builder->get();
|
|
$result = $query->getResult();
|
|
|
|
return $result;
|
|
}
|
|
/**
|
|
* This function used to get user information by id
|
|
* @param number $userId : This is user id
|
|
* @return array $result : This is user information
|
|
*/
|
|
public function saveCostCenter($Cost) {
|
|
$this->db->transStart();
|
|
$builder = $this->db->table('t_costcenter_master');
|
|
$builder->insert($Cost);
|
|
$aff_row = $this->db->affectedRows();
|
|
log_message('error', 'saveCostCenter Query: ' . $this->db->getLastQuery()->getQuery());
|
|
$this->db->transComplete();
|
|
return $aff_row;
|
|
}
|
|
|
|
public function updateCostCenter($Cost, $id) {
|
|
$this->db->table('t_costcenter_master')
|
|
->where('id', $id)
|
|
->update($Cost);
|
|
$aff_row = $this->db->affectedRows();
|
|
return $aff_row;
|
|
}
|
|
public function lastCCID(){
|
|
$query = $this->db->table('t_costcenter_master')->select('id')->orderBy('id', 'DESC')->limit(1)->get();
|
|
$lastCCID = $query->getRow()->id;
|
|
return $lastCCID;
|
|
}
|
|
|
|
function getFiscalYear()
|
|
{
|
|
$builder = $this->db->table('t_company_details')
|
|
->select('year(FiscalYearStartOn) as StartYear,year(FiscalYearEndsOn) as EndYear');
|
|
$query =$builder->get();
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
/**
|
|
* This function used to get user information by id
|
|
* @param number $userId : This is user id
|
|
* @return array $result : This is user information
|
|
*/
|
|
function getuserInfo($CostCenterID)
|
|
{
|
|
|
|
$builder = $this->db->table('T_CostCentre')
|
|
->select('CostCentreID, CostName,Description,CreatedDate')
|
|
->where('CostCentreID', $CostCenterID);
|
|
$query =$builder->get();
|
|
|
|
return $query->getResult();
|
|
}
|
|
/**
|
|
* This function used to get Cost center Master information by Cost center ID
|
|
* @param number $CostCenterCode : CostCenterCode as Parameter
|
|
* @return array $result : This is user information
|
|
*/
|
|
function GetCostCenterMaster($CostCenterCode)
|
|
{
|
|
$builder = $this->db->table('t_costcenter_master Cost')
|
|
->select('Cost.CostCenterCode,Cost.CostCenterName,Cost.ApprovedBy,emp.FirstName,emp.Designation,max(BudgetYear) as BudYear' )
|
|
|
|
->join('t_employee_details emp', 'emp.EmpID = Cost.ApprovedBy')
|
|
->join('t_costcenter_budget Bud', ' Bud.CostCenterCode = Cost.CostCenterCode')
|
|
->where('Cost.CostCenterCode', $CostCenterCode);
|
|
$query =$builder->get();
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
/**
|
|
* This function used to get Cost center Department information by Cost center ID
|
|
* @param number $CostCenterCode : CostCenterCode as Parameter
|
|
* @return array $result : This is user information
|
|
*/
|
|
function GetCostCenterDepartment($CostCenterCode)
|
|
{
|
|
$builder = $this->db->table('t_costcenter_departments Dept')
|
|
->select('Dept.DEPCode,Det.DepartmentName')
|
|
->join('t_departmentdetails Det', 'Det.DEPCode = Dept.DEPCode')
|
|
->where('Dept.CostCenterCode', $CostCenterCode);
|
|
$query =$builder->get();
|
|
|
|
return $query->getResult();
|
|
}
|
|
/**
|
|
* This function used to get Cost center Budget information by Cost center ID
|
|
* @param number $CostCenterCode : CostCenterCode as Parameter
|
|
* @return array $result : This is user information
|
|
*/
|
|
function GetBudgetYear($CostCenterCode)
|
|
{
|
|
|
|
$subQuery = 'select BudgetYear from t_costcenter_budget where CostCenterCode=? ';
|
|
|
|
|
|
$query = $this->db->query($subQuery, array($CostCenterCode));
|
|
|
|
return $query->getResult();
|
|
}
|
|
/**
|
|
* This function used to get Cost center Budget information by Cost center ID
|
|
* @param number $CostCenterCode : CostCenterCode as Parameter
|
|
* @return array $result : This is user information
|
|
*/
|
|
function GetCostCenterBudget($CostCenterCode)
|
|
{
|
|
|
|
$subQuery = 'select BudgetType,BudgetYear,BudgetAmount,Remarks from t_costcenter_budget where CostCenterCode=? and
|
|
BudgetYear=(select max(BudgetYear) from t_costcenter_budget where CostCenterCode=?)';
|
|
|
|
|
|
$query = $this->db->query($subQuery, array($CostCenterCode,$CostCenterCode));
|
|
|
|
return $query->getResult();
|
|
}
|
|
/**
|
|
* This function used to get Cost center Budget information by Cost center ID
|
|
* @param number $CostCenterCode : CostCenterCode as Parameter
|
|
* @return array $result : This is user information
|
|
*/
|
|
function GetCostBudgetTypeByYear($CostCenterCode,$ConfigID,$Year)
|
|
{
|
|
|
|
$subQuery = 'select ConfigValue from t_configdetails Bud where ConfigValue not in
|
|
(select BudgetType from t_costcenter_budget where CostCenterCode=? and BudgetYear=?) and Config_ID=? ';
|
|
|
|
$query = $this->db->query($subQuery, array($CostCenterCode,$Year,$ConfigID));
|
|
|
|
|
|
return $query->getResultArray();
|
|
|
|
}
|
|
/**
|
|
* This function used to get Cost center Budget information by Cost center ID
|
|
* @param number $CostCenterCode : CostCenterCode as Parameter
|
|
* @return array $result : This is user information
|
|
*/
|
|
function GetCostCenterBudgetByYear($CostCenterCode,$Year)
|
|
{
|
|
|
|
$subQuery = 'select BudgetType,BudgetYear,BudgetAmount,Remarks from t_costcenter_budget where CostCenterCode=? and
|
|
budgetyear=?';
|
|
|
|
|
|
$query = $this->db->query($subQuery, array($CostCenterCode,$Year));
|
|
|
|
return $query->getResultArray();
|
|
}
|
|
|
|
/**
|
|
* This function used to Department list which is not available in the Costcenter master
|
|
* @param number $CostCenterCode : CostCenterCode as Parameter
|
|
* @return array $result : This is user information
|
|
*/
|
|
function GetDepartmentNotinCostCenter($CostCenterCode)
|
|
{
|
|
|
|
$subQuery = 'select Dept.DEPCode,Dept.DepartmentName from t_departmentdetails Dept
|
|
where Dept.DEPCode not in
|
|
(select DEPCode from t_costcenter_departments where CostCenterCode=?) order by Dept.DEPCode asc';
|
|
|
|
$query = $this->db->query($subQuery, array($CostCenterCode));
|
|
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
|
|
|
|
function getDepartmentnotadded($Empid)
|
|
{
|
|
|
|
$subQuery = 'select Dept.DEPCode,Dept.DepartmentName from t_departmentdetails Dept
|
|
left join t_accessdepartments Ass on Ass.Departmentcode= Dept.DEPCode
|
|
where Dept.DEPCode not in
|
|
(select Ass.Departmentcode from t_accessdepartments Ass where EmpID=?) group by Dept.DEPCode';
|
|
|
|
$query = $this->db->query($subQuery, array($Empid));
|
|
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
|
|
function getaccessDept($Empid)
|
|
{
|
|
$builder = $this->db->table('t_accessdepartments Ass')
|
|
->select('Ass.Departmentcode as AssDep,Dep.DepartmentName')
|
|
->join('t_departmentdetails Dep', 'Dep.DEPCode = Ass.Departmentcode','left')
|
|
->where('EmpID ',$Empid);
|
|
|
|
$query =$builder->get();
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
|
|
/**
|
|
* This function used to Department list which is not available in the Costcenter master
|
|
* @param number $CostCenterCode : CostCenterCode as Parameter
|
|
* @return array $result : This is user information
|
|
*/
|
|
function GetBudgetTypeNotinCostCenter($CostCenterCode,$ConfigID)
|
|
{
|
|
|
|
$subQuery = 'select ConfigValue from t_configdetails Bud where ConfigValue not in
|
|
(select BudgetType from t_costcenter_budget where CostCenterCode=? and BudgetYear=(select max(BudgetYear) from t_costcenter_budget)) and Config_ID=?';
|
|
|
|
$query = $this->db->query($subQuery, array($CostCenterCode,$ConfigID));
|
|
|
|
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
function EditCost($Cost,$CostCenterCode)
|
|
{
|
|
$this->db->table('t_costcenter_master')
|
|
->where('CostCenterCode',$CostCenterCode)
|
|
->update($Cost);
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
function EditBudget($Budget)
|
|
{
|
|
$this->db->transStart();
|
|
$builder = $this->db->table('t_costcenter_budget');
|
|
$builder->insert($Budget);
|
|
|
|
$insert_id = $this->db->affectedRows();
|
|
|
|
$this->db->transComplete();
|
|
|
|
return $insert_id;
|
|
}
|
|
|
|
function DeleteBudget($BudgetType='',$BudgetYear='',$CostCenterCode ='')
|
|
{
|
|
$this->db->table('t_costcenter_budget')->delete(['BudgetType' => $BudgetType, 'BudgetYear' => $BudgetYear, 'CostCenterCode' => $CostCenterCode]);
|
|
}
|
|
|
|
function DeleteDepartment($DepCode='',$CostCode='')
|
|
{
|
|
$this->db->table('t_costcenter_departments')->delete(['DEPCode' => $DepCode, 'CostCenterCode' => $CostCode]);
|
|
}
|
|
|
|
function checkDeptCostExists($DepCode='',$CostCode='')
|
|
{
|
|
$builder = $this->db->table('t_costcenter_departments')
|
|
->select('CostCenterCode,DEPCode')
|
|
->where('DEPCode',$DepCode)
|
|
->where('CostCenterCode',$CostCode);
|
|
|
|
$query =$builder->get();
|
|
|
|
if ($query->getNumRows() > 0) {
|
|
return $query->getResultArray();
|
|
}
|
|
}
|
|
function checkCostDetailsExists($Code='',$CostName='')
|
|
{
|
|
|
|
$builder = $this->db->table('t_costcenter_master')
|
|
->select('CostCenterCode')
|
|
->where('CostCenterCode',$Code);
|
|
if ($CostName != '')
|
|
{
|
|
$builder->where('CostCenterName !=',$CostName);
|
|
}
|
|
$query =$builder->get();
|
|
// echo $query->getNumRows();
|
|
// print_r($query->getResultArray());
|
|
// echo $this->db->getLastQuery()->getQuery();
|
|
if ($query->getNumRows() > 0) {
|
|
return $query->getResultArray();
|
|
} else{
|
|
return [];
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* This function is used to get the Config value from configuration table
|
|
* @return array $result : This is result of the query
|
|
*/
|
|
function getConfigValue($ConfigID )
|
|
{
|
|
$builder = $this->db->table('t_configdetails')
|
|
->select('ConfigValue')
|
|
->where('Config_id ',$ConfigID );
|
|
$query =$builder->get();
|
|
|
|
return $query->getResult();
|
|
}
|
|
|
|
function export_excel(){
|
|
$where = null;
|
|
|
|
$builder = $this->db->table('t_costcenter_master as bud')
|
|
->select('Mas.*,bud.*,emp.firstname,emp1.firstname as Updated_By')//count(dep.DEPCode) AS ctvalue');
|
|
->select('sum(distinct case when Mas.budgettype="CAPITAL" then budgetamount else 0 end) as capital',FALSE)
|
|
->select('sum(distinct case when Mas.budgettype="REVENUE" then budgetamount else 0 end) as revenue',FALSE)
|
|
->select('sum(distinct case when Mas.budgettype="SERVICE" then budgetamount else 0 end) as service',FALSE)
|
|
->select('sum(distinct case when Mas.budgettype="IMPORT" then budgetamount else 0 end) as import',FALSE)
|
|
->select('GROUP_CONCAT(distinct part.DepartmentName) as DepartmentName')
|
|
->join('t_costcenter_budget as Mas','Mas.CostCenterCode=bud.CostCenterCode','left')
|
|
->join('t_costcenter_departments as dep',' dep.CostCenterCode=Mas.CostCenterCode','left')
|
|
->join('t_departmentdetails as part','part.DEPCode=dep.DEPCode','left')
|
|
->groupBy(array('bud.CostCenterCode','Mas.BudgetYear'))
|
|
->join('tbl_users as tbl',' bud.createdby = tbl.userid','left')
|
|
->join('t_employee_details as emp', 'emp.empid=tbl.empid','left')
|
|
->join('tbl_users as tbl1',' bud.UpdatedBy = tbl1.userid','left')
|
|
->join('t_employee_details as emp1', 'emp1.empid=tbl1.empid','left')
|
|
->where('Mas.BudgetYear !=""');
|
|
return $builder->get()->getResultArray();
|
|
|
|
}
|
|
//get exist budget year details
|
|
function CheckExistBudget($BudgetType,$BudgetYear,$CostCode){
|
|
$builder = $this->db->table('t_costcenter_budget')
|
|
->select('CostCenterCode')
|
|
->where('CostCenterCode',$CostCode)
|
|
->where('BudgetType',$BudgetType)
|
|
->where('BudgetYear',$BudgetYear);
|
|
|
|
$query =$builder->get();
|
|
|
|
if ($query->getNumRows() > 0) {
|
|
return TRUE;
|
|
}
|
|
else{
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
//update budget
|
|
function updateBudget($Budget,$BudgetType,$BudgetYear,$CostCode)
|
|
{
|
|
$this->db->table('t_costcenter_budget')
|
|
->where('CostCenterCode',$CostCode)
|
|
->where('BudgetType',$BudgetType)
|
|
->where('BudgetYear',$BudgetYear)
|
|
->update($Budget);
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
function reActivateCostCenter( $CostCenterCode , $UpdatedBy)
|
|
{
|
|
|
|
$tables = [
|
|
't_costcenter_master',
|
|
't_costcenter_budget',
|
|
't_costcenter_departments'
|
|
];
|
|
|
|
|
|
foreach ($tables as $table) {
|
|
$builder = $this->db->table($table);
|
|
$builder->select('IsActive');
|
|
$builder->where('CostCenterCode', $CostCenterCode);
|
|
$query = $builder->get();
|
|
$result = $query->getRowArray();
|
|
|
|
if ($result && $result['IsActive'] != 1) {
|
|
// If IsActive is not 1, update it to 1
|
|
$builder->where('CostCenterCode', $CostCenterCode);
|
|
$builder->update(['IsActive' => 1,'DeletedBy' => $UpdatedBy ]);
|
|
}
|
|
}
|
|
|
|
return "Cost center $CostCenterCode Reactivated successfully.";
|
|
}
|
|
|
|
function deleteCostCenter($CostCenterCode,$UpdatedBy)
|
|
{
|
|
|
|
$tables = [
|
|
't_costcenter_master',
|
|
't_costcenter_budget',
|
|
't_costcenter_departments'
|
|
];
|
|
|
|
|
|
foreach ($tables as $table) {
|
|
$builder = $this->db->table($table);
|
|
$builder->select('IsActive');
|
|
$builder->where('CostCenterCode', $CostCenterCode);
|
|
$query = $builder->get();
|
|
$result = $query->getRowArray();
|
|
|
|
if ($result && $result['IsActive'] != 0) {
|
|
// If IsActive is not 0, update it to 0
|
|
$builder->where('CostCenterCode', $CostCenterCode);
|
|
$builder->update(['IsActive' => 0,'DeletedBy' => $UpdatedBy ]);
|
|
}
|
|
}
|
|
|
|
return "Cost center $CostCenterCode deactivated successfully.";
|
|
}
|
|
|
|
function checkCostCenter($CostCenterId,$CostCenterName){
|
|
$builder = $this->db->table('t_costcenter_master')
|
|
->select('CostCenterName,IsActive');
|
|
|
|
if(!empty($CostCenterId)){
|
|
$builder = $builder -> where('id !=' ,$CostCenterId);
|
|
}
|
|
$builder = $builder->where('REPLACE(CostCenterName," ", "")',$CostCenterName);
|
|
$query = $builder->get();
|
|
|
|
return $query->getResult();
|
|
|
|
}
|
|
|
|
|
|
}
|
|
?>
|