<?php require_once('../auth/auth.php'); require_once('../auth/acid_balance.php');

if (isset($_POST['newcustomer'])) {
    $ac_name      =  $_POST['ac_name'];
    $alias_name   =  $_POST['alias_name'];
    $ac_number    =  $_POST['ac_number'];
    $ac_email     =  isset($_POST['ac_email']) ? $_POST['ac_email']:0;
    $divisions    =  $_POST['divisions']; 
    $distr        =  $_POST['distr'];
    $polic_sta    =  $_POST['polic_sta'];
    $ship_address =  $_POST['shipping_address'];
    $customer_type=  isset($_POST['customer_type'])? $_POST['customer_type']:0;
    $outlet_id    =  $_POST['out_it'];
    $dis_margin   =  isset($_POST['dis_margin']) ? $_POST['dis_margin']:0;
    $crlimit      =  isset($_POST['crlimit']) ? $_POST['crlimit']:0;
    $ac_nid       =  isset($_POST['ac_nid']) ? $_POST['ac_nid']: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;
    $g_note       =  isset($_POST['g_note']) ? $_POST['g_note']:0;
    $balance      =  isset($_POST['balance']) ? $_POST['balance']:0;
    $ac_op_date   =  ($balance>0)?$co_f_year_start:$_POST['ac_op_date'];
    $sendsms      =  $_POST['sendsms'];
    $gcatid       =  $_POST['gcatid'];
    $ttype        =  $_POST['ttype'];
    $brithday     =  isset($_POST['brithday']) ?  $_POST['brithday'] :$date;
    $father_name  =  isset($_POST['father_name']) ?  $_POST['father_name'] :0;
    $mother_name  =  isset($_POST['mother_name']) ?  $_POST['mother_name'] :0;
    $gender       =  isset($_POST['gender']) ?  $_POST['gender'] :0;
    
    
    $g_name1      =  isset($_POST['g_name1']) ?  $_POST['g_name1'] :0;
    $g_address1   =  isset($_POST['g_address1']) ?  $_POST['g_address1'] :0;
    $g_phone1     =  isset($_POST['g_phone1']) ?  $_POST['g_phone1'] :0;
    
   $g_name2      =  isset($_POST['g_name2']) ?  $_POST['g_name2'] :0;
   $g_address2   =  isset($_POST['g_address2']) ?  $_POST['g_address2'] :0;
   $g_phone2     =  isset($_POST['g_phone2']) ?  $_POST['g_phone2'] :0;


    $opdate     = $ac_op_date.' '.$time; 


    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','$gcatid','$outlet_id','$ac_name','$alias_name','$ac_number','$divisions','$distr','$polic_sta','$ship_address','$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'];
        $invoice    = $_POST['invoice'];
        $tdate      = $co_f_year_start.' '.$time;


        $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 = $gcatid;
            $cr_gcatid = $op_row['gcatid'];
        } else {
            $dr_acid = $op_row['acid'];
            $cr_acid = $lastid;

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

        $chinvoice=mysqli_query($conn,"SELECT invoice FROM transaction WHERE 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`,`g_note`,`brithday`,`father_name`,`mother_name`,`gender`,`g_name1`, `g_address1`, `g_phone1`, `g_name2`, `g_address2`, `g_phone2`) 
            VALUES ('$lastid','$coid','$bid','$brid','$ac_nid','$bankid','$bfsc_code','$bbr_name','$currency','$g_note','$brithday','$father_name','$mother_name','$gender','$g_name1','$g_address1','$g_phone1','$g_name2','$g_address2','$g_phone2')")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'  "))['smstext'];  
     $smstextnew = mysqli_fetch_assoc(mysqli_query($conn,"SELECT smstext FROM sms_tamp_all WHERE  status=1 AND type='NEWACOP' AND coid='$coid' AND bid='$bid' AND brid='$brid'  "))['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"; }
           }
           
           if (isset($_POST['sendsmsac'])){
            $smssendd=   $_POST['sendsmsac'];
            if ($smssendd =='yes') {
                $content_val   = [$outlet_id,$ac_name,$ac_number,$ac_email,$ac_nid,$ship_address];
                $content_var   = ["%accounts_id%","%accounts_name%","%accounts_phone%","%accounts_email%","%accounts_nid%","%accounts_address%"]; 
                $newmsg = str_replace($content_var, $content_val, $smstextnew);
                $msgmsg = urlencode($newmsg);
                require_once('../sms/api_key_access.php');
               } else { echo "Not chexkbox"; }
           }
           
           
           
        
        
        
        
        
}

?>

