<?php
include 'connection.php';
session_start();

if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    }
error_reporting(0);
$querylogin = "select * from  user_details where user_id = '". $_REQUEST["user_id"]."'";
$resultlogin = mysqli_query($conn,$querylogin);
while($row=mysqli_fetch_array($resultlogin))
{
$user_name = $row['user_name'];
$user_id = $row['user_id'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$email_id=$row['email_id'];
$mobile_no=$row['mobile_no'];


}
$final_proforma_no = $_REQUEST['final_proforma_no'];


$sales_person_name = $first_name .' '.$last_name;

$query8s = "select * from company_master  ";
$result8s = mysqli_query($conn,$query8s);	
while($row=mysqli_fetch_array($result8s))
{
$master_company_name=$row['company_name'];
$comp_url = $row['comp_url'];
$donotreply_email = $row['donotreply_email'];
$from_emailid_mail = $row['from_emailid_mail'];
}	

$queryqtn = "select * from quotation_make_table_1 where customer_id = '".$_REQUEST['customer_id']."' AND quotation_id ='".$_REQUEST['quotation_id']."' ";
$resultqtn = mysqli_query($conn,$queryqtn);	
while($row=mysqli_fetch_array($resultqtn))
{
$final_quotation_no=$row['final_quotation_no'];
$customer_name = $row['customer_name'];
$qtn_status = $row['qtn_status'];
}	



$querydiscountds ="select * from quotation_more_product_02 where customer_id = '".$_REQUEST['customer_id']."' AND quotation_id ='".$_REQUEST['quotation_id']."' ORDER BY  more_product_id ASC";
$resultdiscountds = mysqli_query($conn,$querydiscountds);	
while($row=mysqli_fetch_array($resultdiscountds))
{
$product_id = $row['product_id'];
$extra_pro = $row['extra_pro'];
$product_type09 = $row['product_type'];
if($extra_pro==''){
$querylsds = "select * from mst_product_pdf where product_id='$product_id'";
$resultlsds = mysqli_query($conn,$querylsds);	
$rowcountmp=mysqli_num_rows($resultlsds);
//echo "<br>";
if($rowcountmp>='1'){
while($row=mysqli_fetch_array($resultlsds))
{
$pdf_name09 = $row['pdf_name'];
if($pdf_name09!=''){
$pdf_name[] = $pdf_name09;
}
}
}
}
if($pdf_name09!=''){
$pdf_name01 = implode(',', $pdf_name);   
$pdf_name02 = explode(',', $pdf_name01);
}
}

if($_REQUEST['filetype']=='quotation'){

$pdf_name = $final_quotation_no . '.pdf';
$final_no = $final_quotation_no;
}else{
$pdf_name = $final_proforma_no . '.pdf';
$final_no = $final_proforma_no;
}

$email_note = $_REQUEST['email_note'];
//echo $pdf_name;
//exit();

$querydiscountds1 ="select * from quotation_more_product_02 where customer_id = '".$_REQUEST['customer_id']."' AND quotation_id ='".$_REQUEST['quotation_id']."' ORDER BY  more_product_id ASC";
$resultdiscountds1 = mysqli_query($conn,$querydiscountds1);	
while($row=mysqli_fetch_array($resultdiscountds1))
{
$product_id = $row['product_id'];

$querylsds1 = "select * from product_details where product_id='$product_id'";
$rowcountmp1= mysqli_query($conn,$querylsds1);	
while($row=mysqli_fetch_array($rowcountmp1))
{
$category_id = $row['category_id'];
}

$querylsds2 = "select * from product_category_master where category_id='$category_id'";
$resultlsds2 = mysqli_query($conn,$querylsds2);	
$rowcountmp2=mysqli_num_rows($resultlsds2);
if($rowcountmp2>='1'){
while($row=mysqli_fetch_array($resultlsds2))
{
$pdf_name01a = $row['pdf_name'];
if($pdf_name01a!=''){
$pdf_name06[] = $pdf_name01a;
}
}

}
if($pdf_name01a!=''){
$pdf_name01b = implode(',', $pdf_name06);   
$pdf_name02a = explode(',', $pdf_name01b);
}

}


?>   





<?php 
$newfilename5=$pdf_name;
//$newfilename6='AGA-PORTAL-DEALER.ppt';
//$bill_email_id = $_POST['email_id'];

//$bill_email_id=implode(',',$_POST['email_id']);

$arr = explode(",", $bill_email_id);

$sales_person_id = $_REQUEST['user_id'];
$quotation_id=$_REQUEST['quotation_id'];
$customer_id=$_REQUEST['customer_id'];


$qrycustmcust = "select * from quotation_make_table_1 where customer_id = '".$_REQUEST['customer_id']."' AND quotation_id ='".$_REQUEST['quotation_id']."'";
$resultcustmcust = mysqli_query($conn,$qrycustmcust);
while($row26=mysqli_fetch_array($resultcustmcust))
		{  
$sales_person_id = $row26['sales_person_id'];
		}
$bill_email_id02 = $_POST['bill_email_id'];

$qrycustm = "select * from mst_customer where customer_id= '".$_REQUEST['customer_id']."'";
$resultcustm = mysqli_query($conn,$qrycustm);
while($row=mysqli_fetch_array($resultcustm))
		{  
		$email_02 = $row['email_id'];
		}

if($bill_email_id02!=''){$email_01 = $bill_email_id02;}else{$email_01 =$email_02;}

$queryuser = "select * from  user_details where user_id = '$sales_person_id'";
$resultuser = mysqli_query($conn,$queryuser);
while($row=mysqli_fetch_array($resultuser))
{
$user_email_id = $row['email_id'];
$manager_id = $row['manager_id'];
}

$querymanage = "select * from  user_details where user_id = '$manager_id'";
$resultmanage = mysqli_query($conn,$querymanage);
while($row=mysqli_fetch_array($resultmanage))
{
$manager_email_id = $row['email_id'];
}


$queryemail12 = "select * from email_config1 where email_note='$email_note'";
$resultmanage2 = mysqli_query($conn,$queryemail12);
while($email_mail12=mysqli_fetch_array($resultmanage2))
{
$to_email=$email_mail12['to_email'];
$to_sales=$email_mail12['to_sales'];
$to_bmanager=$email_mail12['to_bmanager'];
$additional_email = $email_mail12['additional_email'];

$cc_email=$email_mail12['cc_email'];
$tocc_sales=$email_mail12['tocc_sales'];
$tocc_bmanager=$email_mail12['tocc_bmanager'];
$additional_ccemail=$email_mail12['additional_ccemail'];

$bcc_email=$email_mail12['bcc_email'];
$additional_bccemail=$email_mail12['additional_bccemail'];
$tobcc_sales=$email_mail12['tobcc_sales'];
$tobcc_bmanager=$email_mail12['tobcc_bmanager'];

$from_email01 = $email_mail12['from_email'];
$from_sales01 = $email_mail12['from_sales'];
$from_bmanager01 = $email_mail12['from_bmanager'];
}

if($to_sales=='YES'){
$to_sales01 = explode(',', $user_email_id);
}
if($to_bmanager=='YES'){
$to_bmanager01 = $manager_email_id;
}

if($tocc_sales=='YES'){
$tocc_sales01  = explode(',', $user_email_id);
}
if($tocc_bmanager=='YES'){
$tocc_bmanager01 = explode(',', $manager_email_id);
}

if($tobcc_sales=='YES'){
$tobcc_sales01 = $user_email_id;
}
if($tobcc_bmanager=='YES'){
$tobcc_bmanager01 = $manager_email_id;
}


if($from_sales01=='YES'){
$from_email = $user_email_id;
}
elseif($from_bmanager01=='YES'){
$from_email = $manager_email_id;
}
else{
$from_email = $from_email01;
}


if($to_email!=''){
$address_to_email = explode(',', $to_email); 
}
if($email_01!=''){ 
$address_to_customer = explode(',', $email_01);   
}
if($cc_email!=''){
$address_cc_email = explode(',', $cc_email); 
}
if($email_01!=''){ 
$address_tocc_customer = explode(',', $email_01);   
}
if($bcc_email!=''){
$address_bcc_email = explode(',', $bcc_email);  
}
if($email_01!=''){
$address_tobcc_customer = explode(',', $email_01);   
}
$queryaddto = "select * from email_config1 where additional_email>'0' and email_note='$email_note'";
$resultaddto = mysqli_query($conn,$queryaddto);
$rowcounto=mysqli_num_rows($resultaddto);
while($rowaddto=mysqli_fetch_array($resultaddto))
{
$additional_emailcount = $rowaddto['additional_email'];
}
if($rowcounto>='1')
{
for ($x = 1; $x<= $additional_emailcount; $x++) { 
 $additional_email = $_POST['additional_email'.$x];	
if($additional_email!=''){
$additional_Demail1[] = $additional_email;
$imp_add_email='done';
}
}
if($imp_add_email=='done'){
$additional_email101 = implode(',', $additional_Demail1);   
 $addressaddto = explode(',', $additional_email101);
}
}

//exit();

$queryaddtocc = "select * from email_config1 where additional_ccemail>'0' and email_note='$email_note'";
$resultaddtocc = mysqli_query($conn,$queryaddtocc);
$rowcountocc=mysqli_num_rows($resultaddtocc);
while($rowcc=mysqli_fetch_array($resultaddtocc))
{

$additional_ccemailcount = $rowcc['additional_ccemail'];
}
if($rowcountocc>='1')
{
for ($y = 1; $y<= $additional_ccemailcount; $y++) {
$additional_ccemail = $_POST['additional_ccemail'.$y];	
if($additional_ccemail!=''){
$additional_ccemail1D[] = $additional_ccemail;
$imp_add_cc_email='done';

}
}
if($imp_add_cc_email=='done'){
$additional_ccemail01 = implode(',', $additional_ccemail1D);   

$addressaddtocc = explode(',', $additional_ccemail01);
}

}
$queryaddtobcc = "select * from email_config1 where additional_bccemail>'0' and email_note='$email_note'";
$resultaddtobcc = mysqli_query($conn,$queryaddtobcc);
$rowcountobcc=mysqli_num_rows($resultaddtobcc);
while($rowbcc=mysqli_fetch_array($resultaddtobcc))
{

$additional_bccemailcount = $rowbcc['additional_bccemail'];
}
if($rowcountobcc>='1')
{
for ($z = 1; $z<= $additional_bccemailcount; $z++) {	
$additional_bccemail = $_POST['additional_bccemail'.$z];
if($additional_bccemail!=''){
$additional_bccemail1D[] = $additional_bccemail;
$imp_add_bb_cc_email='done';

}
}
if($imp_add_bb_cc_email=='done'){
$additional_bccemail01 = implode(',', $additional_bccemail1D);   
$addressaddtobcc = explode(',', $additional_bccemail01);
}
}



$query1 = "select * from email_config1 where email_note='$email_note'";
$result1 = mysqli_query($conn,$query1);
while($row=mysqli_fetch_array($result1))
{
$mail_subject = $row['mail_subject'];
}





require 'class/class.phpmailer.php';
require_once('class/class.smtp.php');
$mail = new PHPMailer();
//echo $body = file_get_contents('order_processs_mail.php?user_id='.$user_id.'&&quotation_id='.$quotation_id.'');
//$body = ob_get_clean();

// Settings
//$mail->IsSMTP();
$mail->CharSet = 'UTF-8';

$mail->SMTPDebug = 0;								//Sets Mailer to send message using SMTP
 $mail->Host = 'mail.pearlcrm.in';  //Sets the SMTP hosts of your Email hosting, this for Godaddy
 $mail->Port = '587';        //Sets the default SMTP server port
 $mail->SMTPAuth = true;       //Sets SMTP authentication. Utilizes the Username and Password variables
 $mail->Username = 'info@pearlcrm.in';     //Sets SMTP username
 $mail->Password = 'inf#cnpmasterapp@2021';     //Sets SMTP password
 $mail->SMTPSecure = '';       //Sets connection prefix. Options are "", "ssl" or "tls"
 $mail->From = $from_email; 
 $mail->addReplyTo = 'donotreply@pearlcrm.in';
 $mail->Sender= 'donotreply@pearlcrm.in';   //Sets the From email address for the message
 $mail->FromName = 'Pearl Overseas'; 
 $mail->ReturnPath= 'donotreply@pearlcrm.in'; 
// $mail->addCustomHeader('info@pearlcrm.in');
 $mail->addCustomHeader('Reply-To', $user_email_id); //$mail->addAddress('bsj@cnpindia.com');
//$mail->AddCC('rupali@sahavas.com');
//$mail->addAddress('bsj@cnpindia.com');


$mail->isHTML(true); 
$mail->Subject = $mail_subject .$final_no.':'.$customer_name;

if($to_sales01 !=''){
foreach($to_sales01 as $key => $valtosale) {
$mail->AddAddress($valtosale);
} 
}


	if($to_bmanager01!=''){
	$mail->AddAddress($to_bmanager01);
	}
	if($tobcc_sales01!=''){
	$mail->AddBCC($tobcc_sales01);
	}
	if($tobcc_bmanager01!=''){
	$mail->AddBCC($tobcc_bmanager01);
	}
	
if($tocc_sales01 !=''){
foreach($tocc_sales01 as $key => $valtoccsale) {
$mail->AddCC($valtoccsale);
} 
}

if($tocc_bmanager01 !=''){
foreach($tocc_bmanager01 as $key => $valtoccmag) {
$mail->AddCC($valtoccmag);
} 
}


if($address_to_email !=''){
foreach($address_to_email as $key => $valto) {
$mail->AddAddress($valto);
} 
}
if($address_to_customer !=''){
foreach($address_to_customer as $key => $valtocust) {
$mail->AddAddress($valtocust);
} 
}

if($address_cc_email !=''){
foreach($address_cc_email as $key => $valcc) {
$mail->AddCC($valcc);
} 
}

if($address_tocc_customer !=''){
foreach($address_tocc_customer as $key => $valcccust) {
$mail->AddCC($valcccust);
} 
}

if($address_bcc_email !=''){
foreach($address_bcc_email as $key => $valbcc) {
$mail->AddBCC($valbcc);
} 
}

if($address_tobcc_customer !=''){
foreach($address_tobcc_customer as $key => $valbcccust) {
$mail->AddBCC($valbcccust);
} 
}

if($addressaddto!=''){
foreach($addressaddto as $key => $valaddto) {
$mail->AddAddress($valaddto);
} 
}
if($addressaddtocc!=''){
foreach($addressaddtocc as $key => $valaddtocc) {
$mail->AddCC($valaddtocc);
} 
}

if($addressaddtobcc!=''){
foreach($addressaddtobcc as $key => $valaddtobcc) {
$mail->AddBCC($valaddtobcc);
} 
}


if($newfilename5!='') {
$mail->AddAttachment("$newfilename5"); 
} 
if($pdf_name09!=''){
foreach ($pdf_name02 as $valms) {
$mail->AddAttachment("../CRM_Software/product_datasheet/$valms");
}
}

if($pdf_name01a!=''){
foreach ($pdf_name02a as $valms02) {
$mail->AddAttachment("../CRM_Software/product_datasheet/$valms02");
}
}

if($filename1!='')
{ 
$mail->AddAttachment("../CRM_Software/CreatePDF/CreatePDF/service_complaint_ph/$filename1"); 
}

if($pdf_name_service!=''){ $mail->AddAttachment($pdf_name_service); }

$covering_type = $_REQUEST['type'];

$querylo25 = "select * from mst_email_covering where type = '$covering_type' ";
$resultlogin = mysqli_query($conn,$querylo25);
while($row=mysqli_fetch_array($resultlogin))
{
$note_content = $row['note_content'];
}	
		
$querylo6 = "select * from company_master";
$result6 = mysqli_query($conn,$querylo6);
while($row=mysqli_fetch_array($result6))
{
$company_name01 = $row['company_name'];
}	 


$mail->Body = $note_content .'<br><br><br>Thanking You,<br>Yours Faithfully,<br>'.$company_name01.'<br>Sales Person Name :'.$sales_person_name.'<br>Sales Persons Email: '.$email_id.'<br>Sales Persons Mobile:'.$mobile_no.'';
if(!$mail->Send()) {
 echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo $message = '<label class="text-success">Customer Details has been send successfully...</label>';
if($qtn_status<'6'){
 $query12 = "update quotation_make_table_1 set qtn_status ='6',reason='$reason',qtn_status_date='$today' where customer_id = '".$_REQUEST['customer_id']."' AND quotation_id='$quotation_id' " ;
$r1=mysqli_query($conn,$query12);
  $query121 = "update call_follow_up set step ='6' where customer_id = '".$_REQUEST['customer_id']."' AND call_follow_up_id='$call_follow_up_id' " ;
$r11=mysqli_query($conn,$query121);
}

if($_REQUEST['email_note']=='Proforma_Invoice'){
	$query12 = "update quotation_make_table_1 set PI_mail ='Done' where customer_id = '".$_REQUEST['customer_id']."' AND quotation_id ='".$_REQUEST['quotation_id']."'" ;
$r1=mysqli_query($conn,$query12);
}
}


?>


<a href="https://pearlcrm.in/CRM_Software/view_all_quotation.php" class="btn btn-primary btn-lg waves-effect waves-light" style="float:right; font-size:28px; font-family:Arial, Helvetica, sans-serif; background-color:#ff0000; float:right; color: #FFFFFF;margin-right:10px;">Back to Dashboard</a>
			<br /><br />
			<embed src="<?php echo $pdf_name?>" width="800px" height="800px" />

