| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210 |
- <?php
- namespace App\Service;
- use App\Model\AuxiliaryAccount;
- use App\Model\AuxiliaryAccountDetails;
- use App\Model\CalendarDetails;
- use App\Model\DailyDwOrderDetails;
- use App\Model\DailyPwOrderDetails;
- use App\Model\Device;
- use App\Model\Employee;
- use App\Model\ExpenseClaims;
- use App\Model\ExpenseClaimsDetails;
- use App\Model\Fee;
- use App\Model\Item;
- use App\Model\ItemDetails;
- use App\Model\MonthlyDdOrder;
- use App\Model\MonthlyDdOrderDetails;
- use App\Model\MonthlyPsOrder;
- use App\Model\MonthlyPsOrderDetails;
- use App\Model\MonthlyPwOrderDetails;
- use App\Model\RuleSet;
- use App\Model\RuleSetDetails;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- class StatisticService extends Service
- {
- public function employeeDayHourStatistic($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-%d') as order_date"),
- // 聚合求和
- DB::raw("SUM(total_work_min) as total_work")
- )
- ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m-%d')"), "item_id", "employee_id")
- ->orderby("order_date", "asc")->get()->toArray();
- // $month_employee_list = $this->limit($month_employee_list, ['*'], $data);
- $dataCollection = collect($month_employee_list);
- $item_ids = $dataCollection->pluck('item_id')->unique()->values()->all();
- $employee_ids = $dataCollection->pluck('employee_id')->unique()->values()->all();
- $employee = Employee::Clear($user, $data);
- $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
- $item = Item::Clear($user, $data);
- $item_title_key_list = $item->wherein('id', $item_ids)->pluck("title", "id")->toArray();
- $item_code_key_list = $item->wherein('id', $item_ids)->pluck("code", "id")->toArray();
- $collect = collect($month_employee_list);
- $employee_count = $collect->groupBy(function ($item) {
- // 这里的 $item 是集合中的每一行数据
- return $item['employee_id'] . '_' . $item['order_date'];
- })->map(function ($group) {
- return $group->count();
- })->toArray();
- $employee_work_count = $collect->groupBy(fn($item) => $item['employee_id'] . '_' . $item['order_date'])
- ->map(fn($group) => round($group->sum('total_work') / 60, 2))
- ->toArray();
- $collection = collect($month_employee_list);
- // 注意:这里使用 transform,参数名建议写准确
- // 关键点:use 后面加了 & 符号
- $month_employee_list = $collection->transform(function ($item, $index) use ($employee_key_list, $item_title_key_list, $item_code_key_list, &$employee_work_count, &$employee_count) {
- $item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
- $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']})";
- // 如果不是最后一条
- $key = $item['employee_id'].'_'.$item['order_date'];
- if(--$employee_count[$key] > 0){
- $current_hours = round($item['total_work'] / 60, 2);
- $item['total_work_hours'] = $current_hours;
- // 关键:递减外部的 $absolute_total_hours
- $employee_work_count[$key] -= $current_hours;
- }else{
- $item['total_work_hours'] = $employee_work_count[$key];
- }
- return $item;
- })->all();
- return [true, $month_employee_list];
- }
- public function employeeMonthSalaryStatistic($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")
- ->get()->toArray();
- //查询所有项目人员的工时比例
- //汇总每个人每个月工资
- $salary_map = [];
- $all_salary = [];
- foreach ($month_employee_salary as $val) {
- $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
- if ($month) {
- $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
- }
- if(!isset($all_salary[$month])) $all_salary[$month] = 0;
- $all_salary[$month] += $val['salary'];
- }
- // 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'];
- if (!isset($item_month_list[$item_key])) {
- $item_month_list[$item_key] = [
- "month" => $item['order_month'],
- "allocated_salary" => 0,
- "work_minutes" => 0,
- "item_id" => $item['item_id'],
- ];
- }
- $total_salary = $salary_map[$key] ?? 0;
- $total_min = $employee_monthly_total_min[$key] ?? 0;
- // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
- if ($total_min > 0) {
- $ratio = $item['total_work'] / $total_min;
- $allocated_salary = round($ratio * $total_salary, 2);
- } else {
- $allocated_salary = 0;
- }
- $item_month_list[$item_key]['allocated_salary'] += $allocated_salary;
- $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
- }
- foreach ($item_month_list as $k => $v) {
- $item_month_list[$k]['days'] = round($v['work_minutes'] / 8 / 60);
- }
- $collect = collect($item_month_list);
- $total_day = round($collect->sum('work_minutes')/8/60);
- $count = $collect->count();
- $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
- $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
- $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();
- $collect = collect($item_month_list);
- $item_count = $collect->groupBy(function ($item) {
- // 这里的 $item 是集合中的每一行数据
- return $item['month'];
- })->map(function ($group) {
- return $group->count();
- })->toArray();
- $item_day_count = $collect->groupBy(fn($item) => $item['month'])
- ->map(fn($group) => round($group->sum('work_minutes') /8/ 60))
- ->toArray();
- $item_month_list = collect($item_month_list)->transform(function ($item,$index) use ($item_title_key_list, $item_code_key_list,&$all_salary,&$item_day_count,$item_count) {
- $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['allocated_salary'] = round($item['allocated_salary'],2);
- // 如果不是最后一条
- $key = $item['month'];
- if(--$item_count[$key] > 0){
- // 关键:递减外部的 $absolute_total_hours
- $item_day_count[$key] -= $item['days'];
- $all_salary[$key] -= $item['allocated_salary'];
- }else{
- $item['days'] = $item_day_count[$key];
- $item['allocated_salary'] = $all_salary[$key];
- }
- // if ($index < $count - 1) {
- // $total_day -= $item['days'];
- // // 关键:递减外部的 $absolute_total_hours
- // $all_salary -= $item['allocated_salary'];
- // } else {
- // // 最后一条:直接拿剩下的“余额”
- // $item['days'] = $total_day;
- // $item['allocated_salary'] = $all_salary;
- // }
- return $item;
- })->all();
- return [true, $item_month_list];
- }
- public function itemDaySalaryStatistic($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(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "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")
- ->get()->toArray();
- //查询所有项目人员的工时比例
- //汇总每个人每个月工资
- $salary_map = [];
- $all_salary = [];
- foreach ($month_employee_salary as $val) {
- $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
- if ($month) {
- $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
- }
- if(!isset($all_salary[$val['employee_id']])){
- $all_salary[$val['employee_id']] = 0;
- }
- $all_salary[$val['employee_id']] += $val['salary'];
- }
- // 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 = [];
- $total_work_min = [];
- foreach ($month_employee_list as $item) {
- $key = $item['employee_id'] . '_' . $item['order_month'];
- $employee_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
- $total_salary = $salary_map[$key] ?? 0;
- $total_min = $employee_monthly_total_min[$key] ?? 0;
- if (!isset($item_month_list[$employee_key])) {
- $item_month_list[$employee_key] = [
- "month" => $item['order_month'],
- "allocated_salary" => 0,
- "work_minutes" => 0,
- "total_min" => $total_min,
- "total_salary" => $total_salary,
- "item_id" => $item['item_id'],
- "employee_id" => $item['employee_id'],
- ];
- }
- // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
- if ($total_min > 0) {
- $ratio = $item['total_work'] / $total_min;
- $allocated_salary = round($ratio * $total_salary, 2);
- } else {
- $allocated_salary = 0;
- }
- $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
- $item_month_list[$employee_key]['work_minutes'] += $item['total_work'];
- if(!isset( $total_work_min[$item['employee_id']])) $total_work_min[$item['employee_id']] = 0;
- $total_work_min[$item['employee_id']] += $item['total_work'];
- }
- foreach ($total_work_min as $k=>$v){
- $total_work_min[$k] = round($v / 60, 2);
- }
- $collect = collect($item_month_list);
- $employee_count = $collect->groupBy('employee_id')
- ->map(function ($group) {
- return $group->count(); // 统计每个分组里的数量
- })
- ->toArray();
- $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
- $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
- $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();
- $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
- $employee = Employee::Clear($user, $data);
- $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
- $item_month_list = collect($item_month_list)->transform(function ($item,$index) use ($item_title_key_list, $item_code_key_list, $employee_key_list,&$total_work_min,&$all_salary,&$employee_count) {
- $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']] ?? "未知人员({$item['employee_id']})";
- $item['work_hours'] = round($item['work_minutes'] / 60, 2);
- $item['total_hours'] = round($item['total_min'] / 60);
- if(--$employee_count[$item['employee_id']] > 0){
- $all_salary[$item['employee_id']] -= $item['allocated_salary'];
- $total_work_min[$item['employee_id']] -= $item['work_hours'];
- }else{
- $item['allocated_salary'] = $all_salary[$item['employee_id']];
- $item['work_hours'] = $total_work_min[$item['employee_id']];
- }
- return $item;
- })->all();
- return [true, $item_month_list];
- }
- public function itemDeviceMonthStatistic($data, $user)
- {
- //项目编码、项目名称、设备名称、项目工时、研发工时占比、设备原值、设备折旧额、本项目帐面归集的折旧额、确定的本项目折旧额、加计调整金额、当月工时、日期
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) return [false, $month_start];
- //确认所有项目、设备、设备工时
- $month_device = DailyDwOrderDetails::Clear($user, $data);
- $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "item_id",
- "device_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(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
- //查询所有人员工资
- $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
- ->pluck('id')->toArray();
- $monthly_dd_order_key_list = MonthlyDdOrder::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_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
- ->select("device_id", "depreciation_amount", "main_id")
- ->get()->toArray();
- //查询所有项目人员的工时比例
- //汇总每个人每个月工资
- $depreciatio_map = [];
- foreach ($month_device_salary as $val) {
- $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
- if ($month) {
- $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
- }
- }
- // 2. 计算每个员工在每个月的全月总工时
- $device_monthly_total_min = [];
- foreach ($month_device_list as $row) {
- $key = $row['device_id'] . '_' . $row['order_month'];
- if (!isset($device_monthly_total_min[$key])) {
- $device_monthly_total_min[$key] = 0;
- }
- $device_monthly_total_min[$key] += $row['total_work'];
- }
- // 3. 计算分摊天数与工资
- $item_month_list = [];
- foreach ($month_device_list as $item) {
- $key = $item['device_id'] . '_' . $item['order_month'];
- $device_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['device_id'];
- $total_depreciatio = $depreciatio_map[$key] ?? 0;
- $total_min = $device_monthly_total_min[$key] ?? 0;
- if (!isset($item_month_list[$device_key])) {
- $item_month_list[$device_key] = [
- "month" => $item['order_month'],
- "allocated_depreciatio" => 0,
- "work_minutes" => 0,
- "total_min" => $total_min,
- "item_id" => $item['item_id'],
- "device_id" => $item['device_id'],
- "total_depreciatio" => $total_depreciatio,
- ];
- }
- // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
- if ($total_min > 0) {
- $ratio = round($item['total_work'] / $total_min, 3);
- $allocated_salary = round($ratio * $total_depreciatio, 2);
- } else {
- $ratio = 0;
- $allocated_salary = 0;
- }
- $item_month_list[$device_key]['allocated_depreciatio'] += $allocated_salary;
- $item_month_list[$device_key]['work_minutes'] += $item['total_work'];
- $item_month_list[$device_key]['ratio'] = $ratio;
- }
- foreach ($item_month_list as $k => $v) {
- $item_month_list[$k]['total_hours'] = round($v['total_min'] / 60, 1);
- $item_month_list[$k]['hours'] = round($v['work_minutes'] / 60, 1);
- unset($item_month_list[$k]['work_minutes']);
- }
- $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
- $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
- $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();
- $device_ids = collect($item_month_list)->pluck('device_id')->unique()->values()->all();
- $device = Device::Clear($user, $data);
- $device_key_list = $device->wherein('id', $device_ids)->pluck("title", "id")->toArray();
- $device_original_value_key_list = $device->wherein('id', $device_ids)->pluck("original_value", "id")->toArray();
- $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, $device_key_list, $device_original_value_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['device_title'] = $device_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
- $item['device_original'] = $device_original_value_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
- return $item;
- })->all();
- return [true, $item_month_list];
- }
- private function commonRule($data)
- {
- if (!empty($data['year'])) {
- $return = $this->getYearRangeInfo($data['year']);
- if (is_null($return)) return [false, '年度格式错误'];
- list($data['month_start'], $data['month_end']) = $return;
- } else {
- if (isset($data['time']) && !empty($data['time'])) {
- $start = $this->changeDateToDate($data['time'][0]);
- $end = $this->changeDateToDate($data['time'][1], true);
- $data['month_start'] = date("Y-m-d", $start);
- $data['month_end'] = date("Y-m-d", $end);
- }
- }
- if (!isset($data['month_start'])) $month_start = date('Y-01-01');
- else $month_start = date('Y-m-01', strtotime($data['month_start']));
- if (!isset($data['month_end'])) $month_end = date('Y-01-01', strtotime('+1 year', strtotime($month_start)));
- else {
- $start_year = date('Y', strtotime($month_start));
- $end_year = date('Y', strtotime($data['month_end']));
- if ($start_year != $end_year) return [false, "查询不得跨年!", ""];
- $month_end = date('Y-m-01', strtotime($data['month_end'] . ' +1 month'));
- }
- return [true, strtotime($month_start), strtotime($month_end)];
- }
- /**
- * 根据前端 ISO 时间字符串获取该年份的起止日期和时间戳
- * 适配:2019-12-31T16:00:00.000Z 这种带时区的数据
- *
- * @param string $isoStr 前端传来的时间字符串
- * @return array|null
- */
- public function getYearRangeInfo($isoStr)
- {
- if (empty($isoStr)) return null;
- try {
- // 1. 解析 ISO 8601 字符串
- $date = new \DateTime($isoStr);
- // 2. 强制转为中国时区(PRC),处理 16:00:00Z 这种 UTC 偏移
- $date->setTimezone(new \DateTimeZone('PRC'));
- // 3. 提取年份
- $year = $date->format('Y');
- // 4. 构造日期字符串
- $startDate = $year . "-01-01";
- $endDate = $year . "-12-31";
- return [
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- ];
- } catch (\Exception $e) {
- return null;
- }
- }
- public function employeeAttendanceMonthStatistic($data, $user)
- {
- //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) {
- return [false, $month_start];
- }
- //第一步确定项目
- $item = Item::Clear($user, $data);
- $item_list = $item->where('del_time', 0)
- ->where(function ($query) use ($month_start, $month_end) {
- $query->where('start_time', '<=', $month_start)
- ->orWhere('end_time', '>=', $month_end);
- })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
- $item_key_list = [];
- foreach ($item_list as $v) {
- $item_key_list[$v['id']] = $v;
- }
- //第二步确定人员费用
- $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
- //第三步确定折旧费用
- $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
- //第四步其他费用
- list($item_fee_list, $fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
- //组合所有数据
- $return = [];
- foreach ($item_key_list as $v) {
- //其他费用是个数组
- $item_value = [
- "code" => $v['code'],
- "title" => $v['title'],
- "state" => $v['state'] == 3 ? "完结" : "进行中",
- "employee_salary" => $item_employee_list[$v['id']]['salary'] ?? 0,
- "device_depreciation" => $item_device_list[$v['id']]['depreciation'] ?? 0,
- "expense_type" => "费用化支出",
- "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
- ];
- $return[] = $item_value;
- }
- return [true, ["list" => $return, "fee_type_list" => $fee_type_list]];
- }
- private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
- {
- $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")
- ->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['salary'];
- }
- }
- // 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'];
- }
- // 2. 计算分摊天数与工资
- $item_list = [];
- foreach ($month_employee_list as $item) {
- $key = $item['employee_id'] . '_' . $item['order_month'];
- $item_key = $item['item_id'];
- if (!isset($item_list[$item_key])) {
- $item_list[$item_key] = [
- "salary" => 0,
- ];
- }
- $total_salary = $salary_map[$key] ?? 0;
- $total_min = $employee_monthly_total_min[$key] ?? 0;
- // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
- if ($total_min > 0) {
- $ratio = $item['total_work'] / $total_min;
- $allocated_salary = round($ratio * $total_salary, 2);
- } else {
- $allocated_salary = 0;
- }
- $item_list[$item_key]['salary'] += $allocated_salary;
- }
- return $item_list;
- }
- private function getDeviceItemSalary($month_start, $month_end, $data, $user)
- {
- //确认所有项目、设备、设备工时
- $month_device = DailyDwOrderDetails::Clear($user, $data);
- $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "item_id",
- "device_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(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
- //查询所有人员工资
- $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
- ->pluck('id')->toArray();
- $monthly_dd_order_key_list = MonthlyDdOrder::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_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
- ->select("device_id", "depreciation_amount", "main_id")
- ->get()->toArray();
- //查询所有项目人员的工时比例
- //汇总每个人每个月工资
- $depreciatio_map = [];
- foreach ($month_device_salary as $val) {
- $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
- if ($month) {
- $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
- }
- }
- // 2. 计算每个员工在每个月的全月总工时
- $device_monthly_total_min = [];
- foreach ($month_device_list as $row) {
- $key = $row['device_id'] . '_' . $row['order_month'];
- if (!isset($device_monthly_total_min[$key])) {
- $device_monthly_total_min[$key] = 0;
- }
- $device_monthly_total_min[$key] += $row['total_work'];
- }
- // 3. 计算分摊天数与工资
- $item_list = [];
- foreach ($month_device_list as $item) {
- $key = $item['device_id'] . '_' . $item['order_month'];
- $device_key = $item['item_id'];
- $total_depreciatio = $depreciatio_map[$key] ?? 0;
- $total_min = $device_monthly_total_min[$key] ?? 0;
- if (!isset($item_list[$device_key])) {
- $item_list[$device_key] = [
- "depreciation" => 0,
- ];
- }
- // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
- if ($total_min > 0) {
- $ratio = round($item['total_work'] / $total_min, 3);
- $allocated_salary = round($ratio * $total_depreciatio, 2);
- } else {
- $allocated_salary = 0;
- }
- $item_list[$device_key]['depreciation'] += $allocated_salary;
- }
- return $item_list;
- }
- private function getFeeItemSalary($month_start, $month_end, $data, $user)
- {
- //确认所有项目、费用
- $expense = ExpenseClaimsDetails::Clear($user, $data);
- $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "fee_id",
- "amount",
- "item_id",
- "entrust_type"
- )->get()->toArray();
- //需要根据分类去汇总
- $fee = Fee::Clear($user, $data);
- $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
- return $this->groupListByRoot($expense_list, $fee);
- }
- /**
- * 将报销明细按照一级费用和项目分类进行分组
- * * @param array $list 报销明细列表 (含 fee_id, amount 等)
- * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
- * @return array
- */
- public function groupListByRoot(array $list, array $fee_type_list)
- {
- // 1. 建立 ID 索引,方便快速查找
- $idMap = array_column($fee_type_list, null, 'id');
- // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
- $childToRoot = [];
- foreach ($fee_type_list as $type) {
- $current = $type;
- // 向上追溯直到 parent_id 为 0,即找到一级分类
- while ($current['parent_id'] != 0) {
- $current = $idMap[$current['parent_id']];
- }
- $childToRoot[$type['id']] = [
- 'id' => $current['id'],
- 'title' => $current['title'],
- 'sort' => $current['sort']
- ];
- }
- // 3. 遍历明细数据进行分组
- $item_key_list = [];
- $type_list = [];
- foreach ($list as $item) {
- $feeId = $item['fee_id'];
- // 获取该费用对应的一级分类信息
- if (!isset($childToRoot[$feeId])) continue;
- $rootId = $childToRoot[$feeId]['id'];
- $title = $childToRoot[$feeId]['title'];
- $sort = $childToRoot[$feeId]['sort'];
- $key = $item['item_id'];
- if (!isset($item_key_list[$key][$rootId])) {
- $item_key_list[$key][$rootId] = [
- 'id' => $rootId,
- 'total_amount' => 0,
- 'entrust1_amount' => 0, //委内
- 'entrust2_amount' => 0, //委外
- ];
- }
- if ($item['entrust_type'] == 1) {
- $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount'];
- } elseif ($item['entrust_type'] == 2) {
- $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount'];
- }
- $item_key_list[$key][$rootId]['total_amount'] += $item['amount'];
- //这边需要拿到头部所有的一级费用类型
- if (!isset($type_list[$rootId])) {
- $type_list[$rootId] = [
- 'sort' => $sort,
- 'id' => $rootId,
- 'title' => $title,
- ];
- }
- }
- // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
- $type_list = collect($type_list)->sortBy('sort')->values()->all();
- // 4. 重置数组索引并返回
- return [$item_key_list, $type_list];
- }
- public function auxiliaryStatistic($data, $user)
- {
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) return [false, $month_start];
- //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
- //确认所有项目
- $item = Item::Clear($user, $data);
- $item_list = $item->where('del_time', 0)
- ->where(function ($query) use ($month_start, $month_end) {
- $query->where('start_time', '<=', $month_start)
- ->orWhere('end_time', '>', $month_end);
- })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
- $item_key_list = [];
- foreach ($item_list as $v) {
- $item_key_list[$v['id']] = $v;
- }
- //获取该区间内所有项目人工费、折旧费
- $item_salary = $this->auxiliaryEmployee($user, $data, $month_start, $month_end);
- $device_depreciation = $this->auxiliaryDevice($user, $data, $month_start, $month_end);
- $fee = Fee::Clear($user, $data);
- $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
- $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
- $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "item_id",
- "voucher_date",
- "voucher_type",
- "voucher_no",
- "voucher_remark",
- "voucher_amount",
- "aggregation_amount",
- "entrust_type",
- "entrust1_amount",
- "entrust2_amount",
- "entrust2_amount",
- "total_amount",
- "fee_id",
- "type"
- )->get()->toArray();
- list($fee_amount, $fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list, $fee);
- //找到项目和设备的费用然后进行比例计算
- $return = [];
- foreach ($fee_amount as $v) {
- //人工费用
- if ($v['type'] == 1 || $v['type'] == 2) {
- foreach ($item_key_list as $vv) {
- $detail = [
- "code" => $vv['code'],
- "title" => $vv['title'],
- "state" => $vv['state'] == 3 ? "进行中" : "已完成",
- "voucher_date" => date("Y-m-d", $v['voucher_date']),
- "voucher_type" => $v['voucher_type'],
- "voucher_no" => $v['voucher_no'],
- "voucher_remark" => $v['voucher_remark'],
- "voucher_amount" => $v['voucher_amount'],
- "aggregation_amount" => $v['aggregation_amount'],
- "total_amount" => $v['type'] == 1 ? $item_salary[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_salary'] : ($device_depreciation[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_depreciation'] ?? 0),
- "fee_id" => $v['fee_id'],
- "entrust1_amount" => $v['entrust1_amount'],
- "entrust2_amount" => $v['entrust2_amount'],
- "type" => $v['type'],
- ];
- $return[] = $detail;
- }
- } else {
- if (!isset($item_key_list[$v['item_id']])) continue;
- $item_value = $item_key_list[$v['item_id']];
- $detail = [
- "code" => $item_value['code'],
- "title" => $item_value['title'],
- "state" => $item_value['state'] == 3 ? "进行中" : "已完成",
- "voucher_date" => date("Y-m-d", $v['voucher_date']),
- "voucher_type" => $v['voucher_type'],
- "voucher_no" => $v['voucher_no'],
- "voucher_remark" => $v['voucher_remark'],
- "voucher_amount" => $v['voucher_amount'],
- "aggregation_amount" => $v['aggregation_amount'],
- "total_amount" => $v['total_amount'],
- "fee_id" => $v['fee_id'],
- "entrust1_amount" => $v['entrust1_amount'],
- "entrust2_amount" => $v['entrust2_amount'],
- "type" => $v['type'],
- ];
- $return[] = $detail;
- }
- }
- return [true, [
- 'fee_type_list' => $fee_type_list,
- 'list' => $return,
- ]];
- }
- /**
- * 将报销明细按照一级费用和项目分类进行分组
- * * @param array $list 报销明细列表 (含 fee_id, amount 等)
- * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
- * @return array
- */
- public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
- {
- // 1. 建立 ID 索引,方便快速查找
- $idMap = array_column($fee_type_list, null, 'id');
- // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
- $childToRoot = [];
- foreach ($fee_type_list as $type) {
- $current = $type;
- // 向上追溯直到 parent_id 为 0,即找到一级分类
- while ($current['parent_id'] != 0) {
- $current = $idMap[$current['parent_id']];
- }
- $childToRoot[$type['id']] = [
- 'id' => $current['id'],
- 'title' => $current['title'],
- 'sort' => $current['sort']
- ];
- }
- // 3. 遍历明细数据进行分组
- $type_list = [];
- foreach ($list as $k => $item) {
- if ($item['item_id'] == 0 || $item['fee_id'] == 0) {
- continue;
- }
- $feeId = $item['fee_id'];
- // 获取该费用对应的一级分类信息
- if (!isset($childToRoot[$feeId])) continue;
- $rootId = $childToRoot[$feeId]['id'];
- $title = $childToRoot[$feeId]['title'];
- $sort = $childToRoot[$feeId]['sort'];
- $item['fee_id'] = $rootId;
- $list[$k] = $item;
- //这边需要拿到头部所有的一级费用类型
- if (!isset($type_list[$rootId])) {
- $type_list[$rootId] = [
- 'sort' => $sort,
- 'id' => $rootId,
- 'title' => $title,
- ];
- }
- }
- // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
- $type_list = collect($type_list)->sortBy('sort')->values()->all();
- // 4. 重置数组索引并返回
- return [$list, $type_list];
- }
- public function auxiliaryEmployee($user, $data, $month_start, $month_end)
- {
- //确认所有项目、人员、人员工时
- $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")
- ->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['salary'];
- }
- }
- // 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_year_list = [];
- foreach ($month_employee_list as $item) {
- $key = $item['employee_id'] . '_' . $item['order_month'];
- $item_key = $item['item_id'] . '_' . $item['order_month'];
- if (!isset($item_year_list[$item_key])) {
- $item_year_list[$item_key] = [
- "allocated_salary" => 0,
- "item_id" => $item['item_id'],
- ];
- }
- $total_salary = $salary_map[$key] ?? 0;
- $total_min = $employee_monthly_total_min[$key] ?? 0;
- // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
- if ($total_min > 0) {
- $ratio = $item['total_work'] / $total_min;
- $allocated_salary = round($ratio * $total_salary, 2);
- } else {
- $allocated_salary = 0;
- }
- $item_year_list[$item_key]['allocated_salary'] += $allocated_salary;
- }
- return $item_year_list;
- }
- public function auxiliaryDevice($user, $data, $month_start, $month_end)
- {
- $month_device = DailyDwOrderDetails::Clear($user, $data);
- $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "item_id",
- "device_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(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
- //查询所有人员工资
- $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
- ->pluck('id')->toArray();
- $monthly_dd_order_key_list = MonthlyDdOrder::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_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
- ->select("device_id", "depreciation_amount", "main_id")
- ->get()->toArray();
- //查询所有项目人员的工时比例
- //汇总每个人每个月工资
- $depreciatio_map = [];
- foreach ($month_device_salary as $val) {
- $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
- if ($month) {
- $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
- }
- }
- // 2. 计算每个员工在每个月的全月总工时
- $device_monthly_total_min = [];
- foreach ($month_device_list as $row) {
- $key = $row['device_id'] . '_' . $row['order_month'];
- if (!isset($device_monthly_total_min[$key])) {
- $device_monthly_total_min[$key] = 0;
- }
- $device_monthly_total_min[$key] += $row['total_work'];
- }
- // 3. 计算分摊天数与工资
- $item_year_list = [];
- foreach ($month_device_list as $item) {
- $key = $item['device_id'] . '_' . $row['order_month'];
- $device_key = $item['item_id'] . '_' . $item['order_month'];
- $total_depreciatio = $depreciatio_map[$key] ?? 0;
- $total_min = $device_monthly_total_min[$key] ?? 0;
- if (!isset($item_year_list[$device_key])) {
- $item_year_list[$device_key] = [
- "allocated_depreciation" => 0,
- ];
- }
- // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
- if ($total_min > 0) {
- $ratio = round($item['total_work'] / $total_min, 3);
- $allocated_salary = round($ratio * $total_depreciatio, 2);
- } else {
- $allocated_salary = 0;
- }
- $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
- }
- 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" => $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;
- // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
- 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 = 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['employee_id']})";
- $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
- $item['total_min'] = round($item['total_min'] / 60, 2);
- $item['work_minutes'] = round($item['work_minutes'] / 60, 2);
- $item['work_salary'] = round($item['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];
- }
- }
|