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

if($_GET['mode']=="update")
{
if($_GET[vchtype]=='PURCHASE'){
$sql="UPDATE purcdetails SET `billno` = '$_POST[bno]',  `amount` = '$_POST[amt]',`crbillno` = '$_POST[crbno]',  `cramount` = '$_POST[cramt]', `paytype` = '$_POST[ptype]',`vchtype`='$_POST[vtype]',`timestamp` = 'NOW()' where `entryid` = '$_POST[eid]' and `vchno` = '$_POST[vno]'  ";
}
else{
$sql="UPDATE vchdetails SET `billno` = '$_POST[bno]', `refno` = '$_POST[p_refno]',   `amount` = '$_POST[amt]',`crbillno` = '$_POST[crbno]',  `cramount` = '$_POST[cramt]', `paytype` = '$_POST[ptype]', `angst_d` = '$_POST[angst_d]',`vchtype`='$_POST[vtype]',`timestamp` = 'NOW()' where `entryid` = '$_POST[eid]' and `vchno` = '$_POST[vno]'  ";
}

if(mysqli_query($con,$sql))
	echo "Record updated Successfully!!! ";
	else
	echo "Error:".mysqli_error($con);
	
}//if update ends


else if($_GET['mode']=="insert"){
		if($_GET[vchtype]=='PURCHASE'){
		$sql="INSERT INTO purcdetails (`vchno`,  `billno`, `amount` ,`crbillno`, `cramount` ,`paytype`, `vchtype`,`timestamp`) VALUES ('$_GET[vchno]', '$_POST[bno]',  '$_POST[amt]', '$_POST[crbno]',  '$_POST[cramt]','$_POST[ptype]','$_POST[vtype]', 'NOW()')";
		
		}
		
		else{
      $sql = "INSERT INTO vchdetails (`vchno`,  `billno`, `refno`, `amount` ,`crbillno`, `cramount` ,`paytype`, `angst_d`, `vchtype`,`timestamp`) VALUES ('$_GET[vchno]', '$_POST[bno]', '$_POST[p_refno]' ,  '$_POST[amt]', '$_POST[crbno]',  '$_POST[cramt]','$_POST[ptype]', '$_POST[angst_d]','$_POST[vtype]', 'NOW()')";
            }
	if(mysqli_query($con,$sql)){ 
/*	$ped=0;
	if($_POST['amt']>$_POST['amount'])
	{
	$ped=$_POST['amt']-$_POST['amount'];
	 $ped=number_format($ped, 2, '.', '');
	}*/
	if ($_POST[bno] != ''){
		
	$paid=number_format(abs($_POST[amt]), 2, '.', '');
	if($_GET[vchtype]=='RECEIPT'||$_GET[vchtype]=='JOURNAL'){
		if($_POST[vtype]=='PURCHASE'){
			
		$squery="UPDATE  purcreg SET  `paid` = paid+'$paid',`timestamp` = 'NOW()' WHERE  `bill_no` ='$_POST[bno]'";
		}
        elseif($_POST[vtype]=='SALESCF'){
        $squery="UPDATE  $prebillreg SET  `paid` = paid+'$paid',`timestamp` = 'NOW()' WHERE  `billno` ='$_POST[bno]';update commst set usedlimit=usedlimit-'$paid' where compid ='$_GET[comcode]'";
		}
		elseif($_POST[vtype]=='SALES'){
		$squery="UPDATE  $billreg SET  `paid` = paid+'$paid' WHERE  `orderid` ='$_POST[bno]'";
		}
		 elseif($_POST[vtype]=='ADVRECPT'){
		$squery="UPDATE  vchdetails SET  crbillno = '$_GET[vchno]', `cramount` = cramount+'$paid',`timestamp` = 'NOW()' WHERE  `entryid` ='$_POST[bno]'";
		}
		
        else{
		$squery="UPDATE  vchreg SET  `paid` = paid+'$paid',`timestamp` = 'NOW()' WHERE  `bill_no` ='$_POST[bno]'";
          }
}
    elseif($_GET[vchtype]=='PURCHASE'){
           $squery="UPDATE  $bookingreg SET  `Vbillno` = '$_GET[bill_no]',`timestamp` = 'NOW()' WHERE  `bookid` ='$_POST[bno]'";}
    elseif ($_GET[vchtype]=='PAYMENT'){
		if($_POST[vtype]=='ADVRECPT'){
				$squery="UPDATE  vchdetails SET  crbillno = '$_GET[vchno]', `cramount` = cramount+'$paid',`timestamp` = 'NOW()' WHERE  `entryid` ='$_POST[bno]'";
			}
		else{
		$squery="UPDATE  purcreg SET  `paid` = `paid`+'$paid',`timestamp` = 'NOW()' WHERE  `bill_no` ='$_POST[bno]' and `comcode` = '$_GET[comcode]' and `vchtype` = 'PURCHASE' ;";}
	}
		
	else{
		$squery="UPDATE  vchreg SET  `paid` = paid+'$paid',`timestamp` = 'NOW()' WHERE  `bill_no` ='$_POST[bno]'";
          }

	
	
	
	//if(mysqli_query($con,"UPDATE  $billreg SET  `paid` = paid+'$paid' WHERE  `billno` ='$_POST[bno]';"))
	
	if(mysqli_multi_query($con,$squery))
	echo "Record Inserted Successfully!!!";
	else
	echo "Error:".mysqli_error($con);
	}// if brno end

	if ($_POST[crbno] != ''){
		$crpaid=number_format($_POST[cramt], 2, '.', '');
		if($_GET[vchtype]=='RECEIPT'||$_GET[vchtype]=='JOURNAL'){
		if($_POST[vtype]=='PURCHASE'){
			
		$squery="UPDATE  purcreg SET  `paid` = paid+'$crpaid',`timestamp` = 'NOW()' WHERE  `bill_no` ='$_POST[crbno]'";
		}
        elseif($_POST[vtype]=='SALESCF'){
        $squery="UPDATE  $prebillreg SET  `paid` = paid+'$crpaid',`timestamp` = 'NOW()' WHERE  `billno` ='$_POST[crbno]';update commst set usedlimit=usedlimit-'$crpaid' where compid ='$_GET[comcode]'";
		}
		elseif($_POST[vtype]=='SALES'){
		$squery="UPDATE  $billreg SET  `paid` = paid+'$paid',`timestamp` = 'NOW()' WHERE  `orderid` ='$_POST[crbno]'";
		}
		 elseif($_POST[vtype]=='ADVRECPT'){
		$squery="UPDATE  vchdetails SET  crbillno = '$_GET[vchno]', `cramount` = cramount+'$crpaid',`timestamp` = 'NOW()' WHERE  `entryid` ='$_POST[crbno]'";
		}
		elseif($_POST[vtype]=='ADVPAYMENT'){
		  $squery="UPDATE  vchdetails SET   `cramount` = cramount+'$_POST[cramt]',`timestamp` = 'NOW()' WHERE  `entryid` ='$_POST[p_refno]'";
			}
        else{
		$squery="UPDATE  vchreg SET  `paid` = paid+'$crpaid',`timestamp` = 'NOW()' WHERE  `refno` ='$_POST[crbno]'";
		}
		}
		if(mysqli_multi_query($con,$squery))
		echo "Record updated Successfully!!!";
		else
		echo "Error:".mysqli_error($con);
	}//if crbno end

} //if $con end 
else
echo " Error:".mysqli_error($con);

}//if insert query ends

