panel($idProject, $idCompany); $this->kosztorys($idProject, $idCompany); } catch (Exception $e) { SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage()); } SE_Layout::dol(); } public function ofertaAdminAction() { // TODO: check if user is allowed to run this action SE_Layout::gora(); SE_Layout::menu(); try { $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int'); $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int'); $this->panel($idProject, $idCompany); if ($idProject > 0) { $this->oferta($idProject, $idCompany, $admin = true); } else { $this->defaultOferta(); } } catch (Exception $e) { SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage()); } SE_Layout::dol(); } public function ofertaDefaultAdminAction() { // TODO: check if user is allowed to run this action SE_Layout::gora(); SE_Layout::menu(); try { $idProject = 0; $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int'); $this->panel($idProject, $idCompany); $this->defaultOferta(); } 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(); SE_Layout::menu(); try { $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int'); $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int'); if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");// TODO: show select box if not defined $this->panel($idProject, $idCompany); $this->oferta($idProject, $idCompany, $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'; return $args; } public function reinstallAction() {// TODO: mv to UrlActionBase $jsonData = new stdClass(); $jsonData->type = 'success'; $jsonData->msg = 'Gotowe'; try { $this->reinstall(); } catch (Exception $e) { $jsonData->type = 'danger'; $jsonData->msg = $e->getMessage(); } echo json_encode($jsonData); } public function reinstall() {// TODO: mv struct check to UrlActionBase $pdo = DB::getPDO(); { $dropGDistanceFuncion = "DROP FUNCTION IF EXISTS `GDistance`"; $pdo->exec($dropGDistanceFuncion); // Test for GDistance: // select '' as test, GDistance('') as GDistance, IF(GDistance('') = 0, 1, 0) as test_ok; // select 'NULL' as test, GDistance(NULL) as GDistance, IF(GDistance(NULL) = 0, 1, 0) as test_ok; // select 'GeomFromText(POLYGON((18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799,18.5617610722586 54.346524689865,18.5619728898979 54.3488528136092,18.5609743210268 54.3496376437764,18.5600057772984 54.3494578097971,18.559603951991 54.350742572617,18.5590637025025 54.3507194967965,18.5578575757622 54.349434823939,18.5573431614953 54.3472831082951)))' as test, GDistance(GeomFromText('POLYGON((18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799,18.5617610722586 54.346524689865,18.5619728898979 54.3488528136092,18.5609743210268 54.3496376437764,18.5600057772984 54.3494578097971,18.559603951991 54.350742572617,18.5590637025025 54.3507194967965,18.5578575757622 54.349434823939,18.5573431614953 54.3472831082951))')) as GDistance, IF(1 = GDistance(GeomFromText('POLYGON((18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799,18.5617610722586 54.346524689865,18.5619728898979 54.3488528136092,18.5609743210268 54.3496376437764,18.5600057772984 54.3494578097971,18.559603951991 54.350742572617,18.5590637025025 54.3507194967965,18.5578575757622 54.349434823939,18.5573431614953 54.3472831082951))')), 1, 0) as test_ok; // select 'GeomFromText(LINESTRING(18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799))' as test, GDistance(GeomFromText('LINESTRING(18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799)')) as GDistance, IF(GDistance(GeomFromText('LINESTRING(18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799)')) is not null, 1, 0) as test_ok; // select 'GeomFromText(POINT(18.5573431614953 54.3472831082951))' as test, GDistance(GeomFromText('POINT(18.5573431614953 54.3472831082951)')) as GDistance, IF(GDistance(GeomFromText('POINT(18.5573431614953 54.3472831082951)')) is not null, 1, 0) as test_ok; $gDistanceFuncion = <<exec($gDistanceFuncion); } /* required Zasoby tree structure - XML (parent rel in PARENT_ID field): */ /* required Zasoby tree structure: - required base structure [:ts_main_id] TYPESPECIALS [:action_id] URL_ACTION :url_action_name [:action_param_1_id] PARAM_IN :url_action_param_1_name - current function from schema [:main_db_id] (DATABASE_MYSQL, BAZA_DANYCH, DATABASE_POSTGRESQL) [:tbl_id] TABELA 'IN7_MK_BAZA_DYSTRYBUCJI' [:cell_id] KOMORKA 'ID' [:link_action_id] (ALIAS DO :action_id) URL_ACTION :url_action_name [:link_param_id] (ALIAS DO :cell_id) PARAM_IN :url_action_param_1_name :url_action_name = 'ProjektyKosztyWstepnychRobot' :url_action_param_1_name = 'ID_PROJECT' */ $flatConfig_main = array(); { $flatConfig_main[] = (object)array(// Zasob::assert(TYPESPECIALS) 'query' => " select z.ID from CRM_LISTA_ZASOBOW z where z.TYPE='TYPESPECIALS' and z.A_STATUS not in('DELETED') ", 'createTable' => 'CRM_LISTA_ZASOBOW', 'createArgs' => array('TYPE' => 'TYPESPECIALS', 'DESC' => 'Typespecials'), 'out' => array(':ts_main_id' => 'ID') ); $flatConfig_main[] = (object)array(// Zasob::assert(TYPESPECIALS/:url_action_name) 'query' => " select z.ID from CRM_LISTA_ZASOBOW z where z.PARENT_ID = :ts_main_id and z.TYPE = 'URL_ACTION' and z.DESC = :url_action_name and z.A_STATUS not in('DELETED') ", 'createTable' => 'CRM_LISTA_ZASOBOW', 'createArgs' => array('TYPE' => 'URL_ACTION', 'PARENT_ID' => ':ts_main_id', 'DESC' => ':url_action_name'), 'in' => array(':ts_main_id', ':url_action_name'), 'out' => array(':action_id' => 'ID') ); $flatConfig_main[] = (object)array(// Zasob::assert(TYPESPECIALS/:url_action_name/:url_action_param_1_name) 'query' => " select z.ID from CRM_LISTA_ZASOBOW z where z.PARENT_ID = :action_id and z.TYPE = 'PARAM_IN' and z.DESC = :url_action_param_1_name and z.A_STATUS not in('DELETED') ", 'createTable' => 'CRM_LISTA_ZASOBOW', 'createArgs' => array('TYPE' => 'PARAM_IN', 'PARENT_ID' => ':action_id', 'DESC' => ':url_action_param_1_name'), 'in' => array(':action_id', ':url_action_param_1_name'), 'out' => array(':action_param_1_id' => 'ID') ); } //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) 'query' => " select z.ID from CRM_LISTA_ZASOBOW z where z.ID = :main_db_id and z.TYPE in('DATABASE_MYSQL', 'BAZA_DANYCH', 'DATABASE_POSTGRESQL') and z.A_STATUS not in('DELETED') ", 'createTable' => 'CRM_LISTA_ZASOBOW', 'createArgs' => array('TYPE' => 'BAZA_DANYCH', 'DESC' => 'Baza danych'), 'in' => array(':main_db_id'), 'out' => array(':main_db_id' => 'ID') ); $flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db/IN7_MK_BAZA_DYSTRYBUCJI) 'query' => " select z.ID from CRM_LISTA_ZASOBOW z where z.PARENT_ID = :main_db_id and z.TYPE = 'TABELA' and z.DESC = 'IN7_MK_BAZA_DYSTRYBUCJI' and z.A_STATUS not in('DELETED') ", 'createTable' => 'CRM_LISTA_ZASOBOW', 'createArgs' => array('TYPE' => 'TABELA', 'PARENT_ID' => ':main_db_id', 'DESC' => 'IN7_MK_BAZA_DYSTRYBUCJI'), 'in' => array(':main_db_id'), 'out' => array(':tbl_id' => 'ID') ); $flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db/IN7_MK_BAZA_DYSTRYBUCJI/ID) 'query' => " select z.ID from CRM_LISTA_ZASOBOW z where z.PARENT_ID = :tbl_id and z.TYPE = 'KOMORKA' and z.DESC = 'ID' and z.A_STATUS not in('DELETED') ", 'createTable' => 'CRM_LISTA_ZASOBOW', 'createArgs' => array('TYPE' => 'KOMORKA', 'PARENT_ID' => ':tbl_id', 'DESC' => 'ID'), 'in' => array(':tbl_id'), 'out' => array(':cell_id' => 'ID') ); $flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db/IN7_MK_BAZA_DYSTRYBUCJI/:url_action_name) 'query' => " select z.ID from CRM_LISTA_ZASOBOW z where z.PARENT_ID = :tbl_id and z.ALIAS_ID = ':action_id' and z.TYPE = 'URL_ACTION' and z.DESC = ':url_action_name' and z.A_STATUS not in('DELETED') ", 'createTable' => 'CRM_LISTA_ZASOBOW', 'createArgs' => array('TYPE' => 'URL_ACTION', 'PARENT_ID' => ':tbl_id', 'ALIAS_ID' => ':action_id', 'DESC' => ':url_action_name'), 'in' => array(':tbl_id', ':action_id', ':url_action_name'), 'out' => array(':link_action_id' => 'ID') ); // [:link_param_id] (ALIAS DO :cell_id) PARAM_IN :url_action_param_1_name $flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db/IN7_MK_BAZA_DYSTRYBUCJI/:url_action_name/:url_action_param_1_name) 'query' => " select z.ID from CRM_LISTA_ZASOBOW z where z.PARENT_ID = :link_action_id and z.ALIAS_ID = ':cell_id' and z.TYPE = 'PARAM_IN' and z.DESC = ':url_action_param_1_name' and z.A_STATUS not in('DELETED') ", 'createTable' => 'CRM_LISTA_ZASOBOW', 'createArgs' => array('TYPE' => 'PARAM_IN', 'PARENT_ID' => ':link_action_id', 'ALIAS_ID' => ':cell_id', 'DESC' => ':url_action_param_1_name'), 'in' => array(':cell_id', ':link_action_id', ':url_action_param_1_name'), 'out' => array(':link_action_id' => 'ID') ); } //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'; $knownArgs[':url_action_param_1_name'] = 'ID_PROJECT'; $knownArgs[':main_db_id'] = '36';// from DB::getDB()->getZasobId(); or DB::getPDO()->getZasobId(); $this->_debugFlatConfig($flatConf, $knownArgs); $args = $this->getArgsList(); $clsName = __CLASS__; $urlActionName = str_replace('Route_UrlAction_', '', $clsName); DBG::_('DBG', '>1', "reinstall class", __CLASS__, __CLASS__, __FUNCTION__, __LINE__); DBG::_('DBG', '>1', "reinstall funName", $urlActionName, __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::_('DBG', '>1', "dbFunction", $dbFunction, __CLASS__, __FUNCTION__, __LINE__); if ($dbFunction['action_id_is_null']) { $sth = $pdo->prepare(" insert into CRM_LISTA_ZASOBOW ( TYPE, PARENT_ID, `DESC`, A_RECORD_CREATE_AUTHOR, A_RECORD_CREATE_DATE ) values ( 'URL_ACTION', :ts_id, :url_action_name, 'system-by-{$usrLogin}', NOW() ) "); //$sth->bindValue('ts_id', $dbFunction['ts_id'], PDO::PARAM_INT); //$sth->bindValue('url_action_name', $urlActionName, PDO::PARAM_STR); $bindValues = array(); $bindValues['ts_id'] = array($dbFunction['ts_id'], PDO::PARAM_INT); $bindValues['url_action_name'] = $urlActionName; $pdo->bindValues($sth, $bindValues); 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ć!"); } } {// check params if (!empty($args)) { $todoArgs = array(); foreach ($args as $argName) { if (empty($dbFunction['url_params'])) { $todoArgs[$argName] = true; } else if (!array_key_exists($argName, $dbFunction['url_params'])) { $todoArgs[$argName] = true; } // OK PARAM_IN exists } $todoArgs = array_keys($todoArgs); DBG::_('DBG', '>1', "todoArgs", $todoArgs, __CLASS__, __FUNCTION__, __LINE__); if (!empty($todoArgs)) { $sqlValues = array(); foreach ($todoArgs as $argName) { $sqlValues[] = "( 'PARAM_IN', {$dbFunction['action_id']}, '{$argName}', 'system-by-{$usrLogin}', NOW() ) "; } $sqlValues = implode(", ", $sqlValues); $sth = $pdo->prepare(" insert into CRM_LISTA_ZASOBOW ( TYPE, PARENT_ID, `DESC`, A_RECORD_CREATE_AUTHOR, A_RECORD_CREATE_DATE ) values {$sqlValues} "); DBG::_('DBG', '>1', "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__); //$sth->execute(); $dbFunction = $this->fetchZasobInfo(); } if (empty($dbFunction['url_params'])) { throw new Exception("Brak zdefiniowanych parametrów, nie udało się ich utworzyć!"); } } } {// check links in tables // $info['links']['IN7_MK_BAZA_DYSTRYBUCJI'] = $linkInfo; if (empty($dbFunction['links']['IN7_MK_BAZA_DYSTRYBUCJI'])) throw new Exception("Brak poprawne wprowadzonej tabeli 'IN7_MK_BAZA_DYSTRYBUCJI' w drzewie zasobów!"); $projInfo = $dbFunction['links']['IN7_MK_BAZA_DYSTRYBUCJI']; /* [link_action_id] => [db_id] => 36 [tbl_id] => 636 [cell_id] => 763 [param1_id] => */ if (!$projInfo['link_action_id']) { $sth = $pdo->prepare(" insert into CRM_LISTA_ZASOBOW ( TYPE, PARENT_ID, ALIAS_ID, `DESC`, A_RECORD_CREATE_AUTHOR, A_RECORD_CREATE_DATE ) values ( 'URL_ACTION', {$projInfo['tbl_id']}, {$dbFunction['action_id']}, '{$urlActionName}', 'system-by-{$usrLogin}', NOW() ) "); //DBG::_('DBG', '>1', "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__); $sth->execute(); $dbFunction = $this->fetchZasobInfo(); $projInfo = $dbFunction['links']['IN7_MK_BAZA_DYSTRYBUCJI']; if (!$projInfo['link_action_id']) throw new Exception("Brak zainstalowanej akcji dla tabeli 'IN7_MK_BAZA_DYSTRYBUCJI' - nie udało się zainstalować"); } if (!$projInfo['param1_id']) {// TODO: get from params $sth = $pdo->prepare(" insert into CRM_LISTA_ZASOBOW ( TYPE, PARENT_ID, ALIAS_ID, `DESC`, A_RECORD_CREATE_AUTHOR, A_RECORD_CREATE_DATE ) values ( 'PARAM_IN', {$projInfo['link_action_id']}, {$projInfo['cell_id']}, 'ID_PROJECT', 'system-by-{$usrLogin}', NOW() ) "); $sth->execute(); $dbFunction = $this->fetchZasobInfo(); $projInfo = $dbFunction['links']['IN7_MK_BAZA_DYSTRYBUCJI']; if (!$projInfo['param1_id']) throw new Exception("Brak zainstalowanego parametru 1 w akcji dla tabeli 'IN7_MK_BAZA_DYSTRYBUCJI' - nie udało się zainstalować"); } } } public function _debugFlatConfig($flatConfig, $args = array()) { //DBG::_('DBG', '>1', "flatConfig", $flatConfig, __CLASS__, __FUNCTION__, __LINE__); echo '
';
		foreach ($flatConfig as $conf) {
			/*
				[query] => string
				[createTable] => srting - eg. CRM_LISTA_ZASOBOW
				[createArgs] => array - eg. [ TYPE: 'TYPESPECIALS', DESC: 'Typespecials' ]
				[in] => array - eg. [ ':ts_main_id', ':url_action_name' ]
				[out] => array - eg. [ ':ts_main_id': 'ID' ]
			*/
			echo "Zasob::assert({$conf->createTable}): " . json_encode($conf->createArgs) . " returns: " . json_encode($conf->out) . "\n";
		}
		echo '
'; echo '
';
		echo "DBG: Simulate iter loop:\n";
		$simulateGeneratedId = 1000;
		$i = 0; $limit = 10; $todoIdx = array(); $knownArgsMap = array();
		foreach ($flatConfig as $idx => $conf) $todoIdx[] = $idx;
		foreach ($args as $arg => $val) $knownArgsMap[$arg] = $val;
		for ($i = 0; $i < $limit; $i++) {
			echo ">> DBG loop({$i}):\n";
			$isExecuted = false;
			foreach ($todoIdx as $idx) {
				$conf = $flatConfig[$idx];
				/*
					[query] => string
					[createTable] => srting - eg. CRM_LISTA_ZASOBOW
					[createArgs] => array - eg. [ TYPE: 'TYPESPECIALS', DESC: 'Typespecials' ]
					[in] => array - eg. [ ':ts_main_id', ':url_action_name' ]
					[out] => array - eg. [ ':ts_main_id': 'ID' ]
				*/
				echo ">>>> Loop({$i}): idx({$idx}) Zasob::assert({$conf->createTable}): " . json_encode($conf->createArgs) . " returns: " . json_encode($conf->out) . "\n";
				$canExecute = true;
				if (!empty($conf->in)) foreach ($conf->in as $argName) {
					if (!array_key_exists($argName, $knownArgsMap)) {
						$canExecute = false;
					}
				}
				if ($canExecute) {
					// TODO: execute - $r = $this->_assertZasobConf($conf, $knownArgsMap);
					// fetch params out to $knownArgsMap
					foreach ($conf->out as $argName => $outFieldName) {
						$knownArgsMap[$argName] = $simulateGeneratedId++;// TODO: fetch from returned object ($r->{$outFieldName})
					}
					$todoIdx = array_diff($todoIdx, array($idx));// remove $idx from $todoIdx
					echo ">>>> Loop({$i}): knownArgsMap: " . json_encode($knownArgsMap) . " \n";
					$isExecuted = true;
				} else {
					echo ">>>> Loop({$i}): cannot execute conf [{$idx}] missing args: " . json_encode($conf->in) . " \n";
				}
			}
			if (!$isExecuted) {
				echo "DBG: Stop";
				if (empty($todoIdx)) {
					echo " - OK";
				} else {
					echo " - Error - TODO:\n";
					foreach ($todoIdx as $idx) {
						$conf = $flatConfig[$idx];
						echo "Zasob::assert({$conf->createTable}): " . json_encode($conf->createArgs) . " returns: " . json_encode($conf->out) . "\n";
					}
				}
				break;
			}
		}
		echo '
'; } public function fetchZasobInfo() { $pdo = DB::getPDO(); $args = $this->getArgsList(); $clsName = __CLASS__; $urlActionName = str_replace('Route_UrlAction_', '', $clsName); $sth = $pdo->prepare(" select zp.ID as ts_id , IF(z.ID is null, 1, 0) as action_id_is_null , z.ID as action_id , z.DESC as action_desc from CRM_LISTA_ZASOBOW zp left join CRM_LISTA_ZASOBOW z on(z.PARENT_ID = zp.ID and z.TYPE = 'URL_ACTION' and z.DESC = :url_action_name and z.A_STATUS not in('DELETED') ) where zp.TYPE = 'TYPESPECIALS' and zp.A_STATUS not in('DELETED') "); $sth->bindValue('url_action_name', $urlActionName); $sth->execute(); $info = $sth->fetch(); if (!$info) return $info; $info['url_params'] = array(); $info['links'] = array();// where action is installed - expected `IN7_MK_BAZA_DYSTRYBUCJI` if ($info['action_id'] > 0) { $sthParams = $pdo->prepare(" select z.ID as param_id , z.DESC as param_desc from CRM_LISTA_ZASOBOW z where z.TYPE = 'PARAM_IN' and z.A_STATUS not in('DELETED') and z.PARENT_ID = :url_action_id "); $sthParams->bindValue('url_action_id', $info['action_id']); $sthParams->execute(); $urlParams = array(); $rawUrlParams = $sthParams->fetchAll(); foreach ($rawUrlParams as $urlParam) { $urlParams[$urlParam['param_desc']] = $urlParam; } $info['url_params'] = $urlParams; } { $main_db_id = $pdo->getZasobId(); $sth_dbInfo = $pdo->prepare(" select za.id as link_action_id , zd.id as db_id , zt.id as tbl_id , zi.id as cell_id , zp1.id as param1_id -- for every param from CRM_LISTA_ZASOBOW zt join CRM_LISTA_ZASOBOW zd on(zd.ID = zt.PARENT_ID and zd.TYPE in('DATABASE_MYSQL', 'BAZA_DANYCH', 'DATABASE_POSTGRESQL') and zd.ID = {$main_db_id} ) join CRM_LISTA_ZASOBOW zi on(zi.PARENT_ID = zt.ID and zi.TYPE = 'KOMORKA' and zi.DESC = 'ID' ) left join CRM_LISTA_ZASOBOW za on(za.PARENT_ID = zt.ID and za.TYPE = 'URL_ACTION' and za.ALIAS_ID = {$info['action_id']} ) -- TODO: for every param: left join CRM_LISTA_ZASOBOW zp1 on(zp1.PARENT_ID = za.ID and zp1.TYPE = 'PARAM_IN' and zp1.ALIAS_ID = zi.id and zp1.DESC = 'ID_PROJECT' ) where zt.TYPE = 'TABELA' and zt.A_STATUS not in('DELETED') and zt.DESC = 'IN7_MK_BAZA_DYSTRYBUCJI' "); DBG::_('DBG', '>1', "dbInfo query", $pdo->getRawSql($sth_dbInfo), __CLASS__, __FUNCTION__, __LINE__); $sth_dbInfo->execute(); $linkInfo = $sth_dbInfo->fetch(); if ($linkInfo) { $info['links']['IN7_MK_BAZA_DYSTRYBUCJI'] = $linkInfo; } } return $info; } public function kosztorysXmlAction() { $idProject = 1921; $schema = $this->getSchema(); $data = $this->_fetchKosztorysData($idProject); //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(); $xmlWriter->openUri('php://output'); $xmlWriter->setIndent(true); if ($xmlWriter) { $xmlWriter->startDocument('1.0','UTF-8'); //$xmlWriter->startElementNS(null, 'kosztorysy', 'https://biuro.biall-net.pl/wfs');// adds @xmlns=... $xmlWriter->startElement('kosztorysy'); $xmlWriter->writeAttribute('targetNamespace', 'https://biuro.biall-net.pl/wfs'); $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->getSchema(); $data = $this->_fetchKosztorysData($idProject); $xmlWriter->startElement('kosztorys'); $xmlWriter->startElement('projekt'); $xmlWriter->writeAttribute('id', $idProject); $xmlWriter->startElement('projekt'); $xmlWriter->text("TODO L." . __LINE__); $xmlWriter->endElement(); $xmlWriter->endElement(); $xmlWriter->endElement(); } /* $memXmlWriter = new XMLWriter(); $memXmlWriter->openMemory(); $memXmlWriter->setIndent(true); for ($i = 1; $i <= 10; $i++) { $idProject += 1; $schema = $this->getSchema(); $data = $this->_fetchKosztorysData($idProject); $memXmlWriter->startElement('kosztorys'); $memXmlWriter->writeAttribute('id', $idProject); $memXmlWriter->writeAttributeNS('p5', 'typeName', 'https://biuro.biall-net.pl/wfs', 'Kosztorys'); $memXmlWriter->text('book_'.$i); $memXmlWriter->endElement(); if ($i % 5 == 0) { $batchXmlString = $memXmlWriter->outputMemory(true); $xmlWriter->writeRaw($batchXmlString); } } $memXmlWriter->flush(); unset($memXmlWriter); */ $xmlWriter->endElement(); $xmlWriter->endDocument(); } } public function panel($idProject, $idCompany = 0) { 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 $task = V::get('_task', '', $_GET); ?>
getSchema(); ?>
1', "args", $args, __CLASS__, __FUNCTION__, __LINE__); if (!$admin) return; $cennik = $this->getCennik($idProject, $idCompany); DBG::_('DBG', '>1', "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__); $pdo = DB::getPDO(); { $add_id_zasob = 0; $add_unit = ''; $add_price = 0; $add_quantity = 0; $add__sth = $pdo->prepare(" insert into CRM_LISTA_ZASOBOW_OFFERS ( CRM_LISTA_ZASOBOW_ID , COMPANIES_ID , ID_PROJECT , OFFER_PRICE_PER_RESOURCE_UNIT , RESOURCE_UNIT_TYPE , OFFER_UNIT_TYPE , REQUIRED_RESOURCE_UNITS , A_RECORD_CREATE_AUTHOR , A_RECORD_CREATE_DATE ) values ( :id_zasob , :id_company , :id_project , :price , :unit , :unit , :quantity , :author , NOW() ) "); $add__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR); $add__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT); $add__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT); $add__sth->bindValue(':quantity', $add_quantity, PDO::PARAM_STR); $add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_INT); $add__sth->bindParam(':price', $add_price, PDO::PARAM_STR); $add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR); } { $edit_id_company = $idCompany; $edit_id_zasob = 0; $edit_unit = ''; $edit_price = 0; $edit_author = 0; $edit_quantity = 0; $edit__sth = $pdo->prepare(" update CRM_LISTA_ZASOBOW_OFFERS set CRM_LISTA_ZASOBOW_ID = :id_zasob , COMPANIES_ID = :id_company , ID_PROJECT = :id_project , OFFER_PRICE_PER_RESOURCE_UNIT = :price , RESOURCE_UNIT_TYPE = :unit , OFFER_UNIT_TYPE = :unit , REQUIRED_RESOURCE_UNITS = :quantity , A_RECORD_UPDATE_AUTHOR = :author , A_RECORD_UPDATE_DATE = NOW() where ID = :id "); $edit__sth->bindParam(':id', $edit_id, PDO::PARAM_INT); $edit__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR); $edit__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT); $edit__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT); $edit__sth->bindValue(':quantity', $edit_quantity, PDO::PARAM_STR); $edit__sth->bindParam(':id_zasob', $edit_id_zasob, PDO::PARAM_INT); $edit__sth->bindParam(':price', $edit_price, PDO::PARAM_STR); $edit__sth->bindParam(':unit', $edit_unit, PDO::PARAM_STR); } $schema = $this->getSchema(); foreach ($schema['config']['layer'] as $idLayer => $layData) { DBG::_('DBG', '>1', "layData", $layData, __CLASS__, __FUNCTION__, __LINE__); foreach ($layData['type'] as $idType => $typeLabel) { $edit_id = V::get("edit_price_id_{$idType}", '', $args); $add_price = V::get("price_{$idType}", '', $args); DBG::_('DBG', '>1', "typeLabel (edit={$edit_id}, price={$add_price})", $typeLabel, __CLASS__, __FUNCTION__, __LINE__); if ($edit_id > 0) { $edit_price = V::get("price_{$idType}", '', $args); $edit_price = str_replace(',', '.', $edit_price); if ($edit_id > 0 && $edit_price > 0) { $edit_id_zasob = $idType; $edit_unit = $layData['jednostka']; // TODO: check if anything change $edit__sth->execute(); // TODO: add to hist } } else { $add_price = V::get("price_{$idType}", '', $args); $add_price = str_replace(',', '.', $add_price); if ($add_price > 0) { $add_id_zasob = $idType; $add_unit = $layData['jednostka']; $add__sth->execute(); } } } } } public function saveDefaultOffer($args) { DBG::_('DBG', '>1', "args", $args, __CLASS__, __FUNCTION__, __LINE__); $cennik = $this->getDefaultCennik(); DBG::_('DBG', '>1', "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__); $idCompany = 0; $idProject = 0; $pdo = DB::getPDO(); { $add_id_zasob = 0; $add_unit = ''; $add_price = 0; $add_quantity = 0; $add__sth = $pdo->prepare(" insert into CRM_LISTA_ZASOBOW_OFFERS ( CRM_LISTA_ZASOBOW_ID , COMPANIES_ID , ID_PROJECT , OFFER_PRICE_PER_RESOURCE_UNIT , RESOURCE_UNIT_TYPE , OFFER_UNIT_TYPE , REQUIRED_RESOURCE_UNITS , A_RECORD_CREATE_AUTHOR , A_RECORD_CREATE_DATE ) values ( :id_zasob , :id_company , :id_project , :price , :unit , :unit , :quantity , :author , NOW() ) "); $add__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR); $add__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT); $add__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT); $add__sth->bindValue(':quantity', $add_quantity, PDO::PARAM_STR); $add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_INT); $add__sth->bindParam(':price', $add_price, PDO::PARAM_STR); $add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR); } { $edit_id_zasob = 0; $edit_unit = ''; $edit_price = 0; $edit_author = 0; $edit_quantity = 0; $edit__sth = $pdo->prepare(" update CRM_LISTA_ZASOBOW_OFFERS set CRM_LISTA_ZASOBOW_ID = :id_zasob , COMPANIES_ID = :id_company , ID_PROJECT = :id_project , OFFER_PRICE_PER_RESOURCE_UNIT = :price , RESOURCE_UNIT_TYPE = :unit , OFFER_UNIT_TYPE = :unit , REQUIRED_RESOURCE_UNITS = :quantity , A_RECORD_UPDATE_AUTHOR = :author , A_RECORD_UPDATE_DATE = NOW() where ID = :id "); $edit__sth->bindParam(':id', $edit_id, PDO::PARAM_INT); $edit__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR); $edit__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT); $edit__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT); $edit__sth->bindValue(':quantity', $edit_quantity, PDO::PARAM_STR); $edit__sth->bindParam(':id_zasob', $edit_id_zasob, PDO::PARAM_INT); $edit__sth->bindParam(':price', $edit_price, PDO::PARAM_STR); $edit__sth->bindParam(':unit', $edit_unit, PDO::PARAM_STR); } $schema = $this->getSchema(); foreach ($schema['config']['layer'] as $idLayer => $layData) { DBG::_('DBG', '>1', "layData", $layData, __CLASS__, __FUNCTION__, __LINE__); foreach ($layData['type'] as $idType => $typeLabel) { DBG::_('DBG', '>1', "typeLabel", $typeLabel, __CLASS__, __FUNCTION__, __LINE__); $edit_id = V::get("edit_price_id_{$idType}", '', $args); if ($edit_id > 0) { $edit_price = V::get("price_{$idType}", '', $args); $edit_price = str_replace(',', '.', $edit_price); if ($edit_id > 0 && $edit_price > 0) { $edit_id_zasob = $idType; $edit_unit = $layData['jednostka']; // TODO: check if anything change $edit__sth->execute(); // TODO: add to hist } } else { $add_price = V::get("price_{$idType}", '', $args); $add_price = str_replace(',', '.', $add_price); if ($add_price > 0) { $add_id_zasob = $idType; $add_unit = $layData['jednostka']; $add__sth->execute(); } } } } } /** * @returns [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ] */ public function getDefaultCennik($idCompany = 0) { $schema = $this->getSchema(); $typeIdList = array_keys($schema['config']['type']); $sqlTypeIdList = implode(',', $typeIdList); $pdo = DB::getPDO(); $sth = $pdo->prepare(" select o.ID , o.CRM_LISTA_ZASOBOW_ID as id_zasob , o.COMPANIES_ID as id_company , o.ID_PROJECT as id_project , o.OFFER_PRICE_PER_RESOURCE_UNIT as price , o.RESOURCE_UNIT_TYPE as unit -- , o.OFFER_UNIT_TYPE as unit , o.REQUIRED_RESOURCE_UNITS as quantity from CRM_LISTA_ZASOBOW_OFFERS o where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList}) -- and o.A_STATUS not in ('DELETED') and o.ID_PROJECT = 0 and o.COMPANIES_ID = :id_company "); $sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT); $sth->execute(); $cennikRaw = $sth->fetchAll(); $cennik = array(); foreach ($cennikRaw as $itemRaw) { $item = $itemRaw; $item['price'] = round($item['price'], 2); if (!empty($cennik[$itemRaw['id_zasob']])) { if ($itemRaw['ID'] < $cennik[$itemRaw['id_zasob']]['ID']) continue; } $cennik[$item['id_zasob']] = $item; } return $cennik; } /** * @returns [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ] */ public function getCennik($idProject, $idCompany = 0) { $schema = $this->getSchema(); $typeIdList = array_keys($schema['config']['type']); $sqlTypeIdList = implode(',', $typeIdList); $pdo = DB::getPDO(); $sth = $pdo->prepare(" select o.ID , o.CRM_LISTA_ZASOBOW_ID as id_zasob , o.COMPANIES_ID as id_company , o.ID_PROJECT as id_project , o.OFFER_PRICE_PER_RESOURCE_UNIT as price , o.RESOURCE_UNIT_TYPE as unit -- , o.OFFER_UNIT_TYPE as unit , o.REQUIRED_RESOURCE_UNITS as quantity from CRM_LISTA_ZASOBOW_OFFERS o where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList}) -- and o.A_STATUS not in ('DELETED') and o.ID_PROJECT = :id_project and o.COMPANIES_ID = :id_company "); $sth->bindValue(':id_project', $idProject, PDO::PARAM_INT); $sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT); $sth->execute(); $cennikRaw = $sth->fetchAll(); $cennik = array(); foreach ($cennikRaw as $itemRaw) { $item = $itemRaw; $item['price'] = round($item['price'], 2); $cennik[$itemRaw['id_zasob']] = $item; } return $cennik; } public function defaultOferta($idCompany = 0) { if ('1' == V::get('save_offer', '', $_POST)) { $this->saveDefaultOffer($_POST); } $schema = $this->getSchema(); if (empty($schema['config']['type'])) throw new Exception("Schema error - brak zdefiniowanych typów"); $cennik = $this->getDefaultCennik($idCompany); //DBG::_(true, true, "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__); ?>
$layData) : ?>

