<?php
session_start();
if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}
	include("connect.php");	header("Access-Control-Allow-Origin: *");
		
		$cmpmid=$_SESSION['linkarisecmpmid'];
			$umid = $_SESSION['linkariseumid'];

	 if(isset($_SESSION['lowerdate']))
	 {
		 $lowerdate = $_SESSION['lowerdate'];
		 $upperdate = $_SESSION['upperdate'];
		$aa = "and Date(jobworkdate) between '$lowerdate' and '$upperdate'";		
	 }else
	 {
		$aa = ""; 
	 }
		
		
	 $eve = "select * from res_jobwork_master where tocompanymid=$cmpmid $aa order by jobworkmid desc";

	$re = mysqli_query($conn, $eve);


	$response = array();
	$posts = array();

	$result="0";

	if(mysqli_num_rows($re) > 0)
	{
		$i=1;
		while($rt = mysqli_fetch_assoc($re))
		{
			 	$jobworkmid=$rt['jobworkmid'];
			 	$jobworknumber=$rt['jobworknumber'];
			 	$dispatchedby=$rt['dispatchedby'];
			 	$jobworkdate=date("d-m-Y",strtotime($rt['jobworkdate']));
				$jobworktime=date("H:i",strtotime($rt['dispatchedon']));
				if($rt['receivedon']=='0000-00-00 00:00:00'){
					$receiveddate="";
			 	$receivedtime="";
			 	$receivedby=0;
				}
				else{
					 	$receiveddate=date("d-m-Y",strtotime($rt['receivedon']));
			 	$receivedtime=date("H:i",strtotime($rt['receivedon']));
			 	$receivedby=$rt['receivedby'];
				}
			
				$iedit="<a href='jobworkedit.php?id=".$rt['jobworkmid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-edit fa-lg'>  </a>";
			
				//$idelete="<a href='jobworkdelete.php?id=".$rt['jobworkmid']."' onclick='return confirm(&#39;Are you sure you want to delete ?&#39;);' data-toggle='tooltip' data-placement='top' title='Delete' class='far fa-trash-alt fa-lg'>  </a>";
				
				$idetail="<a href='jobworkreceivedetail.php?id=".$rt['jobworkmid']."' data-toggle='tooltip' data-placement='top' title='Detail' class='fa fa-eye fa-lg'>  </a>";
			

				$op=$idetail;



				$fromcompanymid=$rt['fromcompanymid'];
				$jobworktype=$rt['jobworktype'];
				$jobworkstatus=$rt['jobworkstatus'];
				$jobworknofordisplay=$rt['jobworknofordisplay'];
				
$companyname="";
$cpname="";
$cmpprefix="";
				$eve71="select * from res_company_master where companymid=$fromcompanymid";
					$re71 = mysqli_query($conn, $eve71);
					while($rt71 = mysqli_fetch_assoc($re71))
					{
						$companyname=$rt71['companyname'];
						$cpname=$rt71['companycontactperson'];
					
						
					}

					$totalweight="";
				$eve72="select SUM(trolleynetweight) as cc from res_jobwork_detail where jobworkmid=$jobworkmid";
					$re72 = mysqli_query($conn, $eve72);
					while($rt72 = mysqli_fetch_assoc($re72))
					{
						$totalweight=number_format($rt72['cc'],2,'.','');
						
					}
					
					$correctionweight="";
				$eve721="select SUM(trolleyreceivedgrossweight) as cc from res_jobwork_detail where jobworkmid=$jobworkmid";
					$re721 = mysqli_query($conn, $eve721);
					while($rt721 = mysqli_fetch_assoc($re721))
					{
						$correctionweight=number_format($rt721['cc'],2,'.','');
						
					}
				
				$totaltareweight="";
				$eve722="select SUM(trolleytareweight) as cc from res_jobwork_detail where jobworkmid=$jobworkmid and trolleyreceived=1";
					$re722 = mysqli_query($conn, $eve722);
					while($rt722 = mysqli_fetch_assoc($re722))
					{
						$totaltareweight=number_format($rt722['cc'],2,'.','');
						
					}
					
					$verifiedweight=$correctionweight-$totaltareweight;
					$verifiedweight=number_format($verifiedweight,2,'.','');
					
					$difference=$totalweight-$verifiedweight;
					$difference=number_format($difference,2,'.','');
				if($_SESSION['linkarisecmptype']=='Hotel'){
				if($jobworkstatus=="Dispatched")
				{
					
					$jobworkstatus1="<div class='badge badge-light-primary fw-bold'>Dispatched From I-Wash</div>";
				}
				else if($jobworkstatus=="Received")
				{
					
					$jobworkstatus1="<div class='badge badge-light-primary fw-bold'>Received By Hotel</div>";
				}
					}
					else{
						if($jobworkstatus=="Dispatched")
				{
					
					$jobworkstatus1="<div class='badge badge-light-primary fw-bold'>Dispatched From Hotel</div>";
				}
				else if($jobworkstatus=="Received")
				{
					
					$jobworkstatus1="<div class='badge badge-light-primary fw-bold'>Received By I-Wash</div>";
				}
					}
				$eve73="select * from res_company_master where companymid=$fromcompanymid";
					$re73 = mysqli_query($conn, $eve73);
					while($rt73 = mysqli_fetch_assoc($re73))
					{
					
						$cmpprefix=$rt73['companyprefix'];
						
						
					}
				$dispatchedbyname="";
				$eve72="select * from res_user_master where usermid=$dispatchedby";
					$re72 = mysqli_query($conn, $eve72);
					while($rt72 = mysqli_fetch_assoc($re72))
					{
						$dispatchedbyname=$cmpprefix."-".$rt72['username'];
						$cpmobile=$rt72['usermobile'];
						
					}
				$eve751="select * from res_company_master where companymid=$cmpmid";
					$re751 = mysqli_query($conn, $eve751);
					while($rt751 = mysqli_fetch_assoc($re751))
					{
					
						$cmpprefix1=$rt751['companyprefix'];
						
						
					}
					$receivedbyname="";
				$eve74="select * from res_user_master where usermid=$receivedby";
					$re74 = mysqli_query($conn, $eve74);
					while($rt74 = mysqli_fetch_assoc($re74))
					{
						$receivedbyname=$cmpprefix1."-".$rt74['username'];
						
						
					}
		$flag=1;
				$eve73="select * from res_jobwork_detail where jobworkmid=$jobworkmid";
					$re73 = mysqli_query($conn, $eve73);
					$n=1;
					while($rt73 = mysqli_fetch_assoc($re73))
					{
						$jobworkdid=$rt73['jobworkdid'];
						$trolleyqrcode=$rt73['trolleyqrcode'];
						$trolleygrossweight=$rt73['trolleygrossweight'];
						$trolleytareweight=$rt73['trolleytareweight'];
						$trolleynetweight=$rt73['trolleynetweight'];
		$query11 = "select count(jobworkdid) as cc from res_jobworkitem_detail where jobworkdid=$rt73[jobworkdid]";
															$stmt11=mysqli_query($conn,$query11);
															
															$totalitem="";
															
															if(mysqli_num_rows($stmt11) > 0)
															{
																while($rt11 = mysqli_fetch_assoc($stmt11))
																{
																	$totalitem=$rt11['cc'];
																	
																}
															}
															$eve22 = "select itemname,itemqty from res_jobworkitem_detail where jobworkdid=$jobworkdid";
				$re22= mysqli_query($conn, $eve22);
				$itemname="";
				$itemqty="";
				if(mysqli_num_rows($re) > 0)
				{
					$t1=1;
					while($rt22 = mysqli_fetch_assoc($re22))
					{
						/* $ufullname=$rt22['ufullname']; */
						
						if($t1==1)
						{
							$itemname = $itemname.$rt22['itemname']."-".$rt22['itemqty'];
							$t1=0;
						}
						else
						{
							$itemname = $itemname.", ".$rt22['itemname']."-".$rt22['itemqty'];
						}
					}
					}
		

									if($flag==1)
					{
				$posts[] = array('srno'=>$i,'jobworkmid'=> $jobworkmid,'jobworknumber'=> $jobworknumber, 'op'=> $op,'cmpname'=> $companyname,'cpname'=> $cpname,'jobworkdate'=> $jobworkdate,'status'=> $jobworkstatus1,'nofordisplay'=>$jobworknofordisplay,'cpmobile'=>$cpmobile,'dispatchedby'=>$dispatchedbyname,'totalweight'=>$totalweight,'verifiedweight'=>$verifiedweight,'difference'=>$difference,'jobworktime'=>$jobworktime,'receiveddate'=>$receiveddate,'receivedtime'=>$receivedtime,'receivedby'=>$receivedbyname,'trolleysno'=>$n,'trolleyqrcode'=>$trolleyqrcode,'trolleygrossweight'=>$trolleygrossweight,'trolleytareweight'=>$trolleytareweight,'trolleynetweight'=>$trolleynetweight,'totalitem'=>$totalitem." (".$itemname.")");
$flag=0; 
					}
					else
					{
						$posts[] = array('srno'=>$i,'jobworkmid'=>'' ,'jobworknumber'=>'' , 'op'=> '','cmpname'=>'' ,'cpname'=>'' ,'jobworkdate'=> '','status'=>'' ,'nofordisplay'=>'','cpmobile'=>'','dispatchedby'=>'','totalweight'=>'','verifiedweight'=>'','difference'=>'','jobworktime'=>'','receiveddate'=>'','receivedtime'=>'','receivedby'=>'','trolleysno'=>$n,'trolleyqrcode'=>$trolleyqrcode,'trolleygrossweight'=>$trolleygrossweight,'trolleytareweight'=>$trolleytareweight,'trolleynetweight'=>$trolleynetweight,'totalitem'=>$totalitem." (".$itemname.")");
					}
			$i++;
			$n++;


		}}

		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('data' => $posts)));
		mysqli_close($conn);
		return;
	}

   else
	{
		$result="0";
		$msg="Invalid Error";

		$posts[] = array('p1'=> $result,'p2'=> $msg);

		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('item' => $posts)));
	}

	mysqli_close($conn);
?>
