ria/app/Controllers/CostCenter.php

230 lines
7.3 KiB
PHP
Executable File

<?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 add/edit Cost
*/
public function fetchCostCenterDetails() {
$id = $this->request->getPost('id');
log_message('error', 'id: ' . $id);
$CostCenterName = $this->request->getPost('CostCenterName');
$userId = $this->session->get('userId');
$approverId = $this->session->get('EmpID');
$currentdt = get_current_date_time();
$data = ['status' => false, 'message' => 'An error occurred while creating cost details'];
if ((int)$id == 0) {
$lastccid = $this->costcenter_model->lastCCID();
log_message('error', 'LAST CodeCenterID : ' . $lastccid);
$newccid = (int)$lastccid++;
log_message('error', 'CodeCenterID with Incremented : ' . $newccid);
$newccc = generateCostCenterCode($lastccid);
log_message('error', 'newly Generated ccc : ' . $newccc);
$Cost = ['CostCenterCode' => $newccc,
'CostCenterName' => $CostCenterName,
'ApprovedBy' => $approverId,
'CreatedBy' => $userId,
'CreatedDate' => $currentdt
];
if ($this->costcenter_model->saveCostCenter($Cost) > 0) {
$data = ['status' => true, 'message' => 'Successfully created cost details'];
}
} else {
$Cost = ['CostCenterName' => $CostCenterName,'UpdatedBy' => $userId,'UpdatedDate' => $currentdt];
if ($this->costcenter_model->updateCostCenter($Cost, $id) > 0) {
$data = ['status' => true, 'message' => 'Successfully updated cost details'];
}
}
return $this->response->setJSON($data);
}
/** re activate the cost center */
public function reActivateCostCenter(){
$CostCenterCode = $_GET['CostCenterCode'];
if(!empty($CostCenterCode)){
$updatedBy = $this->session->get('userId');
$message = $this->costcenter_model->reActivateCostCenter($CostCenterCode, $updatedBy);
}else{
$message = "Cost Center Code Not Avalable";
}
$this->session->setFlashdata('success', $message);
return redirect()->route('costListing');
}
/**
* delete the Cost
*/
function deleteCostCenter($CostCode = NULL)
{
$CostCenterID = $CostCode ?? $_GET['CostCenterCode'];
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');
}
/**
* checkCostCenter
*/
function checkCostCenter(){
$data = $this->request->getPost();
$result = $this->costcenter_model->checkCostCenter( $data['CostCenterName']);
return $this->response->setJSON($result);
}
/**
* export the Cost
*/
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;
}
}