menu($args['year']); } SE_Layout::dol(); } public function yearBudgetAction() { $args = array(); $args['year'] = V::get('year', '', $_REQUEST, 'int'); $args['_print'] = V::get('_print', '', $_REQUEST, 'int'); SE_Layout::gora(); SE_Layout::menu(); if (!$args['_print']) { $this->menu($args['year']); } if (empty($args['year'])) { ?>
Nie wybrano roku.
getCostsByYear($args['year']); if (empty($costs)) { ?>
Brak danych na wybrany rok.
$costs: ';print_r($costs);echo''; $this->printCostsForYear($args['year']); SE_Layout::dol(); } private function menu($selectedYear) { //SE_Layout::menu(); $year = ($selectedYear)? $selectedYear : date("Y"); ?>
_costs = $this->_costs; $months = array(); for ($i = 0; $i < 12; $i++) { $months[] = $i + 1; } $this->css(); if(V::get('DBG','',$_GET)){echo'
';print_r($this->_costs);echo'
';} ?>

Zestawienie kosztów projektów na rok

_projectPathsOrder as $projPath => $projId) : ?> _costs[$projId]; ?> '; continue; } ?> costsByMonth)) : ?> costsByMonth); ?> COST_CHILD, 2); ?> COST_SELF, 2); ?> COST_TOTAL, 2); ?> COST_CHILD > 0) : ?> COST_SELF > 0) : ?>
pokaż tylko zaznaczone miesiąc
ERROR:EMPTY: Path('.$projPath.') id('.$projId.')
path; ?> M_DIST_DESC; ?>  
_costs as $projId => $projectInfo) : ?> korespByMonth)) : ?> korespByMonth as $kMonth => $vKorespMonthList) : ?> $vKorespInfo) : ?> 41235 * [MONTH] => 2015-02 * [K_ZAWARTOS] => Faktura za pomiar powykonawczy sieci telekomunikacyjnej * [COST] => 1000.00 * [INCOME] => 0.00 ? [TRANSFER_OPPOSITE_ID_PROJECT] => int */ ?> COST, 2); ?>
query($sql); while ($r = $db->fetch($res)) { if (!array_key_exists($r->ID_PROJECT, $costs)) { $projectInfo = new stdClass(); $projectInfo->ID_PROJECT = $r->ID_PROJECT; $projectInfo->M_DIST_DESC = $r->M_DIST_DESC; $projectInfo->path = $r->path; $projectInfo->costsByMonth = array(); $costs[$r->ID_PROJECT] = $projectInfo; } $cost = new stdClass(); $cost->MONTH = $r->MONTH; $cost->COST_SELF = $r->COST_SELF; $cost->COST_CHILD = $r->COST_CHILD; $cost->COST_TOTAL = $r->COST_TOTAL; $cost->INCOME_SELF = $r->INCOME_SELF; $cost->INCOME_CHILD = $r->INCOME_CHILD; $cost->INCOME_TOTAL = $r->INCOME_TOTAL; $monthNum = intval(substr($r->MONTH, 5, 2)); $costs[$r->ID_PROJECT]->costsByMonth[$monthNum] = $cost; } return $costs; } public function getCostsByYear($year) { $db = DB::getDB(); $this->_costs = array(); $sql = " select k.`ID` , k.`ID_PROJECT` AS `ID_PROJECT` , date_format(k.`K_DATA_OTRZYMANEJ_KORESP`,'%Y-%m') AS `MONTH` , k.`COST_VALUE` AS `COST` , k.`INCOME_VALUE` AS `INCOME` , 0 as `TRANSFER_OPPOSITE_ID_PROJECT` -- TRANSFER_OPPOSITE_ID_PROJECT , k.`path` -- , IF(k.`TRANSFER_OPPOSITE_ID_PROJECT`>0 -- , (select p.`path` -- from `IN7_MK_BAZA_DYSTRYBUCJI` p -- where p.`ID`=k.`TRANSFER_OPPOSITE_ID_PROJECT` -- limit 1 -- ) -- , '') as TRANSFER_OPPOSITE_PROJECT_PATH , '' as TRANSFER_OPPOSITE_PROJECT_PATH , k.`K_ZAWARTOS` from `IN7_DZIENNIK_KORESP` k where ((k.`COST_VALUE` > 0) or (k.`INCOME_VALUE` > 0)) and k.`K_DATA_OTRZYMANEJ_KORESP` like '{$year}-%' "; //echo'
';print_r($sql);echo'
'; $res = $db->query($sql); while ($r = $db->fetch($res)) { $vProjId = ($r->ID_PROJECT > 0)? $r->ID_PROJECT : 0; $vProjPaths = array(); $vProjIds = array(0 => true); if ($r->ID_PROJECT > 0) { $vProjPaths[] = $r->ID_PROJECT; if (!empty($r->path)) { $vProjPaths[] = $r->path; } } if ($r->TRANSFER_OPPOSITE_ID_PROJECT > 0) { $vProjPaths[] = $r->TRANSFER_OPPOSITE_ID_PROJECT; if (!empty($r->TRANSFER_OPPOSITE_PROJECT_PATH)) { $vProjPaths[] = $r->TRANSFER_OPPOSITE_PROJECT_PATH; } } if (!empty($vProjPaths)) { //echo'

DBG:$r->ID_PROJECT['.$r->ID_PROJECT.']: $vProjPaths = ' . json_encode($vProjPaths) . '

'; $vProjPaths = implode('-', $vProjPaths); $projIds = explode('-', $vProjPaths); //echo'

DBG:$r->ID_PROJECT['.$r->ID_PROJECT.']: $projIds = ' . json_encode($projIds) . '

'; foreach ($projIds as $vProjId) { if ($vProjId > 0) $vProjIds[$vProjId] = true; } } { $projectZeroInfo = new stdClass(); $projectZeroInfo->ID_PROJECT = 0; $projectZeroInfo->M_DIST_DESC = 'Wszystkie projekty'; $projectZeroInfo->path = '0'; $projectZeroInfo->costsByMonth = array(); $projectZeroInfo->korespByMonth = array(); $this->_costs[0] = $projectZeroInfo; } foreach ($vProjIds as $vProjId => $vBool) { if (!array_key_exists($vProjId, $this->_costs)) { $projectInfo = new stdClass(); $projectInfo->ID_PROJECT = $vProjId; $projectInfo->M_DIST_DESC = ''; $projectInfo->path = ''; $projectInfo->costsByMonth = array(); $projectInfo->korespByMonth = array(); $this->_costs[$vProjId] = $projectInfo; } } $korespInfo = new stdClass(); $korespInfo->ID = $r->ID; $korespInfo->MONTH = $r->MONTH; $korespInfo->K_ZAWARTOS = $r->K_ZAWARTOS; $monthNum = intval(substr($r->MONTH, 5, 2)); if ($r->TRANSFER_OPPOSITE_ID_PROJECT > 0) { $korespInfo->COST = $r->COST; $korespInfo->INCOME = $r->INCOME; $korespInfo->TRANSFER_OPPOSITE_ID_PROJECT = $r->TRANSFER_OPPOSITE_ID_PROJECT; $this->_costs[$r->TRANSFER_OPPOSITE_ID_PROJECT]->korespByMonth[$monthNum][] = $korespInfo; } else if ($r->ID_PROJECT) { $korespInfo->COST = $r->COST; $korespInfo->INCOME = $r->INCOME; $this->_costs[$r->ID_PROJECT]->korespByMonth[$monthNum][] = $korespInfo; } else { $korespInfo->COST = $r->COST; $korespInfo->INCOME = $r->INCOME; $this->_costs[0]->korespByMonth[$monthNum][] = $korespInfo; } } $this->_fetchProjectInfo(); $this->_buildProjectTree(); $this->_reacountCostsFromKoresp(); return $this->_costs; } private function _fetchProjectInfo() { $db = DB::getDB(); $projectIds = array_keys($this->_costs); $sqlProjIds = "'" . implode("','", $projectIds) . "'"; $sql = "select p.`ID`, p.`P_ID`, p.`path`, p.`M_DIST_DESC` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID` in({$sqlProjIds}) "; $res = $db->query($sql); while ($r = $db->fetch($res)) { $this->_costs[$r->ID]->path = $r->path; $this->_costs[$r->ID]->M_DIST_DESC = $r->M_DIST_DESC; } } private function _reacountCostsFromKoresp() { $projMonthHasCostSelfIds = array(); foreach ($this->_costs as $kProjId => $vProjInfo) { foreach ($vProjInfo->korespByMonth as $kMonthNum => $vKorespList) { $this->_createCostIfNotDefined($kProjId, $kMonthNum); foreach ($vKorespList as $vKoresp) { $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->COST_SELF += $vKoresp->COST; $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->INCOME_SELF += $vKoresp->INCOME; } $projHasCostSelfIds[$kProjId][$kMonthNum] = $vProjInfo->path; } } //echo'
$projHasCostSelfIds: ';print_r($projHasCostSelfIds);echo'
'; foreach ($projHasCostSelfIds as $kProjId => $vProjMonthsList) { if ($kProjId <= 0) continue; foreach ($vProjMonthsList as $kMonthNum => $vProjPath) { $vProjPathIds = explode('-', $vProjPath); $vProjPathIds = array_reverse($vProjPathIds); $vProjMonthCostSelf = $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->COST_SELF; $vProjMonthIncomeSelf = $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->INCOME_SELF; foreach ($vProjPathIds as $vProjId) { if ($vProjId == $kProjId) continue; $this->_createCostIfNotDefined($vProjId, $kMonthNum); $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->COST_CHILD += $vProjMonthCostSelf; $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->INCOME_CHILD += $vProjMonthIncomeSelf; } } } // recount total foreach ($this->_costs as $vProjId => $vProjInfo) { foreach ($vProjInfo->costsByMonth as $kMonthNum => $vCost) { $this->_createCostIfNotDefined($vProjId, $kMonthNum); $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->COST_TOTAL = $vCost->COST_SELF + $vCost->COST_CHILD; $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->INCOME_TOTAL = $vCost->INCOME_SELF + $vCost->INCOME_CHILD; } } } private function _createCostIfNotDefined($projId, $monthNum) { if (empty($this->_costs[$projId]->costsByMonth[$monthNum])) { $vEmptyCost = new stdClass(); $vEmptyCost->COST_SELF = 0; $vEmptyCost->INCOME_SELF = 0; $vEmptyCost->COST_CHILD = 0; $vEmptyCost->INCOME_CHILD = 0; $vEmptyCost->COST_TOTAL = 0; $vEmptyCost->INCOME_TOTAL = 0; $this->_costs[$projId]->costsByMonth[$monthNum] = $vEmptyCost; } } public function _buildProjectTree() { $this->_projectPathsOrder = array(); foreach ($this->_costs as $idProject => $projectInfo) { $this->_projectPathsOrder[$projectInfo->path] = $projectInfo->ID_PROJECT; } //echo'
projPaths: ';print_r($this->_projectPathsOrder);echo'
'; uksort($this->_projectPathsOrder, array($this, 'sortPathsCallback')); //echo'
projPaths sorted: ';print_r($this->_projectPathsOrder);echo'
'; return $this->_projectPathsOrder; } public function sortPathsCallback($a, $b) { $ea = explode('-', $a); $eb = explode('-', $b); $la = count($ea); $lb = count($eb); $lmin = min($la, $lb); for ($i = 0; $i < $lmin; $i++) { if ($ea[$i] < $eb[$i]) { return -1; } else if ($ea[$i] > $eb[$i]) { return 1; } } return $la - $lb; } }