ReportingWork.php 16 KB

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