ria/app/Controllers/CostCenter.php
2024-06-25 07:18:56 +00:00

509 lines
19 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\Validation\Exceptions\ValidationException;
use App\Models\Costcenter_model;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/**
* Module : Cost Center
* Cost Center Class to control all cost center related operations.
* @author : Venba
* @version : 1.1
* @since : 15 Feb 2016
* @example : Revised at 15th april 2024
*/
class CostCenter extends BaseController
{
protected $costcenter_model;
protected $session;
/**
* This is default constructor of the class
*/
public function __construct()
{
parent::__construct();
$this->costcenter_model = new Costcenter_model();
$this->session = session();
helper('form'); //$this->load->library('form_validation');
// $this->load->library('pagination');
$this->isLoggedIn();
}
/**
* This function used to load the first screen of the user
*/
public function index()
{
$data['userRecords'] = $this->costcenter_model->CostListing();
$this->global['pageTitle'] = 'Cost List';
$this->loadViews("costListing", $this->global, $data, NULL);
}
/**
* This function is used to load the add new cost Center */
function addCostCenter()
{
$data['Department'] = $this->costcenter_model->getDepartment();
$data['BudgetType'] = $this->costcenter_model->getConfigValue('C004');
$data['Approver'] = $this->costcenter_model->getApproverName();
$data['FiscalYear'] = $this->costcenter_model->getFiscalYear();
$this->global['pageTitle'] = 'Add Cost Center';
$this->loadViews("addCostCenter", $this->global, $data, NULL);
}
function Dep_SelectedDepartment($selectValue)
{
//echo 'select_validate method called';
// 'none' is the first option and the text says something like "-Choose one-"
if (strlen($selectValue) <= 0) {
// $this->validator->setMessage('Dep_SelectedDepartment', 'Please Select Department.');
return false;
} else // user picked something
{
return true;
}
}
/**
* This function is used to add new Cost to the system
*/
function saveCostCenter()
{
// $this->validator->setRules([
// 'CostName' => 'trim|required',
// 'CostCode' => 'trim|required',
// ]);
// if ($this->validator->run() == FALSE) {
// $this->addCostCenter();
// } else {
$CostCode = $this->request->getPost('CostCode');
$CostCode = ($CostCode !== null && is_string($CostCode)) ? strtoupper(trim($CostCode)) :null;
$CostName = $this->request->getPost('CostName');
$CostName = ($CostName !== null && is_string($CostName)) ? strtoupper(trim($CostName)) :null;
$CreateBy = $this->session->get('userId');
$SelectedDepartment = $this->request->getPost('txtSelectedDepartment');
// $ApproverName = $this->request->getPost('ApprovedBy');
$ApproverName = $this->session->get('EmpID');
$comma_separated = explode(':', (string)$SelectedDepartment);
// $comma_separated = $this->costcenter_model->getDepartment();
// print_r($comma_separated);die;
$createddt = get_current_date_time();
$CodeExists = $this->costcenter_model->checkCostDetailsExists($CostCode);
if (count($CodeExists) == 0) {
$Cost = array('CostCenterCode' => $CostCode, 'CostCenterName' => $CostName, 'ApprovedBy' => $ApproverName, 'CreatedBy' => $CreateBy, 'createdDate' => $createddt);
$this->costcenter_model->saveCostCenter($Cost);
}
for ($j = 0; $j < count($comma_separated); $j++) {
$DeptCode = $comma_separated[$j];
$this->costcenter_model->DeleteDepartment($DeptCode, $CostCode);
$Dept = array('CostCenterCode' => $CostCode, 'DEPCode' => $DeptCode, 'CreatedBy' => $CreateBy, 'createdDate' => $createddt);
$this->costcenter_model->addCostCenterDepartment($Dept);
}
$RowCount = $this->request->getPost('txtRowCount');
for ($i = 1; $i <= $RowCount; $i++) {
$BudgetType = $this->request->getPost('BudgetType' . $i);
$BudgetYear = $this->request->getPost('BudgetYear' . $i);
$BudgetAmount = $this->request->getPost('BudgetAmount' . $i);
$Remarks = $this->request->getPost('Remarks' . $i);
$this->costcenter_model->DeleteBudget($BudgetType, $BudgetYear, $CostCode);
$Budget = array('CostCenterCode' => $CostCode, 'BudgetType' => $BudgetType, 'BudgetYear' => $BudgetYear, 'BudgetAmount' => $BudgetAmount, 'Remarks' => $Remarks, 'CreatedBy' => $CreateBy, 'createdDate' => $createddt);
$this->costcenter_model->addBudget($Budget);
}
echo 'Successfully Created Cost details';
// }
}
/**
* This function is used load user edit information
* @param number $userId : Optional : This is user id
*/
function editCostCenter($CostCode = NULL)
{
if ($CostCode == '') {
$CostCenterID = $_GET['CostCode'];
} else {
$CostCenterID = $CostCode;
}
$data['CostMaster'] = $this->costcenter_model->GetCostCenterMaster($CostCenterID);
$data['CostDepts'] = $this->costcenter_model->GetCostCenterDepartment($CostCenterID);
$data['CostBudget'] = $this->costcenter_model->GetCostCenterBudget($CostCenterID);
$data['DeptList'] = $this->costcenter_model->GetDepartmentNotinCostCenter($CostCenterID);
$data['BudType'] = $this->costcenter_model->GetBudgetTypeNotinCostCenter($CostCenterID, 'C004');
$data['BudYear'] = $this->costcenter_model->GetBudgetYear($CostCenterID);
$data['FiscalYear'] = $this->costcenter_model->getFiscalYear();
//print_r(count($data['BudType']));
$this->global['pageTitle'] = 'Edit Cost';
$this->loadViews("editCostCenter", $this->global, $data, NULL);
}
function deleteCostCenter($CostCode = NULL)
{
$CostCenterID = $CostCode ?? $_GET['CostCode'];
if(!empty($CostCenterID)){
$updatedBy = $this->session->get('userId');
$message = $this->costcenter_model->deleteCostCenter($CostCenterID, $updatedBy);
}else{
$message = "Cost Center Code Not Avalable";
}
$this->session->setFlashdata('success', $message);
return redirect()->route('costListing');
}
function GetBudgetForYear()
{
//$id = $this->request->getPost('id');
$BudYear = $_GET['BudYear'];
$CostCenterID = $_GET['CostCode'];
$result = $this->costcenter_model->GetCostCenterBudgetByYear($CostCenterID, $BudYear);
$BudList = $this->costcenter_model->GetCostBudgetTypeByYear($CostCenterID, 'C004', $BudYear);
$HTML = "";
$HTML2 = "";
$index = 0;
// $HTML2 = $BudList[0]['ConfigValue'];
//if($BudList->num_rows() > 0){
for ($j = 0; $j < count($BudList); $j++) {
$ConfigValue = $BudList[$j]['ConfigValue'];
$HTML2 .= "<option value='" . $ConfigValue . "'>" . $ConfigValue . "</option>";
}
for ($i = 0; $i < count($result); $i++) {
$index = $index + 1;
$BudgetType = $result[$i]['BudgetType'];
$BudgetYear = $result[$i]['BudgetYear'];
$BudgetAmount = $result[$i]['BudgetAmount'];
$Remarks = $result[$i]['Remarks'];
$HTML .= "<tr id='" . $index . "'>
<td align='left'>" . $BudgetType . "</td>
<input type='hidden' name='BudgetType" . $index . "' id='BudgetType" . $index . "' value='" . $BudgetType . "'/>
<input type='hidden' name='BudgetYear" . $index . "' id='BudgetYear" . $index . "' value='" . $BudgetYear . "'/>
<input type='hidden' name='BudgetAmount" . $index . "' id='BudgetAmount" . $index . "' value='" . $BudgetAmount . "'/>
<input type='hidden' name='Remarks" . $index . "' id='Remarks" . $index . "' value='" . $Remarks . "'/>
<td align='left'>" . $BudgetYear . "</td>
<td align='left'>" . $BudgetAmount . "</td>
<td align='left'>" . $Remarks . "</td>
<td>
<a data-target='#Edit' data-id=" . $index . " data-userid=" . $index . " data-toggle='modal' href='#Edit'><i class='fa fa-pencil' data-toggle='tooltip' title='Click here to view/Edit the " . $BudgetType . " Budget details'></i>&nbsp;&nbsp;&nbsp;</a> </td></tr>
";
}
$HTML .= "<input type='hidden' name='txtRowCount' id='txtRowCount' value='" . $index . "'/>";
die(json_encode(array('Html' => $HTML, 'BudYear' => $HTML2)));
// echo $HTML2;
}
/**
* This function is used to edit the user information
*/
function updateCostCenter()
{
$CostCode = $this->request->getPost('CostCode');
$CostCode = ($CostCode !== null && is_string($CostCode)) ? strtoupper(trim($CostCode)) :null;
$CostName = $this->request->getPost('CostName');
$CostName = ($CostName !== null && is_string($CostName)) ? strtoupper(trim($CostName)) :null;
$updatedBy = $this->session->get('userId');
$SelectedDepartment = (string)$this->request->getPost('txtSelectedDepartment');
// $ApproverName = $this->request->getPost('ApprovedBy');
// $ApproverName = $this->session->get('EmpID');
$ApproverName = NULL;
$comma_separated = [];
print_r(strlen($SelectedDepartment));
if(strlen($SelectedDepartment)>0)
{
$comma_separated = explode(':', $SelectedDepartment);
}
$updateddt = get_current_date_time();
$Cost = array( 'CostCenterName'=>$CostName,'ApprovedBy'=>$ApproverName,'UpdatedBy'=>$updatedBy,'UpdatedDate'=>$updateddt);
$this->costcenter_model->EditCost($Cost,$CostCode);
if(count($comma_separated)>= 1)
{
for ($j = 0; $j < count($comma_separated); $j++)
{
// $DeptCode = $comma_separated[$j];
// $this->costcenter_model->DeleteDepartment( $DeptCode,$CostCode);
// $Dept = array('CostCenterCode'=>$CostCode, 'DEPCode'=>$DeptCode,'CreatedBy'=>$updatedBy,'createdDate'=>$updateddt,'UpdatedBy'=>$updatedBy,'UpdatedDate'=>$updateddt);
// $this->costcenter_model->addCostCenterDepartment($Dept);
}
}
$RowCount = $this->request->getPost('txtRowCount');
if($RowCount){
for ($i = 1; $i <= $RowCount; $i++)
{
$BudgetType = $this->request->getPost('BudgetType'.$i);
$BudgetYear = $this->request->getPost('BudgetYear'.$i);
$BudgetAmount = $this->request->getPost('BudgetAmount'.$i);
$Remarks = $this->request->getPost('Remarks'.$i);
// $this->costcenter_model->DeleteBudget($BudgetType,$BudgetYear,$CostCode);
$Budget = array('CostCenterCode'=>$CostCode, 'BudgetType'=>$BudgetType,'BudgetYear'=>$BudgetYear,'BudgetAmount'=>$BudgetAmount,'Remarks'=>$Remarks,'CreatedBy'=>$updatedBy,'createdDate'=>$updateddt,'UpdatedBy'=>$updatedBy,'UpdatedDate'=>$updateddt);
$ExistYearBudget = $this->costcenter_model->CheckExistBudget($BudgetType,$BudgetYear,$CostCode);
// print_r($Budget);
if($ExistYearBudget){
$this->costcenter_model->updateBudget($Budget,$BudgetType,$BudgetYear,$CostCode);
}
else{
$this->costcenter_model->addBudget($Budget);
}
}
}
echo('Successfully Updated Cost details');
}
function DeleteCostDepartment()
{
// echo "dasf";
// die();
$CostCode = $this->request->getPost('CostCode');
$CostCode = ($CostCode !== null && is_string($CostCode)) ? strtoupper(trim($CostCode)) :null;
$CostName = $this->request->getPost('CostName');
$CostName = ($CostName !== null && is_string($CostName)) ? strtoupper(trim($CostName)) :null;
$updatedBy = $this->session->get('userId');
$SelectedDepartment = $this->request->getPost('txtSelectedDepartment');
$this->costcenter_model->DeleteDepartment($SelectedDepartment, $CostCode);
echo ("Department Removed Successfully");
}
function AddCostDepartment()
{
$CostCode = $this->request->getPost('CostCode');
$CostCode = ($CostCode !== null && is_string($CostCode)) ? strtoupper(trim($CostCode)) :null;
$CostName = $this->request->getPost('CostName');
$CostName = ($CostName !== null && is_string($CostName)) ? strtoupper(trim($CostName)) :null;
$updatedBy = $this->session->get('userId');
$SelectedDepartment = $this->request->getPost('txtSelectedDepartment');
// $updatedBy = $this->session->get ( 'userId' );
$updateddt = get_current_date_time();
$Dept = array('CostCenterCode' => $CostCode, 'DEPCode' => $SelectedDepartment, 'CreatedBy' => $updatedBy, 'createdDate' => $updateddt, 'UpdatedBy' => $updatedBy, 'UpdatedDate' => $updateddt);
$this->costcenter_model->addCostCenterDepartment($Dept);
echo ("Department Added Successfully");
}
// function AddCostDepartment()
// {
// // echo "343";
// // die();
// $CostCode = strtoupper(trim($this->request->getPost('CostCode')));
// $CostName = strtoupper(trim($this->request->getPost('CostName')));
// $updatedBy = $this->session->get ( 'userId' );
// $SelectedDepartment = $this->request->getPost('txtSelectedDepartment');
// //echo $SelectedDepartment;
// //die();
// $ApproverName = $this->request->getPost('ApprovedBy');
// $comma_separated = "";
// //print_r(strlen($SelectedDepartment));
// if(strlen($SelectedDepartment)>0)
// {
// $comma_separated = explode(':', $SelectedDepartment);
// }
// //die();
// //echo count($comma_separated);
// $updateddt = get_current_date_time();
// if(count($comma_separated)>= 1)
// {
// for ($j = 0; $j < count($comma_separated); $j++)
// {
// $DeptCode = $comma_separated[$j];
// $this->costcenter_model->DeleteDepartment( $DeptCode,$CostCode);
// $Dept = array('CostCenterCode'=>$CostCode, 'DEPCode'=>$DeptCode,'CreatedBy'=>$updatedBy,'createdDate'=>$updateddt,'UpdatedBy'=>$updatedBy,'UpdatedDate'=>$updateddt);
// //print_r($Dept);
// $this->costcenter_model->addCostCenterDepartment($Dept);
// }
// echo "Updated";
// }
// }
function viewcost($CostCenterID = NULL)
{
if ($CostCenterID == null) {
redirect('costListing');
}
$data['Cost'] = $this->costcenter_model->getuserinfo($CostCenterID);
//$data['users'] = $this->purchaseorder_model->getusers();
$this->global['pageTitle'] = 'View Cost';
$this->loadViews("viewcost", $this->global, $data, NULL);
}
/**
* This function is used to delete the user using userId
* @return boolean $result : TRUE / FALSE
*/
function deletecost()
{
// if($this->isAdmin() == TRUE)
// {
// echo(json_encode(array('status'=>'access')));
// }
// else
// {
$CostCenterID = $this->request->getPost('CostCentreID');
$cost = array('CreateDate' => date('Y-m-d H:i:sa'));
$result = $this->costcenter_model->deletecost($cost, $CostCenterID);
if ($result > 0) {
echo (json_encode(array('status' => TRUE)));
} else {
echo (json_encode(array('status' => FALSE)));
}
//}
}
function exportcost()
{
$exportData = $this->costcenter_model->export_excel();
$file_name = 'Cost_Master' .' '. '.xls'; //save our workbook as this file name
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->mergeCells('B3:P3');
$sheet->getStyle('G3')->getAlignment()->setHorizontal('center');
$sheet->setTitle('Cost Master');
$headers = [
'G3'=>'RESICO Cost Master',
'B5'=>'S.No','C5'=>'CostCenterCode','D5'=>'CostCenterName',
'E5'=>'CAPITAL','F5'=>'REVENUE','G5'=>'IMPORT','H5'=>'SERVICE',
'I5'=>'BudgetYear','J5'=>'DepartmentName','K5'=>'IsActive',
'L5'=>'CreatedBy','M5'=>'UpdatedBy','N5'=>'IsApproved','O5'=>'ApprovedBy'
];
foreach ($headers as $cell => $value) {
$sheet->setCellValue($cell, $value);
$sheet->getStyle($cell)->getFont()->setSize($cell == 'G3' ? 14 : 12);
$sheet->getStyle($cell)->getFont()->setBold(true);
}
if($exportData){
$i = 6;$s = 1;
foreach ($exportData as $data) {
$rowData = [
'B' . $i => $s,
'C' . $i => $data['CostCenterCode'],
'D' . $i => $data['CostCenterName'],
'E' . $i => $data['capital'],
'F' . $i => $data['revenue'],
'G' . $i => $data['import'],
'H' . $i => $data['service'],
'I' . $i => $data['BudgetYear'],
'J' . $i => $data['DepartmentName'],
'K' . $i => $data['IsActive'],
//'K' . $i => $data['Remarks'],
'L' . $i => $data['firstname'],
'M' . $i => $data['Updated_By'],
'N' . $i => $data['IsApproved'],
'O' . $i => $data['firstname']
];
foreach ($rowData as $cell => $value) {
$sheet->setCellValue($cell, $value);
}
$i++;
$s++;
}
}
// $writer = new Xlsx($spreadsheet);
// $writer->save($file_name);
// header("Content-Type: application/vnd.ms-excel");
// header('Content-Disposition: attachment; filename="' . basename($file_name) . '"');
// header('Expires: 0');
// header('Cache-Control: must-revalidate');
// header('Pragma: public');
// header('Content-Length:' . filesize($file_name));
// flush();
// readfile($file_name);
// exit;
// Clear the output buffer to prevent any other output
ob_clean();
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'. $file_name .'"');
header('Cache-Control: max-age=0');
header('Expires: 0');
header('Pragma: public');
$writer->save('php://output');
exit;
}
}