$typeLabel) : ?>
id zasobu typ jednotka miary cena jednostkowa

:numeric, 'sub_costs' => [ $id_zasob => 'price' ] ] */ public function getProjectCostForCennik($idProject, $idCompany = 0) { $projCost = array(); $projCost['cost_total'] = 0; $projCost['sub_costs'] = array(); $defCennik = $this->getDefaultCennik($idCompany); // $defCennik = [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ] $cennik = $this->getCennik($idProject, $idCompany); $data = $this->getData($idProject, $admin); foreach ($data as $idLayer => $layData) { foreach ($layData['data'] as $typeData) { if (!$typeData['idType']) continue; $price = (!empty($cennik[$typeData['idType']]['price']))? $cennik[$typeData['idType']]['price'] : 0; $defPrice = (!empty($defCennik[$typeData['idType']]['price']))? $defCennik[$typeData['idType']]['price'] : 0; $cost = ($price > 0)? $price : $defPrice; $cost = $typeData['ilosc'] * $cost; $projCost['sub_costs'][$typeData['idType']] = $cost; $projCost['cost_total'] += $cost; } } return $projCost; } public function kosztorys($idProject) { $schema = $this->getSchema(); $data = $this->_fetchKosztorysData($idProject); $projCost = $this->getProjectCostForCennik($idProject, $idCompany = 0); ?>

Kosztorys wstępny robót telekomunikacyjnych

viewLayersData($idProject, $idCompany = 0, $admin = false, $projCost['sub_costs']); ?>
0', "data", $data, __CLASS__, __FUNCTION__, __LINE__); DBG::_('DBG', '>0', "schema", $schema, __CLASS__, __FUNCTION__, __LINE__); DBG::_('DBG', '>0', "projCost", $projCost, __CLASS__, __FUNCTION__, __LINE__); } public function oferta($idProject, $idCompany = 0, $admin = false) { if ($admin && '1' == V::get('save_offer', '', $_POST)) { $this->saveOffer($idProject, $idCompany, $_POST, $admin); } ?>
viewLayersData($idProject, $idCompany, $admin); ?>
getSchema(); $conf = $schema['config']; DBG::_('DBG', '>1', "conf", $conf, __CLASS__, __FUNCTION__, __LINE__); $data = $this->getData($idProject, $admin); DBG::_('DBG', '>1', "data", $data, __CLASS__, __FUNCTION__, __LINE__); $defCennik = $this->getDefaultCennik($idCompany); $cennik = $this->getCennik($idProject, $idCompany); ?> $layData) : ?>

