<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php'); ?>
<?php 
if(isset($_POST['rec_invoice'])){
   $dr_acid    = $_POST['dr_acid'];
   $invoice    = $_POST['rec_invoice'];
   $cr_acid    = $_POST['cr_acid'];
   $amount     = $_POST['amount'];

   $d_acid     = $_POST['d_acid'];
   $R_discount = $_POST['R_discount'];
   $us_em_id   = $_POST['us_em_id'];

   $newamount  = $amount -$R_discount;
   $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 && $amount < 0.01) {}else {

		$dr_gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$dr_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='$cr_acid' LIMIT 1"))['gcatid'];	
           

        if ($amount >0 ) {
            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','$dr_acid', '$cr_acid', '$uid', '$dr_gcatid', '$cr_gcatid', '$invoice', 'R','$details','rec','$newamount','$tdate','$time','$uid','$us_em_id','1')") or die(mysqli_error($conn));
           }


    // discount insert 


    if ($R_discount >0  &&  $d_acid > 0 ) {
        $d_gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$d_acid' LIMIT 1"))['gcatid'];

        mysqli_query($conn,"INSERT INTO transaction(coid,bid,brid,dr_acid,cr_acid,e_acid,dr_gcatid,cr_gcatid,invoice,m_invoice,ttype,details,note,amount,date,ptime,userid,em_id,active) 
              VALUES ('$coid','$bid','$brid','$d_acid', '$cr_acid', '$uid', '$d_gcatid', '$cr_gcatid', '$invoice','$uid', 'RD',
                '$details','receive Discount','$R_discount','$tdate','$time','$uid','$us_em_id','1')") or die(mysqli_error($conn));

        $d_balance = balance($d_acid)['ac_balance'];
        mysqli_query($conn,"UPDATE accounts SET balance='$d_balance' WHERE coid='$coid' AND acid='$d_acid'") 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));

        $year = date('Y');
        $invoice_parts = explode('-', $invoice);
        $prefix = $invoice_parts[0];
        mysqli_query($conn,"UPDATE voucher_no SET v_no=(v_no + 1) WHERE coid='$coid' AND v_type ='$prefix' AND v_year='$year'") or die(mysqli_error($conn));        

        if (isset($_POST['email'])) {
        	// Email Part Here
        }
        
        
        
        
    $prebalance=$cr_balance+$amount;
     $PhoneNumber = mysqli_fetch_array(mysqli_query($conn,"SELECT ac_number FROM accounts WHERE  acid='$cr_acid' LIMIT 1 "))['ac_number'];
     $smstext= mysqli_fetch_assoc(mysqli_query($conn,"SELECT smstext FROM sms_tamp_all WHERE  status=1 AND type='REC' 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,$amount,$cr_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"; }
           }
        
        
        //


	}
}

?>