<?php require_once('../auth/auth.php');
if (isset($_POST['newreg'])) {
    $co_name     =  $_POST['co_name'];
    $owner_name  =  $_POST['co_owner_name'];
    $owner_phone =  $_POST['co_gsm'];
    $co_phone    =  $_POST['co_gsm'];
    $username    =  $_POST['username'];  //user table
    $password    =  $_POST['password'];  //user table
    $divisions   =  $_POST['divisions']; 
    $distr       =  $_POST['distr'];
    $polic_sta   =  $_POST['polic_sta'];
    $co_address  =  $_POST['co_address'];
    $smsapi      =  $_POST['smsapi'];
    $waapi       =  $_POST['waapi'];
    $otherapi    =  $_POST['otherapi'];
    $currency    =  $_POST['currency'];
    $language    =  $_POST['language'];  //user table
    $timezone    =  $_POST['timezone'];
    $tinpass     =  $_POST['tinpass'];
    $nid         =  $_POST['nid'];
    $co_email    =  $_POST['co_email'];
    $taxrate     =  $_POST['taxrate'];
    $sf_y_date   =  $_POST['sf_y_date'];
    $ef_y_date   =  $_POST['ef_y_date'];
    $payid       =  $_POST['payid'];    // new table
    $ontime      =  $_POST['ontime'];  // new table
    $mothlybill  =  $_POST['mothlybill'];   // new table
    $co_note     =  $_POST['co_note'];
    $co_op_date  =  $_POST['co_op_date'];
    $policy_read =  $_POST['policy_read'];

    $sendemail   =  $_POST['sendemail'];
    $sendsms     =  $_POST['sendsms'];
    $bid_type    =  $_POST['bid_type'];

    // company>user_group>Accounts_Group>business>branch>accounts>account_info>employee>user >printer_setup > custom_filed

    mysqli_query($conn,"INSERT INTO company(`co_name`,`owner_name`,`owner_phone`,`co_phone`,`co_email`,`co_address`,`divisions`, `distr`,`polic_sta`, `co_f_year_start`,`co_f_year_end`, `smsapi`, `sms_sim_device`, `waapi`, `otherapi`, `currency`, `timezone`, `nid`, `tinpass`, `co_note`, `co_printer_id`, `co_gst_on_off`, `taxrate`, `policy_read`,  `co_op_date`, `co_permission`, `verifyed`, `status`) 
        VALUES ('$co_name','$owner_name','$owner_phone','$co_phone','$co_email', '$co_address', '$divisions', '$distr', '$polic_sta', '$sf_y_date', '$ef_y_date','$smsapi',NULL,'$waapi','$otherapi','$currency', '$timezone','$nid','$tinpass','$co_note',1,NULL, '$taxrate', '$policy_read',  '$co_op_date',NULL,0,1)")or die(mysqli_error($conn));
    $company_id = mysqli_insert_id($conn);

// Userr Group Insert 

    if ($company_id>0) {
        mysqli_query($conn,"INSERT INTO `user_g`(`coid`, `group_name`, `group_permission`, `date`,`default_set`, `status`) VALUES ('$company_id','Administrator',NULL,'$co_op_date','primary', '1')")or die(mysqli_error($conn));
        $group_id = mysqli_insert_id($conn);
    }

    // Group table insert

    if ($company_id>0) {
        mysqli_query($conn,"INSERT INTO `ac_sub_g_category` (`coid`, `ac_s_catid`, `ac_g_cat_name`, `g_type`) SELECT '$company_id',`ac_s_catid`, `ac_g_cat_name`, `g_type` FROM `ac_sub_g_category` WHERE coid=1 AND g_type IN ('mcash','bank','du-adv','invtr','su-deb','stl','su-cre','owner','prls','sl-inc','dr-inc','in-inc','cs-gds','pu-exp','dr-exp','sl-exp','in-exp')")or die(mysqli_error($conn));
    }


// Business Insert 


    if ($company_id >0) {
        mysqli_query($conn," INSERT INTO `business`(`coid`, `bid_type`, `bid_name`, `bid_gsm`, `bid_address`, `bid_opining_d`, `bid_shipping_add`, `bid_td_no`, `business_permission`,`default_set`, `active`) VALUES ('$company_id','$bid_type','$co_name','$owner_phone','$co_address','$co_op_date','$co_address',NULL,NULL,'primary',1)")or die(mysqli_error($conn));
            $business_id = mysqli_insert_id($conn);
        }

// branch table insert

    if ($company_id >0 && $business_id >0) {
        mysqli_query($conn,"INSERT INTO `branch`(`coid`, `bid`, `branch_name`, `branch_number`, `branch_address`, `branch_code`, `branch_shipping_add`, `branch_permission`, `branch_opining_d`,`default_set`,`active`) VALUES ('$company_id','$business_id','Head Office','$owner_phone','$co_address','BR-01','$co_address',NULL,'$co_op_date','primary',1)")or die(mysqli_error($conn));
          $branch_id = mysqli_insert_id($conn);
    }


// Accounts table insert


    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',`0`, `ac_name`, `ac_alise`,'$co_op_date','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type IN('cash','stock','wcustomer','wsupplier','opening','profit','sale','salereturn','pucom','sfrc','cogs','purchase','pureturn','salecom','pfrc','offexp','demployee')")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='mcash' LIMIT 1), `ac_name`, `ac_alise`,'$co_op_date','$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`,'$co_op_date','$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`,'$co_op_date','$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`,'$co_op_date','$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`,'$co_op_date','$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`,'$co_op_date','$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`,'$co_op_date','$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`,'$co_op_date','$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`,'$co_op_date','$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`,'$co_op_date','$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`,'$co_op_date','$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='sl-exp' LIMIT 1), `ac_name`, `ac_alise`,'$co_op_date','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type IN ('demployee') 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='in-exp' LIMIT 1), `ac_name`, `ac_alise`,'$co_op_date','$time',`ac_type`,`active` FROM `accounts` WHERE coid=1 AND ac_type IN ('offexp') LIMIT 1")or die(mysqli_error($conn));  
    }


