Course.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. $customerRows = \db('产品_基本资料')
  27. ->where('客户编号', '<>', '')
  28. ->field('rtrim(客户编号) as 客户编号, MAX(rtrim(客户名称)) as 客户名称')
  29. ->group('客户编号')
  30. ->order('客户编号 asc')
  31. ->select();
  32. $productCustomers = [];
  33. foreach ($customerRows as $row) {
  34. $customerCode = rtrim((string)$row['客户编号']);
  35. if ($customerCode === '') {
  36. continue;
  37. }
  38. $productCustomers[] = [
  39. 'code' => $customerCode,
  40. 'name' => rtrim((string)$row['客户名称']),
  41. ];
  42. }
  43. $orders = \db('工单_基本资料')
  44. ->where('行号', '1')
  45. ->where('gd_statu', 'in', ['2-生产中', '3-计划中'])
  46. ->field('rtrim(成品代号) as 成品代号, rtrim(Gd_客户代号) as Gd_客户代号, MAX(rtrim(Gd_客户名称)) as Gd_客户名称')
  47. ->group('成品代号, Gd_客户代号')
  48. ->order('Gd_客户代号 asc')
  49. ->select();
  50. if (empty($productCustomers) && empty($orders)) {
  51. $this->success('', []);
  52. }
  53. $ordersByInitial = [];
  54. foreach ($orders as $index => $order) {
  55. $orders[$index]['成品代号'] = rtrim((string)$order['成品代号']);
  56. $orders[$index]['Gd_客户代号'] = rtrim((string)$order['Gd_客户代号']);
  57. $orders[$index]['Gd_客户名称'] = rtrim((string)$order['Gd_客户名称']);
  58. $productCode = $orders[$index]['成品代号'];
  59. if ($productCode !== '') {
  60. $ordersByInitial[strtoupper($productCode[0])][] = $orders[$index];
  61. }
  62. }
  63. $data = [
  64. '印刷工单' => [],
  65. '糊盒工单' => [],
  66. ];
  67. $usedCodes = [];
  68. $appendCustomer = function ($code, $name) use (&$data, &$usedCodes) {
  69. $code = rtrim((string)$code);
  70. $codeKey = strtoupper($code);
  71. if ($code === '' || isset($usedCodes[$codeKey])) {
  72. return;
  73. }
  74. $usedCodes[$codeKey] = true;
  75. $label = $code . rtrim((string)$name);
  76. if (strpos($code, 'J') !== false) {
  77. $data['糊盒工单'][] = $label;
  78. } elseif (strpos($code, 'Y') !== false) {
  79. $data['印刷工单'][] = $label;
  80. }
  81. };
  82. foreach ($productCustomers as $customer) {
  83. $customerCode = $customer['code'];
  84. $customerName = $customer['name'];
  85. $matchedOrders = [];
  86. foreach ($ordersByInitial[strtoupper($customerCode[0])] ?? [] as $order) {
  87. if (stripos($order['成品代号'], $customerCode) === 0) {
  88. $matchedOrders[] = $order;
  89. }
  90. }
  91. if (empty($matchedOrders)) {
  92. continue;
  93. }
  94. if ($customerName !== '') {
  95. $appendCustomer($customerCode, $customerName);
  96. continue;
  97. }
  98. foreach ($matchedOrders as $order) {
  99. if ($order['Gd_客户代号'] === '' && $order['Gd_客户名称'] === '') {
  100. continue;
  101. }
  102. $appendCustomer(
  103. $order['Gd_客户代号'] !== '' ? $order['Gd_客户代号'] : $customerCode,
  104. $order['Gd_客户名称']
  105. );
  106. }
  107. }
  108. foreach ($orders as $order) {
  109. $productCode = $order['成品代号'];
  110. $matchedProductCustomer = false;
  111. if ($productCode !== '') {
  112. foreach ($productCustomers as $customer) {
  113. $customerCode = $customer['code'];
  114. if ($customerCode !== '' && strcasecmp($productCode[0], $customerCode[0]) === 0 && stripos($productCode, $customerCode) === 0) {
  115. $matchedProductCustomer = true;
  116. break;
  117. }
  118. }
  119. }
  120. if ($matchedProductCustomer || ($order['Gd_客户代号'] === '' && $order['Gd_客户名称'] === '')) {
  121. continue;
  122. }
  123. $appendCustomer($order['Gd_客户代号'], $order['Gd_客户名称']);
  124. }
  125. $this->success('成功', $data);
  126. }
  127. /**
  128. * 右侧工单详情
  129. * @return void
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. * @throws \think\exception\DbException
  133. */
  134. public function workOrderDetail()
  135. {
  136. if ($this->request->isGet() === false) {
  137. $this->error('请求错误');
  138. }
  139. $param = $this->request->param();
  140. $where = [];
  141. if (!empty($param['order'])) {
  142. $where['a.Gd_cpdh|a.成品代号'] = ['like', $param['order'] . '%'];
  143. }
  144. if (!empty($param['search'])) {
  145. $where['a.Gd_gdbh|a.Gd_cpmc|a.成品名称'] = ['like', '%' . $param['search'] . '%'];
  146. }
  147. $list = \db('工单_基本资料')
  148. ->alias('a')
  149. ->join('工单_印件资料 b', 'a.Gd_gdbh = b.Yj_Gdbh')
  150. ->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 计划投料')
  151. ->where($where)
  152. ->where('a.gd_statu', '2-生产中')
  153. ->where('a.Gd_gdbh', 'like', 'Y%')
  154. ->group('b.Yj_Gdbh,b.yj_Yjno')
  155. ->select();
  156. if (empty($list)) {
  157. $this->success('');
  158. }
  159. $orderNos = [];
  160. foreach ($list as $row) {
  161. $orderNos[$row['工单编号']] = $row['工单编号'];
  162. }
  163. $processRows = \db('工单_工艺资料')
  164. ->alias('a')
  165. ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_yjno = b.sczl_yjno AND a.Gy0_gxh = b.sczl_gxh', 'left')
  166. ->where('a.Gy0_gdbh', 'in', array_values($orderNos))
  167. ->field('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh, a.Gy0_gxmc, a.PD_WG, SUM(b.sczl_cl) as 产量')
  168. ->group('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh')
  169. ->order('a.Gy0_gxh asc')
  170. ->select();
  171. $latestNumSql = \db('设备_产量计酬')
  172. ->where('sczl_gdbh', 'in', array_values($orderNos))
  173. ->field('sczl_gdbh, sczl_yjno, sczl_gxh, MAX(sczl_num) as max_num')
  174. ->group('sczl_gdbh, sczl_yjno, sczl_gxh')
  175. ->buildSql();
  176. $latestRows = \db('设备_产量计酬')
  177. ->alias('t')
  178. ->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')
  179. ->field('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, rtrim(t.sczl_num) as sczl_num, MAX(t.sczl_rq) as sczl_rq')
  180. ->group('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, t.sczl_num')
  181. ->select();
  182. $latestMap = [];
  183. foreach ($latestRows as $latest) {
  184. $latestKey = rtrim((string)$latest['sczl_gdbh']) . '|' . rtrim((string)$latest['sczl_yjno']) . '|' . (int)$latest['sczl_gxh'];
  185. $latestMap[$latestKey] = $latest;
  186. }
  187. $processMap = [];
  188. foreach ($processRows as $process) {
  189. $mapKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']);
  190. $processMap[$mapKey][] = $process;
  191. }
  192. foreach ($list as $key => $value) {
  193. $mapKey = rtrim((string)$value['工单编号']) . '|' . rtrim((string)$value['yj_Yjno']);
  194. $index = 1;
  195. foreach ($processMap[$mapKey] ?? [] as $process) {
  196. $status = ($process['PD_WG'] !== '1900-01-01 00:00:00') ? '完工' : '';
  197. $latestKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']) . '|' . (int)$process['Gy0_gxh'];
  198. $latest = $latestMap[$latestKey] ?? [];
  199. $flowNo = rtrim((string)($latest['sczl_num'] ?? ''));
  200. if ($flowNo === '0') {
  201. $flowNo = '';
  202. }
  203. $reportDate = (string)($latest['sczl_rq'] ?? '');
  204. if ($reportDate !== '') {
  205. $reportDate = substr($reportDate, 0, 10);
  206. }
  207. $list[$key]['工序' . $index] = $value['yj_Yjno'] . '-' . $process['Gy0_gxh'] . '-' . rtrim((string)$process['Gy0_gxmc']) . '[' . $process['产量'] . '/' . ']' . $status . '[' . $flowNo . '/' . $reportDate . ']';
  208. $index++;
  209. }
  210. unset($list[$key]['yj_Yjno']);
  211. }
  212. $this->success('成功', $list);
  213. }
  214. }