ria/app/Controllers/Department.php
2025-03-04 11:37:42 +05:30

276 lines
8.2 KiB
PHP
Executable File

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\Validation\Exceptions\ValidationException;
use App\Models\Department_model;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
/**
* Module : Department
* Department Class to control all department related operations.
* @author : Gandhimathi
* @version : 1.1
* @since : 12 Apr 2017
* @example : Revised at 15th april 2024
*/
class Department extends BaseController
{
protected $department_model;
protected $session;
/**
* This is default constructor of the class
*/
public function __construct()
{
parent::__construct();
$this->department_model = new Department_model();
$this->session = session();
helper('form'); //$this->load->library('form_validation');
// $this->load->library('pagination');
// $this->load->library('Excel');
$this->isLoggedIn();
}
/**
* This function used to load the first screen of the user
*/
public function index()
{
$isActiveFilter = 1 ; // default showing active data
$showArchive = 0 ; //
if ($this->request->getMethod() === 'POST') {
$showArchive = $this->request->getPost('showArchive');
//reversing condition that when user wants deleted data to be shown then we are making active filter 0
if($showArchive){
$isActiveFilter = 0;
}
}
$data['showArchive'] = $showArchive;
$this->department_model = new department_model();
$data['userRecords'] = $this->department_model->departmentListing($isActiveFilter); //$searchText, $returns["page"], $returns["segment"]);
$this->global['pageTitle'] = 'Department Listing';
$this->loadViews("departmentListing", $this->global, $data, NULL);
//}
}
/**
* This function is used to load the add new supplier */
function adddepartment()
{
//$data['payment'] = $this->department_model->getpayment();
//$data['depcode'] = $this->department_model->getdepcode();
$data['depcode'] = $this->department_model->getdepcode();
$this->global['pageTitle'] = 'AddNew Department';
$this->loadViews("adddepartment", $this->global, $data, NULL);
}
function savedepartment()
{
$DepartmentName = $this->request->getPost('DepartmentName');
$DEPCode = $this->request->getPost('DEPCode');
$HeadDept = $this->request->getPost('HeadDept');
$Createdby = $this->session->get('userId');
$chked = $this->request->getPost('IsActive');
if ($chked != '') {
$IsActive = '1';
} else {
$IsActive = '0';
}
$department = array('DepartmentName' => $DepartmentName, 'DEPCode' => $DEPCode, 'HeadDept' => $HeadDept, 'Createdby' => $Createdby, 'IsActive' => $IsActive);
$result = $this->department_model->savedepartment($department);
if ($result > 0) {
$this->session->setFlashdata('success', 'Department Created successfully!');
}else{
$this->session->setFlashdata('error', 'Department Record Not Created!');
}
return redirect()->route('departmentListing');
}
/**
* This function is used to edit the department information
*/
function editdepartment()
{
$DepartmentName = $this->request->getPost('DepartmentName');
$DEPCode = $this->request->getPost('DEPCode');
$HeadDept = $this->request->getPost('HeadDept');
$Createdby = $this->session->get('userId');
$chked = $this->request->getPost('IsActive');
if ($chked != '') {
$IsActive = '1';
} else {
$IsActive = '0';
}
$department = array('DepartmentName' => $DepartmentName, 'DEPCode' => $DEPCode, 'HeadDept' => $HeadDept, 'IsActive' => $IsActive);
$result = $this->department_model->Updatedepartment($department, $DEPCode);
if ($result == True) {
$this->session->setFlashdata('success', 'Department Record Successfully Updated!');
}else{
$this->session->setFlashdata('error', 'Department Record Not Updated!');
}
return redirect()->route('departmentListing');
}
function reActivateDepartment(){
$depCode = $this->request->getGet('SID');
$updateInfo = ['IsActive' => 1];
$result = $this->department_model->reActivateDepartment($depCode , $updateInfo);
return redirect()->route('departmentListing');
}
function deleteDepartment(){
$depCode = $this->request->getGet('SID');
$updateInfo = ['IsActive' => 0];
$result = $this->department_model->reActivateDepartment($depCode , $updateInfo);
return redirect()->route('departmentListing');
}
function viewdepartment($SID = '')
{
//print_r('hello');die();
if ($SID == '') {
$DepartmentID = $_GET['SID'];
} else {
$DepartmentID = $SID;
}
$data['department'] = $this->department_model->getDeptInfo($DepartmentID);
$data['depcode'] = $this->department_model->getdepcode();
$this->global['pageTitle'] = 'Edit Department';
$this->loadViews("editdepartment", $this->global, $data, NULL);
}
function exportdepartment()
{
$exportData = $this->department_model->export_excel();
$file_name = 'Department_Details' .' '. '.xls'; //save our workbook as this file name
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Department Details');
$headers = [
'A1'=>'Department ID','B1'=>'Department Name','C1'=>'Head Deptment ID','D1'=>'Is Active','E1'=>'Created By','F1'=>'Created Date'
];
foreach ($headers as $cell => $value) {
$sheet->setCellValue($cell, $value);
$sheet->getStyle($cell)->getFont()->setSize(12);
$sheet->getStyle($cell)->getFont()->setBold(true);
}
if($exportData){
$i = 6;
foreach ($exportData as $data) {
$rowData = [
'A' . $i=>$data['DEPCode'],
'B' . $i=>$data['DepartmentName'],
'C' . $i=>$data['HeadDept'],
'D' . $i=>$data['firstname'],
'E' . $i=>$data['CreatedDt'],
'F' . $i=>$data['IsActive']
];
foreach ($rowData as $cell => $value) {
$sheet->setCellValue($cell, $value);
}
$i++;
}
}
// $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;
}
function checkDepartmentName(){
$data = $this->request->getPost();
$departmentName = str_replace(" ","",$data['departmentName']);
$depCode = $data['depCode'];
$headDepCode =$data['headDepCode'];
$departmentName = $data['departmentName'];
$departmentName = str_replace(" ","",$departmentName);
$result = $this->department_model->checkDepartmentName($departmentName , $headDepCode , $depCode);
return $this->response->setJSON($result);
}
}