StaffSalary.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $data = db('设备_工分计酬')
  27. ->where('del_rq', null)
  28. ->field('DISTINCT DATE_FORMAT(date, "%Y-%m") as month')
  29. ->order('date desc')
  30. ->select();
  31. //获取员工部门
  32. $big_process = db('人员_基本资料')
  33. ->field('DISTINCT big_process')
  34. ->select();
  35. foreach ($big_process as $v) {
  36. $big_process_name[] = $v['big_process'];
  37. }
  38. $result = [];
  39. foreach ($data as $v) {
  40. $result[$v['month']] = $big_process_name;
  41. }
  42. $this->success('成功', $result);
  43. }
  44. /**
  45. * 查询员工工资列表
  46. */
  47. public function GetStaffSalaryList()
  48. {
  49. if (!$this->request->isGet()) {
  50. $this->error('请求方法错误');
  51. }
  52. $param = $this->request->param();
  53. if (empty($param['month'])) {
  54. $this->error('请选择月份');
  55. }
  56. if (empty($param['big_process'])) {
  57. $this->error('请选择部门');
  58. }
  59. //获取大工序员工列表
  60. $staff = db('人员_基本资料')
  61. ->where('big_process',$param['big_process'])
  62. ->where('status',1)
  63. ->field('staff_no,staff_name')
  64. ->select();
  65. if(empty($staff)){
  66. $this->error('该部门没有员工');
  67. }
  68. $staffMap = [];
  69. $staffNos = [];
  70. foreach ($staff as $item) {
  71. $staffMap[$item['staff_no']] = $item['staff_name'];
  72. $staffNos[] = $item['staff_no'];
  73. }
  74. $salaryRows = db('设备_工分计酬')
  75. ->where('del_rq', null)
  76. ->where('date', 'like', $param['month'] . '%')
  77. ->where('staff_no', 'in', $staffNos)
  78. ->field('staff_no,staff_name,DATE_FORMAT(date, "%Y-%m-%d") as date,sum(salary) as salary')
  79. ->group('staff_no,DATE_FORMAT(date, "%Y-%m-%d")')
  80. ->order('staff_no asc,date asc')
  81. ->select();
  82. $grouped = [];
  83. foreach ($salaryRows as $row) {
  84. $staffNo = $row['staff_no'];
  85. if (!isset($grouped[$staffNo])) {
  86. $name = !empty($row['staff_name']) ? $row['staff_name'] : (isset($staffMap[$staffNo]) ? $staffMap[$staffNo] : '');
  87. $grouped[$staffNo] = [
  88. 'staff' => $staffNo . '-' . $name,
  89. 'children' => [],
  90. ];
  91. }
  92. $grouped[$staffNo]['children'][] = [
  93. '员工编号' => $staffNo,
  94. '员工姓名' => !empty($row['staff_name']) ? $row['staff_name'] : (isset($staffMap[$staffNo]) ? $staffMap[$staffNo] : ''),
  95. '日期' => $row['date'],
  96. '工资' => $row['salary'],
  97. ];
  98. }
  99. $this->success('成功', array_values($grouped));
  100. }
  101. /**
  102. * 查询员工工资详情
  103. *
  104. */
  105. public function GetStaffSalaryDetail()
  106. {
  107. if (!$this->request->isGet()){
  108. $this->error('请求方法错误');
  109. }
  110. $param = $this->request->param();
  111. if (empty($param['staff_no'])) {
  112. $this->error('请选择员工');
  113. }
  114. if(empty($param['date'])){
  115. $this->error('请选择日期');
  116. }
  117. $where = [
  118. 'a.staff_no' => $param['staff_no'],
  119. 'a.date' => ['like', $param['date'] . '%'],
  120. 'a.del_rq' => null,
  121. ];
  122. $list = db('设备_工分计酬')
  123. ->alias('a')
  124. ->join('工单_部件资料 b', 'a.work_order = b.work_order and a.part_code = b.part_code', 'left')
  125. ->field('a.work_order as 订单编号,DATE_FORMAT(a.date, "%Y-%m-%d") as 日期,b.part_name as 部件名称,
  126. a.part_code as 部件编号,a.salary as 工资,a.number as 数量,a.production_hour as 生产工时,a.production_score as 生产分数,
  127. a.machine as 设备名称,a.process_name as 工序名称,a.standard_hour as 标准工时,a.standard_score as 标准分数,
  128. a.coefficient as 系数')
  129. ->where($where)
  130. ->order('a.date asc,a.process_code asc')
  131. ->select();
  132. if(empty($list)){
  133. $this->error('没有数据');
  134. }
  135. $this->success('成功', $list);
  136. }
  137. /**
  138. * 查询月份员工工资数据
  139. * @ApiMethod (GET)
  140. * @return array
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. * @throws \think\exception\DbException
  144. */
  145. public function GetStaffSalaryMonth()
  146. {
  147. if (!$this->request->isGet()){
  148. $this->error('请求方法错误');
  149. }
  150. $param = $this->request->param();
  151. if (empty($param['month'])) {
  152. $this->error('请选择月份');
  153. }
  154. $where = [
  155. 'date' => ['like', $param['month'] . '%'],
  156. 'del_rq' => null,
  157. ];
  158. if (!empty($param['search'])) {
  159. $where['staff_no|staff_name'] = ['like', '%' . $param['search'] . '%'];
  160. }
  161. $list = db('设备_工分计酬')
  162. ->where($where)
  163. ->field('staff_no,staff_name,sum(salary) as salary')
  164. ->group('staff_no,staff_name')
  165. ->field('staff_no,staff_name,DATE_FORMAT(date, "%Y-%m-%d") as date,sum(salary) as salary')
  166. ->group('staff_no,DATE_FORMAT(date, "%Y-%m-%d")')
  167. ->order('staff_no asc,date asc')
  168. ->select();
  169. if(empty($list)){
  170. $this->error('没有数据');
  171. }
  172. $this->success('成功', $list);
  173. }
  174. }