<?php session_start();
date_default_timezone_set('Pacific/Auckland');
	if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  
  	include("configi.php");


  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conn, $theValue) : mysqli_escape_string($conn, $theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

ini_set('memory_limit', '512M');


	include("configi.php");


//echo "Search option is " . $search_option;


//echo $_GET['suppid'];
$suppid =  2;

//695

$ac = 0;

$job_compid = 1;
$reportcompname = "hello";

$msg = "";


$sales_id =  $_GET['sales_id'];

$ac = 0;



	include("configi.php");


		$query_pag_data = "select * from " . $_COOKIE["phxcrmclient_id"] . "_st where st_id = " . $sales_id;

//	echo $query_pag_data;

$result_pag_data = mysqli_query($conn, $query_pag_data);
while ($row = mysqli_fetch_array($result_pag_data)) {
$sales_id 			= $row["st_id"];
$client_id 			= $row["uid_client"];
//echo "client id is " . $client_id;

$booking_status 	= $row["status"];
$comments 			= $row["comments"];
$total_tax 			= $row["total_tax"];
$total_sale 		= $row["total_sale"];
$total_paid 		= $row["total_paid"];
$sales_date 		= $row["sales_date"];	
$sales_staff 		= $row["sales_staff"];
$sales_comments 	= $row["sales_comments"];
$sales_status 		= $row["status"];
$p_sales_status_date		= $row["p_status_date"];


$job_uid_contact 	= $row["job_uid_contact"];


$job_sales_manager_name		= $row["job_sales_manager_name"];
$job_rep_person_name		= $row["job_rep_person_name"];
$job_tech1_person_name		= $row["job_tech1_person_name"];

$a_postline1	 		= $row["a_postline1"];
$a_postline2	 		= $row["a_postline2"];
$a_postline3	 		= $row["a_postline3"];
$a_postline4	 		= $row["a_postline4"];
$a_postline5	 		= $row["a_postline5"];

$s_postline1	 		= $row["s_postline1"];
$s_postline2	 		= $row["s_postline2"];
$s_postline3	 		= $row["s_postline3"];
$s_postline4	 		= $row["s_postline4"];
$s_postline5	 		= $row["s_postline5"];

$s_credterms	 		= $row["s_credterms"];

$job_descr				= GetSQLValueString($row["job_descr"], "text2");
$jobsheet_comments		= GetSQLValueString($row["jobsheet_comments"], "text2");


$client_order_no		= $row["client_order_no"];

$job_compid      		= $row["job_compid"];

$reference_no      		= $row["reference_no"];

$job_date_start    		= $row["job_date_start"];

$job_time_start    		= $row["job_time_start"];

$job_date_end    		= $row["job_date_end"];

$job_time_end    		= $row["job_time_end"];

}


	include("configi.php");
  
$query="select * from phx_userid_outlet where client_id =  " . $_COOKIE['phxclient_id'] . " and outlet_id = " . $job_compid;

$result_pag_data2 = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result_pag_data2)) {
$outlet_name 			= $row["outlet_name"];
$address1 				= $row["address1"];
$address2 				= $row["address2"];
$address3 				= $row["address3"];
$address4 				= $row["address4"];
$address5 				= $row["address5"];

$phys_address1 			= $row["phys_address1"];
$phys_address2 			= $row["phys_address2"];
$phys_address3 			= $row["phys_address3"];
$phys_address4 			= $row["phys_address4"];
$phys_address5 			= $row["phys_address5"];

$quote_text1 			= $row["quote_text1"];
$quote_text2 			= $row["quote_text2"];
$quote_text3 			= $row["quote_text3"];
$quote_text4 			= $row["quote_text4"];
$quote_text5 			= $row["quote_text5"];

$office_phone 			= $row["office_phone"];
$office_fax 			= $row["office_fax"];
$gst_number 			= $row["gst_number"];
$website 				= $row["website"];
$outlet_email			= $row["outlet_email"];

}


	
	include("configi.php");

$sql="SELECT " . $_COOKIE["phxcrmclient_id"] . "_client.* FROM " . $_COOKIE["phxcrmclient_id"] . "_client where " . $_COOKIE["phxcrmclient_id"] . "_client.uid_client = " . $client_id;

//echo $sql;

$result = mysqli_query($conn, $sql);

$intcounter = 0;

