Budget.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('ProcesHelper');
  4. Lib::loadClass('TableAjax');
  5. Lib::loadClass('UserStorageFactory');
  6. class Route_Budget extends RouteBase {
  7. private $_costs = array();
  8. private $_plan = array();
  9. private $_projectInfo = array();
  10. private $_projectPathsOrder = array();
  11. public function handleAuth() {
  12. if (!User::logged()) {
  13. throw new HttpException('Unauthorized', 401);
  14. }
  15. }
  16. public function defaultAction() {
  17. $args = array();
  18. $args['year'] = V::get('year', '', $_REQUEST, 'int');
  19. $args['_print'] = V::get('_print', '', $_REQUEST, 'int');
  20. SE_Layout::gora();
  21. SE_Layout::menu();
  22. if (!$args['_print']) {
  23. $this->menu($args['year']);
  24. }
  25. SE_Layout::dol();
  26. }
  27. public function yearBudgetAction() {
  28. $args = array();
  29. $args['year'] = V::get('year', '', $_REQUEST, 'int');
  30. $args['groups'] = V::get('fltrGroups', array(), $_REQUEST, 'array', array('V', 'filterPositiveInteger'));
  31. $args['_print'] = V::get('_print', '', $_REQUEST, 'int');
  32. $hasData = false;
  33. $groups = null;
  34. if ($args['year'] > 0) {
  35. $hasData = $this->fetchDataByYear($args['year']);
  36. $groups = $this->getUsedUserGroups();
  37. }
  38. SE_Layout::gora();
  39. SE_Layout::menu();
  40. if (!$args['_print']) {
  41. $this->menu($args['year'], $groups, $args['groups']);
  42. }
  43. if (empty($args['year'])) {
  44. ?>
  45. <div class="alert alert-warning">
  46. Nie wybrano roku.
  47. </div>
  48. <?php
  49. SE_Layout::dol();
  50. exit;
  51. }
  52. if (!$hasData) {
  53. ?>
  54. <div class="alert alert-warning">
  55. Brak danych na wybrany rok.
  56. </div>
  57. <?php
  58. return;
  59. }
  60. //echo'<pre style="border:1px solid red;overflow:auto;max-height:400px">$costs: ';print_r($costs);echo'</pre>';
  61. $this->printCostsForYear($args['year'], $args['groups']);
  62. SE_Layout::dol();
  63. }
  64. private function menu($selectedYear, $groups = array(), $selectedGroups = array()) {
  65. //SE_Layout::menu();
  66. $year = ($selectedYear)? $selectedYear : date("Y");
  67. ?>
  68. <div class="jumbotron">
  69. <div class="container">
  70. <form class="form-inline" method="POST">
  71. <input type="hidden" name="_task" value="yearBudget" />
  72. <label for="year">Zestawienie kosztów projektów. Wybierz rok:</label>
  73. <div class="input-group date" id="fldZestYear">
  74. <input type="text" name="year" class="form-control" value="" />
  75. <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
  76. </div>
  77. <?php if (!empty($groups)) : ?>
  78. <div style="margin:8px 0">
  79. <label for="fltrGroups">Pokaż tylko projekty dostępne dla grup:</label>
  80. <select multiple name="fltrGroups[]" size="<?php echo min(5, count($groups) + 1); ?>" class="form-control">
  81. <option value=""> [ Wszystkie ] </option>
  82. <?php foreach ($groups as $idGroup => $groupLdapName) : ?>
  83. <option
  84. value="<?php echo $idGroup; ?>"
  85. <?php if (in_array($idGroup, $selectedGroups)) { echo 'selected="selected"'; } ?>
  86. ><?php echo $groupLdapName; ?></option>
  87. <?php endforeach; ?>
  88. </select>
  89. </div>
  90. <?php endif; ?>
  91. <button type="submit" id="fldZestYearBtn" class="btn btn-primary" autocomplete="off">
  92. Pokaż
  93. </button>
  94. </form>
  95. <div style="text-align:right">
  96. Edytuj
  97. <a href="index.php?_route=Budget&_task=plan&year=<?php echo $year; ?>"
  98. class="btn btn-xs btn-default"
  99. title="Plan budżetu (projects_budget_year_month)">plan budżetu</a>
  100. na rok <?php echo $year; ?>
  101. </div>
  102. </div>
  103. </div>
  104. <script type="text/javascript">
  105. jQuery(document).ready(function () {
  106. jQuery('#fldZestYearBtn').on('click', function () {
  107. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  108. jQuery(this).parent().submit();
  109. })
  110. jQuery("#fldZestYear").datetimepicker({
  111. format: "YYYY",
  112. defaultDate: new Date(<?php echo $year; ?>, <?php echo intval(date("m")); ?>, 1),
  113. // minDate: new Date(2014, 11, 1),
  114. // maxDate: "<?php echo date("Y"); ?>"
  115. });
  116. });
  117. </script>
  118. <?php
  119. }
  120. function css() {
  121. ?>
  122. <style type="text/css">
  123. .c { text-align:center; }
  124. .r { text-align:right; }
  125. .zestawienie-kosztow-tbl { border-collapse:collapse; border:1px solid #aaa; }
  126. .zestawienie-kosztow-tbl td { border:1px solid #aaa; }
  127. .zestawienie-kosztow-tbl .p2 { padding:0 2px; }
  128. .zestawienie-kosztow-tbl .nr { color:#7A7A7A; }
  129. .zestawienie-kosztow-tbl thead th { border:1px solid #aaa; }
  130. .zestawienie-kosztow-tbl tbody tr:hover td { background:#cafbfd; }
  131. .row-selected td {background-color:#d8fded;}
  132. .showOnlySelected tr { display:none; }
  133. .showOnlySelected tr.row-selected { display:table-row; }
  134. .cell-cost { padding:0 2px; min-width:30px; text-align:right; }
  135. .cell-cost-only_self { color:#197fe6; }
  136. .cell-cost-self_and_child { color:#33b2cc; }
  137. .cell-cost-only_child { color:#59a680; }
  138. .cell-plan { padding:0 2px; min-width:30px; text-align:right; color:#777; }
  139. .cell-procent { padding:0 2px; min-width:20px; text-align:right; color:#777; }
  140. .cell-procent-below100 { color:#777; }
  141. .cell-procent-100 { color:#777; }
  142. .cell-procent-over100 { color:#ff9b00; }
  143. .cell-procent-over150 { color:#f00; }
  144. /* print table background colors */
  145. table td, table th { -webkit-print-color-adjust:exact; }
  146. table { page-break-after:auto }
  147. tr { page-break-inside:avoid; page-break-after:auto; position:relative; }
  148. td { page-break-inside:avoid; page-break-after:auto; position:relative; }
  149. thead { display:table-header-group }
  150. tfoot { display:table-footer-group }
  151. #zestawienie-kosztow-projektow.hidden_month_1 .col_month_1 {display:none}
  152. #zestawienie-kosztow-projektow.hidden_month_2 .col_month_2 {display:none}
  153. #zestawienie-kosztow-projektow.hidden_month_3 .col_month_3 {display:none}
  154. #zestawienie-kosztow-projektow.hidden_month_4 .col_month_4 {display:none}
  155. #zestawienie-kosztow-projektow.hidden_month_5 .col_month_5 {display:none}
  156. #zestawienie-kosztow-projektow.hidden_month_6 .col_month_6 {display:none}
  157. #zestawienie-kosztow-projektow.hidden_month_7 .col_month_7 {display:none}
  158. #zestawienie-kosztow-projektow.hidden_month_8 .col_month_8 {display:none}
  159. #zestawienie-kosztow-projektow.hidden_month_9 .col_month_9 {display:none}
  160. #zestawienie-kosztow-projektow.hidden_month_10 .col_month_10 {display:none}
  161. #zestawienie-kosztow-projektow.hidden_month_11 .col_month_11 {display:none}
  162. #zestawienie-kosztow-projektow.hidden_month_12 .col_month_12 {display:none}
  163. .thead__cols_summary,
  164. .row__summary__cost,
  165. .row__summary__plan {background-color:#fcf8e3;}
  166. .thead_col_month .col_month_remove { opacity:0.2; }
  167. .thead_col_month:hover .col_month_remove { opacity:0.6; }
  168. </style>
  169. <?php
  170. }
  171. function printCostsForYear($year, $groups) {
  172. $months = array();
  173. for ($i = 0; $i < 12; $i++) {
  174. $months[] = $i + 1;
  175. }
  176. $this->css();
  177. //DBG::_('DBG', '>1', "costs", $this->_costs, __CLASS__, __FUNCTION__, __LINE__);
  178. if (!empty($groups)) {
  179. foreach ($this->_projectInfo as $idProject => $projInfo) {
  180. if (!$this->hasGroupsAccessToProjects($idProject, $groups)) {
  181. $this->_projectInfo[$idProject]->filteredByGroups = true;
  182. }
  183. }
  184. }
  185. //echo'<pre>' . json_encode(array_keys($this->_projectPathsOrder)) . '</pre>';
  186. //echo'<pre>' . json_encode($this->_projectPathsOrder) . '</pre>';
  187. //echo'<pre>' . json_encode($this->_projectInfo) . '</pre>';
  188. //echo'<pre>' . json_encode($this->_costs) . '</pre>';
  189. //echo'<pre>' . json_encode($this->_plan) . '</pre>';
  190. $projectPathsOrder = array_keys($this->_projectPathsOrder);
  191. foreach ($projectPathsOrder as $key => $value) {
  192. $projectPathsOrder[$key] = "" . $value;
  193. }
  194. ?>
  195. <div id="widget-budget"></div>
  196. <script src="stuff/vendors.js"></script>
  197. <script src="stuff/bundle.se_route_budget.js"></script>
  198. <script>
  199. jQuery("#widget-budget").Budget({
  200. year: '<?php echo $year; ?>',
  201. today: '<?php echo date('Y-m-d'); ?>',
  202. projectPathsOrder: <?php echo json_encode($projectPathsOrder); ?>,
  203. projectPathsMap: <?php echo json_encode($this->_projectPathsOrder); ?>,
  204. projectInfo: <?php echo json_encode($this->_projectInfo); ?>,
  205. costs: <?php echo json_encode($this->_costs); ?>,
  206. plan: <?php echo json_encode($this->_plan); ?>,
  207. dbg: false
  208. });
  209. </script>
  210. <?php
  211. }
  212. public function getCost($idProject, $month) {
  213. if (!array_key_exists($idProject, $this->_costs)) {
  214. return null;
  215. }
  216. if (!array_key_exists($month, $this->_costs[$idProject]->costsByMonth)) {
  217. return null;
  218. }
  219. return $this->_costs[$idProject]->costsByMonth[$month];
  220. }
  221. public function getPlan($idProject, $month) {
  222. if (!array_key_exists($idProject, $this->_plan)) {
  223. return 0;
  224. }
  225. if (!array_key_exists($month, $this->_plan[$idProject])) {
  226. return 0;
  227. }
  228. return $this->_plan[$idProject][$month];
  229. }
  230. public function fetchDataByYear($year) {
  231. $this->_fetchCostsByYear($year);
  232. $this->_fetchPlanByYear($year);
  233. $this->_fetchProjectInfo();
  234. $this->_buildProjectTree();
  235. $this->_reacountCostsFromKoresp();
  236. return count($this->_projectInfo) > 1;// $this->_projectInfo[0] - Wszystkie projekty
  237. }
  238. public function _fetchPlanByYear($year) {
  239. $db = DB::getDB();
  240. $this->_plan = array();
  241. $sql = "
  242. select plan.`ID`
  243. , plan.`ID_PROJECT` AS `ID_PROJECT`
  244. , plan.`MONTH_1_VALUE`
  245. , plan.`MONTH_2_VALUE`
  246. , plan.`MONTH_3_VALUE`
  247. , plan.`MONTH_4_VALUE`
  248. , plan.`MONTH_5_VALUE`
  249. , plan.`MONTH_6_VALUE`
  250. , plan.`MONTH_7_VALUE`
  251. , plan.`MONTH_8_VALUE`
  252. , plan.`MONTH_9_VALUE`
  253. , plan.`MONTH_10_VALUE`
  254. , plan.`MONTH_11_VALUE`
  255. , plan.`MONTH_12_VALUE`
  256. from `projects_budget_year_month` plan
  257. where plan.`year`='{$year}'
  258. -- TODO: acl
  259. ";
  260. //echo'<pre style="border:1px solid red;overflow:auto;max-height:400px">';print_r($sql);echo'</pre>';
  261. $res = $db->query($sql);
  262. while ($r = $db->fetch($res)) {
  263. $plan = array();
  264. for ($i = 1; $i <= 12; $i++) {
  265. $plan[$i] = V::get("MONTH_{$i}_VALUE", 0, $r);
  266. }
  267. $this->_plan[$r->ID_PROJECT] = $plan;
  268. }
  269. return $this->_plan;
  270. }
  271. public function _fetchCostsByYear($year) {
  272. $db = DB::getDB();
  273. $this->_costs = array();
  274. $sql = "
  275. select k.`ID`
  276. , k.`ID_PROJECT` AS `ID_PROJECT`
  277. , date_format(k.`K_DATA_OTRZYMANEJ_KORESP`,'%Y-%m') AS `MONTH`
  278. , k.`COST_VALUE` AS `COST`
  279. , k.`INCOME_VALUE` AS `INCOME`
  280. , k.`TRANSFER_OPPOSITE_ID_PROJECT`
  281. , k.`path`
  282. , IF(k.`TRANSFER_OPPOSITE_ID_PROJECT`>0
  283. , (select p.`path`
  284. from `IN7_MK_BAZA_DYSTRYBUCJI` p
  285. where p.`ID`=k.`TRANSFER_OPPOSITE_ID_PROJECT`
  286. limit 1
  287. )
  288. , '') as TRANSFER_OPPOSITE_PROJECT_PATH
  289. , k.`K_ZAWARTOS`
  290. from `IN7_DZIENNIK_KORESP` k
  291. where ((k.`COST_VALUE` != 0) or (k.`INCOME_VALUE` != 0))
  292. and k.`K_DATA_OTRZYMANEJ_KORESP` like '{$year}-%'
  293. -- TODO: acl
  294. ";
  295. //echo'<pre style="border:1px solid red;overflow:auto;max-height:400px">';print_r($sql);echo'</pre>';
  296. $res = $db->query($sql);
  297. while ($r = $db->fetch($res)) {
  298. $vProjId = ($r->ID_PROJECT > 0)? $r->ID_PROJECT : 0;
  299. $vProjPaths = array();
  300. $vProjIds = array(0 => true);
  301. if ($r->ID_PROJECT > 0) {
  302. $vProjPaths[] = $r->ID_PROJECT;
  303. if (!empty($r->path)) {
  304. $vProjPaths[] = $r->path;
  305. }
  306. }
  307. if ($r->TRANSFER_OPPOSITE_ID_PROJECT > 0) {
  308. $vProjPaths[] = $r->TRANSFER_OPPOSITE_ID_PROJECT;
  309. if (!empty($r->TRANSFER_OPPOSITE_PROJECT_PATH)) {
  310. $vProjPaths[] = $r->TRANSFER_OPPOSITE_PROJECT_PATH;
  311. }
  312. }
  313. if (!empty($vProjPaths)) {
  314. //echo'<p>DBG:$r->ID_PROJECT['.$r->ID_PROJECT.']: $vProjPaths = ' . json_encode($vProjPaths) . '</p>';
  315. $vProjPaths = implode('-', $vProjPaths);
  316. $projIds = explode('-', $vProjPaths);
  317. //echo'<p>DBG:$r->ID_PROJECT['.$r->ID_PROJECT.']: $projIds = ' . json_encode($projIds) . '</p>';
  318. foreach ($projIds as $vProjId) {
  319. if ($vProjId > 0) $vProjIds[$vProjId] = true;
  320. }
  321. }
  322. {
  323. $projectZeroInfo = new stdClass();
  324. $projectZeroInfo->ID_PROJECT = 0;
  325. $projectZeroInfo->costsByMonth = array();
  326. $projectZeroInfo->korespByMonth = array();
  327. $this->_costs[0] = $projectZeroInfo;
  328. }
  329. foreach ($vProjIds as $vProjId => $vBool) {
  330. if (!array_key_exists($vProjId, $this->_costs)) {
  331. $projectInfo = new stdClass();
  332. $projectInfo->ID_PROJECT = $vProjId;
  333. $projectInfo->costsByMonth = array();
  334. $projectInfo->korespByMonth = array();
  335. $this->_costs[$vProjId] = $projectInfo;
  336. }
  337. }
  338. $korespInfo = new stdClass();
  339. $korespInfo->ID = $r->ID;
  340. $korespInfo->MONTH = $r->MONTH;
  341. $korespInfo->K_ZAWARTOS = $r->K_ZAWARTOS;
  342. $monthNum = intval(substr($r->MONTH, 5, 2));
  343. if ($r->ID_PROJECT > 0) {
  344. if ($r->TRANSFER_OPPOSITE_ID_PROJECT > 0) {
  345. $korespOppositeInfo = clone $korespInfo;
  346. $korespInfo->COST = -1 * $r->COST;
  347. $korespInfo->INCOME = -1 * $r->INCOME;
  348. $korespInfo->TRANSFER_OPPOSITE_ID_PROJECT_TO = $r->TRANSFER_OPPOSITE_ID_PROJECT;
  349. $this->_costs[$r->ID_PROJECT]->korespByMonth[$monthNum][] = $korespInfo;
  350. $korespOppositeInfo->COST = $r->COST;
  351. $korespOppositeInfo->INCOME = $r->INCOME;
  352. $korespOppositeInfo->TRANSFER_OPPOSITE_ID_PROJECT_FROM = $r->ID_PROJECT;
  353. $this->_costs[$r->TRANSFER_OPPOSITE_ID_PROJECT]->korespByMonth[$monthNum][] = $korespOppositeInfo;
  354. } else {
  355. $korespInfo->COST = $r->COST;
  356. $korespInfo->INCOME = $r->INCOME;
  357. $this->_costs[$r->ID_PROJECT]->korespByMonth[$monthNum][] = $korespInfo;
  358. }
  359. } else {
  360. $korespInfo->COST = $r->COST;
  361. $korespInfo->INCOME = $r->INCOME;
  362. $this->_costs[0]->korespByMonth[$monthNum][] = $korespInfo;
  363. }
  364. }
  365. return $this->_costs;
  366. }
  367. private function _fetchProjectInfo() {
  368. $db = DB::getDB();
  369. $hasAccessForAllProjects = true;
  370. $projectIds = array();
  371. $projectsFromCostIds = array_keys($this->_costs);
  372. foreach ($projectsFromCostIds as $idProject) $projectIds[$idProject] = true;
  373. $projectsFromPlanIds = array_keys($this->_plan);
  374. foreach ($projectsFromPlanIds as $idProject) $projectIds[$idProject] = true;
  375. foreach ($projectIds as $idProject => $vBool) $this->_projectInfo[$idProject] = new stdClass();
  376. $projectIds = array_keys($projectIds);
  377. $sqlProjIds = "'" . implode("','", $projectIds) . "'";
  378. $sql = "
  379. select p.`ID`
  380. , p.`P_ID`
  381. , p.`path`
  382. , p.`M_DIST_DESC`
  383. , p.`A_ADM_COMPANY` as aclGroupWrite
  384. , p.`A_CLASSIFIED` as aclGroupRead
  385. , p.`L_APPOITMENT_USER` as aclOwner
  386. from `IN7_MK_BAZA_DYSTRYBUCJI` p
  387. where p.`ID` in({$sqlProjIds})
  388. ";
  389. $res = $db->query($sql);
  390. while ($r = $db->fetch($res)) {
  391. $this->_projectInfo[$r->ID]->path = $r->path;
  392. $this->_projectInfo[$r->ID]->M_DIST_DESC = $r->M_DIST_DESC;
  393. $this->_projectInfo[$r->ID]->aclGroupRead = $r->aclGroupRead;
  394. $this->_projectInfo[$r->ID]->hasAccess = $this->_userHasAccessToProject($r);
  395. if (!$this->_projectInfo[$r->ID]->hasAccess) $hasAccessForAllProjects = false;
  396. }
  397. $this->_projectInfo[0]->path = "0";
  398. $this->_projectInfo[0]->M_DIST_DESC = "Wszystkie projekty";
  399. $this->_projectInfo[0]->hasAccess = $hasAccessForAllProjects;
  400. }
  401. public function hasAccessToProject($idProject) {
  402. if ($idProject >= 0) {
  403. if (array_key_exists($idProject, $this->_projectInfo)) {
  404. return V::get('hasAccess', false, $this->_projectInfo[$idProject]);
  405. }
  406. }
  407. return false;
  408. }
  409. public function hasGroupsAccessToProjects($idProject, $groups) {
  410. $selectedUserGroupNames = array();
  411. $userGroups = $this->_getLdapGroupsNames();
  412. DBG::_('DBG', '>1', "hasGroupsAccessToProjects({$idProject}). userGroups", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
  413. foreach ($groups as $idGroup) {
  414. $selectedUserGroupNames[$idGroup] = $userGroups[$idGroup];
  415. }
  416. DBG::_('DBG', '>1', "hasGroupsAccessToProjects({$idProject}). selectedUserGroupNames", $selectedUserGroupNames, __CLASS__, __FUNCTION__, __LINE__);
  417. if ($idProject >= 0) {
  418. if (array_key_exists($idProject, $this->_projectInfo)) {
  419. DBG::_('DBG', '>1', "hasGroupsAccessToProjects({$idProject}). _projectInfo[$idProject]", $this->_projectInfo[$idProject], __CLASS__, __FUNCTION__, __LINE__);
  420. $alcGroupRead = V::get('aclGroupRead', null, $this->_projectInfo[$idProject]);
  421. if (!$alcGroupRead) {
  422. return false;
  423. }
  424. if (in_array($alcGroupRead, $selectedUserGroupNames)) {
  425. return true;
  426. }
  427. }
  428. }
  429. return false;
  430. }
  431. private function _userHasAccessToProject($project) {
  432. $groups = $this->_getLdapGroupsNames();
  433. $userLogin = User::getLogin();
  434. if ($project->aclOwner == $userLogin) {
  435. return true;
  436. }
  437. else if (in_array($project->aclGroupRead, $groups)) {
  438. return true;
  439. }
  440. return false;
  441. }
  442. public function getUsedUserGroups() {
  443. $groups = array();
  444. $userGroups = $this->_getLdapGroupsNames();
  445. DBG::_('DBG', '>2', "getUsedUserGroups(). userGroups:", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
  446. foreach ($this->_projectInfo as $projectInfo) {
  447. if (!empty($projectInfo->aclGroupRead)) {
  448. $groupKey = array_search($projectInfo->aclGroupRead, $userGroups);
  449. if ($groupKey !== false) {
  450. $groups[$groupKey] = $projectInfo->aclGroupRead;
  451. }
  452. }
  453. }
  454. DBG::_('DBG', '>2', "getUsedUserGroups(). groups:", $groups, __CLASS__, __FUNCTION__, __LINE__);
  455. return $groups;
  456. }
  457. public function _getLdapGroupsNames() {
  458. $usrStorageMacOSX = UserStorageFactory::getStorage('MacOSX');
  459. if (!$usrStorageMacOSX) throw new Exception("Error storage 'MacOSX' not exists!");
  460. $userGroupsByZasobId = array();
  461. $userGroups = User::getLdapGroupsNames();
  462. DBG::_('DBG', '>3', "_getLdapGroupsNames(). userGroups:", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
  463. foreach ($userGroups as $uidGroup) {
  464. $idZasob = $usrStorageMacOSX->getGroupIdFromUid($uidGroup);
  465. if ($idZasob) {
  466. $userGroupsByZasobId[$idZasob] = $uidGroup;
  467. } else {
  468. //$userGroupsByZasobId[$uidGroup] = $uidGroup;
  469. }
  470. }
  471. DBG::_('DBG', '>3', "_getLdapGroupsNames(). userGroupsByZasobId:", $userGroupsByZasobId, __CLASS__, __FUNCTION__, __LINE__);
  472. return $userGroupsByZasobId;
  473. }
  474. private function _reacountCostsFromKoresp() {
  475. $projMonthHasCostSelfIds = array();
  476. foreach ($this->_costs as $kProjId => $vProjInfo) {
  477. $projectPath = $this->_projectInfo[$kProjId]->path;
  478. foreach ($vProjInfo->korespByMonth as $kMonthNum => $vKorespList) {
  479. $this->_createCostIfNotDefined($kProjId, $kMonthNum);
  480. foreach ($vKorespList as $vKoresp) {
  481. $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->COST_SELF += $vKoresp->COST;
  482. $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->INCOME_SELF += $vKoresp->INCOME;
  483. }
  484. $projHasCostSelfIds[$kProjId][$kMonthNum] = $projectPath;
  485. }
  486. }
  487. //echo'<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto;">$projHasCostSelfIds: ';print_r($projHasCostSelfIds);echo'</pre>';
  488. foreach ($projHasCostSelfIds as $kProjId => $vProjMonthsList) {
  489. if ($kProjId <= 0) continue;
  490. foreach ($vProjMonthsList as $kMonthNum => $vProjPath) {
  491. $vProjPathIds = explode('-', $vProjPath);
  492. $vProjPathIds = array_reverse($vProjPathIds);
  493. $vProjMonthCostSelf = $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->COST_SELF;
  494. $vProjMonthIncomeSelf = $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->INCOME_SELF;
  495. foreach ($vProjPathIds as $vProjId) {
  496. if ($vProjId == $kProjId) continue;
  497. $this->_createCostIfNotDefined($vProjId, $kMonthNum);
  498. $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->COST_CHILD += $vProjMonthCostSelf;
  499. $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->INCOME_CHILD += $vProjMonthIncomeSelf;
  500. }
  501. }
  502. }
  503. // recount total
  504. foreach ($this->_costs as $vProjId => $vProjInfo) {
  505. foreach ($vProjInfo->costsByMonth as $kMonthNum => $vCost) {
  506. $this->_createCostIfNotDefined($vProjId, $kMonthNum);
  507. $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->COST_TOTAL = $vCost->COST_SELF + $vCost->COST_CHILD;
  508. $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->INCOME_TOTAL = $vCost->INCOME_SELF + $vCost->INCOME_CHILD;
  509. }
  510. }
  511. }
  512. private function _createCostIfNotDefined($projId, $monthNum) {
  513. if (empty($this->_costs[$projId]->costsByMonth[$monthNum])) {
  514. $vEmptyCost = new stdClass();
  515. $vEmptyCost->COST_SELF = 0;
  516. $vEmptyCost->INCOME_SELF = 0;
  517. $vEmptyCost->COST_CHILD = 0;
  518. $vEmptyCost->INCOME_CHILD = 0;
  519. $vEmptyCost->COST_TOTAL = 0;
  520. $vEmptyCost->INCOME_TOTAL = 0;
  521. $this->_costs[$projId]->costsByMonth[$monthNum] = $vEmptyCost;
  522. }
  523. }
  524. public function _buildProjectTree() {
  525. $this->_projectPathsOrder = array();
  526. foreach ($this->_projectInfo as $idProject => $projectInfo) {
  527. $this->_projectPathsOrder[$projectInfo->path] = $idProject;
  528. }
  529. //echo'<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto;">projPaths: ';print_r($this->_projectPathsOrder);echo'</pre>';
  530. uksort($this->_projectPathsOrder, array($this, 'sortPathsCallback'));
  531. //echo'<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto;">projPaths sorted: ';print_r($this->_projectPathsOrder);echo'</pre>';
  532. return $this->_projectPathsOrder;
  533. }
  534. public function sortPathsCallback($a, $b) {
  535. $ea = explode('-', $a);
  536. $eb = explode('-', $b);
  537. $la = count($ea);
  538. $lb = count($eb);
  539. $lmin = min($la, $lb);
  540. for ($i = 0; $i < $lmin; $i++) {
  541. if ($ea[$i] < $eb[$i]) {
  542. return -1;
  543. } else if ($ea[$i] > $eb[$i]) {
  544. return 1;
  545. }
  546. }
  547. return $la - $lb;
  548. }
  549. public function updatePaths() {
  550. $sqlList = array();
  551. $sqlList['updateAllPaths'] = <<<SQL
  552. update `projects_budget_year_month` b
  553. set path = (select coalesce(
  554. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=b.`ID_PROJECT` limit 1)
  555. , '?'));
  556. SQL;
  557. $db = DB::getDB();
  558. if ($db->has_errors()) {
  559. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  560. }
  561. foreach ($sqlList as $sqlName => $sql) {
  562. $res = $db->query($sql);
  563. if ($db->has_errors()) {
  564. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  565. }
  566. }
  567. }
  568. public function reinstall() {
  569. $sqlList = array();
  570. $sqlList['RemoveTrigger_BudgetPlan_BeforeInsert'] = "DROP TRIGGER IF EXISTS `projects_budget_year_month_BEFORE_INSERT`";
  571. $sqlList['CreateTrigger_BudgetPlan_BeforeInsert'] = "
  572. CREATE DEFINER=`root`@`localhost` TRIGGER `projects_budget_year_month_BEFORE_INSERT` BEFORE INSERT ON `projects_budget_year_month`
  573. FOR EACH ROW BEGIN
  574. IF NEW.ID_PROJECT IS NOT NULL and NEW.ID_PROJECT>0 THEN
  575. SET NEW.path = (select coalesce(
  576. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  577. , '?'
  578. ));
  579. END IF;
  580. END
  581. ";
  582. $sqlList['RemoveTrigger_BudgetPlan_BeforeUpdate'] = "DROP TRIGGER IF EXISTS `projects_budget_year_month_BEFORE_UPDATE `";
  583. // throws errors:
  584. // #1146 - Table '{DATABASE_NAME}.P5-MSG:Route_FixZasobPath:ERROR: Loop detected ID=PARENT_ID' doesn't exist
  585. // #1146 - Table '{DATABASE_NAME}.P5-MSG:Route_FixZasobPath:ERROR: Parent item not exists' doesn't exist
  586. // #1146 - Table '{DATABASE_NAME}.P5-MSG:Route_FixZasobPath:ERROR: Loop detected in path' doesn't exist
  587. $sqlList['CreateTrigger_BudgetPlan_BeforeUpdate'] = "
  588. CREATE DEFINER=`root`@`localhost` TRIGGER `projects_budget_year_month_BEFORE_UPDATE` BEFORE UPDATE ON `projects_budget_year_month`
  589. FOR EACH ROW BEGIN
  590. IF NEW.ID_PROJECT IS NULL THEN
  591. SET NEW.path = '';
  592. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  593. IF NEW.ID_PROJECT>0 THEN
  594. SET NEW.path = (select coalesce(
  595. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  596. , '?'));
  597. ELSE
  598. SET NEW.path = '';
  599. END IF;
  600. END IF;
  601. END
  602. ";
  603. $db = DB::getDB();
  604. if ($db->has_errors()) {
  605. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  606. }
  607. foreach ($sqlList as $sqlName => $sql) {
  608. $res = $db->query($sql);
  609. if ($db->has_errors()) {
  610. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  611. }
  612. }
  613. }
  614. public function planAction() {
  615. SE_Layout::gora();
  616. SE_Layout::menu();
  617. $args = array();
  618. $args['year'] = V::get('year', '', $_REQUEST, 'int');
  619. if ($args['year'] > 0) {
  620. $_REQUEST['ff_YEAR'] = $_GET['ff_YEAR'] = $args['year'];
  621. }
  622. $this->menu($args['year']);
  623. if ($args['year'] > 0) {
  624. ?>
  625. <div class="container">
  626. <a class="btn btn-xs btn-default" href="#">Utwórz plan na kolejny rok na podstawie danych z <?php echo $args['year']; ?> roku</a>
  627. </div>
  628. <?php
  629. }
  630. $zasobObj = ProcesHelper::getZasobTableInfoByUri('default_db/projects_budget_year_month');
  631. if (!$zasobObj) {
  632. ?>
  633. <div class="alert alert-danger">
  634. Zasob Tabela Plan budżetu (projects_budget_year_month) nie istnieje
  635. </div>
  636. <?php
  637. // TODO: btn utwórz
  638. SE_Layout::dol();
  639. return;
  640. }
  641. $userAcl = User::getAcl();
  642. $userAcl->fetchGroups();
  643. if (!$userAcl->hasTableAcl($zasobObj->ID)) {
  644. ?>
  645. <div class="alert alert-danger">
  646. Brak uprawnień do tabeli Plan budżetu (projects_budget_year_month)
  647. </div>
  648. <?php
  649. SE_Layout::dol();
  650. return;
  651. }
  652. $tblAcl = $userAcl->getTableAcl($zasobObj->ID);
  653. $forceTblAclInit = ('1' == V::get('_force', '', $_GET));
  654. $tblAcl->init($forceTblAclInit);
  655. $forceFilterInit = array();
  656. $filterInit = new stdClass();
  657. $filterInit->currSortCol = 'ID';
  658. $filterInit->currSortFlip = 'desc';
  659. foreach ($_GET as $k => $v) {
  660. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && !empty($v)) {// filter prefix
  661. $filterInit->$k = $v;
  662. }
  663. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && !empty($v)) {// special filter prefix
  664. $filterInit->$k = $v;
  665. }
  666. else if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  667. $fldName = substr($k, 3);
  668. $forceFilterInit[$fldName] = $v;
  669. }
  670. }
  671. $tbl = new TableAjax($tblAcl);
  672. $tblLabel = array();
  673. if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
  674. if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
  675. $tblLabel = implode(" - ", $tblLabel);
  676. $tbl->setLabel($tblLabel);
  677. $tbl->setFilterInit($filterInit);
  678. if (!empty($forceFilterInit)) $tbl->setForceFilterInit($forceFilterInit);
  679. $tbl->addRowFunction('edit');
  680. $tbl->addRowFunction('hist');
  681. $tbl->addRowFunction('files');
  682. $tbl->addRowFunction('cp');
  683. $tbl->showProcesInit(false);
  684. echo $tbl->render();
  685. SE_Layout::dol();
  686. }
  687. }