Course.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. $listQuery = \db('工单_基本资料')
  145. ->alias('a')
  146. ->join('工单_印件资料 b', 'a.Gd_gdbh = b.Yj_Gdbh')
  147. ->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 计划投料')
  148. ->where($where)
  149. ->where('a.gd_statu', '2-生产中')
  150. ->where('a.Gd_gdbh', 'like', 'Y%');
  151. $workOrderNo = trim((string)($param['工单编号'] ?? ''));
  152. if ($workOrderNo !== '') {
  153. $listQuery->where('a.Gd_gdbh', 'like', '%' . $this->courseLikeKeyword($workOrderNo) . '%');
  154. }
  155. $productName = trim((string)($param['产品名称'] ?? ''));
  156. if ($productName !== '') {
  157. $nameLike = '%' . $this->courseLikeKeyword($productName) . '%';
  158. $listQuery->where(function ($query) use ($nameLike) {
  159. $query->where('a.成品名称', 'like', $nameLike)
  160. ->whereOr('a.Gd_cpmc', 'like', $nameLike)
  161. ->whereOr('b.yj_yjmc', 'like', $nameLike);
  162. });
  163. }
  164. $salesOrderNo = trim((string)($param['销售订单号'] ?? ''));
  165. if ($salesOrderNo !== '') {
  166. $listQuery->where('a.销售订单号', 'like', '%' . $this->courseLikeKeyword($salesOrderNo) . '%');
  167. }
  168. $this->applyCourseRange($listQuery, 'a.投料大箱', $param['投料大箱起'] ?? '', $param['投料大箱止'] ?? '');
  169. $this->applyCourseRange($listQuery, 'a.接单日期', $param['接单日期起'] ?? '', $param['接单日期止'] ?? '', true);
  170. $this->applyCourseRange($listQuery, 'a.交货日期', $param['交货日期起'] ?? '', $param['交货日期止'] ?? '', true);
  171. $this->applyCourseRange($listQuery, 'b.yj_平张投料', $param['计划投料起'] ?? '', $param['计划投料止'] ?? '');
  172. $list = $listQuery
  173. ->group('b.Yj_Gdbh,b.yj_Yjno')
  174. ->select();
  175. if (empty($list)) {
  176. $this->success('');
  177. }
  178. $orderNos = [];
  179. foreach ($list as $row) {
  180. $orderNos[$row['工单编号']] = $row['工单编号'];
  181. }
  182. $processRows = \db('工单_工艺资料')
  183. ->alias('a')
  184. ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_yjno = b.sczl_yjno AND a.Gy0_gxh = b.sczl_gxh', 'left')
  185. ->where('a.Gy0_gdbh', 'in', array_values($orderNos))
  186. ->field('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh, a.Gy0_gxmc, a.PD_WG, SUM(b.sczl_cl) as 产量')
  187. ->group('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh')
  188. ->order('a.Gy0_gxh asc')
  189. ->select();
  190. $latestNumSql = \db('设备_产量计酬')
  191. ->where('sczl_gdbh', 'in', array_values($orderNos))
  192. ->field('sczl_gdbh, sczl_yjno, sczl_gxh, MAX(sczl_num) as max_num')
  193. ->group('sczl_gdbh, sczl_yjno, sczl_gxh')
  194. ->buildSql();
  195. $latestRows = \db('设备_产量计酬')
  196. ->alias('t')
  197. ->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')
  198. ->field('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, rtrim(t.sczl_num) as sczl_num, MAX(t.sczl_rq) as sczl_rq')
  199. ->group('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, t.sczl_num')
  200. ->select();
  201. $latestMap = [];
  202. foreach ($latestRows as $latest) {
  203. $latestKey = rtrim((string)$latest['sczl_gdbh']) . '|' . rtrim((string)$latest['sczl_yjno']) . '|' . (int)$latest['sczl_gxh'];
  204. $latestMap[$latestKey] = $latest;
  205. }
  206. $processMap = [];
  207. foreach ($processRows as $process) {
  208. $mapKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']);
  209. $processMap[$mapKey][] = $process;
  210. }
  211. foreach ($list as $key => $value) {
  212. $mapKey = rtrim((string)$value['工单编号']) . '|' . rtrim((string)$value['yj_Yjno']);
  213. $index = 1;
  214. foreach ($processMap[$mapKey] ?? [] as $process) {
  215. $status = ($process['PD_WG'] !== '1900-01-01 00:00:00') ? '完工' : '';
  216. $latestKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']) . '|' . (int)$process['Gy0_gxh'];
  217. $latest = $latestMap[$latestKey] ?? [];
  218. $flowNo = rtrim((string)($latest['sczl_num'] ?? ''));
  219. if ($flowNo === '0') {
  220. $flowNo = '';
  221. }
  222. $reportDate = (string)($latest['sczl_rq'] ?? '');
  223. if ($reportDate !== '') {
  224. $reportDate = substr($reportDate, 0, 10);
  225. }
  226. $list[$key]['工序' . $index] = $value['yj_Yjno'] . '-' . $process['Gy0_gxh'] . '-' . rtrim((string)$process['Gy0_gxmc']) . '[' . $process['产量'] . '/' . ']' . $status . '[' . $flowNo . '/' . $reportDate . ']';
  227. $index++;
  228. }
  229. unset($list[$key]['yj_Yjno']);
  230. }
  231. $this->success('成功', $list);
  232. }
  233. /**
  234. * 范围条件只在传入起或止时生效,日期止取到当天结束
  235. */
  236. private function applyCourseRange($query, $field, $from, $to, $isDate = false)
  237. {
  238. $from = trim((string)$from);
  239. $to = trim((string)$to);
  240. if ($from !== '') {
  241. $query->where($field, '>=', $from);
  242. }
  243. if ($to !== '') {
  244. if ($isDate && preg_match('/^\d{4}-\d{2}-\d{2}$/', $to)) {
  245. $to .= ' 23:59:59';
  246. }
  247. $query->where($field, '<=', $to);
  248. }
  249. }
  250. private function courseLikeKeyword($keyword)
  251. {
  252. return addcslashes((string)$keyword, '%_\\');
  253. }
  254. }