<?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');
	 $jobworkyear = date('Y', strtotime($mdate1));
	$usermid = $_SESSION['linkariseumid'];
	
	$jobworkmid=$_POST['jobworkmid'];
	$tocompanymid=$_POST['tocompanymid'];
	$fromcompanymid=$_POST['fromcompanymid'];
	$cmpmid=$_SESSION['linkarisecmpmid'];
	if($_SESSION['linkarisecmptype']=="Hotel"){
	$jobworktype="Inword";
	}
	else{
		$jobworktype="Outword";
	}
	$jobworkdate=date("Y-m-d",strtotime($_POST['joworkdate']));
	//$ordermrefdate=date("Y-m-d",strtotime($_POST['ordermrefdate']));
	$jobworkstatus="Dispatched";
	
	
	$query01 = "select companyprefix from res_company_master where companymid=:cmpmid";
	$stmt01 = $db7->prepare($query01);
	$stmt01->bindValue(':cmpmid', "$cmpmid");
	$stmt01->execute();
	$companyprefix="";
	
	if ($stmt01->rowCount() > 0)
	{
		while($rt01 = $stmt01->fetch(PDO::FETCH_ASSOC))
		{
			$companyprefix=$rt01['companyprefix'];
			
		}
	}

	
	$ordermno=0;
	$evemaxq = "SELECT max(jobworknumber) AS ordermno FROM res_jobwork_master where fromcompanymid=$cmpmid";
	$remaxq = mysqli_query($conn, $evemaxq);
	while($rtmaxq = mysqli_fetch_assoc($remaxq))
	{
		$ordermno=$rtmaxq['ordermno'];
	}
	$maxpomno1=$ordermno+1;
	$maxpomno2=$companyprefix.$maxpomno1;
	
	
	
	/*--Order Add--*/
	if($jobworkmid==0)
	{
		if(isset($_COOKIE['mepoc123']))
		{
			$no=$_COOKIE['mepoc123'];
		}
		else
		{
			$no=-555;
		}	
			
		$tt=" and orderdrandomid='$no'";
		
		 $sql = "INSERT INTO res_jobwork_master (fromcompanymid,tocompanymid,jobworkyear,jobworkprefix,jobworknumber,jobworkdate,jobworktype,jobworkstatus,dispatchedby,dispatchedon,jobworknofordisplay) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
		$stmt= $db7->prepare($sql);
		if($stmt->execute([$fromcompanymid,$tocompanymid,$jobworkyear,$companyprefix,$maxpomno1,$jobworkdate,$jobworktype,$jobworkstatus,$usermid,$mdate,$maxpomno2]))
		{
			$jobworkmid = $db7->lastInsertId();
		}
	}
	else
	{
		$tt=" and jobworkmid=$jobworkmid";
	}
	
	/*--Order Update--*/
	 $sql1 = "update res_jobwork_master set tocompanymid=?,jobworkdate=?,jobworktype=?,jobworkstatus=? where jobworkmid=?";
	$stmt1= $db7->prepare($sql1);
	if($stmt1->execute([$tocompanymid,$jobworkdate,$jobworktype,$jobworkstatus,$jobworkmid]))
	{
		$eve1="SELECT jobworkdid from res_jobwork_detail where 1 $tt";
		$re1 = mysqli_query($conn, $eve1);
		
		while ($rt1=mysqli_fetch_assoc($re1)) 
		{
			$jobworkdid=$rt1['jobworkdid'];
		
			
			 $sql4 = "update res_jobwork_detail set jobworkmid=? where jobworkdid=?";
			$stmt4= $db7->prepare($sql4);
			if($stmt4->execute([$jobworkmid,$jobworkdid]))
			{}
		
		 $sql5 = "update res_jobworkitem_detail set jobworkmid=? where jobworkdid=?";
			$stmt5= $db7->prepare($sql5);
			if($stmt5->execute([$jobworkmid,$jobworkdid]))
			{}
		}
		
	
		$_SESSION['msg'] = "Record saved successfully";
	}
	else
	{
		$_SESSION['msg1'] = "Error";
	}

	
	$random= date("Ymd").rand(100000000,999999999);
	setcookie("mepoc123", "$random", time()+3600, "/", "",  0);
	
	unset($_SESSION['currentcustomer']);
	
	//header("Location: jobwork.php");
	header("Location: generatepdf/pdfjobwork.php?jobworkmid=$jobworkmid");
	mysqli_close($conn);	
?>