Budget.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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?v=2.1"></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. , k.`K_DATA_OTRZYMANEJ_KORESP`
  291. , k.`K_NR_OTRZYM_KORESP`
  292. , k.`K_OD_KOGO`
  293. from `IN7_DZIENNIK_KORESP` k
  294. where ((k.`COST_VALUE` != 0) or (k.`INCOME_VALUE` != 0))
  295. and k.`K_DATA_OTRZYMANEJ_KORESP` like '{$year}-%'
  296. -- TODO: acl
  297. ";
  298. //echo'<pre style="border:1px solid red;overflow:auto;max-height:400px">';print_r($sql);echo'</pre>';
  299. $res = $db->query($sql);
  300. while ($r = $db->fetch($res)) {
  301. $vProjId = ($r->ID_PROJECT > 0)? $r->ID_PROJECT : 0;
  302. $vProjPaths = array();
  303. $vProjIds = array(0 => true);
  304. if ($r->ID_PROJECT > 0) {
  305. $vProjPaths[] = $r->ID_PROJECT;
  306. if (!empty($r->path)) {
  307. $vProjPaths[] = $r->path;
  308. }
  309. }
  310. if ($r->TRANSFER_OPPOSITE_ID_PROJECT > 0) {
  311. $vProjPaths[] = $r->TRANSFER_OPPOSITE_ID_PROJECT;
  312. if (!empty($r->TRANSFER_OPPOSITE_PROJECT_PATH)) {
  313. $vProjPaths[] = $r->TRANSFER_OPPOSITE_PROJECT_PATH;
  314. }
  315. }
  316. if (!empty($vProjPaths)) {
  317. //echo'<p>DBG:$r->ID_PROJECT['.$r->ID_PROJECT.']: $vProjPaths = ' . json_encode($vProjPaths) . '</p>';
  318. $vProjPaths = implode('-', $vProjPaths);
  319. $projIds = explode('-', $vProjPaths);
  320. //echo'<p>DBG:$r->ID_PROJECT['.$r->ID_PROJECT.']: $projIds = ' . json_encode($projIds) . '</p>';
  321. foreach ($projIds as $vProjId) {
  322. if ($vProjId > 0) $vProjIds[$vProjId] = true;
  323. }
  324. }
  325. {
  326. $projectZeroInfo = new stdClass();
  327. $projectZeroInfo->ID_PROJECT = 0;
  328. $projectZeroInfo->costsByMonth = array();
  329. $projectZeroInfo->korespByMonth = array();
  330. $this->_costs[0] = $projectZeroInfo;
  331. }
  332. foreach ($vProjIds as $vProjId => $vBool) {
  333. if (!array_key_exists($vProjId, $this->_costs)) {
  334. $projectInfo = new stdClass();
  335. $projectInfo->ID_PROJECT = $vProjId;
  336. $projectInfo->costsByMonth = array();
  337. $projectInfo->korespByMonth = array();
  338. $this->_costs[$vProjId] = $projectInfo;
  339. }
  340. }
  341. $korespInfo = new stdClass();
  342. $korespInfo->ID = $r->ID;
  343. $korespInfo->MONTH = $r->MONTH;
  344. $korespInfo->K_ZAWARTOS = $r->K_ZAWARTOS;
  345. $korespInfo->K_DATA_OTRZYMANEJ_KORESP = $r->K_DATA_OTRZYMANEJ_KORESP;
  346. $korespInfo->K_NR_OTRZYM_KORESP = $r->K_NR_OTRZYM_KORESP;
  347. $korespInfo->K_OD_KOGO = $r->K_OD_KOGO;
  348. $monthNum = intval(substr($r->MONTH, 5, 2));
  349. if ($r->ID_PROJECT > 0) {
  350. if ($r->TRANSFER_OPPOSITE_ID_PROJECT > 0) {
  351. $korespOppositeInfo = clone $korespInfo;
  352. $korespInfo->COST = $r->COST;
  353. $korespInfo->INCOME = $r->INCOME;
  354. $korespInfo->TRANSFER_OPPOSITE_ID_PROJECT_TO = $r->TRANSFER_OPPOSITE_ID_PROJECT;
  355. $this->_costs[$r->ID_PROJECT]->korespByMonth[$monthNum][] = $korespInfo;
  356. $korespOppositeInfo->COST = -1 * $r->COST;
  357. $korespOppositeInfo->INCOME = -1 * $r->INCOME;
  358. $korespOppositeInfo->TRANSFER_OPPOSITE_ID_PROJECT_FROM = $r->ID_PROJECT;
  359. $this->_costs[$r->TRANSFER_OPPOSITE_ID_PROJECT]->korespByMonth[$monthNum][] = $korespOppositeInfo;
  360. } else {
  361. $korespInfo->COST = $r->COST;
  362. $korespInfo->INCOME = $r->INCOME;
  363. $this->_costs[$r->ID_PROJECT]->korespByMonth[$monthNum][] = $korespInfo;
  364. }
  365. } else {
  366. $korespInfo->COST = $r->COST;
  367. $korespInfo->INCOME = $r->INCOME;
  368. $this->_costs[0]->korespByMonth[$monthNum][] = $korespInfo;
  369. }
  370. }
  371. return $this->_costs;
  372. }
  373. private function _fetchProjectInfo() {
  374. $db = DB::getDB();
  375. $hasAccessForAllProjects = true;
  376. $projectIds = array();
  377. $projectsFromCostIds = array_keys($this->_costs);
  378. foreach ($projectsFromCostIds as $idProject) $projectIds[$idProject] = true;
  379. $projectsFromPlanIds = array_keys($this->_plan);
  380. foreach ($projectsFromPlanIds as $idProject) $projectIds[$idProject] = true;
  381. foreach ($projectIds as $idProject => $vBool) $this->_projectInfo[$idProject] = new stdClass();
  382. $projectIds = array_keys($projectIds);
  383. $sqlProjIds = "'" . implode("','", $projectIds) . "'";
  384. $sql = "
  385. select p.`ID`
  386. , p.`P_ID`
  387. , p.`path`
  388. , p.`M_DIST_DESC`
  389. , p.`A_ADM_COMPANY` as aclGroupWrite
  390. , p.`A_CLASSIFIED` as aclGroupRead
  391. , p.`L_APPOITMENT_USER` as aclOwner
  392. from `IN7_MK_BAZA_DYSTRYBUCJI` p
  393. where p.`ID` in({$sqlProjIds})
  394. ";
  395. $res = $db->query($sql);
  396. while ($r = $db->fetch($res)) {
  397. $this->_projectInfo[$r->ID]->path = $r->path;
  398. $this->_projectInfo[$r->ID]->M_DIST_DESC = $r->M_DIST_DESC;
  399. $this->_projectInfo[$r->ID]->aclGroupRead = $r->aclGroupRead;
  400. $this->_projectInfo[$r->ID]->hasAccess = $this->_userHasAccessToProject($r);
  401. if (!$this->_projectInfo[$r->ID]->hasAccess) $hasAccessForAllProjects = false;
  402. }
  403. $this->_projectInfo[0]->path = "0";
  404. $this->_projectInfo[0]->M_DIST_DESC = "Wszystkie projekty";
  405. $this->_projectInfo[0]->hasAccess = $hasAccessForAllProjects;
  406. }
  407. public function hasAccessToProject($idProject) {
  408. if ($idProject >= 0) {
  409. if (array_key_exists($idProject, $this->_projectInfo)) {
  410. return V::get('hasAccess', false, $this->_projectInfo[$idProject]);
  411. }
  412. }
  413. return false;
  414. }
  415. public function hasGroupsAccessToProjects($idProject, $groups) {
  416. $selectedUserGroupNames = array();
  417. $userGroups = $this->_getLdapGroupsNames();
  418. DBG::_('DBG', '>1', "hasGroupsAccessToProjects({$idProject}). userGroups", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
  419. foreach ($groups as $idGroup) {
  420. $selectedUserGroupNames[$idGroup] = $userGroups[$idGroup];
  421. }
  422. DBG::_('DBG', '>1', "hasGroupsAccessToProjects({$idProject}). selectedUserGroupNames", $selectedUserGroupNames, __CLASS__, __FUNCTION__, __LINE__);
  423. if ($idProject >= 0) {
  424. if (array_key_exists($idProject, $this->_projectInfo)) {
  425. DBG::_('DBG', '>1', "hasGroupsAccessToProjects({$idProject}). _projectInfo[$idProject]", $this->_projectInfo[$idProject], __CLASS__, __FUNCTION__, __LINE__);
  426. $alcGroupRead = V::get('aclGroupRead', null, $this->_projectInfo[$idProject]);
  427. if (!$alcGroupRead) {
  428. return false;
  429. }
  430. if (in_array($alcGroupRead, $selectedUserGroupNames)) {
  431. return true;
  432. }
  433. }
  434. }
  435. return false;
  436. }
  437. private function _userHasAccessToProject($project) {
  438. $groups = $this->_getLdapGroupsNames();
  439. $userLogin = User::getLogin();
  440. if ($project->aclOwner == $userLogin) {
  441. return true;
  442. }
  443. else if (in_array($project->aclGroupRead, $groups)) {
  444. return true;
  445. }
  446. return false;
  447. }
  448. public function getUsedUserGroups() {
  449. $groups = array();
  450. $userGroups = $this->_getLdapGroupsNames();
  451. DBG::_('DBG', '>2', "getUsedUserGroups(). userGroups:", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
  452. foreach ($this->_projectInfo as $projectInfo) {
  453. if (!empty($projectInfo->aclGroupRead)) {
  454. $groupKey = array_search($projectInfo->aclGroupRead, $userGroups);
  455. if ($groupKey !== false) {
  456. $groups[$groupKey] = $projectInfo->aclGroupRead;
  457. }
  458. }
  459. }
  460. DBG::_('DBG', '>2', "getUsedUserGroups(). groups:", $groups, __CLASS__, __FUNCTION__, __LINE__);
  461. return $groups;
  462. }
  463. public function _getLdapGroupsNames() {
  464. $usrStorageMacOSX = UserStorageFactory::getStorage('MacOSX');
  465. if (!$usrStorageMacOSX) throw new Exception("Error storage 'MacOSX' not exists!");
  466. $userGroupsByZasobId = array();
  467. $userGroups = User::getLdapGroupsNames();
  468. DBG::_('DBG', '>3', "_getLdapGroupsNames(). userGroups:", $userGroups, __CLASS__, __FUNCTION__, __LINE__);
  469. foreach ($userGroups as $uidGroup) {
  470. $idZasob = $usrStorageMacOSX->getGroupIdFromUid($uidGroup);
  471. if ($idZasob) {
  472. $userGroupsByZasobId[$idZasob] = $uidGroup;
  473. } else {
  474. //$userGroupsByZasobId[$uidGroup] = $uidGroup;
  475. }
  476. }
  477. DBG::_('DBG', '>3', "_getLdapGroupsNames(). userGroupsByZasobId:", $userGroupsByZasobId, __CLASS__, __FUNCTION__, __LINE__);
  478. return $userGroupsByZasobId;
  479. }
  480. private function _reacountCostsFromKoresp() {
  481. $projMonthHasCostSelfIds = array();
  482. foreach ($this->_costs as $kProjId => $vProjInfo) {
  483. $projectPath = $this->_projectInfo[$kProjId]->path;
  484. foreach ($vProjInfo->korespByMonth as $kMonthNum => $vKorespList) {
  485. $this->_createCostIfNotDefined($kProjId, $kMonthNum);
  486. foreach ($vKorespList as $vKoresp) {
  487. $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->COST_SELF += $vKoresp->COST;
  488. $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->INCOME_SELF += $vKoresp->INCOME;
  489. }
  490. $projHasCostSelfIds[$kProjId][$kMonthNum] = $projectPath;
  491. }
  492. }
  493. //echo'<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto;">$projHasCostSelfIds: ';print_r($projHasCostSelfIds);echo'</pre>';
  494. foreach ($projHasCostSelfIds as $kProjId => $vProjMonthsList) {
  495. if ($kProjId <= 0) continue;
  496. foreach ($vProjMonthsList as $kMonthNum => $vProjPath) {
  497. $vProjPathIds = explode('-', $vProjPath);
  498. $vProjPathIds = array_reverse($vProjPathIds);
  499. $vProjMonthCostSelf = $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->COST_SELF;
  500. $vProjMonthIncomeSelf = $this->_costs[$kProjId]->costsByMonth[$kMonthNum]->INCOME_SELF;
  501. foreach ($vProjPathIds as $vProjId) {
  502. if ($vProjId == $kProjId) continue;
  503. $this->_createCostIfNotDefined($vProjId, $kMonthNum);
  504. $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->COST_CHILD += $vProjMonthCostSelf;
  505. $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->INCOME_CHILD += $vProjMonthIncomeSelf;
  506. }
  507. }
  508. }
  509. // recount total
  510. foreach ($this->_costs as $vProjId => $vProjInfo) {
  511. foreach ($vProjInfo->costsByMonth as $kMonthNum => $vCost) {
  512. $this->_createCostIfNotDefined($vProjId, $kMonthNum);
  513. $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->COST_TOTAL = $vCost->COST_SELF + $vCost->COST_CHILD;
  514. $this->_costs[$vProjId]->costsByMonth[$kMonthNum]->INCOME_TOTAL = $vCost->INCOME_SELF + $vCost->INCOME_CHILD;
  515. }
  516. }
  517. }
  518. private function _createCostIfNotDefined($projId, $monthNum) {
  519. if (empty($this->_costs[$projId]->costsByMonth[$monthNum])) {
  520. $vEmptyCost = new stdClass();
  521. $vEmptyCost->COST_SELF = 0;
  522. $vEmptyCost->INCOME_SELF = 0;
  523. $vEmptyCost->COST_CHILD = 0;
  524. $vEmptyCost->INCOME_CHILD = 0;
  525. $vEmptyCost->COST_TOTAL = 0;
  526. $vEmptyCost->INCOME_TOTAL = 0;
  527. $this->_costs[$projId]->costsByMonth[$monthNum] = $vEmptyCost;
  528. }
  529. }
  530. public function _buildProjectTree() {
  531. $this->_projectPathsOrder = array();
  532. foreach ($this->_projectInfo as $idProject => $projectInfo) {
  533. $this->_projectPathsOrder[$projectInfo->path] = $idProject;
  534. }
  535. //echo'<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto;">projPaths: ';print_r($this->_projectPathsOrder);echo'</pre>';
  536. uksort($this->_projectPathsOrder, array($this, 'sortPathsCallback'));
  537. //echo'<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto;">projPaths sorted: ';print_r($this->_projectPathsOrder);echo'</pre>';
  538. return $this->_projectPathsOrder;
  539. }
  540. public function sortPathsCallback($a, $b) {
  541. $ea = explode('-', $a);
  542. $eb = explode('-', $b);
  543. $la = count($ea);
  544. $lb = count($eb);
  545. $lmin = min($la, $lb);
  546. for ($i = 0; $i < $lmin; $i++) {
  547. if ($ea[$i] < $eb[$i]) {
  548. return -1;
  549. } else if ($ea[$i] > $eb[$i]) {
  550. return 1;
  551. }
  552. }
  553. return $la - $lb;
  554. }
  555. public function updatePaths() {
  556. $sqlList = array();
  557. $sqlList['updateAllPaths'] = <<<SQL
  558. update `projects_budget_year_month` b
  559. set path = (select coalesce(
  560. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=b.`ID_PROJECT` limit 1)
  561. , '?'));
  562. SQL;
  563. $db = DB::getDB();
  564. if ($db->has_errors()) {
  565. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  566. }
  567. foreach ($sqlList as $sqlName => $sql) {
  568. $res = $db->query($sql);
  569. if ($db->has_errors()) {
  570. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  571. }
  572. }
  573. }
  574. public function reinstall() {
  575. $sqlList = array();
  576. $sqlList['RemoveTrigger_BudgetPlan_BeforeInsert'] = "DROP TRIGGER IF EXISTS `projects_budget_year_month_BEFORE_INSERT`";
  577. $sqlList['CreateTrigger_BudgetPlan_BeforeInsert'] = "
  578. CREATE DEFINER=`root`@`localhost` TRIGGER `projects_budget_year_month_BEFORE_INSERT` BEFORE INSERT ON `projects_budget_year_month`
  579. FOR EACH ROW BEGIN
  580. IF NEW.ID_PROJECT IS NOT NULL and NEW.ID_PROJECT>0 THEN
  581. SET NEW.path = (select coalesce(
  582. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  583. , '?'
  584. ));
  585. END IF;
  586. END
  587. ";
  588. $sqlList['RemoveTrigger_BudgetPlan_BeforeUpdate'] = "DROP TRIGGER IF EXISTS `projects_budget_year_month_BEFORE_UPDATE `";
  589. // throws errors:
  590. // #1146 - Table '{DATABASE_NAME}.P5-MSG:Route_FixZasobPath:ERROR: Loop detected ID=PARENT_ID' doesn't exist
  591. // #1146 - Table '{DATABASE_NAME}.P5-MSG:Route_FixZasobPath:ERROR: Parent item not exists' doesn't exist
  592. // #1146 - Table '{DATABASE_NAME}.P5-MSG:Route_FixZasobPath:ERROR: Loop detected in path' doesn't exist
  593. $sqlList['CreateTrigger_BudgetPlan_BeforeUpdate'] = "
  594. CREATE DEFINER=`root`@`localhost` TRIGGER `projects_budget_year_month_BEFORE_UPDATE` BEFORE UPDATE ON `projects_budget_year_month`
  595. FOR EACH ROW BEGIN
  596. IF NEW.ID_PROJECT IS NULL THEN
  597. SET NEW.path = '';
  598. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  599. IF NEW.ID_PROJECT>0 THEN
  600. SET NEW.path = (select coalesce(
  601. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  602. , '?'));
  603. ELSE
  604. SET NEW.path = '';
  605. END IF;
  606. END IF;
  607. END
  608. ";
  609. $db = DB::getDB();
  610. if ($db->has_errors()) {
  611. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  612. }
  613. foreach ($sqlList as $sqlName => $sql) {
  614. $res = $db->query($sql);
  615. if ($db->has_errors()) {
  616. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  617. }
  618. }
  619. }
  620. public function planAction() {
  621. SE_Layout::gora();
  622. SE_Layout::menu();
  623. $args = array();
  624. $args['year'] = V::get('year', '', $_REQUEST, 'int');
  625. if ($args['year'] > 0) {
  626. $_REQUEST['ff_YEAR'] = $_GET['ff_YEAR'] = $args['year'];
  627. }
  628. $this->menu($args['year']);
  629. if ($args['year'] > 0) {
  630. ?>
  631. <div class="container">
  632. <a class="btn btn-xs btn-default" href="#">Utwórz plan na kolejny rok na podstawie danych z <?php echo $args['year']; ?> roku</a>
  633. </div>
  634. <?php
  635. }
  636. $zasobObj = ProcesHelper::getZasobTableInfoByUri('default_db/projects_budget_year_month');
  637. if (!$zasobObj) {
  638. ?>
  639. <div class="alert alert-danger">
  640. Zasob Tabela Plan budżetu (projects_budget_year_month) nie istnieje
  641. </div>
  642. <?php
  643. // TODO: btn utwórz
  644. SE_Layout::dol();
  645. return;
  646. }
  647. $userAcl = User::getAcl();
  648. $userAcl->fetchGroups();
  649. if (!$userAcl->hasTableAcl($zasobObj->ID)) {
  650. ?>
  651. <div class="alert alert-danger">
  652. Brak uprawnień do tabeli Plan budżetu (projects_budget_year_month)
  653. </div>
  654. <?php
  655. SE_Layout::dol();
  656. return;
  657. }
  658. $tblAcl = $userAcl->getTableAcl($zasobObj->ID);
  659. $forceTblAclInit = ('1' == V::get('_force', '', $_GET));
  660. $tblAcl->init($forceTblAclInit);
  661. $forceFilterInit = array();
  662. $filterInit = new stdClass();
  663. $filterInit->currSortCol = 'ID';
  664. $filterInit->currSortFlip = 'desc';
  665. foreach ($_GET as $k => $v) {
  666. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && !empty($v)) {// filter prefix
  667. $filterInit->$k = $v;
  668. }
  669. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && !empty($v)) {// special filter prefix
  670. $filterInit->$k = $v;
  671. }
  672. else if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  673. $fldName = substr($k, 3);
  674. $forceFilterInit[$fldName] = $v;
  675. }
  676. }
  677. $tbl = new TableAjax($tblAcl);
  678. $tblLabel = array();
  679. if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
  680. if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
  681. $tblLabel = implode(" - ", $tblLabel);
  682. $tbl->setLabel($tblLabel);
  683. $tbl->setFilterInit($filterInit);
  684. if (!empty($forceFilterInit)) $tbl->setForceFilterInit($forceFilterInit);
  685. $tbl->addRowFunction('edit');
  686. $tbl->addRowFunction('hist');
  687. $tbl->addRowFunction('files');
  688. $tbl->addRowFunction('cp');
  689. $tbl->showProcesInit(false);
  690. echo $tbl->render();
  691. SE_Layout::dol();
  692. }
  693. }