ria/app/Controllers/Rawmaterialdetails.php
2025-03-20 17:51:44 +05:30

487 lines
17 KiB
PHP
Executable File

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\Validation\Exceptions\ValidationException;
use App\Models\Rawmaterialdetails_model;
use App\Models\Config_model;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use DateTime;
/**
* Module : Material Master details
* Rawmaterialdetails Class to control all Rawmaterialdetails related operations.
* @author : Gandhimathi
* @version : 1.1
* @since : 12 Apr 2017
* @example : Revised at 15th april 2024
*/
class Rawmaterialdetails extends BaseController
{
protected $rawmaterialdetails_model;
protected $config_model;
protected $session;
/**
* This is default constructor of the class
*/
public function __construct()
{
parent::__construct();
$this->rawmaterialdetails_model = new Rawmaterialdetails_model();
$this->config_model = new Config_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()
{
$this->global['pageTitle'] = 'RawMaterialDetails';
$this->loadViews("rawmaterialListing", $this->global, NULL, NULL);
}
/**
* This function is used to load the RawMaterialDetails list
*/
function rawmaterialListing()
{
$isActiveFilter = 1 ; // default flag for active data
$showArchive = 0 ; // default flag for inactive data
if ($this->request->getMethod() === 'POST') {
$showArchive = $this->request->getPost('showArchive');
//reversing condition that when user wants deleted data (archived)to be shown , then we are making active filter 0
if($showArchive){
$isActiveFilter = 0;
}
}
$this->rawmaterialdetails_model = new rawmaterialdetails_model();
$data['userRecords'] = $this->rawmaterialdetails_model->rawmaterialListing($isActiveFilter);
$data['showArchive'] = $showArchive ;
$data['materialCategoryList']= $this->config_model->getMaterialCategoryList();
$this->global['pageTitle'] = 'RawMaterial Listing';
$this->loadViews("rawmaterialListing", $this->global, $data, NULL);
}
private function get_current_date_time() {
$datetime = new DateTime();
return $datetime->format('Y-m-d H:i:s');
}
function deleteMaterial(){
$materialId = $this->request->getPost('id');
$updatedDate = $this->get_current_date_time();
$session = \Config\Services::session();
$userId = $session->get('userId');
$info=array('IsActive' => 0 , "UpdatedBy"=>$userId , "UpdatedDate" => $updatedDate ) ;
$updated=$this->rawmaterialdetails_model->deleteMaterial($materialId,$info);
if($updated){
echo"Deleted Material successfully..!!";
}else{
echo"Deleting Material unsuccessfull ..!!";
}
}
function reActivateMaterial(){
$materialId = $this->request->getPost('id');
$updatedDate = $this->get_current_date_time();
$session = \Config\Services::session();
$userId = $session->get('userId');
$info=array('IsActive' => 1 , "UpdatedBy"=>$userId , "UpdatedDate" => $updatedDate ) ;
$updated=$this->rawmaterialdetails_model->reActivateMaterial($materialId,$info);
if($updated){
echo"Re-Activated Material successfully..!!";
}else{
echo"Re-Activated Material unsuccessfull..!!";
}
}
function shortagematerialListing()
{
$this->rawmaterialdetails_model = new rawmaterialdetails_model();;
$data['Records'] = $this->rawmaterialdetails_model->reOrderListing();
$this->global['pageTitle'] = 'ReOrderLevel Listing';
$this->loadViews("shortagematerialListing", $this->global, $data, NULL);
}
/**
* This function is used to load the add new cost */
function addRawMaterial()
{
$this->rawmaterialdetails_model = new rawmaterialdetails_model();
$data['material'] = $this->rawmaterialdetails_model->getmaterialType();
$data['materialcategory'] = $this->rawmaterialdetails_model->getmaterialCategory();
$data['UOM'] = $this->rawmaterialdetails_model->getUOM();
$data['assetcode'] = $this->rawmaterialdetails_model->getAssetcode();
$data['costcentercode'] = $this->rawmaterialdetails_model->getcostcentercode();
$data['cfUOM'] = $this->rawmaterialdetails_model->getConversionfactorUOM();
$this->global['pageTitle'] = 'AddNew RawMaterial';
$this->loadViews("addRawMaterial", $this->global, $data, NULL);
}
/* Validation for Material Type Dropdown */
function MaterialType_validate($selectValue)
{
//echo 'select_validate method called';
//'none' is the first option and the text says something like "-Choose one-"
if ($selectValue == '-1') {
// $this->validator->setMessage('MaterialType_validate', 'Please Select Material Type.');
echo "<script>alert('Please Select Material Type.');</script>";
return false;
} else // user picked something
{
return true;
}
}
/* Validation for Material Type Dropdown */
function UOM_validate($selectValue)
{
//echo 'select_validate method called';
// 'none' is the first option and the text says something like "-Choose one-"
if ($selectValue == '-1') {
// $this->validator->setMessage('UOM_validate', 'Please Select UOM.');
echo "<script>alert('Please Select UOM.');</script>";
return false;
} else // user picked something
{
return true;
}
}
/* Validation for Material Category Dropdown */
function materialcategory_validate($selectValue)
{
if ($selectValue == '0') {
// $this->validator->setMessage('materialcategory_validate', 'Please Select Material Category.');
echo "<script>alert('Please Select Material Category.');</script>";
return false;
} else {
return true;
}
}
function addNewRawMaterial()
{
$TempArr = [];
$MaterialName = $this->request->getPost('MaterialName');
$MaterialType = $this->request->getPost('MaterialType');
$UOM = $this->request->getPost('UOM');
$MaterialCategory = $this->request->getPost('MatCate');
$Remarks = $this->request->getPost('remarks');
$HSN = $this->request->getPost('HSNcode');
$CreatedBy = $this->session->get('userId');
$stock = $this->request->getPost('openstock');
$reorder = $this->request->getPost('reorder');
$stockdate = $this->request->getPost('Date');
$currentstock = '';
$today_dt = get_current_date();
if (empty($stockdate)) {
// $stockdate = NULL;
$stockdate = get_current_date();
$currentstock = $stock;
} else {
$stockdate = format_date($stockdate,0,"Y-m-d");
$todaydateyear = format_date($today_dt,0,"Y");
$openstockdateyear = format_date($stockdate,0,"Y");
$parts = explode('-', $stockdate);
$openstockdatemonth = $parts[1];
$openstockdatedate = $parts[2];
if ($openstockdateyear == $todaydateyear) {
if ($openstockdatemonth <= 03) {
$currentstock = 0;
} else {
$currentstock = $stock;
}
} else if ($openstockdateyear < $todaydateyear) {
//$total= $y;
$currentstock = 0;
} else if ($openstockdateyear > $todaydateyear) {
$currentstock = $stock;
}
}
$HSNcode = ($HSN == '') ? null : $HSN;
$material_rate = $this->request->getPost('material_rate');
$RawMaterial = array('MaterialName' => $MaterialName, 'MaterialType' => $MaterialType, 'UOM' => $UOM, 'Category' => $MaterialCategory, 'CreatedBy' => $CreatedBy, 'Remarks' => $Remarks, 'HSNCODE' => $HSNcode, 'stock' => $stock, 'stockdate' => $stockdate, 'reorder' => $reorder, 'Current_stock' => $currentstock, 'material_rate'=>$material_rate);
//print_r($RawMaterial);die();
$result = $this->rawmaterialdetails_model->addNewRawMaterial($RawMaterial);
if ($result > 0) {
$this->session->setFlashdata('success', 'RawMaterial Created successfully!');
} else {
$this->session->setFlashdata('error', 'RawMaterial Not Created!');
}
return redirect()->route('rawmaterialListing');
}
function viewRawmaterial($RID = '')
{
$MaterialType = '';
$UOM = '';
$MaterialCategory = '';
if ($RID == '') {
$RawMaterialID = $_GET['RID'];
$MaterialType = $_GET['MType'];
$UOM = $_GET['UOM'];
$currentdate = $_GET['date1'];
} else {
$RawMaterialID = $RID;
}
$data['materialDetails'] = $this->rawmaterialdetails_model->getMaterialDetails($RawMaterialID);
$data['material'] = $this->rawmaterialdetails_model->getmaterialType();
$data['currentstock'] = $this->rawmaterialdetails_model->getMaterialstock($RawMaterialID, $currentdate);
$data['materialType'] = $this->rawmaterialdetails_model->getmaterialType($MaterialType);
$data['materialCategory'] = $this->rawmaterialdetails_model->getmaterialCategory($MaterialCategory);
$data['materialCategoryList']= $this->config_model->getMaterialCategoryList(); // Told by Pavithra
$data['UOMList'] = $this->rawmaterialdetails_model->getUOM($UOM);
$data['costcentercode'] = $this->rawmaterialdetails_model->getCostCenterCode();
$data['cfUOM'] = $this->rawmaterialdetails_model->getConversionfactorUOM();
$data['assetcode'] = $this->rawmaterialdetails_model->getAssetcode();
$data['avg_price'] = $this->rawmaterialdetails_model->getMaterialAvg($RawMaterialID);
$data['materialRateHistory'] = $this->rawmaterialdetails_model->getMaterialRateHistory($RawMaterialID);
//print_r($data['avg_price']);die();
$this->global['pageTitle'] = 'Edit Material Master';
$this->loadViews("editRawmaterial", $this->global, $data, NULL);
}
/**
* This function is used to edit the user information
*/
function editRawmaterial()
{
$RawmaterialID = $this->request->getPost('MaterialCode');
$MaterialCode = $this->request->getPost('MaterialCode');
$MaterialName = $this->request->getPost('MaterialName');
$MaterialType = $this->request->getPost('MaterialType');
$MaterialCategory = $this->request->getPost('MaterialCategory');
$material_rate = $this->request->getPost('material_rate');
$UOM = $this->request->getPost('UOM');
$Remarks = $this->request->getPost('remarks');
$HSN = $this->request->getPost('HSNcode');
$UpdatedBy = $this->session->get('userId');
$stock = $this->request->getPost('openstock');
$reorder = $this->request->getPost('reorder');
$stockdate = $this->request->getPost('Date');
$stockdate = (empty($stockdate)) ? NULL : get_date_time_dynamical_format('d-m-Y','Y-m-d',"$stockdate");
$chked = $this->request->getPost('isactive');
$IsActive = ($chked != '') ? '1' : '0';
$HSNcode = ($HSN == '') ? null : $HSN;
$RawMaterial = array();
$RawMaterial = array('MaterialName' => $MaterialName, 'MaterialType' => $MaterialType, 'UpdatedBy' => $UpdatedBy, 'UOM' => $UOM, 'IsActive' => $IsActive, 'Category' => $MaterialCategory, 'Remarks' => $Remarks, 'HSNCODE' => $HSNcode, 'stock' => $stock, 'stockdate' => $stockdate, 'reorder' => $reorder,'material_rate'=>$material_rate); //,'RMCode'=>$rmcode);
// print_r($RawMaterial);die;
$result = $this->rawmaterialdetails_model->editRawmaterial($RawMaterial, $MaterialCode);
if ($result == true) {
$this->session->setFlashdata('success', 'RawMaterial Updated successfully!');
} else {
echo "<script>alert('RawMaterial Record Not updated!');</script>";
$this->session->setFlashdata('error', 'RawMaterial Record Not updated!');
}
return redirect()->route('rawmaterialListing');
}
// on 28/jan/2025. both add and edit. category condition added.
function checkmaterial()
{
$data = $this->request->getPost();
$materialCode = $data['materialCode'] ?? '';
$materialName = str_replace(' ', '', $data['materialName']);
$materialCategory = $data['materialCategory'] ?? '';
$query = $this->rawmaterialdetails_model->checkMaterialExists($materialCode , $materialName , $materialCategory);
return $this->response->setJSON($query);
// if (!empty($query)) {
// foreach ((array)$query as $q) {
// $text .= $q['MaterialName'] . " " . ($q['IsActive'] == 1 ? '' : ' ( Inactive ) ') . "<br>";
// }
// }
}
function exportmaterial()
{
$exportData = $this->rawmaterialdetails_model->export_excel();
$file_name = 'MaterialMaster' .' '. '.xls'; //save our workbook as this file name
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->mergeCells('F3:M3');
$sheet->getStyle('F3')->getAlignment()->setHorizontal('center');
$sheet->setTitle('Material Master');
$headers = [
'F3'=>'RESICO Material Master',
'C5'=>'MaterialCode',
'D5'=>'MaterialName',
'E5'=>'MaterialType',
'F5'=>'UOM',
'G5'=>'RMCode',
'H5'=>'Category',
'I5'=>'assetcode',
'J5'=>'CostCenterCode',
'K5'=>'ConversionFactorUnit',
'L5'=>'ConversionFactorUOM',
'M5'=>'HSNCode',
'N5'=>'Remarks',
'O5'=>'Created by',
'P5'=>'Updated By',
'Q5'=>'IsActive'
];
foreach ($headers as $cell => $value) {
$sheet->setCellValue($cell, $value);
$sheet->getStyle($cell)->getFont()->setSize($cell == 'F3' ? 14 : 12);
$sheet->getStyle($cell)->getFont()->setBold(true);
}
if($exportData){
$i = 6;$s = 1;
foreach ($exportData as $data) {
$rowData = [
'C' . $i=>$data['MaterialCode'],
'D' . $i=>$data['MaterialName'],
'E' . $i=>$data['MaterialType'],
'F' . $i=>$data['UOM'],
'G' . $i=>$data['RMCode'],
'H' . $i=>$data['Category'],
'I' . $i=>$data['assetcode'],
'J' . $i=>$data['CostCenterCode'],
'K' . $i=>$data['ConversionFactorUnit'],
'L' . $i=>$data['ConversionFactorUOM'],
'M' . $i=>$data['HSNCODE'],
'N' . $i=>$data['Remarks'],
'O' . $i=>$data['firstname'],
'P' . $i=>$data['Updated_By'],
'Q' . $i=>$data['IsActive']
];
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;
}
function materialCodesConfigCategory (){
$configKey = $this->request->getPost('configKey');
//need to get the config key from the post data
$result = $this->rawmaterialdetails_model->materialCodesConfigCategory($configKey);
return $this->response->setJSON($result);
}
}