ria/app/Models/Requistion_model.php
2025-04-25 07:37:06 +00:00

604 lines
20 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
class Requistion_model extends Model
{
/**
* This function is used to get the SupplierID and SupplierName information
* @return array $result : This is result of the query
*/
function getSupplierName()
{
$builder = $this->db->table('t_supplierdetailsn')
->select('SupplierID, SupplierName,ADeleteRequistionLineItemddress');
// ->where('roleId !=', 1);
$query = $builder->get();
return $query->getResult();
}
/**
* This function is used to get the userId information
* @return array $result : This is result of the query
*/
function getusers()
{
$builder = $this->db->table('tbl_users')
->select('userId, name');
//->where('roleId !=', 1);
$query = $builder->get();
return $query->getResult();
}
/* This function is used to getEditRequistItemList view the information
* @return array $result : This is result of the query
*/
// don't forgot to check this 13th april 2024.
function getEditRequistItemList($ReqNo)
{
$result = $this->db->query("CALL P_GET_REQUISTITEMLIST('".$ReqNo."')");
//echo 'REQUISTITEMLIST Line item view successfully';
// $result->next_result();
if($result->getNumRows() > 0)
{
return $result->getResult();
}
/* $subQuery ='select distinct Req.ReqNo, Req.MaterialCode,Mat.MaterialName,Mat.UOM, Req.Quantity,
ifnull(Po.Quantity,0) as POQTY,sta.StatusName
from t_requestion_details Req join t_materialmaster Mat on
Req.MaterialCode = Mat.MaterialCode
left join t_purchaseorder_lineitem Po on Po.ReqNo = Req.ReqNo
left join t_purchaseorder_master POmast on POmast.PONO=Po.PONO
and Po.MaterialCode = Req.MaterialCode
join t_status sta on sta.StatusCode = Req.Status
where Req.ReqNo = ? and POmast.Status != ?';
$query = $this->db->query($subQuery, [$ReqNo,PO_AMENDED));
return $query->getResult(); */
}
/**
* This function is used to get the userId information
* @return array $result : This is result of the query
*/
function getStatus()
{
$builder = $this->db->table('t_status')
->select('StatusCode,StatusName')
->where('StatusType', REQUISTSTATUS)
->where('StatusCode !=', REQ_DRAFT);
$query = $builder->get();
return $query->getResult();
}
/* This function is used to get the Raw Material information
* @return array $result : This is result of the query
*/
function getRequistionList($ReqNo, $IsArray = '')
{
$result = $this->db->query("CALL P_GET_AllREQUISTION('".$ReqNo."')");
if (!$result) {
return [];
}
if ($IsArray != '') {
$data = $result->getResultArray(); // Fetch as an array
return array_reverse($data); // Reverse the array and return
} else {
$data = $result->getResult(); // Fetch as an object array
return array_reverse($data); // Reverse the array and return
}
}
/**
* This function is used to get Employee details based on Employee ID Selection
* @return array $result : This is result of the query
*/
function GetEmployeeDetails($EmpID)
{
$builder = $this->db->table('t_employee_details EMP')
->select(' EMP.EmpID,EMP.Designation,DEPT.DEPCode,DEPT.DepartmentName')
->join('t_departmentdetails DEPT', 'EMP.Departmentcode = DEPT.DEPCode')
->where('EMP.EmpID ',$EmpID );
$query = $builder->get();
return $query->getResultArray();
}
/**
* This function is used to get the Cost Center details for the Login user department
* @return array $result : This is result of the query
*/
function GetCostCenterByDept($DeptId)
{
$builder = $this->db->table('t_costcenter_master Mast ')
->select('Mast.CostCenterCode,Mast.CostCenterName')
->where('Mast.IsActive ',1 );
// ->join('t_costcenter_departments COST', 'COST.CostCenterCode = Mast.CostCenterCode','left')
// ->where('COST.DEPCode ',$DeptId );
$query = $builder->get();
return $query->getResultArray();
}
/**
* This function is used to get the Available BudgetAmount for the Cost center
* @return array $result : This is result of the query
*/
function GetAvailableBudgetAmount($CostCode,$Year,$BudgetType)
{
$builder = $this->db->table('t_costcenter_budget')
->select('BudgetAmount')
->where('CostCenterCode',$CostCode )
->where('BudgetYear',$Year )
->where('BudgetType',$BudgetType );
$query = $builder->get();
return $query->getResultArray();
}
// This function also called in store req. also $reqType not removed.....
function EditMaterialCode($ReqNo, $ReqType = NULL){
$subQuery = 'SELECT Mat.MaterialCode, Mat.MaterialName, Mat.UOM,Mat.HSNCODE FROM t_materialmaster Mat
WHERE Mat.MaterialCode NOT IN (
SELECT Req.MaterialCode FROM t_requestion_details Req
JOIN t_requestion_master mast ON Req.ReqNo = mast.ReqNo
WHERE mast.ReqNo = ?' . ($ReqType ? ' AND mast.ReqType = ?' : '') .')'
. ($ReqType ? ' AND Mat.MaterialType = ?' : '');
$params = $ReqType ? [$ReqNo, $ReqType, $ReqType] : [$ReqNo];
$query = $this->db->query($subQuery, $params);
return $query->getResult();
}
/**
* This function is used to get the Material details for the Request type
* @return array $result : This is result of the query
*/
function GetMaterialCode($ReqType,$NotArray='')
{
$builder = $this->db->table('t_materialmaster')
->select('MaterialCode, MaterialName,UOM,material_rate,HSNCODE')
->where('IsActive',1);
//->where('MaterialType',$ReqType );
$query = $builder->get();
if($NotArray == '')
{
return $query->getResultArray();
}
else
{
return $query->getResult();
}
}
/**
* This function is used to get the Raw Material information
* @return array $result : This is result of the query
*/
function getRawMaterialList($MaterialCode)
{
$builder = $this->db->table('t_materialmaster MM')
->select('MM.MaterialName,MM.UOM,MM.Category,CD.ConfigValue as CategoryName,MM.HSNCODE')
->join('t_configdetails CD', 'CD.Key = MM.Category', 'left')
->where('MM.MaterialCode',$MaterialCode );
$query = $builder->get();
return $query->getResultArray();
}
/**
* This function is used to get the Config value from configuration table
* @return array $result : This is result of the query
*/
function getConfigValue($ConfigID ,$ConfigValue = '')
{
$builder = $this->db->table('t_configdetails')
->select('ConfigValue')
->where('Config_id ',$ConfigID );
if($ConfigValue != '')
{
$builder->where('ConfigValue != ',$ConfigValue );
}
$query = $builder->get();
return $query->getResult();
}
/**
* This function is used to get the All employees
* @return array $result : This is result of the query
*/
function getAllEmployees()
{
$builder = $this->db->table('t_employee_details')
->select('EmpID,FirstName,LastName');
$query = $builder->get();
return $query->getResult();
}
/**
* This function is used to get the Employee Details
* @return array $result : This is result of the query
*/
function getEmpIdByUserID($UserID)
{
$subQuery ='SELECT EMP.EmpID,EMP.FirstName,EMP.Designation,DEPT.DEPCode,DEPT.DepartmentName
FROM t_employee_details EMP JOIN t_departmentdetails DEPT ON
EMP.Departmentcode = DEPT.DEPCode
WHERE EMP.EmpID=(select EmpID from tbl_users where userId=?)';
$query = $this->db->query($subQuery, [$UserID]);
return $query->getResult();
}
/**
* This function is used to get the CostCenter for the Selected Employee
* @return array $result : This is result of the query
*/
function getCostCenterUserID($UserID)
{
// $subQuery ='SELECT Mast.CostCenterCode,Mast.CostCenterName from t_costcenter_master Mast
// LEFT JOIN t_costcenter_departments COST ON COST.CostCenterCode = Mast.CostCenterCode
// WHERE COST.DEPCode=(select Departmentcode from t_employee_details where EmpID = (select EmpID from tbl_users where userId=?))';
$subQuery ='SELECT Mast.CostCenterCode,Mast.CostCenterName from t_costcenter_master Mast where Mast.IsActive = 1';
$query = $this->db->query($subQuery, [$UserID]);
return $query->getResult();
}
/**
* This function is used to get the Purchase order Type
* @return array $result : This is result of the query
*/
function CheckDraftStatus($UserID)
{
$subQuery = 'select count(ReqNo) as cnt,ReqNo from t_requestion_master where
Requestedby= ( select EmpID from tbl_users where userId=?) and status=?';
$query = $this->db->query($subQuery, [$UserID,REQ_DRAFT]);
return $query->getResult();
}
/**
* This function is used to get the Requsition List for the login user
* @return array $result : This is result of the query
*/
function getRequistDetails($ReqID,$NoArray='')
{
$subQuery = 'select distinct ReqNo,ReqType,Status,Requestedby,DATE_FORMAT(mast.CreatedDate, "%d-%m-%Y") as CreatedDate,Schedule_Type,NumberOfService,Service_Period ,emp.FirstName,emp.EmpID,emp.Designation, Dept.DEPCode,Dept.DepartmentName,DATE_FORMAT(mast.ReqDate, "%d-%m-%Y") as ReqDate,mast.CostCenterCode,Cost.CostCenterName
from t_requestion_master mast
left join t_costcenter_master Cost on Cost.CostCenterCode = mast.CostCenterCode
join t_employee_details emp on mast.Requestedby = emp.EmpID
join t_departmentdetails Dept on emp.Departmentcode = Dept.DEPCode
where ReqNo=?';
$query = $this->db->query($subQuery, [$ReqID]);
if($NoArray == '')
{
return $query->getResultArray();
}
else
{
return $query->getResult();
}
}
/**
* This function is used to get the Requsition List for the login user
* @return array $result : This is result of the query
*/
function getRequistListUserID($UserID)
{
$subQuery = 'select ReqNo, ReqType ,Status,CreatedDate ,StatusName from t_requestion_master mast
join t_status st on st.StatusCode= mast.Status where
Requestedby = ( select EmpID from tbl_users where userId=?) and Status != ? ';
$query = $this->db->query($subQuery, [$UserID,REQ_DELETED]);
return $query->getResult();
}
/**
* This function is used to get the Requsition List for the login user
* @return array $result : This is result of the query
*/
function getRequistItemList($ReqID,$NoArray='')
{
/* $subQuery = 'select distinct Req.ReqNo, Mat.MaterialCode,Mat.MaterialName,Mat.UOM, Req.Quantity,sum(Po.Quantity) as POQTY,sta.StatusName from t_materialmaster Mat
left join t_requestion_details Req on Req.MaterialCode = Mat.MaterialCode
left join t_purchaseorder_lineitem Po on Po.ReqNo = Req.ReqNo and Po.MaterialCode = Req.MaterialCode
left join t_status sta on sta.StatusCode = Req.Status where Req.ReqNo=? group by Po.ReqNo,Req.MaterialCode';
$query = $this->db->query($subQuery, [$ReqID)); */
// $result = $this->db-> query("CALL P_GET_REQUISTITEMLIST('".$ReqID."')") ; or die(mysql_error());
// $result ->next_result();
$result = $this->db->query("CALL P_GET_REQUISTITEMLIST('".$ReqID."')");
if ($result->getNumRows() > 0) {
if ($NoArray == '') {
return $result->getResultArray();
} else {
return $result->getResult();
}
}
}
/**
* This function is used to add the Requistion into the Database
* @return array $result : This is result of the query
*/
// function addRequistion($Requistion)
// {
// $this->db->transStart();
// $builder = $this->db->table('t_requestion_master');
// $builder->insert($Requistion);
// $insert_id = $this->db->affectedRows();
// $this->db->transComplete();
// if($insert_id>0)
// {
// $subQuery = 'select max(ReqNo) as ReqNo from t_requestion_master';
// $query = $this->db->query($subQuery);
// return $query->getResultArray();
// }
function addRequisition($Requisition) {
$result = $this->db->table('t_requestion_master')->insert($Requisition);
$insert_id = $this->db->insertID();
$affected_rows = $this->db->affectedRows();
if ($affected_rows > 0) {
$subQuery = 'SELECT ReqNo FROM t_requestion_master ORDER BY CreatedDate DESC LIMIT 1';
$query = $this->db->query($subQuery);
log_message('error', 'Added new Requisition: ReqID '.$insert_id);
return $query->getResultArray();
} else {
return [];
}
}
/**
* This function is used to check the Request is in Draft status
* @return array $result : This is result of the query
*/
function RequistionIsExists($ReqBy)
{
$builder = $this->db->table('t_requestion_master')
->select('ReqNo')
->where('Status ',REQ_DRAFT )
->where('Requestedby ',$ReqBy );
$query = $builder->get();
if ($query->getNumRows() > 0) {
return $query->getResultArray();
}else{
return [];
}
}
/**
* This function is used to check the Request Line Item is in Draft status
* @return array $result : This is result of the query
*/
function LineItemIsExists($ReqNo,$MaterialCode)
{
$builder = $this->db->table('t_requestion_details')
->select('ReqNo')
->where('ReqNo ',$ReqNo )
->where('MaterialCode ',$MaterialCode );
$query = $builder->get();
if ($query->getNumRows() > 0) {
//echo $result->getNumRows();
return $query->getResultArray();
}
}
/**
* This function is used to add the Line item details to the Database
* @return array $result : This is result of the query
*/
function addRequistionDetails($Requistion,$RegNo='')
{
$this->db->transStart();
$builder = $this->db->table('t_requestion_details');
$builder->insert($Requistion);
$insert_id = $this->db->affectedRows();
$this->db->transComplete();
$subQuery = 'select max(ItemNo) as ItemNo from t_requestion_details where ReqNo=?';
$query = $this->db->query($subQuery,[$RegNo]);
return $query->getResultArray();
}
function getItemNumber($ReqNo,$MaterialCode)
{
$subQuery = 'select ItemNo from t_requestion_details where ReqNo=? and MaterialCode=?';
$query = $this->db->query($subQuery,[$ReqNo,$MaterialCode]);
return $query->getResultArray();
}
/**
* This function is used to delete the Line item from details into Database
* @return array $result : This is result of the query
*/
function DeleteRequistionLineItem( $ReqNo,$MaterialCode)
{
$this->db->table('t_requestion_details')->delete(['ReqNo' => $ReqNo,'MaterialCode'=>$MaterialCode]);
return TRUE;
}
/**
* This function is used to update the details into Database
* @return array $result : This is result of the query
*/
function UpdateRequistionLineItem($Requist, $ReqNo,$MaterialCode)
{
$this->db->table('t_requestion_details')
->where('ReqNo', $ReqNo)
->where('MaterialCode', $MaterialCode)
->update($Requist);
return TRUE;
}
function UpdateAvlQty($BalanceQuantity,$MaterialCode)
{
$this->db->table('t_store_stockdetails')
->where('MaterialCode',$MaterialCode)
->update($BalanceQuantity);
return TRUE;
}
/**
* This function is used to update the details into Database
* @return array $result : This is result of the query
*/
function UpdateRequistion($Requistion, $ReqNo)
{
$this->db->table('t_requestion_master')
->where('ReqNo', $ReqNo)
->update($Requistion);
return TRUE;
}
/**
* This function is used to get the List of Requist details to the approver
* @return array $result : This is result of the query
*/
function getApproverRequistList($UserID,$Status = '',$FromDate='' ,$ToDate='')
{
// echo 'UserId: '.$UserID . 'From: '.$FromDate .'TO: '. $ToDate. 'Status: '. $Status;
$subQuery = 'select distinct ReqNo,Status,mast.CreatedDate,Requestedby,emp.FirstName,mast.Comments,st.StatusName
from t_requestion_master mast join t_employee_details emp on mast.Requestedby = emp.EmpID
join t_departmentdetails Dept on emp.Departmentcode = Dept.DEPCode
join t_status st on st.StatusCode = mast.Status
where status not in(?,?,?) and emp.Departmentcode in (select DEPCode from t_departmentdetails where HeadDept=
(select HeadDept from t_departmentdetails where DEPCode = (select Departmentcode from t_employee_details where
EmpID=(select EmpID from tbl_users where userId=?))))';
$subQuery1 = $subQuery;
$order_by = ' order by mast.CreatedDate desc';
$query ='';
if($FromDate != '' && $ToDate != '' && $Status !='' )
{
$Where = 'and mast.CreatedDate >= ? and mast.CreatedDate <= ? and Status=?';
$subQuery1 .= $Where;
$subQuery1 .= $order_by;
$query = $this->db->query($subQuery1, [REQ_DRAFT,REQ_DELETED,REQ_REJECTED,$UserID,$FromDate,$ToDate,$Status]);
}
else if($FromDate != '' && $ToDate != '' && $Status =='')
{
$Where = 'and mast.CreatedDate >= ? and mast.CreatedDate <= ? ';
$subQuery1 .= $Where;
$subQuery1 .= $order_by;
$query = $this->db->query($subQuery1, [REQ_DRAFT,REQ_DELETED,REQ_REJECTED,$UserID,$FromDate,$ToDate]);
}
else if($Status != '' && $FromDate == '' && $ToDate == '')
{
$Where = 'and Status = ?';
$subQuery1 .= $Where;
$subQuery1 .= $order_by;
$query = $this->db->query($subQuery1, [REQ_DRAFT,REQ_DELETED,REQ_REJECTED,$UserID,$Status]);
}
else
{
$subQuery .= $order_by;
// $query = $this->db->query($subQuery, [REQ_DRAFT,REQ_DELETED,REQ_REJECTED,$UserID]);
$query = $this->db->query($subQuery, [REQ_DELETED,REQ_REJECTED,$UserID]);
}
return $query->getResult();
}
function getRequestBy(){
$subQuery ='SELECT CONCAT_WS(" ",ted.FirstName,ted.LastName) as Full_Name ,tdd.DepartmentName, ted.EmpID
from t_employee_details as ted
left join t_departmentdetails as tdd on tdd.DEPCode = ted.Departmentcode
where ted.Designation = "Admin Head" and ted.IsActive = 1';
$query = $this->db->query($subQuery);
return $query->getResult();
}
function getCategoryName($materialCode){
$subQuery ='SELECT MM.MaterialCode,MM.MaterialName,MM.MaterialType,MM.Category,MM.IsActive,CD.ConfigValue
from t_materialmaster as MM
left join t_configdetails as CD on CD.Key = MM.Category
where MM.MaterialCode = '.$materialCode;
$categoryName = $this->db->query($subQuery)->getRow()->ConfigValue;
return $categoryName;
}
function getmaterialCategory()
{
$Config_ID = 'C023';
$builder = $this->db->table('t_configdetails')
->select('Key,Config_ID,ConfigValue')
->where('isActive', 1)
->where('Config_id', $Config_ID);
$query = $builder->get();
return $query->getResultArray();
}
}