siddharth_application/application/models/batchcard_model.php

1689 lines
52 KiB
PHP

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class batchcard_model extends CI_Model
{
function getEmplistforReport()
{
$this->db->select('T_Employee_Details.EmpID,T_Employee_Details.FirstName,T_Employee_Details.LastName,T_Employee_Details.Departmentcode,T_DepartmentDetails.shortName');
$this->db->from('T_Employee_Details');
$this->db->join('T_DepartmentDetails','T_Employee_Details.Departmentcode=T_DepartmentDetails.DEPCode');
//$this->db->join('ip_invoice_items','ip_invoices.invoice_number=ip_invoice_items.invoice_number');
//$this->db->join('ip_products','ip_invoice_items.item_product_id=ip_products.product_id');
//$this->db->join('ip_invoice_custom','ip_invoices.invoice_number=ip_invoice_custom.invoice_number and invoice_custom_fieldid=62');
$this->db->order_by('T_Employee_Details.EmpID');
$res = $this->db->get();
return $res->result();
}
function getSupplierlist(){
$this->db->select('SupplierName,SupplierID');
$this->db->from('T_SupplierDetailsN');
$supp = $this->db->get();
return $supp->result();
}
function Rawsupplier()
{
$this->db->select('mas.SupplierID,sup.SupplierName,mmas.MaterialCode,mmas.MaterialName');
$this->db->from('T_PurchaseOrder_Master mas');
$this->db->join('T_SupplierDetailsN sup','sup.SupplierID=mas.SupplierID');
$this->db->join('T_PurchaseOrder_LineItem line','line.PONO=mas.PONO');
$this->db->join('T_MaterialMaster mmas','mmas.MaterialCode=line.MaterialCode');
$this->db->where('Category','Raw Material');
$this->db->group_by('SupplierID');
$supp = $this->db->get();
// print_r($this->db->last_query());die();
return $supp->result();
}
function Consupplier()
{
$this->db->select('mas.SupplierID,sup.SupplierName');
$this->db->from('T_PurchaseOrder_Master mas');
$this->db->join('T_SupplierDetailsN sup','sup.SupplierID=mas.SupplierID');
$this->db->join('T_PurchaseOrder_LineItem line','line.PONO=mas.PONO');
$this->db->join('T_MaterialMaster mmas','mmas.MaterialCode=line.MaterialCode');
$this->db->where('Category','Consumables');
$this->db->group_by('SupplierID');
$supp = $this->db->get();
return $supp->result();
}
function Pacsupplier()
{
$this->db->select('mas.SupplierID,sup.SupplierName');
$this->db->from('T_PurchaseOrder_Master mas');
$this->db->join('T_SupplierDetailsN sup','sup.SupplierID=mas.SupplierID');
$this->db->join('T_PurchaseOrder_LineItem line','line.PONO=mas.PONO');
$this->db->join('T_MaterialMaster mmas','mmas.MaterialCode=line.MaterialCode');
$this->db->where('Category','Packing material');
$this->db->group_by('SupplierID');
$supp = $this->db->get();
return $supp->result();
}
function getStockValue()
{
$this->db->select('MaterialCode,stock,stockdate');
$this->db->from('T_MaterialMaster');
$supp = $this->db->get();
return $supp->result();
}
function saveHourlyBatchInfo($datatostore)
{
$this->db->insert('T_hourly_batch_info',$datatostore);
$res = $this->db->affected_rows();
return $res;
}
function saveDailyBatchCardData($datatostore)
{
$this->db->insert('T_daily_batchcard_data',$datatostore);
$res = $this->db->affected_rows();
return $res;
}
function updateHourlyBatchInfo($datatostore,$hid)
{
$this->db->where('hourlyid',$hid);
$this->db->update('T_hourly_batch_info',$datatostore);
$res = $this->db->affected_rows();
return $res;
}
function getAllHourlyData($hid='',$from='',$to='')
{
//echo "Model".$from.'-'.$to;
$this->db->select('T_hourly_batch_info.*,T_Employee_Details.FirstName as F,T_Employee_Details.LastName as L');
$this->db->from('T_hourly_batch_info');
$this->db->join('tbl_users','T_hourly_batch_info.checkedby=tbl_users.userId');
$this->db->join('T_Employee_Details','tbl_users.EmpID=T_Employee_Details.EmpID');
if(!empty($hid))
{
$this->db->where('hourlyid',$hid);
}
if(!empty($from) && !empty($to))
{
$this->db->where('T_hourly_batch_info.report_date <=',$to);
$this->db->where('T_hourly_batch_info.report_date >=',$from);
}
$this->db->order_by('hourlyid','desc');
$res = $this->db->get();
//print_r($res->result());die();
return $res->result();
}
function checkExistHourlyData($bdate,$btime)
{
//echo 'Model'.$bdate.'-'.$btime;
$this->db->select('count(*) as count');
$this->db->from('T_hourly_batch_info');
$this->db->where('report_date',$bdate);
$this->db->where('report_time',$btime);
$res = $this->db->get();
return $res->result();
}
function getPowerConsumptionDetails($rdate)
{
$this->db->select('hourlyid,report_time,opening_trp_reading,close_trp_reading,auto_start,auto_stop');
$this->db->from('T_hourly_batch_info');
$this->db->where('report_date',$rdate);
$res = $this->db->get();
return $res->result();
}
function getDailyBatchCardData($month)
{
// $this->db->select('*');
// $this->db->from('T_daily_batchcard_data');
// $this->db->where('month(ddate)',$month);
$sql = 'select T_daily_batchcard_data.*,T_Employee_Details.FirstName,T_Employee_Details.LastName from T_daily_batchcard_data
join tbl_users on T_daily_batchcard_data.createdby = tbl_users.userId
join T_Employee_Details on T_Employee_Details.EmpID = tbl_users.EmpID
where month(ddate) = ?';
$res = $this->db->query($sql,array($month));
return $res->result();
}
function getIndividualDailyData($did)
{
$this->db->select('*');
$this->db->from('T_daily_batchcard_data');
$this->db->where('dailyid',$did);
$res = $this->db->get();
return $res->result();
}
function checkExistDaily($rdate)
{
$this->db->select('count(*) as count');
$this->db->from('T_daily_batchcard_data');
$this->db->where('ddate',$rdate);
$res = $this->db->get();
return $res->result();
}
function updateDailyBatchCardData($datatostore,$did)
{
$this->db->where('dailyid',$did);
$this->db->update('T_daily_batchcard_data',$datatostore);
$res = $this->db->affected_rows();
return $res;
}
function checkAbstractReport($date)
{
$this->db->select('count(*) as count');
$this->db->from('T_abstract_report');
//$this->db->where('abstract_report_date',$date);
$this->db->where("date_format(abstract_report_date,'%Y-%m')",$date);
$res = $this->db->get();
return $res->result();
}
function saveAbstractReport($datatostore)
{
$this->db->insert('T_abstract_report',$datatostore);
$res = $this->db->affected_rows();
return $res;
}
function updateAbstractReport($datatostore,$date,$abstract_details)
{
//$this->db->where('abstract_report_date',$date);
$this->db->where("date_format(abstract_report_date,'%Y-%m')",$date);
$this->db->where('abstract_details',$abstract_details);
$this->db->update('T_abstract_report',$datatostore);
$res = $this->db->affected_rows();
return $res;
}
function getPreviousMonthAbstractDetails($month,$year)
{
//$sql = "select abstract_details,close_stock from T_abstract_report where month(abstract_report_date) =? and year(abstract_report_date) =? ";
$sql = "select abstract_details,close_stock as Available from T_abstract_report where month(abstract_report_date) =? and year(abstract_report_date) =? ";
$res = $this->db->query($sql,array($month,$year));
return $res->result();
}
function getMonthAbstractDetails($month_year)
{
$sql = "select abstract_details,close_stock,open_stock as Available,physical_stock from T_abstract_report where date_format(abstract_report_date,'%Y-%m') = ? ";
$res = $this->db->query($sql,array($month_year));
return $res->result();
}
function getPreviousMonthClosedStockDetails($y,$m,$sn)
{
$month = 'month(ddate)';
$year = 'year(ddate)';
//$this->db->select($sn);
$this->db->select($sn,'ddate');
$this->db->from('T_daily_batchcard_data');
$this->db->where($month,$m);
$this->db->where($year,$y);
$this->db->order_by('ddate','desc');
$this->db->limit(1);
$res = $this->db->get();
//print_r($res->result());
return $res->result();
}
function currentMonthStockDetails($m,$y,$sd1,$sd2)
{
$month = 'month(ddate)';
$year = 'year(ddate)';
$list = array('ddate',$sd1,$sd2);
$this->db->select($list);
$this->db->from('T_daily_batchcard_data');
$this->db->where($month,$m);
$this->db->where($year,$y);
$this->db->order_by('ddate','asc');
$res = $this->db->get();
//print_r($res->result());
return $res->result();
}
function addmaint_dtl($array)
{
// $this->db->insert('T_Batchcard_MaintanceDetails',$array);
// $bmd = $this->db->affected_rows();
// return $bmd;
$this->db->insert('T_Batchcard_MaintanceDetails',$array);
$bmd = $this->db->affected_rows();
if($bmd>0)
{
$this->db->select('max(Maint_ID) as Maint_ID');
$this->db->from('T_Batchcard_MaintanceDetails');
$res = $this->db->get();
return $res->result();
}
}
function getmaint_dtl($fdate,$tdate,$type )
{
$this->db->select('BMD.*');
// $this->db->select()
$this->db->select('CONCAT(ED1.FirstName, " - " , ED1.LastName) AS Approver_Name', FALSE);
$this->db->select('CONCAT(ED2.FirstName, " - " , ED2.LastName) AS Worker_Name', FALSE);
$this->db->from('T_Batchcard_MaintanceDetails BMD');
$this->db->join('T_Employee_Details ED1', 'ED1.EmpID = BMD.Approved_By', 'left');
$this->db->join('T_Employee_Details ED2', 'ED2.EmpID = BMD.WorkDone_By', 'left');
if ($fdate and $tdate != ''){
$fromdate= date("Y-m-d",strtotime($fdate));
$todate=date("Y-m-d",strtotime($tdate));
$date = "date(BMD.CreatedOn) >= '".$fromdate."'
and date( BMD.CreatedOn) <= '". $todate."'";
$this->db->where($date);
}
if($type!=""){
$this->db->where("BMD.type" ,$type);
}
$bmd = $this->db->get();
return $bmd->result();
}
function getmaint_dtl_MntID($MntID)
{
$this->db->select('*');
$this->db->from('T_Batchcard_MaintanceDetails');
$this->db->where('Maint_ID',$MntID);
$bmd = $this->db->get();
return $bmd->result();
}
function updatemaint_dtl($array,$MntID)
{
$this->db->where('Maint_ID',$MntID);
$this->db->update('T_Batchcard_MaintanceDetails',$array);
$bmd = $this->db->affected_rows();
return $bmd;
}
function maintenancesdata($date){
$this->db->select("bm.*,mm.Machine_Name");
$this->db->select("DATE_FORMAT(bm.CreatedOn,'%d-%m-%Y') AS date", FALSE);
$this->db->select("DATE_FORMAT(bm.CreatedOn,'%l:%i %p') AS time", FALSE);
$this->db->select("CONCAT(ED1.FirstName, ' - ' , ED1.LastName) AS Approver_Name", FALSE);
$this->db->select("CONCAT(ED2.FirstName, ' - ' , ED2.LastName) AS Worker_Name", FALSE);
$this->db->from("T_Batchcard_MaintanceDetails bm");
$this->db->join("T_Machine_Master mm", "bm.Machine_Code = mm.Machine_Code", "left");
$this->db->join("T_Employee_Details ED1", "bm.Approved_By = ED1.EmpID", "left");
$this->db->join("T_Employee_Details ED2", "bm.WorkDone_By = ED2.EmpID", "left");
$this->db->where("mm.Machines_Category = 'TRP'");
//AND DATE_FORMAT(bm.CreatedOn,'%Y-%m-%d') = '19-04-2018'
$this->db->where("DATE_FORMAT(bm.CreatedOn,'%d-%m-%Y')",$date);
$bmd = $this->db->get();
return $bmd->result();
//print_r($this->db->last_query());
//die();
}
function getmachine_codename(){
$this->db->select('Machine_Code,Machine_Name');
$this->db->from('T_Machine_Master');
$res = $this->db->get();
return $res->result();
}
/** Machines History/Master */
function getdept_dtls()
{
$this->db->select('T_DepartmentDetails.DEPCode,T_DepartmentDetails.shortName');
$this->db->from('T_DepartmentDetails');
$this->db->where('IsActive',1);
$res = $this->db->get();
return $res->result();
}
function getpono(){
$this->db->select('PONO');
$this->db->from('T_PurchaseOrder_Master');
$this->db->where('Status','ST026');
$res = $this->db->get();
return $res->result();
}
function getpo_dtls($pono){
// $this->db->select('T_PurchaseOrder_Master.PONO,T_PurchaseOrder_Master.SupplierID,T_SupplierDetailsN.SupplierName,DATE_FORMAT(T_PurchaseOrder_Master.PODate,"%d-%m-%Y") as PO_Date');
// $this->db->from('T_PurchaseOrder_Master');
// $this->db->join('T_SupplierDetailsN','T_PurchaseOrder_Master.SupplierID=T_SupplierDetailsN.SupplierID');
// $this->db->where('PONO',$pono);
// $res = $this->db->get();
// return $res->result();
$sql = "select T_PurchaseOrder_Master.PONO,T_PurchaseOrder_Master.SupplierID,T_SupplierDetailsN.SupplierName,DATE_FORMAT(T_PurchaseOrder_Master.PODate,'%d-%m-%Y') as PO_Date
from T_PurchaseOrder_Master
join T_SupplierDetailsN on T_PurchaseOrder_Master.SupplierID=T_SupplierDetailsN.SupplierID
where PONO = ? ";
$res = $this->db->query($sql,array($pono));
return $res->result();
}
function addhist_mstr($array)
{
$this->db->insert('T_Machine_Master',$array);
$mstr = $this->db->affected_rows();
//print_r($this->db->last_query());
//print_r($mstr);
if($mstr>0)
{
$this->db->select('Machine_Code');
$this->db->from('T_Machine_Master');
$this->db->order_by('CreatedOn','desc');
$this->db->limit(1);
$res = $this->db->get();
//print_r($res->result());
return $res->result();
}
}
function addhist_dtl($array){
$this->db->insert('T_Machine_Details',$array);
$child = $this->db->affected_rows();
return $child;
}
//This function for multi file upload option (both machine and maint/br screen)
function addfiledetails($array){
$this->db->insert('T_Machines_file', $array);
$r = $this->db->affected_rows();
return $r;
//return TRUE;
}
function insertvalueintoconfig($config){
$this->db->insert('T_ConfigDetails', $config);
return TRUE;
}
//This function used to get the material category using configdetails table
function getmachineCategory($MaterialCategory = '')
{
$Config_ID = 'C028';
$this->db->select('Config_ID,ConfigValue');
$this->db->from('T_ConfigDetails');
$this->db->where('Config_id',$Config_ID );
if($MaterialCategory != '')
{
$this->db->where('ConfigValue !=',$MaterialCategory );
}
$query = $this->db->get();
return $query->result();
}
function getHist_dtls($MachineCode = '')
{
$this->db->select('*');
$this->db->from('T_Machine_Details');
if($MachineCode != '')
{
$this->db->where('Machine_Code',$MachineCode );
}
$bmd = $this->db->get();
return $bmd->result();
}
//This function for multi file upload option (both machine and maint/br screen)
function getfiledetails($data = '')
{
$this->db->select('*');
$this->db->from('T_Machines_file');
if($data != '')
{
$this->db->where('Machine_code/Maint_id',$data );
}
$bmd = $this->db->get();
return $bmd->result();
}
function getHist_mstr($MachineCode = '')
{
$this->db->select('*');
$this->db->from('T_Machine_Master');
if($MachineCode != '')
{
$this->db->where('Machine_Code',$MachineCode );
}
$bmd = $this->db->get();
return $bmd->result();
}
// function getHist_dtlsID($MntID)
// {
// $this->db->select('*');
// $this->db->from('T_Batchcard_MaintanceDetails');
// $this->db->where('Maint_ID',$MntID);
// $bmd = $this->db->get();
// return $bmd->result();
// }
// function getHist_dtlsDate($dateval)
// {
// $this->db->select('*');
// $this->db->from('T_Batchcard_MaintanceDetails');
// $this->db->where('Maint_Date',$dateval);
// $bmd = $this->db->get();
// return $bmd->result();
// }
function edithist_mstr($array,$machinecode){
$this->db->where('Machine_Code',$machinecode );
$this->db->update('T_Machine_Master',$array);
$mstr = $this->db->affected_rows();
if($mstr>0)
{
$this->db->select('Machine_Code');
$this->db->from('T_Machine_Master');
$this->db->order_by('CreatedOn','desc');
$this->db->limit(1);
$res = $this->db->get();
return $res->result();
}
}
function edithist_dtl($array,$machinecode){
$this->db->where('Machine_Code',$machinecode );
$this->db->update('T_Machine_Details',$array);
$mdtl = $this->db->affected_rows();
return $mdtl;
}
function updatefilelist($array,$fileid){
$this->db->where('File_No',$fileid);
$this->db->update('T_Machines_file', $array);
$ar = $this->db->affected_rows();
return $ar;
}
function checkMachineCategory($mc){
// $this->db->select('ConfigValue');
// $this->db->from('T_ConfigDetails');
// $this->db->where(Config_ID,'C023');
// $this->db->like('ConfigValue',$mc);
// $query = $this->db->get();
// //print_r($query->result());
// return $query->result();
// //return $this->db->get('T_MaterialMaster');
// }
$temp[] = '' ;
$this->db->distinct();
$this->db->select('ConfigValue');
$this->db->from('T_ConfigDetails');
$this->db->where('Config_ID','C028');
$this->db->like('ConfigValue',$mc);
$query = $this->db->get();
//print_r($query->result());die;
$temparr[] = '' ;
foreach($query->result() as $arr){
$temparr[] = $arr->ConfigValue ;
}
$temp['suggestions'] = $temparr;
//print_r($temp);
return $temp;
}
//This function for multi file upload option (both machine and maint/br screen)
function deletefiledetails($id,$name){
$sql = "DELETE FROM T_Machines_file WHERE File_No =".$id;
unlink("uploads/images/".$name);//this deletes the file on particular folder too
$query = $this->db->query($sql);
$ar = $this->db->affected_rows();
return $ar;
}
function getHist_tbls($MachineCode){
$this->db->select('T_Machine_Master.*,T_Batchcard_MaintanceDetails.*,T_DepartmentDetails.DepartmentName');
$this->db->select("DATE_FORMAT(T_Batchcard_MaintanceDetails.CreatedOn,'%d-%m-%Y') AS date", FALSE);
$this->db->select("DATE_FORMAT(T_Batchcard_MaintanceDetails.CreatedOn,'%l:%i %p') AS time", FALSE);
$this->db->select("CONCAT(ED1.FirstName, ' - ' , ED1.LastName) AS Approver_Name", FALSE);
$this->db->select("CONCAT(ED2.FirstName, ' - ' , ED2.LastName) AS Worker_Name", FALSE);
$this->db->from('T_Machine_Master');
$this->db->join('T_Batchcard_MaintanceDetails','T_Batchcard_MaintanceDetails.Machine_Code=T_Machine_Master.Machine_Code');
$this->db->join('T_DepartmentDetails','T_DepartmentDetails.DEPCode=T_Machine_Master.Machines_Dept');
$this->db->join('T_Employee_Details ED1', 'ED1.EmpID = T_Batchcard_MaintanceDetails.Approved_By', 'left');
$this->db->join('T_Employee_Details ED2', 'ED2.EmpID = T_Batchcard_MaintanceDetails.WorkDone_By', 'left');
$this->db->where('T_Machine_Master.Machine_Code',$MachineCode);
$res = $this->db->get();
return $res->result();
// $sql = "SELECT T_Machine_Master.*,T_Batchcard_MaintanceDetails.*,
// T_DepartmentDetails.DepartmentName,
// DATE_FORMAT(T_Batchcard_MaintanceDetails.CreatedOn, '%d-%m-%Y') as date,
// DATE_FORMAT(T_Batchcard_MaintanceDetails.CreatedOn, '%l:%i %p') as time,
// FROM T_Machine_Master
// LEFT JOIN T_Batchcard_MaintanceDetails ON T_Batchcard_MaintanceDetails.Machine_Code=T_Machine_Master.Machine_Code
// LEFT JOIN T_DepartmentDetails ON T_DepartmentDetails.DEPCode=T_Machine_Master.Machines_Dept
// WHERE T_Machine_Master.Machine_Code = '".$MachineCode."'" ;
// $res = $this->db->query($sql);
// return $res->result();
}
function getinvoice_products()
{
$this->db->select('product_id,product_name,product_description');
$this->db->from('ip_products');
$supp = $this->db->get();
return $supp->result();
}
function get_consumablematerials()
{
$this->db->select('MaterialCode,MaterialName,');
$this->db->from('T_MaterialMaster');
//$this->db->where('T_MaterialMaster.Category',Consumables);
$this->db->where('T_MaterialMaster.Category like','%Consumable%');
$supp = $this->db->get();
return $supp->result();
}
function get_rawmaterials()
{
$this->db->select('MaterialCode,MaterialName,');
$this->db->from('T_MaterialMaster');
$this->db->where('T_MaterialMaster.Category like','%Raw Material%');
$supp = $this->db->get();
//print_r($this->db->last_query());
return $supp->result();
}
function get_packmaterials()
{
$this->db->select('MaterialCode,MaterialName,');
$this->db->from('T_MaterialMaster');
// $this->db->where('T_MaterialMaster.Category',Packingmaterial);
$this->db->where('T_MaterialMaster.Category like','%Packing%');
$supp = $this->db->get();
return $supp->result();
}
function GetReclamationMaterial()
{
$this->db->select('product_id,product_name,product_description');
$this->db->from('ip_products');
$this->db->join('ip_families','ip_families.family_id=ip_products.family_id');
// $this->db->where('T_MaterialMaster.Category',Packingmaterial);
$this->db->where('ip_families.family_name like','%Reclam%');
$supp = $this->db->get();
return $supp->result();
}
function getMaterial()
{
$this->db->select('MaterialCode,MaterialName,');
$this->db->from('T_MaterialMaster');
//$this->db->where('T_MaterialMaster.Category',Consumables);
$supp = $this->db->get();
return $supp->result();
}
function GettotalQty($MaterialCode,$date)
{
echo $model;
$dt = date('d',strtotime($date));
$year = date('Y', strtotime($date));
$month = date('m', strtotime($date));
if($month<04)
{
//echo "b4finance";
$newyr = $year - 1;
$startdate = $newyr.'-'.'04'.'-'.'01';
//echo $startdate;
}
else
{
//echo "after Finance";
$startdate = $year.'-'.'04'.'-'.'01';
// echo $startdate;
}
$nedt= $dt;
$newdate=$year.'-'.$month.'-'.$nedt;
$subQuery='select T_MaterialMaster.MaterialName,T_Material_stock_history.MaterialCode,ifnull((select ifnull(sum(Quantity),0) as AddQty from T_Material_stock_history
where Transaction_type = "Add" and MaterialCode=?),0) as Addqty,
ifnull((select ifnull(sum(Quantity),0) as ReduceQty from T_Material_stock_history where Transaction_type = "Reduce"
and MaterialCode=?),0) as ReduceQty from T_Material_stock_history join T_MaterialMaster on T_Material_stock_history.MaterialCode=T_MaterialMaster.MaterialCode where T_Material_stock_history.MaterialCode=?
and CreatedOn >= ? and CreatedOn<=? group by Addqty';
$query = $this->db->query($subQuery,array($MaterialCode,$MaterialCode,$MaterialCode,$startdate,$newdate));
//print_r($this->db->last_query());
return $query->result_array();
}
function GetNetQty($MaterialCode,$date,$Supplier)
{
//echo $date;
$dt = date('d',strtotime($date));
$year = date('Y', strtotime($date));
$month = date('m', strtotime($date));
if($month<04)
{
//echo "b4finance";
$newyr = $year - 1;
$startdate = $newyr.'-'.'04'.'-'.'01';
//echo $startdate;
}
else
{
//echo "after Finance";
$startdate = $year.'-'.'04'.'-'.'01';
// echo $startdate;
}
$nedt= $dt+1;
$newdate=$year.'-'.$month.'-'.$nedt;
// $subQuery='select (select sum(Quantity) as AddQty from T_Material_stock_history where Transaction_type = "Add" and MaterialCode=?
// ) as Addqty,(select sum(Quantity) as ReduceQty from T_Material_stock_history where Transaction_type = "Reduce" and MaterialCode=?
// ) as ReduceQty from T_Material_stock_history where MaterialCode=? and CreatedOn >= ? and CreatedOn<=? and SupplierID=? group by Addqty';
$subQuery='select ifnull((select ifnull(sum(Quantity),0) as AddQty from T_Material_stock_history
where Transaction_type = "Add" and MaterialCode=? and SupplierID=? ),0) as Addqty,
ifnull((select ifnull(sum(Quantity),0) as ReduceQty from T_Material_stock_history where Transaction_type = "Reduce"
and MaterialCode=? and SupplierID=? ),0) as ReduceQty from T_Material_stock_history where MaterialCode=?
and CreatedOn >= ? and CreatedOn<=? and SupplierID=? group by Addqty';
$query = $this->db->query($subQuery,array($MaterialCode,$Supplier,$MaterialCode,$Supplier,$MaterialCode,$startdate,$newdate,$Supplier));
//print_r($this->db->last_query());
return $query->result_array();
}
function GetMaterialHistory($AdjustmentId)
{
$this->db->select('T_Material_Adjustment.*,SupplierName,MaterialName');
$this->db->from('T_Material_Adjustment');
$this->db->join('T_SupplierDetailsN',"T_SupplierDetailsN.SupplierID=T_Material_Adjustment.SupplierID");
$this->db->join('T_MaterialMaster',"T_MaterialMaster.MaterialCode=T_Material_Adjustment.MaterialCode");
$this->db->where('T_Material_Adjustment.AdjustmentId',$AdjustmentId);
$supp = $this->db->get();
return $supp->result();
join("T_Machine_Master mm", "bm.Machine_Code = mm.Machine_Code", "left");
}
function AddmaterialAdjustment($adjustmentaarray)
{
$this->db->trans_start();
$this->db->insert('T_Material_Adjustment',$adjustmentaarray);
$insert_id = $this->db->affected_rows();
$this->db->trans_complete();
// print_r($this->db->last_query());
//print_r($insert_id);
if($insert_id>0)
{
$subQuery = 'select max(AdjustmentId) as AdjustmentId from T_Material_Adjustment';
$query = $this->db->query($subQuery);
return $query->result();
}
}
function AddMaterialHistory($historytable)
{
$this->db->trans_start();
$this->db->insert('T_Material_stock_history',$historytable);
$insert_id = $this->db->affected_rows();
$this->db->trans_complete();
return $insert_id;
}
function insert_final_product($final_master)
{
$this->db->trans_start();
$this->db->insert('T_FinalProduct_Master',$final_master);
$insert_id = $this->db->affected_rows();
$this->db->trans_complete();
// print_r($this->db->last_query());
//print_r($insert_id);
if($insert_id>0)
{
$subQuery = 'select max(FPId) as PId from T_FinalProduct_Master';
$query = $this->db->query($subQuery);
return $query->result();
}
}
function save_finalraw($rawarray)
{
$this->db->trans_start();
$this->db->insert('T_FinalProduct_RawMaterials',$rawarray);
$insert_id = $this->db->affected_rows();
$this->db->trans_complete();
return true;
}
function save_finalcon($conarray)
{
$this->db->trans_start();
$this->db->insert('T_FinalProduct_ConsumableMaterials',$conarray);
$insert_id = $this->db->affected_rows();
$this->db->trans_complete();
return true;
}
function save_finalpacking($packarray)
{
$this->db->trans_start();
$this->db->insert('T_FinalProduct_PackingMaterials',$packarray);
$insert_id = $this->db->affected_rows();
$this->db->trans_complete();
return true;
}
function save_finalcoproduct($coparray)
{
//print_r($coparray);
$this->db->trans_start();
$this->db->insert('T_FinalProduct_CoProduct_Materials',$coparray);
$insert_id = $this->db->affected_rows();
$this->db->trans_complete();
return true;
}
function getAllMaterialAdjustments()
{
$this->db->select('T_Material_Adjustment.*,SupplierName,MaterialName');
$this->db->from('T_Material_Adjustment');
$this->db->join('T_SupplierDetailsN', 'T_Material_Adjustment.SupplierID = T_SupplierDetailsN.SupplierID');
$this->db->join('T_MaterialMaster', 'T_MaterialMaster.MaterialCode = T_Material_Adjustment.MaterialCode');
$res = $this->db->get();
return $res->result();
}
function save_rawmaterialto_materialHistory($materialhistory)
{
$this->db->trans_start();
$this->db->insert('T_Material_stock_history',$materialhistory);
$insert_id = $this->db->affected_rows();
$this->db->trans_complete();
return true;
}
function getFinalProductDetaisl()
{
$this->db->select('T_FinalProduct_Master.*,product_name,product_description');
$this->db->from('T_FinalProduct_Master');
$this->db->join('ip_products','ip_products.product_id=T_FinalProduct_Master.MaterialCode');
$this->db->order_by('T_FinalProduct_Master.FPId','desc');
$res = $this->db->get();
return $res->result();
}
function getmasterProductDetails($FPId)
{
$this->db->select('T_FinalProduct_Master.*,product_name,product_description');
$this->db->from('T_FinalProduct_Master');
$this->db->join('ip_products','ip_products.product_id=T_FinalProduct_Master.MaterialCode');
$this->db->where('T_FinalProduct_Master.FPId',$FPId);
$res = $this->db->get();
return $res->result();
}
function getrawmaterialDetails($FPId)
{
$this->db->select('T_FinalProduct_RawMaterials.*,SupplierName,MaterialName');
$this->db->from('T_FinalProduct_RawMaterials');
$this->db->join('T_MaterialMaster','T_MaterialMaster.MaterialCode=T_FinalProduct_RawMaterials.MaterialCode');
$this->db->join('T_SupplierDetailsN','T_SupplierDetailsN.SupplierID=T_FinalProduct_RawMaterials.SupplierID');
$this->db->where('T_FinalProduct_RawMaterials.FPId',$FPId);
$res = $this->db->get();
return $res->result();
}
function getconsumaterialDetails($FPId)
{
$this->db->select('T_FinalProduct_ConsumableMaterials.*,SupplierName,MaterialName');
$this->db->from('T_FinalProduct_ConsumableMaterials');
$this->db->join('T_MaterialMaster','T_MaterialMaster.MaterialCode=T_FinalProduct_ConsumableMaterials.MaterialCode');
$this->db->join('T_SupplierDetailsN','T_SupplierDetailsN.SupplierID=T_FinalProduct_ConsumableMaterials.SupplierID');
$this->db->where('T_FinalProduct_ConsumableMaterials.FPId',$FPId);
$res = $this->db->get();
return $res->result();
}
function getpackagematerialDetails($FPId)
{
$this->db->select('T_FinalProduct_PackingMaterials.*,SupplierName,MaterialName');
$this->db->from('T_FinalProduct_PackingMaterials');
$this->db->join('T_MaterialMaster','T_MaterialMaster.MaterialCode=T_FinalProduct_PackingMaterials.MaterialCode');
$this->db->join('T_SupplierDetailsN','T_SupplierDetailsN.SupplierID=T_FinalProduct_PackingMaterials.SupplierID');
$this->db->where('T_FinalProduct_PackingMaterials.FPId',$FPId);
$res = $this->db->get();
return $res->result();
}
function getcoproductmaterial($FPId)
{
$subQuery ='select T_FinalProduct_CoProduct_Materials.*,product_name
from T_FinalProduct_CoProduct_Materials
join ip_products on ip_products.product_id = T_FinalProduct_CoProduct_Materials.MaterialCode
where T_FinalProduct_CoProduct_Materials.FPId=?';
$query = $this->db->query($subQuery,array($FPId));
return $query->result_array();
}
function update_final_product($final_master,$FPId)
{
//print_r($final_master);
$this->db->where('FPId', $FPId);
$this->db->update('T_FinalProduct_Master', $final_master);
$insert_id = $this->db->affected_rows();
//return TRUE;
if($insert_id>0)
{
$subQuery = 'select max(FPId) as FPId from T_FinalProduct_Master';
$query = $this->db->query($subQuery,array());
return $query->result_array();
}
}
function GetRawid($rawilneno)
{
$this->db->select('*');
$this->db->from('T_FinalProduct_RawMaterials');
$this->db->where('RawId',$rawilneno);
$query = $this->db->get();
//print_r($this->db->last_query());
return $query->result();
}
function Getconsuid($consuline)
{
$this->db->select('*');
$this->db->from('T_FinalProduct_ConsumableMaterials');
$this->db->where('ConId',$consuline);
$query = $this->db->get();
//print_r($this->db->last_query());
return $query->result();
}
function Getpackid($packlineId)
{
$this->db->select('*');
$this->db->from('T_FinalProduct_PackingMaterials');
$this->db->where('PackId',$packlineId);
$query = $this->db->get();
//print_r($this->db->last_query());
return $query->result();
}
function Getcopid($coplineId)
{
$this->db->select('*');
$this->db->from('T_FinalProduct_CoProduct_Materials');
$this->db->where('CoId',$coplineId);
$query = $this->db->get();
//print_r($this->db->last_query());
return $query->result();
}
function update_finalraw($rawarray,$rawilneno)
{
$this->db->where('RawId', $rawilneno);
$this->db->update('T_FinalProduct_RawMaterials', $rawarray);
return TRUE;
}
function GetRawCheck($rawmaterial,$final_product_id)
{
$this->db->select('*');
$this->db->from('T_Material_stock_history');
$this->db->where('MaterialCode',$rawmaterial);
$this->db->where('Ref_No',$final_product_id);
$this->db->where('Ref_Type',FinalProduct);
$query = $this->db->get();
//print_r($this->db->last_query());
return $query->result();
}
function update_rawmaterialto_materialHistory($rawmaterialhistory,$rawmaterial,$FPID)
{
$this->db->where('Ref_No', $FPID);
$this->db->where('MaterialCode', $rawmaterial);
$this->db->update('T_Material_stock_history', $rawmaterialhistory);
return TRUE;
}
function update_finalconsu($conarray,$consuline)
{
$this->db->where('ConId', $consuline);
$this->db->update('T_FinalProduct_ConsumableMaterials', $conarray);
return TRUE;
}
function update_finalpacking($conarray,$packlineId)
{
$this->db->where('PackId', $packlineId);
$this->db->update('T_FinalProduct_PackingMaterials', $conarray);
return TRUE;
}
function update_finalcoproduct($coparray,$coplineId)
{
$this->db->where('CoId', $coplineId);
$this->db->update('T_FinalProduct_CoProduct_Materials', $coparray);
return TRUE;
}
function getEditMaterialAdjustments($AdjustmentId)
{
$this->db->select('T_Material_Adjustment.*,SupplierName,MaterialName,Description,AdjustmentId,ActualQuantity,T_Material_stock_history.Quantity,Ref_Type,Transaction_type');
$this->db->from('T_Material_Adjustment');
$this->db->join('T_Material_stock_history', 'T_Material_Adjustment.AdjustmentId = T_Material_stock_history.Ref_No and T_Material_stock_history.Ref_Type="Adjust"');
$this->db->join('T_SupplierDetailsN', 'T_Material_Adjustment.SupplierID = T_SupplierDetailsN.SupplierID');
$this->db->join('T_MaterialMaster', 'T_MaterialMaster.MaterialCode = T_Material_Adjustment.MaterialCode');
$this->db->where('T_Material_Adjustment.AdjustmentId',$AdjustmentId);
$res = $this->db->get();
return $res->result();
}
function material_name(){
$sql="SELECT distinct MaterialName FROM T_MaterialMaster;";
$query = $this->db->query($sql);
return $query->result();
}
function getMaterial_history($prod,$frm,$t)
{
$sql="SELECT msh.StockId,sd.SupplierName,mm.MaterialName,msh.Transaction_type,msh.Ref_Type,msh.Ref_No,msh.Quantity,msh.ItemValue,date_format(msh.UpdatedOn,'%d-%m-%Y') as updatedOn FROM T_Material_stock_history as msh
left join T_SupplierDetailsN as sd on sd.SupplierID=msh.SupplierID
left join T_MaterialMaster as mm on mm.MaterialCode=msh.MaterialCode
where msh.StockId != 'null' ";
if ($prod!= ''){
$sql.=" and mm.MaterialName = '".$prod."' ";
}
if ($frm and $t != ''){
$fromd= date("Y-m-d",strtotime($frm));
$tod=date("Y-m-d",strtotime($t));
$sql.="and date(msh.UpdatedOn) >= '".$fromd."'
and date(msh.UpdatedOn) <= '".$tod."'";
}
$query =$this->db->query($sql);
return $query->result();
}
//created by TamilBala
//for update quantity stockhistory
function updateQuantityStockHistory($updatequantity,$id)
{
$this->db->where('Ref_No',$id);
$this->db->where('Ref_Type',Adjust);
$this->db->update('T_Material_stock_history',$updatequantity);
$r = $this->db->affected_rows();
//return $r->result();
//echo "HIS";
}
//created by TamilBala
//for update quantity T_Material_Adjustment
function updateQuantityAdjusment($updatequantitydes,$id)
{
$this->db->where('AdjustmentId',$id);
$this->db->update('T_Material_Adjustment',$updatequantitydes);
$r = $this->db->affected_rows();
return $r;
}
//created by TamilBala
//for update isactive T_Material_Adjustment
function updateisactiveadjusment($Updateactive,$AdjustmentId)
{
$this->db->where('AdjustmentId',$AdjustmentId);
$this->db->update('T_Material_Adjustment',$Updateactive);
$r = $this->db->affected_rows();
return $r;
}
//created by TamilBala
//for update isactive stockhistory
function updateisactiveastockhistory($Updateactive,$AdjustmentId)
{
$this->db->where('Ref_No',$AdjustmentId);
$this->db->where('Ref_Type',Adjust);
$this->db->update('T_Material_stock_history',$Updateactive);
$r = $this->db->affected_rows();
return $r;
}
// function GetFinishedProductList($starting_date,$end_date,$product_id)
// {
// $subQuery ='select ip_products.product_id,ip_products.product_name,ip_invoices.invoice_id,ip_invoice_items.item_quantity,ip_invoice_items.item_price,ip_invoices.invoice_number as IDnumber,ip_invoices.receivedstatus as Type from ip_products join ip_invoice_items on ip_products.product_id = ip_invoice_items.item_product_id join ip_invoices on ip_invoice_items.invoice_id = ip_invoices.invoice_id
// join T_FinalProduct_Master on ip_products.product_id = T_FinalProduct_Master.MaterialCode where (ip_invoices.invoice_date_modified>=? and ip_invoices.invoice_date_modified<=? and ip_invoice_items.item_product_id=?) union
// select ip_products.product_id,ip_products.product_name,T_FinalProduct_Master.FPId,T_FinalProduct_Master.Quantity,T_FinalProduct_Master.Price,T_FinalProduct_Master.FPId as IDnumber,T_FinalProduct_Master.ProductType as Type from ip_products join ip_invoice_items on ip_products.product_id = ip_invoice_items.item_product_id
// join T_FinalProduct_Master on ip_products.product_id = T_FinalProduct_Master.MaterialCode where
// (T_FinalProduct_Master.ProductDate<=? and T_FinalProduct_Master.ProductDate>=? and T_FinalProduct_Master.MaterialCode=?) group by FPId';
// $query = $this->db->query($subQuery,array($starting_date,$end_date,$product_id,$end_date,$starting_date,$product_id));
// //print_r($this->db->last_query());
// return $query->result_array();
// }
function GetFinishedProductList($starting_date,$end_date,$product_id)
{
// $subQuery = 'select * from T_FinalProduct_History,ip_products.product_name
// join ip_products on T_FinalProduct_History.ProductCode = ip_products.product_id
// where ProductCode=?
// and Transactiondate >= ? and Transactiondate <= ?';
$subQuery='select RefID,ProductCode,product_description,Quantity,Price,Transactiondate,TransactionType,product_name
from T_FinalProduct_History
join ip_products on ip_products.product_id= T_FinalProduct_History.ProductCode
where ProductCode=? and Transactiondate >= ? and Transactiondate <= ?';
// $subQuery='select FPId as RefID,product_id as ProductCode,Quantity,product_description,Price,ProductDate as Transactiondate,ProductType as TransactionType,product_name from T_FinalProduct_Master join ip_products on ip_products.product_id= T_FinalProduct_Master.MaterialCode where MaterialCode=? and ProductDate >= ? and ProductDate <= ?';
$query = $this->db->query($subQuery,array($product_id,$starting_date,$end_date));
// print_r($this->db->last_query());
return $query->result_array();
}
function SaveFinalHistory($final_history)
{
$this->db->trans_start();
$this->db->insert('T_FinalProduct_History',$final_history);
$insert_id = $this->db->affected_rows();
$this->db->trans_complete();
return true;
}
function UpdateFinalHistory($final_history,$FPID,$rawmaterial)
{
//print_r($final_history);
$this->db->where('RefID', $FPID);
$this->db->where('ProductCode',$rawmaterial);
$this->db->update('T_FinalProduct_History',$final_history);
//print_r($this->db->last_query());
return TRUE;
}
function GetAvgPrice($MaterialCode,$date,$Supplier)
{
$subQuery='select AVG(Rate) as avg_rate from T_PurchaseOrder_LineItem
join T_PurchaseOrder_Master on T_PurchaseOrder_Master.PONO=T_PurchaseOrder_LineItem.PONO
where MaterialCode = ? and SupplierID=?';
$query = $this->db->query($subQuery,array($MaterialCode,$Supplier));
//print_r($this->db->last_query());
return $query->result_array();
}
function Get_Product_Supplier($MaterialCode,$type)
{
//echo $MaterialCode;
$ltype='%'.$type.'%';
//echo $ltype;
$this->db->select('mas.SupplierID,sup.SupplierName,mmas.MaterialCode,mmas.MaterialName');
$this->db->from('T_PurchaseOrder_Master mas');
$this->db->join('T_SupplierDetailsN sup','sup.SupplierID=mas.SupplierID');
$this->db->join('T_PurchaseOrder_LineItem line','line.PONO=mas.PONO');
$this->db->join('T_MaterialMaster mmas','mmas.MaterialCode=line.MaterialCode');
$this->db->where('Category like',$ltype);
// $this->db->where('T_MaterialMaster.Category like','%Raw Material%');
$this->db->where('mmas.MaterialCode',$MaterialCode);
$this->db->group_by('SupplierID');
$supp = $this->db->get();
//print_r($this->db->last_query());die();
return $supp->result();
}
function getSalesProductvalue()
{
$this->db->select('product_id,product_price');
$this->db->from('ip_products');
$price = $this->db->get();
return $price->result();
}
function finyear(){
$sql="SELECT
CASE WHEN MONTH(MaterialRcvdDate)>=4
THEN concat(YEAR(MaterialRcvdDate), '-',YEAR(MaterialRcvdDate)+1)
ELSE concat(YEAR(MaterialRcvdDate)-1,'-', YEAR(MaterialRcvdDate))
END AS financial_year
FROM T_IGR_Master
GROUP BY financial_year";
$query = $this->db->query($sql);
// print_r($this->db->last_query());die();
return $query->result();
}
function GetStockAbstract($search)
{
// echo 'in';
$fdate = $search['from_date'];
$fromdate = date("Y-m-d", strtotime($fdate) );
$tdate = $search['to_date'];
$todate = date("Y-m-d", strtotime($tdate) );
$currentyr = date("Y");
$finyear = $search['year'];
$pieces = explode("-", $finyear);///fin year
//print_r($search); die();
$startyr = $pieces['0'];
$endyr = $pieces['1'];
//echo $search['month'].'-'.$startyr.'-'. $endyr;die();
//echo $endyr;die();
$subquery="SELECT
DISTINCT(p.MaterialCode) AS ProductID,Ref_No,Date(CreatedOn),mm.MaterialName,stock,MaterialType,Category,stockdate,mm.Category,ip.product_name,ip.product_id,
SUM(IF(p.Transaction_type='Add',p.Quantity,0)) AS Addition,
SUM(IF(p.Transaction_type='Reduce',p.Quantity,0)) AS Reduce,
SUM((IF(p.Transaction_type='Add',p.Quantity,0))-(IF(p.Transaction_type='Reduce',p.Quantity,0))) AS AvailableStock,ROUND(AVG(Rate),2) as Avg_rate,ROUND(AVG(ItemValue),2) as Final_avg
FROM
T_Material_stock_history AS p
left join T_MaterialMaster mm on mm.MaterialCode = p.MaterialCode
left join ip_products ip on ip.product_id=p.MaterialCode
left join T_PurchaseOrder_LineItem pol on pol.MaterialCode = mm.MaterialCode
";
if($search['month']=='' && $startyr =='')
{
if($search['from_date'] != '')
{
$subquery.=" left join (select StockId,pp.MaterialCode mc,
SUM((IF(pp.Transaction_type='Add',Quantity,0))-(IF(pp.Transaction_type='Reduce',Quantity,0)))
AS OpeningStock
FROM T_Material_stock_history AS pp
where pp.CreatedOn < '".$fromdate."'
GROUP BY pp.MaterialCode
) as opens on opens.mc = p.MaterialCode";
}
}
else if($search['month']!='' && $startyr !='')
{
if($search['month']!='')
{
$subquery.=" left join (select StockId,pq.MaterialCode mc,
SUM((IF(pq.Transaction_type='Add',Quantity,0))-(IF(pq.Transaction_type='Reduce',Quantity,0)))
AS OpeningStock
FROM T_Material_stock_history AS pq
where month(pq.CreatedOn) < '".$search['month']."'
GROUP BY pq.MaterialCode
) as opens on opens.mc = p.MaterialCode";
}
}
else if($search['month']!='' && $startyr =='')
{
$subquery.=" left join (select StockId,pq.MaterialCode mc,
SUM((IF(pq.Transaction_type='Add',Quantity,0))-(IF(pq.Transaction_type='Reduce',Quantity,0)))
AS OpeningStock
FROM T_Material_stock_history AS pq
where date(pq.CreatedOn) < '".$currentyr.'-'.$search['month']."-01'
GROUP BY pq.MaterialCode
) as opens on opens.mc = p.MaterialCode";
}
else if($search['month']=='' && $startyr !='' )
{
// if($search['month']!='')
// {
$subquery.=" left join (select StockId,pq.MaterialCode mc,
SUM((IF(pq.Transaction_type='Add',Quantity,0))-(IF(pq.Transaction_type='Reduce',Quantity,0)))
AS OpeningStock
FROM T_Material_stock_history AS pq
where date(pq.CreatedOn) < '".$startyr."-03-31'
GROUP BY pq.MaterialCode
) as opens on opens.mc = p.MaterialCode";
// }
}
//$subquery.=" where mm.IsActive='1'";
if($search['month']=='' && $startyr =='')
{
if($search['from_date'] != '')
{
$subquery.=" where date(p.CreatedOn)>= '".$fromdate."'";
}
if($search['to_date'] != '')
{
//$subquery. = " and date(p.CreatedOn)<='".$todate."'";
$subquery.=" and date(p.CreatedOn)<= '".$todate."'";
}
}
else
{
if($search['month']!='' && $startyr =='')
{
$subquery.=" where date(p.CreatedOn)>= '".$currentyr.'-'.$search['month']."-01' and
date(p.CreatedOn)<='".$currentyr.'-'.$search['month']."-31'";
}
if($search['month']!='' && $startyr !='')
{
$subquery.=" where month(p.CreatedOn)= '".$search['month']."' and date(p.CreatedOn)>= '".$startyr."-04-01' and date(p.CreatedOn)<= '".$endyr."-03-31'";
}
if($startyr !='' && $search['month']=='')
{
$subquery.=" where date(p.CreatedOn)>= '".$startyr."-04-01' and date(p.CreatedOn)<= '".$endyr."-03-31'";
}
}
$subquery.=" and (mm.Category like '%Consumable%' or mm.Category like '%Raw Material%'
or mm.Category like '%Packing%' or mm.Category like '%Reclam%' or mm.Category like '%Finish%' or mm.Category like '%Final%' )";
$subquery.=" GROUP BY p.MaterialCode";
//$subquery. = "GROUP BY p.MaterialCode";
$query = $this->db->query($subquery,array());
//print_r($query->result());die();
//print_r($this->db->last_query());die();
return $query->result();
}
function GetStockAbstractdetails($search)
{
// echo 'in';
$fdate = $search['fdate'];
if($fdate != '0')
{
$fromdate = date("Y-m-d", strtotime($fdate) );
}
else
{
$fromdate ='';
}
$tdate = $search['tdate'];
if($tdate != '0')
{
$todate = date("Y-m-d", strtotime($tdate) );
}
else
{
$todate = '';
}
//echo $todate;die();
$finyear = $search['year'];
$pieces = explode("-", $finyear);///fin year
//print_r($search); die();
$startyr = $pieces['0'];
$endyr = $pieces['1'];
//echo $endyr;die();
$subquery="SELECT
DISTINCT(p.MaterialCode) AS ProductID,Ref_No,Date(CreatedOn) as Cdate,mm.MaterialName,stock,MaterialType,Category,stockdate,p.Transaction_type,Ref_Type, Ref_No,ip.product_name,invoice_number,
SUM(IF(p.Transaction_type='Add',Quantity,0)) AS Addition,
SUM(IF(p.Transaction_type='Reduce',Quantity,0)) AS Reduce,
SUM((IF(p.Transaction_type='Add',Quantity,0))-(IF(p.Transaction_type='Reduce',Quantity,0))) AS AvailableStock
FROM
T_Material_stock_history AS p
left join T_MaterialMaster mm on mm.MaterialCode = p.MaterialCode
left join ip_products ip on ip.product_id=p.MaterialCode
left join ip_invoices inv on inv.invoice_id=p.Ref_No
where p.MaterialCode='".$search['materialcode']."'
";
if($search['month']=='' && $startyr =='' && $endyr =='')
{
if($fromdate != '')
{
//echo "sv";die();
$subquery.=" and date(p.CreatedOn)>= '".$fromdate."'";
}
if($todate != '')
{
//$subquery. = " and date(p.CreatedOn)<='".$todate."'";
$subquery.=" and date(p.CreatedOn)<= '".$todate."'";
}
}
else
{
if($search['month']!='')
{
$subquery.=" and month(p.CreatedOn)>= '".$search['month']."'";
}
if($startyr !='' && $endyr !='')
{
$subquery.=" and date(p.CreatedOn)>= '".$startyr."-04-01' and date(p.CreatedOn)<= '".$endyr."-03-31'";
}
}
$subquery.=" GROUP BY p.CreatedOn";
//$subquery. = "GROUP BY p.MaterialCode";
$query = $this->db->query($subquery,array());
//print_r($this->db->last_query());die();
return $query->result();
}
public function GetMaterialCode($itemproductid)
{
$this->db->select('MaterialCode');
$this->db->from('ip_products');
$this->db->where('product_id',$itemproductid);
$query = $this->db->get();
// print_r( $this->db->last_query());
return $query->result();
}
public function Delete_ProductBatchCard($FPId)
{
$this->db->where('FPId', $FPId);
$this->db->delete('T_FinalProduct_ConsumableMaterials');
$this->db->where('FPId', $FPId);
$this->db->delete('T_FinalProduct_CoProduct_Materials');
$this->db->where('RefID', $FPId);
$this->db->delete('T_FinalProduct_History');
$this->db->where('FPId', $FPId);
$this->db->delete('T_FinalProduct_Master');
$this->db->where('FPId', $FPId);
$this->db->delete('T_FinalProduct_PackingMaterials');
$this->db->where('FPId', $FPId);
$this->db->delete('T_FinalProduct_RawMaterials');
$this->db->where('Ref_No', $FPId);
$this->db->delete('T_Material_stock_history');
return true;
}
}
?>