ManagementModel.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use app\models\TasksModel;
  6. use \app\models\entity\Accounts;
  7. use app\models\entity\ProjectsLocotech;
  8. use app\models\entity\JobtypesTasktypes;
  9. use app\models\entity\Tasktypes;
  10. /**
  11. * This is the model class for table "s_regions".
  12. *
  13. * The followings are the available columns in table 's_regions':
  14. * @property integer $rg_id
  15. * @property string $rg_name
  16. */
  17. class ManagementModel extends Model
  18. {
  19. public $errors;
  20. const STATUS_DONE = 5;
  21. //=====================================================
  22. //
  23. // ФОРМИРУЕТ ЕДИНЫЙ МАССИВ ДЛЯ РЕНДЕРА В ПРЕДСТАВЛЕНИЕ
  24. //
  25. //=====================================================
  26. public function mainFuncController(){
  27. $arResult = [];
  28. $arResult['accountList'] = $this->getAccountList();
  29. $arResult['locomotiveList'] = $this->getLocomotiveList();
  30. return $arResult;
  31. }
  32. //=============================================
  33. //
  34. // СПИСОК СОТРУДНИКОВ ОНЛАЙН ДЛЯ ЛЕВОЙ ТАБЛИЧКИ
  35. //
  36. //=============================================
  37. private function getAccountList(){
  38. global $companyID;
  39. $result = [];
  40. $query = "SELECT ai.id, ai.name, ai.last_seen_mobile, aj.priority, aj.jobtype_id, jt.name as job_name
  41. FROM accounts_internal ai
  42. LEFT JOIN accounts_jobtypes aj ON aj.account_id = ai.id
  43. right join jobtypes jt on jt.id = aj.jobtype_id
  44. WHERE ai.company=". $companyID;
  45. $res = Yii::$app->db->createCommand($query)->queryAll();
  46. $tempArray = [];
  47. foreach($res as $item) {
  48. $last_seen_mobile = ( null != $item['last_seen_mobile'] )? new \DateTime($item['last_seen_mobile']): $item['last_seen_mobile'] ;
  49. $online_mobile = OnlineAccountCheck(TimeString($last_seen_mobile));
  50. if (!$online_mobile) { continue;}
  51. $tempArray[$item['id']][] = $item;
  52. $tempArray[$item['id']]['name'] = $item['name'];
  53. }
  54. $result = $tempArray;
  55. return $result;
  56. }
  57. //===========================================
  58. //
  59. // ОСНОВНОЙ СПИСОК ЛОКОМОТИВОВ
  60. //
  61. //===========================================
  62. private function getLocomotiveList(){
  63. global $companyID;
  64. $result = [];
  65. //var_dump($companyID);
  66. $projectsLocotechEntity = ProjectsLocotech::find()->where(['company' => $companyID] )->orderBy(['id' => SORT_DESC])->all();
  67. $listArray = [];
  68. foreach( $projectsLocotechEntity as $item){ //var_dump($item->id);echo "\n";
  69. //$tempArray = GetTasksForProject($item->id);
  70. $status = GetProjectStatus($item->id);
  71. if ($status != 2 && $status != 1) {continue;}
  72. $countDone = ['done' => 0, 'all' => 0 ];
  73. foreach( GetTasksForProject($item->id) as $proj) { //var_dump($proj);echo "<br>";
  74. $countDone['all']++;
  75. $tempArray = $proj;
  76. $tempArray['status'] = $this->CheckAcceptedStatus($proj);
  77. $tempArray['name'] = GetTaskNameAndDesc($proj['type'])[0];
  78. $tempArray['listAccounts'] = $this->getAccountListForTask($proj['type']);
  79. if ( self::STATUS_DONE == $tempArray['status'] ) {$countDone['done']++;}
  80. //$countDone['all'] ." / " . $countDone['done'];
  81. $listArray[] = $tempArray;
  82. }
  83. $result[$item->id]['countDone'] = [ 'done' => $countDone['done'], 'all' => $countDone['all'] ];
  84. //$tempArray['name'] = GetTaskNameAndDesc($tempArray['type'])[0];
  85. $result[$item->id]['arr'] = $listArray;//GetTasksForProject($item->id);
  86. $result[$item->id]['obj'] = $item;
  87. $listArray = [];
  88. }
  89. //exit;
  90. return $result;
  91. }
  92. //======================================================
  93. //
  94. // В СПИСКЕ ЛОКОМОТИВОВ ВЫПАДАЮЩИЙ СПИСОК С СОТРУДНИКАМИ
  95. //
  96. //======================================================
  97. private function getAccountListForTask($type){
  98. $query = "select a.id, a.name
  99. from accounts a
  100. where a.id in
  101. (select aj.account_id
  102. from accounts_jobtypes aj
  103. where aj.jobtype_id in (select jt.jobtype_id
  104. from jobtypes_tasktypes jt
  105. where tasktype_id in (select tt1.id
  106. from tasktypes tt
  107. right join tasktypes tt1 on INSTR(tt.subtasks, tt1.id) > 0
  108. where tt.id in (". $type ."))))
  109. and timestampdiff(MINUTE, a.last_seen_mobile, NOW()) < 10;";
  110. $result = Yii::$app->db->createCommand($query)->queryAll();
  111. return $result;
  112. }
  113. //======================================================
  114. //
  115. // ПРОВЕРКА СТАТУСА ЗАДАЧИ (ПРИНЯТА ОНА ИЛИ НЕТ)
  116. //
  117. //======================================================
  118. private function CheckAcceptedStatus($task) {
  119. $result = false;
  120. /*var_dump($task);
  121. $tasktypesEntity = Tasktypes::findOne($task);
  122. // var_dump($tasktypesEntity);
  123. if ( null == $task['finished_time'] ){
  124. if ( null == $task['accepted_time'] ) {
  125. $result = 1;
  126. } else {
  127. $result = 2;
  128. $acceptedTime = strtotime($task['accepted_time']);
  129. $expectedTime = ($tasktypesEntity->time_to_complete_minutes * 60) + $acceptedTime;
  130. if ( strtotime("now") > $expectedTime) {
  131. $result = 6;
  132. }
  133. }
  134. } else {
  135. $result = 5;
  136. }
  137. */
  138. $query = "select if(tx.total = tx.finished, 5, if (tx.accepted <> tx.finished, if(tx.attention > 0, 7, 2), if (tx.accepted = 0, 1, 3))) as taskstatus
  139. from (select count(t.id) as total,
  140. sum(if(t.accepted_time is not null, 1, 0)) as accepted,
  141. sum(if(t.finished_time is not null, 1, 0)) as finished,
  142. sum(if(t.finished_time is null, if(timestampdiff(MINUTE, t.accepted_time, NOW()) > tt.time_to_complete_minutes, 1, 0), 0)) as attention
  143. from tasks t
  144. right join tasktypes tt on t.type = tt.id
  145. where t.parent_id = ".$task['id'].") tx;";
  146. $result = Yii::$app->db->createCommand($query)->queryAll();
  147. $result = $result[0]["taskstatus"];
  148. return $result;
  149. }
  150. }