ProjectKosztorysSchema.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. class ProjectKosztorysSchema {
  3. public static function getSchema() {
  4. static $_schema = null;
  5. if (null !== $_schema) return $_schema;
  6. /*
  7. 22444 INNE Kosztorys - zasoby
  8. 22445 INNE Kabel 2J do wdmuchiwania w mikrorurce 7/4 mm z wdmuchiwaniem
  9. 22446 INNE Kabel 4J do wdmuchiwania w mikrorurce 7/4 mm z wdmuchiwaniem
  10. ... Labels for order form
  11. 22460 TABELA Światłowód (Alias do [20225] TABELA default_db/Rozdzielcza_Kabel_Swiatlowodowy_wsg84)
  12. 22461 20299 KOMORKA ZASOB (Alias do [20299] KOMORKA OznKabla)
  13. 22462 20292 KOMORKA JEDNOSTKA_METR ilość [m] (Alias do [20292] KOMORKA Dlugosc)
  14. ... where to search for data
  15. */
  16. {
  17. $pdo = DB::getPDO();
  18. $sth = $pdo->prepare("
  19. select z.ID, z.TYPE, z.DESC, z.ALIAS_ID
  20. , c.ID as c_ID, c.TYPE as c_TYPE, c.DESC as c_DESC, c.ALIAS_ID as c_ALIAS_ID
  21. , a.ID as a_ID, a.TYPE as a_TYPE, a.DESC as a_DESC, a.ALIAS_ID as a_ALIAS_ID
  22. , za.ID as za_ID, za.TYPE as za_TYPE, za.DESC as za_DESC, za.ALIAS_ID as za_ALIAS_ID
  23. from CRM_LISTA_ZASOBOW z
  24. left join CRM_LISTA_ZASOBOW c on(c.PARENT_ID = z.ID)
  25. left join CRM_LISTA_ZASOBOW a on(a.ID = z.ALIAS_ID and z.ALIAS_ID > 0)
  26. left join CRM_LISTA_ZASOBOW za on(za.ID = c.ALIAS_ID and c.ALIAS_ID > 0)
  27. where z.PARENT_ID = 22444 -- TODO how to find ID, Typespecial with link
  28. ");
  29. $sth->execute();
  30. $rawConf = $sth->fetchAll();
  31. $conf = array();
  32. $conf['type'] = array();
  33. $conf['layer'] = array();
  34. foreach ($rawConf as $z) {
  35. if ('INNE' == $z['TYPE']) {
  36. if (!array_key_exists($z['ID'], $conf['type'])) {
  37. $conf['type'][$z['ID']] = $z['DESC'];
  38. }
  39. }
  40. }
  41. foreach ($rawConf as $z) {
  42. if ('TABELA' == $z['TYPE']) {
  43. if (!array_key_exists($z['ID'], $conf['layer'])) {
  44. $layer = array();
  45. $layer['label'] = $z['DESC'];
  46. $layer['tabela_id'] = $z['a_ID'];
  47. $layer['tabela_name'] = $z['a_DESC'];
  48. $layer['jednostka'] = '';
  49. $layer['zasob_type'] = '';
  50. $layer['zasob_field'] = '';
  51. $layer['zasob_id'] = '';
  52. $layer['ilosc_field'] = '';
  53. $layer['type'] = array();
  54. $conf['layer'][$z['ID']] = $layer;
  55. }
  56. if ($z['c_ID'] > 0) {
  57. if ('JEDNOSTKA_' == substr($z['c_DESC'], 0, 10)) {
  58. $layer = $conf['layer'][$z['ID']];
  59. $layer['jednostka'] = substr($z['c_DESC'], 10);
  60. $layer['ilosc_field'] = $z['za_DESC'];
  61. $layer['zasob_id'] = $z['za_ID'];
  62. $conf['layer'][$z['ID']] = $layer;
  63. }
  64. /*
  65. [za_ID] => 20299
  66. [za_TYPE] => KOMORKA
  67. [za_DESC] => OznKabla
  68. [za_ALIAS_ID] => 0
  69. */
  70. switch ($z['c_DESC']) {
  71. case 'ZASOB': {
  72. if ($z['za_ID'] > 0) {
  73. $layer = $conf['layer'][$z['ID']];
  74. $layer['zasob_type'] = $z['c_DESC'];
  75. $layer['zasob_field'] = $z['za_DESC'];
  76. $layer['zasob_id'] = $z['za_ID'];
  77. $conf['layer'][$z['ID']] = $layer;
  78. }
  79. } break;
  80. case 'ZASOB_ID': {
  81. if ($z['za_ID'] > 0) {
  82. $layer = $conf['layer'][$z['ID']];
  83. $layer['zasob_type'] = $z['c_DESC'];
  84. $layer['zasob_id'] = $z['za_ID'];
  85. $layer['zasob_label'] = $z['za_DESC'];
  86. $conf['layer'][$z['ID']] = $layer;
  87. $conf['layer'][$z['ID']]['type'][$z['za_ID']] = $z['za_DESC'];
  88. }
  89. } break;
  90. case 'TYPE': {
  91. //DBG::_(true, true, "z", $z, __CLASS__, __FUNCTION__, __LINE__);
  92. if (empty($z['c_ALIAS_ID'])) throw new Exception("Schema error - brak ALIAS_ID dla typu");
  93. if (empty($conf['type'][$z['za_ID']])) ;// TODO: throw exception
  94. $conf['layer'][$z['ID']]['type'][$z['za_ID']] = $z['za_DESC'];
  95. } break;
  96. }
  97. }
  98. }
  99. }
  100. foreach ($rawConf as $z) {
  101. if ('INNE' == $z['TYPE']) {
  102. } else if ('TABELA' == $z['TYPE']) {
  103. } else {
  104. SE_Layout::alert('warnig', "BUG: unimplemented type '{$z['TYPE']}' for zasob nr: {$z['ID']}");
  105. }
  106. }
  107. DBG::_('DBG', '>1', "config", $conf, __CLASS__, __FUNCTION__, __LINE__);
  108. {// validate schema - show warnings
  109. foreach ($conf['layer'] as $layer) {
  110. try {
  111. if (empty($layer['tabela_id'])) throw new Exception("brak zdefiniowanego aliasa do tabeli dla warstwy {$layer['label']}");
  112. if (empty($layer['jednostka'])) throw new Exception("brak jednostki dla warstwy [{$layer['tabela_id']}] {$layer['label']}");
  113. switch ($layer['jednostka']) {
  114. case 'METR': break;
  115. case 'SZTUKA': break;
  116. default: throw new Exception("Nieznana jednostka '{$layer['jednostka']}' dla warstwy [{$layer['tabela_id']}] {$layer['label']}");
  117. }
  118. } catch (Exception $e) {
  119. SE_Layout::alert('warning', $e->getMessage());
  120. }
  121. }
  122. }
  123. DBG::_('DBG', '>2', "rawConf", $rawConf, __CLASS__, __FUNCTION__, __LINE__);
  124. $_schema['config'] = $conf;
  125. }
  126. $_schema['nr'] = "Nr projektu";
  127. $_schema['title'] = "Tytuł projektu";
  128. $_schema['ownerName'] = "Osoba prowadząca";
  129. $_schema['cost_total'] = "Szacowany koszt projektu [zł]";
  130. return $_schema;
  131. }
  132. public static function getLayerJednostka($idLayer) {
  133. $schema = self::getSchema();
  134. if (empty($schema['config']['layer'][$idLayer])) return null;// TODO: throw exception?
  135. return $schema['config']['layer'][$idLayer]['jednostka'];
  136. }
  137. }