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

$jobh_id=$_POST['jobh_id'];
$id_posub=$_POST['id_posub'];
$part_qty=$_POST['part_qty'];
$id_dep = $_SESSION['id_dep'];
$job_pdid=$_POST['job_pdid'];
$job_date=$_POST['job_date'];
$job_step=$_POST['job_step'];
$job_respon=$_POST['job_respon'];
$job_qty=$_POST['job_qty'];
$id_dep = $_SESSION['id_dep'];
$stop_total =$_POST['stop_total'];


if($job_qty>$part_qty){
    ?>
    <script language="JavaScript" type="text/javascript">
                        <!--
                        alert("คุณระบุยอดการผลิตเกินจำนวนที่กำหนด !!");
                        location.href='../user/user_jobsub2.php?id_posub=<?=$id_posub;?>';
                        //-->
                    </script>
    <?
    
    
}else{
  
  
$strSQL = "SELECT * FROM `pd_jobver` where id_posub ='$id_posub' and id_dep='$id_dep'";
$objQuery = mysqli_query($con,$strSQL); 
$objResult = mysqli_fetch_array($objQuery);
$num = mysqli_num_rows($objQuery);
echo $num;

if($num==0){
   echo "เพิ่มข้อมุลในตาราง Ver"; 

        $sql = "INSERT INTO `pd_jobver` (`ver_id`, `id_dep`, `job_round`, `job_pdid`, `id_posub`) VALUES ('NULL', '$id_dep', '1', '$job_pdid','$id_posub')";
	    $query = mysqli_query($con,$sql);
	    
	    
        if($query){
            echo "สร้างเรียบร้อย  ";
            
            $sqlstep = "INSERT INTO `pd_jobstep` (`id_stepjob`, `job_round`, `id_dep`, `job_pdid`, `stop_total`) VALUES ('NULL', '1', '$id_dep', '$job_pdid','$stop_total')";
	        $querystep = mysqli_query($con,$sqlstep);
        
    
            $strSQL1 = "SELECT * FROM `pd_jobver` where id_posub ='$id_posub'";
            $objQuery1 = mysqli_query($con,$strSQL1); 
            $objResult1 = mysqli_fetch_array($objQuery1);
            
            $round = $objResult1['job_round'];
            echo $round;
            
            $sqljob = "INSERT INTO `pd_job2` (`job_id`,`job_round`,`jobh_id`,`id_posub`,`id_dep`,`job_pdid`,`job_date`,`job_step`,`job_respon`,`job_qty`,`job_record`) 
            VALUES ('NULL','$round','$jobh_id','$id_posub','$id_dep','$job_pdid','$job_date','$job_step','$job_respon','$job_qty',NOW())";
	        $queryjob = mysqli_query($con,$sqljob);
                if($queryjob){
                    ?>
                    <script language="JavaScript" type="text/javascript">
                        <!--
                        alert("บันทึกข้อมูลเรียบร้อย");
                        location.href='../user/user_jobsub2.php?id_posub=<?=$id_posub;?>';
                        //-->
                    </script>
                    <?php
                }else{
                    
                    echo "Error: " . $sqljob . "<br>" . mysqli_error($con);
        
                }
        mysqli_close($con);
            
            
        }else{
            
            echo "Error: " . $sql . "<br>" . mysqli_error($con);

        }
        mysqli_close($con);
    
}else{
    echo "มีข้อมูลแล้วให้อัพเดต Ver";
    
    $strSQL2 = "SELECT * FROM `pd_jobver` where id_posub ='$id_posub' and id_dep='$id_dep'";
    $objQuery2 = mysqli_query($con,$strSQL2); 
    $objResult2 = mysqli_fetch_array($objQuery2);
    
    $dep = $objResult2['id_dep'];
    $ver = $objResult2['job_round'];
    
    echo $dep;?></br><?
    echo 'รอบจาก master='.$ver;?></br><?
    
    
    
    $strSQL3 = "SELECT id_posub,MAX(job_round),id_dep FROM `pd_job2` where id_posub ='$id_posub'";
    $objQuery3 = mysqli_query($con,$strSQL3); 
    $objResult3 = mysqli_fetch_array($objQuery3);
    echo 'รอบท้ายสุด='.$objResult3['MAX(job_round)'];?></br><?
    $old=$objResult3['MAX(job_round)'];
    

    //exit;
    
    if($ver!=$objResult3['MAX(job_round)']){
        
        echo 'อัพเดตรอบใหม่ในข้อมูล jobsub'?></br><?;
        
        $sqlstep = "INSERT INTO `pd_jobstep` (`id_stepjob`, `job_round`, `id_dep`, `job_pdid`, `stop_total`) VALUES ('NULL', '$ver', '$id_dep', '$job_pdid','$stop_total')";
	    $querystep = mysqli_query($con,$sqlstep);
        
        
            $sqljob = "INSERT INTO `pd_job2` (`job_id`,`job_round`,`jobh_id`,`id_posub`,`id_dep`,`job_pdid`,`job_date`,`job_step`,`job_respon`,`job_qty`,`job_record`) 
            VALUES ('NULL','$ver','$jobh_id','$id_posub','$id_dep','$job_pdid','$job_date','$job_step','$job_respon','$job_qty',NOW())";
	        $queryjob = mysqli_query($con,$sqljob);
                if($queryjob){
                    ?>
                    <script language="JavaScript" type="text/javascript">
                        <!--
                        alert("บันทึกข้อมูลเรียบร้อย");
                        location.href='../user/user_jobsub2.php?id_posub=<?=$id_posub;?>';
                        //-->
                    </script>
                    <?php
                }else{
                    
                    echo "Error: " . $sqljob . "<br>" . mysqli_error($con);
        
                }
            mysqli_close($con);    
    }else{
        
        echo 'ใช้รอบเดิม';?></br><?
        $sqljob = "INSERT INTO `pd_job2` (`job_id`,`job_round`,`jobh_id`,`id_posub`,`id_dep`,`job_pdid`,`job_date`,`job_step`,`job_respon`,`job_qty`,`job_record`) 
            VALUES ('NULL','$old','$jobh_id','$id_posub','$id_dep','$job_pdid','$job_date','$job_step','$job_respon','$job_qty',NOW())";
	        $queryjob = mysqli_query($con,$sqljob);
                if($queryjob){
                    ?>
                    <script language="JavaScript" type="text/javascript">
                        <!--
                        alert("บันทึกข้อมูลเรียบร้อย");
                        location.href='../user/user_jobsub2.php?id_posub=<?=$id_posub;?>';
                        //-->
                    </script>
                    <?php
                }else{
                    
                    echo "Error: " . $sqljob . "<br>" . mysqli_error($con);
        
                }
            mysqli_close($con);  
    
        
    }
    
    
}
    
    
}










?>