Browse Source

added panel, oferta for Route_UrlAction_ProjektyKosztyWstepnychRobot

Piotr Labudda 10 years ago
parent
commit
f9c5340797
1 changed files with 297 additions and 35 deletions
  1. 297 35
      SE/se-lib/Route/UrlAction/ProjektyKosztyWstepnychRobot.php

+ 297 - 35
SE/se-lib/Route/UrlAction/ProjektyKosztyWstepnychRobot.php

@@ -16,6 +16,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		try {
 			$idProject = V::get('ID_PROJECT', '', $_REQUEST, 'int');
 			if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");
+			$this->panel($idProject);
 			$this->kosztorys($idProject);
 		} catch (Exception $e) {
 			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
@@ -23,6 +24,34 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		SE_Layout::dol();
 	}
 
+	public function ofertaAdminAction() {
+		// TODO: check if user is allowed to run this action
+		SE_Layout::gora();
+		try {
+			$idProject = V::get('ID_PROJECT', '', $_REQUEST, 'int');
+			if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");
+			$this->panel($idProject);
+			$this->oferta($idProject, $admin = true);
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+		}
+		SE_Layout::dol();
+	}
+
+	public function ofertaAction() {
+		// TODO: check if user is allowed to run this action
+		SE_Layout::gora();
+		try {
+			$idProject = V::get('ID_PROJECT', '', $_REQUEST, 'int');
+			if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");
+			$this->panel($idProject);
+			$this->oferta($idProject, $admin = false);
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+		}
+		SE_Layout::dol();
+	}
+
 	public function getArgsList() {// TODO: override UrlActionBase::getArgsList action
 		$args = array();
 		$args[] = 'ID_PROJECT';
@@ -114,7 +143,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 				'out' => array(':action_param_1_id' => 'ID')
 			);
 		}
-		//DBG::_(true, true, "flatConfig_main", $flatConfig_main, __CLASS__, __FUNCTION__, __LINE__);
+		//DBG::_('DBG', '>1', "flatConfig_main", $flatConfig_main, __CLASS__, __FUNCTION__, __LINE__);
 		$flatConfig_link_in_projekt = array();
 		{
 			$flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db)
@@ -185,7 +214,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 				'out' => array(':link_action_id' => 'ID')
 			);
 		}
-		//DBG::_(true, true, "flatConfig_link_in_projekt", $flatConfig_link_in_projekt, __CLASS__, __FUNCTION__, __LINE__);
+		//DBG::_('DBG', '>1', "flatConfig_link_in_projekt", $flatConfig_link_in_projekt, __CLASS__, __FUNCTION__, __LINE__);
 		$flatConf = array_merge($flatConfig_main, $flatConfig_link_in_projekt);
 		$knownArgs = array();
 		$knownArgs[':url_action_name'] = 'Test';
@@ -197,15 +226,15 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		$args = $this->getArgsList();
 		$clsName = __CLASS__;
 		$urlActionName = str_replace('Route_UrlAction_', '', $clsName);
-		DBG::_(true, true, "reinstall class", __CLASS__, __CLASS__, __FUNCTION__, __LINE__);
-		DBG::_(true, true, "reinstall funName", $urlActionName, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG', '>1', "reinstall class", __CLASS__, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG', '>1', "reinstall funName", $urlActionName, __CLASS__, __FUNCTION__, __LINE__);
 
