|
@@ -14,6 +14,8 @@ use App\Model\Item;
|
|
|
use App\Model\ItemDetails;
|
|
use App\Model\ItemDetails;
|
|
|
use App\Model\MonthlyPsOrder;
|
|
use App\Model\MonthlyPsOrder;
|
|
|
use App\Model\MonthlyPsOrderDetails;
|
|
use App\Model\MonthlyPsOrderDetails;
|
|
|
|
|
+use App\Model\MonthlyPwOrder;
|
|
|
|
|
+use App\Model\MonthlyPwOrderDetails;
|
|
|
use App\Service\Statistic\StatisticCommonService;
|
|
use App\Service\Statistic\StatisticCommonService;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
@@ -949,7 +951,7 @@ class StatisticService extends StatisticCommonService
|
|
|
return $return_item_list;
|
|
return $return_item_list;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function itemEmployeeSalaryStatistic($data, $user)
|
|
|
|
|
|
|
+ public function itemEmployeeSalaryStatisticold($data, $user)
|
|
|
{
|
|
{
|
|
|
list($status, $month_start, $month_end) = $this->commonRule($data);
|
|
list($status, $month_start, $month_end) = $this->commonRule($data);
|
|
|
if (!$status) return [false, $month_start];
|
|
if (!$status) return [false, $month_start];
|
|
@@ -1117,6 +1119,198 @@ class StatisticService extends StatisticCommonService
|
|
|
return [true, $item_month_list];
|
|
return [true, $item_month_list];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function itemEmployeeSalaryStatistic($data, $user)
|
|
|
|
|
+ {
|
|
|
|
|
+ list($status, $month_start, $month_end) = $this->commonRule($data);
|
|
|
|
|
+ if (!$status) return [false, $month_start];
|
|
|
|
|
+
|
|
|
|
|
+ // 获取人员工资项目相关分组数据
|
|
|
|
|
+ $month_employee_list = $this->getItemEmployeeMonthWorkList($user, $data, $month_start, $month_end);
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 查询所有人员工资单
|
|
|
|
|
+ $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)
|
|
|
|
|
+ ->where('del_time', 0)
|
|
|
|
|
+ ->where("month", ">=", $month_start)
|
|
|
|
|
+ ->where("month", "<", $month_end)
|
|
|
|
|
+ ->pluck('id')->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
|
|
|
|
|
+ ->where('del_time', 0)
|
|
|
|
|
+ ->where("month", ">=", $month_start)
|
|
|
|
|
+ ->where("month", "<", $month_end)
|
|
|
|
|
+ ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
|
|
|
|
|
+ ->pluck('month_str', 'id')
|
|
|
|
|
+ ->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
|
|
|
|
|
+ ->select("employee_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id", "social_insurance", "public_housing_fund")
|
|
|
|
|
+ ->get()->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 查询所有人员月度工时单 (新增逻辑)
|
|
|
|
|
+ $monthly_pw_order_key_list = MonthlyPwOrder::Clear($user, $data)
|
|
|
|
|
+ ->where('del_time', 0)
|
|
|
|
|
+ ->where("month", ">=", $month_start)
|
|
|
|
|
+ ->where("month", "<", $month_end)
|
|
|
|
|
+ ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
|
|
|
|
|
+ ->pluck('month_str', 'id')
|
|
|
|
|
+ ->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ $monthly_pw_order_ids = array_keys($monthly_pw_order_key_list);
|
|
|
|
|
+
|
|
|
|
|
+ $month_employee_pw = MonthlyPwOrderDetails::whereIn('main_id', $monthly_pw_order_ids)
|
|
|
|
|
+ ->where('del_time', 0)
|
|
|
|
|
+ ->select('employee_id', 'main_id', 'total_hours')
|
|
|
|
|
+ ->get()->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ $pw_hours_map = [];
|
|
|
|
|
+ foreach ($month_employee_pw as $pw_val) {
|
|
|
|
|
+ $pw_month = $monthly_pw_order_key_list[$pw_val['main_id']] ?? '';
|
|
|
|
|
+ if ($pw_month) {
|
|
|
|
|
+ $pw_key = $pw_val['employee_id'] . '_' . $pw_month;
|
|
|
|
|
+ $pw_hours_map[$pw_key] = (float)$pw_val['total_hours'];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 汇总每个人每个月工资
|
|
|
|
|
+ $salary_map = [];
|
|
|
|
|
+ $all_salary = [];
|
|
|
|
|
+ foreach ($month_employee_salary as $val) {
|
|
|
|
|
+ $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
|
|
|
|
|
+ if ($month) {
|
|
|
|
|
+ $key = $val['employee_id'] . '_' . $month;
|
|
|
|
|
+ $salary_map[$key] = $val;
|
|
|
|
|
+ $all_salary[$key] = [
|
|
|
|
|
+ "salary" => (float)$val['salary'] * 100,
|
|
|
|
|
+ "social_insurance" => (float)$val['social_insurance'] * 100,
|
|
|
|
|
+ "public_housing_fund" => (float)$val['public_housing_fund'] * 100,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 获取每个员工在每个月的全月总工时(改自月度工时单)
|
|
|
|
|
+ $employee_monthly_total_min = [];
|
|
|
|
|
+ $all_min = [];
|
|
|
|
|
+ foreach ($month_employee_list as $row) {
|
|
|
|
|
+ $key = $row['employee_id'] . '_' . $row['order_month'];
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($employee_monthly_total_min[$key])) {
|
|
|
|
|
+ // 直接从工时单映射中获取小时数,乘以 60 转为分钟供后续比例计算
|
|
|
|
|
+ $total_hours = $pw_hours_map[$key] ?? 0;
|
|
|
|
|
+ $employee_monthly_total_min[$key] = $total_hours * 60;
|
|
|
|
|
+ $all_min[$key] = round($total_hours * 100, 2); // 放大100倍用于尾差倒扣
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果此员工该月无工资记录,初始化默认全 0 结构,防止报错
|
|
|
|
|
+ if (!isset($all_salary[$key])) {
|
|
|
|
|
+ $all_salary[$key] = [
|
|
|
|
|
+ "salary" => 0,
|
|
|
|
|
+ "social_insurance" => 0,
|
|
|
|
|
+ "public_housing_fund" => 0,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 计算分摊天数与工资
|
|
|
|
|
+ $item_month_list = [];
|
|
|
|
|
+ foreach ($month_employee_list as $item) {
|
|
|
|
|
+ $key = $item['employee_id'] . '_' . $item['order_month'];
|
|
|
|
|
+ $item_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($item_month_list[$item_key])) {
|
|
|
|
|
+ $item_month_list[$item_key] = [
|
|
|
|
|
+ "month" => $item['order_month'],
|
|
|
|
|
+ "allocated_salary" => 0,
|
|
|
|
|
+ "employee_id" => $item['employee_id'],
|
|
|
|
|
+ "work_minutes" => 0,
|
|
|
|
|
+ "salary" => ($salary_map[$key]['salary'] ?? 0),
|
|
|
|
|
+ "social_insurance" => ($salary_map[$key]['social_insurance'] ?? 0),
|
|
|
|
|
+ "public_housing_fund" => ($salary_map[$key]['public_housing_fund'] ?? 0),
|
|
|
|
|
+ "item_id" => $item['item_id'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $total_min = $employee_monthly_total_min[$key] ?? 0;
|
|
|
|
|
+ // 计算工资分摊比例(项目实际工作分钟数 / 工时单月度总分钟数)
|
|
|
|
|
+ if ($total_min > 0) {
|
|
|
|
|
+ $ratio = round($item['total_work'] / $total_min, 4);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $ratio = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $item_month_list[$item_key]['radio'] = $ratio;
|
|
|
|
|
+ $item_month_list[$item_key]['total_min'] = $total_min;
|
|
|
|
|
+ $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
|
|
|
|
|
+ $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
|
|
|
|
|
+ $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
|
|
|
|
|
+
|
|
|
|
|
+ $employee_list = Employee::TopClear($user, $data)
|
|
|
|
|
+ ->wherein('id', $employee_ids)
|
|
|
|
|
+ ->select("major", "title", "id")
|
|
|
|
|
+ ->get()
|
|
|
|
|
+ ->keyBy('id')
|
|
|
|
|
+ ->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ $item_query = Item::TopClear($user, $data)->wherein('id', $items);
|
|
|
|
|
+ $item_title_key_list = (clone $item_query)->pluck("title", "id")->toArray();
|
|
|
|
|
+ $item_code_key_list = (clone $item_query)->pluck("code", "id")->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ $collect = collect($item_month_list);
|
|
|
|
|
+ $employee_count = $collect->groupBy(function ($item) {
|
|
|
|
|
+ return $item['employee_id'] . '_' . $item['month'];
|
|
|
|
|
+ })->map(function ($group) {
|
|
|
|
|
+ return $group->count();
|
|
|
|
|
+ })->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ // 6. 组装最终结果
|
|
|
|
|
+ $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, $employee_list, &$employee_count, &$all_salary, &$all_min) {
|
|
|
|
|
+ $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
|
|
|
|
|
+ $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
|
|
|
|
|
+ $item['employee_title'] = $employee_list[$item['employee_id']]['title'] ?? "未知人员({$item['employee_id']})";
|
|
|
|
|
+ $item['major'] = $employee_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
|
|
|
|
|
+ $item['total_min'] = round($item['total_min'] / 60, 2);
|
|
|
|
|
+
|
|
|
|
|
+ $key = $item['employee_id'] . '_' . $item['month'];
|
|
|
|
|
+
|
|
|
|
|
+ // 容错处理:确保 $all_salary[$key] 和 $all_min[$key] 一定存在
|
|
|
|
|
+ if (!isset($all_salary[$key])) {
|
|
|
|
|
+ $all_salary[$key] = ['salary' => 0, 'social_insurance' => 0, 'public_housing_fund' => 0];
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!isset($all_min[$key])) {
|
|
|
|
|
+ $all_min[$key] = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (--$employee_count[$key] > 0) {
|
|
|
|
|
+ $work_minutes = round($item['work_minutes'] / 60, 2);
|
|
|
|
|
+ $item['work_minutes'] = $work_minutes;
|
|
|
|
|
+ $all_min[$key] -= ($work_minutes * 100);
|
|
|
|
|
+
|
|
|
|
|
+ $work_salary = round($item['salary'] * $item['radio'], 2);
|
|
|
|
|
+ $all_salary[$key]['salary'] -= ($work_salary * 100);
|
|
|
|
|
+ $item['work_salary'] = $work_salary;
|
|
|
|
|
+
|
|
|
|
|
+ $social_insurance = round($item['social_insurance'] * $item['radio'], 2);
|
|
|
|
|
+ $all_salary[$key]['social_insurance'] -= ($social_insurance * 100);
|
|
|
|
|
+ $item['work_social_insurance'] = $social_insurance;
|
|
|
|
|
+
|
|
|
|
|
+ $work_public_housing_fund = round($item['public_housing_fund'] * $item['radio'], 2);
|
|
|
|
|
+ $all_salary[$key]['public_housing_fund'] -= ($work_public_housing_fund * 100);
|
|
|
|
|
+ $item['work_public_housing_fund'] = $work_public_housing_fund;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 最后一笔记录通过反扣差额尾差,保证总和 100% 精确对齐
|
|
|
|
|
+ $item['work_salary'] = round($all_salary[$key]['salary'] / 100, 2);
|
|
|
|
|
+ $item['work_social_insurance'] = round($all_salary[$key]['social_insurance'] / 100, 2);
|
|
|
|
|
+ $item['work_public_housing_fund'] = round($all_salary[$key]['public_housing_fund'] / 100, 2);
|
|
|
|
|
+ $item['work_minutes'] = round($all_min[$key] / 100, 2);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $item;
|
|
|
|
|
+ })->all();
|
|
|
|
|
+
|
|
|
|
|
+ return [true, $item_month_list];
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
public function enterpriseRdStatistic($data, $user){
|
|
public function enterpriseRdStatistic($data, $user){
|
|
|
$model = Item::TopClear($user,$data);
|
|
$model = Item::TopClear($user,$data);
|