<?php  require_once('../auth/auth.php'); ?>
<?php
ob_start();
if (isset($_POST['bran_submit'])) {
    $bran_submit      =   $_POST['bran_submit'];
    $company_id       =   $_POST['com_id'];
    $business_id      =   $_POST['bid_id'];
    $branch_name      =   $_POST['branch_name'];
    $branch_number    =   $_POST['branch_number'];
    $branch_email     =   $_POST['branch_email'];
    $b_email_smtp     =   $_POST['b_email_smtp'];
    $branch_code      =   $_POST['branch_code'];
    $branch_address   =   $_POST['branch_address'];
    $shipping_add     =   $_POST['branch_shipping_add'];
    $branch_opining_d =   $_POST['branch_opining_d'];
    $printer          =   $_POST['printer'];
    
    if ($bran_submit=='0') {
        mysqli_query($conn,"INSERT INTO branch(`coid`, `bid`, `branch_name`, `branch_number`,`branch_email`,`b_email_smtp`, `branch_address`, `branch_code`, `branch_shipping_add`,  `branch_permission`,`branch_opining_d`,`printer_set`, `active`) 
            VALUES ('$company_id','$business_id', '$branch_name', '$branch_number','branch_email','$b_email_smtp','$branch_address','$branch_code', '$shipping_add','dash,hrm','$branch_opining_d','$printer',1)")or die(mysqli_error($conn));
        $branch_id = mysqli_insert_id($conn);


        if ($company_id >0 && $business_id >0 && $branch_id>0) {

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='mcash' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type = 'cash' LIMIT 1")or die(mysqli_error($conn));   

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='invtr' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type = 'stock' LIMIT 1")or die(mysqli_error($conn)); 

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='su-deb' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type = 'wcustomer' LIMIT 1")or die(mysqli_error($conn)); 

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='su-cre' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type = 'wsupplier' LIMIT 1")or die(mysqli_error($conn)); 

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='owner' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type = 'opening' LIMIT 1")or die(mysqli_error($conn)); 

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='prls' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type = 'profit' LIMIT 1")or die(mysqli_error($conn)); 

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='sl-inc' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type IN ('sale','salereturn') LIMIT 2")or die(mysqli_error($conn));

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='dr-inc' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type IN ('pucom','sfrc') LIMIT 2")or die(mysqli_error($conn));

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='cs-gds' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type = 'cogs' LIMIT 1")or die(mysqli_error($conn));

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='pu-exp' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type IN ('purchase','pureturn') LIMIT 2")or die(mysqli_error($conn));

            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='dr-exp' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type IN ('salecom','pfrc') LIMIT 2")or die(mysqli_error($conn)); 


            mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `ac_name`, `ac_alise`,`opdate`,`atime`,`ac_type`,`active`) SELECT '$company_id','$business_id','$branch_id',(SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$company_id' AND g_type='in-exp' LIMIT 1), `ac_name`, `ac_alise`,'$branch_opining_d','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type IN ('offexp') LIMIT 1")or die(mysqli_error($conn));  
        }

        if ($company_id >0 && $business_id >0 && $branch_id>0) {
         $acqr = mysqli_query($conn,"SELECT acid,ac_type FROM accounts WHERE coid='$company_id' AND bid='$business_id' AND brid='$branch_id' AND ac_type IN('wcustomer','wsupplier') ORDER BY acid ASC LIMIT 3") or die(mysqli_error($conn));
         while ($acr = mysqli_fetch_assoc($acqr)) {
             $gacid = $acr['acid'];

             mysqli_query($conn,"INSERT INTO `account_info`( `acid`, `coid`, `bid`, `brid`,`g_note`) VALUES ('$gacid','$company_id','$business_id','$branch_id',NULL)")or die(mysqli_error($conn));
        }
    }

        mysqli_query($conn,"INSERT INTO `business_w_house`( `coid`, `bid`, `brid`, `warehouse_code`, `war_location`, `date`, `active`) VALUES ('$company_id','$business_id','$branch_id','$branch_code','All Stock','$branch_opining_d',1)")or die(mysqli_error($conn));

         mysqli_query($conn,"INSERT INTO `route_area`(`coid`, `bid`, `brid`, `route_name`, `road_name`, `op_date`) 
            VALUES ('$company_id','$business_id','$branch_id','Market Name','Road NO','$branch_opining_d')")or die(mysqli_error($conn));



} else {
    mysqli_query($conn,"UPDATE branch SET bid='$business_id',branch_name='$branch_name',branch_number='$branch_number',branch_email='$branch_email',b_email_smtp='$b_email_smtp',branch_code='$branch_code',branch_address='$branch_address',branch_shipping_add='$shipping_add',printer_set='$printer' WHERE brid='$bran_submit'")or die(mysqli_error($conn));
}



}
ob_end_clean();
?>


<?php
ob_start();
if (isset($_POST['sms_api_key'])) {
    $sms_api_key      =   $_POST['sms_api_key'];
    $sms_senderid     =   $_POST['sms_senderid'];
    $new_api          =   $_POST['new_api'];
    $sms_status        =   $_POST['sms_status'];
    if($new_api >0){
         mysqli_query($conn,"UPDATE branch SET sms_api_key='$sms_api_key',sms_senderid='$sms_senderid',sms_status='$sms_status' WHERE brid='$new_api'")or die(mysqli_error($conn));
    }
}
ob_end_clean();
?>  