<?php
require_once('../Db/dbconfig.php');

if($_POST['type'] == 'getHistoryByStorage'){
	$storageId=$_POST['storageId'];
	$filterdt=$_POST['filterdt'];
	$mysqltime = date ('Y-m-d',  strtotime($filterdt));

	$sql="select * from total_purchase_qty where id =".$storageId;
	$res=mysqli_query($con,$sql);

	$status =false;

	$array = array();
	if (mysqli_num_rows($res) > 0) 
	{	
		while($rows=mysqli_fetch_assoc($res)){
			$sql2="select qt.id as qtytypeId,pur.author,p2.description as item,s.name as supplier_name, qt.q_name as quantity_type, pur.quantity, pur.created_at from 
				purchase pur,product p,productitem p2,supplier s,quantity_type qt where 
				pur.item_category = p.id and pur.item = p2.id and pur.supplier_name = s.id and pur.quantity_type = qt.id and DATE(pur.created_at) ='".$mysqltime."' and pur.item = '".$rows['item']."' and pur.quantity_type = '".$rows['quantity_type']."' and pur.ptype=".$rows['ptype'];
			$res2=mysqli_query($con,$sql2);
			if (mysqli_num_rows($res2) > 0) 
			{	
				while($rows2=mysqli_fetch_assoc($res2)){
					$data =['mode'=>'Created On','date'=>$rows2['created_at'],'dpt'=>'Purchase','qty'=>$rows2['quantity'],'qtyType'=>$rows2['quantity_type'],'user'=>$rows2['author'],'qtytypeId'=>$rows2['qtytypeId']];
					array_push($array,$data);
				}
			}

			$sql3="select qt.id as qtytypeId,pur.author, p2.description as item,s.name as supplier_name, qt.q_name as quantity_type, pur.quantity, pur.updated_by from 
				purchase_history pur,product p,productitem p2,supplier s,quantity_type qt where 
				pur.item_category = p.id and pur.item = p2.id and pur.supplier_name = s.id and pur.quantity_type = qt.id and DATE(pur.updated_by) ='".$mysqltime."' and pur.item = '".$rows['item']."' and pur.quantity_type = '".$rows['quantity_type']."' and pur.ptype=".$rows['ptype'];
			
			$res3=mysqli_query($con,$sql3);
			if (mysqli_num_rows($res3) > 0) 
			{	
				while($rows3=mysqli_fetch_assoc($res3)){
					$data =['mode'=>'Updated On','date'=>$rows3['updated_by'],'dpt'=>'Purchase','qty'=>$rows3['quantity'],'qtyType'=>$rows3['quantity_type'],'user'=>$rows3['author'],'qtytypeId'=>$rows3['qtytypeId']];
					array_push($array,$data);
				}
			}

			$sql4="select qt.id as qtytypeId,pur.author,p2.description as item,s.name as supplier_name, qt.q_name as quantity_type, pur.quantity, pur.deleted_on from 
				purchase_history pur,product p,productitem p2,supplier s,quantity_type qt where 
				pur.item_category = p.id and pur.item = p2.id and pur.supplier_name = s.id and pur.quantity_type = qt.id and DATE(pur.deleted_on) ='".$mysqltime."' and pur.item = '".$rows['item']."' and pur.quantity_type = '".$rows['quantity_type']."' and pur.ptype=".$rows['ptype'];
			
			$res4=mysqli_query($con,$sql4);
			if (mysqli_num_rows($res4) > 0) 
			{	
				while($rows4=mysqli_fetch_assoc($res4)){
					$data =['mode'=>'Deleted On','date'=>$rows4['deleted_on'],'dpt'=>'Purchase','qty'=>$rows4['quantity'],'qtyType'=>$rows4['quantity_type'],'user'=>$rows4['author'],'qtytypeId'=>$rows4['qtytypeId']];
					array_push($array,$data);
				}
			}

			/*$sqlstor="select p3.item as ptype,tpq.id, p2.description as item, tpq.quantity,p.created_at,qt.q_name as quantity_type,qt.id as qtytypeId from purchase p,total_purchase_qty_delete_history tpq,quantity_type qt,
				productitem p2,productype p3 where
					p.item = tpq.item and p.quantity_type = qt.id and p.quantity_type = tpq.quantity_type and p.ptype = tpq.ptype and tpq.item = p2.id and tpq.ptype = p3.id and DATE(tpq.deleted_on) ='".$mysqltime."' and tpq.item = '".$rows['item']."' and tpq.quantity_type = '".$rows['quantity_type']."' and tpq.ptype=".$rows['ptype'];
			
			$resstor=mysqli_query($con,$sqlstor);
			if (mysqli_num_rows($resstor) > 0) 
			{	
				while($rowstor=mysqli_fetch_assoc($resstor)){
					$data =['mode'=>'Deleted On','date'=>$rowstor['deleted_on'],'dpt'=>'Storage','qty'=>$rowstor['quantity'],'qtyType'=>$rowstor['quantity_type'],'user'=>$rowstor['author']];
					array_push($array,$data);
				}
			}*/

			$sqlSt="select ad.storageId,p2.item as ptypename,p2.id as ptypeId,ad.id ,todpt.depart as todrpt,p.description as prodname,ad.send_to_quantity,sendqt.q_name,ad.created_at as senddttime ,ad.author,ad.waste,ad.water_wt,sendqt.id as qtytypeid 
					from admin_dpts_history ad ,department todpt,productitem p ,quantity_type sendqt,productype p2 
					where ad.send_to = todpt.id and ad.productItem_id = p.id and ad.send_to_qtny_type = sendqt.id and ad.ptype = p2.id and (ad.admindpt_parent_id = 0 or ad.send_from =11) and ad.updated_by is null and ad.deleted_by is null and DATE(ad.created_at)='".$mysqltime."' and storageId=".$storageId;
			$resSt=mysqli_query($con,$sqlSt);
			if (mysqli_num_rows($resSt) > 0) 
			{	
				while($rowsSt=mysqli_fetch_assoc($resSt)){
					$data =['mode'=>'Send On','date'=>$rowsSt['senddttime'],'dpt'=>$rowsSt['todrpt'],'qty'=>$rowsSt['send_to_quantity'],'qtyType'=>$rowsSt['q_name'],'user'=>$rowsSt['author'],'qtytypeId'=>$rowsSt['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sql5="select fp.id as finalproductId,p2.item as ptypename,p2.id as ptypeId,ad.id ,todpt.depart as todrpt,p.description as prodname,fp.item as finalprodct,ad.send_to_quantity,sendqt.q_name,sendqt.id as qtytypeid,ad.created_at as senddttime ,ad.water_wt,ad.author
				from admin_dpts_history ad ,department todpt,productitem p ,final_product fp ,quantity_type sendqt,productype p2 
				where ad.send_to = todpt.id and ad.productItem_id = p.id and ad.final_product = fp.id and ad.send_to_qtny_type = sendqt.id and ad.ptype = p2.id and ad.updated_by is null and ad.deleted_by is null and DATE(ad.created_at)='".$mysqltime."' and ad.storageId=".$storageId;
			$res5=mysqli_query($con,$sql5);
			if (mysqli_num_rows($res5) > 0) 
			{	
				while($rows5=mysqli_fetch_assoc($res5)){
					$data =['mode'=>'Send On','date'=>$rows5['senddttime'],'dpt'=>$rows5['todrpt'],'qty'=>$rows5['send_to_quantity'],'qtyType'=>$rows5['q_name'],'user'=>$rows5['author'],'qtytypeId'=>$rows5['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sql6="select fp.id as finalproductId,p2.item as ptypename,p2.id as ptypeId,ad.id ,todpt.depart as todrpt,p.description as prodname,fp.item as finalprodct,ad.send_to_quantity,sendqt.q_name,sendqt.id as qtytypeid,ad.updated_by as senddttime ,ad.water_wt,ad.author
				from admin_dpts_history ad ,department todpt,productitem p ,final_product fp ,quantity_type sendqt,productype p2 
				where ad.send_to = todpt.id and ad.productItem_id = p.id and ad.final_product = fp.id and ad.send_to_qtny_type = sendqt.id and ad.ptype = p2.id and DATE(ad.updated_by)='".$mysqltime."' and ad.storageId=".$storageId;
			$res6=mysqli_query($con,$sql6);
			if (mysqli_num_rows($res5) > 0) 
			{	
				while($rows6=mysqli_fetch_assoc($res6)){
					$data =['mode'=>'Updated On','date'=>$rows6['senddttime'],'dpt'=>$rows6['todrpt'],'qty'=>$rows6['send_to_quantity'],'qtyType'=>$rows6['q_name'],'user'=>$rows6['author'],'qtytypeId'=>$rows6['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sql7="select fp.id as finalproductId,p2.item as ptypename,p2.id as ptypeId,ad.id ,todpt.depart as todrpt,p.description as prodname,fp.item as finalprodct,ad.send_to_quantity,sendqt.q_name,sendqt.id as qtytypeid,ad.deleted_by as senddttime ,ad.water_wt,ad.author
				from admin_dpts_history ad ,department todpt,productitem p ,final_product fp ,quantity_type sendqt,productype p2
				where ad.send_to = todpt.id and ad.productItem_id = p.id and ad.final_product = fp.id and ad.send_to_qtny_type = sendqt.id and ad.ptype = p2.id and DATE(ad.deleted_by)='".$mysqltime."' and ad.storageId=".$storageId;
			$res7=mysqli_query($con,$sql7);
			if (mysqli_num_rows($res7) > 0) 
			{	
				while($rows7=mysqli_fetch_assoc($res7)){
					$data =['mode'=>'Deleted On','date'=>$rows7['senddttime'],'dpt'=>$rows7['todrpt'],'qty'=>$rows7['send_to_quantity'],'qtyType'=>$rows7['q_name'],'user'=>$rows7['author'],'qtytypeId'=>$rows7['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sqlpkg="select pts.author, pts.created_at as senddttime,pts.send_to_quantity,qt.q_name,qt.id as qtytypeid from package_to_stock_history pts ,quantity_type qt where pts.send_to_qtny_type = qt.id and pts.updated_by is null and pts.deleted_by is null and DATE(pts.created_at)='".$mysqltime."' and pts.storageId=".$storageId;
			$respkg=mysqli_query($con,$sqlpkg);
			if (mysqli_num_rows($respkg) > 0) 
			{	
				while($rowspkg=mysqli_fetch_assoc($respkg)){
					$data =['mode'=>'Send On','date'=>$rowspkg['senddttime'],'dpt'=>'Packing','qty'=>$rowspkg['send_to_quantity'],'qtyType'=>$rowspkg['q_name'],'user'=>$rowspkg['author'],'qtytypeId'=>$rowspkg['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sqlpkg1="select pts.author,pts.updated_by as senddttime,pts.send_to_quantity,qt.q_name,qt.id as qtytypeid from package_to_stock_history pts ,quantity_type qt where pts.send_to_qtny_type = qt.id and DATE(pts.updated_by)='".$mysqltime."' and pts.storageId=".$storageId;
			$respkg1=mysqli_query($con,$sqlpkg1);
			if (mysqli_num_rows($respkg1) > 0) 
			{	
				while($rowspkg1=mysqli_fetch_assoc($respkg1)){
					$data =['mode'=>'Updated On','date'=>$rowspkg1['senddttime'],'dpt'=>'Packing','qty'=>$rowspkg1['send_to_quantity'],'qtyType'=>$rowspkg1['q_name'],'user'=>$rowspkg1['author'],'qtytypeId'=>$rowspkg1['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sqlpkg2="select pts.author,pts.deleted_by as senddttime,pts.send_to_quantity,qt.q_name,qt.id as qtytypeid from package_to_stock_history pts ,quantity_type qt where pts.send_to_qtny_type = qt.id and DATE(pts.deleted_by)='".$mysqltime."' and pts.storageId=".$storageId;
			$respkg2=mysqli_query($con,$sqlpkg2);
			if (mysqli_num_rows($respkg2) > 0) 
			{	
				while($rowspkg2=mysqli_fetch_assoc($respkg2)){
					$data =['mode'=>'Deleted On','date'=>$rowspkg2['senddttime'],'dpt'=>'Packing','qty'=>$rowspkg2['send_to_quantity'],'qtyType'=>$rowspkg2['q_name'],'user'=>$rowspkg2['author'],'qtytypeId'=>$rowspkg2['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sql8="select qt.id as qtytypeid,ste.author,ste.id,ste.purchaser_name ,qt.q_name ,ste.quantity ,ste.invoice_dt ,ste.invoice_no ,fp.item,ste.no_of_boxes,ste.created_at  from stock_to_export ste,quantity_type qt ,final_product fp where ste.quantity_type = qt.id and ste.final_productId = fp.id and DATE(ste.created_at)='".$mysqltime."' and storage_id=".$storageId;
			$res8=mysqli_query($con,$sql8);
			if (mysqli_num_rows($res8) > 0) 
			{	
				while($rows8=mysqli_fetch_assoc($res8)){
					$data =['mode'=>'Send On','date'=>$rows8['created_at'],'dpt'=>'Export','qty'=>$rows8['quantity'],'qtyType'=>$rows8['q_name'],'user'=>$rows8['author'],'qtytypeId'=>$rows8['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sql9="select qt.id as qtytypeid,ste.author,ste.id,ste.purchaser_name ,qt.q_name ,ste.quantity ,ste.invoice_dt ,ste.invoice_no ,fp.item,ste.no_of_boxes,ste.delete_on  from stock_to_export_history ste,quantity_type qt ,final_product fp where ste.quantity_type = qt.id and ste.final_productId = fp.id and DATE(ste.delete_on)='".$mysqltime."' and storage_id=".$storageId;
			$res9=mysqli_query($con,$sql9);
			if (mysqli_num_rows($res9) > 0) 
			{	
				while($rows9=mysqli_fetch_assoc($res9)){
					$data =['mode'=>'Deleted On','date'=>$rows9['delete_on'],'dpt'=>'Export','qty'=>$rows9['quantity'],'qtyType'=>$rows9['q_name'],'user'=>$rows9['author'],'qtytypeId'=>$rows9['qtytypeid']];
					array_push($array,$data);
				}
			}

			if (count($array) > 0){
				$status = true;
			}
		}
	}

	$myObj = [ 'status' => $status,'dataAry'=>$array];
	mysqli_close($con);
	header('Content-type: application/json');
	
	echo json_encode($myObj);
}else if($_POST['type'] == 'getHistoryByStorageMl'){
	$storageId=$_POST['storageId'];
	$filterdt=$_POST['filterdt'];
	$mysqltime = date ('Y-m-d',  strtotime($filterdt));

	$sql="select * from total_purchase_qty where id in (".$storageId.");";
	$res=mysqli_query($con,$sql);

	$status =false;

	$array = array();
	if (mysqli_num_rows($res) > 0) 
	{	
		while($rows=mysqli_fetch_assoc($res)){
			$sql2="select qt.id as qtytypeId,pur.author,p2.description as item,s.name as supplier_name, qt.q_name as quantity_type, pur.quantity, pur.created_at from 
				purchase pur,product p,productitem p2,supplier s,quantity_type qt where 
				pur.item_category = p.id and pur.item = p2.id and pur.supplier_name = s.id and pur.quantity_type = qt.id and DATE(pur.created_at) ='".$mysqltime."' and pur.item = '".$rows['item']."' and pur.quantity_type = '".$rows['quantity_type']."' and pur.ptype=".$rows['ptype'];
			$res2=mysqli_query($con,$sql2);
			if (mysqli_num_rows($res2) > 0) 
			{	
				while($rows2=mysqli_fetch_assoc($res2)){
					$data =['mode'=>'Created On','date'=>$rows2['created_at'],'dpt'=>'Purchase','qty'=>$rows2['quantity'],'qtyType'=>$rows2['quantity_type'],'user'=>$rows2['author'],'qtytypeId'=>$rows2['qtytypeId']];
					array_push($array,$data);
				}
			}

			$sql3="select qt.id as qtytypeId,pur.author, p2.description as item,s.name as supplier_name, qt.q_name as quantity_type, pur.quantity, pur.updated_by from 
				purchase_history pur,product p,productitem p2,supplier s,quantity_type qt where 
				pur.item_category = p.id and pur.item = p2.id and pur.supplier_name = s.id and pur.quantity_type = qt.id and DATE(pur.updated_by) ='".$mysqltime."' and pur.item = '".$rows['item']."' and pur.quantity_type = '".$rows['quantity_type']."' and pur.ptype=".$rows['ptype'];
			
			$res3=mysqli_query($con,$sql3);
			if (mysqli_num_rows($res3) > 0) 
			{	
				while($rows3=mysqli_fetch_assoc($res3)){
					$data =['mode'=>'Updated On','date'=>$rows3['updated_by'],'dpt'=>'Purchase','qty'=>$rows3['quantity'],'qtyType'=>$rows3['quantity_type'],'user'=>$rows3['author'],'qtytypeId'=>$rows3['qtytypeId']];
					array_push($array,$data);
				}
			}

			$sql4="select qt.id as qtytypeId,pur.author,p2.description as item,s.name as supplier_name, qt.q_name as quantity_type, pur.quantity, pur.deleted_on from 
			purchase_history pur,product p,productitem p2,supplier s,quantity_type qt where 
			pur.item_category = p.id and pur.item = p2.id and pur.supplier_name = s.id and pur.quantity_type = qt.id and DATE(pur.deleted_on) ='".$mysqltime."' and pur.item = '".$rows['item']."' and pur.quantity_type = '".$rows['quantity_type']."' and pur.ptype=".$rows['ptype'];
		
			$res4=mysqli_query($con,$sql4);
			if (mysqli_num_rows($res4) > 0) 
			{	
				while($rows4=mysqli_fetch_assoc($res4)){
					$data =['mode'=>'Deleted On','date'=>$rows4['deleted_on'],'dpt'=>'Purchase','qty'=>$rows4['quantity'],'qtyType'=>$rows4['quantity_type'],'user'=>$rows4['author'],'qtytypeId'=>$rows4['qtytypeId']];
					array_push($array,$data);
				}
			}
		}

		$sql5="select ad.storageId,todpt.depart as todrpt,ad.send_to_quantity,sendqt.q_name,ad.created_at as senddttime ,ad.author,sendqt.id as qtytypeid 
			from admin_dpts_mul_history ad ,department todpt,quantity_type sendqt
			where ad.send_to = todpt.id and ad.send_to_qtny_type = sendqt.id and ad.updated_by is null and ad.deleted_by is null and DATE(ad.created_at)='".$mysqltime."' and ad.storageId='".$storageId."';";
		$res5=mysqli_query($con,$sql5);
		if (mysqli_num_rows($res5) > 0) 
		{	
			while($rows5=mysqli_fetch_assoc($res5)){
				$data =['mode'=>'Send On','date'=>$rows5['senddttime'],'dpt'=>$rows5['todrpt'],'qty'=>$rows5['send_to_quantity'],'qtyType'=>$rows5['q_name'],'user'=>$rows5['author'],'qtytypeId'=>$rows5['qtytypeid']];
				array_push($array,$data);
			}
		}

		$sql6="select ad.storageId,todpt.depart as todrpt,ad.send_to_quantity,sendqt.q_name,ad.updated_by as senddttime ,ad.author,sendqt.id as qtytypeid 
			from admin_dpts_mul_history ad ,department todpt,quantity_type sendqt
			where ad.send_to = todpt.id and ad.send_to_qtny_type = sendqt.id and DATE(ad.updated_by)='".$mysqltime."' and ad.storageId='".$storageId."'";
		$res6=mysqli_query($con,$sql6);
		if (mysqli_num_rows($res5) > 0) 
		{	
			while($rows6=mysqli_fetch_assoc($res6)){
				$data =['mode'=>'Updated On','date'=>$rows6['senddttime'],'dpt'=>$rows6['todrpt'],'qty'=>$rows6['send_to_quantity'],'qtyType'=>$rows6['q_name'],'user'=>$rows6['author'],'qtytypeId'=>$rows6['qtytypeid']];
				array_push($array,$data);
			}
		}

		$sql7="select ad.storageId,todpt.depart as todrpt,ad.send_to_quantity,sendqt.q_name,ad.deleted_by as senddttime ,ad.author,sendqt.id as qtytypeid 
			from admin_dpts_mul_history ad ,department todpt,quantity_type sendqt
			where ad.send_to = todpt.id and ad.send_to_qtny_type = sendqt.id and DATE(ad.deleted_by)='".$mysqltime."' and ad.storageId='".$storageId."'";
		$res7=mysqli_query($con,$sql7);
		if (mysqli_num_rows($res7) > 0) 
		{	
			while($rows7=mysqli_fetch_assoc($res7)){
				$data =['mode'=>'Deleted On','date'=>$rows7['senddttime'],'dpt'=>$rows7['todrpt'],'qty'=>$rows7['send_to_quantity'],'qtyType'=>$rows7['q_name'],'user'=>$rows7['author'],'qtytypeId'=>$rows7['qtytypeid']];
				array_push($array,$data);
			}
		}

		$sqlpkg="select pts.author, pts.created_at as senddttime,pts.send_to_quantity,qt.q_name,qt.id as qtytypeid from package_to_stock_history pts ,quantity_type qt where pts.send_to_qtny_type = qt.id and pts.updated_by is null and pts.deleted_by is null and DATE(pts.created_at)='".$mysqltime."' and pts.storageID_ml='".$storageId."'";
		$respkg=mysqli_query($con,$sqlpkg);
		if (mysqli_num_rows($respkg) > 0) 
		{	
			while($rowspkg=mysqli_fetch_assoc($respkg)){
				$data =['mode'=>'Send On','date'=>$rowspkg['senddttime'],'dpt'=>'Packing','qty'=>$rowspkg['send_to_quantity'],'qtyType'=>$rowspkg['q_name'],'user'=>$rowspkg['author'],'qtytypeId'=>$rowspkg['qtytypeid']];
				array_push($array,$data);
			}
		}

		$sqlpkg1="select pts.author,pts.updated_by as senddttime,pts.send_to_quantity,qt.q_name,qt.id as qtytypeid from package_to_stock_history pts ,quantity_type qt where pts.send_to_qtny_type = qt.id and DATE(pts.updated_by)='".$mysqltime."' and pts.storageID_ml='".$storageId."'";
		$respkg1=mysqli_query($con,$sqlpkg1);
		if (mysqli_num_rows($respkg1) > 0) 
		{	
			while($rowspkg1=mysqli_fetch_assoc($respkg1)){
				$data =['mode'=>'Updated On','date'=>$rowspkg1['senddttime'],'dpt'=>'Packing','qty'=>$rowspkg1['send_to_quantity'],'qtyType'=>$rowspkg1['q_name'],'user'=>$rowspkg1['author'],'qtytypeId'=>$rowspkg1['qtytypeid']];
				array_push($array,$data);
			}
		}

		$sqlpkg2="select pts.author,pts.deleted_by as senddttime,pts.send_to_quantity,qt.q_name,qt.id as qtytypeid from package_to_stock_history pts ,quantity_type qt where pts.send_to_qtny_type = qt.id and DATE(pts.deleted_by)='".$mysqltime."' and pts.storageID_ml='".$storageId."'";
		$respkg2=mysqli_query($con,$sqlpkg2);
		if (mysqli_num_rows($respkg2) > 0) 
		{	
			while($rowspkg2=mysqli_fetch_assoc($respkg2)){
				$data =['mode'=>'Deleted On','date'=>$rowspkg2['senddttime'],'dpt'=>'Packing','qty'=>$rowspkg2['send_to_quantity'],'qtyType'=>$rowspkg2['q_name'],'user'=>$rowspkg2['author'],'qtytypeId'=>$rowspkg2['qtytypeid']];
				array_push($array,$data);
			}
		}

		$sql8="select qt.id as qtytypeid,ste.author,ste.id,ste.purchaser_name ,qt.q_name ,ste.quantity ,ste.invoice_dt ,ste.invoice_no ,fp.item,ste.no_of_boxes,ste.created_at  from stock_to_export ste,quantity_type qt ,final_product fp where ste.quantity_type = qt.id and ste.final_productId = fp.id and DATE(ste.created_at)='".$mysqltime."' and storageIdml='".$storageId."'";
		$res8=mysqli_query($con,$sql8);
		if (mysqli_num_rows($res8) > 0) 
		{	
			while($rows8=mysqli_fetch_assoc($res8)){
				$data =['mode'=>'Send On','date'=>$rows8['created_at'],'dpt'=>'Export','qty'=>$rows8['quantity'],'qtyType'=>$rows8['q_name'],'user'=>$rows8['author'],'qtytypeId'=>$rows8['qtytypeid']];
				array_push($array,$data);
			}
		}

		$sql9="select qt.id as qtytypeid,ste.author,ste.id,ste.purchaser_name ,qt.q_name ,ste.quantity ,ste.invoice_dt ,ste.invoice_no ,fp.item,ste.no_of_boxes,ste.delete_on  from stock_to_export_history ste,quantity_type qt ,final_product fp where ste.quantity_type = qt.id and ste.final_productId = fp.id and DATE(ste.delete_on)='".$mysqltime."' and storageIdml='".$storageId."'";
		$res9=mysqli_query($con,$sql9);
		if (mysqli_num_rows($res9) > 0) 
		{	
			while($rows9=mysqli_fetch_assoc($res9)){
				$data =['mode'=>'Deleted On','date'=>$rows9['delete_on'],'dpt'=>'Export','qty'=>$rows9['quantity'],'qtyType'=>$rows9['q_name'],'user'=>$rows9['author'],'qtytypeId'=>$rows9['qtytypeid']];
				array_push($array,$data);
			}
		}

		if (count($array) > 0){
			$status = true;
		}
	}
	

	$myObj = [ 'status' => $status,'dataAry'=>$array];
	mysqli_close($con);
	header('Content-type: application/json');
	
	echo json_encode($myObj);
}else if($_POST['type'] == 'getProcessedProducts'){
	$storageId=$_POST['storageId'];
	/*$filterdt=$_POST['filterdt'];*/
	/*$mysqltime = date ('Y-m-d',  strtotime($filterdt));*/

	$sql="select * from total_purchase_qty where id =".$storageId;
	$res=mysqli_query($con,$sql);

	$status =false;

	$array = array();
	if (mysqli_num_rows($res) > 0) 
	{	
		while($rows=mysqli_fetch_assoc($res)){
			
			$sqlSt="select ad.storageId,p2.item as ptypename,p2.id as ptypeId,ad.id ,todpt.depart as todrpt,p.description as prodname,ad.send_to_quantity,sendqt.q_name,ad.created_at as senddttime ,ad.author,ad.waste,ad.water_wt,sendqt.id as qtytypeid 
					from admin_dpts_history ad ,department todpt,productitem p ,quantity_type sendqt,productype p2 
					where ad.send_to = todpt.id and ad.productItem_id = p.id and ad.send_to_qtny_type = sendqt.id and ad.ptype = p2.id and (ad.admindpt_parent_id = 0 or ad.send_from =11) and ad.updated_by is null and ad.deleted_by is null and storageId=".$storageId;
			$resSt=mysqli_query($con,$sqlSt);
			if (mysqli_num_rows($resSt) > 0) 
			{	
				while($rowsSt=mysqli_fetch_assoc($resSt)){
					$date = new DateTime($rowsSt['senddttime'], new DateTimeZone('MST'));
					$date->setTimezone(new DateTimeZone('Asia/Kolkata'));

					$data =['id'=>$rowsSt['id'],'prodt'=>$rowsSt['prodname'],'date'=>$date->format('Y-m-d H:i:s'),'dpt'=>$rowsSt['todrpt'],'qty'=>$rowsSt['send_to_quantity'],'qtyType'=>$rowsSt['q_name'],'qtytypeId'=>$rowsSt['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sql5="select fp.id as finalproductId,p2.item as ptypename,p2.id as ptypeId,ad.id ,todpt.depart as todrpt,p.description as prodname,fp.item as finalprodct,ad.send_to_quantity,sendqt.q_name,sendqt.id as qtytypeid,ad.created_at as senddttime ,ad.water_wt,ad.author
				from admin_dpts_history ad ,department todpt,productitem p ,final_product fp ,quantity_type sendqt,productype p2 
				where ad.send_to = todpt.id and ad.productItem_id = p.id and ad.final_product = fp.id and ad.send_to_qtny_type = sendqt.id and ad.ptype = p2.id and ad.updated_by is null and ad.deleted_by is null and ad.storageId=".$storageId;
			$res5=mysqli_query($con,$sql5);
			if (mysqli_num_rows($res5) > 0) 
			{	
				while($rows5=mysqli_fetch_assoc($res5)){
					$date = new DateTime($rows5['senddttime'], new DateTimeZone('MST'));
					$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
					$data =['id'=>$rows5['id'],'prodt'=>$rows5['finalprodct'],'date'=>$date->format('Y-m-d H:i:s'),'dpt'=>$rows5['todrpt'],'qty'=>$rows5['send_to_quantity'],'qtyType'=>$rows5['q_name'],'qtytypeId'=>$rows5['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sqlpkg="select fp.item,pts.id,pts.author, pts.created_at as senddttime,pts.send_to_quantity,qt.q_name,qt.id as qtytypeid from package_to_stock pts ,quantity_type qt,final_product fp where pts.send_to_qtny_type = qt.id and pts.final_product = fp.id and pts.storageId=".$storageId;
			$respkg=mysqli_query($con,$sqlpkg);
			if (mysqli_num_rows($respkg) > 0) 
			{	
				while($rowspkg=mysqli_fetch_assoc($respkg)){
					$date = new DateTime($rowspkg['senddttime'], new DateTimeZone('MST'));
					$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
					$data =['id'=>$rowspkg['id'],'prodt'=>$rowspkg['item'],'date'=>$date->format('Y-m-d H:i:s'),'dpt'=>'Packing','qty'=>$rowspkg['send_to_quantity'],'qtyType'=>$rowspkg['q_name'],'qtytypeId'=>$rowspkg['qtytypeid']];
					array_push($array,$data);
				}
			}

			$sql8="select qt.id as qtytypeid,ste.author,ste.id,ste.purchaser_name ,qt.q_name ,ste.quantity ,ste.invoice_dt ,ste.invoice_no ,fp.item,ste.no_of_boxes,ste.created_at  from stock_to_export ste,quantity_type qt ,final_product fp where ste.quantity_type = qt.id and ste.final_productId = fp.id and storage_id=".$storageId;
			$res8=mysqli_query($con,$sql8);
			if (mysqli_num_rows($res8) > 0) 
			{	
				while($rows8=mysqli_fetch_assoc($res8)){
					$date = new DateTime($rows8['created_at'], new DateTimeZone('MST'));
					$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
					$data =['id'=>$rows8['id'],'prodt'=>$rows8['item'],'date'=>$date->format('Y-m-d H:i:s'),'dpt'=>'Export','qty'=>$rows8['quantity'],'qtyType'=>$rows8['q_name'],'qtytypeId'=>$rows8['qtytypeid']];
					array_push($array,$data);
				}
			}

			if (count($array) > 0){
				$status = true;
			}
		}
	}

	$myObj = [ 'status' => $status,'dataAry'=>$array];
	mysqli_close($con);
	header('Content-type: application/json');
	
	echo json_encode($myObj);
}

?>