<?php
include 'connection.php';
session_start();
if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    }

date_default_timezone_set('Asia/Calcutta');
$today = date("Y-m-d");
$time = date("h:i A"); 
$currentyear = date("Y");

?>
<?php 
$querylogin = "select * from users where username = '". $_SESSION["LOGIN_NAME"]."' AND password = '".$_SESSION["EMPLOYEE_PASSWORD"]."'";
$resultlogin = mysql_query($querylogin,$conn);
while($row=mysql_fetch_array($resultlogin))
{
$Designation = $row['designation'];
$staff_name = $row['full_name'];

}
?>
<?php
function randomPassword() {
    $alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 5; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}
$unique_id = randomPassword();
?>
<?php
$customer_name = $_POST['customer_name'];
$customer_id = $_REQUEST['customer_id'];
$id = $_REQUEST['id'];

$comptype = $_POST['comptype'];
$assigned_to = $_POST['assigned_to'];
$subindustry = $_POST['subindustry'];
 $tertiary =$_POST['tertiary'];
$paccname = $_POST['paccname'];
$industry = $_POST['industry'];
$turnover = $_POST['turnover'];
$website = $_POST['website'];
$credittime = $_POST['credittime'];
$creditlimit = $_POST['creditlimit'];
$last_modified = $_POST['modifiedby'];
$created_by = $_POST['reportto'];
$Outstanding = $_POST['Outstanding'];

$billing_address = $_POST['baddrl1'];
$bcity = $_POST['bcity'];
$bstate = $_POST['bstate'];
$bcountry = $_POST['bcountry'];
$bpin = $_POST['bpin'];
$bphone = $_POST['bphone'];
$bfax = $_POST['bfax'];
$bemail = $_POST['bemail'];
$description = $_POST['description'];
$acccode= $_POST['acccode'];
$dispname = $_POST['dispname'];
$lsrc = $_POST['lsrc'];
$last_year_turnover = $_POST['last_year_turnover'];
$bgst_no = $_POST['bgst_no'];

//$assigned_to = $_POST['allname'];

$shipping_address = $_POST['saddrl1'];
$scity = $_POST['scity'];
$sstate = $_POST['sstate'];
$scountry = $_POST['scountry'];
$spin = $_POST['spin'];
$sphone = $_POST['sphone'];
$sfax = $_POST['sfax'];
$semail = $_POST['semail'];
$sdistrict = $_POST['sdistrict'];
$bdistrict = $_POST['bdistrict'];
$segment=$_POST['segment'];
$bstate_code = $_POST['bstate_code'];
$pan_no = $_POST['pan_no'];

$query1 = "update customer_master set customer_name = '$customer_name', comptype= '$comptype', assigned_to = '$assigned_to', pan_no = '$pan_no',turnover='$turnover',credittime='$credittime',creditlimit='$creditlimit',last_modified='$staff_name',billing_address='$billing_address',bcity='$bcity',bstate='$bstate',bcountry='$bcountry',bpin='$bpin',bphone='$bphone',bfax='$bfax',bemail='$bemail',description='$description',acccode='$acccode',dispname='$dispname',lsrc='$lsrc',bdistrict='$bdistrict',tertiary='$tertiary',last_year_turnover='$last_year_turnover',Outstanding='$Outstanding',segment='$segment',bstate_code='$bstate_code',bgst_no='$bgst_no' where customer_id = '".$_REQUEST['customer_id']."'and id='".$_REQUEST['id']."'";
//echo $query1;





$r=mysql_query($query1,$conn);

//echo $query;






//echo $qry2;

