ReportingWork.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Request;
  5. use \think\Db;
  6. /**
  7. * 车间工分报工
  8. */
  9. class ReportingWork extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 根据机台编号获取大工序和人员信息
  15. * @ApiMethod (GET)
  16. * @param string $machine 机台编号
  17. * @return array
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public function getMajorprocessAndPerson()
  23. {
  24. if (!$this->request->isGet()) {
  25. $this->error('请求方法错误');
  26. }
  27. $params = $this->request->param();
  28. if (empty($params['machine'])) {
  29. $this->error('机台编号不能为空');
  30. }
  31. //获取大工序
  32. $majorprocess = db('设备_基本资料')
  33. ->where('设备编号', $params['machine'])
  34. ->field('生产工序,设备编组')
  35. ->find();
  36. if (empty($majorprocess)) {
  37. $this->error('设备信息');
  38. }
  39. //小组信息
  40. // $person = db('人员_小组资料')
  41. // ->where('team_name', $majorprocess['设备编组'])
  42. // ->where('status', 1)
  43. // ->field('team_name as 小组名称,staff_no as 员工编号,staff_name as 员工姓名')
  44. // ->select();
  45. $person = Db::name('人员_小组资料')->alias('a')
  46. ->field('
  47. b.staff_no as 员工编号,
  48. b.staff_name as 员工姓名,
  49. a.team_name as 小组名称
  50. ')
  51. ->join('人员_基本资料 b', 'a.staff_no = b.staff_no')
  52. ->where('a.team_name', $majorprocess['设备编组'])
  53. ->whereNull('a.mod_rq')
  54. ->whereNull('b.mod_rq')
  55. ->order('a.id asc')
  56. ->select();
  57. if (empty($person)) {
  58. $this->error('未找到人员信息');
  59. }
  60. $data['majorprocess'] = $majorprocess;
  61. $data['person'] = $person;
  62. $this->success('成功', $data);
  63. }
  64. /**
  65. * 获取订单工艺数据
  66. * @ApiMethod (GET)
  67. * @param string $workorder 工单编号
  68. * @param string $majorprocess 大工序
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @throws \think\exception\DbException
  73. *
  74. */
  75. public function GetOrderProcess()
  76. {
  77. if (!$this->request->isGet()) {
  78. $this->error('请求方法错误');
  79. }
  80. $params = $this->request->param();
  81. if (empty($params['workorder'])) {
  82. $this->error('工单编号不能为空');
  83. }
  84. if (empty($params['majorprocess'])) {
  85. $this->error('大工序不能为空');
  86. }
  87. if ($params['majorprocess'] === '车缝') {
  88. $data = db('工单_部件资料')
  89. ->where('work_order', $params['workorder'])
  90. ->whereNull('del_rq')
  91. ->where('status', 1)
  92. ->field('part_code as 部件编号,part_name as 部件名称')
  93. ->select();
  94. if (empty($data)) {
  95. $this->error('未找到部件资料');
  96. }
  97. usort($data, function ($a, $b) {
  98. return strnatcmp((string)$a['部件编号'], (string)$b['部件编号']);
  99. });
  100. } else {
  101. $data = db('工单_基础工艺资料')
  102. ->where('work_order', $params['workorder'])
  103. ->where('big_process', $params['majorprocess'])
  104. ->whereNull('del_rq')
  105. ->where('status', 0)
  106. ->field('part_code,process_code as 工艺编号,process_name as 工艺名称,standard_hour as 标准工时,
  107. standard_score as 标准工分,coefficient as 系数,remark as 备注')
  108. ->select();
  109. if (empty($data)) {
  110. $this->error('未找到工艺数据');
  111. }
  112. usort($data, function ($a, $b) {
  113. $partCmp = strnatcmp((string)$a['part_code'], (string)$b['part_code']);
  114. if ($partCmp !== 0) {
  115. return $partCmp;
  116. }
  117. return strnatcmp((string)$a['工艺编号'], (string)$b['工艺编号']);
  118. });
  119. foreach ($data as &$row) {
  120. unset($row['part_code']);
  121. }
  122. unset($row);
  123. }
  124. $this->success('成功', $data);
  125. }
  126. /**
  127. * 获取车缝部件的工艺资料
  128. * @ApiMethod (GET)
  129. * @param string $part_code 部件编号
  130. * @param string $workorder 工单编号
  131. * @return array
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. * @throws \think\exception\DbException
  135. */
  136. public function GetCarProcess()
  137. {
  138. if (!$this->request->isGet()) {
  139. $this->error('请求方法错误');
  140. }
  141. $params = $this->request->param();
  142. if (empty($params['workorder'])) {
  143. $this->error('工单编号不能为空');
  144. }
  145. if (empty($params['part_code'])) {
  146. $this->error('部件编号不能为空');
  147. }
  148. $data = db('工单_基础工艺资料')
  149. ->where('work_order', $params['workorder'])
  150. ->where('part_code', $params['part_code'])
  151. ->where('big_process', '车缝')
  152. ->where('del_rq', null)
  153. ->where('status', 0)
  154. ->field('process_code as 工艺编号,process_name as 工艺名称,standard_hour as 标准工时,standard_minutes as 标准分钟,
  155. standard_score as 标准工分,coefficient as 系数,remark as 备注,money as 金额')
  156. ->order('process_code')
  157. ->select();
  158. $this->success('成功', $data);
  159. }
  160. /**
  161. * 工分报工接口
  162. * @ApiMethod (POST)
  163. * @param array $list 报工数据
  164. * @return array
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. * @throws \think\exception\DbException
  168. */
  169. public function ReportingWork()
  170. {
  171. if (!$this->request->isPost()) {
  172. $this->error('请求方法错误');
  173. }
  174. $params = $this->request->post();
  175. $reportList = [];
  176. if (isset($params[0]) && is_array($params[0])) {
  177. $reportList = $params;
  178. } elseif (!empty($params['list']) && is_array($params['list'])) {
  179. $reportList = $params['list'];
  180. } elseif (!empty($params['data']) && is_array($params['data'])) {
  181. $reportList = $params['data'];
  182. } else {
  183. $rawBody = file_get_contents('php://input');
  184. $decoded = json_decode($rawBody, true);
  185. if (is_array($decoded)) {
  186. if (isset($decoded[0]) && is_array($decoded[0])) {
  187. $reportList = $decoded;
  188. } elseif (!empty($decoded['list']) && is_array($decoded['list'])) {
  189. $reportList = $decoded['list'];
  190. } elseif (!empty($decoded['data']) && is_array($decoded['data'])) {
  191. $reportList = $decoded['data'];
  192. }
  193. }
  194. }
  195. if (empty($reportList)) {
  196. $this->error('请传入报工数据');
  197. }
  198. $insertData = [];
  199. $now = date('Y-m-d H:i:s');
  200. foreach ($reportList as $idx => $item) {
  201. $rowNo = $idx + 1;
  202. if (empty($item['staff_no'])) {
  203. $this->error('第' . $rowNo . '条员工编号不能为空');
  204. }
  205. if (empty($item['staff_name'])) {
  206. $this->error('第' . $rowNo . '条员工姓名不能为空');
  207. }
  208. if (empty($item['work_order']) && empty($item['workorder'])) {
  209. $this->error('第' . $rowNo . '条订单编号不能为空');
  210. }
  211. if (empty($item['majorprocess'])) {
  212. $this->error('第' . $rowNo . '条大工序不能为空');
  213. }
  214. $isSewing = $item['majorprocess'] === '车缝';
  215. if ($isSewing && (!isset($item['part_code']) || $item['part_code'] === '')) {
  216. $this->error('第' . $rowNo . '条车缝工序必须填写部件编号');
  217. }
  218. if (empty($item['process_code'])) {
  219. $this->error('第' . $rowNo . '条工艺编号不能为空');
  220. }
  221. if (empty($item['process_name'])) {
  222. $this->error('第' . $rowNo . '条工艺名称不能为空');
  223. }
  224. // if (!isset($item['standard_hour']) || $item['standard_hour'] === '') {
  225. // $this->error('第' . $rowNo . '条标准工时不能为空');
  226. // }
  227. // if (!isset($item['money']) || $item['money'] === '') {
  228. // $this->error('第' . $rowNo . '条金额不能为空');
  229. // }
  230. // if (empty($item['standard_minutes'])) {
  231. // $this->error('第' . $rowNo . '条标准分钟不能为空');
  232. // }
  233. if (!isset($item['standard_score']) || $item['standard_score'] === '') {
  234. $this->error('第' . $rowNo . '条标准工分不能为空');
  235. }
  236. if (!isset($item['number']) || $item['number'] === '') {
  237. $this->error('第' . $rowNo . '条产量不能为空');
  238. }
  239. $standardHour = floatval($item['standard_hour']);
  240. $standardScore = floatval($item['standard_score']);
  241. $money = floatval($item['money']);
  242. $coefficient = isset($item['coefficient']) && $item['coefficient'] !== '' ? floatval($item['coefficient']) : 'C';
  243. $number = floatval($item['number']);
  244. $standardMinutes = floatval($item['standard_minutes']);
  245. $productionHour = $standardHour * $number;
  246. $productionScore = $standardScore * $number;
  247. $productionMoney = $money * $number;
  248. $partCode = null;
  249. if (isset($item['part_code']) && $item['part_code'] !== '') {
  250. $partCode = intval($item['part_code']);
  251. }
  252. $insertData[] = [
  253. 'staff_no' => $item['staff_no'],
  254. 'staff_name' => $item['staff_name'],
  255. 'work_order' => !empty($item['work_order']) ? $item['work_order'] : $item['workorder'],
  256. 'date' => !empty($item['date']) ? $item['date'] : date('Y-m-d'),
  257. 'majorprocess' => $item['majorprocess'],
  258. 'part_code' => $partCode,
  259. 'process_code' => $item['process_code'],
  260. 'process_name' => $item['process_name'],
  261. 'standard_hour' => $standardHour,
  262. 'standard_score' => $standardScore,
  263. 'standard_minutes' => $standardMinutes,
  264. 'money' => $money,
  265. 'coefficient' => $coefficient,
  266. 'number' => $number,
  267. 'production_hour' => round($productionHour, 4),
  268. 'production_score' => round($productionScore, 4),
  269. 'salary' => round($productionMoney, 4),
  270. 'machine' => isset($item['machine']) ? $item['machine'] : '',
  271. 'sys_id' => isset($item['sys_id']) ? $item['sys_id'] : '',
  272. 'sys_rq' => $now,
  273. 'mod_id' => isset($item['mod_id']) ? $item['mod_id'] : '',
  274. 'mod_rq' => null,
  275. 'del_rq' => null,
  276. ];
  277. }
  278. Db::startTrans();
  279. try {
  280. $result = db('设备_工分计酬')->insertAll($insertData);
  281. if ($result === false) {
  282. Db::rollback();
  283. $this->error('报工失败');
  284. }
  285. Db::commit();
  286. } catch (\Exception $e) {
  287. Db::rollback();
  288. $this->error('报工失败:' . $e->getMessage());
  289. }
  290. $this->success('报工成功', ['count' => count($insertData)]);
  291. }
  292. //报工工分数据表左侧菜单栏
  293. public function GetReportingWorkLeft()
  294. {
  295. if (!$this->request->isGet()) {
  296. $this->error('请求方法错误');
  297. }
  298. $startDate = date('Y-m-d', strtotime('-39 day'));
  299. $endDate = date('Y-m-d');
  300. $list = db('设备_工分计酬')
  301. ->where('del_rq', null)
  302. ->where('date', 'between time', [$startDate, $endDate])
  303. ->where('machine', '<>', '')
  304. ->field('machine,date')
  305. ->order('machine asc,date desc')
  306. ->select();
  307. $menuMap = [];
  308. foreach ($list as $row) {
  309. $machine = $row['machine'];
  310. $date = $row['date'];
  311. if (!isset($menuMap[$machine])) {
  312. $menuMap[$machine] = [];
  313. }
  314. if (!in_array($date, $menuMap[$machine])) {
  315. $menuMap[$machine][] = $date;
  316. }
  317. }
  318. $data = [];
  319. foreach ($menuMap as $machine => $dateList) {
  320. $children = [];
  321. foreach ($dateList as $date) {
  322. $children[] = [
  323. 'date' => $date,
  324. ];
  325. }
  326. $data[] = [
  327. 'machine' => $machine,
  328. 'children' => $children,
  329. ];
  330. }
  331. $this->success('成功', $data);
  332. }
  333. /**
  334. * 机台报工数据
  335. * @ApiMethod (GET)
  336. * @param string $date 日期
  337. * @param string $machine 机台编号
  338. * @return array
  339. * @throws \think\db\exception\DataNotFoundException
  340. * @throws \think\db\exception\ModelNotFoundException
  341. * @throws \think\exception\DbException
  342. */
  343. public function GetReportingWorkData()
  344. {
  345. if (!$this->request->isGet()) {
  346. $this->error('请求方法错误');
  347. }
  348. $params = $this->request->param();
  349. if (empty($params['date'])) {
  350. $this->error('日期不能为空');
  351. }
  352. if (empty($params['machine'])) {
  353. $this->error('机台编号不能为空');
  354. }
  355. $rows = db('设备_工分计酬')
  356. ->where('del_rq', null)
  357. ->where('date', $params['date'])
  358. ->where('machine', $params['machine'])
  359. ->field('id,work_order,majorprocess,part_code,process_code,process_name,staff_no,staff_name,standard_hour,
  360. standard_score,money,coefficient,number,production_hour,production_score,
  361. salary,machine,sys_id,sys_rq')
  362. ->order('process_code asc,staff_no asc')
  363. ->select();
  364. $processMap = [];
  365. foreach ($rows as $row) {
  366. $groupKey = $row['majorprocess'] . '|' . $row['process_code'] . '|' . $row['process_name'] . '|' . $row['part_code'];
  367. if (!isset($processMap[$groupKey])) {
  368. $processMap[$groupKey] = [
  369. 'work_order' => $row['work_order'],
  370. 'majorprocess' => $row['majorprocess'],
  371. 'part_code' => $row['part_code'],
  372. 'process_code' => $row['process_code'],
  373. 'process_name' => $row['process_name'],
  374. 'machine' => $row['machine'],
  375. 'date' => $params['date'],
  376. 'staffs' => [],
  377. ];
  378. }
  379. $processMap[$groupKey]['staffs'][] = [
  380. 'staff_no' => $row['staff_no'],
  381. 'staff_name' => $row['staff_name'],
  382. 'standard_hour' => $row['standard_hour'],
  383. 'standard_score' => $row['standard_score'],
  384. 'money' => $row['money'],
  385. 'coefficient' => $row['coefficient'],
  386. 'number' => $row['number'],
  387. 'production_hour' => $row['production_hour'],
  388. 'production_score' => $row['production_score'],
  389. 'salary' => $row['salary'],
  390. 'sys_id' => $row['sys_id'],
  391. 'sys_rq' => $row['sys_rq'],
  392. 'id' => $row['id'],
  393. ];
  394. }
  395. $data = array_values($processMap);
  396. $this->success('成功', $data);
  397. }
  398. /**
  399. * 修改报工数据
  400. * @ApiMethod (POST)
  401. * @param array $list 报工数据
  402. * @return array
  403. * @throws \think\db\exception\DataNotFoundException
  404. * @throws \think\db\exception\ModelNotFoundException
  405. * @throws \think\exception\DbException
  406. */
  407. public function UpdateReportingWork()
  408. {
  409. if (!$this->request->isPost()) {
  410. $this->error('请求方法错误');
  411. }
  412. $params = $this->request->post();
  413. if (empty($params['id'])) {
  414. $this->error('ID不能为空');
  415. }
  416. if (empty($params['mod_id'])) {
  417. $this->error('修改人不能为空');
  418. }
  419. $data = [
  420. 'mod_id' => $params['mod_id'],
  421. 'mod_rq' => date('Y-m-d H:i:s'),
  422. 'number' => $params['number'],
  423. 'production_hour' => $params['production_hour'],
  424. 'production_score' => $params['production_score'],
  425. 'money' => $params['money'],
  426. 'salary' => $params['money'] * $params['number'],
  427. ];
  428. $result = db('设备_工分计酬')
  429. ->where('id', $params['id'])
  430. ->update($data);
  431. if ($result === false) {
  432. $this->error('修改失败');
  433. }
  434. $this->success('修改成功');
  435. }
  436. /**
  437. * 删除报工数据
  438. * @ApiMethod (POST)
  439. * @param array $id ID
  440. * @return array
  441. * @throws \think\db\exception\DataNotFoundException
  442. * @throws \think\db\exception\ModelNotFoundException
  443. * @throws \think\exception\DbException
  444. */
  445. public function DeleteReportingWork()
  446. {
  447. if (!$this->request->isPost()) {
  448. $this->error('请求方法错误');
  449. }
  450. $params = $this->request->post();
  451. if (empty($params['id'])) {
  452. $this->error('ID不能为空');
  453. }
  454. $data = [
  455. 'del_rq' => date('Y-m-d H:i:s'),
  456. ];
  457. $result = db('设备_工分计酬')
  458. ->where('id', $params['id'])
  459. ->update($data);
  460. if ($result === false) {
  461. $this->error('删除失败');
  462. }
  463. $this->success('删除成功');
  464. }
  465. /**
  466. * 查询机台报工记录
  467. * @ApiMethod (GET)
  468. * @param string $date 日期
  469. * @param string $machine 机台编号
  470. * @return array
  471. * @throws \think\db\exception\DataNotFoundException
  472. * @throws \think\db\exception\ModelNotFoundException
  473. * @throws \think\exception\DbException
  474. */
  475. public function GetReportingWorkRecord()
  476. {
  477. if (!$this->request->isGet()) {
  478. $this->error('请求方法错误');
  479. }
  480. $params = $this->request->param();
  481. if (empty($params['date'])) {
  482. $this->error('日期不能为空');
  483. }
  484. if (empty($params['machine'])) {
  485. $this->error('机台编号不能为空');
  486. }
  487. $rows = db('设备_工分计酬')
  488. ->where('del_rq', null)
  489. ->where('date', 'like', $params['date'] . '%')
  490. ->where('machine', $params['machine'])
  491. ->field('work_order,majorprocess,part_code,process_code,process_name,staff_no,staff_name,
  492. standard_hour,standard_score,money,coefficient,number,production_hour,
  493. production_score,salary,machine,sys_id,sys_rq,id')
  494. ->order('process_code asc,staff_no asc')
  495. ->select();
  496. $this->success('成功', $rows);
  497. }
  498. }