ReportingWork.php 19 KB

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