<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php'); ?>
<?php 
if(isset($_POST['pay_invoice'])){
   $dr_acid    = $_POST['dr_acid'];
   $invoice    = $_POST['pay_invoice'];
   $cr_acid    = $_POST['cr_acid'];
   $newamount  = $_POST['amount'];
   echo $ttp        = $_POST['ttp'];
   $cheques_no = $_POST['cheques_no'];
   $details    = $_POST['details'];
   $tdate      = $_POST['tdate'].' '.$time;



		$chinvoice=mysqli_query($conn,"SELECT invoice FROM transaction WHERE coid='$coid' AND invoice='$invoice' LIMIT 1");
		if (mysqli_num_rows($chinvoice)>0 && $newamount < 0.01) {}else {

		
           


if($ttp =='P'){

echo $ttp;
echo $ccr_acid = $cr_acid;
echo $ddr_acid = $dr_acid;


$dr_gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$ddr_acid' LIMIT 1"))['gcatid'];

 $cr_gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$ccr_acid' LIMIT 1"))['gcatid']; 


echo $ccr_gcatid = $cr_gcatid;
echo $ddr_gcatid =$dr_gcatid;




}else if($ttp =='R_R'){

$ccr_acid = $dr_acid;
$ddr_acid = $cr_acid;

$dr_gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$ccr_acid' LIMIT 1"))['gcatid'];

 $cr_gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$ddr_acid' LIMIT 1"))['gcatid']; 
 
$ccr_gcatid = $dr_gcatid;
$ddr_gcatid =$cr_gcatid;


}



         mysqli_query($conn,"INSERT INTO transaction(coid,bid,brid,dr_acid,cr_acid,e_acid,dr_gcatid,cr_gcatid,invoice,ttype,details,note,amount,date,ptime,userid,em_id,active) 
	        VALUES ('$coid','$bid','$brid','$ccr_acid','$ddr_acid','$uid', '$ccr_gcatid','$ddr_gcatid','$invoice','$ttp','$details','$cheques_no','$newamount','$tdate','$time','$uid','$us_em_id','1')") or die(mysqli_error($conn));

        mysqli_query($conn,"UPDATE tran_cheque SET active='1' WHERE coid='$coid' AND invoice ='$invoice'") or die(mysqli_error($conn));





        $dr_balance = balance($dr_acid)['ac_balance'];
        $cr_balance = balance($cr_acid)['ac_balance'];
        mysqli_query($conn,"UPDATE accounts SET balance='$dr_balance' WHERE coid='$coid' AND acid ='$dr_acid'") or die(mysqli_error($conn));
        mysqli_query($conn,"UPDATE accounts SET balance='$cr_balance' WHERE coid='$coid' AND acid ='$cr_acid'") or die(mysqli_error($conn));
			
        if (isset($_POST['email'])) {
        	// Email Part Here
        }
        
      
    $prebalance=$dr_balance-$newamount;
     $PhoneNumber = mysqli_fetch_array(mysqli_query($conn,"SELECT ac_number FROM accounts WHERE  acid='$dr_acid' LIMIT 1 "))['ac_number'];
     $smstext= mysqli_fetch_assoc(mysqli_query($conn,"SELECT smstext FROM sms_tamp_all WHERE  status=1 AND type='PAY' AND coid='$coid' AND bid='$bid' AND brid='$brid' LIMIT 1 "))['smstext'];  
   
          if (isset($_POST['sms'])){
            $smssend=   $_POST['sms'];
            if ($smssend =='yes') {
                $content_val   = [$prebalance,$newamount,$dr_balance];
                $content_var   = ["%prebalance%","%payment%","%lastbalance%"]; 
                $newmsg = str_replace($content_var, $content_val, $smstext);
                $msgmsg = urlencode($newmsg);
                require_once('../sms/api_key_access.php');
               } else { echo "Not chexkbox"; }
           }

	}
}

?>