StaffSalary.php 6.7 KB

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