You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
4.8 KiB
146 lines
4.8 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\api\member;
|
|
|
|
use app\model\salary\Salary;
|
|
use app\model\personnel\Personnel;
|
|
use app\model\campus\Campus;
|
|
use core\base\BaseApiService;
|
|
use core\exception\ApiException;
|
|
|
|
/**
|
|
* 员工工资查询服务类
|
|
* Class SalaryService
|
|
* @package app\service\api\member
|
|
*/
|
|
class SalaryService extends BaseApiService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->model = new Salary();
|
|
}
|
|
|
|
/**
|
|
* 获取员工工资分页列表
|
|
* @param array $where
|
|
* @param int $staffId 员工ID
|
|
* @return array
|
|
*/
|
|
public function getPage(array $where = [], int $staffId = 0)
|
|
{
|
|
if (empty($staffId)) {
|
|
throw new ApiException('员工信息不存在');
|
|
}
|
|
|
|
$field = 's.*, p.name as staff_name, c.campus_name as campus_name';
|
|
|
|
$search_model = $this->model
|
|
->alias('s')
|
|
->join('school_personnel p', 's.staff_id = p.id', 'left')
|
|
->leftJoin('school_campus_person_role cpr', 'p.id = cpr.person_id')
|
|
->leftJoin('school_campus c', 'cpr.campus_id = c.id')
|
|
->field($field)
|
|
->where('s.staff_id', $staffId) // 只能查看自己的工资
|
|
->group('s.id') // 添加分组避免重复数据
|
|
->order('s.created_at desc');
|
|
|
|
// 筛选条件 - 按月份筛选
|
|
if (!empty($where['salary_month'])) {
|
|
$search_model->where('s.salary_month', 'like', $where['salary_month'] . '%');
|
|
}
|
|
|
|
$result = $this->pageQuery($search_model);
|
|
|
|
// 处理返回数据,转换状态
|
|
if (!empty($result['data'])) {
|
|
foreach ($result['data'] as $key => $item) {
|
|
$result['data'][$key] = $this->formatSalaryData($item);
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 获取员工工资详情
|
|
* @param int $id 工资记录ID
|
|
* @param int $staffId 员工ID
|
|
* @return array
|
|
*/
|
|
public function getInfo(int $id, int $staffId = 0)
|
|
{
|
|
if (empty($staffId)) {
|
|
throw new ApiException('员工信息不存在');
|
|
}
|
|
|
|
$info = $this->model
|
|
->alias('s')
|
|
->join('school_personnel p', 's.staff_id = p.id', 'left')
|
|
->leftJoin('school_campus_person_role cpr', 'p.id = cpr.person_id')
|
|
->leftJoin('school_campus c', 'cpr.campus_id = c.id')
|
|
->field('s.*, p.name as staff_name, c.campus_name as campus_name')
|
|
->where('s.id', $id)
|
|
->where('s.staff_id', $staffId) // 只能查看自己的工资
|
|
->group('s.id') // 添加分组避免重复数据
|
|
->findOrEmpty()
|
|
->toArray();
|
|
|
|
if (empty($info)) {
|
|
throw new ApiException('工资条不存在或无权限查看');
|
|
}
|
|
|
|
return $this->formatSalaryData($info);
|
|
}
|
|
|
|
/**
|
|
* 格式化工资数据
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
private function formatSalaryData(array $data)
|
|
{
|
|
// 转换发放状态为前端期望的数字格式
|
|
if (isset($data['payment_status'])) {
|
|
switch ($data['payment_status']) {
|
|
case 'pending':
|
|
$data['status'] = 1; // 未发放
|
|
break;
|
|
case 'paid':
|
|
$data['status'] = 2; // 已发放
|
|
break;
|
|
default:
|
|
$data['status'] = 1;
|
|
}
|
|
}
|
|
|
|
// 确保所有数字字段都有默认值
|
|
$numericFields = [
|
|
'base_salary', 'performance_bonus', 'deductions', 'other_subsidies',
|
|
'work_salary', 'mgr_performance', 'social_security', 'individual_income_tax',
|
|
'gross_salary', 'net_salary', 'attendance', 'full_attendance_days'
|
|
];
|
|
|
|
foreach ($numericFields as $field) {
|
|
if (!isset($data[$field]) || $data[$field] === null) {
|
|
$data[$field] = '0.00';
|
|
}
|
|
}
|
|
|
|
// 确保整数字段
|
|
if (!isset($data['full_attendance_days']) || $data['full_attendance_days'] === null) {
|
|
$data['full_attendance_days'] = 0;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|