-		DBG::_(true, true, "reinstall test", $args, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG', '>1', "reinstall test", $args, __CLASS__, __FUNCTION__, __LINE__);
 		$dbFunction = $this->fetchZasobInfo();
 		$usrLogin = User::getLogin();
 		{// check if URL_ACTION already exists
 			if (!$dbFunction) throw new Exception("Brak id zasobu typu TYPESPECIALS - TODO: CREATE");// $this->createZasobTypespecials();
-			DBG::_(true, true, "dbFunction", $dbFunction, __CLASS__, __FUNCTION__, __LINE__);
+			DBG::_('DBG', '>1', "dbFunction", $dbFunction, __CLASS__, __FUNCTION__, __LINE__);
 			if ($dbFunction['action_id_is_null']) {
 				$sth = $pdo->prepare("
 					insert into CRM_LISTA_ZASOBOW (
@@ -229,7 +258,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 				$bindValues['ts_id'] = array($dbFunction['ts_id'], PDO::PARAM_INT);
 				$bindValues['url_action_name'] = $urlActionName;
 				$pdo->bindValues($sth, $bindValues);
-				DBG::_(true, true, "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
+				DBG::_('DBG', '>1', "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
 				$sth->execute();
 				$dbFunction = $this->fetchZasobInfo();
 				if (!$dbFunction || $dbFunction['action_id_is_null']) throw new Exception("Brak zasobu typu URL_ACTION, nie udało się go utworzyć!");
@@ -247,7 +276,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 					// OK PARAM_IN exists
 				}
 				$todoArgs = array_keys($todoArgs);
-				DBG::_(true, true, "todoArgs", $todoArgs, __CLASS__, __FUNCTION__, __LINE__);
+				DBG::_('DBG', '>1', "todoArgs", $todoArgs, __CLASS__, __FUNCTION__, __LINE__);
 				if (!empty($todoArgs)) {
 					$sqlValues = array();
 					foreach ($todoArgs as $argName) {
@@ -271,7 +300,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 						)
 						values {$sqlValues}
 					");
-					DBG::_(true, true, "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
+					DBG::_('DBG', '>1', "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
 					//$sth->execute();
 					$dbFunction = $this->fetchZasobInfo();
 				}
@@ -310,7 +339,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 						NOW()
 					)
 				");
-				//DBG::_(true, true, "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
+				//DBG::_('DBG', '>1', "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
 				$sth->execute();
 				$dbFunction = $this->fetchZasobInfo();
 				$projInfo = $dbFunction['links']['IN7_MK_BAZA_DYSTRYBUCJI'];
@@ -345,7 +374,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 	}
 
 	public function _debugFlatConfig($flatConfig, $args = array()) {
-		//DBG::_(true, true, "flatConfig", $flatConfig, __CLASS__, __FUNCTION__, __LINE__);
+		//DBG::_('DBG', '>1', "flatConfig", $flatConfig, __CLASS__, __FUNCTION__, __LINE__);
 		echo '<pre style="border:1px solid green">';
 		foreach ($flatConfig as $conf) {
 			/*
@@ -489,7 +518,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 					and zt.A_STATUS not in('DELETED')
 					and zt.DESC = 'IN7_MK_BAZA_DYSTRYBUCJI'
 			");
-			DBG::_(true, true, "dbInfo query", $pdo->getRawSql($sth_dbInfo), __CLASS__, __FUNCTION__, __LINE__);
+			DBG::_('DBG', '>1', "dbInfo query", $pdo->getRawSql($sth_dbInfo), __CLASS__, __FUNCTION__, __LINE__);
 			$sth_dbInfo->execute();
 			$linkInfo = $sth_dbInfo->fetch();
 			if ($linkInfo) {
@@ -501,9 +530,9 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 
 	public function kosztorysXmlAction() {
 		$idProject = 1921;
-		$schema = $this->_getKosztorysSchema();
+		$schema = $this->getSchema();
 		$data = $this->_fetchKosztorysData($idProject);
-		//DBG::_(true, true, "XMLWriter", class_exists('XMLWriter'), __CLASS__, __FUNCTION__, __LINE__);
+		//DBG::_('DBG', '>1', "XMLWriter", class_exists('XMLWriter'), __CLASS__, __FUNCTION__, __LINE__);
 		//header('Content-type: application/xml; charset=utf-8');
 		header('Content-type: text/plain; charset=utf-8');
 		$xmlWriter = new XMLWriter();
@@ -517,7 +546,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 			$xmlWriter->writeAttributeNS('xmlns', 'p5', 'http://www.w3.org/2000/xmlns/', 'https://biuro.biall-net.pl/wfs');
 			for ($i = 1; $i <= 10; $i++) {
 				$idProject += 1;
-				$schema = $this->_getKosztorysSchema();
+				$schema = $this->getSchema();
 				$data = $this->_fetchKosztorysData($idProject);
 				$xmlWriter->startElement('kosztorys');
 					$xmlWriter->startElement('projekt');
@@ -535,7 +564,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 
 			for ($i = 1; $i <= 10; $i++) {
 				$idProject += 1;
-				$schema = $this->_getKosztorysSchema();
+				$schema = $this->getSchema();
 				$data = $this->_fetchKosztorysData($idProject);
 
 				$memXmlWriter->startElement('kosztorys');
@@ -557,12 +586,68 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		}
 	}
 
+	public function panel($idProject) {
+		if (User::get('ADM_ADMIN_LEVEL') > 6 || 'Pracownik' != User::getType()) return;// Only for workers with admin level < 6
+		if (1 == V::get('_print', '', $_GET)) return;// print mode
+		?>
+<div class="jumbotron">
+  <div class="container">
+<?php
+		$schema = $this->getSchema();
+?>
+		<div class="row">
+			<div class="col-md-12">
+				<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>">Kosztorys</a>
+				<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_print=1" target="_blank">Wydruk Kosztorysu</a>
+				<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=ofertaAdmin">oferta (Admin)</a>
+				<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=oferta">oferta</a>
+			</div>
+		</div>
+  </div>
+</div>
+<?php
+	}
+
+	public function oferta($idProject, $admin = false) {
+		$schema = $this->getSchema();
+		$conf = $schema['config'];
+		DBG::_('DBG', '>1', "conf", $conf, __CLASS__, __FUNCTION__, __LINE__);
+
+		$data = $this->fetchData($idProject);
+		DBG::_('DBG', '>1', "data", $data, __CLASS__, __FUNCTION__, __LINE__);
+		?>
+<div class="container">
+	<?php foreach ($data as $layData) : ?>
+		<h4 style="padding:0 6px"><?php echo $layData['label']; ?></h4>
+		<table class="tabel table-bordered" style="width:100%">
+		<tbody>
+			<tr>
+				<th style="padding:0 6px">typ</th>
+				<th style="padding:0 6px">ilość</th>
+				<th style="padding:0 6px">jednotka miary</th>
+				<th style="padding:0 6px">cena</th>
+			</tr>
+			<?php foreach ($layData['data'] as $typeData) : ?>
+				<tr>
+					<td style="padding:0 6px" title="[<?php echo $typeData['idType']; ?>] <?php echo $typeData['type']; ?>"><?php echo $typeData['type']; ?></td>
+					<td style="padding:0 6px"><?php echo $typeData['ilosc']; ?></td>
+					<td style="padding:0 6px"><?php echo $typeData['jednostka']; ?></td>
+					<td style="padding:0 6px">TODO: cena</td>
+				</tr>
+			<?php endforeach; ?>
+		</tbody>
+		</table>
+	<?php endforeach; ?>
+</div>
+<?php
+	}
+
 	public function kosztorys($idProject) {
-		$schema = $this->_getKosztorysSchema();
+		$schema = $this->getSchema();
 		$data = $this->_fetchKosztorysData($idProject);
 ?>
 <div class="container">
-	<h1>Kosztorys wstępnych robót telekomunikacyjnych</h1>
+	<h1>Kosztorys wstępny robót telekomunikacyjnych</h1>
 	<table class="table">
 		<tr>
 			<th><?php echo $schema['nr']; ?></th>
@@ -614,12 +699,127 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		DBG::_('DBG', '>0', "schema", $schema, __CLASS__, __FUNCTION__, __LINE__);
 	}
 
-	public function _getKosztorysSchema() {
-		$schema['nr'] = "Nr projektu";
-		$schema['title'] = "Tytuł projektu";
-		$schema['owner_name'] = "Osoba prowadząca";
-		$schema['cost_total'] = "Szacowany koszt projektu [zł]";
-		$schema['sub_costs'] = array();
+	public function checkGeomDuplicate() {
+		// TODO: for every schema.layer
+		$sqlTblName = 'Rozdzielcza_Kabel_Swiatlowodowy_wsg84';
+		$sql = "select a.ID, b.ID, aswkt(a.the_geom)
+			from `{$sqlTblName}` a
+				join `$sqlTblName` b on(b.the_geom = a.the_geom and a.ID != b.ID)
+		";
+	}
+
+	public function getSchema() {
+		static $_schema = null;
+		if (null !== $_schema) return $_schema;
+		$_schema = array();
+		/*
+			22444 INNE Kosztorys - zasoby
+				22445 INNE Kabel 2J do wdmuchiwania w mikrorurce 7/4 mm z wdmuchiwaniem
+				22446 INNE Kabel 4J do wdmuchiwania w mikrorurce 7/4 mm z wdmuchiwaniem
+				... Labels for order form
+				22460 TABELA Światłowód (Alias do [20225] TABELA default_db/Rozdzielcza_Kabel_Swiatlowodowy_wsg84)
+					22461 20299 KOMORKA ZASOB (Alias do [20299] KOMORKA OznKabla)
+					22462 20292 KOMORKA JEDNOSTKA_METR ilość [m] (Alias do [20292] KOMORKA Dlugosc)
+				... where to search for data
+		*/
+		{
+			$pdo = DB::getPDO();
+			$sth = $pdo->prepare("
+				select z.ID, z.TYPE, z.DESC, z.ALIAS_ID
+					, c.ID as c_ID, c.TYPE as c_TYPE, c.DESC as c_DESC, c.ALIAS_ID as c_ALIAS_ID
+					, a.ID as a_ID, a.TYPE as a_TYPE, a.DESC as a_DESC, a.ALIAS_ID as a_ALIAS_ID
+					, za.ID as za_ID, za.TYPE as za_TYPE, za.DESC as za_DESC, za.ALIAS_ID as za_ALIAS_ID
+				from CRM_LISTA_ZASOBOW z
+					left join CRM_LISTA_ZASOBOW c  on(c.PARENT_ID = z.ID)
+					left join CRM_LISTA_ZASOBOW a  on(a.ID  = z.ALIAS_ID and z.ALIAS_ID > 0)
+					left join CRM_LISTA_ZASOBOW za on(za.ID = c.ALIAS_ID and c.ALIAS_ID > 0)
+				where z.PARENT_ID = 22444  -- TODO how to find ID, Typespecial with link
+			");
+			$sth->execute();
+			$rawConf = $sth->fetchAll();
+			$conf = array();
+			$conf['type'] = array();
+			$conf['layer'] = array();
+			foreach ($rawConf as $z) {
+				if ('INNE' == $z['TYPE']) {
+					if (!array_key_exists($z['ID'], $conf['type'])) {
+						$conf['type'][$z['ID']] = $z['DESC'];
+					}
+				} else if ('TABELA' == $z['TYPE']) {
+					if (!array_key_exists($z['ID'], $conf['layer'])) {
+						$layer = array();
+						$layer['label'] = $z['DESC'];
+						$layer['tabela_id'] = $z['a_ID'];
+						$layer['tabela_name'] = $z['a_DESC'];
+						$layer['jednostka'] = '';
+						$layer['zasob_type'] = '';
+						$layer['zasob_field'] = '';
+						$layer['zasob_id'] = '';
+						$layer['ilosc_field'] = '';
+						$conf['layer'][$z['ID']] = $layer;
+					}
+					if ($z['c_ID'] > 0) {
+						if ('JEDNOSTKA_' == substr($z['c_DESC'], 0, 10)) {
+							$layer = $conf['layer'][$z['ID']];
+							$layer['jednostka'] = substr($z['c_DESC'], 10);
+							$layer['ilosc_field'] = $z['za_DESC'];
+							$layer['zasob_id'] = $z['za_ID'];
+							$conf['layer'][$z['ID']] = $layer;
+						}
+						/*
+							[za_ID] => 20299
+							[za_TYPE] => KOMORKA
+							[za_DESC] => OznKabla
+							[za_ALIAS_ID] => 0
+						*/
+						if ('ZASOB' == $z['c_DESC']) {
+							if ($z['za_ID'] > 0) {
+								$layer = $conf['layer'][$z['ID']];
+								$layer['zasob_type'] = $z['c_DESC'];
+								$layer['zasob_field'] = $z['za_DESC'];
+								$layer['zasob_id'] = $z['za_ID'];
+								$conf['layer'][$z['ID']] = $layer;
+							}
+						}
+						if ('ZASOB_ID' == $z['c_DESC']) {
+							if ($z['za_ID'] > 0) {
+								$layer = $conf['layer'][$z['ID']];
+								$layer['zasob_type'] = $z['c_DESC'];
+								$layer['zasob_id'] = $z['za_ID'];
+								$layer['zasob_label'] = $z['za_DESC'];
+								$conf['layer'][$z['ID']] = $layer;
+							}
+						}
+					}
+				} else {
+					SE_Layout::alert('warnig', "BUG: unimplemented type '{$z['TYPE']}' for zasob nr: {$z['ID']}");
+				}
+			}
+			DBG::_('DBG', '>1', "config", $conf, __CLASS__, __FUNCTION__, __LINE__);
+			{// validate schema - sho warnings
+				foreach ($conf['layer'] as $layer) {
+					try {
+						if (empty($layer['tabela_id'])) throw new Exception("brak zdefiniowanego aliasa do tabeli dla warstwy {$layer['label']}");
+						if (empty($layer['jednostka'])) throw new Exception("brak jednostki dla warstwy [{$layer['tabela_id']}] {$layer['label']}");
+						switch ($layer['jednostka']) {
+							case 'METR': break;
+							case 'SZTUKA': break;
+							default: throw new Exception("Nieznana jednostka '{$layer['jednostka']}' dla warstwy  [{$layer['tabela_id']}] {$layer['label']}");
+						}
+					} catch (Exception $e) {
+						SE_Layout::alert('warning', $e->getMessage());
+					}
+				}
+			}
+			DBG::_('DBG', '>2', "rawConf", $rawConf, __CLASS__, __FUNCTION__, __LINE__);
+			$_schema['config'] = $conf;
+		}
+
+		$_schema['nr'] = "Nr projektu";
+		$_schema['title'] = "Tytuł projektu";
+		$_schema['owner_name'] = "Osoba prowadząca";
+		$_schema['cost_total'] = "Szacowany koszt projektu [zł]";
+		$_schema['sub_costs'] = array();
 		{
 			$layerConf = array();
 			$layerConf['label'] = "Wykop";
@@ -627,7 +827,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 			$layerConf['_agr_fields_to_cols']['ilosc'] = "ilość [m]";
 			$layerConf['_agr_fields_to_cols']['cena'] = "podstawowa cena [zł/m]";
 			$layerConf['_agr_fields_to_cols']['koszt'] = "koszt [zł]";
-			$schema['sub_costs']['Rozdzielcza_Wykop_przedmiar_na_mikrorurki'] = $layerConf;
+			$_schema['sub_costs']['Rozdzielcza_Wykop_przedmiar_na_mikrorurki'] = $layerConf;
 		}
 		{
 			$layerConf = array();
@@ -636,7 +836,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 			$layerConf['_agr_fields_to_cols']['ilosc'] = "ilość [m]";
 			$layerConf['_agr_fields_to_cols']['cena'] = "podstawowa cena [zł/m]";
 			$layerConf['_agr_fields_to_cols']['koszt'] = "koszt [zł]";
-			$schema['sub_costs']['Rozdzielcza_Mikrokanalizacja_do_klienta'] = $layerConf;
+			$_schema['sub_costs']['Rozdzielcza_Mikrokanalizacja_do_klienta'] = $layerConf;
 		}
 		{
 			$layerConf = array();
@@ -645,7 +845,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 			$layerConf['_agr_fields_to_cols']['ilosc'] = "ilość [m]";
 			$layerConf['_agr_fields_to_cols']['cena'] = "podstawowa cena [zł/m]";
 			$layerConf['_agr_fields_to_cols']['koszt'] = "koszt [zł]";
-			$schema['sub_costs']['Rozdzielcza_Przeciski_110mm'] = $layerConf;
+			$_schema['sub_costs']['Rozdzielcza_Przeciski_110mm'] = $layerConf;
 		}
 		{
 			$layerConf = array();
@@ -654,7 +854,7 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 			$layerConf['_agr_fields_to_cols']['ilosc'] = "ilość [m]";
 			$layerConf['_agr_fields_to_cols']['cena'] = "podstawowa cena [zł/m]";
 			$layerConf['_agr_fields_to_cols']['koszt'] = "koszt [zł]";
-			$schema['sub_costs']['Rozdzielcza_Zabruki'] = $layerConf;
+			$_schema['sub_costs']['Rozdzielcza_Zabruki'] = $layerConf;
 		}
 		{
 			$layerConf = array();
@@ -675,38 +875,100 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 				$layerSubCostsConf['labels']['sum_dlugosc'] = "suma długości";
 				$layerConf['sub_costs'] = $layerSubCostsConf;
 			}
-			$schema['sub_costs']['Rozdzielcza_Kabel_Swiatlowodowy_wsg84'] = $layerConf;
+			$_schema['sub_costs']['Rozdzielcza_Kabel_Swiatlowodowy_wsg84'] = $layerConf;
 		}
 		{
 			$layerConf = array();
 			$layerConf['label'] = "Studnie - TODO (Lokalizacje)";
 			$layerConf['_agr_fields_to_cols'] = array();
-			$schema['sub_costs']['__STUDNIE__'] = $layerConf;
+			$_schema['sub_costs']['__STUDNIE__'] = $layerConf;
 		}
 		{
 			$layerConf = array();
 			$layerConf['label'] = "Rury osłonowe - TODO";
 			$layerConf['_agr_fields_to_cols'] = array();
-			$schema['sub_costs']['Rura_oslonowa_rozdzielcza_magistralna'] = $layerConf;
+			$_schema['sub_costs']['Rura_oslonowa_rozdzielcza_magistralna'] = $layerConf;
 		}
 		{
 			$layerConf = array();
 			$layerConf['label'] = "Pakiet mikrorurek - TODO";
 			$layerConf['_agr_fields_to_cols'] = array();
-			$schema['sub_costs']['__PAKIET_MIKRORUREK__'] = $layerConf;
+			$_schema['sub_costs']['__PAKIET_MIKRORUREK__'] = $layerConf;
 		}
 		{
 			$layerConf = array();
 			$layerConf['label'] = "Koszty dodatkowe - TODO";
 			$layerConf['_agr_fields_to_cols'] = array();
-			$schema['sub_costs']['__KOSZTY_DODATKOWE__'] = $layerConf;
+			$_schema['sub_costs']['__KOSZTY_DODATKOWE__'] = $layerConf;
+		}
+		return $_schema;
+	}
+
+	public function fetchData($idProject) {
+		$data = array();
+		$schema = $this->getSchema();
+		$conf = $schema['config'];
+		$pdo = DB::getPDO();
+		foreach ($conf['layer'] as $idLayer => $layer) {
+			$layData = array();
+			$layData['label'] = $layer['label'];
+			{
+				if (!$layer['tabela_name']) continue;// TODO: throw error in validate
+				$tblName = $layer['tabela_name'];
+				$sqlIloscField = (!empty($layer['ilosc_field']))? $layer['ilosc_field'] : 'ID';
+				$sqlIlosc = ('SZTUKA' == $layer['jednostka'])? "count(1)" : "sum(t.{$sqlIloscField})";
+				$sqlZasobField = (!empty($layer['zasob_field']))? $layer['zasob_field'] : 'ID';
+				$sqlGroupBy = (!empty($layer['zasob_field']))? "group by t.{$sqlZasobField}" : '';
+				$sth = $pdo->prepare("
+					select t.{$sqlZasobField}
+						, {$sqlIlosc} as ilosc
+					from {$tblName} t
+					where t.ID_PROJECT = :id_project
+					{$sqlGroupBy}
+				");
+				$sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
+				$sth->execute();
+				$rawLayData = $sth->fetchAll();
+				$layData['_raw'] = $rawLayData;
+				$layData['_rawSql'] = "
+					select t.{$sqlZasobField}
+						, sum(t.{$sqlIloscField}) as ilosc
+					from {$tblName} t
+					where t.ID_PROJECT = {$idProject}
+					{$sqlGroupBy}
+				";
+				$iloscByType = array();
+				$hasIlosc = false;
+				foreach ($rawLayData as $d) {
+					if ($d['ilosc'] <= 0) continue;
+					$hasIlosc = true;
+					DBG::_('DBG', '>1', "layer", $layer, __CLASS__, __FUNCTION__, __LINE__);
+					if ('ZASOB' == $layer['zasob_type']) {
+						$iloscType = array();
+						$iloscType['type'] = $d[$sqlZasobField];
+						$iloscType['jednostka'] = $layer['jednostka'];
+						$iloscType['ilosc'] = $d['ilosc'];
+						$iloscByType[] = $iloscType;
+					} else if ('ZASOB_ID' == $layer['zasob_type']) {
+						$iloscType = array();
+						$iloscType['type'] = $layer['zasob_label'];
+						$iloscType['jednostka'] = $layer['jednostka'];
+						$iloscType['ilosc'] = $d['ilosc'];
+						$iloscByType[] = $iloscType;
+					} else {
+						// TODO: BUG
+					}
+				}
+				if ($hasIlosc) $layData['data'] = $iloscByType;
+			}
+			if (!empty($layData['data'])) $data[$idLayer] = $layData;
 		}
-		return $schema;
+		return $data;
 	}
 
 	public function _fetchKosztorysData($idProject) {
 		$project = array();
-		$schema = $this->_getKosztorysSchema();
+		$schema = $this->getSchema();
 
 		if(0){// TEST join by ogc
 $exampleOgcJoin = <<<OGC_JOIN