Pārlūkot izejas kodu

added Response; added test costs category in Budget

Piotr Labudda 10 gadi atpakaļ
vecāks
revīzija
a593ee145c
2 mainītis faili ar 143 papildinājumiem un 10 dzēšanām
  1. 27 0
      SE/se-lib/Response.php
  2. 116 10
      SE/se-lib/Route/Budget.php

+ 27 - 0
SE/se-lib/Response.php

@@ -0,0 +1,27 @@
+<?php
+
+class Response {
+
+	public static function sendJson($data) {
+		header("Content-type: application/json");
+		echo json_encode($data);
+	}
+
+	public static function sendPlainText($data) {
+		header("Content-type: text/plain");
+		if (!is_scalar($data)) throw new Exception("Wrong data type - only scalar values allowed");
+		echo $data;
+	}
+
+
+	public static function sendJsonExit($data) {
+		self::sendJson($data);
+		exit;
+	}
+
+	public static function sendPlainTextExit($data) {
+		self::sendPlainText($data);
+		exit;
+	}
+
+}

+ 116 - 10
SE/se-lib/Route/Budget.php

@@ -4,6 +4,7 @@ Lib::loadClass('RouteBase');
 Lib::loadClass('ProcesHelper');
 Lib::loadClass('TableAjax');
 Lib::loadClass('UserStorageFactory');
