<?php

require("config/config.php");
if (!empty($_GET['page'])) {
    $c_page = $_GET['page'];
} else {
    $c_page = "1";
}
$search_customer_name = $_REQUEST['search_customer_name'];
$vendor_from = $_REQUEST['vendor_from'];
$search_customer_code = $_REQUEST['search_customer_code'];

$search_cond = "";
if (!empty($search_customer_code)) {
    $search_cond .= " and code like '" . $db_cust->escape($search_customer_code) . "%'";
}
if (!empty($search_customer_name)) {
    $search_cond .= " and name like '" . $db_cust->escape($search_customer_name) . "%'";
}
if (!empty($vendor_from)) {
    $search_cond .= " and a.vendor_from = '" . $db_cust->escape($vendor_from) . "'";
}
$sql = "SELECT a.vendor_id,b.address_id,
                a.vendor_type,
                a.vendor_group,
		a.code,
		a.name,
                a.cin_number,
                a.stn_number,
		b.phone,
		b.address,
                b.address_2,
                b.address_3,
                b.is_sez,
                b.gst_number,
		b.city,
                b.pin,
                a.pan_number,
                b.email,
                b.country,
                b.state,
                b.state_code,
                b.website,
                a.ceo,
                b.fax,
                a.contact_person,
                a.contact_designation,
                a.contact_person_email_id,
                a.contact_person_phone,
                a.created_ts,
                a.vendor_from,
                a.created_by,
                a.updated_by,
                a.updated_ts FROM master_vendors as a "
                ." inner join master_vendors_address as b on a.vendor_id = b.vendor_id where b.address_type ='CA' and a.is_deleted = 0 " . $search_cond . " ORDER BY a.name asc";
$customer_data = $db_cust->query($sql);

$file_name = 'customers_list_'.date("y-m-d").'.csv';
$fp = fopen(REPORTS_FOLDER . $file_name, 'w');

$rw = array('Sl.No', 'Vendor Type','Group', 'Code', 'Name', 'CIN Number',
            'STN Number','Phone','Address 1','Address 2','Address 3','City','City Code','Pincode','PAN Number','Email ID','Country','State','State Code','GSTIN','IS SEZ','Website',
            'CEO','FAX','Contact Person','Designation','Contact Email ID','Contact Phone','Customer Added From');
fputcsv($fp, $rw);
$count = 0;
while ($row = $db_cust->fetch_array($customer_data)) {
    $count++;
    $write_arr['Sl.No'] = $count;
    if($row['vendor_type']=="C"){
       $write_arr['Vendor_type'] = 'Customer'; 
    }else{
       $write_arr['Vendor_type'] = 'Member'; 
    }
    
    $write_arr['Group'] = $row['vendor_group'];
    $write_arr['Code'] = $row['code'];
    $write_arr['Name'] = $row['name'];
    $write_arr['CIN_Number'] = $row['cin_number'];
    $write_arr['STN_Number'] = $row['stn_number'];
    $write_arr['Phone'] = $row['phone'];
    $write_arr['Address_1'] = $row['address'];
    $write_arr['Address_2'] = $row['address_2'];
    $write_arr['Address_3'] = $row['address_3'];
    $write_arr['City'] = $row['city'];
    $write_arr['City_Code'] = $row['city_code'];
    $write_arr['Pincode'] = $row['pincode'];
    $write_arr['PAN_Number'] = $row['pan_number'];
    $write_arr['Email_ID'] = $row['email_id'];
    $write_arr['Country'] = $row['country'];
    $write_arr['State'] = $row['state'];
    $write_arr['State_Code'] = $row['state_code'];
    $write_arr['gst_number'] = $row['gst_number'];
    $write_arr['is_sez'] = ($row['is_sez']==1?"Yes":"No");
    $write_arr['Website'] = $row['website'];
    $write_arr['CEO'] = $row['ceo'];
    $write_arr['FAX'] = $row['fax'];
    $write_arr['Contact_Person'] = $row['contact_person'];
    $write_arr['Designation'] = $row['contact_designation'];
    $write_arr['contact_email_id'] = $row['contact_person_email_id'];
    $write_arr['Contact_Phone'] = $row['contact_person_phone'];
    
    if($row['vendor_from']==1){
       $write_arr['Customer_Added_From'] = "IMTEX";
    }else if($row['vendor_from']==2){
        $write_arr['Customer_Added_From'] = "BIEC BILLING";
    }else if($row['vendor_from']==3){
        $write_arr['Customer_Added_From'] = "MTX";
    }else if($row['vendor_from']==4){
        $write_arr['Customer_Added_From'] = "Member";
    }else if($row['vendor_from']==5){
        $write_arr['Customer_Added_From'] = "PMS";
    }
    fputcsv($fp, $write_arr);
    unset($write_arr);
}
fclose($fp);

$db_cust->close();

echo "success|".$file_name;
?>