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

$reload=0;
 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  `sorderreg` SET   `orderdate` = STR_TO_DATE('$_POST[orderdate]','%d/%m/%Y'), `ordertype` = '$_POST[ordertype]', `buyercode` = '$_POST[buyercode]', `refno` = '$_POST[refno]', `dlvdate` = STR_TO_DATE('$_POST[dlvdate]','%d/%m/%Y'), `ratetype` = '$_POST[ratetype]',`rtvalue` = '$_POST[rtvalue]', `shipto` = '$_POST[shipto]', `pos` = '$_POST[pos]', `agentcode` = '$_POST[agentcode]', `billedto` = '$_POST[billedto]', `distr` = '$_POST[distrcode]', `shipvia` = '$_POST[shipvia]', `shippingpaid` = '$_POST[shippingpaid]', `comm` = '$_POST[comm]', `comm_on` = '$_POST[comm_on]', `notes` = '$_POST[notes]', `packinglistno` = '$_POST[packinglistno]', `ptotal` = '$_POST[ptotal]', `disper` = '$_POST[disper]', `pdiscount` = '$_POST[pdiscount]', `pnamount` = '$_POST[pnamount]',  `pervat` = '$_POST[pervat]', `vat` = '$_POST[vat]', `grtotal` = '$_POST[grtotal]', `status` = '$_POST[status]'";


if(!mysqli_query($con,$sql)){
  echo 'Error: ' . mysqli_error($con);
  }
  else{
    echo "Record  Insert!!###".mysqli_insert_id($con);
	
  }
   
}

