<?php
include_once ("include/MPDF57/mpdf.php");
global $app_list_strings;
$p2=new mPDF('','A4', null, 'helvetica', 10,10,10,10,0,0);
$i=0;
$j=0;
$db = $GLOBALS ['db'];
$data = array ();
//var_dump($_POST);
foreach ($_POST as $klucz => $wartosc){
	$pos = strpos($klucz, 'box_');
	if($pos !== false){
		if($j%4==0){
			$p2->AddPage();
		}
		$j++;
		$tytul = "";
		$opis = "";
		$query = "SELECT
			d.document_date as docdate,
			d.document_name as docname,
			DATE_FORMAT(DATE(d.date_entered), '%d-%m-%Y') as docdateentered,
			d.category_id as doccategory,
			d.description as docdes,
			ifnull(d.value,0) as docwartosc,
			u.first_name as username,
			u.last_name as userlastname
		FROM
			documents d 
			left join users u on u.id = d.created_by
			left join accounts a on d.parent_id = a.id
		WHERE
			d.id = '".$wartosc. "' AND d.deleted = 0";
			
		/**
		* ************* GET DATA FROM DB********************
		*/
		$rows = $db->query ( $query );
		while ( $r = $db->fetchByAssoc ( $rows ) ) {			
			if($r['docname']!=NULL)
				$tytul = $tytul. "Nazwa dokumentu: ".$r['docname']."<br>";
			if($r['docdateentered']!=NULL)
				$tytul = $tytul."Data wprowadzenia: ".$r['docdateentered']."<br>";
			if($r['docdate']!=NULL)
				$tytul = $tytul."Data dokumentu: ".$r['docdate']."<br>";
			if($r ['doccategory']!=NULL)
				$tytul = $tytul."Kategoria: ".$app_list_strings['document_category_dom'][$r ['doccategory']]."<br>";
			if($r['docwartosc']!=NULL)
				$tytul = $tytul."Wartość: ".number_format($r['docwartosc'], 2, ",", ".")."<br>";
			if($r['username']!=NULL && $r['userlastname']!=NULL)
				$tytul = $tytul."Wprowadził: ".$r['username']." ".$r['userlastname']."<br>";
			if($r['docdes']!=NULL)
				$tytul = $tytul."Opis dokumentu: ".$r['docdes']."<br>";
			
			// Wczytywanie raportów
			$queryraport = "
				SELECT
					CONCAT(u.first_name, ' ', u.last_name) AS name,
					d.accepted AS accepted,
					d.accepted_description AS accepteddes
				FROM 
					documents_user d
					left join users u on u.id = d.user_id
				WHERE 
					d.document_id = '".$wartosc."'";
			$tmp = $db->query ( $queryraport );
			if($tmp->num_rows == 0){
				$opisy = "";
			}else{
				$opisy = "<table style='border: 1px solid black; padding: 3px;'>
							<tr>
								<th style='border: 1px solid black; padding: 3px;'>Użytkownik</th>
								<th style='border: 1px solid black; padding: 3px;'>Status</th>
								<th style='border: 1px solid black; padding: 3px;'>Opis</th>
							</tr>";
				while($tmp2 = $db->fetchByAssoc($tmp)){
					$opisy = $opisy . "<tr>";
					$opisy = $opisy ."<td style='border: 1px solid black; padding: 3px;'>".$tmp2['name']."</td>";
					if($tmp2['accepted']==0){
						$opisy = $opisy ."<td style='border: 1px solid black; padding: 3px;'>Oczekuje</td>";
					}elseif($tmp2['accepted']==1){
						$opisy = $opisy ."<td style='border: 1px solid black; padding: 3px;'>Zaakceptowano</td>";
					}elseif($tmp2['accepted']==2){
						$opisy = $opisy ."<td style='border: 1px solid black; padding: 3px;'>Odrzucono</td>";
					}
					$opisy = $opisy ."<td style='border: 1px solid black; padding: 3px;'>".str_replace("XVZC","<br>",$tmp2['accepteddes'])."</td>";
					$opisy = $opisy ."</tr>";
				};
				$opisy = $opisy . "</table>";
			}
		}
		$p2->WriteHTML($tytul . $opisy . "<hr>");
	}
	$i++;
}
$p2->Output ();	
?>