StaffSalary.php 15 KB

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