ReportingWork.php 18 KB

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