else if($_GET['mode']=='update'){
	$jobid=$_POST[orderid];
	if(isset($jobid)&&!empty($jobid)){
	  $sql="UPDATE `sorderreg` SET  `orderdate` = STR_TO_DATE('$_POST[orderdate]','%d/%m/%Y'), `ordertype` = '$_POST[ordertype]', `buyercode` = '$_POST[buyercode]', `refno` = '$_POST[refno]', `dlvdate` = STR_TO_DATE('$_POST[dlvdate]','%d/%m/%Y'), `ratetype` = '$_POST[ratetype]',`rtvalue` = '$_POST[rtvalue]', `shipto` = '$_POST[shipto]', `pos` = '$_POST[pos]', `agentcode` = '$_POST[agentcode]', `billedto` = '$_POST[billedto]', `distr` = '$_POST[distrcode]',  `shipvia` = '$_POST[shipvia]', `shippingpaid` = '$_POST[shippingpaid]', `comm` = '$_POST[comm]', `comm_on` = '$_POST[comm_on]', `notes` = '$_POST[notes]', `packinglistno` = '$_POST[packinglistno]', `ptotal` = '$_POST[ptotal]', `disper` = '$_POST[disper]', `pdiscount` = '$_POST[pdiscount]', `pnamount` = '$_POST[pnamount]', `pervat` = '$_POST[pervat]', `vat` = '$_POST[vat]', `grtotal` = '$_POST[grtotal]', `status` = '$_POST[status]' WHERE `orderid` = '$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[orderid];
	if(isset($jobid)&&!empty($jobid)){
	  $sql="INSERT INTO  `spacklistreg` (  `packingid` ,  `packingdate` ,  `packingtype` ,  `buyercode` ,  `refno` ,  `dlvdate` ,  `ratetype` ,   `rtvalue` , `shipto` , `pos`, `agentcode` , `distr` , `billedto`, `shipvia` , `shippingpaid` ,  `comm` ,  `comm_on` ,  `notes`  ,  `ptotal` ,  `disper` , `pdiscount` ,  `pnamount` ,  `pervat` , `vat` ,  `grtotal` ,  `status` ) 
SELECT  NULL,  `orderdate` ,  `ordertype` ,  `buyercode` ,  `orderid` ,  `dlvdate` ,  `ratetype` , `rtvalue` , `shipto` , `pos`,  `agentcode` , `distr` , `billedto`, `shipvia` ,  `shippingpaid` ,  `comm` ,  `comm_on` , `notes`  ,  `ptotal` ,  `disper` , `pdiscount` ,  `pnamount` ,  
`pervat` , `vat` ,  `grtotal` ,  `status` 
FROM  `sorderreg`  WHERE orderid =$jobid";
if(!mysqli_query($con,$sql)){
  echo 'Error: ' . mysqli_error($con);
  }
  else{
	  $processID = mysqli_insert_id($con);
			$sql="INSERT INTO  `spacklistregdet` (  `packingid` ,  `srno` ,  `prodcode` ,  `cat` ,  `subcode` ,  `colorcode` ,  `shortdesc` ,  `sizeset` ,  `set` ,  `qty` ,  `fresh` ,  `reprocess` , `demage` , `sordmrp` , `sorddisc` , `pcost` ,  `pamt` ,  `scost` ) 
SELECT  '$processID' ,  `srno` ,  `prodcode` ,  `cat` ,  `subcode` ,  `colorcode` ,  `shortdesc` ,  `sizeset` ,  `set` ,  `qty` ,  `fresh` ,  `reprocess` ,  `demage` , `sordmrp` , `sorddisc` , `pcost` ,  `pamt` , `scost`  FROM  `sorderregdet`  WHERE orderid =$jobid";
	  if(!mysqli_query($con,$sql)){
		echo 'Error: ' . mysqli_error($con);
		}
	   else{
		   
		   
		   $prodcode=$_POST['prodname_pv'];
		   $size=$_POST['size_pv'];
			$qnty=$_POST['qnty_pv'];
			$fresh=$_POST['fresh_pv'];
			$reprocess=$_POST['reprocess_pv'];
			$demage=$_POST['demage_pv'];
			$entryid = $_POST[entryid_pv];
			echo  $prodcode;
			$in= 1;
			for ($i = 0; $i < count($_POST['size_pv']); $i++) 
			  {
				if($size[$i]!='')
				{
					
					
					$spackinglist = "INSERT INTO spacklistvar SET  `entryid` = '', srno = '$in', `job_id` = '$processID', `prodname` = '$prodcode[$i]', `size` = '$size[$i]', `qnty` = '$qnty[$i]', `fresh` = '$fresh[$i]', `reprocess` = '$reprocess[$i]', `demage` = '$demage[$i]'  ";
					if(!mysqli_query($con,$spackinglist))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
				echo $spackinglist;
				$in++;
			  } 
		   
		   
    echo "Record  Processed!!###".$processID;
	   }
  }
  
	}//if jobid set
	}
	
	
	else if($_GET['mode']=='salesprocess'){
	$jobid=$_POST[orderid];
	if(isset($jobid)&&!empty($jobid)){
	  $sql="INSERT INTO  `spacklistreg` (  `packingid` ,  `packingdate` ,  `packingtype` ,  `buyercode` ,  `refno` ,  `dlvdate` ,  `ratetype` ,  `rtvalue` , `shipto` , `pos`,  `agentcode` ,  `shipvia` , `shippingpaid` ,  `comm` ,  `comm_on` ,  `notes` ,  `packinglistno` ,  `ptotal` ,   `disper` , `pdiscount` ,  `pnamount` ,  `pervat` , `vat` ,  `grtotal` ,  `status` ) 
SELECT  NULL,  `orderdate` ,  `ordertype` ,  `buyercode` ,  `refno` ,  `dlvdate` ,  `ratetype` , `rtvalue` , `shipto` , `pos`, `agentcode` ,  `shipvia` ,  `shippingpaid` ,  `comm` ,  `comm_on` , `notes` ,  `packinglistno` ,  `ptotal` ,  `disper` , `pdiscount` ,  `pnamount` ,  `pervat` ,`vat` ,  `grtotal` ,  `status` 
FROM  `porderreg` 
WHERE orderid =$jobid";
if(!mysqli_query($con,$sql)){
  echo 'Error: ' . mysqli_error($con);
  }
  else{
	  $processID = mysqli_insert_id($con);
			$sql="INSERT INTO  `spacklistregdet` (  `packingid` ,  `srno` ,  `prodcode` ,  `cat` ,  `subcode` ,  `colorcode` ,  `shortdesc` ,  `sizeset` ,  `set` ,  `qty` ,  `fresh` ,  `reprocess` , `demage` , `sordmrp` , `sorddisc` , `pcost` ,  `pamt` ,  `scost` ) 
SELECT  '$processID' ,  `srno` ,  `prodcode` ,  `cat` ,  `subcode` ,  `colorcode` ,  `shortdesc` ,  `sizeset` ,  `set` ,  `qty` ,  `fresh` ,  `reprocess` ,  `demage` , `sordmrp` , `sorddisc` ,  `pcost` ,  `pamt` , `scost` 
FROM  `porderregdet` 
WHERE orderid =$jobid";
	  if(!mysqli_query($con,$sql)){
		echo 'Error: ' . mysqli_error($con);
		}
	   else{
		   
		   
    echo "Record  Processed!!###".$processID;
	   }
  	}
  
	}//if jobid set
}

?>