<?php

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

//$sql = "select id,CAST(REPLACE(code,'CUST','') AS INT) as code from customers order by code desc limit 0,1"; // casting not working in biec server
//$sql = "select id from customers order by id desc limit 0,1";
$sql = "select vendor_id,TRIM(REPLACE(code,'CUST',''))+0 AS numeric_code from master_vendors where vendor_from IN ('1','2','3') order by numeric_code desc limit 0,1";
$result = $db_cust->query_first($sql);
if ($db_cust->affected_rows > 0) {
    $last_id = ltrim($result['numeric_code'],'0') + 1;
} else {
    $last_id = 1;
}

$data['vendor_group'] = $_POST['vendor_group'];
$data['vendor_type'] = 'C';
$data["name"] = $_POST['name'];
$data["cin_number"] = $_POST['cin_number'];
$data["stn_number"] = $_POST['stn_number'];
$data["gst_number"] = $_POST['gst_number'];

$data["contact_person"] = $_POST['contact_person'];
$data["contact_designation"] = $_POST['contact_designation'];
$data["contact_person_email_id"] = $_POST['contact_person_email_id'];

$data["contact_person_phone"] = $_POST['contact_person_phone'];
$data["pan_number"] = $_POST['pan_number'];
$data["ceo"] = $_POST['ceo'];
$data["ceo_email_id"] = $_POST['ceo_email_id'];
$data["ceo_phone"] = $_POST['ceo_phone'];

$data["created_ts"] = "NOW()";
$data["created_by"] = $_REQUEST['user_id'];
$data["vendor_from"] = $_REQUEST['from'];

$vendor_code = "CUST" . str_pad($last_id, 6, '0', STR_PAD_LEFT);
$data["code"] = $vendor_code;

$vendor_id = $db_cust->query_insert("master_vendors", $data);


/* address part */ 
$address["is_sez"] = $_POST['is_sez'];
$address["gst_type"] = $_POST['gst_type'];
$address["gst_number"] = $_POST['gst_number'];

$address["vendor_id"] = $vendor_id;
$address["address_type"] = 'CA';
$address["country"] = $_POST['country'];
$address["address"] = $_POST['address'];

$address["address_2"] = $_POST['address_2'];
$address["address_3"] = $_POST['address_3'];

$address["state"] = $_POST['state'];
$address["state_code"] = $_POST['state_code'];
$address["city"] = $_POST['city'];
$address["city_code"] = $_POST['city_code'];
$address["pin"] = $_POST['pincode'];
$address["phone"] = $_POST['phone'];
$address["fax"] = $_POST['fax'];
$address["email"] = $_POST['email_id'];
$address["website"] = $_POST['website'];

$db_cust->query_insert("master_vendors_address", $address);

$db_cust->close();

pushingCustomer(array_merge($data,$address)); 

unset($data,$address);
?>
