فهرست منبع

added UrlActions and Kosztorys test

Piotr Labudda 10 سال پیش
والد
کامیت
8036bccc0a
4فایلهای تغییر یافته به همراه467 افزوده شده و 0 حذف شده
  1. 38 0
      SE/se-lib/Core/Pdo.php
  2. 45 0
      SE/se-lib/DB.php
  3. 108 0
      SE/se-lib/Route/UrlAction.php
  4. 276 0
      SE/se-lib/Route/UrlAction/ProjektyKosztyWstepnychRobot.php

+ 38 - 0
SE/se-lib/Core/Pdo.php

@@ -0,0 +1,38 @@
+<?php
+
+class Core_Pdo {
+
+	private $pdo;
+
+	public function __construct(PDO $pdo) {
+		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+		$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
+		$this->pdo = $pdo;
+	}
+
+	public function getPDO() {
+		return $this->pdo;
+	}
+
+	//public PDOStatement PDO::prepare ( string $statement [, array $driver_options = array() ] )
+	public function prepare($statement, $driver_options = array()) {
+		return $this->pdo->prepare($statement, $driver_options);
+	}
+	// public PDOStatement query ( string $statement )
+	public function query($statement) {
+		return $this->pdo->query($statement);
+	}
+	// public string lastInsertId ([ string $name = NULL ] )
+	public function lastInsertId() {
+		return $this->pdo->lastInsertId();
+	}
+	// public int exec ( string $statement )
+	public function exec($statement) {
+		return $this->pdo->exec($statement);
+	}
+	// public string quote ( string $string [, int $parameter_type = PDO::PARAM_STR ] )
+	public function quote($string, $parameter_type = PDO::PARAM_STR) {
+		return $this->pdo->quote($string, $parameter_type);
+	}
+
+}

+ 45 - 0
SE/se-lib/DB.php

@@ -2,6 +2,7 @@
 
 Lib::loadClass('Config');
 Lib::loadClass('DataSourceException');
