<?php

require("../config/config.php");

$data = @file_get_contents('php://input');

$json_data = json_decode($data, true);

if (empty($json_data)) {
    echo '{"response":"0","message":"Invalid JSON Format"}';
    die();
}

$api_key = $json_data["authentication"]["api_key"];

if (empty($api_key)) {
    echo '{"response":"0","message":"API key cannot be blank"}';
    die();
}

if ($api_key != GLOBAL_APP_API_KEY) {
    echo '{"response":"0","message":"Invalid API Key"}';
    die();
}

$company = $json_data["authentication"]["data"]['company'];
$return_arr = array();
if(!empty($company)){
    $sql = "SELECT * FROM master_vendors as a inner join master_vendors_address as b on a.vendor_id = b.vendor_id "
            . " inner join master_country as c on b.country = c.CountryRegionId "
            . " where a.vendor_from = '4' and b.address_type = 'CA' and (a.status = 'no' OR a.status ='No') and a.vendor_type = 'M' and a.is_deleted = '0' "            
            . " and a.name ='" . $db_cust->escape($company) . "'";
    $result = $db_cust->query_first($sql);
    $ca_count = $db_cust->affected_rows;
    if($ca_count == 0){
        $sql = "SELECT * FROM master_vendors as a inner join master_vendors_address as b on a.vendor_id = b.vendor_id "
            . " inner join master_country as c on b.country = c.CountryRegionId "
            . " where a.vendor_from = '4' and b.address_type = 'RA' and (a.status = 'no' OR a.status ='No') and a.vendor_type = 'M' and a.is_deleted = '0' "            
            . " and a.name ='" . $db_cust->escape($company) . "'";
        $result = $db_cust->query_first($sql); 
        $ra_count = $db_cust->affected_rows;
    }
    if($ca_count > 0 || $ra_count > 0 ) {    
    
    $return_arr['vendor_id'] = $result['vendor_id'];
    $return_arr['vendor_type'] = $result['vendor_type'];
    $return_arr['type'] = $result['type'];
    $return_arr['code'] = $result['code'];
    $return_arr['vendor_from'] = $result['vendor_from'];
    
    $return_arr['vendor_group'] = $result['vendor_group'];
    $return_arr['category'] = $result['category'];
    $return_arr['vendor_category'] = $result['vendor_category'];
    $return_arr['name'] = $result['name'];
    
    $return_arr['currency'] = $result['currency'];
    $return_arr['cin_number'] = $result['cin_number'];
    $return_arr['stn_number'] = $result['stn_number'];
    $return_arr['pan_number'] = $result['pan_number'];    
	$return_arr['gst_number'] = $result['gst_number'];
    
    $return_arr['ceo'] = $result['ceo'];
    $return_arr['contact_person'] = $result['contact_person'];
    $return_arr['contact_designation'] = $result['contact_designation'];
    $return_arr['contact_person_email_id'] = $result['contact_person_email_id'];
    
    $return_arr['application_date'] = $result['application_date'];
    $return_arr['address_type'] = $result['address_type'];
    $return_arr['address'] = $result['address'];
    $return_arr['address_2'] = $result['address_2'];
    
    $return_arr['address_3'] = $result['address_3'];
    $return_arr['city'] = $result['city'];
    $return_arr['city_code'] = $result['city_code'];
    $return_arr['state'] = $result['state'];
    $return_arr['state_code'] = $result['state_code'];
    $return_arr['pin'] = $result['pin'];
    
    $return_arr['country'] = $result['country'];
    $return_arr['phone'] = $result['phone'];
    $return_arr['mobile'] = $result['mobile'];
    $return_arr['email'] = $result['email'];
    
    $return_arr['website'] = $result['website'];
    $return_arr['fax'] = $result['fax'];
    
    $return_arr['short_name'] = $result['ShortName'];
    
        echo json_encode($return_arr);
    }else {
        echo json_encode($return_arr);
    }
}else{
    echo json_encode($return_arr);
}

die();
?>