$saul = $_POST['saul'];
$name = $_POST['name'];
$gender = $_POST['gender'];
$bday = $_POST['bday'];
$department = $_POST['department'];
$designation = $_POST['designation'];
$contact_types = $_POST['contact_types'];
$email = $_POST['email'];
$office_phone = $_POST['office_phone'];
$mobile_no = $_POST['mobile_no'];
$linkedinid = $_POST['linkedinid'];
$twitterid = $_POST['twitterid'];
$remark = $_POST['remark'];
if($_REQUEST['contact_id']=='' AND $_REQUEST['newcontact']!='')
{
$query3 = "insert into contact_person(id,saul,name,gender,bday,department,designation,contact_types,email,office_phone,mobile_no,linkedinid,twitterid,remark,customer_name,customer_id) values ('$id','$saul','$name','$gender','$bday','$department','$designation','$contact_types','$email','$office_phone','$mobile_no','$linkedinid','$twitterid','$remark','$customer_name','$customer_id')";
//echo $query3;
$r3=mysql_query($query3,$conn);

}else {
$query3 = "update contact_person set saul = '$saul', name= '$name', gender = '$gender', bday = '$bday',department='$department',designation='$designation',contact_types='$contact_types',email='$email',office_phone='$office_phone',mobile_no='$mobile_no',linkedinid='$linkedinid',twitterid='$twitterid',remark='$remark',customer_name='$customer_name' where customer_id = '".$_REQUEST['customer_id']."' AND contact_id='".$_REQUEST['contact_id']."'";
//echo $query3;
$r3=mysql_query($query3,$conn);

}

$query31 = "select * from factory_address where customer_id='".$_REQUEST['customer_id']."'and id='".$_REQUEST['id']."' and biil_address!='YES' ";
//echo $query31;
$result31 = mysql_query($query31,$conn);	
while($row=mysql_fetch_array($result31))
{

$factory_id=$row['factory_id'];
$address= $_POST['address'.$factory_id];
$country= $_POST['country'.$factory_id];
$state= $_POST['state'.$factory_id];
$district= $_POST['district'.$factory_id];
$pincode= $_POST['pincode'.$factory_id];
$email= $_POST['email'.$factory_id];
$phone= $_POST['phone'.$factory_id];
$fax= $_POST['fax'.$factory_id];
$city= $_POST['city'.$factory_id];
$sstate_code= $_POST['sstate_code'.$factory_id];
$sgst_no= $_POST['sgst_no'.$factory_id];


$query2aa="update factory_address set address ='$address',country='$country',state='$state',district='$district',pincode='$pincode',email='$email',phone='$phone',fax='$fax',city='$city',sstate_code='$sstate_code',gst_no='$sgst_no' where customer_id = '" . $_REQUEST['customer_id'] . "' and factory_id='$factory_id'and id = '" . $_REQUEST['id'] . "'";
//echo $query2aa;
$r2=mysql_query($query2aa,$conn);
}
$bill_factory_id=$_POST['bill_factory_id'];

$query2aa="update factory_address set address ='$billing_address',country='$bcountry',state='$bstate',district='$district',pincode='$bpin',email='$bemail',phone='$bphone',fax='$bfax',city='$bcity',sstate_code='$bstate_code',gst_no='$bgst_no' where customer_id = '" . $_REQUEST['customer_id'] . "' and factory_id='$bill_factory_id' and id = '" . $_REQUEST['id'] . "'";
//echo $query2aa;,
$r2=mysql_query($query2aa,$conn);


$query2aac="update customer_master set shipping_address ='$address',scountry='$country',sstate='$state',sdistrict='$district',spin='$pincode',semail='$email',sphone='$phone',sfax='$fax',scity='$city',sstate_code='$sstate_code',sgst_no='$sgst_no' where customer_id = '" . $_REQUEST['customer_id'] . "'and id = '" . $_REQUEST['id'] . "'";


$r2=mysql_query($query2aac,$conn);

//echo $qry3;
//exit();

?> 

<script>
javascript:window.location.href="edit_accounts.php?contact_id=<?php echo $_REQUEST['contact_id'] ?>&&customer_id=<?php echo $_REQUEST['customer_id'] ?>&&id=<? echo $_REQUEST['id']?>";
</script>
