|
@@ -917,5 +917,113 @@ class StatisticService extends Service
|
|
|
return $item_year_list;
|
|
return $item_year_list;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function itemEmployeeSalaryStatistic($data,$user){
|
|
|
|
|
+ list($status, $month_start, $month_end) = $this->commonRule($data);
|
|
|
|
|
+ if (!$status) return [false, $month_start];
|
|
|
|
|
+ //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
|
|
|
|
|
+ //获取人员工资项目相关分组数据
|
|
|
|
|
+ $month_employee = DailyPwOrderDetails::Clear($user, $data);
|
|
|
|
|
+ $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
|
|
|
|
|
+ ->where('del_time', 0)
|
|
|
|
|
+ ->select(
|
|
|
|
|
+ "item_id",
|
|
|
|
|
+ "employee_id",
|
|
|
|
|
+ // 将时间戳转为 Y-m-d 格式并起别名
|
|
|
|
|
+ DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
|
|
|
|
|
+ // 聚合求和
|
|
|
|
|
+ DB::raw("SUM(total_work_min) as total_work")
|
|
|
|
|
+ )
|
|
|
|
|
+ ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
|
|
|
|
|
+ //查询所有人员工资
|
|
|
|
|
+ $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", "salary", "main_id","social_insurance","public_housing_fund")
|
|
|
|
|
+ ->get()->toArray();
|
|
|
|
|
+ //查询所有项目人员的工时比例
|
|
|
|
|
+ //汇总每个人每个月工资
|
|
|
|
|
+ $salary_map = [];
|
|
|
|
|
+ foreach ($month_employee_salary as $val) {
|
|
|
|
|
+ $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
|
|
|
|
|
+ if ($month) {
|
|
|
|
|
+ $salary_map[$val['employee_id'] . '_' . $month] = $val;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+// var_dump($salary_map);die;
|
|
|
|
|
+ // 2. 计算每个员工在每个月的全月总工时
|
|
|
|
|
+ $employee_monthly_total_min = [];
|
|
|
|
|
+ foreach ($month_employee_list as $row) {
|
|
|
|
|
+ $key = $row['employee_id'] . '_' . $row['order_month'];
|
|
|
|
|
+ if (!isset($employee_monthly_total_min[$key])) {
|
|
|
|
|
+ $employee_monthly_total_min[$key] = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $employee_monthly_total_min[$key] += $row['total_work'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 计算分摊天数与工资
|
|
|
|
|
+ $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" => 0,
|
|
|
|
|
+ "work_minutes" => 0,
|
|
|
|
|
+ "salary" => $salary_map[$key]['salary'],
|
|
|
|
|
+ "social_insurance" => $salary_map[$key]['social_insurance'],
|
|
|
|
|
+ "public_housing_fund" => $salary_map[$key]['public_housing_fund'],
|
|
|
|
|
+ "item_id" => $item['item_id'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ $total_min = $employee_monthly_total_min[$key] ?? 0;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
|
|
|
|
|
+ if ($total_min > 0) {
|
|
|
|
|
+ $ratio = round($item['total_work'] / $total_min,4);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ $ratio = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $item_month_list[$item_key]['ratio'] = $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 = Employee::Clear($user, $data);
|
|
|
|
|
+ $employee_list = $employee->wherein('id', $employee_ids)->select("major","title", "id")->get()->toArray();
|
|
|
|
|
+ $employee_key_list = [];
|
|
|
|
|
+ foreach ($employee_list as $v){
|
|
|
|
|
+ $employee_key_list[$v['id']] = $v;
|
|
|
|
|
+ }
|
|
|
|
|
+ $item = Item::Clear($user, $data);
|
|
|
|
|
+ $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
|
|
|
|
|
+ $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
|
|
|
|
|
+ //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
|
|
|
|
|
+ $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list,$employee_key_list) {
|
|
|
|
|
+ $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_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['item_id']})";
|
|
|
|
|
+ $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['item_id']})";
|
|
|
|
|
+ $item['total_min'] = round($item['total_min']/60,2);
|
|
|
|
|
+ $item['work_minutes'] = round($item['work_minutes']/60,2);
|
|
|
|
|
+ $item['work_salary'] = round($item['work_salary']*$item['radio'],2);
|
|
|
|
|
+ $item['work_social_insurance'] = round($item['social_insurance']*$item['radio'],2);
|
|
|
|
|
+ $item['work_public_housing_fund'] = round($item['public_housing_fund']*$item['radio'],2);
|
|
|
|
|
+ return $item;
|
|
|
|
|
+ })->all();
|
|
|
|
|
+ return [true, $item_month_list];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
}
|
|
}
|