StaffSalary.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 StaffSalary extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. // public function GetReportingWorkMonth()
  14. // {
  15. // if (!$this->request->isGet()) {
  16. // $this->error('请求方法错误');
  17. // }
  18. // $data = db('设备_工分计酬')
  19. // ->where('del_rq', null)
  20. // ->field('DISTINCT DATE_FORMAT(date, "%Y-%m") as month')
  21. // ->order('date desc')
  22. // ->select();
  23. //
  24. // //获取员工部门
  25. // $big_process = db('人员_基本资料')
  26. // ->field('DISTINCT big_process')
  27. // ->where('big_process','<>','')
  28. // ->select();
  29. // foreach ($big_process as $v) {
  30. // $big_process_name[] = $v['big_process'];
  31. // }
  32. // $result = [];
  33. // foreach ($data as $v) {
  34. // $result[$v['month']] = $big_process_name;
  35. // }
  36. //
  37. // $this->success('成功', $result);
  38. // }
  39. /**
  40. * 查询报工数据月份(年-年月-日期-工序-小组五级树)
  41. * @ApiMethod (GET)
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. */
  47. public function GetReportingWorkMonth()
  48. {
  49. if (!$this->request->isGet()) {
  50. $this->error('请求方法错误');
  51. }
  52. $rows = db('设备_工分计酬')
  53. ->where('del_rq', null)
  54. ->where('sys_rq', 'not null')
  55. ->field('LEFT(sys_rq, 10) as date, IF(majorprocess IS NULL OR majorprocess = "", "其他", majorprocess) as process, rtrim(sys_id) as sys_id')
  56. ->group('LEFT(sys_rq, 10), process, sys_id')
  57. ->order('sys_rq desc, process asc, sys_id asc')
  58. ->select();
  59. if (empty($rows)) {
  60. $this->success('成功', []);
  61. }
  62. $treeMap = [];
  63. foreach ($rows as $row) {
  64. $date = $row['date'];
  65. if (empty($date)) {
  66. continue;
  67. }
  68. // 拆分年、年月、日期
  69. $year = substr($date, 0, 4);
  70. $yearMonth = substr($date, 0, 7);
  71. $process = $row['process'];
  72. $sysId = $row['sys_id'] !== '' ? $row['sys_id'] : '未知';
  73. // 构建五级结构
  74. if (!isset($treeMap[$year])) {
  75. $treeMap[$year] = [];
  76. }
  77. if (!isset($treeMap[$year][$yearMonth])) {
  78. $treeMap[$year][$yearMonth] = [];
  79. }
  80. if (!isset($treeMap[$year][$yearMonth][$date])) {
  81. $treeMap[$year][$yearMonth][$date] = [
  82. 'processes' => [],
  83. ];
  84. }
  85. if (!isset($treeMap[$year][$yearMonth][$date]['processes'][$process])) {
  86. $treeMap[$year][$yearMonth][$date]['processes'][$process] = [];
  87. }
  88. if (!in_array($sysId, $treeMap[$year][$yearMonth][$date]['processes'][$process], true)) {
  89. $treeMap[$year][$yearMonth][$date]['processes'][$process][] = $sysId;
  90. }
  91. }
  92. // 工序固定排序
  93. $processOrder = ['裁剪', '车缝', '手工', '大烫', '总检', '包装', '其他'];
  94. $result = [];
  95. krsort($treeMap);
  96. foreach ($treeMap as $year => $yearMonths) {
  97. $yearNode = [
  98. 'label' => (string)$year,
  99. 'value' => (string)$year,
  100. 'children' => [],
  101. ];
  102. krsort($yearMonths);
  103. foreach ($yearMonths as $yearMonth => $dates) {
  104. $yearMonthNode = [
  105. 'label' => $yearMonth,
  106. 'value' => $yearMonth,
  107. 'children' => [],
  108. ];
  109. krsort($dates);
  110. foreach ($dates as $dateKey => $dateData) {
  111. $processChildren = [];
  112. $sortedProcesses = $this->sortProcessList(array_keys($dateData['processes']), $processOrder);
  113. foreach ($sortedProcesses as $process) {
  114. $groups = $dateData['processes'][$process];
  115. sort($groups);
  116. $groupChildren = [];
  117. foreach ($groups as $group) {
  118. $groupChildren[] = [
  119. 'label' => $group,
  120. 'value' => $group,
  121. ];
  122. }
  123. $processChildren[] = [
  124. 'label' => $process,
  125. 'value' => $process,
  126. 'children' => $groupChildren,
  127. ];
  128. }
  129. $dateNode = [
  130. 'label' => $dateKey,
  131. 'value' => $dateKey,
  132. 'children' => $processChildren,
  133. ];
  134. $yearMonthNode['children'][] = $dateNode;
  135. }
  136. $yearNode['children'][] = $yearMonthNode;
  137. }
  138. $result[] = $yearNode;
  139. }
  140. $this->success('成功', $result);
  141. }
  142. /**
  143. * 按指定顺序排序工序
  144. * @param array $processList
  145. * @param array $order
  146. * @return array
  147. */
  148. private function sortProcessList(array $processList, array $order): array
  149. {
  150. $sorted = [];
  151. foreach ($order as $item) {
  152. if (in_array($item, $processList)) {
  153. $sorted[] = $item;
  154. unset($processList[array_search($item, $processList)]);
  155. }
  156. }
  157. return array_merge($sorted, $processList);
  158. }
  159. /**
  160. * 查询员工工资列表
  161. * @ApiMethod (GET)
  162. * @return array
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @throws \think\exception\DbException
  166. */
  167. public function GetStaffSalaryList()
  168. {
  169. if (!$this->request->isGet()) {
  170. $this->error('请求方法错误');
  171. }
  172. $param = $this->request->param();
  173. if (empty($param['sys_rq'])) {
  174. $this->error('请选择日期(sys_rq)');
  175. }
  176. $sysRq = $param['sys_rq'];
  177. $bigProcess = !empty($param['big_process']) ? $param['big_process'] : '';
  178. $group = !empty($param['group']) ? $param['group'] : '';
  179. // ========== 第一步:连表查询员工列表(无exp、无whereRaw) ==========
  180. $staffWhere = [];
  181. $staffWhere['a.status'] = 1;
  182. // 日期条件:纯LIKE匹配,兼容带时分秒的sys_rq
  183. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $sysRq)) {
  184. $staffWhere['b.sys_rq'] = ['like', $sysRq . '%'];
  185. } elseif (preg_match('/^\d{4}-\d{2}$/', $sysRq)) {
  186. $staffWhere['b.sys_rq'] = ['like', $sysRq . '%'];
  187. } elseif (preg_match('/^\d{4}$/', $sysRq)) {
  188. $staffWhere['b.sys_rq'] = ['like', $sysRq . '%'];
  189. } else {
  190. $this->error('sys_rq 格式错误,请使用 2026 / 2026-06 / 2026-06-08 格式');
  191. }
  192. // 工序筛选(可选)
  193. if (!empty($bigProcess)) {
  194. $staffWhere['a.big_process'] = $bigProcess;
  195. }
  196. // 小组筛选(可选)
  197. if (!empty($group)) {
  198. $staffWhere['b.sys_id'] = $group;
  199. }
  200. // 关键:用whereNull处理del_rq IS NULL,彻底解决EXP报错
  201. $staff = db('人员_基本资料')
  202. ->alias('a')
  203. ->join('设备_工分计酬 b','a.staff_no = b.staff_no','left')
  204. ->where($staffWhere)
  205. ->whereNull('b.del_rq')
  206. ->field('a.staff_no,a.staff_name,sum(b.salary) as salary')
  207. ->group('a.staff_no,a.staff_name')
  208. ->select();
  209. if(empty($staff)){
  210. $this->error('当前条件下暂无工资数据');
  211. }
  212. $staffMap = [];
  213. $staffNos = [];
  214. foreach ($staff as $item) {
  215. $staffMap[$item['staff_no']] = $item['staff_name'];
  216. $staffNos[] = $item['staff_no'];
  217. }
  218. // ========== 第二步:查询明细数据(无exp、无whereRaw) ==========
  219. $detailWhere = [];
  220. $detailWhere['staff_no'] = ['in', $staffNos];
  221. // 日期条件:和上面保持一致
  222. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $sysRq)) {
  223. $detailWhere['sys_rq'] = ['like', $sysRq . '%'];
  224. } elseif (preg_match('/^\d{4}-\d{2}$/', $sysRq)) {
  225. $detailWhere['sys_rq'] = ['like', $sysRq . '%'];
  226. } elseif (preg_match('/^\d{4}$/', $sysRq)) {
  227. $detailWhere['sys_rq'] = ['like', $sysRq . '%'];
  228. }
  229. // 工序筛选(可选)
  230. if (!empty($bigProcess)) {
  231. $detailWhere['majorprocess'] = $bigProcess;
  232. }
  233. // 小组筛选(可选)
  234. if (!empty($group)) {
  235. $detailWhere['sys_id'] = $group;
  236. }
  237. // 关键:用whereNull处理del_rq IS NULL
  238. $salaryRows = db('设备_工分计酬')
  239. ->where($detailWhere)
  240. ->whereNull('del_rq')
  241. // 新增sys_id字段,并在group里加上,确保不同小组的数据不被合并
  242. ->field('staff_no,staff_name,DATE_FORMAT(sys_rq, "%Y-%m-%d") as date,sum(salary) as salary,sys_id')
  243. ->group('staff_no,DATE_FORMAT(sys_rq, "%Y-%m-%d"),sys_id')
  244. ->order('staff_no asc,sys_rq asc')
  245. ->select();
  246. $grouped = [];
  247. foreach ($salaryRows as $row) {
  248. $staffNo = $row['staff_no'];
  249. if (!isset($grouped[$staffNo])) {
  250. $name = !empty($row['staff_name']) ? $row['staff_name'] : (isset($staffMap[$staffNo]) ? $staffMap[$staffNo] : '');
  251. $grouped[$staffNo] = [
  252. 'staff' => $staffNo . '-' . $name,
  253. 'total_salary' => 0,
  254. 'children' => [],
  255. ];
  256. }
  257. $grouped[$staffNo]['children'][] = [
  258. '员工编号' => $staffNo,
  259. '员工姓名' => !empty($row['staff_name']) ? $row['staff_name'] : (isset($staffMap[$staffNo]) ? $staffMap[$staffNo] : ''),
  260. '日期' => $row['date'],
  261. '工资' => $row['salary'],
  262. // 新增小组字段
  263. '小组' => $row['sys_id'],
  264. ];
  265. $grouped[$staffNo]['total_salary'] += $row['salary'];
  266. }
  267. $this->success('成功', array_values($grouped));
  268. }
  269. // /**
  270. // * 查询员工工资列表
  271. // * @ApiMethod (GET)
  272. // * @return array
  273. // * @throws \think\db\exception\DataNotFoundException
  274. // * @throws \think\db\exception\ModelNotFoundException
  275. // * @throws \think\exception\DbException
  276. // */
  277. // public function GetStaffSalaryList()
  278. // {
  279. // if (!$this->request->isGet()) {
  280. // $this->error('请求方法错误');
  281. // }
  282. // $param = $this->request->param();
  283. // if (empty($param['month'])) {
  284. // $this->error('请选择月份');
  285. // }
  286. // if (empty($param['big_process'])) {
  287. // $this->error('请选择部门');
  288. // }
  289. // //获取大工序员工列表
  290. // $staff = db('人员_基本资料')
  291. // ->alias('a')
  292. // ->join('设备_工分计酬 b','a.staff_no = b.staff_no','left')
  293. // ->where('a.big_process',$param['big_process'])
  294. // ->where('a.status',1)
  295. // ->where('b.date', 'like', $param['month'] . '%')
  296. // ->field('a.staff_no,a.staff_name,sum(b.salary) as salary')
  297. // ->group('a.staff_no,a.staff_name')
  298. // ->select();
  299. // if(empty($staff)){
  300. // $this->error('该工序没有报工数据');
  301. // }
  302. //
  303. // $staffMap = [];
  304. // $staffNos = [];
  305. // foreach ($staff as $item) {
  306. // $staffMap[$item['staff_no']] = $item['staff_name'];
  307. // $staffNos[] = $item['staff_no'];
  308. // }
  309. //
  310. // $salaryRows = db('设备_工分计酬')
  311. // ->where('del_rq', null)
  312. // ->where('date', 'like', $param['month'] . '%')
  313. // ->where('staff_no', 'in', $staffNos)
  314. // ->field('staff_no,staff_name,DATE_FORMAT(date, "%Y-%m-%d") as date,sum(salary) as salary,sys_id')
  315. // ->group('staff_no,DATE_FORMAT(date, "%Y-%m-%d")')
  316. // ->order('staff_no asc,date asc')
  317. // ->select();
  318. //
  319. // $grouped = [];
  320. // foreach ($salaryRows as $row) {
  321. // $staffNo = $row['staff_no'];
  322. // if (!isset($grouped[$staffNo])) {
  323. // $name = !empty($row['staff_name']) ? $row['staff_name'] : (isset($staffMap[$staffNo]) ? $staffMap[$staffNo] : '');
  324. // $grouped[$staffNo] = [
  325. // 'staff' => $staffNo . '-' . $name,
  326. // 'total_salary' => 0,
  327. // 'children' => [],
  328. // ];
  329. // }
  330. // $grouped[$staffNo]['children'][] = [
  331. // '员工编号' => $staffNo,
  332. // '员工姓名' => !empty($row['staff_name']) ? $row['staff_name'] : (isset($staffMap[$staffNo]) ? $staffMap[$staffNo] : ''),
  333. // '日期' => $row['date'],
  334. // '工资' => $row['salary'],
  335. // ];
  336. // $grouped[$staffNo]['total_salary'] += $row['salary'];
  337. // }
  338. //
  339. // $this->success('成功', array_values($grouped));
  340. //
  341. // }
  342. /**
  343. * 查询员工工资详情
  344. *
  345. */
  346. public function GetStaffSalaryDetail()
  347. {
  348. if (!$this->request->isGet()){
  349. $this->error('请求方法错误');
  350. }
  351. $param = $this->request->param();
  352. if (empty($param['staff_no'])) {
  353. $this->error('请选择员工');
  354. }
  355. if(empty($param['date'])){
  356. $this->error('请选择日期');
  357. }
  358. $where = [
  359. 'a.staff_no' => $param['staff_no'],
  360. 'a.sys_rq' => ['like', $param['date'] . '%'],
  361. 'a.del_rq' => null,
  362. ];
  363. $list = db('设备_工分计酬')
  364. ->alias('a')
  365. ->join('工单_部件资料 b', 'a.work_order = b.work_order and a.part_code = b.part_code', 'left')
  366. ->join('工单_基本资料 c', 'a.work_order = c.订单编号', 'left')
  367. ->field('a.work_order as 订单编号,DATE_FORMAT(a.date, "%Y-%m-%d") as 日期,b.part_name as 部件名称,
  368. a.part_code as 部件编号,a.salary as 工资,a.number as 数量,a.production_hour as 生产工时,a.production_score as 生产分数,
  369. a.machine as 设备名称,a.process_code as 工序编号,a.process_name as 工序名称,a.standard_hour as 标准工时,a.standard_score as 标准分数,
  370. a.coefficient as 系数,a.sys_id as 设备编号,c.生产款号,c.款式')
  371. ->where($where)
  372. ->order('a.sys_rq asc,a.process_code asc')
  373. ->select();
  374. if(empty($list)){
  375. $this->error('没有数据');
  376. }
  377. $this->success('成功', $list);
  378. }
  379. /**
  380. * 查询月份员工工资数据
  381. * @ApiMethod (GET)
  382. * @return array
  383. * @throws \think\db\exception\DataNotFoundException
  384. * @throws \think\db\exception\ModelNotFoundException
  385. * @throws \think\exception\DbException
  386. */
  387. public function GetStaffSalaryMonth()
  388. {
  389. if (!$this->request->isGet()){
  390. $this->error('请求方法错误');
  391. }
  392. $param = $this->request->param();
  393. if (empty($param['month'])) {
  394. $this->error('请选择月份');
  395. }
  396. $where = [
  397. 'date' => ['like', $param['month'] . '%'],
  398. 'del_rq' => null,
  399. ];
  400. if (!empty($param['search'])) {
  401. $where['staff_no|staff_name'] = ['like', '%' . $param['search'] . '%'];
  402. }
  403. $list = db('设备_工分计酬')
  404. ->where($where)
  405. ->field('staff_no,staff_name,sum(salary) as salary')
  406. ->group('staff_no,staff_name')
  407. ->field('staff_no,staff_name,DATE_FORMAT(date, "%Y-%m-%d") as date,sum(salary) as salary')
  408. ->group('staff_no,DATE_FORMAT(date, "%Y-%m-%d")')
  409. ->order('staff_no asc,sys_rq asc')
  410. ->select();
  411. if(empty($list)){
  412. $this->error('没有数据');
  413. }
  414. $this->success('成功', $list);
  415. }
  416. }