ProcesHelper.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. class ProcesHelper {
  3. public static function get_wskazniki( $id ) {
  4. $ret = array();
  5. $sql = "select
  6. clz.*
  7. , w.`ID` as CW_ID, w.`TYP` as CW_TYP, w.`OPIS_ZASOB` as OPIS_ZASOB, w.`SORT_PRIO` as CW_SORT_PRIO, w.`ID_PRZYPADEK` as CW_ID_PRZYPADEK
  8. , w.`A_HAS_IMAGE` as CW_A_HAS_IMAGE
  9. , cp.`PYTANIE` as CP_PYTANIE, cp.`OPIS` as CP_OPIS, cp.`FORM_TREAT` as CP_FORM_TREAT
  10. from `CRM_WSKAZNIK` as w
  11. left join `CRM_LISTA_ZASOBOW` as clz on (clz.`ID`=w.`ID_ZASOB`)
  12. left join `CRM_PRZYPADEK` as cp on (cp.`ID`=w.`ID_PRZYPADEK`)
  13. where
  14. w.`ID_PROCES`='".$id."'
  15. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  16. order by w.`SORT_PRIO` asc, w.`ID` asc
  17. ";
  18. $res = DB::query( $sql );
  19. while ($r = DB::fetch( $res )) {
  20. $ret[$r->CW_ID] = $r;
  21. }
  22. return $ret;
  23. }
  24. public static function getWskaznikiByIds($ids, $params = array()) {
  25. $wskazniki = array();
  26. if (empty($ids)) {
  27. return $wskazniki;
  28. }
  29. $db = DB::getDB();
  30. $sql = "select
  31. clz.*
  32. , w.`ID_PROCES`
  33. , w.`ID` as CW_ID, w.`TYP` as CW_TYP, w.`OPIS_ZASOB` as OPIS_ZASOB, w.`SORT_PRIO` as CW_SORT_PRIO, w.`ID_PRZYPADEK` as CW_ID_PRZYPADEK
  34. , w.`A_HAS_IMAGE` as CW_A_HAS_IMAGE
  35. , cp.`PYTANIE` as CP_PYTANIE, cp.`OPIS` as CP_OPIS, cp.`FORM_TREAT` as CP_FORM_TREAT
  36. from `CRM_WSKAZNIK` as w
  37. left join `CRM_LISTA_ZASOBOW` as clz on (clz.`ID`=w.`ID_ZASOB`)
  38. left join `CRM_PRZYPADEK` as cp on (cp.`ID`=w.`ID_PRZYPADEK`)
  39. where
  40. w.`ID_PROCES` in(" . implode(",", $ids) . ")
  41. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  42. order by w.`SORT_PRIO` asc, w.`ID` asc
  43. ";
  44. $res = $db->query($sql);
  45. while ($r = $db->fetch($res)) {
  46. $wskazniki[$r->ID_PROCES][$r->CW_ID] = $r;
  47. }
  48. if (1 == V::get('group_stanowiska', '', $params)) {
  49. foreach ($wskazniki as $kProcesId => $vWskazniki) {
  50. $stanowiska = array();
  51. foreach ($vWskazniki as $kWskId => $vWsk) {
  52. if ($vWsk->TYPE == 'STANOWISKO') {
  53. $stanowiska[$kWskId] = $vWsk;
  54. }
  55. }
  56. if (!empty($stanowiska)) {
  57. foreach ($stanowiska as $kWskId => $vWsk) {
  58. unset($wskazniki[$kProcesId][$kWskId]);
  59. }
  60. }
  61. $newWsk = array('stanowiska'=>$stanowiska, 'inne'=>$wskazniki[$kProcesId]);
  62. $wskazniki[$kProcesId] = $newWsk;
  63. }
  64. }
  65. return $wskazniki;
  66. }
  67. public static function get_list_for_user_count( $user_groups, $params = array() ) {
  68. $ret = array();
  69. $sql = self::_get_list_for_user_sql( $user_groups, $params, true );
  70. $res = DB::query( $sql );
  71. if ($r = DB::fetch( $res )) {
  72. $ret = $r->cnt;
  73. }
  74. return $ret;
  75. }
  76. public static function get_list_for_user( $user_groups, $params = array() ) {
  77. $ret = array();
  78. $sql = self::_get_list_for_user_sql( $user_groups, $params );
  79. $res = DB::query( $sql );
  80. while ($r = DB::fetch( $res )) {
  81. $ret []= $r;
  82. }
  83. return $ret;
  84. }
  85. /**
  86. * TODO: find user proces
  87. */
  88. public static function _get_list_for_user_sql( $user_groups, $params = array(), $count = false ) {
  89. $sql_select = array();
  90. $sql_limit = "";
  91. $sql_where = "";
  92. if ($count) {
  93. $sql_select []= "count(1) as cnt";
  94. } else {
  95. $sql_select []= "t.*";
  96. $sql_limit = V::get('limit', '10', $params);
  97. $sql_offset = V::get('offset', '0', $params);
  98. $sql_limit = "limit $sql_limit offset $sql_offset";
  99. }
  100. $sql_select = implode(", ", $sql_select);
  101. $sql = "select
  102. $sql_select
  103. from `CRM_PROCES_LOG` as t
  104. -- left join `` as p on(p.``=t.``)
  105. where
  106. $sql_where
  107. $sql_limit
  108. ";
  109. return $sql;
  110. }
  111. /**
  112. * Get allowed Stanowiska ID for given proces.
  113. * @param $proces_id - proces ID
  114. * @returns array of Stanowiska ID
  115. * Search recursively up by p.PARENT_ID, stop when find p.PROCES_INIT or find z.STANOWISKO
  116. */
  117. public static function get_allowed_stanowiska_by_proces_id( $proces_id ) {
  118. $ret = array();
  119. $proces_id = intval($proces_id);
  120. if ($proces_id <= 0) return $ret;
  121. // TODO: DB::get_by_id('CRM_PROCES', $proces_id);
  122. // TODO: check TYPE=PROCES_INIT -> get STANOWISKO and stop
  123. // TODO: find wskazniki STANOWISKO
  124. // TODO: if !empty return else recursive - TODO: add recursive limit!
  125. $sql = "select
  126. p.`TYPE` as p__TYPE
  127. , z.`ID` as z__ID
  128. , z.`TYPE` as z__TYPE
  129. from `CRM_PROCES` as p
  130. left join `CRM_WSKAZNIK` as w on(w.`ID_PROCES`=p.`ID`)
  131. left join `CRM_LISTA_ZASOBOW` as z on (z.`ID`=w.`ID_ZASOB`)
  132. where
  133. p.`ID`='".$proces_id."'
  134. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  135. and z.`TYPE`='STANOWISKO'
  136. ";
  137. $res = DB::query( $sql );
  138. while ($r = DB::fetch( $res )) {
  139. $ret []= $r->z__ID;
  140. }
  141. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">ret: ';print_r($ret);echo'</pre>';
  142. return $ret;
  143. }
  144. public static function getProcesByUser( $user_name, $groups ) {
  145. $ret = array();
  146. $sql = "select plog.*
  147. from `CRM_PROCES_LOG` as plog
  148. where
  149. ( plog.`A_RECORD_CREATE_AUTHOR`='".$user_name."'
  150. or plog.`A_RECORD_UPDATE_AUTHOR`='".$user_name."'
  151. )
  152. order by plog.ID DESC
  153. ";
  154. $res = DB::query($sql);
  155. while ($r = DB::fetch($res)) {
  156. $ret[$r->ID] = $r;
  157. }
  158. return $ret;
  159. }
  160. public static function split_wskazniki_by_table( &$wsk ) {
  161. $wsk_split = array();
  162. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">wsk: ';print_r($wsk);echo'</pre>';
  163. foreach ($wsk as $k_id => $w) {
  164. $w->_parents = TreeHelper::get_all_parents( 'CRM_LISTA_ZASOBOW', $w->PARENT_ID );
  165. }
  166. foreach ($wsk as $k_id => $w) {// $w->CW_ID == $k_id
  167. if ($w->TYPE == 'KOMORKA') {
  168. $last_wsk = end($wsk_split);
  169. if (empty($last_wsk)) {
  170. }
  171. } else if ($w->TYPE == 'DOKUMENTY') {
  172. // file
  173. }
  174. }//end foreach
  175. }
  176. /**
  177. * @param $stanowiska_id - array of integer
  178. */
  179. public static function get_procesy_by_stanowiska( $stanowiska_id = array() ) {
  180. $db = DB::getDB();
  181. $ret = array();
  182. if (empty($stanowiska_id)) {
  183. return $ret;
  184. }
  185. $sql_stanowiska_id = array();
  186. foreach ($stanowiska_id as $v_id) {
  187. $v_id = intval($v_id);
  188. if ($v_id > 0) {
  189. $sql_stanowiska_id []= "'" . $v_id . "'";
  190. }
  191. }
  192. if (empty($sql_stanowiska_id)) {
  193. return $ret;
  194. }
  195. $sql = "select
  196. p.`ID`
  197. , p.`PARENT_ID`
  198. , p.`TYPE`
  199. , p.`DESC`
  200. , w.`ID` as w__ID
  201. , w.`OPIS_ZASOB` as w__OPIS_ZASOB
  202. , z.`ID` as z__ID
  203. from `CRM_PROCES` as p
  204. left join `CRM_WSKAZNIK` as w on(w.`ID_PROCES`=p.`ID`)
  205. left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=w.`ID_ZASOB`)
  206. where
  207. p.`TYPE`='PROCES_INIT'
  208. and p.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  209. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  210. -- and w.`ID_PRZYPADEK`=1
  211. and z.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  212. and z.`TYPE`='STANOWISKO'
  213. and z.`ID` is not null
  214. and z.`ID` in (" . implode(",", $sql_stanowiska_id) . ")
  215. order by p.`TEST_SORT_PRIO` DESC, p.`ID` ASC
  216. ";
  217. $res = $db->query($sql);
  218. while ($r = $db->fetch($res)) {
  219. $ret []= $r;
  220. }
  221. return $ret;
  222. }
  223. /**
  224. * List of PROCES_INIT - only order info.
  225. * @return array ID => TEST_SORT_PRIO
  226. */
  227. public static function get_procesy_init_list_order() {
  228. $ret = array();
  229. $db = DB::getDB();
  230. $sql = "select
  231. p.`ID`
  232. , p.`TEST_SORT_PRIO`
  233. from `CRM_PROCES` as p
  234. where
  235. p.`TYPE`='PROCES_INIT'
  236. and p.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  237. order by p.`TEST_SORT_PRIO` DESC, p.`ID` ASC
  238. ";
  239. $res = $db->query($sql);
  240. while ($r = $db->fetch($res)) {
  241. $ret [$r->ID]= $r->TEST_SORT_PRIO;
  242. }
  243. return $ret;
  244. }
  245. /**
  246. * List of PROCES_INIT to sort.
  247. */
  248. public static function get_procesy_init_list() {
  249. $ret = array();
  250. $db = DB::getDB();
  251. $sql = "select
  252. p.`ID`
  253. , p.`PARENT_ID`
  254. , p.`TYPE`
  255. , p.`DESC`
  256. , p.`TEST_SORT_PRIO`
  257. -- , w.`ID` as w__ID
  258. -- , w.`OPIS_ZASOB` as w__OPIS_ZASOB
  259. -- , z.`ID` as z__ID
  260. from `CRM_PROCES` as p
  261. -- left join `CRM_WSKAZNIK` as w on(w.`ID_PROCES`=p.`ID`)
  262. -- left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=w.`ID_ZASOB`)
  263. where
  264. p.`TYPE`='PROCES_INIT'
  265. and p.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  266. -- and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  267. -- and w.`ID_PRZYPADEK`=1
  268. -- and z.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  269. -- and z.`TYPE`='STANOWISKO'
  270. -- and z.`ID` is not null
  271. order by p.`TEST_SORT_PRIO` DESC, p.`ID` ASC
  272. ";
  273. $res = $db->query($sql);
  274. while ($r = $db->fetch($res)) {
  275. $ret []= $r;
  276. }
  277. return $ret;
  278. }
  279. /**
  280. * Przeniesienie procesu w kolejności w testach.
  281. */
  282. public static function proces_init_move($proces_id, $sort_prio_dir) {
  283. $db = DB::getDB();
  284. $proces_list = self::get_procesy_init_list();
  285. $proces_list = array_reverse($proces_list);
  286. $wsk = array();
  287. $wsk_order = array();
  288. $sort_prio = 0;
  289. foreach ($proces_list as $r) {
  290. $wsk [$r->ID] = $sort_prio;//$r->SORT_PRIO;
  291. $wsk_order [$sort_prio]= $r->ID;
  292. $sort_prio += 1;
  293. }
  294. if (empty($wsk)) return;
  295. $wsk_new = array();
  296. if ($sort_prio_dir == 'dw') {// wskaznik $wsk_id 'w gore' (zmniejsz sort_prio)
  297. $old_wsk_id = $wsk[ $proces_id ];
  298. if ($old_wsk_id == 0) return;
  299. //echo'zamien "'.$proces_id.'" na "'.$wsk_order[ $wsk[ $proces_id ] - 1 ].'"';
  300. $tmp = $wsk_order[ $wsk[ $proces_id ] ];
  301. $wsk_order[ $wsk[ $proces_id ] ] = $wsk_order[ $wsk[ $proces_id ] - 1 ];
  302. $wsk_order[ $wsk[ $proces_id ] - 1 ] = $tmp;
  303. } else {// wskaznik $wsk_id 'w dol' (zwieksz sort_prio)
  304. $old_wsk_id = $wsk[ $proces_id ];
  305. if ($old_wsk_id + 1 == count($wsk)) return;
  306. //echo'zamien "'.$proces_id.'" na "'.$wsk_order[ $wsk[ $proces_id ] + 1 ].'"';
  307. $tmp = $wsk_order[ $wsk[ $proces_id ] ];
  308. $wsk_order[ $wsk[ $proces_id ] ] = $wsk_order[ $wsk[ $proces_id ] + 1 ];
  309. $wsk_order[ $wsk[ $proces_id ] + 1 ] = $tmp;
  310. }
  311. if (empty($wsk_order)) return;
  312. foreach ($wsk_order as $k_sort_prio => $v_proces_id) {
  313. $sql = "update `CRM_PROCES` set `TEST_SORT_PRIO`='{$k_sort_prio}' where `ID`='{$v_proces_id}'; ";
  314. $db->query( $sql );
  315. }
  316. return true;
  317. }
  318. public static function proces_flag($proces_id, $goto_id, $flag) {
  319. $ret = '';
  320. switch ($flag) {
  321. case 'GOTO':
  322. $ret = '<span style="color:#FF0000">' . "&rarr;" . '</span>' . $goto_id;
  323. break;
  324. case 'GOTO_AND_RETURN':
  325. $ret = '<span style="color:#FF0000">' . "&rarr;" . '</span>' . $goto_id . '<span style="color:#FF0000">' . "&crarr;" . '</span>';
  326. break;
  327. case 'FORK':
  328. $ret = '<span style="color:#FF0000">' . "&oplus;&rarr;" . '</span>' . $goto_id;
  329. break;
  330. }
  331. return $ret;
  332. }
  333. /**
  334. * Get proces GOTO data list.
  335. * @param array $params
  336. * $params['return_by']:
  337. * '' - default retrun array('ID'=>$row)
  338. * 'dest' - return array('IF_TRUE_GOTO'=>array('ID'=>$row))
  339. * @returns array
  340. */
  341. public static function get_goto_list($params = array()) {
  342. $ret = array();
  343. $return_by = V::get('return_by', '', $params);
  344. $db = DB::getDB();
  345. $sql = "select
  346. p.`ID`
  347. , p.`IF_TRUE_GOTO`
  348. , p.`IF_TRUE_GOTO_FLAG`
  349. from `CRM_PROCES` as p
  350. where
  351. p.`IF_TRUE_GOTO`>0
  352. and p.`PARENT_ID`>=0
  353. and p.`A_STATUS` in('WAITING','NORMAL')
  354. ";
  355. $res = $db->query($sql);
  356. while ($r = $db->fetch($res)) {
  357. if ($return_by == 'dest') {
  358. $ret[$r->IF_TRUE_GOTO] []= $r;
  359. } else {
  360. $ret[$r->ID] = $r;
  361. }
  362. }
  363. return $ret;
  364. }
  365. public static function getGoToProces($id) {
  366. static $_cache;
  367. if (!is_array($_cache)) {
  368. $_cache = array();
  369. $db = DB::getDB();
  370. //todo do optymalizacji ze struktur
  371. $sql="select p.`ID`, p.`IF_TRUE_GOTO`, p.`IF_TRUE_GOTO_FLAG`
  372. from `CRM_PROCES` as p
  373. where p.`IF_TRUE_GOTO` > 0
  374. ";
  375. $res = $db->query($sql);
  376. while ($r = $db->fetch($res)) {
  377. $_cache[$r->IF_TRUE_GOTO][$r->ID] = $r->IF_TRUE_GOTO_FLAG;
  378. }
  379. }
  380. $gotoList = array();
  381. if (array_key_exists($id, $_cache)) {
  382. $gotoList = $_cache[$id];
  383. }
  384. return $gotoList;
  385. }
  386. public static function get_wskaznik( $wsk_id ) {
  387. $ret = array();
  388. $sql = "select
  389. clz.*
  390. , w.`ID` as CW_ID, w.`TYP` as CW_TYP, w.`OPIS_ZASOB` as OPIS_ZASOB, w.`SORT_PRIO` as CW_SORT_PRIO, w.`ID_PRZYPADEK` as CW_ID_PRZYPADEK
  391. , w.`A_HAS_IMAGE` as CW_A_HAS_IMAGE
  392. , cp.`PYTANIE` as CP_PYTANIE, cp.`OPIS` as CP_OPIS, cp.`FORM_TREAT` as CP_FORM_TREAT
  393. from `CRM_WSKAZNIK` as w
  394. left join `CRM_LISTA_ZASOBOW` as clz on (clz.`ID`=w.`ID_ZASOB`)
  395. left join `CRM_PRZYPADEK` as cp on (cp.`ID`=w.`ID_PRZYPADEK`)
  396. where
  397. w.`ID`='" . $wsk_id . "'
  398. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  399. ";
  400. $res = DB::query( $sql );
  401. if ($r = DB::fetch( $res )) {
  402. $ret[$r->CW_ID] = $r;
  403. }
  404. return $ret;
  405. }
  406. public static function get_proces( $next_id ) {
  407. return DB::get_by_id('CRM_PROCES', $next_id);
  408. }
  409. public static function get_proces_init_by_zasob_id($id_zasob) {
  410. $db = DB::getDB();
  411. $sqlFilter = " cw.ID_ZASOB={$id_zasob}";
  412. if (is_array($id_zasob)) {
  413. $sqlFilter = " cw.ID_ZASOB in ".implode(',', $id_zasob);
  414. }
  415. $sql = "select
  416. cps.ID_PROCES_INIT
  417. , cw.ID_PROCES
  418. from CRM_WSKAZNIK as cw
  419. left join _CRM_PROCES_STATS_proc_wiev as cps on(cps.ID = cw.ID_PROCES)
  420. where cw.A_STATUS in('NORMAL', 'WAITING')
  421. and cps.ID_PROCES_INIT > 0 and
  422. {$sqlFilter}
  423. ";
  424. $res = $db->query($sql);
  425. while ($h = $db->fetch($res)) {
  426. $ret[] = $h->ID_PROCES_INIT;
  427. }
  428. return $ret;
  429. }
  430. public static function getZasobTableInfo($zasobID) {
  431. $zasobObj = null;
  432. $db = DB::getDB();
  433. $sql = "select z.`ID`, z.`DESC`, z.`DESC_PL`, z.`OPIS`
  434. , zp.`ID` as P__ID, zp.`DESC` as P__DESC, zp.`TYPE` as P__TYPE
  435. from `CRM_LISTA_ZASOBOW` as z
  436. left join `CRM_LISTA_ZASOBOW` as zp on(zp.`ID`=z.`PARENT_ID`)
  437. where z.`ID`='{$zasobID}'
  438. and z.`TYPE`='TABELA'
  439. ";
  440. $res = $db->query($sql);
  441. if ($res) {
  442. $zasobObj = $db->fetch($res);
  443. }
  444. return $zasobObj;
  445. }
  446. public static function getZasobInfo($zasobID) {
  447. $zasobObj = null;
  448. $db = DB::getDB();
  449. $sql = "select z.`ID`, z.`DESC`, z.`DESC_PL`, z.`OPIS`, z.`TYPE`
  450. , zp.`ID` as P__ID
  451. , zp.`DESC` as P__DESC
  452. , zp.`TYPE` as P__TYPE
  453. from `CRM_LISTA_ZASOBOW` as z
  454. left join `CRM_LISTA_ZASOBOW` as zp on(zp.`ID`=z.`PARENT_ID`)
  455. where z.`ID`='{$zasobID}'
  456. ";
  457. $res = $db->query($sql);
  458. if ($res) {
  459. $zasobObj = $db->fetch($res);
  460. }
  461. return $zasobObj;
  462. }
  463. public static function getZasobTableID($tblName) {
  464. $zasobID = 0;
  465. $db = DB::getDB();
  466. $sql = "select z.`ID`
  467. from `CRM_LISTA_ZASOBOW` as z
  468. where z.`DESC`='{$tblName}'
  469. and z.`TYPE`='TABELA'
  470. ";
  471. $res = $db->query($sql);
  472. if ($res) {
  473. $r = $db->fetch($res);
  474. $zasobID = $r->ID;
  475. }
  476. return $zasobID;
  477. }
  478. }