<?php

if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
if (!defined('APP_PATH_ROOT')) define('APP_PATH_ROOT', realpath(dirname(__FILE__) . '/../'));
if (!defined('APP_PATH_WWW')) define('APP_PATH_WWW', realpath(dirname(__FILE__) . '/../'));
if (!defined('APP_PATH_CONFIG')) define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');

function IN7_DZIENNIK_KORESP() {
	global $thiss;

	// init columns if not set
	if (empty($_SESSION['IN7_DZIENNIK_KORESP_COLUMN']['ID'])) {
		SEF('USERS_COLUMN_INIT2');
		USERS_COLUMN_INIT2('IN7_DZIENNIK_KORESP', '', 'N');
	}

	require_once APP_PATH_ROOT . '/' . 'se-lib' . '/' . 'Lib.php';
	Lib::loadClass('V');
	Lib::loadClass('DB');

	$params = array();
	$params['toolbar'] = array();
	$params['toolbar'] []= '<a href="' . "?MENU_INIT=IN7_DZIENNIK_KORESP" . "&ARG1=OUTBOX" . '" title="' . "Ksiazka nadawcza" . '">' . "Ksiazka nadawcza" . '</a>';

	// router
	global $ARG1, $ARG1_VAL;
	switch ($ARG1) {
		case 'OUTBOX':
			IN7_DZIENNIK_KORESP_OUTBOX();
			break;
		default:
			SEF('WIEVTABLE');
			WIEVTABLE($params);
	}
}


class Koresp_Outbox_View {

	function css() {
		echo '<style type="text/css">' . "
.tbl-view th,
.tbl-view td {padding:0 1px;font-size:11px;border-color:#000;}
.tbl-view-koresp {page-break-after:always;}
		" . '</style>';
	}

	function page($koresp) {
		echo '<p style="text-align:right">' . "Załącznik nr ........................" . '</p>';
		echo '<p style="text-align:left">' . "Imię i nazwisko (nazwa) oraz adres nadawcy ..........................................................................." . '</p>';
		echo '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view tbl-view-koresp">';
		echo '<thead>';
		echo '<tr>';
			echo '<th rowspan="2">' . "Lp." . '</th>';
			echo '<th rowspan="2">' . "A D R E S A T<br />(imię i nazwisko lub nazwa)" . '</th>';
			echo '<th rowspan="2">' . "Dokładne miejsce doręczenia" . '</th>';
			echo '<th colspan="2">' . "Wartość<br />Kwota" . '</th>';
			echo '<th colspan="2">' . "Masa" . '</th>';
			echo '<th rowspan="2">' . "Nr nadawczy" . '</th>';
			echo '<th rowspan="2">' . "Uwagi" . '</th>';
			echo '<th colspan="2">' . "Opłata" . '</th>';
			echo '<th colspan="2">' . "Kwota<br />pobraniowa" . '</th>';
			echo '<th colspan="2">' . "Opłata za<br />pobranie" . '</th>';
		echo '</tr>';
		echo '<tr>';
			echo '<th>' . "zł" . '</th>';
			echo '<th>' . "gr" . '</th>';
			echo '<th>' . "kg" . '</th>';
			echo '<th>' . "g" . '</th>';
			echo '<th>' . "zł" . '</th>';
			echo '<th>' . "gr" . '</th>';
			echo '<th>' . "zł" . '</th>';
			echo '<th>' . "gr" . '</th>';
			echo '<th>' . "zł" . '</th>';
			echo '<th>' . "gr" . '</th>';
		echo '</tr>';
		echo '<tr>';
			echo '<th>' . "1" . '</th>';
			echo '<th>' . "2" . '</th>';
			echo '<th>' . "3" . '</th>';
			echo '<th colspan="2">' . "4" . '</th>';
			echo '<th colspan="2">' . "5" . '</th>';
			echo '<th>' . "6" . '</th>';
			echo '<th>' . "7" . '</th>';
			echo '<th colspan="2">' . "8" . '</th>';
			echo '<th colspan="2">' . "9" . '</th>';
			echo '<th colspan="2">' . "10" . '</th>';
		echo '</tr>';
		echo '<tr>';
			echo '<th colspan="3" style="padding:0 20px;text-align:right;">' . "Z przeniesienia" . '</th>';
			echo '<th>' . "" . '</th>';
			echo '<th>' . "" . '</th>';
			echo '<th colspan="3" style="text-align:right;">' . "Z przeniesienia" . '</th>';
			echo '<th>' . "" . '</th>';
			echo '<th>' . "" . '</th>';
			echo '<th>' . "" . '</th>';
			echo '<th>' . "" . '</th>';
			echo '<th>' . "" . '</th>';
			echo '<th>' . "" . '</th>';
			echo '<th>' . "" . '</th>';
		echo '</tr>';
		echo '</thead>';
		echo '<tfoot>';
		echo '<tr>';
			echo '<td colspan="3" style="border:none;padding:0 20px 0 0;text-align:right;">' . "Do przeniesienia" . '</td>';
			echo '<td>' . "" . '</td>';
			echo '<td>' . "" . '</td>';
			echo '<td colspan="4" style="border:none">' . "" . '</td>';
			echo '<td>' . "" . '</td>';
			echo '<td>' . "" . '</td>';
			echo '<td colspan="2" style="border:none">' . "" . '</td>';
			echo '<td>' . "" . '</td>';
			echo '<td>' . "" . '</td>';
		echo '</tr>';
		echo '</tfoot>';
		echo '<tbody>';
		foreach ($koresp as $r) {
			echo '<tr>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . $r->K_OD_KOGO . '</td>';
				echo '<td>' . $r->OD_KOGO_ADRES . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . $r->ID . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . "" . '</td>';
				echo '<td>' . "" . '</td>';
			echo '</tr>';
		}
		echo '</tbody>';
		echo '</table>';
	}
	
}


function IN7_DZIENNIK_KORESP_OUTBOX() {
	$page_limit = 20;

	$db = DB::getDB();

	$koresp = array();
	$sql = "select
			`ID`
			, `K_OD_KOGO`
			, `OD_KOGO_ADRES`
		from `IN7_DZIENNIK_KORESP` as t
		where t.`A_STATUS`='NORMAL'
			and t.`K_TYP_KORESP`='OUT'
			and t.`K_TYP_RODZAJ` in('POLECONY', 'POLECONY PRIORYTET')
	";
	$res = $db->query($sql);
	while ($r = $db->fetch($res)) {
		$koresp []= $r;
	}

	if (empty($koresp)) {
		echo '<p>' . "Brak danych." . '</p>';
		return;
	}

	// split table by $page_limit
	$koresp_pages = array();
	$page_nr = 0;
	$page_cur_nr = 0;
	foreach ($koresp as $r) {
		$koresp_pages[$page_nr] []= $r;

		$page_cur_nr++;
		if ($page_cur_nr >= $page_limit) {
			$page_nr++;
			$page_cur_nr = 0;
		}
	}

	Koresp_Outbox_View::css();

	foreach ($koresp_pages as $koresp_page) {
		Koresp_Outbox_View::page($koresp_page);
	}

}
