<?php session_name('CAKEPHP'); session_start();
ob_start();
require_once('config/lang/eng.php');
require_once('tcpdf.php');
include('config.php');

function getField($table, $condition, $val, $field)
{
	global $db;
	$sql = mysqli_query($db, "select " . $field . " from " . $table . " where " . $condition . " ='" . $val . "'");
	$r = mysqli_fetch_array($sql);
	//echo "select ".$field." from ".$table." where ".$condition." ='".$val."'"; 
	return ($r[$field]);
}


$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$add_query = "select * from companies where 1=1";
$result = mysqli_query($con, $add_query);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) {
	$row = mysqli_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 = 'company-1.png';
}

$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();
$data = '';
$data .= "<table width='100%' border='1' cellspacing='1' cellpadding='2'>";
$data .= "<tr>";
$data .= "<th width='11%'><strong>ITEM CODE </strong></th>";
$data .= "<th width='34%'><strong>ITEM NAME </strong></th>";
$data .= "<th width='19%'><strong>MAIN CATEGORY</strong></th>";
$data .= "<th width='19%'><strong>SUB CATEGORY</strong></th>";
$data .= "</tr>";

$sql1 = $_SESSION['item'];

$result_item = mysqli_query($con, $sql1);


$i = 1;

while ($item_row = mysqli_fetch_array($result_item)) {

	$data .= "<tr>";
	$data .= "<td>" . $item_row['StockID'] . "</td>";
	$data .= "<td>" . $item_row['item_name'] . "</td>";
	$data .= "<td>";
	$main_sql = mysqli_query($con, "select stockmaincategoryname from stockmaincategory where stockmaincategoryid='" . $item_row['CategoryID'] . "'");
	$main_res = mysqli_fetch_array($main_sql);
	$data .= $main_res['stockmaincategoryname'] . " - " . $item_row['CategoryID'];
	$data .= "</td>";
	$data .= "<td>";
	$Sub_sql = mysqli_query($con, "select categorydescription from stockcategory where categoryid='" . $item_row['SubCategoryID'] . "'");
	$sub_res = mysqli_fetch_array($Sub_sql);
	$data .= $sub_res['categorydescription'] . " - " . $item_row['SubCategoryID'];
	$data .= "</td>";

	$data .= "</tr>";
	$i++;
}

$data .= "</table>";
// output the HTML content
$pdf->writeHTML($data, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();
//Close and output PDF document
$pdf->Output("ITEMLISTVIEW.pdf", 'D');
