<?php
session_start(); 
	
	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
	}

	include("connect.php");
	include("connectp.php");
	
	date_default_timezone_set("Asia/Singapore");
	$mdate= date('Y-m-d H:i:s');
	$mdate1= date('Y-m-d');
	
	$usermid = $_SESSION['linkariseumid'];
	
	
	$jobworkmid=$_POST['jobworkmid'];
	$jobworkdid=$_POST['jobworkdid'];
	$trolleymid=$_POST['trolleymid'];
    $trolleytareweight=$_POST['trolleytareweight'];
	$trolleygrossweight=$_POST['trolleygrossweight'];
	$trolleynetweight=$_POST['trolleynetweight'];
	
$query1 = "select trolleyqrcode from res_trolley_master where trolleymid=:trolleymid";
	$stmt1 = $db7->prepare($query1);
	$stmt1->bindValue(':trolleymid', "$trolleymid");
	$stmt1->execute();
	$trolleyqrcode="";
	
	if ($stmt1->rowCount() > 0)
	{
		while($rt1 = $stmt1->fetch(PDO::FETCH_ASSOC))
		{
			$trolleyqrcode=$rt1['trolleyqrcode'];
			
		}
	}
	
	if($jobworkmid==0)
	{
		$cookiekey=$_COOKIE['mepoc123'];
		
		if(strlen($cookiekey) < 3 ){
			//setcookie("jm123", $random);
			//echo "<br>11: ".$random= rand(1000000,9999999999);
			$random= date("Ymd").rand(100000000,999999999);
			//setcookie("jm123", "$random", time()+3600, "/", "",  0);
			setcookie("mepoc123", $random, time() + (86400 * 1), "/"); // expire after 1 day
			$cookiekey = $random;
		}
		else
		{
			$cookiekey=$_COOKIE['mepoc123'];
		}
	}
	else
	{
		$query = "select orderdrandomid from res_jobwork_detail where jobworkmid=:jobworkmid";
		$stmt = $db7->prepare($query);
		$stmt->bindValue(':jobworkmid', "$jobworkmid");
		$stmt->execute();
		if ($stmt->rowCount() > 0)
		{
			while($rt = $stmt->fetch(PDO::FETCH_ASSOC))
			{
				$cookiekey=$rt['orderdrandomid'];
			}
		}
	}


	if($jobworkdid==0)
	{
		/*-----If already added item update item detail----------*/
		$query2 = "select jobworkdid from res_jobwork_detail where orderdrandomid=:orderdrandomid and trolleymid=:trolleymid";
		$stmt2 = $db7->prepare($query2);
		$stmt2->bindValue(':orderdrandomid', "$cookiekey");
		$stmt2->bindValue(':trolleymid', "$trolleymid");
		$stmt2->execute();
		if ($stmt2->rowCount() > 0)
		{
			while($rt2 = $stmt2->fetch(PDO::FETCH_ASSOC))
			{
				$jobworkdid=$rt2['jobworkdid'];
				
				if($trolleymid==0 || $trolleymid=="")
				{}
				else
				{
					$sql01 = "update res_jobwork_detail set jobworkmid=? trolleymid=?, trolleyqrcode=?, trolleytareweight=?, trolleygrossweight=?, trolleynetweight=? where trolleymid=? and jobworkdid=? and orderdrandomid=?";
					$stmt01= $db7->prepare($sql01);
					if($stmt01->execute([$jobworkmid,$trolleymid,$trolleyqrcode,$trolleytareweight,$trolleygrossweight,$trolleynetweight,$trolleymid,$jobworkdid,$cookiekey]))
					{}
				}
			}
		}
		else
		{
			if($trolleymid==0 || $trolleymid=="")
			{}
			else
			{
				 $sql = "INSERT INTO res_jobwork_detail (jobworkmid,trolleymid,trolleyqrcode,trolleytareweight,trolleygrossweight,trolleynetweight,trolleyreceived,orderdrandomid) VALUES (?,?,?,?,?,?,?,?)";
				$stmt= $db7->prepare($sql);

				if($stmt->execute([$jobworkmid,$trolleymid,$trolleyqrcode,$trolleytareweight,$trolleygrossweight,$trolleynetweight,0,$cookiekey]))
				{
					$last_id = $db7->lastInsertId();
					$_SESSION['msg'] = "Record saved successfully";
				} 
				else
				{
					$_SESSION['msg1'] = "Error";
				}
			}
		}
	}
	else
	{
		/*-----If already added item update item detail----------*/
		$query2 = "select jobworkdid from res_jobwork_detail where orderdrandomid=:orderdrandomid and trolleymid=:trolleymid and jobworkdid=:jobworkdid";
		$stmt2 = $db7->prepare($query2);
		$stmt2->bindValue(':orderdrandomid', "$cookiekey");
		$stmt2->bindValue(':trolleymid', "$trolleymid");
		$stmt2->bindValue(':jobworkdid', "$jobworkdid");
		$stmt2->execute();
		if ($stmt2->rowCount() > 0)
		{
			while($rt2 = $stmt2->fetch(PDO::FETCH_ASSOC))
			{
				if($trolleymid==0 || $trolleymid=="")
				{}
				else
				{
					$sql01 = "update res_jobwork_detail set trolleytareweight=?, trolleygrossweight=?, trolleynetweight=? where trolleymid=? and jobworkdid=? and orderdrandomid=?";
					$stmt01= $db7->prepare($sql01);
					if($stmt01->execute([$trolleytareweight,$trolleygrossweight,$trolleynetweight,$trolleymid,$jobworkdid,$cookiekey]))
					{}
				}
			}
		}
		else
		{
			if($trolleymid==0 || $trolleymid=="")
			{}
			else
			{
				$sql01 = "update res_jobwork_detail set trolleymid=?, trolleyqrcode=?, trolleytareweight=?, trolleygrossweight=?, trolleynetweight=?, trolleyreceived=? where jobworkdid=? and orderdrandomid=?";
				$stmt01= $db7->prepare($sql01);
				if($stmt01->execute([$trolleymid,$trolleyqrcode,$trolleytareweight,$trolleygrossweight,$trolleynetweight,0,$jobworkdid,$cookiekey]))
				{}
			}
		}
	}
	
	if($jobworkmid!=0)
	{
		header("Location: jobworkadd.php?jobworkmid=$jobworkmid");
	}
	else
	{
		header('Location: jobworkadd.php');
	}
	mysqli_close($conn);	
?>