<?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  `sinvoicereg` 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]', `shipto` = '$_POST[shipto]', `pos` = '$_POST[pos]', `agentcode` = '$_POST[agentcode]',`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]', `pdiscount` = '$_POST[pdiscount]', `pnamount` = '$_POST[pnamount]', `vat` = '$_POST[vat]', `grtotal` = '$_POST[grtotal]', `status` = '$_POST[status]', `rtvalue` = '$_POST[rtvalue]', `formtype` = '$_POST[formtype]', `totparcel` = '$_POST[totparcel]', `lrno` = '$_POST[lrno]', `lrdate` = STR_TO_DATE('$_POST[lrdate]','%d/%m/%Y'), `disamt` = '$_POST[disamt]'";


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 `sinvoicereg` 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]', `shipto` = '$_POST[shipto]', `pos` = '$_POST[pos]', `agentcode` = '$_POST[agentcode]' ,`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]', `pdiscount` = '$_POST[pdiscount]', `pnamount` = '$_POST[pnamount]', `vat` = '$_POST[vat]', `grtotal` = '$_POST[grtotal]', `status` = '$_POST[status]', `rtvalue` = '$_POST[rtvalue]', `formtype` = '$_POST[formtype]', `totparcel` = '$_POST[totparcel]', `lrno` = '$_POST[lrno]', `lrdate` = STR_TO_DATE('$_POST[lrdate]','%d/%m/%Y'), `disamt` = '$_POST[disamt]' 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` ,  `agentcode` ,  `shipvia` , `shippingpaid` ,  `comm` ,  `comm_on` ,  `notes` ,  `packinglistno` ,  `ptotal` ,  `pdiscount` ,  `pnamount` ,  `vat` ,  `grtotal` ,  `status` ) 
SELECT  NULL,  `orderdate` ,  `ordertype` ,  `buyercode` ,  `refno` ,  `dlvdate` ,  `ratetype` , `rtvalue` , `shipto` ,  `agentcode` ,  `shipvia` ,  `shippingpaid` ,  `comm` ,  `comm_on` , `notes` ,  `packinglistno` ,  `ptotal` ,  `pdiscount` ,  `pnamount` ,  `vat` ,  `grtotal` ,  `status` 
FROM  `sinvoicereg` 
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` ,  `pcost` ,  `pamt` ,  `scost` ) 
SELECT  '$processID' ,  `srno` ,  `prodcode` ,  `cat` ,  `subcode` ,  `colorcode` ,  `shortdesc` ,  `sizeset` ,  `set` ,  `qty` ,  `fresh` ,  `reprocess` ,  `demage` ,  `pcost` ,  `pamt` , `scost` 
FROM  `sinvoiceregdet` 
WHERE orderid =$jobid";
	  if(!mysqli_query($con,$sql)){
		echo 'Error: ' . mysqli_error($con);
		}
	   else{
    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` ,  `agentcode` ,  `shipvia` , `shippingpaid` ,  `comm` ,  `comm_on` ,  `notes` ,  `packinglistno` ,  `ptotal` ,  `pdiscount` ,  `pnamount` ,  `vat` ,  `grtotal` ,  `status` ) 
SELECT  NULL,  `orderdate` ,  `ordertype` ,  `buyercode` ,  `refno` ,  `dlvdate` ,  `ratetype` , `rtvalue` , `shipto` ,  `agentcode` ,  `shipvia` ,  `shippingpaid` ,  `comm` ,  `comm_on` , `notes` ,  `packinglistno` ,  `ptotal` ,  `pdiscount` ,  `pnamount` ,  `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` ,  `pcost` ,  `pamt` ,  `scost` ) 
SELECT  '$processID' ,  `srno` ,  `prodcode` ,  `cat` ,  `subcode` ,  `colorcode` ,  `shortdesc` ,  `sizeset` ,  `set` ,  `qty` ,  `fresh` ,  `reprocess` ,  `demage` ,  `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
	}

?>