<?php  
error_reporting(E_ERROR | E_PARSE);
	include("../connect.php");
	$par1=$_GET['mobile'];
	$par2=$_GET['password'];
	$par3=$_GET['jdid'];
	$par4=$_GET['trolleyreceivedweight'];


	$response = array();
	$posts = array();
	
	
	date_default_timezone_set("Asia/Singapore");
	$mdate= date('Y-m-d H:i:s');
	$myObj2 = new stdClass();
    $eve1 = "SELECT * FROM res_user_master where  userloginid='$par1' and usersecurepasword='$par2' and userstatus='Active' and userdesignation='User' limit 0,1";
	$re1 = mysqli_query($conn,$eve1);
																	
		if(mysqli_num_rows($re1) > 0)
	{
		//$result="1";
		while($rt11 = mysqli_fetch_assoc($re1))
		{
			$umid=$rt11['usermid'];
			
		}
		 $evenewpass1 = "select * from res_jobwork_detail where jobworkdid='$par3'";
		$renewpass1 = mysqli_query($conn, $evenewpass1);
																	
	
		while($rt1 = mysqli_fetch_assoc($renewpass1))
		{
			$trolleyreceived=$rt1['trolleyreceived'];
			$jobworkmid=$rt1['jobworkmid'];
		}
		if($trolleyreceived==1)
		{
			$result="0";
			$msg = "Already Received";
			
			$posts[] = array('result'=> $result,'msg'=> $msg );
			echo stripslashes(json_encode( array('item' => $posts)));
		
		}
		else
		{
			 $evenewpass = "update res_jobwork_detail set trolleyreceivedgrossweight='$par4',trolleyreceived=1 where jobworkdid='$par3'";
			$renewpass = mysqli_query($conn, $evenewpass);
			$result="1";
			$msg = "Success";
			
			$query = "SELECT jobworkdid, trolleyreceived FROM res_jobwork_detail WHERE jobworkmid = '$jobworkmid'";
			$result1 = mysqli_query($conn, $query);

			if ($result1) 
			{
				$allReceived = true;

				while ($row = mysqli_fetch_assoc($result1)) 
				{
					$jobworkdid = $row['jobworkdid'];
					$trolleyreceived = $row['trolleyreceived'];
					if ($trolleyreceived != 1) 
					{
						$allReceived = false;
						break;
					}
				}
				if ($allReceived) 
				{
					$jobstatus = "Received";
					$evenewpass = "update res_jobwork_master set jobworkstatus='$jobstatus',receivedby=$umid,receivedon='$mdate' where jobworkmid='$jobworkmid'";
					$renewpass = mysqli_query($conn, $evenewpass);
									
				} 
				else 
				{
					$jobstatus = "Dispatched";
				}
			}
		$posts[] = array('result'=> $result,'msg'=> $msg,'jobstatus'=>$jobstatus);
		echo stripslashes(json_encode( array('item' => $posts)));
	}
	}
   else
   {
		$result="0";
		$msg="Failure";
		
		$posts[] = array('result'=> $result,'msg'=> $msg);
		echo stripslashes(json_encode( array('item' => $posts)));
   }
	mysqli_close($conn);
?>
