ReportingWork.php 18 KB

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