ReportingWork.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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_score']) || $item['standard_score'] === '') {
  221. $this->error('第' . $rowNo . '条标准工分不能为空');
  222. }
  223. if (!isset($item['number']) || $item['number'] === '') {
  224. $this->error('第' . $rowNo . '条产量不能为空');
  225. }
  226. $standardHour = floatval($item['standard_hour']);
  227. $standardScore = floatval($item['standard_score']);
  228. $money = floatval($item['money']);
  229. $coefficient = isset($item['coefficient']) && $item['coefficient'] !== '' ? floatval($item['coefficient']) : 'C';
  230. $number = floatval($item['number']);
  231. $productionHour = $standardHour * $number;
  232. $productionScore = $standardScore * $number;
  233. $productionMoney = $money * $number;
  234. $partCode = null;
  235. if (isset($item['part_code']) && $item['part_code'] !== '') {
  236. $partCode = intval($item['part_code']);
  237. }
  238. $insertData[] = [
  239. 'staff_no' => $item['staff_no'],
  240. 'staff_name' => $item['staff_name'],
  241. 'work_order' => !empty($item['work_order']) ? $item['work_order'] : $item['workorder'],
  242. 'date' => !empty($item['date']) ? $item['date'] : date('Y-m-d'),
  243. 'majorprocess' => $item['majorprocess'],
  244. 'part_code' => $partCode,
  245. 'process_code' => $item['process_code'],
  246. 'process_name' => $item['process_name'],
  247. 'standard_hour' => $standardHour,
  248. 'standard_score' => $standardScore,
  249. 'money' => $money,
  250. 'coefficient' => $coefficient,
  251. 'number' => $number,
  252. 'production_hour' => round($productionHour, 4),
  253. 'production_score' => round($productionScore, 4),
  254. 'salary' => round($productionMoney, 4),
  255. 'machine' => isset($item['machine']) ? $item['machine'] : '',
  256. 'sys_id' => isset($item['sys_id']) ? $item['sys_id'] : '',
  257. 'sys_rq' => $now,
  258. 'mod_id' => isset($item['mod_id']) ? $item['mod_id'] : '',
  259. 'mod_rq' => null,
  260. 'del_rq' => null,
  261. ];
  262. }
  263. Db::startTrans();
  264. try {
  265. $result = db('设备_工分计酬')->insertAll($insertData);
  266. if ($result === false) {
  267. Db::rollback();
  268. $this->error('报工失败');
  269. }
  270. Db::commit();
  271. } catch (\Exception $e) {
  272. Db::rollback();
  273. $this->error('报工失败:' . $e->getMessage());
  274. }
  275. $this->success('报工成功', ['count' => count($insertData)]);
  276. }
  277. //报工工分数据表左侧菜单栏
  278. public function GetReportingWorkLeft()
  279. {
  280. if (!$this->request->isGet()) {
  281. $this->error('请求方法错误');
  282. }
  283. $startDate = date('Y-m-d', strtotime('-39 day'));
  284. $endDate = date('Y-m-d');
  285. $list = db('设备_工分计酬')
  286. ->where('del_rq', null)
  287. ->where('date', 'between time', [$startDate, $endDate])
  288. ->where('machine', '<>', '')
  289. ->field('machine,date')
  290. ->order('machine asc,date desc')
  291. ->select();
  292. $menuMap = [];
  293. foreach ($list as $row) {
  294. $machine = $row['machine'];
  295. $date = $row['date'];
  296. if (!isset($menuMap[$machine])) {
  297. $menuMap[$machine] = [];
  298. }
  299. if (!in_array($date, $menuMap[$machine])) {
  300. $menuMap[$machine][] = $date;
  301. }
  302. }
  303. $data = [];
  304. foreach ($menuMap as $machine => $dateList) {
  305. $children = [];
  306. foreach ($dateList as $date) {
  307. $children[] = [
  308. 'date' => $date,
  309. ];
  310. }
  311. $data[] = [
  312. 'machine' => $machine,
  313. 'children' => $children,
  314. ];
  315. }
  316. $this->success('成功', $data);
  317. }
  318. /**
  319. * 机台报工数据
  320. * @ApiMethod (GET)
  321. * @param string $date 日期
  322. * @param string $machine 机台编号
  323. * @return array
  324. * @throws \think\db\exception\DataNotFoundException
  325. * @throws \think\db\exception\ModelNotFoundException
  326. * @throws \think\exception\DbException
  327. */
  328. public function GetReportingWorkData()
  329. {
  330. if (!$this->request->isGet()) {
  331. $this->error('请求方法错误');
  332. }
  333. $params = $this->request->param();
  334. if (empty($params['date'])) {
  335. $this->error('日期不能为空');
  336. }
  337. if (empty($params['machine'])) {
  338. $this->error('机台编号不能为空');
  339. }
  340. $rows = db('设备_工分计酬')
  341. ->where('del_rq', null)
  342. ->where('date', $params['date'])
  343. ->where('machine', $params['machine'])
  344. ->field('id,work_order,majorprocess,part_code,process_code,process_name,staff_no,staff_name,standard_hour,
  345. standard_score,money,coefficient,number,production_hour,production_score,
  346. salary,machine,sys_id,sys_rq')
  347. ->order('process_code asc,staff_no asc')
  348. ->select();
  349. $processMap = [];
  350. foreach ($rows as $row) {
  351. $groupKey = $row['majorprocess'] . '|' . $row['process_code'] . '|' . $row['process_name'] . '|' . $row['part_code'];
  352. if (!isset($processMap[$groupKey])) {
  353. $processMap[$groupKey] = [
  354. 'work_order' => $row['work_order'],
  355. 'majorprocess' => $row['majorprocess'],
  356. 'part_code' => $row['part_code'],
  357. 'process_code' => $row['process_code'],
  358. 'process_name' => $row['process_name'],
  359. 'machine' => $row['machine'],
  360. 'date' => $params['date'],
  361. 'staffs' => [],
  362. ];
  363. }
  364. $processMap[$groupKey]['staffs'][] = [
  365. 'staff_no' => $row['staff_no'],
  366. 'staff_name' => $row['staff_name'],
  367. 'standard_hour' => $row['standard_hour'],
  368. 'standard_score' => $row['standard_score'],
  369. 'money' => $row['money'],
  370. 'coefficient' => $row['coefficient'],
  371. 'number' => $row['number'],
  372. 'production_hour' => $row['production_hour'],
  373. 'production_score' => $row['production_score'],
  374. 'salary' => $row['salary'],
  375. 'sys_id' => $row['sys_id'],
  376. 'sys_rq' => $row['sys_rq'],
  377. 'id' => $row['id'],
  378. ];
  379. }
  380. $data = array_values($processMap);
  381. $this->success('成功', $data);
  382. }
  383. /**
  384. * 修改报工数据
  385. * @ApiMethod (POST)
  386. * @param array $list 报工数据
  387. * @return array
  388. * @throws \think\db\exception\DataNotFoundException
  389. * @throws \think\db\exception\ModelNotFoundException
  390. * @throws \think\exception\DbException
  391. */
  392. public function UpdateReportingWork()
  393. {
  394. if (!$this->request->isPost()) {
  395. $this->error('请求方法错误');
  396. }
  397. $params = $this->request->post();
  398. if (empty($params['id'])) {
  399. $this->error('ID不能为空');
  400. }
  401. if (empty($params['mod_id'])) {
  402. $this->error('修改人不能为空');
  403. }
  404. $data = [
  405. 'mod_id' => $params['mod_id'],
  406. 'mod_rq' => date('Y-m-d H:i:s'),
  407. 'number' => $params['number'],
  408. 'production_hour' => $params['production_hour'],
  409. 'production_score' => $params['production_score'],
  410. 'money' => $params['money'],
  411. 'salary' => $params['money'] * $params['number'],
  412. ];
  413. $result = db('设备_工分计酬')
  414. ->where('id', $params['id'])
  415. ->update($data);
  416. if ($result === false) {
  417. $this->error('修改失败');
  418. }
  419. $this->success('修改成功');
  420. }
  421. /**
  422. * 删除报工数据
  423. * @ApiMethod (POST)
  424. * @param array $id ID
  425. * @return array
  426. * @throws \think\db\exception\DataNotFoundException
  427. * @throws \think\db\exception\ModelNotFoundException
  428. * @throws \think\exception\DbException
  429. */
  430. public function DeleteReportingWork()
  431. {
  432. if (!$this->request->isPost()) {
  433. $this->error('请求方法错误');
  434. }
  435. $params = $this->request->post();
  436. if (empty($params['id'])) {
  437. $this->error('ID不能为空');
  438. }
  439. $data = [
  440. 'del_rq' => date('Y-m-d H:i:s'),
  441. ];
  442. $result = db('设备_工分计酬')
  443. ->where('id', $params['id'])
  444. ->update($data);
  445. if ($result === false) {
  446. $this->error('删除失败');
  447. }
  448. $this->success('删除成功');
  449. }
  450. /**
  451. * 查询机台报工记录
  452. * @ApiMethod (GET)
  453. * @param string $date 日期
  454. * @param string $machine 机台编号
  455. * @return array
  456. * @throws \think\db\exception\DataNotFoundException
  457. * @throws \think\db\exception\ModelNotFoundException
  458. * @throws \think\exception\DbException
  459. */
  460. public function GetReportingWorkRecord()
  461. {
  462. if (!$this->request->isGet()) {
  463. $this->error('请求方法错误');
  464. }
  465. $params = $this->request->param();
  466. if (empty($params['date'])) {
  467. $this->error('日期不能为空');
  468. }
  469. if (empty($params['machine'])) {
  470. $this->error('机台编号不能为空');
  471. }
  472. $rows = db('设备_工分计酬')
  473. ->where('del_rq', null)
  474. ->where('date', 'like', $params['date'] . '%')
  475. ->where('machine', $params['machine'])
  476. ->field('work_order,majorprocess,part_code,process_code,process_name,staff_no,staff_name,
  477. standard_hour,standard_score,money,coefficient,number,production_hour,
  478. production_score,salary,machine,sys_id,sys_rq,id')
  479. ->order('process_code asc,staff_no asc')
  480. ->select();
  481. $this->success('成功', $rows);
  482. }
  483. }