<?php 
require_once('config/lang/eng.php');
require_once('tcpdf.php');
$con = mysqli_connect("127.0.0.1", "root", "", "herp_amch"); 

 $recordid=$_REQUEST['record'];
 $SID=$_REQUEST['Sid'];
 function getDataField($table,$condition,$field)
 {  
 
       	 $sql=mysql_query("select ".$field." from ".$table." where  $condition  ");
	     $r=mysql_fetch_array($sql);
    	return ($r[$field]);
  }
 
 // create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	$add_query = "select * from companies where 1=1";
  	$result = mysql_query($add_query);
	$num_rows = mysql_num_rows($result);
 	if($num_rows > 0)
	{
		$row=mysql_fetch_array($result);
		$org_name = $row["CompanyName"];
		$org_logoname = 'compaint.jpg';
		$org_address = $row["Address1"];
		$org_city = $row["City"];
		$org_country  = $row["Country"];
		$org_Zipcode  = $row["Zipcode"];
		$org_State  = $row["State"];
		$logo_name = 'compaint.jpg';
 	}

   
  $pdf->SetHeaderData($logo_name, 10, $org_name , $org_address."\n".$org_city." ".$org_Zipcode."\n ".$org_country);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
 $pdf->SetMargins(PDF_MARGIN_LEFT,25, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
$pdf->AddPage();
  $tbl='';
 $r="Sales Order";

 	$contact_details = "select * from crm_leads where 1=1";
	$result_contact = mysql_query($contact_details);
	$num_rows =mysql_num_rows($result_contact);
 	if($num_rows > 0)
	{
		$rowdate=mysql_fetch_array($result_contact);
 	 	$salutation   = $rowdate["contact_name"];
	 	$firstname   = $rowdate["fname"];
	  	$lastname   = $rowdate["lname"];
	 	$emailid     = $rowdate["email"];
 	}
 
 
 $terms_cond1 = "select * from paymentterms where termsindicator='1'";
$result_termcond= mysql_query($terms_cond1);
$row_term= mysql_fetch_array($result_termcond);
  $terms_conditions = $row_term["terms_condition"];

 
 $logo="images/".$org_logoname;
$tbl.='<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center"><tr><td style="font-size:30px;"><strong><br>'.$r.'</strong></td></tr></table><br>';
 $tbl.='<table width="100%" border="0" cellspacing="0" cellpadding="5" style="border:1px solid #cccccc;"><tr><td align="left" width="60%">Kind Attention : '.$salutation.' </td>
<td  align="left" width="40%"> Email : '.$emailid.'</td>
</tr></table><br>';

 	$get_tern_query =mysql_query( "select enquiryNo,validTill,quotationNo,site from quotation where id='".$recordid."'");
 		$rowquotoes= mysql_fetch_array($get_tern_query);
 	 	 $validtill   = $rowquotoes["validTill"]; 
		 $quotationNo = $rowquotoes["quotationNo"]; 
	 	 $enqueryNo = $rowquotoes["enquiryNo"]; 
		  $site = $rowquotoes["site"]; 

       $account_details = "select * from crm_accounts  where      accid='".$enqueryNo."'";
	    $result_account = mysql_query($account_details);
 		$rowquotoes_address= mysql_fetch_array($result_account);
	 	$leadid=   $rowquotoes_address["mst_leadid"];
	 	$accountname          = getDataField('crm_leads',"leadid='".$leadid."'",'company_name');  
		 
		$bill_city   = getDataField('city_master',"id='".$rowquotoes_address["city"]."'",'city');  
		$bill_code   =$rowquotoes_address["zipcode"];
		$bill_country   =getDataField('country_master',"id='".$rowquotoes_address["country"]."'",'country'); ;
		$bill_state     = getDataField('state_master',"id='".$rowquotoes_address["state"]."'",'state'); 
 		$phone=$rowquotoes_address["address1"];     
 	 
	 
	
$validtill_date=explode('-',$validtill);
$tbl.='<table width="100%" border="0" cellspacing="0" cellpadding="5" style="border:1px solid #cccccc; font-size:28px;"><tr><td align="left" width="60%">'.$accountname.'<br>'.$bill_state.', '.$bill_city.'<br>Pincode :'.$bill_code.' ,'.$bill_country.'<br>Tel:'.$phone.'</td>
<td  align="left" width="40%" style="font-size:28px;">Quotation No:'.$quotationNo.'<br>Date: '.date('d/m/Y').'<br>Valid Till: '.$validtill_date[2].'/'.$validtill_date[1].'/'.$validtill_date[0].' </td>
</tr></table><br>';

$tbl.='<table width="100%" border="0" cellpadding="2" cellspacing="0" style="border-bottom:1px solid #cccccc;">  
  <tr valign="top">
    <td width="5%" align="center" style="border-top:1px solid #cccccc; border-left:1px solid #cccccc;"><strong>S.No</strong></td>
    <td width="30%" align="center" style="border-top:1px solid #cccccc; border-left:1px solid #cccccc;"><strong>Description</strong></td>
	<td width="15%" align="center" style="border-top:1px solid #cccccc; border-left:1px solid #cccccc;"><strong>Shade Name</strong></td>
    <td width="15%"align="center" style="border-top:1px solid #cccccc; border-left:1px solid #cccccc;"><strong>Serial No</strong></td>
	<td width="15%" align="center" style="border-top:1px solid #cccccc; border-right:1px solid #cccccc; border-left:1px solid #cccccc;"><strong>Unit Sale Price Price</strong></td>
     <td width="20%"align="center" style="border-top:1px solid #cccccc; border-left:1px solid #cccccc; border-right:1px solid #cccccc;"><strong>Min.Qty./<br>Shade/Order</strong></td>
	 
  </tr>';
  $j=1;
  
  $associated_products="select * from crm_lead_items where mst_leadid=$leadid";
   $result_product= mysql_query($associated_products);
   while($row_product= mysql_fetch_array($result_product))
  {
	$pcode=$row_product['item_code'];
	 $productname=getDataField('item_master',"id=$pcode",'item_name');
 	 $serialno=getDataField('item_master',"id=$pcode",'serial_no');
 	 $shade_name=getDataField('item_master',"id=$pcode",'shade_name');
	
 
 //	$comment=$row_product['comment'];
	$quantity=$row_product['stock_qty'];
	$listprice=$row_product['stock_price'];
 		 $tbl.='<tr valign="top">
			<td  style="border-top:1px solid #cccccc; border-left:1px solid #cccccc;">'.$j.'</td>
			<td align="left" style="border-top:1px solid #cccccc; border-left:1px solid #cccccc;">'.$productname.'</td>
			<td align="center" style="border-top:1px solid #cccccc; border-left:1px solid #cccccc;">'.$shade_name.'</td>
			<td align="center" style="border-top:1px solid #cccccc; border-left:1px solid #cccccc;">'.$serialno.'</td>
			<td align="center" style="border-top:1px solid #cccccc; border-right:1px solid #cccccc; border-left:1px solid #cccccc;">'.$listprice.'</td>
 			<td align="center" style="border-top:1px solid #cccccc; border-left:1px solid #cccccc; border-right:1px solid #cccccc;">'.$quantity.' </td>
			
		  </tr>';
		  $j++;
 	} 
	
	
 
  $tbl.='</table><br>';
    $tbl.="<table width='100%' border='0' cellpadding='0' cellspacing='0' bgcolor='#cccccc'><tr bgcolor='#ffffff'><td align='left'><strong>Terms &amp; Conditions</strong><br></td></tr><tr bgcolor='#ffffff'><td  align='left'>".$terms_conditions."</td></tr> </table>";

 // output the HTML content
$pdf->writeHTML($tbl, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();
//Close and output PDF document
$pdf->Output("SalesOrder_$recordid.$SID.pdf", 'D');

//============================================================+
// END OF FILE                                                
//============================================================+
