<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php'); ?>
<?php 
if(isset($_POST['op_invoice'])){
   $acid       = $_POST['acid'];
   $invoice    = $_POST['op_invoice'];
   $ttype      = $_POST['ttype'];
   $amount     = $_POST['amount'];
   $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 {

      $op_row= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ac_type='opening' LIMIT 1"));

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

      if ($ttype == '+') {
        $dr_acid = $acid;
        $cr_acid = $op_row['acid'];

        $dr_gcatid = $gcatid;
        $cr_gcatid = $op_row['gcatid'];
      } else {
        $dr_acid = $op_row['acid'];
        $cr_acid = $acid;

        $dr_gcatid = $op_row['gcatid'];
        $cr_gcatid = $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','$dr_acid', '$cr_acid', '$uid', '$dr_gcatid', '$cr_gcatid', '$invoice', 'OP','$details','opening','$amount','$tdate','$time','$uid','$us_em_id','1')") 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
        }
        
        
        
     $PhoneNumber = mysqli_fetch_array(mysqli_query($conn,"SELECT ac_number FROM accounts WHERE  acid='$acid' LIMIT 1 "))['ac_number'];
     $smstext= mysqli_fetch_assoc(mysqli_query($conn,"SELECT smstext FROM sms_tamp_all WHERE  status=1 AND type='OP' AND coid='$coid' AND bid='$bid' AND brid='$brid' LIMIT 1 "))['smstext'];  
   
          if (isset($_POST['sms'])){
            $smssend=   $_POST['sms'];
            if ($smssend =='yes') {
                $content_val   = [$amount,$dr_balance];
                $content_var   = ["%opbalance%","%lastbalance%"]; 
                $newmsg = str_replace($content_var, $content_val, $smstext);
                $msgmsg = urlencode($newmsg);
                require_once('../sms/api_key_access.php');
               } else { echo "Not chexkbox"; }
           }

	}
  }
?>
