<?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");
$today01 = date("d-m-Y");
$time = date("h:i A"); 
$currentyear = date("Y");

?>
<?php $querylogin = "select * from customer_invoice_details where username = '". $_SESSION["LOGIN_NAME"]."' AND password = '".$_SESSION["EMPLOYEE_PASSWORD"]."'";
$resultlogin = mysql_query($querylogin,$conn);
while($row=mysql_fetch_array($resultlogin))
{
$customer_name_entry = $row['customer_name'];
$customer_id_forword = $row['customer_id'];
}

?>
<?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 
	          
$inv_id01 = $_REQUEST['inv_id'];               

$kind_attn=$_POST['kind_attn'];		
$contact_no = $_POST['contact_no'];
$billing_address = $_POST['billing_address'];
$email_id = $_POST['bemail'];
$mobileno = "91"."$contact_no";
$customer_id01 = $_REQUEST['customer_id01'];


if(!is_numeric($_REQUEST['customer_id']) ) {
$customer_name = $_REQUEST['customer_id'];
$qrycust = "insert into customer_master(customer_name,kind_attn,contact_no,bemail,unique_id,entry_by,dealer_id,billing_address) values ('$customer_name','$kind_attn','$contact_no','$email_id','$unique_id','$customer_name_entry','$customer_id01','$billing_address')";

$rcust=mysql_query($qrycust,$conn);
//echo $qrycust;
$qry3z = "select * from customer_master where customer_name = '".$_REQUEST['customer_id']."' and unique_id='$unique_id'";
$re3z= mysql_query($qry3z);
while($row=mysql_fetch_array($re3z))
  {  
  $customer_id = $row['customer_id'];
  }
}else {
 $query2s = "select * from customer_master where customer_id='".$_REQUEST['customer_id']."'";
      //echo $query2s;
       $result2s = mysql_query($query2s,$conn); 
        while($row=mysql_fetch_array($result2s))
                                                {
             $customer_name = $row['customer_name'];
			 $customer_id = $_REQUEST['customer_id'];
             }
}
$qry3 = "insert into contact_person(customer_id,name,email,mobile_no,customer_name) values ('$customer_id','$kind_attn','$email_id','$contact_no','$customer_name')";
$r3=mysql_query($qry3,$conn);
?>
<?php 


$checked_count = count($_POST['check_list']);
foreach($_POST['check_list'] as $selected) {
$serial_no = $_POST['serial_no'.$selected];
$inv_id = $_POST['inv_id'.$selected];

$query05 = "select * from dispatched_product_details where dispatched_id = '$selected' and serial_no_warpp = '$serial_no'";
$result05 = mysql_query($query05,$conn);
//echo $query05;
$num_rows2 = mysql_num_rows($result05);
if($num_rows2==1){

while($row=mysql_fetch_array($result05))
{
               $machine_name = $row['machine_name'];
			   $qty = $row['qty'];
			   $machine_id = $row['machine_id'];
			   $serial_no_warpp = $row['serial_no_warpp'];

$query10 = "insert dispatched_product_details(customer_id,customer_name,inv_no,machine_id,machine_name,qty,inv_id,serial_no_warpp,serial_no,serial_no_updated,Forward_by,update_date) values ('$customer_id','$customer_name','$inv_no','$machine_id','$machine_name','$qty','$inv_id','$serial_no_warpp','$serial_no','Done','$customer_id01','$today01')";
$result10=mysql_query($query10,$conn);

$query11 = "update dispatched_product_details set serial_no_updated='Done',Forward_by='$customer_id' where dispatched_id = '$selected'";
$result11=mysql_query($query11,$conn);
//echo $query10;
}
} else { 

$query05 = "select * from dispatched_product_details where dispatched_id = '$selected' and serial_no_warpp != '$serial_no'";
$result05 = mysql_query($query05,$conn);

while($row=mysql_fetch_array($result05))
{
               $machine_name01 = $row['machine_name'];


}

}
}

$query2d = "select * from dispatched_product_details where customer_id='$customer_id01' and inv_no = '$inv_no' and serial_no_warpp=''";
$result2d = mysql_query($query2d,$conn); 
//echo $query2d;
$num_rows2d = mysql_num_rows($result2d);
if($num_rows2d ==0) {
$query11s = "update customer_invoice_details set serial_no_updated='Done' where customer_id='$customer_id01' and inv_no = '$inv_no'";
$result11s=mysql_query($query11s,$conn);
//echo $query11s;
}





//exit();
?> 

<?php 
$to = "$email_id";
   //$to1 = "info@tagabag.in";
$subject = "Congratulations";
$message1 = "Dear $kind_attn\n\n";
$message1 .= "Congratulations on buying Warpp Machine from our Authorised Distributor $customer_name_entry.\n\n";
$message1 .= "We sincerely believe that the equipment supplied by us will perform to its fullest potential. We thank you for your trust in brand Warpp.\n\n\n\n";
$message1 .="Thanks & Regards,\n";
$message1 .="Team Warpp\n";
$message1 .="http://www.warpp.co.in";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "From:support@warpp.co.in \r\n";
$retval = mail ($to,$subject,$message1,$header);
//$retval = mail ($to1,$subject,$message,$header);

if( $retval == true )  
   {
//"Message has been sent";
}
else{
// "Message has been  not sent";
}
// $query10;
//echo $query11;
//echo $$query02;
//echo $qrycust;
//exit();

//exit();
?>


<script>
javascript:window.location.href="index.php?massage=<?php echo $machine_name01?>";
</script>