typ ilość jednotka miary cena jednostkowa cena jednostkowa (domyślna) szacowany koszt [zł]
' . "Popraw" . '' . " (dane dla projektu [{$idProject}])"; $fixAllLink = '' . "Popraw wszystkie" . ''; SE_Layout::alert('danger', "Niezdefiniowany typ: {$typeData['type']} - {$fixProjectLink}, {$fixAllLink}"); } ?> 1', "Error conf", $conf, __CLASS__, __FUNCTION__, __LINE__); ?> 1', "Error", $typeData, __CLASS__, __FUNCTION__, __LINE__); ?>
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']; } } } foreach ($rawConf as $z) { 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'] = ''; $layer['type'] = array(); $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 */ switch ($z['c_DESC']) { case 'ZASOB': { 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; } } break; case 'ZASOB_ID': { 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; $conf['layer'][$z['ID']]['type'][$z['za_ID']] = $z['za_DESC']; } } break; case 'TYPE': { //DBG::_(true, true, "z", $z, __CLASS__, __FUNCTION__, __LINE__); if (empty($z['c_ALIAS_ID'])) throw new Exception("Schema error - brak ALIAS_ID dla typu"); if (empty($conf['type'][$z['za_ID']])) ;// TODO: throw exception $conf['layer'][$z['ID']]['type'][$z['za_ID']] = $z['za_DESC']; } break; } } } } foreach ($rawConf as $z) { if ('INNE' == $z['TYPE']) { } else if ('TABELA' == $z['TYPE']) { } 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 - show 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"; $layerConf['_agr_fields_to_cols'] = array(); $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; } { $layerConf = array(); $layerConf['label'] = "Mikrokanalizacja do klienta"; $layerConf['_agr_fields_to_cols'] = array(); $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; } { $layerConf = array(); $layerConf['label'] = "Przeciski"; $layerConf['_agr_fields_to_cols'] = array(); $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; } { $layerConf = array(); $layerConf['label'] = "Zabruki"; $layerConf['_agr_fields_to_cols'] = array(); $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; } { $layerConf = array(); $layerConf['label'] = "Światłowód"; $layerConf['_agr_fields_to_cols'] = array(); $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ł]"; { $layerSubCostsConf = array(); $layerSubCostsConf['group_by_field'] = 'wlokien_j'; $layerSubCostsConf['sql_agr_func'] = array(); $layerSubCostsConf['sql_agr_func']['sum_dlugosc'] = array();//sum(Dlugosc) as sum_dlugosc $layerSubCostsConf['sql_agr_func']['sum_dlugosc']['func'] = 'sum'; $layerSubCostsConf['sql_agr_func']['sum_dlugosc']['field'] = 'Dlugosc'; $layerSubCostsConf['labels'] = array(); $layerSubCostsConf['labels']['cost_type'] = "rodzaj kosztu"; $layerSubCostsConf['labels']['sum_dlugosc'] = "suma długości"; $layerConf['sub_costs'] = $layerSubCostsConf; } $_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; } { $layerConf = array(); $layerConf['label'] = "Rury osłonowe - TODO"; $layerConf['_agr_fields_to_cols'] = array(); $_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; } { $layerConf = array(); $layerConf['label'] = "Koszty dodatkowe - TODO"; $layerConf['_agr_fields_to_cols'] = array(); $_schema['sub_costs']['__KOSZTY_DODATKOWE__'] = $layerConf; } return $_schema; } public function getData($idProject, $admin = 0) { static $_data = null; if (!is_array($_data)) $_data = array(); if (!array_key_exists($idProject, $_data)) $_data[$idProject] = $this->fetchData($idProject, $admin); return $_data[$idProject]; } public function fetchData($idProject, $admin = 0) { $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; $idType = 0; $iloscType = null; DBG::_('DBG', '>1', "layer", $layer, __CLASS__, __FUNCTION__, __LINE__); DBG::_('DBG', '>1', "conf", $conf, __CLASS__, __FUNCTION__, __LINE__); if ('ZASOB' == $layer['zasob_type']) { $iloscType = array(); $iloscType['type'] = trim($d[$sqlZasobField]); $idType = array_search($iloscType['type'], $conf['type']); $iloscType['idType'] = $idType; $iloscType['jednostka'] = $layer['jednostka']; $iloscType['ilosc'] = $d['ilosc']; } else if ('ZASOB_ID' == $layer['zasob_type']) { $iloscType = array(); $iloscType['type'] = $layer['zasob_label']; $idType = (array_key_exists($layer['zasob_id'], $conf['type']))? $layer['zasob_id'] : null; $iloscType['idType'] = $idType; $iloscType['jednostka'] = $layer['jednostka']; $iloscType['ilosc'] = $d['ilosc']; } else { // TODO: BUG } if (!empty($iloscType) && $idType) { if (!array_key_exists($idType, $iloscByType)) { $iloscByType[$idType] = $iloscType; } else { DBG::_('DBG', '>1', "merge stats {$idType}", array($iloscByType[$idType], $iloscType), __CLASS__, __FUNCTION__, __LINE__); $iloscByType[$idType]['ilosc'] += $iloscType['ilosc']; } } else { DBG::_('DBG', '>1', "skip stats", $d, __CLASS__, __FUNCTION__, __LINE__); if ($admin) SE_Layout::alert('warning', "Pomijanie nieznanego oznaczenia '{$iloscType['type']}' w ilości {$iloscType['ilosc']} - warstwa '{$layer['label']}' ({$layer['tabela_name']})"); } } if ($hasIlosc) $layData['data'] = $iloscByType; } if (!empty($layData['data'])) $data[$idLayer] = $layData; } return $data; } public function _fetchKosztorysData($idProject) { $project = array(); $schema = $this->getSchema(); if(0){// TEST join by ogc $exampleOgcJoin = << p/ID {$idProject} u/ADM_ACCOUNT p/L_APPOITMENT_USER OGC_JOIN; } $pdo = DB::getPDO(); { $sth = $pdo->prepare(" select p.ID, p.M_DIST_DESC, p.L_APPOITMENT_USER , p.koszt_wspolny , p.koszt_na_budynek , p.koszt_na_mieszkanie , p.Agr_Rozdzielcza_Wykop_przedmiar_na_mikrorurki_ilosc , p.Agr_Rozdzielcza_Wykop_przedmiar_na_mikrorurki_cena , p.Agr_Rozdzielcza_Wykop_przedmiar_na_mikrorurki_koszt , p.Agr_Rozdzielcza_Mikrokanalizacja_do_klienta_ilosc , p.Agr_Rozdzielcza_Mikrokanalizacja_do_klienta_cena , p.Agr_Rozdzielcza_Mikrokanalizacja_do_klienta_koszt , p.Agr_metrow_mikrorurek_5szt , p.Agr_metrow_mikrorurek_5szt_cena , p.Agr_Rozdzielcza_Przeciski_110mm_ilosc , p.Agr_Rozdzielcza_Przeciski_110mm_cena , p.Agr_Rozdzielcza_Przeciski_110mm_koszt , p.Agr_Rozdzielcza_Zabruki_ilosc , p.Agr_Rozdzielcza_Zabruki_cena , p.Agr_Rozdzielcza_Zabruki_koszt , p.Agr_Rozdzielcza_wezly_ilosc , p.Agr_Rozdzielcza_wezly_cena , p.Agr_Rozdzielcza_wezly_koszt , p.Agr_Rozdzielcza_koszty_dodatkowe_wsg84 , p.Agr_Rozdzielcza_rurociag_wsg84_ilosc , p.Agr_Rozdzielcza_rurociag_wsg84_cena , p.Agr_Rozdzielcza_rurociag_wsg84_koszt , p.Agr_Rozdzielcza_Kabel_Swiatlowodowy_wsg84_ilosc , p.Agr_Rozdzielcza_Kabel_Swiatlowodowy_wsg84_cena , p.Agr_Rozdzielcza_Kabel_Swiatlowodowy_wsg84_koszt , p.Agr_USERS2_MARKETING_ilosc , p.Agr_USERS2_MARKETING_cena , p.Agr_USERS2_MARKETING_koszt , p.Agr_BUILDINGS_ilosc from IN7_MK_BAZA_DYSTRYBUCJI p where p.ID = :ID_PROJECT -- TODO: check perms! "); $sth->bindValue('ID_PROJECT', $idProject, PDO::PARAM_INT); $sth->execute(); $projectList = $sth->fetchAll(); if (empty($projectList)) throw new Exception("404 - Project Not Found"); $projectRaw = reset($projectList); } { $project['nr'] = $projectRaw['ID']; $project['title'] = $projectRaw['M_DIST_DESC']; $project['owner_name'] = $this->fetchUserName($projectRaw['L_APPOITMENT_USER']); $project['cost_total'] = $projectRaw['koszt_wspolny']; foreach ($schema['sub_costs'] as $layerName => $layerConf) { $values = array(); foreach ($layerConf['_agr_fields_to_cols'] as $fldName => $label) { $values[$fldName] = V::get("Agr_{$layerName}_{$fldName}", '', $projectRaw); } $project['sub_costs'][$layerName] = $values; if (!empty($layerConf['sub_costs'])) { $sub_costs = array(); { $groupByField = $layerConf['sub_costs']['group_by_field']; $sqlFields = array(); foreach ($layerConf['sub_costs']['sql_agr_func'] as $fldName => $funcConf) { $sqlFuncName = $funcConf['func']; $sqlFuncField = $funcConf['field']; $sqlFields[] = "{$sqlFuncName}(l.{$sqlFuncField}) as {$fldName}"; } $sqlFields = implode(", ", $sqlFields); $sth = $pdo->prepare(" select l.{$groupByField} as cost_type , $sqlFields from Rozdzielcza_Kabel_Swiatlowodowy_wsg84 l where l.ID_PROJECT = :ID_PROJECT group by l.{$groupByField} "); $sth->bindValue('ID_PROJECT', $idProject, PDO::PARAM_INT); $sth->execute(); $sub_costs = $sth->fetchAll(); } $project['sub_costs'][$layerName]['sub_costs'] = $sub_costs; } } } $project['_raw'] = $projectRaw; return $project; } public function fetchUserName($userLogin) { $userName = $userLogin; // TODO: sql from ADMIN_USERS return $userName; } }