+Lib::loadClass('Response');
 
 class Route_Budget extends RouteBase {
 
@@ -300,6 +301,58 @@ jQuery("#widget-budget").Budget({
 		return $this->_plan;
 	}
 
+	public function getCostsCategoryDBGAction() {// TODO: RMME
+		SE_Layout::gora();
+		$year = V::get('year', 0, $_GET, 'int');
+		if ($year <= 0) throw new Exception("Year not set!");
+		$costsCat = $this->_fetchCostsCategoryByYear($year);
+		DBG::table("costsCat", $costsCat, __CLASS__, __FUNCTION__, __LINE__);
+/* TEST COST_TYPE for proj 0-4132-3934
+		SELECT k.ID, k.COST_VALUE, k.INCOME_VALUE, k.COST_TYPE, k.ID_PROJECT
+		FROM  `IN7_DZIENNIK_KORESP` k
+		WHERE  k.`ID_PROJECT` =3934
+			and k.K_DATA_OTRZYMANEJ_KORESP like '2016-01-%'
+*/
+		$rows = DB::getPDO()->fetchAll("
+			SELECT k.ID
+				, k.COST_VALUE
+				, k.INCOME_VALUE
+				, k.COST_TYPE
+				, k.ID_PROJECT
+			FROM `IN7_DZIENNIK_KORESP` k
+			where ((k.COST_VALUE != 0) or (k.INCOME_VALUE != 0))
+				and k.K_DATA_OTRZYMANEJ_KORESP like '{$year}-%'
+			ORDER BY k.`ID` desc
+		");
+		DBG::table("rows", $rows, __CLASS__, __FUNCTION__, __LINE__);
+	}
+	public function getCostsCategoryAction() {
+		$year = V::get('year', 0, $_GET, 'int');
+		try {
+			if ($year <= 0) throw new Exception("Year not set!");
+			$costsCat = $this->_fetchCostsCategoryByYear($year);
+			Response::sendJsonExit($costsCat);
+		} catch (Exception $e) {
+			$response = array();
+			$response['type'] = 'danger';
+			$response['msg'] = $e->getMessage();
+			Response::sendJsonExit($response);
+		}
+	}
+
+	public function _fetchCostsCategoryByYear($year) {
+		// TODO: mv KATEGORIA_KOSZTU (enum) to COST_TYPE (varchar(32)) in pro-netmedia.pl
+		return DB::getPDO()->fetchAll("
+			select k.ID
+				, k.COST_TYPE
+			from IN7_DZIENNIK_KORESP k
+			where ((k.COST_VALUE != 0) or (k.INCOME_VALUE != 0))
+				and k.K_DATA_OTRZYMANEJ_KORESP like '{$year}-%'
+				and k.COST_TYPE != ''
+			-- TODO: acl
+		");
+	}
+
 	public function _fetchCostsByYear($year) {
 		$db = DB::getDB();
 		$this->_costs = array();
@@ -501,17 +554,70 @@ jQuery("#widget-budget").Budget({
 	}
 
 	public function _getLdapGroupsNames() {
-		$usrStorageMacOSX = UserStorageFactory::getStorage('MacOSX');
-		if (!$usrStorageMacOSX) throw new Exception("Error storage 'MacOSX' not exists!");
 		$userGroupsByZasobId = array();
-		$userGroups = User::getLdapGroupsNames();
-		DBG::_('DBG', '>3', "_getLdapGroupsNames(). userGroups:", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
-		foreach ($userGroups as $uidGroup) {
-			$idZasob = $usrStorageMacOSX->getGroupIdFromUid($uidGroup);
-			if ($idZasob) {
-				$userGroupsByZasobId[$idZasob] = $uidGroup;
-			} else {
-				//$userGroupsByZasobId[$uidGroup] = $uidGroup;
+		$usrStorageMacOSX = UserStorageFactory::getStorage('MacOSX');
+		if (!$usrStorageMacOSX) {
+			// throw new Exception("Error storage 'MacOSX' not exists!");
+			$userGroups = User::getGroups();
+			$usrStorageDB = UserStorageFactory::getStorage('DB');
+			//DBG::_(true, true, "userGroups", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
+			$rows = DB::getPDO()->fetchAll("
+				select distinct a.perm_group_uid
+					from (
+						SELECT DISTINCT `A_ADM_COMPANY` as perm_group_uid FROM `IN7_MK_BAZA_DYSTRYBUCJI`
+						union
+						SELECT DISTINCT `A_CLASSIFIED` as perm_group_uid FROM `IN7_MK_BAZA_DYSTRYBUCJI`
+						union
+						SELECT DISTINCT `A_ADM_COMPANY` as perm_group_uid FROM `IN7_DZIENNIK_KORESP`
+						union
+						SELECT DISTINCT `A_CLASSIFIED` as perm_group_uid FROM `IN7_DZIENNIK_KORESP`
+					) as a
+			");
+			//DBG::_(true, true, "rows", $rows, __CLASS__, __FUNCTION__, __LINE__);
+			DBG::_('DBG', '>3', "userGroups:", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
+			$parentGroups = array();
+			foreach ($userGroups as $idGroup => $group) {
+				$parentGroups = $usrStorageDB->fetchParentGroups($idGroup);
+				foreach ($parentGroups as $idParentGroup => $parentGroup) {
+					$parentGroups[$idParentGroup] = true;
+				}
+			}
+			DBG::_('DBG', '>3', "parentGroups:", $parentGroups, __CLASS__, __FUNCTION__, __LINE__);
+			$grandParentGroups = array();
+			foreach ($parentGroups as $idGroup => $group) {
+				$parentGroups = $usrStorageDB->fetchParentGroups($idGroup);
+				foreach ($parentGroups as $idParentGroup => $parentGroup) {
+					$grandParentGroups[$idParentGroup] = true;
+				}
+			}
+			DBG::_('DBG', '>3', "grandParentGroups:", $grandParentGroups, __CLASS__, __FUNCTION__, __LINE__);
+			$userGroupIdsAll = array();
+			foreach ($userGroups as $idGroup => $group) $userGroupIdsAll[$idGroup] = true;
+			foreach ($parentGroups as $idGroup => $group) $userGroupIdsAll[$idGroup] = true;
+			foreach ($grandParentGroups as $idGroup => $group) $userGroupIdsAll[$idGroup] = true;
+			DBG::_('DBG', '>3', "userGroupIdsAll:", $userGroupIdsAll, __CLASS__, __FUNCTION__, __LINE__);
+			foreach ($rows as $row) {
+				$perm_group_uid = $row['perm_group_uid'];
+				if (empty($perm_group_uid)) continue;
+				$parts = explode('_', $perm_group_uid, 2);
+				DBG::_('DBG', '>3', "loop - $perm_group_uid:", $parts, __CLASS__, __FUNCTION__, __LINE__);
+				$idGroup = $parts[0];
+				DBG::_('DBG', '>3', "loop - $perm_group_uid - idGroup({$idGroup}):", array_key_exists($idGroup, $userGroups), __CLASS__, __FUNCTION__, __LINE__);
+				if (array_key_exists($idGroup, $userGroupIdsAll)) {
+					$userGroupsByZasobId[$idGroup] = $perm_group_uid;
+				}
+			}
+			DBG::_('DBG', '>3', "_getLdapGroupsNames(). userGroupsByZasobId:", $userGroupsByZasobId, __CLASS__, __FUNCTION__, __LINE__);
+		} else {
+			$userGroups = User::getLdapGroupsNames();
+			DBG::_('DBG', '>3', "_getLdapGroupsNames(). userGroups:", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
+			foreach ($userGroups as $uidGroup) {
+				$idZasob = $usrStorageMacOSX->getGroupIdFromUid($uidGroup);
+				if ($idZasob) {
+					$userGroupsByZasobId[$idZasob] = $uidGroup;
+				} else {
+					//$userGroupsByZasobId[$uidGroup] = $uidGroup;
+				}
 			}
 		}
 		DBG::_('DBG', '>3', "_getLdapGroupsNames(). userGroupsByZasobId:", $userGroupsByZasobId, __CLASS__, __FUNCTION__, __LINE__);