Course.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. $page = (int)($param['page'] ?? 1);
  141. $limit = (int)($param['limit'] ?? 20);
  142. if ($page < 1) {
  143. $page = 1;
  144. }
  145. if ($limit < 1) {
  146. $limit = 20;
  147. }
  148. if ($limit > 200) {
  149. $limit = 200;
  150. }
  151. $where = [];
  152. if (!empty($param['order'])) {
  153. $where['a.Gd_cpdh|a.成品代号'] = ['like', $param['order'] . '%'];
  154. }
  155. $listQuery = \db('工单_基本资料')
  156. ->alias('a')
  157. ->join('工单_印件资料 b', 'a.Gd_gdbh = b.Yj_Gdbh')
  158. ->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 计划投料')
  159. ->where($where)
  160. ->where('a.gd_statu', '2-生产中')
  161. ->where('a.Gd_gdbh', 'like', 'Y%');
  162. $workOrderNo = trim((string)($param['工单编号'] ?? ''));
  163. if ($workOrderNo !== '') {
  164. $listQuery->where('a.Gd_gdbh', 'like', '%' . $this->courseLikeKeyword($workOrderNo) . '%');
  165. }
  166. $productName = trim((string)($param['产品名称'] ?? ''));
  167. if ($productName !== '') {
  168. $nameLike = '%' . $this->courseLikeKeyword($productName) . '%';
  169. $listQuery->where(function ($query) use ($nameLike) {
  170. $query->where('a.成品名称', 'like', $nameLike)
  171. ->whereOr('a.Gd_cpmc', 'like', $nameLike)
  172. ->whereOr('b.yj_yjmc', 'like', $nameLike);
  173. });
  174. }
  175. $salesOrderNo = trim((string)($param['销售订单号'] ?? ''));
  176. if ($salesOrderNo !== '') {
  177. $listQuery->where('a.销售订单号', 'like', '%' . $this->courseLikeKeyword($salesOrderNo) . '%');
  178. }
  179. $this->applyCourseRange($listQuery, 'a.投料大箱', $param['投料大箱起'] ?? '', $param['投料大箱止'] ?? '');
  180. $this->applyCourseRange($listQuery, 'a.接单日期', $param['接单日期起'] ?? '', $param['接单日期止'] ?? '', true);
  181. $this->applyCourseRange($listQuery, 'a.交货日期', $param['交货日期起'] ?? '', $param['交货日期止'] ?? '', true);
  182. $this->applyCourseRange($listQuery, 'b.yj_平张投料', $param['计划投料起'] ?? '', $param['计划投料止'] ?? '');
  183. $total = (clone $listQuery)->group('b.Yj_Gdbh,b.yj_Yjno')->count();
  184. $list = $listQuery
  185. ->group('b.Yj_Gdbh,b.yj_Yjno')
  186. ->order('b.Yj_Gdbh asc, b.yj_Yjno asc')
  187. ->limit(($page - 1) * $limit, $limit)
  188. ->select();
  189. if (empty($list)) {
  190. $this->success('成功', [
  191. 'total' => (int)$total,
  192. 'page' => $page,
  193. 'limit' => $limit,
  194. 'list' => [],
  195. ]);
  196. }
  197. $orderNos = [];
  198. foreach ($list as $row) {
  199. $orderNos[$row['工单编号']] = $row['工单编号'];
  200. }
  201. $processRows = \db('工单_工艺资料')
  202. ->alias('a')
  203. ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_yjno = b.sczl_yjno AND a.Gy0_gxh = b.sczl_gxh', 'left')
  204. ->where('a.Gy0_gdbh', 'in', array_values($orderNos))
  205. ->field('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh, a.Gy0_gxmc, a.PD_WG, SUM(b.sczl_cl) as 产量')
  206. ->group('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh')
  207. ->order('a.Gy0_gxh asc')
  208. ->select();
  209. $latestNumSql = \db('设备_产量计酬')
  210. ->where('sczl_gdbh', 'in', array_values($orderNos))
  211. ->field('sczl_gdbh, sczl_yjno, sczl_gxh, MAX(sczl_num) as max_num')
  212. ->group('sczl_gdbh, sczl_yjno, sczl_gxh')
  213. ->buildSql();
  214. $latestRows = \db('设备_产量计酬')
  215. ->alias('t')
  216. ->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')
  217. ->field('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, rtrim(t.sczl_num) as sczl_num, MAX(t.sczl_rq) as sczl_rq')
  218. ->group('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, t.sczl_num')
  219. ->select();
  220. $latestMap = [];
  221. foreach ($latestRows as $latest) {
  222. $latestKey = rtrim((string)$latest['sczl_gdbh']) . '|' . rtrim((string)$latest['sczl_yjno']) . '|' . (int)$latest['sczl_gxh'];
  223. $latestMap[$latestKey] = $latest;
  224. }
  225. $processMap = [];
  226. foreach ($processRows as $process) {
  227. $mapKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']);
  228. $processMap[$mapKey][] = $process;
  229. }
  230. foreach ($list as $key => $value) {
  231. $list[$key]['印件号'] = rtrim((string)$value['yj_Yjno']);
  232. $mapKey = rtrim((string)$value['工单编号']) . '|' . rtrim((string)$value['yj_Yjno']);
  233. $index = 1;
  234. foreach ($processMap[$mapKey] ?? [] as $process) {
  235. $status = ($process['PD_WG'] !== '1900-01-01 00:00:00') ? '完工' : '';
  236. $latestKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']) . '|' . (int)$process['Gy0_gxh'];
  237. $latest = $latestMap[$latestKey] ?? [];
  238. $flowNo = rtrim((string)($latest['sczl_num'] ?? ''));
  239. if ($flowNo === '0') {
  240. $flowNo = '';
  241. }
  242. $reportDate = (string)($latest['sczl_rq'] ?? '');
  243. if ($reportDate !== '') {
  244. $reportDate = substr($reportDate, 0, 10);
  245. }
  246. $list[$key]['工序' . $index] = $value['yj_Yjno'] . '-' . $process['Gy0_gxh'] . '-' . rtrim((string)$process['Gy0_gxmc']) . '[' . $process['产量'] . '/' . ']' . $status . '[' . $flowNo . '/' . $reportDate . ']';
  247. $index++;
  248. }
  249. unset($list[$key]['yj_Yjno']);
  250. }
  251. $this->success('成功', [
  252. 'total' => (int)$total,
  253. 'page' => $page,
  254. 'limit' => $limit,
  255. 'list' => $list,
  256. ]);
  257. }
  258. /**
  259. * 工单印件的全部工艺及报工条数
  260. * 参数:工单编号、印件编号
  261. */
  262. public function workOrderProcess()
  263. {
  264. if ($this->request->isGet() === false) {
  265. $this->error('请求错误');
  266. }
  267. $param = $this->request->param();
  268. $workOrderNo = trim((string)($param['工单编号'] ?? ''));
  269. $printNo = trim((string)($param['印件编号'] ?? ''));
  270. if ($workOrderNo === '' || $printNo === '') {
  271. $this->error('参数错误');
  272. }
  273. $printNos = $this->coursePrintNoValues($printNo);
  274. $processes = \db('工单_工艺资料')
  275. ->where('Gy0_gdbh', $workOrderNo)
  276. ->where('Gy0_yjno', 'in', $printNos)
  277. ->field('rtrim(Gy0_gxh) as 工序号, rtrim(Gy0_gxmc) as 工序名称')
  278. ->orderRaw('Gy0_gxh+0 asc, Gy0_gxh asc')
  279. ->select();
  280. $reportCounts = \db('设备_产量计酬')
  281. ->where('sczl_gdbh', $workOrderNo)
  282. ->where('sczl_yjno', 'in', $printNos)
  283. ->field('rtrim(sczl_gxh) as 工序号, COUNT(*) as 报工条数')
  284. ->group('sczl_gxh')
  285. ->select();
  286. $countMap = [];
  287. foreach ($reportCounts as $row) {
  288. $countMap[$this->courseProcessKey($row['工序号'])] = (int)$row['报工条数'];
  289. }
  290. $data = [];
  291. $seen = [];
  292. foreach ($processes as $process) {
  293. $processNo = rtrim((string)$process['工序号']);
  294. $processKey = $this->courseProcessKey($processNo);
  295. if (isset($seen[$processKey])) {
  296. continue;
  297. }
  298. $seen[$processKey] = true;
  299. $processName = rtrim((string)$process['工序名称']);
  300. $count = $countMap[$processKey] ?? 0;
  301. $data[] = [
  302. '工序号' => $processNo,
  303. '工序名称' => $processName,
  304. '报工条数' => $count,
  305. '工艺' => $processNo . '-' . $processName . '(' . $count . ')',
  306. ];
  307. }
  308. $this->success('成功', $data);
  309. }
  310. /**
  311. * 某道工艺的报工明细,按流程单号从小到大
  312. * 参数:工单编号、印件编号、工序号
  313. */
  314. public function processReport()
  315. {
  316. if ($this->request->isGet() === false) {
  317. $this->error('请求错误');
  318. }
  319. $param = $this->request->param();
  320. $workOrderNo = trim((string)($param['工单编号'] ?? ''));
  321. $printNo = trim((string)($param['印件编号'] ?? ''));
  322. $processNo = trim((string)($param['工序号'] ?? ''));
  323. if ($workOrderNo === '' || $printNo === '' || $processNo === '') {
  324. $this->error('参数错误');
  325. }
  326. $rows = \db('设备_产量计酬')
  327. ->alias('a')
  328. ->join('设备_基本资料 m', 'a.sczl_jtbh = m.设备编号', 'left')
  329. ->where('a.sczl_gdbh', $workOrderNo)
  330. ->where('a.sczl_yjno', 'in', $this->coursePrintNoValues($printNo))
  331. ->where('a.sczl_gxh', 'in', $this->coursePrintNoValues($processNo))
  332. ->field('rtrim(a.sczl_num) as 流程单号, a.sczl_cl as 产量, a.sczl_zcfp as 制程废品, rtrim(a.sczl_jtbh) as 机台编号, rtrim(m.设备名称) as 机台名称, rtrim(a.sczl_bzdh) as 班组代号, a.sys_rq as 上报时间, rtrim(a.sys_id) as 上报人员, rtrim(a.sczl_bh1) as bh1, rtrim(a.sczl_bh2) as bh2, rtrim(a.sczl_bh3) as bh3, rtrim(a.sczl_bh4) as bh4, rtrim(a.sczl_bh5) as bh5, rtrim(a.sczl_bh6) as bh6, rtrim(a.sczl_bh7) as bh7, rtrim(a.sczl_bh8) as bh8, rtrim(a.sczl_bh9) as bh9, rtrim(a.sczl_bh10) as bh10')
  333. ->orderRaw('a.sczl_num+0 asc, a.sczl_num asc, a.sys_rq asc')
  334. ->select();
  335. $employeeCodes = [];
  336. foreach ($rows as $row) {
  337. for ($i = 1; $i <= 10; $i++) {
  338. $code = rtrim((string)$row['bh' . $i]);
  339. if ($code !== '' && $code !== '000000') {
  340. $employeeCodes[$code] = $code;
  341. }
  342. }
  343. }
  344. $employeeNames = [];
  345. if (!empty($employeeCodes)) {
  346. $people = \db('人事_基本资料')
  347. ->where('员工编号', 'in', array_values($employeeCodes))
  348. ->field('rtrim(员工编号) as 员工编号, rtrim(员工姓名) as 员工姓名')
  349. ->select();
  350. foreach ($people as $person) {
  351. $employeeNames[rtrim((string)$person['员工编号'])] = rtrim((string)$person['员工姓名']);
  352. }
  353. }
  354. $data = [];
  355. foreach ($rows as $row) {
  356. $members = [];
  357. for ($i = 1; $i <= 10; $i++) {
  358. $code = rtrim((string)$row['bh' . $i]);
  359. if ($code === '' || $code === '000000') {
  360. continue;
  361. }
  362. $members[] = $employeeNames[$code] ?? $code;
  363. }
  364. $machineNo = rtrim((string)$row['机台编号']);
  365. $machineName = rtrim((string)$row['机台名称']);
  366. $data[] = [
  367. '流程单号' => rtrim((string)$row['流程单号']),
  368. '产量' => $row['产量'],
  369. '制程废品' => $row['制程废品'],
  370. '生产机台' => $machineName !== '' ? $machineNo . ' ' . $machineName : $machineNo,
  371. '班组代号' => rtrim((string)$row['班组代号']),
  372. '班组人员' => implode(' ', $members),
  373. '上报人员' => rtrim((string)$row['上报人员']),
  374. '上报时间' => rtrim((string)$row['上报时间']),
  375. ];
  376. }
  377. $totalQty = 0.0;
  378. foreach ($data as $item) {
  379. $totalQty += (float)$item['产量'];
  380. }
  381. $this->success('成功', [
  382. '产量合计' => floor($totalQty) == $totalQty ? (int)$totalQty : $totalQty,
  383. '报工' => $data,
  384. ]);
  385. }
  386. private function coursePrintNoValues($value)
  387. {
  388. $value = rtrim((string)$value);
  389. $values = [$value];
  390. if ($value !== '' && is_numeric($value)) {
  391. $number = (string)(int)$value;
  392. $values[] = $number;
  393. $values[] = str_pad($number, 2, '0', STR_PAD_LEFT);
  394. }
  395. return array_values(array_unique($values));
  396. }
  397. private function courseProcessKey($value)
  398. {
  399. $value = rtrim((string)$value);
  400. if ($value !== '' && is_numeric($value)) {
  401. return (string)(int)$value;
  402. }
  403. return $value;
  404. }
  405. /**
  406. * 范围条件只在传入起或止时生效,日期止取到当天结束
  407. */
  408. private function applyCourseRange($query, $field, $from, $to, $isDate = false)
  409. {
  410. $from = trim((string)$from);
  411. $to = trim((string)$to);
  412. if ($from !== '') {
  413. $query->where($field, '>=', $from);
  414. }
  415. if ($to !== '') {
  416. if ($isDate && preg_match('/^\d{4}-\d{2}-\d{2}$/', $to)) {
  417. $to .= ' 23:59:59';
  418. }
  419. $query->where($field, '<=', $to);
  420. }
  421. }
  422. private function courseLikeKeyword($keyword)
  423. {
  424. return addcslashes((string)$keyword, '%_\\');
  425. }
  426. }