<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php'); ?>
<?php 
if(isset($_POST['ac_g_catid'])){
   $ac_g_catid  = $_POST['ac_g_catid'];

   $ac_name    = $_POST['ac_name'];
   $ac_alise   = $_POST['alias_name'];
   $ac_number  = isset($_POST['ac_number']) ? $_POST['ac_number']:0 ;
   $ac_email     =  isset($_POST['ac_email']) ? $_POST['ac_email']:0;
   $ac_details = $_POST['ac_details'];

   $balance      =  isset($_POST['balance']) ? $_POST['balance']:0;
   $ttype        =   $_POST['ttype'];
   $ac_op_date   =  ($balance>0)?$co_f_year_start:$_POST['opdate'];
   $opdate      = $ac_op_date.' '.$time; 


$g_type_get= mysqli_fetch_assoc(mysqli_query($conn,"SELECT g_type FROM ac_sub_g_category WHERE coid='$coid' AND ac_g_catid='$ac_g_catid' LIMIT 1"))['g_type']; 
if ($g_type_get=='su-deb' || 'su-cre') {
   $divisions    =  isset($_POST['divisions']) ? $_POST['divisions']:0; 
   $distr        =  isset($_POST['distr']) ? $_POST['distr']:0; 
   $polic_sta    =  isset($_POST['polic_sta']) ? $_POST['polic_sta']:0; 
   $ac_nid       =  isset($_POST['ac_nid']) ? $_POST['ac_nid']:0;
   $outlet_id    =  isset($_POST['out_it'])? $_POST['out_it']:0;
   $customer_type=  isset($_POST['customer_type'])? $_POST['customer_type']:0;
   $dis_margin   =  isset($_POST['dis_margin']) ? $_POST['dis_margin']:0;
   $crlimit      =  isset($_POST['crlimit']) ? $_POST['crlimit']:0;
   $bankid       =  isset($_POST['bankid']) ? $_POST['bankid']:0;
   $bfsc_code    =  isset($_POST['bfsc_code']) ? $_POST['bfsc_code']:0;
   $bbr_name     =  isset($_POST['bbr_name']) ? $_POST['bbr_name']:0;
   $areaid       =  isset($_POST['areaid']) ? $_POST['areaid']:0;
   $vatno        =  isset($_POST['vatno']) ? $_POST['vatno']:0;
   $currency     =  isset($_POST['currency']) ? $_POST['currency'] :0;
   $p_terms      =  isset($_POST['p_terms']) ? $_POST['p_terms']:0;
}

    mysqli_query($conn,"INSERT INTO accounts(`coid`, `bid`, `brid`, `gcatid`, `outlet_id`, `ac_name`, `ac_alise`, `ac_number`,`divisions`, `distr`, `polic_sta`, `ac_details`, `ac_email`, `ac_area`, `opdate`,`ac_dis`,`cr_limit`,`userid`,`acsub_type`, `active`) 
        VALUES ('$coid','$bid','$brid','$ac_g_catid','$outlet_id', '$ac_name', '$ac_alise', '$ac_number','$divisions','$distr','$polic_sta', '$ac_details', '$ac_email','$areaid', '$opdate','$dis_margin','$crlimit','$uid','$customer_type',1)") or die(mysqli_error($conn));
    $lastid = mysqli_insert_id($conn);

	if($_POST['balance'] > 0 && $lastid > 0){
		$ttype      = $_POST['ttype'];
		$balance    = $_POST['balance'];
    $tdate      = $co_f_year_start.' '.$time;
		$invoice    = $_POST['invoice'];

        $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"));

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

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

				$dr_gcatid = $op_row['gcatid'];
				$cr_gcatid = $ac_g_catid;
			}

	$chinvoice=mysqli_query($conn,"SELECT invoice FROM transaction WHERE coid='$coid' AND invoice='$invoice' LIMIT 1");
		if (mysqli_num_rows($chinvoice)>0) {}else {
           
         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','Opening Balance','opening','$balance','$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));
		}		
	}

	  if ($lastid>0) {
            mysqli_query($conn,"INSERT INTO `account_info`( `acid`,`coid`, `bid`, `brid`, `nid`,`bankid`, `bfsc_code`, `bbr_name`,`currency`) VALUES ('$lastid','$coid','$bid','$brid','$ac_nid','$bankid','$bfsc_code','$bbr_name','$currency')")or die(mysqli_error($conn));
        }


                     $PhoneNumber = $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['sendsms'])){
            $smssend=   $_POST['sms'];
            if ($smssend =='yes') {
                $content_val   = [$balance,$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"; }
           }

}


?>