functions.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. use app\models\entity\Accounts;
  3. use app\models\entity\Log;
  4. use app\models\entity\Stack;
  5. use app\models\entity\Storage;
  6. use app\models\entity\Tmc;
  7. use app\models\entity\TmcStatus;
  8. use app\models\entity\Warehouse;
  9. use yii\db\Query;
  10. /** Created by Anton on 14.02.2020. */
  11. function addTmc(array $devices, $tmc)
  12. {
  13. if (!isset($devices['total'])) {//Инициализация переменных в массиве
  14. $devices['total'] = 0;
  15. $devices['issued'] = 0;
  16. $devices['inCharge'] = 0;
  17. }
  18. $devices['total']++;
  19. switch ($tmc['status']) {
  20. case 1:
  21. $devices['issued']++;
  22. break;
  23. case 2: case 6:
  24. $devices['inCharge']++;
  25. break;
  26. }
  27. $devices['list'][] = $tmc;
  28. return $devices;
  29. }
  30. function getColorByStatus(int $status)
  31. {
  32. $color = 'loco_';
  33. switch ($status) {
  34. case 1:
  35. $color .= 'red';
  36. break;
  37. case 3:
  38. $color .= 'blue';
  39. break;
  40. default:
  41. $color .= 'grey';
  42. break;
  43. }
  44. return $color;
  45. }
  46. function getTmcs()
  47. {
  48. $query = new Query();
  49. $query->select('t.id, t.title, t.tmc_type, t.status, t.employee, ta.name, ts.title as storage')
  50. ->from(Tmc::tableName() . ' t')
  51. ->leftJoin(Accounts::tableName() . ' ta', 'ta.id = t.employee')
  52. ->leftJoin(Storage::tableName() . ' ts', 'ts.id = t.storage')
  53. ;
  54. return $query->all();
  55. }
  56. function getTmcsByStatus(int $status)
  57. {
  58. $query = new Query();
  59. $query->select('t.id, t.title, t.tmc_type, t.status, t.employee, ta.name, ts.title as storage')
  60. ->from(Tmc::tableName() . ' t')
  61. ->leftJoin(Accounts::tableName() . ' ta', 'ta.id = t.employee')
  62. ->leftJoin(Storage::tableName() . ' ts', 'ts.id = t.storage')
  63. ->where(['t.status' => $status])
  64. ;
  65. return $query->all();
  66. }
  67. function sortTmc($tmcs)
  68. {
  69. $cameras = [];
  70. $phones = [];
  71. $oilMeters = [];
  72. foreach ($tmcs as $tmc) {
  73. switch ($tmc['tmc_type']) {
  74. case 1:
  75. $cameras = addTmc($cameras, $tmc);
  76. break;
  77. case 2:
  78. $oilMeters = addTmc($oilMeters, $tmc);
  79. break;
  80. case 3:
  81. $phones = addTmc($phones, $tmc);
  82. break;
  83. }
  84. }
  85. return [$cameras, $phones, $oilMeters];
  86. }
  87. function getDataStorages()
  88. {
  89. $query = new Query();
  90. $query->select('ts.title as storage, ts.warehouse, tw.title as w_title, ts.stack, tstack.title as s_title,
  91. ts.tmc, t.title as name, tstat.title as status, tstat.id as id_status, td.issue_date, ta.name AS user')
  92. ->from(Storage::tableName() . ' ts')
  93. ->leftJoin(Tmc::tableName() . ' t', 'ts.tmc = t.id')
  94. ->leftJoin(Log::tableName() . ' td', 'td.tmc = ts.tmc')
  95. ->leftJoin(Accounts::tableName() . ' ta', 'ta.id = td.employee')
  96. ->leftJoin(TmcStatus::tableName() . ' tstat', 't.status = tstat.id')
  97. ->leftJoin(Warehouse::tableName() . ' tw', 'tw.id = ts.warehouse')
  98. ->leftJoin(Stack::tableName() . ' tstack', 'tstack.id = ts.stack')
  99. ->groupBy('ts.id')//todo: надо ли?
  100. ;
  101. return $query->all();
  102. }
  103. function getDevicesFromRows(array $rows)
  104. {
  105. /*
  106. * [warehouseId => [
  107. * 'title' => string,
  108. * 'stacks' => [
  109. * stackId => [
  110. * 'title' => string,
  111. * 'tmcs' => [
  112. * key => value,
  113. * ...
  114. * ]
  115. * ],
  116. * ...
  117. * ],
  118. * ...
  119. * ]
  120. * */
  121. $devices = [];
  122. foreach ($rows as $row) {
  123. $devices[$row['warehouse']]['title'] = $row['w_title'];
  124. $devices[$row['warehouse']]['stacks'][$row['s_title']]['title'] = $row['s_title'];
  125. $devices[$row['warehouse']]['stacks'][$row['s_title']]['tmcs'][] = $row;
  126. }
  127. return $devices;
  128. }
  129. function sortDevicesAlph(array $devices)
  130. {
  131. //Сортируем в алфавитном порядке по title стеллажа
  132. foreach ($devices as $key => $device) {
  133. ksort($device['stacks']);
  134. $devices[$key] = $device;
  135. }
  136. return $devices;
  137. }