<?php 
if (!$_REQUEST['settings'] || $_REQUEST['settings']=="")
	die('Błąd parametrów');

	$tmp = base64_decode($_REQUEST['settings']);;
	$settings = objectToArray(json_decode($tmp));
	$module = $settings['module'];
	$fields = $settings['fields'];
	$table = $settings['table'];
	$where = base64_decode($settings['where']);

$where = str_replace("like '", "like '%", $where);
$where = str_replace("(ecmstockstates.empty = '0' OR ecmstockstates.empty IS NULL)", "", $where);
$where = str_replace("AND ( ecmstockstates.empty = '0' OR ecmstockstates.empty IS NULL)", "", $where);
	//vd($settings, true);
	if($where!=''){
		$where.=' and '.strtolower($module).'.deleted=0 ';
	} else {
		$where.=' '.strtolower($module).'.deleted=0 ';
	}
	if( $_SESSION[$module.'_order']!=''){
		$where.=' order by '.$_SESSION[$module.'_order'];
	}
	$l = return_module_language('pl_pl', $module);
	global $app_strings;
	$l = array_merge($l, $app_strings);
	$db=$GLOBALS['db'];
   $query="select * from ".$table." where ".$where.""; 

	function vd($v, $die = false) {
		echo '<pre>'; var_dump($v); echo '</pre>';
		if ($die == true) die();
	}
	function objectToArray($d) {
		if (is_object($d)) {
			// Gets the properties of the given object
			// with get_object_vars function
			$d = get_object_vars($d);
		}
	
		if (is_array($d)) {
			/*
			 * Return array converted to object
			 * Using __FUNCTION__ (Magic constant)
			 * for recursive call
			 */
			return array_map(__FUNCTION__, $d);
		}
		else {
			// Return array
			return $d;
		}
	}

	global $app_list_strings;
$res=$db->query($query);
$data=array();
while($dane=$db->fetchByAssoc($res)){
	$p = new EcmProduct();
	$p->retrieve($dane['product_id']);
	$dane['jm']=strtoupper($app_list_strings['ecmproducts_unit_dom'][$p->unit_id]);
	$data[]=$dane;
}
$si= new EcmSysInfo();

$smarty = new Sugar_Smarty ();
$smarty->assign("data",$data);

$content= $smarty->fetch("modules/Home/tpl/StanyPDF.tpl");
include_once ("include/MPDF57/mpdf.php");
	
$mPDF = new mPDF ( 'pl', 'A4', 9, 'arial',  10, 10,35, 20, 14, 20);

$mPDF->setAutoTopMargi = 'stretch';
$mPDF->setAutoBottomMargin = 'stretch';

$mPDF->SetHTMLHeader('<div style="text-align: left; border-bottom: 1px solid #000000; font-weight: bold; font-size: 10pt;">
	<table>
		<tr>
			<td style="font-size: 20px; font-weight: bold">ARKUSZE SPISU Z NATURY</td>
		</tr>
		<tr>
			<td style="font-size: 12px; font-weight: bold">'.$si->getName().'</td>
		</tr>
		<tr>
			<td style="font-size: 12px; font-weight: bold">ARKUSZ NR: {PAGENO}</td>
		</tr>
	</table>
</div>

		');
$mPDF->SetHTMLFooter("<p >
podpisy członków komisji inwentaryzacyjnej
</p> ");
$mPDF->WriteHTML($content);
$mPDF->Output('inwentaryzacja.pdf', "I");
?>