// Accounts /Employee  Info table insert

    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','demployee') 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));

          if ($acr['ac_type']=='demployee') {

            mysqli_query($conn,"INSERT INTO `em_employee`(`em_coid`, `em_bid`,`em_brid`,  `em_acid`, `em_acname`, `em_dis_id`) VALUES ('$company_id','$business_id','$branch_id','$gacid','Default Employee',1)")or die(mysqli_error($conn));
          }
       }
    }


// user table insert
    if ($company_id >0 && $business_id >0 && $branch_id>0) {

        $ugqr = mysqli_query($conn,"SELECT ugid,em_id FROM user_g INNER JOIN em_employee ON (user_g.coid=em_employee.em_coid) WHERE user_g.coid='$company_id' AND em_bid='$business_id' AND em_brid='$branch_id' AND group_name='Administrator' AND em_acname='Default Employee'") or die(mysqli_error($conn));
        $ugr = mysqli_fetch_assoc($ugqr);
        $em_id = $ugr['em_id'];
        $ugid  = $ugr['ugid'];

        mysqli_query($conn,"INSERT INTO `user`(`us_coid`, `us_bid`, `us_brid`, `us_em_id`, `us_ugid`, `username`, `password`, `us_sid`, `us_date`, `us_language`,`default_set`,`us_status`) VALUES ('$company_id','$business_id','$branch_id','$em_id','$ugid','$username','$password',NULL,'$co_op_date','$language','primary',1)")or die(mysqli_error($conn));
        $user_id = mysqli_insert_id($conn);




        mysqli_query($conn,"INSERT INTO `business_w_house`( `coid`, `bid`, `brid`, `warehouse_code`, `war_location`,`default_set`, `date`, `active`) VALUES ('$company_id','$business_id','$branch_id','W-001','All Stock','primary','$co_op_date',1)")or die(mysqli_error($conn));

        mysqli_query($conn,"INSERT INTO `pro_unit`(`coid`, `unit_name`, `unit_symble`, `unit_convart`,`default_set`) 
            VALUES ('$company_id','Pices','PCS',1,'primary')")or die(mysqli_error($conn));
            
        mysqli_query($conn,"INSERT INTO `batch_no`(`coid`, `bid`, `brid`, `batch_no`, `default_set`,`active`) 
            VALUES ('$company_id','$business_id','$branch_id','Common Batch','primary',1)")or die(mysqli_error($conn));
            
        mysqli_query($conn,"INSERT INTO `route_area`(`coid`, `bid`, `brid`, `route_name`, `road_name`, `userid`, `default_set`, `op_date`) 
            VALUES ('$company_id','$business_id','$branch_id','Market Name','Road NO','$user_id','primary','$co_op_date')")or die(mysqli_error($conn));

        mysqli_query($conn,"INSERT INTO `item_group`(`coid`,`bid`,`pgroup_name`,`date`,`userid`, `default_set`,`active`) 
            VALUES ('$company_id','$business_id','Common Group','$date','$user_id','primary', '1')")or die(mysqli_error($conn));
            
        mysqli_query($conn,"INSERT INTO `item_catagory`(`coid`,`bid`,`pcat_name`,`date`,`userid`,`default_set`,`active`)
            VALUES ('$company_id','$business_id','Common Catagory','$date','$user_id','primary', '1')")or die(mysqli_error($conn));

        mysqli_query($conn,"INSERT INTO `item_brand`(`coid`,`bid`,`pbrand_name`,`date`,`userid`,`default_set`,`active`) 
            VALUES ('$company_id','$business_id','Common Brand','$date','$user_id','primary','1')")or die(mysqli_error($conn));
            
        mysqli_query($conn,"INSERT INTO `pro_tax`(`coid`, `bid`, `brid`, `sp_type`, `tax_group`, `tax_margin`, `uid`, `date`,`default_set`,`active`) 
            VALUES ('$company_id' , '$business_id' , '$branch_id' ,'sales' , 'Sale Tax' , 0 ,'$user_id', '$date' ,'primary',1),
                   ('$company_id' , '$business_id' , '$branch_id' ,'purchase' , 'purchase Tax' , 0 ,'$user_id', '$date' ,'primary',1)")or die(mysqli_error($conn));
          

    }

// printer_setup insert 
    mysqli_query($conn,"INSERT INTO `printer_setup`( `coid`, `bid`, `brid`) VALUES ('$company_id','$business_id','$branch_id')")or die(mysqli_error($conn));
    
// custom_field  insert 
    mysqli_query($conn,"INSERT INTO `custom_field`(`coid`,`userid`) VALUES ('$company_id','$user_id')")or die(mysqli_error($conn));




}


        // company Profile Update

        if (isset($_POST['up_company_id'])) {
           $up_company_id = $_POST['up_company_id'];
           $col_name   = $_POST['col_name'];
           $col_value  = $_POST['col_value'];
           mysqli_query($conn,"UPDATE company SET $col_name='$col_value' WHERE coid='$up_company_id'");
        }

    if (isset($_POST['ac_img_id'])) {
            $ac_img_id = $_POST['ac_img_id'];
            $dataurl = base64_encode(file_get_contents($_POST['dataurl']));
            mysqli_query($conn,"UPDATE company SET co_logo='$dataurl' WHERE coid='$ac_img_id'");
        }
?>