| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?php
- namespace App\Service;
- use App\Model\AuxiliaryAccount;
- use App\Model\AuxiliaryAccountDetails;
- use App\Model\CalendarDetails;
- 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 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");
- $month_employee_list = $this->limit($month_employee_list, ['*'], $data);
- $dataCollection = collect($month_employee_list['data']);
- $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();
- $month_employee_list['data'] = collect($month_employee_list['data'])->transform(function ($item) use ($employee_key_list, $item_title_key_list, $item_code_key_list) {
- $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']})";
- $item['total_work_hours'] = round($item['total_work'] / 60, 2);
- 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 = [];
- 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_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'] += $total_min;
- }
- foreach ($item_month_list as $k=>$v){
- $item_month_list[$k]['days'] = round($v['work_minutes']/8/60);
- 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();
- $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_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']})";
- 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("order_month", "item_id", "employee_id")->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)
- ->pluck(DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month"), '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'];
- }
- // 3. 计算分摊天数与工资
- $item_month_list = [];
- 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,
- ];
- }
- // 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'] += $total_min;
- }
- foreach ($item_month_list as $k=>$v){
- $item_month_list[$k]['days'] = round($v['work_minutes']/8/60);
- 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();
- $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) use ($item_title_key_list, $item_code_key_list,$employee_key_list) {
- $item['item_title'] = $item_key_list[$item_title_key_list['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']})";
- 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 = DailyPwOrderDetails::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("order_month", "item_id", "device_id")->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)
- ->pluck(DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month"), '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['salary'];
- }
- }
- // 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,
- "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'] += $total_min;
- $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_key_list[$item_title_key_list['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 (!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'));
- 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)];
- }
- }
|