Course.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. */
  237. public function workOrderProcess()
  238. {
  239. if ($this->request->isGet() === false) {
  240. $this->error('请求错误');
  241. }
  242. $param = $this->request->param();
  243. $workOrderNo = trim((string)($param['工单编号'] ?? ''));
  244. $printNo = trim((string)($param['印件编号'] ?? ''));
  245. if ($workOrderNo === '' || $printNo === '') {
  246. $this->error('参数错误');
  247. }
  248. $printNos = $this->coursePrintNoValues($printNo);
  249. $processes = \db('工单_工艺资料')
  250. ->where('Gy0_gdbh', $workOrderNo)
  251. ->where('Gy0_yjno', 'in', $printNos)
  252. ->field('rtrim(Gy0_gxh) as 工序号, rtrim(Gy0_gxmc) as 工序名称')
  253. ->orderRaw('Gy0_gxh+0 asc, Gy0_gxh asc')
  254. ->select();
  255. $reportCounts = \db('设备_产量计酬')
  256. ->where('sczl_gdbh', $workOrderNo)
  257. ->where('sczl_yjno', 'in', $printNos)
  258. ->field('rtrim(sczl_gxh) as 工序号, COUNT(*) as 报工条数')
  259. ->group('sczl_gxh')
  260. ->select();
  261. $countMap = [];
  262. foreach ($reportCounts as $row) {
  263. $countMap[$this->courseProcessKey($row['工序号'])] = (int)$row['报工条数'];
  264. }
  265. $data = [];
  266. $seen = [];
  267. foreach ($processes as $process) {
  268. $processNo = rtrim((string)$process['工序号']);
  269. $processKey = $this->courseProcessKey($processNo);
  270. if (isset($seen[$processKey])) {
  271. continue;
  272. }
  273. $seen[$processKey] = true;
  274. $processName = rtrim((string)$process['工序名称']);
  275. $count = $countMap[$processKey] ?? 0;
  276. $data[] = [
  277. '工序号' => $processNo,
  278. '工序名称' => $processName,
  279. '报工条数' => $count,
  280. '工艺' => $processNo . '-' . $processName . '(' . $count . ')',
  281. ];
  282. }
  283. $this->success('成功', $data);
  284. }
  285. /**
  286. * 某道工艺的报工明细,按流程单号从小到大
  287. * 参数:工单编号、印件编号、工序号
  288. */
  289. public function processReport()
  290. {
  291. if ($this->request->isGet() === false) {
  292. $this->error('请求错误');
  293. }
  294. $param = $this->request->param();
  295. $workOrderNo = trim((string)($param['工单编号'] ?? ''));
  296. $printNo = trim((string)($param['印件编号'] ?? ''));
  297. $processNo = trim((string)($param['工序号'] ?? ''));
  298. if ($workOrderNo === '' || $printNo === '' || $processNo === '') {
  299. $this->error('参数错误');
  300. }
  301. $rows = \db('设备_产量计酬')
  302. ->alias('a')
  303. ->join('设备_基本资料 m', 'a.sczl_jtbh = m.设备编号', 'left')
  304. ->where('a.sczl_gdbh', $workOrderNo)
  305. ->where('a.sczl_yjno', 'in', $this->coursePrintNoValues($printNo))
  306. ->where('a.sczl_gxh', 'in', $this->coursePrintNoValues($processNo))
  307. ->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')
  308. ->orderRaw('a.sczl_num+0 asc, a.sczl_num asc, a.sys_rq asc')
  309. ->select();
  310. $employeeCodes = [];
  311. foreach ($rows as $row) {
  312. for ($i = 1; $i <= 10; $i++) {
  313. $code = rtrim((string)$row['bh' . $i]);
  314. if ($code !== '' && $code !== '000000') {
  315. $employeeCodes[$code] = $code;
  316. }
  317. }
  318. }
  319. $employeeNames = [];
  320. if (!empty($employeeCodes)) {
  321. $people = \db('人事_基本资料')
  322. ->where('员工编号', 'in', array_values($employeeCodes))
  323. ->field('rtrim(员工编号) as 员工编号, rtrim(员工姓名) as 员工姓名')
  324. ->select();
  325. foreach ($people as $person) {
  326. $employeeNames[rtrim((string)$person['员工编号'])] = rtrim((string)$person['员工姓名']);
  327. }
  328. }
  329. $data = [];
  330. foreach ($rows as $row) {
  331. $members = [];
  332. for ($i = 1; $i <= 10; $i++) {
  333. $code = rtrim((string)$row['bh' . $i]);
  334. if ($code === '' || $code === '000000') {
  335. continue;
  336. }
  337. $members[] = $employeeNames[$code] ?? $code;
  338. }
  339. $machineNo = rtrim((string)$row['机台编号']);
  340. $machineName = rtrim((string)$row['机台名称']);
  341. $data[] = [
  342. '流程单号' => rtrim((string)$row['流程单号']),
  343. '产量' => $row['产量'],
  344. '制程废品' => $row['制程废品'],
  345. '生产机台' => $machineName !== '' ? $machineNo . ' ' . $machineName : $machineNo,
  346. '班组代号' => rtrim((string)$row['班组代号']),
  347. '班组人员' => implode(' ', $members),
  348. '上报人员' => rtrim((string)$row['上报人员']),
  349. '上报时间' => rtrim((string)$row['上报时间']),
  350. ];
  351. }
  352. $totalQty = 0.0;
  353. foreach ($data as $item) {
  354. $totalQty += (float)$item['产量'];
  355. }
  356. $this->success('成功', [
  357. '产量合计' => floor($totalQty) == $totalQty ? (int)$totalQty : $totalQty,
  358. '报工' => $data,
  359. ]);
  360. }
  361. private function coursePrintNoValues($value)
  362. {
  363. $value = rtrim((string)$value);
  364. $values = [$value];
  365. if ($value !== '' && is_numeric($value)) {
  366. $number = (string)(int)$value;
  367. $values[] = $number;
  368. $values[] = str_pad($number, 2, '0', STR_PAD_LEFT);
  369. }
  370. return array_values(array_unique($values));
  371. }
  372. private function courseProcessKey($value)
  373. {
  374. $value = rtrim((string)$value);
  375. if ($value !== '' && is_numeric($value)) {
  376. return (string)(int)$value;
  377. }
  378. return $value;
  379. }
  380. /**
  381. * 范围条件只在传入起或止时生效,日期止取到当天结束
  382. */
  383. private function applyCourseRange($query, $field, $from, $to, $isDate = false)
  384. {
  385. $from = trim((string)$from);
  386. $to = trim((string)$to);
  387. if ($from !== '') {
  388. $query->where($field, '>=', $from);
  389. }
  390. if ($to !== '') {
  391. if ($isDate && preg_match('/^\d{4}-\d{2}-\d{2}$/', $to)) {
  392. $to .= ' 23:59:59';
  393. }
  394. $query->where($field, '<=', $to);
  395. }
  396. }
  397. private function courseLikeKeyword($keyword)
  398. {
  399. return addcslashes((string)$keyword, '%_\\');
  400. }
  401. }