ReportingWork.php 21 KB

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