<?php
ob_start();
session_start();
require_once("../conn/conn.php");
//include('../utility/chksession.php');
date_default_timezone_set("Asia/Bangkok");


$id_posub=$_POST['id_posub'];
$part_status=$_POST['part_status'];
$id_dep = $_SESSION['id_dep'];
$job_pdid = $_POST['job_pdid'];
$job_round = $_POST['job_round'];
$job_pdid= $_POST['job_pdid'];
$job_qty= $_POST['job_qty'];




if($part_status=='รอดำเนินการQC'){
    $sent_to='D04';
}else if($part_status=='รอดำเนินการงานเครื่องกล'){
    $sent_to='D02';
}else if($part_status=='รอดำเนินการงานเชื่อม'){
    $sent_to='D03';
}


echo $part_status;?></br><?
echo $id_dep;?></br><?
echo $id_posub;?></br><?

//exit;
if($part_status=='รอดำเนินการQC'){
    
    $sql = "UPDATE pd_posub SET part_status = '$part_status'  WHERE id_posub = '$id_posub'";
	$query = mysqli_query($con,$sql);
	

        if($query){
           
            $sqljob = "INSERT INTO `pd_history` 
            (`pdh_id`,`job_round`,`id_posub`,`id_dep`,`job_pdid`,`job_qty`,`sent_to`,`sent_date`) 
            VALUES 
            ('NULL','$job_round','$id_posub','$id_dep','$job_pdid','$job_qty','$sent_to',NOW())";
        	$queryjob = mysqli_query($con,$sqljob);
	//exit;
	    if($queryjob){
	    
            ?>
                <script language="JavaScript" type="text/javascript">
                    <!--
                    alert("บันทึกข้อมูลเรียบร้อย");
                    location.href='../user/user_job.php';
                    //-->
                </script>
            <?php
        }else{
            
            echo "Error: " . $sql . "<br>" . mysqli_error($con);

        }
        }
        mysqli_close($con);
    
    

    
}else{
    //กรณีผลิต
  	$strSQL = "SELECT * FROM pd_ver
			where id_posub='$id_posub'
			and id_dep='$id_dep'";
	$objQuery = mysqli_query($con,$strSQL); 
	$objResult = mysqli_fetch_array($objQuery);
	
	$addround= $objResult['job_round']+1;
  
    $sql1 = "UPDATE pd_ver SET job_round = '$addround'  WHERE id_posub='$id_posub'
			and id_dep='$id_dep'";
	$query1 = mysqli_query($con,$sql1);
	
    $sql = "UPDATE pd_posub SET part_status = '$part_status'  WHERE id_posub = '$id_posub'";
	$query = mysqli_query($con,$sql);
	
        if($query){
            
 
            $sqljob = "INSERT INTO `pd_history` 
            (`pdh_id`,`job_round`,`id_posub`,`id_dep`,`job_pdid`,`job_qty`,`sent_to`,`sent_date`) 
            VALUES 
            ('NULL','$job_round','$id_posub','$id_dep','$job_pdid','$job_qty','$sent_to',NOW())";
        	$queryjob = mysqli_query($con,$sqljob);
            if($queryjob)
            ?>
                <script language="JavaScript" type="text/javascript">
                    <!--
                    alert("บันทึกข้อมูลเรียบร้อย");
                    location.href='../user/user_job.php';
                    //-->
                </script>
            <?php
            }else{
            
            echo "Error: " . $sql . "<br>" . mysqli_error($con);

        }
        mysqli_close($con);
    
}

?>