<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$subscriptions_taxrateObj =  new  subscriptions_taxrate();

$stripeObj   = new stripePay();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'taxadd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['plan_id'])){
    $planDetail = $subscription_planObj->fromId($_REQUEST['plan_id']);
    //print_r($planDetail->plan_amount);exit;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='getstatedata' && isset($_REQUEST['country_id']) && $_REQUEST['country_id'] != "")
{
    $stateObj = new state();
    $where = "country_id = '".$_REQUEST['country_id']."' ";
    $orderby = array(" state_id desc ");
    $stateArg    = array('where' => $where,'orderBy'=>$orderby);
    $stateDetails = $stateObj->getAllRecords($stateArg);
    ?>
    <option value="">Select State</option>
    <?php
    foreach ($stateDetails as $key => $statevalue) 
    { ?>
        <?php if(isset($_REQUEST['state_id']) && $_REQUEST['state_id'] != '')
        { ?>
            <option value="<?php echo $statevalue->state_id; ?>" <?php echo (isset($_REQUEST['state_id']) && $_REQUEST['state_id'] == $statevalue->state_id) ? "selected='selected'" : ""; ?> ><?php echo $statevalue->state_name; ?></option>
       <?php }else{ ?>
            <option value="<?php echo $statevalue->state_id; ?>" ><?php echo $statevalue->state_name; ?></option>
        <?php } ?>
       
                             
    <?php }
    exit;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['tax_type']) && $_REQUEST['tax_type'] != "")
{
   
    $tax_type = $_REQUEST['tax_type'];
    $tax_display_name = $_REQUEST['tax_display_name'];
    $country_id = $_REQUEST['country_id'];
    $state_id = $_REQUEST['state_id'];
    $tax_rate = $_REQUEST['tax_rate'];
    $tax_mode = $_REQUEST['tax_mode'];
    $tax_status = $_REQUEST['tax_status'];
    

    $countryObj = new country();
    $countryDetail = $countryObj->fromId($country_id);
    
    $stateObj = new state();
    $stateDetail = $stateObj->fromId($state_id);

    if($tax_type == 'custom')
    {
        $tax_type_value = 'hst';
    }

    
    $region = $stateDetail->state_name.", ".$countryDetail->country_name;
    $jurisdiction = $countryDetail->country_shortcode." - ".$stateDetail->state_shortcode;
    $data=array(
        'tax_type' => $tax_type_value,
        'display_name' => $tax_display_name,
        'country' => $countryDetail->country_shortcode,
        'state' => $stateDetail->state_shortcode,
        'jurisdiction' => $jurisdiction,
        'percentage' => $tax_rate,
        'mode' => $tax_mode,
    );

    $alltaxdetail= $stripeObj->createtax($data);
    if($alltaxdetail->id)
    {
        $taxdata['taxrate_txr_id'] = $alltaxdetail->id;
        $taxdata['taxrate_type'] = $tax_type;
        $taxdata['taxrate_country_shortcode'] = $alltaxdetail->country;
        $taxdata['taxrate_state_short_code'] = $alltaxdetail->state;
        $taxdata['taxrate_country_id'] = $country_id;
        $taxdata['taxrate_state_id'] = $state_id;
        $taxdata['taxrate_percentage'] = $alltaxdetail->percentage;
        $taxdata['taxrate_mode'] = $tax_mode;
        $taxdata['taxrate_display_name'] = $tax_display_name;
        $taxdata['taxrate_region'] = $region;
        $taxdata['taxrate_region_label'] = $jurisdiction;
        $taxdata['taxrate_response'] = json_encode($alltaxdetail);
        $taxdata['taxrate_status'] =$tax_status;
        $taxdata['taxrate_created_at'] = date('Y-m-d h:i:s');

        $sub_tax = "<b>Add Record</b> ";
        $sub_tax.= " <b>Tax Data</b>: ".json_encode($taxdata);

        $rs  = $subscriptions_taxrateObj->addRecords($taxdata);
            
        if($rs)
        {
            
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$sub_tax);

            header('Location:taxlist.php');
            exit;
        }
    }
}


// Include Template 
include_once('../libs/routs.php')

?>