while ($row = mysqli_fetch_array($result)) {


$ccomp_name				= $row["comp_name"];
$ccont_name 			= $row["cont_name"];
$ccell_phone 			= $row["cell_phone"];
$cwork_phone 			= $row["work_phone"];
$chome_phone 			= $row["home_phone"];
$cemail		 			= $row["email"];
$cwebsite		 		= $row["website"];



	$intcounter = $intcounter + 1;
	
	}
	
	if ($job_uid_contact == 0){
}else{
						
					$sqlcontact = "SELECT * FROM ". $_COOKIE["phxcrmclient_id"]."_client_contacts where uid_client = ".$client_id ." and con_uid = ". $job_uid_contact;
		//	echo $sqlcontact;

					$resultcontact = $conn->query($sqlcontact);


					// output data of each row
					while($rowcontact = $resultcontact->fetch_assoc()) {

						$ccont_name 		= $rowcontact["contact_name"];
						$cwork_phone 		= $rowcontact["contact_work_phone"];					
						$ccell_phone 		= $rowcontact["contact_mobile"];	
						$a_postline5 		= $rowcontact["contact_name"];	

					}

}

	$address1_len = strlen($a_postline1);
	$address2_len = strlen($a_postline2);
	$address3_len = strlen($a_postline3);
	$address4_len = strlen($a_postline4);
	$address5_len = strlen($a_postline5);

    $address5p_len = strlen($a_postline5);
	
  if ($address5p_len > 1) {

        $a_postline5 = "Attn: " . $a_postline5;

    }
	
	    $address5s_len = strlen($s_postline5);
	
  if ($address5s_len > 1) {

        $s_postline5 = "Attn: " . $s_postline5;

    }

					$qtl = '<table  cellspacing="0" style="border: none;">
					<tr bgcolor="lightgrey" border="none">
	                        <td style="width: 640px; text-align: centre;"><strong>Description</strong></td>
                        <td style="width: 90px; text-align: centre;"><strong>Originals</strong>
						<td style="width: 90px; text-align: centre;"><strong>Quantity</strong></td>
                    </tr>';
                
				
$sql="SELECT " . $_COOKIE["phxcrmclient_id"] . "_qtl.*, " . $_COOKIE["phxcrmclient_id"] . "_qtl.qtl_catname as prod_catname,"  . $_COOKIE["phxcrmclient_id"] . "_prodserv.prod_catname," . $_COOKIE["phxcrmclient_id"] . "_qtl.qtl_title AS line_desc, " . $_COOKIE["phxcrmclient_id"] . "_cat_sort.level, If(level>=1,level,99) AS sort, concat(If(level>=1,level,99), " . $_COOKIE["phxcrmclient_id"] . "_qtl.qtl_catname) AS sort_cat FROM (" . $_COOKIE["phxcrmclient_id"] . "_qtl LEFT JOIN " . $_COOKIE["phxcrmclient_id"] . "_prodserv ON " . $_COOKIE["phxcrmclient_id"] . "_qtl.prodserv_uid = " . $_COOKIE["phxcrmclient_id"] . "_prodserv.uid) LEFT JOIN " . $_COOKIE["phxcrmclient_id"] . "_cat_sort ON " . $_COOKIE["phxcrmclient_id"] . "_qtl.qtl_catname = " . $_COOKIE["phxcrmclient_id"] . "_cat_sort.category  where st_id = " . $sales_id . " ORDER BY sort_cat, id_stl;";
//echo $sql;
$qtl = $qtl . '<tr>';
 $ttl_quote_net = 0;
 $currcat = "";
 $lastcat = "";
$i2 = 1;
$result_pag_data = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result_pag_data)) {

$prodserv_uid			= $row["prodserv_uid"];
$qtl_title				= $row["qtl_title"];
$qtl_orig				= $row["qtl_orig"];
$qtl_qty				= $row["qty"];
$qtl_days				= $row["qtl_days"];
$qtl_rrp_price			= $row["rrp_price"];
$qtl_rrp_price_format	= number_format($qtl_rrp_price, 2, '.', ',');

$qtl_line_disc			= $row["line_disc"];
$qtl_item_amount		= $row["item_amount"];
$qtl_item_amount_format	= number_format($qtl_item_amount, 2, '.', ',');
$qtl_line_amount		= $row["line_amount"];
$qtl_line_amount_format	= number_format($qtl_line_amount, 2, '.', ',');
$ttl_quote_net = $ttl_quote_net + $qtl_line_amount;
 
$currcat				= $row["qtl_catname"];
 
 
 if ($currcat == $lastcat){
 
 }else{

	$qtl = $qtl . '<tr>
					<td>
					<span style="font-size: 20px;"><b>' . $currcat . '</b></span>
					</td>
					</tr>';
	$lastcat = $currcat;
					
 }


 $qtl = $qtl . '<tr>
   <td style="width: 500px; border: none;">' . $qtl_title . '</td>
     <td style="width: 70px; border: none; align: centre;">' . $qtl_orig . '</td>
	      <td style="width: 70px; border: none; align: centre;">' . $qtl_qty . '</td>
 </tr>';

  
  }

  $currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('d/m/Y H:i:s', strtotime($currentDateTime));
$current_username = "Kevin Jellyman";
 
  $qtl = $qtl.'</tbody></table>';

