<?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  `jobreciept` SET `recrefno` = '', `refdate` = STR_TO_DATE('$_POST[refnodate]','%d/%m/%Y'), `challan` = '$_POST[challan]', `jobid` = '$_POST[jobid]', `batchno` = '$_POST[batchno]' ";
if(!mysqli_query($con,$sql)){
  echo 'Error: ' . mysqli_error($con);
  }
else{
	$jobid = mysqli_insert_id($con);	  
    echo "Record  Insert!! ###".$jobid;
  }
   
}

else if($_GET['mode']=='update')
{
	$refno=$_POST[refno];
	if(isset($refno)&&!empty($refno))
	{
	  $sql="UPDATE `jobreciept` SET `refdate` = STR_TO_DATE('$_POST[refnodate]','%d/%m/%Y'), `jobid` = '$_POST[jobid]', `challan` = '$_POST[challan]', `batchno` = '$_POST[batchno]' WHERE `recrefno` = $refno";
	  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]',  `transcode` = '$_POST[transcode]',`transport` = '$_POST[transport]',`department` = '$_POST[departmentto]', `paidby` = '$_POST[paidby]', `Supervisor` = '$_POST[supervisor]',`scontact` = '$_POST[scontact]', `status` = '$_POST[status]', `notes` = '$_POST[notes]'";
	  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`)  (select '' ,'$insertId', `srno`, `prodcode`, `cat`, `subcode`, `colorcode`, `shortdesc`, `sizeset`, `set`, `ratiotot`, `qty`, `fresh`, `reprocess`, `demage`, `pcost`, `pamt` 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!!";
			  
		 }	//else(!mysqli_multi_query($con,$multiSql))
	  } //else(!mysqli_query($con,$sql))
  
	}//if(isset($jobid)&&!empty($jobid))
	
	echo $outputMsg;
}

	
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);	
}

?>