<?php

function PRZYPOMNIJ_FUNC() {

// TODO: id zasobu IN7_DZIENNIK_KORESP
// https://biuro.biall-net.pl/SE/se-dev-pl/index.php?MENU_INIT=IN7_DZIENNIK_KORESP_EDIT&ARG1=EDIT&ARG1_VAL=3530
// https://biuro.biall-net.pl/SE/se-dev-pl/index.php?MENU_INIT=IN7_MK_BAZA_DYSTRYBUCJI_EDIT&ARG1=EDIT&ARG1_VAL=375
// OK https://biuro.biall-net.pl/SE/se-dev-pl/procesy5.php?task=CRM_PROCES&filtr_search_id=&filtr_id=2783
// 		index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobProjID}#EDIT/{$ID}
// 		index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobKorespID}#EDIT/{$ID}
Lib::loadClass('ProcesHelper');
$zasobProjID = ProcesHelper::getZasobTableID('IN7_MK_BAZA_DYSTRYBUCJI');
$zasobKorespID = ProcesHelper::getZasobTableID('IN7_DZIENNIK_KORESP');

SEF('MENU');
MENU();

$db = DB::getDB();

//written by A.Binder at 2008-09-16 for sync purposes
//@2012-05-03 mods for deep searching of duties P_ID 

$DEEP_SQL_RECURSE = 10; //jak głęboko wnikać w strukturę spraw PID/ID

echo'<style type="text/css">'."
body{font-family:arial;}
.box{border:1px solid #999;padding:5px;margin:5px;}
.box-center{border:1px solid #999;padding:5px;margin:5px auto;}
.box-silver{border-color:#999;}
.box-red{border-color:#f00;}
.box-blue{border-color:#00f;}
.box-green{border-color:#008000;}
.box-hover-active{background:#fff;border-color:#999;}
.box-hover-active:hover{background:#eee;border-color:#000;}

.btn-box{margin:0 2px 0 0;padding:0 3px;background:#bbb;color:#fff;text-decoration:none;border:0;font-weight:bold;}

.tbl-view {margin:0;font-size:small;}
.tbl-view tbody tr.row-0 {background:#f3f3f3;}
.tbl-view tbody tr.row-1 {background:#fff;}
.tbl-view caption{text-align:left;font-style:italic;font-size:medium;}
.tbl-view caption em{padding:0 5px;font-size:13px;font-style:normal;}
.tbl-view th{padding:0 3px;font-size:small;}
.tbl-view td{padding:0 3px;}
.tbl-view .r{text-align:right;}
.tbl-view thead th {background:#F3F3F3;}
.tbl-view thead td {background:#F3F3F3;}

.tbl-przypomnij {margin:5px 0;}
.use-filtr_only_stare tr.l-app-stare{display:none;}
.use-filtr_hide_PROJEKT tr.type-PROJEKT {display:none;}
.use-filtr_hide_POCZTA tr.type-POCZTA {display:none;}
.use-filtr_hide_PROCES tr.type-PROCES {display:none;}

.nobr {white-space:nowrap;}
".'</style>';

echo '<script type="text/javascript">' . "
jQuery(document).ready(function(){
	jQuery('#filtr_only_stare').click(function(e){
		var n=jQuery(this);
		var tbl=jQuery('table.tbl-przypomnij');
		if (n.is(':checked')) {
			tbl.addClass('use-filtr_only_stare');
		} else {
			tbl.removeClass('use-filtr_only_stare');
		}
	});

	jQuery('#filtr_hide_PROJEKT').click(function(e){
		var n=jQuery(this);
		var tbl=jQuery('table.tbl-przypomnij');
		if (n.is(':checked')) {
			tbl.addClass('use-filtr_hide_PROJEKT');
		} else {
			tbl.removeClass('use-filtr_hide_PROJEKT');
		}
	});
	jQuery('#filtr_hide_POCZTA').click(function(e){
		var n=jQuery(this);
		var tbl=jQuery('table.tbl-przypomnij');
		if (n.is(':checked')) {
			tbl.addClass('use-filtr_hide_POCZTA');
		} else {
			tbl.removeClass('use-filtr_hide_POCZTA');
		}
	});
	jQuery('#filtr_hide_PROCES').click(function(e){
		var n=jQuery(this);
		var tbl=jQuery('table.tbl-przypomnij');
		if (n.is(':checked')) {
			tbl.addClass('use-filtr_hide_PROCES');
		} else {
			tbl.removeClass('use-filtr_hide_PROCES');
		}
	});
});
" . '</script>';

//$_SERVER['argv'][1] = addslashes($_SERVER['argv'][1]);// ???

//aby było uniwersalnie do wersji KIKE
if(file_exists("../../.config")) include("../../.config");
if(file_exists("../.config")) include("../.config");
if(file_exists("../.config.php")) include("../.config.php");
if(file_exists(".config.php")) include(".config.php");



//if ($_GET['KTO']) $_SERVER['argv'][1] = $_GET['KTO'];// ???


$sql_allowed_users = array();
$zap_sql = "SELECT DISTINCT  `L_APPOITMENT_USER` FROM  `IN7_MK_BAZA_DYSTRYBUCJI` WHERE  `L_APPOITMENT_USER` !=  '' ; ";
// TODO: $zap_sql = "SELECT DISTINCT  `L_APPOITMENT_USER` FROM  `IN7_MK_BAZA_DYSTRYBUCJI` WHERE  `L_APPOITMENT_USER` !=  '' and (  A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or A_CLASSIFIED=''   ) ";
$result=$db->query($zap_sql) or die("blad zapytania do bazy ".$zap_sql);
while ($r = $db->fetch($result)) {
	$sql_allowed_users[ $r->L_APPOITMENT_USER ] = true;
}
$zap_sql = "SELECT DISTINCT  `L_APPOITMENT_USER` FROM  `IN7_DZIENNIK_KORESP` WHERE  `L_APPOITMENT_USER` !=  '' ; ";
//TODO: $zap_sql = "SELECT DISTINCT  `L_APPOITMENT_USER` FROM  `IN7_DZIENNIK_KORESP` WHERE  `L_APPOITMENT_USER` !=  '' and (  A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or A_CLASSIFIED=''   )  ; ";
$result=$db->query($zap_sql) or die("blad zapytania do bazy ".$zap_sql);
while ($r = $db->fetch($result)) {
	$sql_allowed_users[ $r->L_APPOITMENT_USER ] = true;
}

$zap_sql = "SELECT DISTINCT  `L_APPOITMENT_USER` FROM  `CRM_PROCES` WHERE  `L_APPOITMENT_USER` !=  '' ; ";
//TODO: $zap_sql = "SELECT DISTINCT  `L_APPOITMENT_USER` FROM  `IN7_DZIENNIK_KORESP` WHERE  `L_APPOITMENT_USER` !=  '' and (  A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or A_CLASSIFIED=''   )  ; ";
$result=$db->query($zap_sql) or die("blad zapytania do bazy ".$zap_sql);
while ($r = $db->fetch($result)) {
	$sql_allowed_users[ $r->L_APPOITMENT_USER ] = true;
}

$zap_sql = "SELECT DISTINCT  `L_APPOITMENT_USER` FROM  `CRM_LISTA_ZASOBOW` WHERE  `L_APPOITMENT_USER` !=  '' ; ";
//TODO: $zap_sql = "SELECT DISTINCT  `L_APPOITMENT_USER` FROM  `IN7_DZIENNIK_KORESP` WHERE  `L_APPOITMENT_USER` !=  '' and (  A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or A_CLASSIFIED=''   )  ; ";
$result=$db->query($zap_sql) or die("blad zapytania do bazy ".$zap_sql);
while ($r = $db->fetch($result)) {
	$sql_allowed_users[ $r->L_APPOITMENT_USER ] = true;
}


$selected_user = isset($_GET['KTO'])? $_GET['KTO'] : '';//$_SERVER['argv'][1];
if (!empty($selected_user)) {
	if (!array_key_exists($selected_user, $sql_allowed_users)) {
		$selected_user = '';
		$selected_user_err = 'Brak danych - wybierz innego użytkownika';
	}
}

// TODO: error jeśli KTO nie ma na liście
?>
<div class="alert alert-info">
  <strong>Testowa wersja przypomnij</strong> - <a href="index.php?MENU_INIT=PRZYPOMNIJ&KTO=<?php echo $selected_user; ?>">uruchom</a>
</div>
<?php

echo'<form action="" method="get">';
	echo '<input type="hidden" name="MENU_INIT" value="PRZYPOMNIJ_FUNC" />';
	echo'<a href="'."index.php?MENU_INIT=PRZYPOMNIJ_FUNC".'" >'."Wszyscy".'</a>';
	echo' | ';
	echo'<a href="'."index.php?MENU_INIT=PRZYPOMNIJ_FUNC&KTO=".$_SESSION['AUTHORIZE_USER'].'" >'."Twoje (".$_SESSION['AUTHORIZE_USER'].")".'</a>';
	echo' | ';
	if (!empty($sql_allowed_users)) {
		echo'<select name="'."KTO".'">';
		echo'<option value="">'." Wszyscy ".'</option>';
		foreach ($sql_allowed_users as $k => $v) {
			$sel = ($k == $selected_user)? ' selected="selected"' : '';
			echo'<option value="'.$k.'"'.$sel.'>'.$k.'</option>';
		}//end foreach
		echo'</select>';
	}
	echo'<input type="submit" value="'."Pokaż".'" />';
	echo '<span style="padding:0 0 0 30px;">';
		$chkd = (isset($_GET['filtr_only_stare']) && $_GET['filtr_only_stare'] == 1)? 'checked="checked"' : '';
		echo '<input type="checkbox" name="filtr_only_stare" value="1" '.$chkd.' id="filtr_only_stare" />';
		echo '<label title="' . "L_APPOITMENT_DATE w przeszłości" . '">' . " Tylko przedawnione" . '</label>';
	echo '</span>';

	echo '<span style="padding:0 0 0 30px;">';
		echo '<label>' . "Ukryj: " . '</label>';
		$chkd = (isset($_GET['filtr_only_stare']) && $_GET['filtr_only_stare'] == 1)? 'checked="checked"' : '';
		echo '<input type="checkbox" name="filtr_hide_PROJEKT" value="1" '.$chkd.' id="filtr_hide_PROJEKT" />';
		echo "PROJEKT";
		echo '<input type="checkbox" name="filtr_hide_POCZTA" value="1" '.$chkd.' id="filtr_hide_POCZTA" />';
		echo "POCZTA";
		echo '<input type="checkbox" name="filtr_hide_PROCES" value="1" '.$chkd.' id="filtr_hide_PROCES" />';
		echo "PROCESY";
	echo '</span>';
echo'</form>';


if ($selected_user_err) {
	echo '<div class="alert alert-error">' . $selected_user_err . '</div>';
	return;
}

$sql_extra = "";
if ($selected_user) {
	$sql_extra = " and ( kor.`L_APPOITMENT_USER`='".$selected_user."' 
	-- or proj.`L_APPOITMENT_USER`='".$selected_user."' 
	) ";
}
$zap_sql = "select kor.*
	, 'POCZTA' as CO
--	, 'POCZTA_0' as _test
--	, kor.`L_APPOITMENT_USER` as _test_kor_app
--	, proj.`L_APPOITMENT_USER` as _test_proj_app
	, IF(''='".$selected_user."', '',
			IF(kor.`L_APPOITMENT_USER`='".$selected_user."', kor.`ID`, proj.`ID`)
		) as _test_id
	, IF(''='".$selected_user."', '',
			IF(kor.`L_APPOITMENT_USER`='".$selected_user."', 'kor.', 'proj.')
		) as _test_type
	, unix_timestamp(now()) as NOW
	, unix_timestamp(kor.A_RECORD_UPDATE_DATE) as A_RECORD_UPDATE_DATE_TS
	, proj.`ID` as PROJ_ID
	, proj.`P_ID`  as P_P_ID
	, proj.`M_DIST_DESC` as P_M_DIST_DESC
	, proj.`M_DISTRIBUTOR` as P_M_DISTRIBUTOR
	, kor.`K_LOKALIZACJA`
	, kor.`K_LOKALIZACJA_NEW`
	, kor.`T_TELBOX_NEIGHBOUR_IN_ID`
	, kor.`T_TELBOX_NEIGHBOUR_IN_ID_NEW`

from `IN7_DZIENNIK_KORESP` as kor
	left join `IN7_MK_BAZA_DYSTRYBUCJI` as proj on (proj.`ID`=kor.`ID_PROJECT`)
where (
		kor.`A_STATUS`='WAITING'
		or ( kor.`K_LOKALIZACJA`!=kor.`K_LOKALIZACJA_NEW` and kor.`K_LOKALIZACJA_NEW`!='' )
		or ( kor.`T_TELBOX_NEIGHBOUR_IN_ID`!=kor.`T_TELBOX_NEIGHBOUR_IN_ID_NEW` and kor.`T_TELBOX_NEIGHBOUR_IN_ID_NEW`>0 )
		or ( kor.`ID_ZASOB`!=kor.`ID_ZASOB_NEW` and kor.`ID_ZASOB_NEW`!='' )

		or ( kor.`A_STATUS`!='OFF_HARD' )
	)
	and kor.`A_STATUS`!='DELETED'
	and  proj.`A_ADM_COMPANY` not like '%HIDDEN%'
--	and (  kor.A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or kor.A_CLASSIFIED=''   )

	".$sql_extra."

union

select kor.*
	, 'POCZTA' as CO
--	, 'POCZTA_1' as _test
--	, kor.`L_APPOITMENT_USER` as _test_kor_app
--	, proj_1.`L_APPOITMENT_USER` as _test_proj_app
	, IF(''='".$selected_user."', '',
			IF(kor.`L_APPOITMENT_USER`='".$selected_user."', kor.`ID`, proj_1.`ID`)
		) as _test_id
	, IF(''='".$selected_user."', '',
			IF(kor.`L_APPOITMENT_USER`='".$selected_user."', 'kor.', 'proj.')
		) as _test_type
	, unix_timestamp(now()) as NOW
	, unix_timestamp(kor.`A_RECORD_UPDATE_DATE`) as A_RECORD_UPDATE_DATE_TS
	, proj.`ID` as PROJ_ID
	, proj.`P_ID`  as P_P_ID
	, proj.`M_DIST_DESC` as P_M_DIST_DESC
	, proj.`M_DISTRIBUTOR` as P_M_DISTRIBUTOR
	, kor.`K_LOKALIZACJA`
	, kor.`K_LOKALIZACJA_NEW`
	, kor.`T_TELBOX_NEIGHBOUR_IN_ID`
	, kor.`T_TELBOX_NEIGHBOUR_IN_ID_NEW`
from `IN7_DZIENNIK_KORESP` as kor
	left join `IN7_MK_BAZA_DYSTRYBUCJI` as proj on (proj.`ID`=kor.`ID_PROJECT`)
	left join `IN7_MK_BAZA_DYSTRYBUCJI` as proj_1 on (proj_1.`ID`=proj.`P_ID`)
where (
		kor.`A_STATUS`='WAITING'
		or ( kor.`K_LOKALIZACJA`!=kor.`K_LOKALIZACJA_NEW` and kor.`K_LOKALIZACJA_NEW`!='' )
		or ( kor.`T_TELBOX_NEIGHBOUR_IN_ID`!=kor.`T_TELBOX_NEIGHBOUR_IN_ID_NEW` and kor.`T_TELBOX_NEIGHBOUR_IN_ID_NEW`>0 )

		or ( kor.`A_STATUS`!='OFF_HARD' )
	)
	and kor.A_STATUS!='DELETED'
	and proj.`A_CLASSIFIED` not like '%HIDDEN%'
	and proj_1.`A_CLASSIFIED` not like '%HIDDEN%'
	and kor.A_CLASSIFIED not like '%HIDDEN%'
--	and (kor.A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or kor.A_CLASSIFIED='')

";

if ($selected_user) {
	$zap_sql .= "\n	and ( kor.`L_APPOITMENT_USER`='".$selected_user."' or proj_1.`L_APPOITMENT_USER`='".$selected_user."' ) ";
}

$zap_sql_where = "";
if ($selected_user) {
	$zap_sql_where = "\n	and proj.`L_APPOITMENT_USER`='' ";
}
for ($CD = 2; $CD <= $DEEP_SQL_RECURSE; $CD++) {
	if ($selected_user) {
		$zap_sql_where .= "
			and  proj_".($CD-1).".`L_APPOITMENT_USER`=''
		";
	}
	$zap_sql_join .= "
		left join `IN7_MK_BAZA_DYSTRYBUCJI` as proj_".$CD." on (proj_".$CD.".`ID`=proj_".($CD-1).".`P_ID`)
	";

	$zap_sql .= "
union
		
select kor.*
		, 'POCZTA' as CO
--		, 'POCZTA_".$CD."' as _test
--	, kor.`L_APPOITMENT_USER` as _test_kor_app
--	, proj_".$CD.".`L_APPOITMENT_USER` as _test_proj_app
	, IF(''='".$selected_user."', '',
			IF(kor.`L_APPOITMENT_USER`='".$selected_user."', kor.`ID`, proj_".$CD.".`ID`)
		) as _test_id
	, IF(''='".$selected_user."', '',
			IF(kor.`L_APPOITMENT_USER`='".$selected_user."', 'kor.', 'proj.')
		) as _test_type
		, unix_timestamp(now()) as NOW
		, unix_timestamp(kor.`A_RECORD_UPDATE_DATE`) as A_RECORD_UPDATE_DATE_TS
		, proj.`ID` as PROJ_ID
		, proj.`P_ID` as P_P_ID
		, proj.`M_DIST_DESC` as P_M_DIST_DESC
		, proj.`M_DISTRIBUTOR` as P_M_DISTRIBUTOR
		, kor.`K_LOKALIZACJA`
	, kor.`K_LOKALIZACJA_NEW`
	, kor.`T_TELBOX_NEIGHBOUR_IN_ID`
	, kor.`T_TELBOX_NEIGHBOUR_IN_ID_NEW`

	from `IN7_DZIENNIK_KORESP` as kor
		left join `IN7_MK_BAZA_DYSTRYBUCJI` as proj on (proj.`ID`=kor.`ID_PROJECT`)
		left join `IN7_MK_BAZA_DYSTRYBUCJI` as proj_1 on (proj_1.`ID`=proj.`P_ID`)
		".$zap_sql_join."
	where (
			kor.`A_STATUS`='WAITING'
			or ( kor.`K_LOKALIZACJA`!=kor.`K_LOKALIZACJA_NEW` and kor.`K_LOKALIZACJA_NEW`!='' )
			or ( kor.`T_TELBOX_NEIGHBOUR_IN_ID`!=kor.`T_TELBOX_NEIGHBOUR_IN_ID_NEW` and kor.`T_TELBOX_NEIGHBOUR_IN_ID_NEW`>0 )
			or ( kor.`A_STATUS`!='OFF_HARD' )
		)
		and kor.`A_STATUS`!='DELETED'
		and proj.`A_CLASSIFIED` not like '%HIDDEN%'
		and proj_1.`A_CLASSIFIED` not like '%HIDDEN%'
		and kor.A_CLASSIFIED not like '%HIDDEN%'
--	and (  kor.A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or kor.A_CLASSIFIED=''   )

	";
	if ($selected_user) {
		$zap_sql .= $zap_sql_where . "\n" . "and ( kor.`L_APPOITMENT_USER`='".$selected_user."' or proj_".$CD.".`L_APPOITMENT_USER`='".$selected_user."' ) ";
	}
}

if(isset($_GET['DBG']) && $_GET['DBG']==1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">zap_sql: '."\n";print_r($zap_sql);echo'</pre>';}

$result = $db->query($zap_sql) or die("blad zapytania do bazy ".$zap_sql);

$t = 0;
echo '<table border="1" cellspacing="0" cellpadding="0" width="800" class="tbl-view tbl-przypomnij">';
echo'<thead>';
echo '<tr>';
	echo '<th style="width:5%;">'."CO".'</th>';
	echo '<th>'."Numer Dziennika Korespondencji".'</th>';
	echo '<th>'."Data Dziennika".'</th>';
	echo '<th>'."Typ".'</th>';
	echo '<th>'."Zadanie do wykonania".'</th>';
	echo '<th>'."Dane Administracyjne Rekordu Dziennika Korespondencji".'</th>';
	echo '<th>'."Od kogo/temat/sprawa".'</th>';
	echo '<th>'."Osoba Prowadz.".'</th>';
	echo '<th>'."Numer Referencyjny".'</th>';
	echo '<th>'."Zadekretowany Numer Sprawy".'</th>';
	echo '<th>'."Lokalizacja".'</th>';
echo '</tr>';
echo'</thead>'."\n";
echo'<tbody>'."\n";
while ($h = $db->fetch_array($result)) {
	$ILOSC_PISM++;
	$JOB = "";
	if (($h['K_LOKALIZACJA']!=$h['K_LOKALIZACJA_NEW'])&&($h['K_LOKALIZACJA_NEW']!='')) {
		$JOB .= "<font color=red> PRZEMIEŚĆ / WYŚLIJ DOKUMENT DO MIEJSCA</font> :<br> ".$h['K_LOKALIZACJA_NEW']."(".$h['K_LOKALIZACJA_OPIS_NEW'].") ";
	}
	
	if (($h['T_TELBOX_NEIGHBOUR_IN_ID']!=$h['T_TELBOX_NEIGHBOUR_IN_ID_NEW'])&&($h['T_TELBOX_NEIGHBOUR_IN_ID_NEW']>0)) {
		$JOB .= "<font color=red> PRZEMIEŚĆ / WYŚLIJ DOKUMENT DO MIEJSCA</font> :<br> ".$h['T_TELBOX_NEIGHBOUR_IN_ID_NEW']."(".$h['T_TELBOX_NEIGHBOUR_IN_ID_NEW'].") ";
	}
	
	if (($h['NOW']>$h['A_RECORD_UPDATE_DATE_TS']+864000)&&($h['A_STATUS']!='OFF_HARD')) {
		$JOB .= "<font color=red> SPRAWA PRZEDAWNIONA PONAD 10 DNI!!! </font> ";
	}
	if (($h['ID_PROJECT']=='0')) {
		$JOB .= "<font color=red> REKORD WYMAGA DEKRETACJI NA SPRAWE </font> ";
	}
	if (($h['A_STATUS']=='WAITING')) {
		$JOB .= "<font color=GREEN> SPRAWA WYMAGA ZAŁATWIENIA (WAITING) </font> ";
	}
	if (strlen($h['M_DIST_FILES'])>5) {
		$JOB .= "<font color=YELLOW> (JEST SKAN) </font> ";
	}
	$cls = 'row-'.($t = 1 - $t);
	$cls .= ' type-' . "POCZTA";
	if ($h['L_APPOITMENT_DATE'] > date('Y-m-d')) $cls .= ' l-app-stare';// . ' l-app-date-' . $h['L_APPOITMENT_DATE'];
	echo '<tr class="' . $cls . '">';
		echo '<td>';
			echo $h['CO'];
			//echo'<br />('.$h['_test'].')';
			//echo'<br />[K.'.$h['ID'].'/P.'.$h['PROJ_ID'].']';
			//echo'<br />{K.'.$h['_test_kor_app'].'}';
			//echo'<br />{P.'.$h['_test_proj_app'].'}';
			if ($selected_user) {
				echo'<br /><em style="color:#999;" title="'.$selected_user." przypisany/a do ".$h['_test_type']." ".$h['_test_id'].'">('.$h['_test_id'].')</em>';
			//} else {
			//	echo '<br />';
			//	if ($h['_test_kor_app']) {
			//		echo '<em style="color:#999;" title="">(kor. ' . $h['_test_kor_app'] . ')</em> ';
			//	}
			//	if ($h['_test_proj_app']) {
			//		echo '<em style="color:#999;" title="">(proj. ' . $h['_test_proj_app'] . ')</em> ';
			//	}
			}
		echo '</td>';
		echo '<td>';
			echo "L.Dz.(K_ID): ";
			echo '<a href="'."index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobKorespID}#EDIT/{$h['ID']}".'">'.$h['ID'].'</a>';
			echo ' <a href="'."index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobKorespID}#FILES/{$h['ID']}".'" target="_blank"><i title="Pliki" class="ico-folder-open"></i></a>';
		echo '</td>';
		echo '<td>'.$h['A_RECORD_CREATE_DATE'].'</td>';
		echo '<td>'.$h['K_TYP_KORESP']." -".$h['K_TYP_RODZAJ']." (".$h['K_TYP_RODZAJ_OPIS'].")".'</td>';
		echo '<td>'.$JOB.'</td>';
		echo '<td>'."Wpr: ".$h['A_RECORD_CREATE_AUTHOR']."/ akt : ".$h['A_RECORD_UPDATE_AUTHOR']."(".$h['A_RECORD_UPDATE_DATE'].")".'</td>';
		echo '<td>'.$h['K_OD_KOGO']."/".$h['OD_KOGO_ADRES']."/".$h['K_ZAWARTOS'].'</td>';
		echo '<td>'.$h['L_APPOITMENT_USER']." ".$h['L_APPOITMENT_INFO']." ".$h['L_APPOITMENT_INFO']."Termin:".$h['L_APPOITMENT_DATE'].'</td>';
		echo '<td>'.$h['K_ZNAK_REFERENTA'].'</td>';
		echo '<td>'.$h['ID_PROJECT']." <br><font color=\"#FF5555\">".$h['P_M_DIST_DESC']."</font> (<font color=red> <i>firma</i> <b> ".$h['P_M_DISTRIBUTOR']."</b>)</font> ".'</td>';
		echo '<td>'.$h['K_LOKALIZACJA']."(".$h['K_LOKALIZACJA_OPIS'].")".'</td>';
	echo '</tr>'."\n";
}// eof while sql

/**
 * zapytania:
 * 1. wszytkie projekty przypisane do $selected_user
 * 2. przypisane do nikogo, gdzie proj. nadrzedny (P_ID) jest przyipsany do $selected_user
 * 3. przypisane do nikogo, gdzie proj. nadrzedny do nikogo, a projekt nad nim do $selected_user
 * 4. ...
 * ...
 */
if ($selected_user) {// for recursive search of admins of project @2012-05-03
	$extra_proj[1] = " and t1.`L_APPOITMENT_USER`='".$selected_user."' ";
	$extra_proj[2] = " and t2.`L_APPOITMENT_USER`='".$selected_user."' ";
	$extra_proj[3] = " and t3.`L_APPOITMENT_USER`='".$selected_user."' ";
}
$zap_sql = "select t1.*
	-- , 'SPRAWA/PROJEKT' as CO
	, 'PROJEKT' as CO
	, IF(''='".$selected_user."', '',
			IF(t1.`L_APPOITMENT_USER`='".$selected_user."', t1.ID, t1.ID)
		) as _test_proj_id
	, unix_timestamp(now()) as NOW
	, unix_timestamp(t1.A_RECORD_UPDATE_DATE) as A_RECORD_UPDATE_DATE_TS
from IN7_MK_BAZA_DYSTRYBUCJI as t1
where (
		t1.A_STATUS='WAITING'	
--		or ( t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > (864000+unix_timestamp(t1.A_RECORD_UPDATE_DATE)) )
--		or ( t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > unix_timestamp(t1.L_APPOITMENT_DATE) and t1.L_APPOITMENT_DATE!='' )
		or (
			IF(t1.L_APPOITMENT_DATE!=''
				, t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > unix_timestamp(t1.L_APPOITMENT_DATE)
				, t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > (864000+unix_timestamp(t1.A_RECORD_UPDATE_DATE))
			)
		)
	)
	and t1.A_STATUS!='DELETED'
	and t1.`A_CLASSIFIED` not like '%HIDDEN%'
--	and (  t1.A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or t1.A_CLASSIFIED=''   )
	".$extra_proj[1]."

union 

select t1.*
	-- , 'SPRAWA/PROJEKT' as CO
	, 'PROJEKT' as CO
	, IF(''='".$selected_user."', '',
			IF(t1.`L_APPOITMENT_USER`='".$selected_user."', t1.ID, t2.ID)
		) as _test_proj_id
	, unix_timestamp(now()) as NOW
	, unix_timestamp(t1.A_RECORD_UPDATE_DATE) as A_RECORD_UPDATE_DATE_TS
from IN7_MK_BAZA_DYSTRYBUCJI as t2
	left join   IN7_MK_BAZA_DYSTRYBUCJI as t1 on ( t2.ID=t1.P_ID )
where (
		t1.A_STATUS='WAITING'
--		or ( t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > (864000+unix_timestamp(t1.A_RECORD_UPDATE_DATE)) )
--		or ( t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > unix_timestamp(t1.L_APPOITMENT_DATE) and t1.L_APPOITMENT_DATE!='' )
		or (
			IF(t1.L_APPOITMENT_DATE!=''
				, t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > unix_timestamp(t1.L_APPOITMENT_DATE)
				, t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > (864000+unix_timestamp(t1.A_RECORD_UPDATE_DATE))
			)
		)
	)
	and t1.A_STATUS!='DELETED'
	and t1.L_APPOITMENT_USER=''
	and t1.`A_CLASSIFIED` not like '%HIDDEN%'  and t2.`A_CLASSIFIED` not like '%HIDDEN%' 
--	and (  t1.A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or t1.A_CLASSIFIED=''   )
	".$extra_proj[2]."

union 

select t1.*
	-- , 'SPRAWA/PROJEKT' as CO
	, 'PROJEKT' as CO
	, IF(''='".$selected_user."', '',
			IF(t1.`L_APPOITMENT_USER`='".$selected_user."', t1.ID, t3.ID)
		) as _test_proj_id
	, unix_timestamp(now()) as NOW
	, unix_timestamp(t1.A_RECORD_UPDATE_DATE) as A_RECORD_UPDATE_DATE_TS
from IN7_MK_BAZA_DYSTRYBUCJI as t3
	left join   IN7_MK_BAZA_DYSTRYBUCJI as t2 on ( t3.ID=t2.P_ID   )
	left join   IN7_MK_BAZA_DYSTRYBUCJI as t1 on ( t2.ID=t1.P_ID   )
where ( 
		t1.A_STATUS='WAITING'
--		or ( t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > (86400+unix_timestamp(t1.A_RECORD_UPDATE_DATE)) )
--		or ( t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > unix_timestamp(t1.L_APPOITMENT_DATE) and t1.L_APPOITMENT_DATE!='' )
		or (
			IF(t1.L_APPOITMENT_DATE!=''
				, t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > unix_timestamp(t1.L_APPOITMENT_DATE)
				, t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > (864000+unix_timestamp(t1.A_RECORD_UPDATE_DATE))
			)
		)
	)
	and t1.A_STATUS!='DELETED'
	and t1.L_APPOITMENT_USER=''
	and t2.L_APPOITMENT_USER=''
	and t1.`A_CLASSIFIED` not like '%HIDDEN%'
	and t2.`A_CLASSIFIED` not like '%HIDDEN%'
--	and (  t1.A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or t1.A_CLASSIFIED=''   )
	".$extra_proj[3]."
";
$zap_sql_join = "";
$zap_sql_where = "";
if ($selected_user) {
	$CD = 2;
	$zap_sql_where .= "\n and  t".($CD-1).".L_APPOITMENT_USER='' ";
	$CD = 3;
	$zap_sql_where .= "\n and  t".($CD-1).".L_APPOITMENT_USER='' ";
}
for ($CD = 4; $CD <= $DEEP_SQL_RECURSE; $CD++) {
	if ($selected_user) {
		$zap_sql_where .= "\n and  t".($CD-1).".L_APPOITMENT_USER='' ";
	}
	$zap_sql_join = "
		left join  `IN7_MK_BAZA_DYSTRYBUCJI` as t".($CD-1)." on ( t".$CD.".ID=t".($CD-1).".P_ID   )
	".$zap_sql_join;
	$zap_sql .= "
union

select t1.*
	-- ,'SPRAWA/PROJEKT' as CO
	, 'PROJEKT' as CO
	, IF(''='".$selected_user."', '',
			IF(t1.`L_APPOITMENT_USER`='".$selected_user."', t1.ID, t".$CD.".ID)
		) as _test_proj_id
	, unix_timestamp(now()) as NOW
	, unix_timestamp(t1.A_RECORD_UPDATE_DATE) as A_RECORD_UPDATE_DATE_TS
from `IN7_MK_BAZA_DYSTRYBUCJI` as t".$CD."
	".$zap_sql_join."
	left join `IN7_MK_BAZA_DYSTRYBUCJI` as t2 on ( t3.ID=t2.P_ID   )
	left join `IN7_MK_BAZA_DYSTRYBUCJI` as t1 on ( t2.ID=t1.P_ID   )
	where (
		t1.`A_STATUS`='WAITING'
--		or ( t1.`A_STATUS`!='OFF_HARD' and unix_timestamp(now()) > (86400+unix_timestamp(t1.`A_RECORD_UPDATE_DATE`)) )
--		or  ( t1.`A_STATUS`!='OFF_HARD' and unix_timestamp(now()) > unix_timestamp(t1.`L_APPOITMENT_DATE`) and t1.`L_APPOITMENT_DATE`!=''  )
		or (
			IF(t1.L_APPOITMENT_DATE!=''
				, t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > unix_timestamp(t1.L_APPOITMENT_DATE)
				, t1.A_STATUS!='OFF_HARD' and unix_timestamp(now()) > (864000+unix_timestamp(t1.A_RECORD_UPDATE_DATE))
			)
		)
	)
	and t1.`A_STATUS`!='DELETED'
	and t1.`A_CLASSIFIED` not like '%HIDDEN%'
	and t2.`A_CLASSIFIED` not like '%HIDDEN%'
--	and (  t1.A_CLASSIFIED in  ('".implode( "','" , $_SESSION['AUTH_LDAP_CLIENT__LDAP_USERS'][$_SESSION['ADM_ACCOUNT']])."') or t1.A_CLASSIFIED=''   )
	";
	if ($selected_user) {
		$zap_sql .= $zap_sql_where . " and t".$CD.".`L_APPOITMENT_USER`='".$selected_user."' \n ";
	}
}
$zap_sql .= " group by t1.ID ";

if(isset($_GET['DBG']) && $_GET['DBG']==1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">zap_sql: '."\n";print_r($zap_sql);echo'</pre>';}

$result = $db->query($zap_sql) or die("blad zapytania do bazy ".$zap_sql);
while ($h = $db->fetch_array($result)) {
	$ILOSC_SPRAW++;
	echo '<tr class="row-'.($t = 1 - $t) . ' ' . "type-" . $h['CO'] . '">';
		echo '<td>';
			echo $h['CO']." ".'<span style="color:#705680;">'.$h['A_STATUS'].'</span>';
			if ($selected_user) {
				echo'<br /><em style="color:#999;" title="'.$selected_user." przypisany/a do projektu ".$h['_test_proj_id'].'">('.$h['_test_proj_id'].')</em>';
			}
		echo '</td>';
		echo '<td>';
			echo "Sprawa(ID): ".'<a href="'."index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobProjID}#EDIT/{$h['ID']}".'">'.$h['ID'].'</a>';
			echo ' <a href="'."index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobProjID}#FILES/{$h['ID']}".'" target="_blank"><i title="Pliki" class="ico-folder-open"></i></a>';
			echo "<br> Teczka Nadrzędna(PID): ".'<a href="'."index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobProjID}#EDIT/{$h['P_ID']}".'">'.$h['P_ID'].'</a>'."  ";
			echo ' <a href="'."index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobProjID}#FILES/{$h['P_ID']}".'" target="_blank"><i title="Pliki" class="ico-folder-open"></i></a>';
		echo '</td>';
		echo '<td>'."M DIST DATE ".$h['M_DIST_DATE'].'</td>';
		echo '<td>';
		if (strlen($h['M_DIST_TYPE'] > 20)) {
			echo $h['M_DIST_TYPE'];
		} else {
			echo str_replace('_',' ',$h['M_DIST_TYPE']);
		}
		echo '</td>';
		echo '<td>';
			echo $h['M_DIST_DESC']."  (<font color=red> <i>firma</i> <b> ".$h['M_DISTRIBUTOR']."</b>)</font> ";
			if (strstr($h['M_DIST_TYPE'], 'TV_')) {
				foreach ($h as $HRI => $HRV) {
					if (isset($h[$HRI])) {
						if (strstr($HRI, 'TV_')) {
							if (strlen($h[$HRI]) > 1) {
								echo " <font size=-2><b>".str_replace('_',' ',$HRI).":</b> ".$h[$HRI]."</font>";
							}
						}
					}
				}
			}
		echo '</td>';
		echo '<td>'."Wpr: ".$h['A_RECORD_CREATE_AUTHOR']."/ akt : ".$h['A_RECORD_UPDATE_AUTHOR']."(".$h['A_RECORD_UPDATE_DATE'].")".'</td>';
		echo '<td>'." <b>".$h['A_PROBLEM']."  ".$h['A_PROBLEM_DATE']."</b><font color=\"#444444\"><pre>  ".$h['A_PROBLEM_DESC']."</pre></font>".'</td>';
	echo'</tr>';
} //eof while sql
echo'</tbody>';
echo '</table>'."\n";

$ILOSC_PROCES = 0;
{
	$t = 0;
	echo '<table border="1" cellspacing="0" cellpadding="0" width="800" class="tbl-view tbl-przypomnij">';
	echo'<thead>';
	echo '<tr>';
		echo '<th style="width:5%;">'."CO".'</th>';
		echo '<th>'."Numer ID".'</th>';
		echo '<th>'."Data".'</th>';
		echo '<th>'."Typ".'</th>';
		echo '<th>'."Nazwa".'</th>';
		echo '<th>'."Dane Administracyjne Rekordu".'</th>';
		echo '<th>'."Opis".'</th>';
	echo '</tr>';
	echo'</thead>'."\n";
	echo'<tbody>'."\n";
	$sql = "select p.*
		from `CRM_PROCES` as p
		where p.`TYPE`='PROCES_INIT'
			and p.`A_STATUS` in('NORMAL', 'WAITING')
			and p.`L_APPOITMENT_DATE`!=''
			and p.`L_APPOITMENT_USER`!=''
			" . (($selected_user != '')? "and p.`L_APPOITMENT_USER`='" . $selected_user . "'" : "") . "
	";
	$result = $db->query($sql) or die("blad zapytania do bazy ".$sql);
	while ($h = $db->fetch_assoc($result)) {
		$ILOSC_PROCES++;
		$cls = '';
		if ($h['L_APPOITMENT_DATE'] > date('Y-m-d')) $cls .= ' l-app-stare';// . ' l-app-date-' . $h['L_APPOITMENT_DATE'];
		echo '<tr class="row-' . ($t = 1 - $t) . ' type-' . "PROCES" . $cls . '">';
			echo '<td>';
				echo "Proces ".'<span style="color:#705680;">'.$h['A_STATUS'].'</span>';
				if ($h['L_APPOITMENT_USER']) {
					echo'<br /><em style="color:#999;" title="'.$h['L_APPOITMENT_USER']." przypisany/a do procesu ".$h['ID'].'">('.$h['ID'].')</em>';
				}
			echo '</td>';
			echo '<td>'."Proces(ID):".'<a href="'."procesy5.php?task=CRM_PROCES&filtr_search_id=&filtr_id=".$h['ID'].'" target="_blank">'.$h['ID'].'</a>'.'</td>';
			echo '<td class="nobr">' . $h['L_APPOITMENT_DATE'] . '</td>';
			echo '<td>' . $h['TYPE'] . '</td>';
			echo '<td>' . $h['DESC'] . '</td>';
			echo '<td>'."Wpr: ".$h['A_RECORD_CREATE_AUTHOR']."/ akt : ".$h['A_RECORD_UPDATE_AUTHOR']."(".$h['A_RECORD_UPDATE_DATE'].")".'</td>';
			echo '<td>' . $h['L_APPOITMENT_INFO'] . '</td>';
		echo'</tr>';
	}//eof while sql
	echo'</tbody>';
	echo '</table>'."\n";
}


echo "Ilość pism : ".$ILOSC_PISM.", ilość spraw : ".$ILOSC_SPRAW.", ilość procesów : ".$ILOSC_PROCES.". Zestawienie w oprarciu o  Obiegu dokumentow </a> By Arkadusz Binder. <br>
<br>
Proszę o uznanie spraw w wykazie jako zadań do jakich istnieje zobowiązanie do wykonania lub podjęcia stosownego działania, zgodnie z nadmienionym regulaminem . <br>
W przypadku załatwienia sprawy, zgodnie z par 2 pkt 6 regulaminu należy wykazany rekord zaznaczyĆ jako ZAŁATWIONY. Względnie , jeżeli sprawa wykracza poza nasze kompetencje
lub jeżeli została niewłaściwie zadekretowana proszę o wysłanie informacji na biuro lub proszę korzystając z uprawnień , oraz znając inne sprawy
w wykazie spraw aby sprawę zadekretowaĆ do właściwej sprawy i dla niej odpowiedzialnej osoby. <br>
w przypaku chęci dostosowania indywidualnego widoku kolumn proszę o kontakt z administratorem systemu . 

Z poważaniem Arkadiusz Binder Prezes.
 ";

echo '</body></html>';

}
?>