$report = '
<body style="background: url(../../images/jobsheet_91.jpg); background-image-resize:5;">
<style>
p {
	margin-top: 0px;
    font-size: 14px;
	}
</style>
<div style="position: fixed; left: -9mm; top: 1mm;">
 #*company_name*#
 </div>
 <div style="position: fixed; left: -9mm; top: 11mm; width: 350px;">
 #*contact_name*#
 </div>
  <div style="position: fixed; left: -9mm; top: 21mm;">
 #*contact_email*#
 </div>
   <div style="position: fixed; left: -9mm; top: 32mm;">
 #*contact_work_phone*#
 </div>
    <div style="position: fixed; right: 1mm; bottom: -4mm; font-size: 12px;">
 Printed by ' . $current_username . ' on ' .$newDateTime.'
 </div>
    <div style="position: fixed; left: 1mm; top: 105mm;">
 <strong>Job Description:</strong> #*job_descr*#
	<strong>Jobsheet Comments:</strong> #*jobsheet_comments*#
	<br>
 #*qtllines*#
 </div>
 
    <div style="position: fixed; left: 4mm; top: 39mm;">
 <span style="font-size: 18px">#*job_start_date*# <b>T:</b> #*job_start_time*#</span>
 </div>
     <div style="position: fixed; right: 1mm; top: 71mm; text-align: center; width: 190px;">
 <span style="font-size: 14px;">#*job_credterms*#</span>
 </div>

    <div style="position: fixed; left: 18mm; top: 51mm;">
 <span style="font-size: 18px">#*job_end_date*# <b>T:</b> #*job_end_time*#</span>
 </div> 
 <div style="position: fixed; right: 0mm; top: -3mm;">
 <span style="font-size: 30px">#*ff1_st_id*#</span>
 </div>

</body>
';
			

$report = str_replace("#*job_credterms*#",$s_credterms,$report);
			
$report = str_replace("#*company_name*#",$ccomp_name,$report);

$report = str_replace("#*contact_name*#",$ccont_name,$report);

$report = str_replace("#*contact_email*#",$cemail,$report);

$report = str_replace("#*company_addr1*#",$s_postline1,$report);

$report = str_replace("#*company_addr2*#",$s_postline2,$report);

$report = str_replace("#*company_addr3*#",$s_postline3,$report);

$report = str_replace("#*company_addr4*#",$s_postline4,$report);

$report = str_replace("#*company_addr5*#",$s_postline5,$report);

$report = str_replace("#*ff1_st_id*#",$sales_id,$report);

$report = str_replace("#*job_descr*#",$job_descr,$report);

$report = str_replace("#*qtllines*#",$qtl,$report);


$report = str_replace("#*jobsheet_comments*#",$jobsheet_comments,$report);

//$report = str_replace("#*quote_txn_line*#",$qtl,$report);

//$report = str_replace("#*yourcompany_name*#",$outlet_name,$report);

//$report = str_replace("#*yourcompany_addr1*#",$phys_address1,$report);

//$report = str_replace("#*yourcompany_addr2*#",$phys_address2,$report);

//$report = str_replace("#*yourcompany_phone*#","Office Phone: ".$office_phone,$report);


$report = str_replace("#*cust_ref*#",$client_order_no,$report);
$report = str_replace("#*contact_name*#",$ccont_name,$report);
$report = str_replace("#*contact_work_phone*#",$cwork_phone,$report);
$report = str_replace("#*contact_cell_phone*#",$ccell_phone,$report);
$jsd		=	strtotime($job_date_start);
$report = str_replace("#*job_date*#",date("d-M-Y",$jsd),$report);
$jed		=	strtotime($job_date_end);
$report = str_replace("#*job_start_date*#",date("d-m-Y",$jsd),$report);
$report = str_replace("#*job_end_date*#",date("d-m-Y",$jed),$report);

$jst = date ('h:i a',strtotime($job_time_start));
$jet = date ('h:i a',strtotime($job_time_end));

$report = str_replace("#*job_start_time*#",$jst,$report);
$report = str_replace("#*job_end_time*#",$jet,$report);


$report = str_replace("#*job_outlet_compid*#",$_COOKIE['phxclient_id'],$report);
$report = str_replace("#*job_outlet_id*#",$job_compid,$report);

//echo "dave".$ccont_name."dave";

//echo $report;

//exit;

include("mpdf574/mpdf.php");

//$mpdf->useOnlyCoreFonts = true;

	$file_name = "jobsheet_".$sales_id.".pdf";

$mpdf=new mPDF('c','A4', 14, 'Arial', 15, 15, 5, 5, 5, 5, 'P');
//$mpdf->debug = true;
$mpdf->SetDisplayMode('fullpage');

$mpdf->WriteHTML($report);	// Separate Paragraphs  defined by font
$content = $mpdf->Output('', 'S');

// You can now optionally also send it to the browser
$mpdf->Output($file_name, 'D');
exit;
?>