else if($_GET[mode]=='delete')
{
	
	if(isset($_GET['tmode'])) // mass delete purcdetails for bulkbooking
	{
	if($_GET[vtype]=='PURCHASE'){
		 $sql="DELETE from purcdetails where vchno='$_GET[vchno]';"; 
		 	}
			
			if(!mysqli_multi_query($con,$sql))
 	{
  	die('Error:  '.$_GET[vchno] . mysqli_error($con));
 	 }
	echo "Record Deleted Successfully!!!";
	}
	
	 if ($_POST[eid] != ''){
		
			if($_POST[vtype]=='PURCHASE'){
			//$sql="UPDATE  purcreg set paid=paid-$_POST[amt] where `paid`-$_POST[amt]>=0 and bill_no='$_POST[bno]'; ";
			  $sql="DELETE from purcdetails where entryid='$_POST[eid]';"; 
			  }
			elseif($_POST[vtype]=='SALES'){
			$sql="UPDATE $billreg set `paid`= CASE WHEN `paid`-$_POST[amt] < 0 THEN 0 ELSE `paid`-$_POST[amt] END where  orderid='$_POST[bno]';";
			$sql.="DELETE from vchdetails where entryid='$_POST[eid]';";}
			elseif($_POST[vtype]=='SALESCF'){
			$sql="UPDATE  $prebillreg set `paid`= CASE WHEN `paid`-$_POST[amt] < 0 THEN 0 ELSE `paid`-$_POST[amt] END where  billno='$_POST[bno]';update commst set usedlimit=`usedlimit`-$_POST[amt]  where compid ='$_GET[comcode]';";
			$sql.="DELETE from vchdetails where entryid='$_POST[eid]';";}
			elseif($_POST[vtype]=='ADVRECPT'){
			$sql="UPDATE  vchdetails SET  crbillno = '$_GET[vchno]', `cramount` = cramount-$_POST[amt] WHERE  `entryid` ='$_POST[bno]'";
			$sql.="DELETE from vchdetails where entryid='$_POST[eid]';";	
				}
				elseif($_POST[vtype]=='ADVPAYMENT'){
			$sql="UPDATE  vchdetails SET  `cramount` = `cramount`-$_POST[cramt] WHERE  `entryid` ='$_POST[p_refno]'";
			//$sql.="DELETE from vchdetails where entryid='$_POST[eid]'";	
				}
            elseif ($_GET[vchtype]=='PAYMENT'){
			$sql="UPDATE purcreg set `paid`= CASE WHEN `paid`-$_POST[amt] < 0 THEN 0 ELSE `paid`-$_POST[amt] END  where  `bill_no`='$_POST[bno]' and `comcode` = '$_GET[comcode]' and `vchtype` = 'PURCHASE'; ";	
			$sql.="DELETE from vchdetails where entryid='$_POST[eid]';";}
			else{
        	$sql="UPDATE  vchreg set `paid`= CASE WHEN `paid`-$_POST[amt] < 0 THEN 0 ELSE `paid`-$_POST[amt] END where  bill_no='$_POST[bno]'; ";
		
			//$sql="UPDATE $billreg set paid=paid-$_POST[amt] where `paid`-$_POST[amt]>=0 and billno='$_POST[bno]'; ";	
			$sql.="DELETE from vchdetails where entryid='$_POST[eid]';";
			}
			
		
			
	if(!mysqli_multi_query($con,$sql))
 	{
  	die('Error:  ' . mysqli_error($con));
 	 }
	echo "Record Deleted Successfully!!!".$sql;

	}//if eid ends
	
	if ($_POST[crbno] != ''){
		if($_POST[vtype]=='PURCHASE'){
		$sql="UPDATE  purcreg set paid=paid-$_POST[cramt] where `paid`-$_POST[cramt]>=0 and bill_no='$_POST[crbno]'; ";}
		else{
		$sql="UPDATE vchreg set paid=paid-$_POST[cramt], `timestamp` = 'NOW()' where paid-$_POST[cramt]>=0 and refno='$_POST[crbno]'; ";	}
		$sql.="DELETE from vchdetails where entryid='$_POST[eid]';";

	if(!mysqli_multi_query($con,$sql))
 	{
  	die('Error: ' . mysqli_error($con));
 	 }
	echo "Record Deleted Successfully!!!";

	}// if crbno ends
}

mysqli_close($con);


?>