+Lib::loadClass('Core_Pdo');
 
 class DB {
 
@@ -118,6 +119,50 @@ class DB {
 		return $_instance[$dbConfName];
 	}
 
+	public static function getPDO($db = null) {
+		static $_instance;
+		if (!is_array($_instance)) $_instance = array();
+		$dbConfName = 'default_db';
+		if (is_numeric($db) && $db > 0) {
+			$dbConfName = "zasob_{$db}";
+		} else if ($db == 'import_db') {
+			$dbConfName = "import_db";
+		} else if ($db == 'test_db') {
+			$dbConfName = "test_db";
+		}  else if ($db == 'billing_db') {
+			$dbConfName = "billing_db";
+		}
+
+		if (!array_key_exists($dbConfName, $_instance)) {
+			$_instance[$dbConfName] = null;
+
+			Lib::loadClass('Config');
+			$conf = Config::getConfFile($dbConfName);
+			if (!$conf) throw new Exception("Config file for db {$dbConfName} not exists!");
+			$type = V::get('type', 'mysql', $conf);
+			$host = V::get('host', '', $conf);
+			$port = V::get('port', '', $conf);
+			$user = V::get('user', '', $conf);
+			$pass = V::get('pass', '', $conf);
+			$zasob_id = V::get('zasob_id', '', $conf);
+
+			$database = V::get('database', '', $conf);
+			if ($port && $host) $host .= ";port={$port}";
+			$names = 'utf8';
+			if(0){// TODO: mssql tdsver
+				$tdsver = V::get('tdsver', '', $conf);
+				if (!empty($tdsver)) {
+					$params['tdsver'] = $tdsver;
+				}
+			}
+			$pdo = new PDO($type . ':host=' . $host . ';dbname=' . $database, $user, $pass);
+			$pdo->exec("SET NAMES 'utf8'");
+			$sdb = new Core_Pdo($pdo);
+			$_instance[$dbConfName] = $sdb;
+		}
+		return $_instance[$dbConfName];
+	}
+
 
 public static function connect() {
 	static $conn;

+ 108 - 0
SE/se-lib/Route/UrlAction.php

@@ -0,0 +1,108 @@
+<?php
+
+Lib::loadClass('RouteBase');
+
+/*
+## Url Actions (to replace Typespecial's):
+
+- new Zasob type: 'URL_ACTION'
+
+## Config in Zasoby tree (TODO: upload from gui xml file):
+[100] - TYPESPECIALS
+[110]   - URL_ACTION ProjektyKosztyWstepnychRobot
+[111] 	  - PARAM_IN ID_PROJECT
+[200] - TABELA IN7_MK_BAZA_DYSTRYBUCJI Projekty
+[210]   - KOMORKA ID
+[220]   - (ALIAS to [110]) URL_ACTION action name/label
+[221] 	  - (ALIAS to [210]) PARAM_IN ID_PROJECT
+class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {
+	public function defaultAction() {
+		$args = array();
+		$args['ID_PROJECT'] = V::get('ID_PROJECT', '', $_REQUEST);// [111]
+	}
+}
+
+## Process - Create:
+- 1. define action name in Zasoby tree (URL_ACTION) and expected args (PARAM_IN) - `ActionDefinition`
+- 2. create action alias inside TABLE - `ActionInstance`
+- 3. set perms for `ActionInstance`
+
+## Process - Install ActionDefinitio's:
+- class UrlActionBase :: installZasobAction
+  - create required Zasoby in Zasob Tree if not exists
+
+*/
+class Route_UrlAction extends RouteBase {// TODO: UrlActionBase
+
+	public function handleAuth() {
+		if (!User::logged()) {
+			throw new HttpException('Unauthorized', 401);
+		}
+	}
+
+	public function defaultAction() {
+		SE_Layout::gora();
+		?>
+<div class="container">
+	<h1>UrlActions system</h1>
+	...
+</div>
+		<?php
+		SE_Layout::dol();
+	}
+
+	public function getArgsList() {
+		$args = array();
+		$args[] = 'ID_PROJECT';
+		return $args;
+	}
+
+	public function reinstallAction() {
+		$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 runAction() {
+		$jsonData = new stdClass();
+		$jsonData->type = 'success';
+		$jsonData->msg = 'Gotowe';
+		try {
+			$idAction = V::get('_idAction', 0, $_REQUEST, 'int');
+			if ($idAction > 0) {
+				$this->runActionById($idAction);
+			}
+		} catch (Exception $e) {
+			$jsonData->type = 'danger';
+			$jsonData->msg = $e->getMessage();
+		}
+		echo json_encode($jsonData);
+	}
+
+	public function reinstall() {
+		$sqlList = array();
+		//$sqlList['RemoveTable'] = "DROP TABLE IF EXISTS `CRM_UI_MSGS`";
+		$pdo = DB::getPDO();
+		foreach ($sqlList as $sqlName => $sql) {
+			$pdo->exec($sql);
+		}
+	}
+
+	public function runActionById($id) {
+		$msgRow = $this->getActiveMessage($id);
+		$execNotes = '';
+		if (!empty($msgRow->app_className)) {
+			$route = Router::getRoute($msgRow->app_className);
+			$route->runByMessageFromMsgsSystem($msgRow->msg, $execNotes);
+		}
+		$this->forceFinishMessage($id, $execNotes);
+	}
+
+}

+ 276 - 0
SE/se-lib/Route/UrlAction/ProjektyKosztyWstepnychRobot.php

@@ -0,0 +1,276 @@
+<?php
+
+Lib::loadClass('RouteBase');
+
+class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: UrlActionBase @see Route_UrlAction
+
+	public function handleAuth() {
+		if (!User::logged()) {
+			throw new HttpException('Unauthorized', 401);
+		}
+	}
+
+	public function defaultAction() {
+		// 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->kosztorys($idProject);
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+		}
+		SE_Layout::dol();
+	}
+
+	public function getArgsList() {
+		$args = array();
+		$args[] = 'ID_PROJECT';
+		return $args;
+	}
+
+	public function kosztorys($idProject) {
+		$schema = $this->_getKosztorysSchema();
+		$data = $this->_fetchKosztorysData($idProject);
+?>
+<div class="container">
+	<h1>Kosztorys wstępnych robót telekomunikacyjnych</h1>
+	<table class="table">
+		<tr>
+			<th><?php echo $schema['nr']; ?></th>
+			<th><?php echo $schema['title']; ?></th>
+			<th><?php echo $schema['owner']; ?></th>
+			<th><?php echo $schema['cost_total']; ?></th>
+		</tr>
+		<tr>
+			<td><?php echo $data['nr']; ?></td>
+			<td><?php echo $data['title']; ?></td>
+			<td><?php echo $data['owner']; ?></td>
+			<td><?php echo $data['cost_total']; ?></td>
+		</tr>
+	</table>
+	<?php foreach ($schema['sub_costs'] as $layerName => $layerConf) : ?>
+		<h4><?php echo $layerConf['label']; ?>:</h4>
+		<table class="table" style="width:auto">
+			<tr>
+				<?php foreach ($layerConf['_agr_fields_to_cols'] as $fldName => $label) : ?>
+					<th><?php echo $label; ?></th>
+				<?php endforeach; ?>
+			</tr>
+			<tr>
+				<?php foreach ($layerConf['_agr_fields_to_cols'] as $fldName => $label) : ?>
+					<td><?php echo V::get($fldName, '', $data['sub_costs'][$layerName]); ?></td>
+				<?php endforeach; ?>
+			</tr>
+		</table>
+		<?php if (!empty($layerConf['sub_costs'])) : ?>
+			<table class="table" style="width:auto">
+				<tr>
+					<?php foreach ($layerConf['sub_costs']['labels'] as $fldName => $label) : ?>
+						<th><?php echo $label; ?></th>
+					<?php endforeach; ?>
+				</tr>
+				<?php foreach ($data['sub_costs'][$layerName]['sub_costs'] as $subCost) : ?>
+					<tr>
+						<?php foreach ($layerConf['sub_costs']['labels'] as $fldName => $label) : ?>
+							<td><?php echo V::get($fldName, '', $subCost); ?></td>
+						<?php endforeach; ?>
+					</tr>
+				<?php endforeach; ?>
+			</table>
+		<?php endif; ?>
+	<?php endforeach; ?>
+</div>
+		<?php
+		DBG::_('DBG', '>1', "data", $data, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG', '>1', "schema", $schema, __CLASS__, __FUNCTION__, __LINE__);
+	}
+
+	public function _getKosztorysSchema() {
+		$schema['nr'] = "Nr projektu";
+		$schema['title'] = "Tytuł projektu";
+		$schema['owner'] = "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'] = "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'] = "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'] = "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'] = "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'] = "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']['__RURY_OSLONOWE__'] = $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 _fetchKosztorysData($idProject) {
+		$project = array();
+		$schema = $this->_getKosztorysSchema();
+
+		$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'] = $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;
+	}
+
+}