Bladeren bron

fixed GDistance function, added test offer form in Kosztorysy

Piotr Labudda 10 jaren geleden
bovenliggende
commit
3e1686e4f4
1 gewijzigde bestanden met toevoegingen van 129 en 3 verwijderingen
  1. 129 3
      SE/se-lib/Route/UrlAction/ProjektyKosztyWstepnychRobot.php

+ 129 - 3
SE/se-lib/Route/UrlAction/ProjektyKosztyWstepnychRobot.php

@@ -71,7 +71,62 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		echo json_encode($jsonData);
 		echo json_encode($jsonData);
 	}
 	}
 
 
-	public function reinstall() {// TODO: mv to UrlActionBase
+	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 = <<<SQL_FUN
+				CREATE DEFINER=`root`@`localhost` FUNCTION `GDistance`(`LS` LINESTRING) RETURNS double NO SQL
+				BEGIN
+					DECLARE LON_A, LAT_A, LON_B, LAT_B, R, RESULT DOUBLE;
+					DECLARE A, B POINT;
+					DECLARE I, N INT;
+					-- if LS is not geometry type then return 0
+					-- if GeometryType is not 'LINESTRING' then return 1
+					-- if GeometryType is 'LINESTRING' then return length
+
+					IF LS is null or GeometryType(LS) is null THEN
+						RETURN 0;
+					END IF;
+					IF 'LINESTRING' != GeometryType(LS) THEN
+						RETURN 1;
+					END IF;
+
+					SET R = 6372795.477598;
+
+					SET N = NUMPOINTS(LS);
+					IF N is null or N = 1 THEN
+						RETURN 1;
+					END IF;
+
+					SET RESULT = 0;
+					SET I = 1;
+
+					WHILE I < N DO
+						SET A = POINTN(LS, I);
+						SET B = POINTN(LS, I + 1);
+						SET LON_A = RADIANS(X(A));
+						SET LAT_A = RADIANS(Y(A));
+						SET LON_B = RADIANS(X(B));
+						SET LAT_B = RADIANS(Y(B));
+						SET RESULT = RESULT + ACOS(SIN(LAT_A) * SIN(LAT_B) + COS(LAT_A) * COS(LAT_B) * COS(LON_A - LON_B));
+						SET I = I + 1;
+					END WHILE;
+
+					SET RESULT = RESULT * R;
+					RETURN RESULT;
+				END
+SQL_FUN;
+			$pdo->exec($gDistanceFuncion);
+		}
+
 		/* required Zasoby tree structure - XML (parent rel in PARENT_ID field):
 		/* required Zasoby tree structure - XML (parent rel in PARENT_ID field):
 <zasob:tree>
 <zasob:tree>
 	<zasob:TYPESPECIALS>
 	<zasob:TYPESPECIALS>
@@ -221,7 +276,6 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		$knownArgs[':url_action_param_1_name'] = 'ID_PROJECT';
 		$knownArgs[':url_action_param_1_name'] = 'ID_PROJECT';
 		$knownArgs[':main_db_id'] = '36';// from DB::getDB()->getZasobId(); or DB::getPDO()->getZasobId();
 		$knownArgs[':main_db_id'] = '36';// from DB::getDB()->getZasobId(); or DB::getPDO()->getZasobId();
 		$this->_debugFlatConfig($flatConf, $knownArgs);
 		$this->_debugFlatConfig($flatConf, $knownArgs);
-		$pdo = DB::getPDO();
 
 
 		$args = $this->getArgsList();
 		$args = $this->getArgsList();
 		$clsName = __CLASS__;
 		$clsName = __CLASS__;
@@ -608,7 +662,71 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 <?php
 <?php
 	}
 	}
 
 
+	public function saveOffer($idProject, $args, $admin = false) {
+		DBG::_(true, true, "TODO...", null, __CLASS__, __FUNCTION__, __LINE__);return;
+		if (!$admin) return;
+		DBG::_(true, true, "args", $args, __CLASS__, __FUNCTION__, __LINE__);
+		$pdo = DB::getPDO();
+		$add_id_zasob = 0;
+		$add_id_companies = 0;// TODO: oferty dostawców
+		$add_id_project = $idProject;
+		$add_unit = '';
+		$add_price = 0;
+		$add_author = 0;
+		$add_quantity = 0;
+		$add__sth = $pdo->prepare("
+			insert into CRM_LISTA_ZASOBOW_OFFERS__TEST (
+				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_companies
+				, :id_project
+				, :price
+				, :unit
+				, :unit
+				, :quantity
+				, :author
+				, NOW()
+			)
+		");
+		$add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_STR);
+		$add__sth->bindParam(':id_companies', $add_id_companies, PDO::PARAM_STR);
+		$add__sth->bindParam(':id_project', $add_id_project, PDO::PARAM_STR);
+		$add__sth->bindParam(':price', $add_price, PDO::PARAM_STR);
+		$add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR);
+		$add__sth->bindParam(':quantity', $add_quantity, PDO::PARAM_STR);
+		$add__sth->bindParam(':author',   $usrLogin, PDO::PARAM_STR);
+		$schema = $this->getSchema();
+		$data = $this->fetchData($idProject, $admin);
+		foreach ($data as $idLayer => $layData) {
+			DBG::_(true, true, "layData", $layData, __CLASS__, __FUNCTION__, __LINE__);
+			foreach ($layData['data'] as $typeData) {
+				DBG::_(true, true, "typeData", $typeData, __CLASS__, __FUNCTION__, __LINE__);
+				if ($typeData['idType']) {
+					$add_id_zasob = $typeData['idType'];
+					$add_price = V::get("price_{$idLayer}_{$typeData['idType']}", '', $args);
+					$add_unit = $typeData['jednostka'];
+					$add_quantity = $typeData['ilosc'];
+					if ($add_price > 0) {
+						$add__sth->execute();
+					}
+				}
+			}
+		}
+	}
+
 	public function oferta($idProject, $admin = false) {
 	public function oferta($idProject, $admin = false) {
+		if ($admin && '1' == V::get('save_offer', '', $_POST)) {
+			$this->saveOffer($idProject, $_POST, $admin);
+		}
 		$schema = $this->getSchema();
 		$schema = $this->getSchema();
 		$conf = $schema['config'];
 		$conf = $schema['config'];
 		DBG::_('DBG', '>1', "conf", $conf, __CLASS__, __FUNCTION__, __LINE__);
 		DBG::_('DBG', '>1', "conf", $conf, __CLASS__, __FUNCTION__, __LINE__);
@@ -617,6 +735,9 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		DBG::_('DBG', '>1', "data", $data, __CLASS__, __FUNCTION__, __LINE__);
 		DBG::_('DBG', '>1', "data", $data, __CLASS__, __FUNCTION__, __LINE__);
 		?>
 		?>
 <div class="container">
 <div class="container">
+	<?php if ($admin) : ?>
+		<form action="" method="post">
+	<?php endif; ?>
 	<?php foreach ($data as $idLayer => $layData) : ?>
 	<?php foreach ($data as $idLayer => $layData) : ?>
 		<h4 style="padding:0 6px"><?php echo $layData['label']; ?></h4>
 		<h4 style="padding:0 6px"><?php echo $layData['label']; ?></h4>
 		<table class="tabel table-bordered" style="width:100%">
 		<table class="tabel table-bordered" style="width:100%">
@@ -648,13 +769,18 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 						<td style="padding:0 6px" title="[<?php echo $typeData['idType']; ?>] <?php echo $typeData['type']; ?>"><?php echo $typeData['type']; ?></td>
 						<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['ilosc']; ?></td>
 						<td style="padding:0 6px"><?php echo $typeData['jednostka']; ?></td>
 						<td style="padding:0 6px"><?php echo $typeData['jednostka']; ?></td>
-						<td style="padding:3px 6px"><input type="text" class="form-control input-sm" value="<?php echo V::get('cena_jednostkowa', '', $typeData); ?>"/></td>
+						<td style="padding:3px 6px"><input type="text" class="form-control input-sm" name="price_<?php echo $idLayer; ?>_<?php echo $typeData['idType']; ?>" value="<?php echo V::get('cena_jednostkowa', '', $typeData); ?>"/></td>
 					</tr>
 					</tr>
 				<?php endif; ?>
 				<?php endif; ?>
 			<?php endforeach; ?>
 			<?php endforeach; ?>
 		</tbody>
 		</tbody>
 		</table>
 		</table>
 	<?php endforeach; ?>
 	<?php endforeach; ?>
+	<?php if ($admin) : ?>
+			<input type="hidden" name="save_offer" value="1">
+			<hr><input class="btn btn-primary" type="submit" value="Zapisz ofertę">
+		</form>
+	<?php endif; ?>
 </div>
 </div>
 <?php
 <?php
 	}
 	}