StaffSalary.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. * @param string $sys_rq 可选,左侧树日期:2026 / 2026-06 / 2026-06-08
  163. * @param string $big_process 可选,大工序
  164. * @param string $group 可选,小组
  165. * @param string $search 可选,顶部搜索:订单编号/员工编号/员工姓名(也兼容 keyword、work_order、staff_no、staff_name)
  166. * @return array
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. * @throws \think\exception\DbException
  170. */
  171. public function GetStaffSalaryList()
  172. {
  173. if (!$this->request->isGet()) {
  174. $this->error('请求方法错误');
  175. }
  176. $rawParam = $this->request->param();
  177. $param = [];
  178. foreach ($rawParam as $key => $value) {
  179. $normKey = strtolower(rtrim(str_replace([' ', ' '], '', (string)$key), '_'));
  180. if ($normKey === '') {
  181. continue;
  182. }
  183. if (!array_key_exists($normKey, $param) || $param[$normKey] === '' || $param[$normKey] === null) {
  184. $param[$normKey] = $value;
  185. }
  186. }
  187. $sysRq = !empty($param['sys_rq']) ? trim((string)$param['sys_rq']) : '';
  188. $bigProcess = !empty($param['big_process']) ? trim((string)$param['big_process']) : '';
  189. $group = !empty($param['group']) ? trim((string)$param['group']) : '';
  190. $search = '';
  191. foreach (['search', 'keyword', 'work_order', 'staff_no', 'staff_name'] as $key) {
  192. if (!empty($param[$key])) {
  193. $search = trim((string)$param[$key]);
  194. break;
  195. }
  196. }
  197. //日期 或 搜索
  198. if ($sysRq === '' && $search === '') {
  199. $this->error('请选择左侧日期,或输入订单编号/员工编号/姓名进行查询');
  200. }
  201. if ($sysRq !== '') {
  202. if (!preg_match('/^\d{4}(-\d{2})?(-\d{2})?$/', $sysRq)) {
  203. $this->error('sys_rq 格式错误,请使用 2026 / 2026-06 / 2026-06-08 格式');
  204. }
  205. }
  206. // 直接查报工计酬表,支持无日期仅按订单/员工搜索
  207. $query = db('设备_工分计酬')->whereNull('del_rq');
  208. if ($sysRq !== '') {
  209. $query->where('sys_rq', 'like', $sysRq . '%');
  210. }
  211. if ($bigProcess !== '') {
  212. $query->where('majorprocess', $bigProcess);
  213. }
  214. if ($group !== '') {
  215. $query->where('sys_id', $group);
  216. }
  217. if ($search !== '') {
  218. // 订单编号 / 员工编号 / 员工姓名 模糊匹配
  219. $query->where('work_order|staff_no|staff_name', 'like', '%' . $search . '%');
  220. }
  221. $salaryRows = $query
  222. ->field('staff_no,staff_name,DATE_FORMAT(IFNULL(sys_rq, date), "%Y-%m-%d") as date,sum(salary) as salary,sys_id')
  223. ->group('staff_no,DATE_FORMAT(IFNULL(sys_rq, date), "%Y-%m-%d"),sys_id')
  224. ->order('staff_no asc,date asc')
  225. ->select();
  226. if (empty($salaryRows)) {
  227. // $this->error('当前条件下暂无工资数据');
  228. }
  229. $grouped = [];
  230. foreach ($salaryRows as $row) {
  231. $staffNo = $row['staff_no'];
  232. $name = !empty($row['staff_name']) ? $row['staff_name'] : '';
  233. if (!isset($grouped[$staffNo])) {
  234. $grouped[$staffNo] = [
  235. 'staff' => $staffNo . '-' . $name,
  236. 'total_salary' => 0,
  237. 'children' => [],
  238. ];
  239. }
  240. $grouped[$staffNo]['children'][] = [
  241. '员工编号' => $staffNo,
  242. '员工姓名' => $name,
  243. '日期' => $row['date'],
  244. '工资' => $row['salary'],
  245. '小组' => $row['sys_id'],
  246. ];
  247. $grouped[$staffNo]['total_salary'] += $row['salary'];
  248. }
  249. $this->success('成功', array_values($grouped));
  250. }
  251. /**
  252. * 查询员工工资详情
  253. *
  254. */
  255. public function GetStaffSalaryDetail()
  256. {
  257. if (!$this->request->isGet()){
  258. $this->error('请求方法错误');
  259. }
  260. $param = $this->request->param();
  261. if (empty($param['staff_no'])) {
  262. $this->error('请选择员工');
  263. }
  264. if(empty($param['date'])){
  265. $this->error('请选择日期');
  266. }
  267. $where = [
  268. 'a.staff_no' => $param['staff_no'],
  269. 'a.sys_rq' => ['like', $param['date'] . '%'],
  270. 'a.del_rq' => null,
  271. 'c.mod_rq' => null,
  272. ];
  273. $list = db('设备_工分计酬')
  274. ->alias('a')
  275. ->join('工单_部件资料 b', 'a.work_order = b.work_order and a.part_code = b.part_code', 'left')
  276. ->join('工单_基本资料 c', 'a.work_order = c.订单编号', 'left')
  277. ->field('a.id,a.work_order as 订单编号,DATE_FORMAT(a.date, "%Y-%m-%d") as 日期,a.sys_rq,b.part_name as 部件名称,
  278. a.part_code as 部件编号,a.salary as 工资,a.number as 数量,a.production_hour as 生产工时,a.production_score as 生产分数,
  279. a.machine as 设备名称,a.process_code as 工序编号,a.process_name as 工序名称,a.standard_hour as 标准工时,a.standard_score as 标准分数,
  280. a.coefficient as 系数,a.sys_id as 设备编号,c.生产款号,c.款式')
  281. ->where($where)
  282. ->order('a.sys_rq asc,a.process_code asc')
  283. ->select();
  284. if(empty($list)){
  285. $this->error('没有数据');
  286. }
  287. $this->success('成功', $list);
  288. }
  289. /**
  290. * 查询月份员工工资数据
  291. * @ApiMethod (GET)
  292. * @return array
  293. * @throws \think\db\exception\DataNotFoundException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. * @throws \think\exception\DbException
  296. */
  297. public function GetStaffSalaryMonth()
  298. {
  299. if (!$this->request->isGet()){
  300. $this->error('请求方法错误');
  301. }
  302. $param = $this->request->param();
  303. if (empty($param['month'])) {
  304. $this->error('请选择月份');
  305. }
  306. $where = [
  307. 'date' => ['like', $param['month'] . '%'],
  308. 'del_rq' => null,
  309. ];
  310. if (!empty($param['search'])) {
  311. $where['staff_no|staff_name'] = ['like', '%' . $param['search'] . '%'];
  312. }
  313. $list = db('设备_工分计酬')
  314. ->where($where)
  315. ->field('staff_no,staff_name,sum(salary) as salary')
  316. ->group('staff_no,staff_name')
  317. ->field('staff_no,staff_name,DATE_FORMAT(date, "%Y-%m-%d") as date,sum(salary) as salary')
  318. ->group('staff_no,DATE_FORMAT(date, "%Y-%m-%d")')
  319. ->order('staff_no asc,sys_rq asc')
  320. ->select();
  321. if(empty($list)){
  322. $this->error('没有数据');
  323. }
  324. $this->success('成功', $list);
  325. }
  326. }