<?php
session_start();
include '../../db_connect.php';

$reload=0;
$outputMsg;
 foreach($_POST as $key => $value){
	 if(!is_array($_POST[$key]))
$_POST[$key] = mysqli_real_escape_string($con,trim($value));
}
if($_GET['mode']=='insert')
{    
	
	$sql="INSERT INTO  `jobreg` SET  `batchno` = '$_POST[batchno]', `issdate` = STR_TO_DATE('$_POST[issdate]','%d/%m/%Y'), `divdate` = STR_TO_DATE('$_POST[divdate]','%d/%m/%Y'),  `jobcode` = '$_POST[jobcode]', `jobbername` = '$_POST[jobbername]',  `transcode` = '$_POST[transcode]',`transport` = '$_POST[transport]',`department` = '$_POST[department]', `paidby` = '$_POST[paidby]', `Supervisor` = '$_POST[supervisor]',`scontact` = '$_POST[scontact]', `status` = '$_POST[status]', `notes` = '$_POST[notes]' , `disper` = '$_POST[disper]', `pdiscount` = '$_POST[pdiscount]', `pnamount` = '$_POST[pnamount]', `tpercgst` = '$_POST[tpercgst]', `totcgst` = '$_POST[totcgst]', `tpersgst` = '$_POST[tpersgst]', `totsgst` = '$_POST[totsgst]', `pervat` = '$_POST[pervat]', `vat` = '$_POST[vat]', `grtotal` = '$_POST[grtotal]', `refno` = '$_POST[refno]', `refnodate` = STR_TO_DATE('$_POST[refnodate]','%d/%m/%Y'), `challan` = '$_POST[challan]', `challandate` = STR_TO_DATE('$_POST[challandate]','%d/%m/%Y'), `jobcodeto` = '$_POST[jobcodeto]', `departmentto` = '$_POST[departmentto]', `jobstatus` = '$_POST[jobstatus]', `statusto` = '$_POST[jobypeval]'";


if(!mysqli_query($con,$sql)){
  echo 'Error: ' . mysqli_error($con);
  }
  else{
	  $jobid = mysqli_insert_id($con);
	  
	  $prodcode =$_POST['prodcode']; $gencode =$_POST['gencode']; $cat=$_POST['cat']; $colorcode=$_POST['colorcode']; $shortdesc=$_POST['shortdesc']; $sizeset=$_POST['sizeset'];
			$set =$_POST['set'];  $qty =$_POST['qty']; $fresh =$_POST['fresh']; $reprocess =$_POST['reprocess']; $demage =$_POST['demage']; $rate =$_POST['rate']; $amount =$_POST['amount'];
			for ($i = 0, $ser=1; $i < count($gencode); $i++,$ser++)
			  {
				
					$jobSql = "INSERT INTO jobregdet (`entryid`, `jobid`, `srno`, `prodcode`, `cat`, `subcode`, `colorcode`, `shortdesc`, `sizeset`, `set`,  `qty`, `fresh`, `reprocess`, `demage`, `pcost`, `pamt`) VALUES ('', '$jobid', '$ser', '$prodcode[$i]', '$cat[$i]', '$shortdesc[$i]', '$colorcode[$i]', '$sizeset[$i]', '$sizeset[$i]', '$set[$i]', '$qty[$i]', '$fresh[$i]', '$reprocess[$i]', '$demage[$i]' ,'$rate[$i]', '$amount[$i]')";
					
					echo $jobSql;
					if(!mysqli_query($con,$jobSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				
			  } // for ($i = 0; $i < count($_POST['size_pv']); $i++)
	  
	  if(empty($_POST[batchno])){
	 mysqli_query($con,"UPDATE `jobreg` SET `batchno` = '$jobid' WHERE `jobid` = '$jobid'" );
	  }
	  
	  		 
			  
    echo "Record  Insert!! ###".$jobid;
	
  }
   
}

else if($_GET['mode']=='update')
{
	$jobid=$_POST[jobid];
	if(isset($jobid)&&!empty($jobid))
	{
	  $sql="UPDATE `jobreg` SET `batchno` = '$_POST[batchno]',`issdate` = STR_TO_DATE('$_POST[issdate]','%d/%m/%Y'), `divdate` = STR_TO_DATE('$_POST[divdate]','%d/%m/%Y'),  `jobcode` = '$_POST[jobcode]', `jobbername` = '$_POST[jobbername]',  `transcode` = '$_POST[transcode]',`transport` = '$_POST[transport]', `department` = '$_POST[department]', `paidby` = '$_POST[paidby]', `Supervisor` = '$_POST[supervisor]',`scontact` = '$_POST[scontact]', `status` = '$_POST[status]', `statusto` = '$_POST[jobypeval]', `notes` = '$_POST[notes]',
`disper` = '$_POST[disper]',  `pdiscount` = '$_POST[pdiscount]', `pnamount` = '$_POST[pnamount]', `tpercgst` = '$_POST[tpercgst]', `totcgst` = '$_POST[totcgst]', `tpersgst` = '$_POST[tpersgst]', `totsgst` = '$_POST[totsgst]', `pervat` = '$_POST[pervat]', `vat` = '$_POST[vat]', `grtotal` = '$_POST[grtotal]', `refno` = '$_POST[refno]', `challan` = '$_POST[challan]', `challandate` = STR_TO_DATE('$_POST[challandate]','%d/%m/%Y'), `jobcodeto` = '$_POST[jobcodeto]', `departmentto` = '$_POST[departmentto]', `jobidfrom` = '$_POST[jobidfrom]', `refnodate` = STR_TO_DATE('$_POST[refnodate]','%d/%m/%Y'), `jobstatus` = '$_POST[jobstatus]' WHERE `jobid` = $jobid";
	  if(!mysqli_query($con,$sql))
	  {
		  echo 'Error: ' . mysqli_error($con);
	  }
	  else
	  {
		  echo "Record  Updated!! ###";
	  }
  
	}//if jobid set
}

else if($_GET['mode']=='process')
{
	$jobid=$_POST[jobid];
	if(isset($jobid)&&!empty($jobid)){
	  $sql="INSERT INTO `jobreg` set `jobid` = NULL,`batchno` = '$_POST[batchno]', `issdate` = STR_TO_DATE('$_POST[issdate]','%d/%m/%Y'), `divdate` = STR_TO_DATE('$_POST[divdate]','%d/%m/%Y'),  `jobcode` = '$_POST[jobcodeto]', `jobbername` = '$_POST[jobbernameto]', `department` = '$_POST[departmentto]', `transcode` = '$_POST[transcode]',`transport` = '$_POST[transport]', `paidby` = '$_POST[paidby]', `Supervisor` = '$_POST[supervisor]',`scontact` = '$_POST[scontact]', `statusto` = '$_POST[status]', `notes` = '$_POST[notes]' , `refno` = '$_POST[refno]', `refnodate` = STR_TO_DATE('$_POST[refnodate]','%d/%m/%Y'), `jobstatus` = '$_POST[jobstatus]', `challan` = '$_POST[challan]', `challandate` = STR_TO_DATE('$_POST[challandate]','%d/%m/%Y')";
	  if(!mysqli_query($con,$sql))
	  {
		  $outputMsg .= 'Error: ' . mysqli_error($con);
	  } // if(!mysqli_query($con,$sql))
	  else
	  {
		  $insertId	= mysqli_insert_id($con);
		  
		  $multiSql = "INSERT INTO `jobregdet` ( `entryid`, `jobid`, `srno`, `prodcode`, `cat`, `subcode`, `colorcode`, `shortdesc`, `sizeset`, `set`, `ratiotot`, `qty`, `fresh`, `reprocess`, `demage`, `pcost`, `pamt`, `status`)  (select '' ,'$insertId', `srno`, `prodcode`, `cat`, `subcode`, `colorcode`, `shortdesc`, `sizeset`, `set`, `ratiotot`, `qty`, `fresh`, `reprocess`, `demage`, `pcost`, `pamt`, '$_POST[status]' FROM  `jobregdet`  where jobid = $jobid );
		  INSERT INTO jobvar (`entryid`, `job_id`,`srno`, `prodname`, `size`, `qnty`, `fresh`, `reprocess`, `demage`)  (SELECT NULL, '$insertId',`srno`, `prodname`, `size`, `qnty`, `fresh`, `reprocess`, `demage` FROM  `jobvar`  where job_id = $jobid )";
		  if(!mysqli_multi_query($con,$multiSql))
		  	{
			  $outputMsg .= 'Error: ' . mysqli_error($con);
	  		}	//if(!mysqli_multi_query($con,$multiSql))
		  else
		  {
			  
				updateFreshStock($insertId);
				$outputMsg .= "Record  Updated!!".$sql;
			  
		 }	//else(!mysqli_multi_query($con,$multiSql))
	  } //else(!mysqli_query($con,$sql))
  
	}//if(isset($jobid)&&!empty($jobid))
	
	echo $outputMsg."###$insertId";
}

	
function updateFreshStock($jobId)
{
	$sql="select `status` jobid = $jobId'";
			  if($result=mysqli_query($con,$sql))
			  {
					$row =  mysqli_fetch_assoc($result);

					if($row['status']==50)
					{
						if(!mysqli_query($con,"UPDATE prodreg inner join jobregdet on prodreg.code = jobregdet.prodcode  set prodreg.stock = prodreg.stock + jobregdet.fresh  where jobregdet.jobid =  $jobId"))
						$outputMsg .= 'Error: ' . mysqli_error($con);
					}//if($row['status']==50)
					
		  		}	//if($result=mysqli_query($con,$sql))
			  else
				$outputMsg = 'Error: ' . mysqli_error($con);	
}

?>