StaffSalary.php 6.7 KB

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