ReportingWork.php 22 KB

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