Course.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Request;
  6. use function fast\e;
  7. /**
  8. * 生产进程管理
  9. */
  10. class Course extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 左侧菜单
  16. * @return void
  17. * @throws \think\Exception
  18. * @throws \think\db\exception\BindParamException
  19. * @throws \think\exception\PDOException
  20. */
  21. public function getTab()
  22. {
  23. if ($this->request->isGet() === false) {
  24. $this->error('请求错误');
  25. }
  26. $customers = \db('产品_基本资料')
  27. ->where('客户编号', '<>', '')
  28. ->field('rtrim(客户编号) as 客户编号, MAX(rtrim(客户名称)) as 客户名称')
  29. ->group('客户编号')
  30. ->order('客户编号 asc')
  31. ->select();
  32. if (empty($customers)) {
  33. $this->success('', []);
  34. }
  35. $productCodes = \db('工单_基本资料')
  36. ->where('行号', '1')
  37. ->where('成品代号', '<>', '')
  38. ->where('gd_statu', 'in', ['2-生产中', '3-计划中'])
  39. ->distinct(true)
  40. ->column('成品代号');
  41. $codesByInitial = [];
  42. foreach ($productCodes as $productCode) {
  43. $productCode = rtrim((string)$productCode);
  44. if ($productCode === '') {
  45. continue;
  46. }
  47. $codesByInitial[$productCode[0]][] = $productCode;
  48. }
  49. $data = [
  50. '印刷工单' => [],
  51. '糊盒工单' => [],
  52. ];
  53. foreach ($customers as $value) {
  54. $customerCode = rtrim((string)$value['客户编号']);
  55. if ($customerCode === '') {
  56. continue;
  57. }
  58. $matched = false;
  59. foreach ($codesByInitial[$customerCode[0]] ?? [] as $productCode) {
  60. if (stripos($productCode, $customerCode) === 0) {
  61. $matched = true;
  62. break;
  63. }
  64. }
  65. if (!$matched) {
  66. continue;
  67. }
  68. $name = $customerCode . $value['客户名称'];
  69. if (strpos($customerCode, 'J') !== false) {
  70. $data['糊盒工单'][] = $name;
  71. } elseif (strpos($customerCode, 'Y') !== false) {
  72. $data['印刷工单'][] = $name;
  73. }
  74. }
  75. $this->success('成功', $data);
  76. }
  77. /**
  78. * 右侧工单详情
  79. * @return void
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. public function workOrderDetail()
  85. {
  86. if ($this->request->isGet() === false) {
  87. $this->error('请求错误');
  88. }
  89. $param = $this->request->param();
  90. $where = [];
  91. if (!empty($param['order'])) {
  92. $where['a.Gd_cpdh|a.成品代号'] = ['like', $param['order'] . '%'];
  93. }
  94. if (!empty($param['search'])) {
  95. $where['a.Gd_gdbh'] = ['like', '%' . $param['search'] . '%'];
  96. }
  97. $list = \db('工单_基本资料')
  98. ->alias('a')
  99. ->join('工单_印件资料 b', 'a.Gd_gdbh = b.Yj_Gdbh')
  100. ->field('b.Yj_Gdbh as 工单编号,b.yj_Yjno,b.yj_Yjdh as 印件代号,b.yj_yjmc as 印件名称,b.yj_ls as 联数,a.投料大箱 as 投料大箱,b.yj_平张投料 as 计划投料')
  101. ->where($where)
  102. ->where('a.gd_statu', '2-生产中')
  103. ->group('b.Yj_Gdbh,b.yj_Yjno')
  104. ->select();
  105. if (empty($list)) {
  106. $this->success('');
  107. }
  108. $orderNos = [];
  109. foreach ($list as $row) {
  110. $orderNos[$row['工单编号']] = $row['工单编号'];
  111. }
  112. $processRows = \db('工单_工艺资料')
  113. ->alias('a')
  114. ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_yjno = b.sczl_yjno AND a.Gy0_gxh = b.sczl_gxh', 'left')
  115. ->where('a.Gy0_gdbh', 'in', array_values($orderNos))
  116. ->field('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh, a.Gy0_gxmc, a.PD_WG, SUM(b.sczl_cl) as 产量')
  117. ->group('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh')
  118. ->order('a.Gy0_gxh asc')
  119. ->select();
  120. $latestNumSql = \db('设备_产量计酬')
  121. ->where('sczl_gdbh', 'in', array_values($orderNos))
  122. ->field('sczl_gdbh, sczl_yjno, sczl_gxh, MAX(sczl_num) as max_num')
  123. ->group('sczl_gdbh, sczl_yjno, sczl_gxh')
  124. ->buildSql();
  125. $latestRows = \db('设备_产量计酬')
  126. ->alias('t')
  127. ->join([$latestNumSql => 'm'], 't.sczl_gdbh = m.sczl_gdbh AND t.sczl_yjno = m.sczl_yjno AND t.sczl_gxh = m.sczl_gxh AND t.sczl_num = m.max_num')
  128. ->field('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, rtrim(t.sczl_num) as sczl_num, MAX(t.sczl_rq) as sczl_rq')
  129. ->group('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, t.sczl_num')
  130. ->select();
  131. $latestMap = [];
  132. foreach ($latestRows as $latest) {
  133. $latestKey = rtrim((string)$latest['sczl_gdbh']) . '|' . rtrim((string)$latest['sczl_yjno']) . '|' . (int)$latest['sczl_gxh'];
  134. $latestMap[$latestKey] = $latest;
  135. }
  136. $processMap = [];
  137. foreach ($processRows as $process) {
  138. $mapKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']);
  139. $processMap[$mapKey][] = $process;
  140. }
  141. foreach ($list as $key => $value) {
  142. $mapKey = rtrim((string)$value['工单编号']) . '|' . rtrim((string)$value['yj_Yjno']);
  143. $index = 1;
  144. foreach ($processMap[$mapKey] ?? [] as $process) {
  145. $status = ($process['PD_WG'] !== '1900-01-01 00:00:00') ? '完工' : '';
  146. $latestKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']) . '|' . (int)$process['Gy0_gxh'];
  147. $latest = $latestMap[$latestKey] ?? [];
  148. $flowNo = rtrim((string)($latest['sczl_num'] ?? ''));
  149. if ($flowNo === '0') {
  150. $flowNo = '';
  151. }
  152. $reportDate = (string)($latest['sczl_rq'] ?? '');
  153. if ($reportDate !== '') {
  154. $reportDate = substr($reportDate, 0, 10);
  155. }
  156. $list[$key]['工序' . $index] = $value['yj_Yjno'] . '-' . $process['Gy0_gxh'] . '-' . rtrim((string)$process['Gy0_gxmc']) . '[' . $process['产量'] . '/' . ']' . $status . '[' . $flowNo . '/' . $reportDate . ']';
  157. $index++;
  158. }
  159. unset($list[$key]['yj_Yjno']);
  160. }
  161. $this->success('成功', $list);
  162. }
  163. }