| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369 |
- <?php
- namespace App\Service;
- use App\Model\AuxiliaryAccountDetails;
- use App\Model\DailyDwOrderDetails;
- use App\Model\DailyPwOrderDetails;
- use App\Model\Device;
- use App\Model\Employee;
- use App\Model\EmployeeDepartPermission;
- 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\Service\Statistic\StatisticCommonService;
- use Illuminate\Support\Facades\DB;
- class StatisticService extends StatisticCommonService
- {
- 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::TopClear($user, $data);
- $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
- $item = Item::TopClear($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(function ($group) {
- // 1. 算出小时并保留两位小数(例如 26.08)
- $hours = round($group->sum('total_work') / 60, 2);
- // 2. 乘以 100 并强转为整数(例如 2608)
- // 使用 round 是为了再次修正 26.08 * 100 可能产生的 2607.9999 误差
- return (int)round($hours * 100);
- })
- ->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 * 100;
- } else {
- $item['total_work_hours'] = round($employee_work_count[$key] / 100, 2);
- }
- return $item;
- })->all();
- return [true, $month_employee_list];
- }
- public function itemDaySalaryStatistic($data, $user)
- {
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) return [false, $month_start];
- // 1. 获取工时 (复用逻辑)
- $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
- ->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)->where('del_time', 0)
- ->select("item_id", "employee_id", 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();
- // 2. 获取工资 (复用逻辑)
- $ps_query = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end);
- $monthly_ps_order_ids = $ps_query->pluck('id')->toArray();
- $month_map = $ps_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))->pluck('month_str', 'id')->toArray();
- $salary_details = MonthlyPsOrderDetails::whereIn('main_id', $monthly_ps_order_ids)
- ->select("employee_id", "main_id", DB::raw("(base_salary + performance_salary + bonus + other) as s_orig"))->get();
- $salary_map = [];
- foreach ($salary_details as $val) {
- $m = $month_map[$val['main_id']] ?? '';
- if ($m) {
- $k = $val['employee_id'] . '_' . $m;
- $salary_map[$k] = ($salary_map[$k] ?? 0) + (int)round($val['s_orig'] * 100);
- }
- }
- // 3. 统计月总工时与项目数
- $emp_total_min = [];
- $emp_item_count = [];
- foreach ($month_employee_list as $row) {
- $k = $row->employee_id . '_' . $row->order_month;
- $emp_total_min[$k] = ($emp_total_min[$k] ?? 0) + $row->total_work;
- $emp_item_count[$k] = ($emp_item_count[$k] ?? 0) + 1;
- }
- // 4. 分摊并生成明细
- $rem_salary_pool = $salary_map;
- $detail_list = [];
- foreach ($month_employee_list as $item) {
- $k = $item->employee_id . '_' . $item->order_month;
- $total_s = $salary_map[$k] ?? 0;
- $total_m = $emp_total_min[$k] ?? 0;
- if ($total_m > 0 && --$emp_item_count[$k] > 0) {
- $allocated = (int)floor(($item->total_work / $total_m) * $total_s);
- $rem_salary_pool[$k] -= $allocated;
- } else {
- $allocated = $rem_salary_pool[$k] ?? 0;
- }
- $detail_list[] = [
- 'month' => $item->order_month,
- 'item_id' => $item->item_id,
- 'employee_id' => $item->employee_id,
- 'allocated_salary' => round($allocated / 100, 2),
- 'work_hours' => round($item->total_work / 60, 2),
- 'total_hours' => round($total_m / 60, 2),
- 'total_salary' => round($total_s / 100, 2)
- ];
- }
- // 5. 补充名称信息
- $i_ids = collect($detail_list)->pluck('item_id')->unique()->all();
- $e_ids = collect($detail_list)->pluck('employee_id')->unique()->all();
- $i_info = Item::TopClear($user, $data)->whereIn('id', $i_ids)->get()->keyBy('id');
- $e_info = Employee::TopClear($user, $data)->whereIn('id', $e_ids)->get()->keyBy('id');
- $result = collect($detail_list)->sortBy('month')->transform(function ($v) use ($i_info, $e_info) {
- $v['item_title'] = $i_info[$v['item_id']]->title ?? "未知({$v['item_id']})";
- $v['item_code'] = $i_info[$v['item_id']]->code ?? "-";
- $v['employee_title'] = $e_info[$v['employee_id']]->title ?? "未知({$v['employee_id']})";
- return $v;
- })->values()->all();
- return [true, $result];
- }
- public function employeeMonthSalaryStatistic($data, $user)
- {
- // 1. 基础规则验证
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) return [false, $month_start];
- // 2. 获取所有工时明细(按月、项目、人聚合)
- $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
- ->where("order_time", ">=", $month_start)
- ->where("order_time", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "item_id",
- "employee_id",
- 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();
- if ($month_employee_list->isEmpty()) {
- return [true, []];
- }
- // 3. 获取工资数据
- $ps_query = MonthlyPsOrder::Clear($user, $data)
- ->where('del_time', 0)
- ->where("month", ">=", $month_start)
- ->where("month", "<", $month_end);
- $monthly_ps_order_ids = $ps_query->pluck('id')->toArray();
- $month_map = $ps_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
- ->pluck('month_str', 'id')->toArray();
- $salary_details = MonthlyPsOrderDetails::whereIn('main_id', $monthly_ps_order_ids)
- ->select("employee_id", "main_id", DB::raw("(base_salary + performance_salary + bonus + other) as s_orig"))
- ->get();
- // 4. 建立工资映射(统一使用分单位,避免浮点误差)
- $salary_map = [];
- foreach ($salary_details as $val) {
- $m = $month_map[$val['main_id']] ?? '';
- if ($m) {
- $k = $val['employee_id'] . '_' . $m;
- // 采用与 itemDaySalaryStatistic 一致的整数处理
- $salary_map[$k] = ($salary_map[$k] ?? 0) + (int)round($val['s_orig'] * 100);
- }
- }
- // 5. 统计每人每月总工时和参与的项目数(用于精准分摊尾差)
- $emp_total_min = [];
- $emp_item_count = [];
- foreach ($month_employee_list as $row) {
- $k = $row->employee_id . '_' . $row->order_month;
- $emp_total_min[$k] = ($emp_total_min[$k] ?? 0) + $row->total_work;
- $emp_item_count[$k] = ($emp_item_count[$k] ?? 0) + 1;
- }
- // 6. 执行分摊逻辑并按项目汇总
- $item_summary = []; // 存放项目维度的汇总结果
- $rem_salary_pool = $salary_map; // 余额池,按人+月控制
- foreach ($month_employee_list as $item) {
- $k = $item->employee_id . '_' . $item->order_month;
- $total_s = $salary_map[$k] ?? 0;
- $total_m = $emp_total_min[$k] ?? 0;
- // 计算当前员工在该项目上的分摊金额(单位:分)
- if ($total_m > 0 && --$emp_item_count[$k] > 0) {
- // 非该员工的最后一个项目:计算分摊
- $allocated = (int)floor(($item->total_work / $total_m) * $total_s);
- $rem_salary_pool[$k] -= $allocated;
- } else {
- // 该员工的最后一个项目:取余额清零
- $allocated = $rem_salary_pool[$k] ?? 0;
- }
- // 按项目+月份进行累加
- $item_key = $item->order_month . '_' . $item->item_id;
- if (!isset($item_summary[$item_key])) {
- $item_summary[$item_key] = [
- 'month' => $item->order_month,
- 'item_id' => $item->item_id,
- 'allocated_salary' => 0,
- 'work_minutes' => 0,
- ];
- }
- $item_summary[$item_key]['allocated_salary'] += $allocated;
- $item_summary[$item_key]['work_minutes'] += $item['total_work'];
- }
- // 7. 获取项目信息并进行最终格式化
- $i_ids = collect($item_summary)->pluck('item_id')->unique()->all();
- $i_info = Item::TopClear($user, $data)->whereIn('id', $i_ids)->get()->keyBy('id');
- $result = collect($item_summary)->sortBy('month')->transform(function ($v) use ($i_info) {
- $v['item_title'] = $i_info[$v['item_id']]->title ?? "未知项目({$v['item_id']})";
- $v['item_code'] = $i_info[$v['item_id']]->code ?? "-";
- // 将“分”转回“元”
- $v['allocated_salary'] = round($v['allocated_salary'] / 100, 2);
- // 计算天数(按 8 小时/天)
- $v['days'] = round($v['work_minutes'] / 480);
- unset($v['work_minutes']); // 移除中间计算字段
- return $v;
- })->values()->all();
- return [true, $result];
- }
- public function itemDeviceMonthStatistic($data, $user)
- {
- // 1. 基础校验与时间初始化
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) return [false, $month_start];
- // 2. 获取设备工时数据 (项目+设备+月)
- $month_device_list = DailyDwOrderDetails::Clear($user, $data)
- ->where("order_time", ">=", $month_start)
- ->where("order_time", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "item_id",
- "device_id",
- 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();
- // 3. 获取折旧费主表及详情
- $monthly_dd_query = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)
- ->where("month", ">=", $month_start)->where("month", "<", $month_end);
- $monthly_dd_ids = $monthly_dd_query->pluck('id')->toArray();
- $monthly_dd_months = $monthly_dd_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
- ->pluck("month_str", 'id')->toArray();
- $device_salary_details = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_ids)
- ->select("device_id", "depreciation_amount", "main_id")->get()->toArray();
- // 4. 构建高精度折旧池 (单位:分)
- $depreciation_pool = [];
- foreach ($device_salary_details as $val) {
- $m = $monthly_dd_months[$val['main_id']] ?? '';
- if ($m) {
- $key = $val['device_id'] . '_' . $m;
- // 同一个设备在同一个月可能存在多条折旧记录,需累加
- $depreciation_pool[$key] = ($depreciation_pool[$key] ?? 0) + (int)round($val['depreciation_amount'] * 100);
- }
- }
- // 5. 计算全月总工时及初始化余额池
- $device_monthly_total_min = [];
- foreach ($month_device_list as $row) {
- $key = $row['device_id'] . '_' . $row['order_month'];
- $device_monthly_total_min[$key] = ($device_monthly_total_min[$key] ?? 0) + $row['total_work'];
- }
- $rem_depreciation_cents = $depreciation_pool; // 折旧余额池
- $rem_hours_cents = []; // 工时余额池 (小时*100)
- foreach ($device_monthly_total_min as $key => $min) {
- $rem_hours_cents[$key] = (int)round(($min / 60), 2) * 100;
- }
- // 6. 统计项目数用于判断“最后一条”
- $device_count = collect($month_device_list)->groupBy(fn($i) => $i['device_id'].'_'.$i['order_month'])
- ->map(fn($g) => $g->count())->toArray();
- // 7. 预加载设备与项目基础信息
- $items_ids = collect($month_device_list)->pluck('item_id')->unique()->all();
- $device_ids = collect($month_device_list)->pluck('device_id')->unique()->all();
- $item_info = Item::TopClear($user, $data)->whereIn('id', $items_ids)->get()->keyBy('id');
- $device_info = Device::TopClear($user, $data)->whereIn('id', $device_ids)->get()->keyBy('id');
- // 8. 核心转换逻辑
- $item_month_list = collect($month_device_list)->sortBy('order_month')->values()->transform(function ($item) use (
- $item_info, $device_info, $depreciation_pool, $device_monthly_total_min,
- &$device_count, &$rem_depreciation_cents, &$rem_hours_cents
- ) {
- $key = $item['device_id'] . '_' . $item['order_month'];
- $total_min = $device_monthly_total_min[$key] ?? 0;
- $item['item_title'] = $item_info[$item['item_id']]->title ?? "未知项目";
- $item['item_code'] = $item_info[$item['item_id']]->code ?? "-";
- $item['device_title'] = $device_info[$item['device_id']]->title ?? "未知设备";
- $item['device_original'] = $device_info[$item['device_id']]->original_value ?? 0;
- $item['month'] = $item['order_month'];
- $item['total_depreciatio'] = ($depreciation_pool[$key] ?? 0) / 100;
- $item['total_hours'] = round($total_min / 60, 2);
- // 分摊比例计算
- $ratio = $total_min > 0 ? ($item['total_work'] / $total_min) : 0;
- $item['ratio'] = round($ratio, 4);
- if (--$device_count[$key] > 0) {
- // 非最后一条项目
- $cur_hours = round($item['total_work'] / 60, 2);
- $item['hours'] = $cur_hours;
- $rem_hours_cents[$key] -= (int)round($cur_hours * 100);
- $cur_depre = round($item['total_depreciatio'] * $ratio, 2);
- $item['allocated_depreciatio'] = $cur_depre;
- $rem_depreciation_cents[$key] -= (int)round($cur_depre * 100);
- } else {
- // 最后一条项目,余额补平
- $item['hours'] = round(($rem_hours_cents[$key] ?? 0) / 100, 2);
- $item['allocated_depreciatio'] = round(($rem_depreciation_cents[$key] ?? 0) / 100, 2);
- // 比例也做个平账展示
- $item['ratio'] = round($item['hours'] / ($item['total_hours'] ?: 1), 4);
- }
- return $item;
- })->all();
- return [true, $item_month_list];
- }
- //原方法---
- public function employeeMonthSalaryStatistic1($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",DB::raw("(base_salary + performance_salary + bonus + other) as 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'] * 100;
- }
- // 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);
- $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
- $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
- $item = Item::TopClear($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'] * 100;
- } else {
- $item['days'] = $item_day_count[$key];
- $item['allocated_salary'] = round($all_salary[$key] / 100, 2);
- }
- // 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 itemDaySalaryStatistic1($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",DB::raw("(base_salary + performance_salary + bonus + other) as 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'];
- }
- $key = $val['employee_id'] . '_' . $month;
- if (!isset($all_salary[$key])) {
- $all_salary[$key] = 0;
- }
- $all_salary[$key] += $val['salary'] * 100;
- }
- // 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'];
- $key = $item['employee_id'] . '_' . $item['order_month'];
- if (!isset($total_work_min[$key])) $total_work_min[$key] = 0;
- $total_work_min[$key] += $item['total_work'];
- }
- foreach ($total_work_min as $k => $v) {
- $total_work_min[$k] = round($v / 60, 2) * 100;
- }
- $collect = collect($item_month_list);
- $employee_count = $collect->groupBy(function ($item) {
- // 这里的 $item 是集合中的每一行数据
- return $item['employee_id'] . '_' . $item['month'];
- })->map(function ($group) {
- return $group->count();
- })->toArray();
- // $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::TopClear($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::TopClear($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);
- $key = $item['employee_id'] . '_' . $item['month'];
- if (--$employee_count[$key] > 0) {
- $all_salary[$key] -= $item['allocated_salary'] * 100;
- $total_work_min[$key] -= $item['work_hours'] * 100;
- } else {
- $item['allocated_salary'] = round($all_salary[$key] / 100, 2);
- $item['work_hours'] = round($total_work_min[$key] / 100, 2);
- }
- return $item;
- })->all();
- return [true, $item_month_list];
- }
- public function itemDeviceMonthStatistic1($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 = [];
- $device_total_depreciatio = [];
- 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,
- ];
- $key = $item['device_id'] . '_' . $item['order_month'];
- $device_total_depreciatio[$key] = [
- 'total_hours' => round($total_min/60,2)*100,
- 'total_depreciatio' => $total_depreciatio*100
- ];
- }
- // 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::TopClear($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::TopClear($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();
- $collect = collect($item_month_list);
- $device_count = $collect->groupBy(function ($item) {
- // 这里的 $item 是集合中的每一行数据
- return $item['device_id'] . '_' . $item['month'];
- })->map(function ($group) {
- return $group->count();
- })->toArray();
- $rate_list = [];
- $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, &$device_total_depreciatio, &$device_count,&$rate_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']})";
- $key = $item['device_id'] . '_' . $item['month'];
- if(!isset($rate_list[$key])) $rate_list[$key] = 100;
- if (--$device_count[$key] > 0) {
- $rate_list[$key] -= $item['ratio'] * 100;
- $device_total_depreciatio[$key]['total_hours'] -= $item['hours'] * 100;
- $device_total_depreciatio[$key]['total_depreciatio'] -= $item['allocated_depreciatio'] * 100;
- } else {
- $item['ratio'] = round($rate_list[$key] / 100, 2);
- $item['hours'] = round( $device_total_depreciatio[$key]['total_hours']/ 100, 2);
- $item['allocated_depreciatio'] = round( $device_total_depreciatio[$key]['total_depreciatio']/ 100, 2);
- }
- return $item;
- })->all();
- return [true, $item_month_list];
- }
- public function employeeAttendanceMonthStatistic($data, $user)
- {
- // 1. 基础规则验证
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) return [false, $month_start];
- // 2. 确定该区间内涉及的项目
- $item_list = Item::TopClear($user, $data)
- ->where('del_time', 0)
- ->where(function ($query) use ($month_start, $month_end) {
- $query->where('start_time', '>=', $month_start)
- ->where('end_time', '<', $month_end);
- })
- ->select("code", "title", "start_time", "end_time", "id", "state")
- ->orderby("start_time", "asc")
- ->get();
- $item_key_list = $item_list->keyBy('id')->toArray();
- // 3. 获取精准分摊的人员费用(内部已处理精度,返回单位为:分)
- $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
- // 4. 获取精准分摊的折旧费用(内部已处理精度,返回单位为:分)
- $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
- // 5. 获取其他报销费用
- list($item_fee_list, $fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
- // 6. 组合所有数据
- $result = [];
- foreach ($item_key_list as $itemId => $v) {
- // 从分摊结果中取值,注意单位换算回“元”
- $salary_cents = $item_employee_list[$itemId]['salary'] ?? 0;
- $dep_cents = $item_device_list[$itemId]['depreciation'] ?? 0;
- $item_value = [
- "code" => $v['code'],
- "title" => $v['title'],
- "state" => $v['state'] == 3 ? "完结" : "进行中",
- "employee_salary" => round($salary_cents / 100, 2),
- "device_depreciation" => round($dep_cents / 100, 2),
- "expense_type" => "费用化支出",
- "fee_list" => collect($item_fee_list[$itemId] ?? [])->values()->all(),
- ];
- $result[] = $item_value;
- }
- return [true, ["list" => $result, "fee_type_list" => $fee_type_list]];
- }
- private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
- {
- // 获取工时明细(按月、人、项目分组)
- $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
- ->where("order_time", ">=", $month_start)
- ->where("order_time", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "item_id",
- "employee_id",
- 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();
- // 获取工资主表并建立月份映射
- $ps_query = MonthlyPsOrder::Clear($user, $data)
- ->where('del_time', 0)
- ->where("month", ">=", $month_start)
- ->where("month", "<", $month_end);
- $ps_ids = $ps_query->pluck('id')->toArray();
- $month_map = $ps_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
- ->pluck('month_str', 'id')->toArray();
- // 获取工资明细并转为整数“分”
- $salary_details = MonthlyPsOrderDetails::whereIn('main_id', $ps_ids)
- ->select("employee_id", "main_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"))
- ->get();
- $all_salary_pool = []; // 存放 [人员_月份 => 总金额分]
- foreach ($salary_details as $val) {
- $m = $month_map[$val['main_id']] ?? '';
- if ($m) {
- $k = $val['employee_id'] . '_' . $m;
- $all_salary_pool[$k] = (int)round($val['salary'] * 100);
- }
- }
- // 统计每个人每月的总工时和参与的项目数(用于尾差平账)
- $emp_monthly_total_min = [];
- $emp_item_count = [];
- foreach ($month_employee_list as $row) {
- $k = $row['employee_id'] . '_' . $row['order_month'];
- $emp_monthly_total_min[$k] = ($emp_monthly_total_min[$k] ?? 0) + $row['total_work'];
- $emp_item_count[$k] = ($emp_item_count[$k] ?? 0) + 1;
- }
- $rem_salary = $all_salary_pool; // 剩余金额池
- $return_item_list = [];
- foreach ($month_employee_list as $item) {
- $k = $item['employee_id'] . '_' . $item['order_month'];
- $total_s = $all_salary_pool[$k] ?? 0;
- $total_m = $emp_monthly_total_min[$k] ?? 0;
- if ($total_m > 0 && --$emp_item_count[$k] > 0) {
- // 非最后一个项目:按比例计算分值(取整,余额存入池子)
- $allocated = (int)floor(($item['total_work'] / $total_m) * $total_s);
- $rem_salary[$k] -= $allocated;
- } else {
- // 最后一个项目:直接取剩余全部金额
- $allocated = $rem_salary[$k] ?? 0;
- }
- $itemId = $item['item_id'];
- if (!isset($return_item_list[$itemId])) $return_item_list[$itemId]['salary'] = 0;
- $return_item_list[$itemId]['salary'] += $allocated;
- }
- return $return_item_list;
- }
- private function getDeviceItemSalary($month_start, $month_end, $data, $user)
- {
- // 获取设备工时明细
- $month_device_list = DailyDwOrderDetails::Clear($user, $data)
- ->where("order_time", ">=", $month_start)
- ->where("order_time", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "item_id",
- "device_id",
- 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")
- ->get()->toArray();
- // 获取折旧主表
- $dd_query = MonthlyDdOrder::Clear($user, $data)
- ->where('del_time', 0)
- ->where("month", ">=", $month_start)
- ->where("month", "<", $month_end);
- $dd_ids = $dd_query->pluck('id')->toArray();
- $month_map = $dd_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
- ->pluck("month_str", 'id')->toArray();
- // 获取折旧明细并转为整数“分”
- $device_details = MonthlyDdOrderDetails::whereIn('main_id', $dd_ids)
- ->select("device_id", "depreciation_amount", "main_id")
- ->get();
- $all_dep_pool = [];
- foreach ($device_details as $val) {
- $m = $month_map[$val['main_id']] ?? '';
- if ($m) {
- $k = $val['device_id'] . '_' . $m;
- $all_dep_pool[$k] = (int)round($val['depreciation_amount'] * 100);
- }
- }
- // 统计设备每月总工时和项目数
- $dev_monthly_total_min = [];
- $dev_item_count = [];
- foreach ($month_device_list as $row) {
- $k = $row['device_id'] . '_' . $row['order_month'];
- $dev_monthly_total_min[$k] = ($dev_monthly_total_min[$k] ?? 0) + $row['total_work'];
- $dev_item_count[$k] = ($dev_item_count[$k] ?? 0) + 1;
- }
- $rem_dep = $all_dep_pool;
- $return_item_list = [];
- foreach ($month_device_list as $item) {
- $k = $item['device_id'] . '_' . $item['order_month'];
- $total_d = $all_dep_pool[$k] ?? 0;
- $total_m = $dev_monthly_total_min[$k] ?? 0;
- if ($total_m > 0 && --$dev_item_count[$k] > 0) {
- $allocated = (int)floor(($item['total_work'] / $total_m) * $total_d);
- $rem_dep[$k] -= $allocated;
- } else {
- $allocated = $rem_dep[$k] ?? 0;
- }
- $itemId = $item['item_id'];
- if (!isset($return_item_list[$itemId])) $return_item_list[$itemId]['depreciation'] = 0;
- $return_item_list[$itemId]['depreciation'] += $allocated;
- }
- return $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::TopClear($user, $data);
- $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
- return $this->groupListByRoot($expense_list, $fee);
- }
- //原方法-------
- public function employeeAttendanceMonthStatistic1($data, $user)
- {
- //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) return [false, $month_start];
- //第一步确定项目
- $item = Item::TopClear($user, $data);
- $item_list = $item->where('del_time', 0)
- ->where(function ($query) use ($month_start, $month_end) {
- $query->where('start_time', '>=', $month_start)
- ->where('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" => isset($item_employee_list[$v['id']]['salary']) ? round($item_employee_list[$v['id']]['salary']/100,2) : 0,
- "device_depreciation" => isset($item_device_list[$v['id']]['depreciation']) ? round($item_device_list[$v['id']]['depreciation']/100,2): 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 getEmployeeItemSalary1($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", DB::raw("(base_salary + performance_salary + bonus + other) as 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'];
- $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
- }
- }
- // 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'].'_'.$item['order_month'];
- // 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)*100;
- } else {
- $allocated_salary = 0;
- }
- $item_list[] = [
- 'employee_id' => $item['employee_id'],
- 'allocated_salary' => $allocated_salary,
- 'month' => $item['order_month'],
- 'item_id' => $item['item_id'],
- ];
- }
- $collect = collect($item_list);
- $employee_count = $collect->groupBy(function ($item) {
- // 这里的 $item 是集合中的每一行数据
- return $item['employee_id'] . '_' . $item['month'];
- })->map(function ($group) {
- return $group->count();
- })->toArray();
- $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
- // 如果不是最后一条
- $key = $item['employee_id'] . '_' . $item['month'];
- if(!isset($all_salary[$key])) $item['allocated_salary'] = 0;
- else{
- if (--$employee_count[$key] > 0) {
- $all_salary[$key] -= $item['allocated_salary'];
- } else {
- $item['allocated_salary'] = $all_salary[$key];
- }
- }
- return $item;
- })->all();
- $return_item_list = [];
- foreach ($month_employee_list as $v){
- $key = $v['item_id'];
- if(!isset($return_item_list[$key])) $return_item_list[$key]['salary'] = 0;
- $return_item_list[$key]['salary'] += $v['allocated_salary'];
- }
- return $return_item_list;
- }
- private function getDeviceItemSalary1($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 = [];
- $all_depreciatio = [];
- 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'];
- $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
- }
- }
- // 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)*100;
- } else {
- $allocated_salary = 0;
- }
- $item_list[] = [
- 'device_id' => $item['device_id'],
- 'depreciation' => $allocated_salary,
- 'month' => $item['order_month'],
- 'item_id' => $item['item_id'],
- ];
- }
- $collect = collect($item_list);
- $device_count = $collect->groupBy(function ($item) {
- // 这里的 $item 是集合中的每一行数据
- return $item['device_id'] . '_' . $item['month'];
- })->map(function ($group) {
- return $group->count();
- })->toArray();
- $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
- // 如果不是最后一条
- $key = $item['device_id'] . '_' . $item['month'];
- if (--$device_count[$key] > 0) {
- $all_depreciatio[$key] -= $item['depreciation'];
- } else {
- $item['depreciation'] = $all_depreciatio[$key];
- }
- return $item;
- })->all();
- $return_item_list = [];
- foreach ($month_employee_list as $v){
- $key = $v['item_id'];
- if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
- $return_item_list[$key]['depreciation'] += $v['depreciation'];
- }
- // var_dump($return_item_list);die;
- return $return_item_list;
- }
- /**
- * 将报销明细按照一级费用和项目分类进行分组
- * * @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'],
- 'is_other' => $type['is_other']
- ];
- }
- // 3. 遍历明细数据进行分组
- $item_key_list = [];
- $type_list = [];
- foreach ($list as $item) {
- $feeId = $item['fee_id'];
- // 获取该费用对应的一级分类信息
- if (!isset($childToRoot[$feeId])) continue;
- $rootId = $childToRoot[$feeId]['id'];
- $is_other = $childToRoot[$feeId]['is_other'];
- $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, //委外
- 'is_other' => $is_other //是否其他费用
- ];
- }
- if ($item['entrust_type'] == 1) {
- $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount']*100;
- } elseif ($item['entrust_type'] == 2) {
- $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount']*100;
- }
- $item_key_list[$key][$rootId]['total_amount'] += $item['amount']*100;
- //这边需要拿到头部所有的一级费用类型
- if (!isset($type_list[$rootId])) {
- $type_list[$rootId] = [
- 'sort' => $sort,
- 'id' => $rootId,
- 'title' => $title,
- ];
- }
- }
- // var_dump($item_key_list);die;
- foreach ($item_key_list as $k=>$v){
- foreach ($v as $kk=>$vv){
- $item_key_list[$k][$kk]['entrust1_amount'] = round( $vv['entrust1_amount']/100,2);
- $item_key_list[$k][$kk]['entrust2_amount'] = round( $vv['entrust2_amount']/100,2);
- $item_key_list[$k][$kk]['total_amount'] = round( $vv['total_amount']/100,2);
- }
- }
- // $item_key_list = collect($item_key_list)->transform(function ($item) {
- // $item['entrust1_amount'] = round( $item['entrust1_amount']/100,2);
- // $item['entrust2_amount'] = round( $item['entrust2_amount']/100,2);
- // $item['total_amount'] = round( $item['total_amount']/100,2);
- // return $item;
- // })->all();
- // 使用 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];
- // 1. 获取项目列表
- $item_list = Item::TopClear($user, $data)->where('del_time', 0)
- ->where(function ($query) use ($month_start, $month_end) {
- $query->where('start_time', '>=', $month_start)->where('end_time', '<', $month_end);
- })->select("code", "title", "id", "state")->orderby("start_time", "asc")->get()->keyBy('id')->toArray();
- // 2. 获取分摊的人工和折旧 (此时结果内部已是“分”)
- $item_salary = $this->auxiliaryEmployee($user, $data, $month_start, $month_end);
- $device_depreciation = $this->auxiliaryDevice($user, $data, $month_start, $month_end);
- // 3. 处理凭证费用
- $fee = Fee::TopClear($user, $data)->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
- $auxiliary_list = AuxiliaryAccountDetails::Clear($user, $data)
- ->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
- ->where('del_time', 0)->get()->toArray();
- list($fee_amount, $fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list, $fee);
- $return = [];
- foreach ($fee_amount as $v) {
- $v_month = date("Y-m", $v['voucher_date']);
- // 类型 1(人工) 或 2(折旧) 需要遍历项目进行分摊显示
- if ($v['type'] == 1 || $v['type'] == 2) {
- foreach ($item_list as $itemId => $vv) {
- $search_key = $itemId . "_" . $v_month;
- // 只有当该项目在该月有实际分摊金额时才产生记录
- $allocated_amount = 0;
- if ($v['type'] == 1) {
- $allocated_amount = $item_salary[$search_key]['allocated_salary'] ?? 0;
- } else {
- $allocated_amount = $device_depreciation[$search_key]['depreciation'] ?? 0;
- }
- if ($allocated_amount <= 0) continue;
- $return[] = [
- "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" => round($allocated_amount / 100, 2), // 转回元
- "fee_id" => $v['fee_id'],
- "entrust1_amount" => $v['entrust1_amount'],
- "entrust2_amount" => $v['entrust2_amount'],
- "type" => $v['type'],
- ];
- }
- } else {
- // 普通凭证费用
- if (!isset($item_list[$v['item_id']])) continue;
- $item_value = $item_list[$v['item_id']];
- $return[] = [
- "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 [true, [
- 'fee_type_list' => $fee_type_list,
- 'list' => $return,
- ]];
- }
- 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",DB::raw("(base_salary + performance_salary + bonus + other) as 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'];
- $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
- }
- }
- // 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 = [];
- $item_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)*100;
- } else {
- $allocated_salary = 0;
- }
- $item_list[] = [
- 'employee_id' => $item['employee_id'],
- 'allocated_salary' => $allocated_salary,
- 'month' => $item['order_month'],
- 'item_id' => $item['item_id'],
- ];
- }
- $collect = collect($item_list);
- $employee_count = $collect->groupBy(function ($item) {
- // 这里的 $item 是集合中的每一行数据
- return $item['employee_id'] . '_' . $item['month'];
- })->map(function ($group) {
- return $group->count();
- })->toArray();
- $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
- // 如果不是最后一条
- $key = $item['employee_id'] . '_' . $item['month'];
- if(!isset($all_salary[$key])) $item['allocated_salary'] = 0;
- else {
- if (--$employee_count[$key] > 0) {
- $all_salary[$key] -= $item['allocated_salary'];
- } else {
- $item['allocated_salary'] = $all_salary[$key];
- }
- }
- return $item;
- })->all();
- $return_item_list = [];
- foreach ($month_employee_list as $v){
- $key = $v['item_id'] . '_' . $v['month'];
- if(!isset($return_item_list[$key])) $return_item_list[$key]['allocated_salary'] = 0;
- $return_item_list[$key]['allocated_salary'] += $v['allocated_salary'];
- }
- return $return_item_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 = [];
- $all_depreciatio = [];
- 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'];
- $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
- }
- }
- // 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 = [];
- $item_list = [];
- foreach ($month_device_list as $item) {
- $key = $item['device_id'] . '_' . $item['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)*100;
- } else {
- $allocated_salary = 0;
- }
- // $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
- $item_list[] = [
- 'device_id' => $item['device_id'],
- 'depreciation' => $allocated_salary,
- 'month' => $item['order_month'],
- 'item_id' => $item['item_id'],
- ];
- }
- $collect = collect($item_list);
- $device_count = $collect->groupBy(function ($item) {
- // 这里的 $item 是集合中的每一行数据
- return $item['device_id'] . '_' . $item['month'];
- })->map(function ($group) {
- return $group->count();
- })->toArray();
- $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
- // 如果不是最后一条
- $key = $item['device_id'] . '_' . $item['month'];
- if (--$device_count[$key] > 0) {
- $all_depreciatio[$key] -= $item['depreciation'];
- } else {
- $item['depreciation'] = $all_depreciatio[$key];
- }
- return $item;
- })->all();
- $return_item_list = [];
- foreach ($month_employee_list as $v){
- $key = $v['item_id']. '_' . $v['month'];
- if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
- $return_item_list[$key]['depreciation'] += $v['depreciation'];
- }
- // dd($return_item_list);
- return $return_item_list;
- }
- //原方法----
- public function auxiliaryStatistic1($data, $user)
- {
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) return [false, $month_start];
- //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
- //确认所有项目
- $item = Item::TopClear($user, $data);
- $item_list = $item->where('del_time', 0)
- ->where(function ($query) use ($month_start, $month_end) {
- $query->where('start_time', '>=', $month_start)
- ->where('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::TopClear($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) {
- // var_dump();
- foreach ($item_key_list as $vv) {
- if(!isset($item_salary[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_salary'])) continue;
- $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 ?round( $item_salary[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_salary']/100,2) : (isset($device_depreciation[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['depreciation']) ?round($device_depreciation[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['depreciation']/100,2) : 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,
- ]];
- }
- public function auxiliaryEmployee1($user, $data, $month_start, $month_end)
- {
- // 1. 获取工时明细
- $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
- ->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
- ->where('del_time', 0)
- ->select("item_id", "employee_id", 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();
- // 2. 获取工资并转为“分”
- $monthly_ps_order_query = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)
- ->where("month", ">=", $month_start)->where("month", "<", $month_end);
- $monthly_ps_order_ids = $monthly_ps_order_query->pluck('id')->toArray();
- $month_map = $monthly_ps_order_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
- ->pluck('month_str', 'id')->toArray();
- $salary_details = MonthlyPsOrderDetails::whereIn('main_id', $monthly_ps_order_ids)
- ->select("employee_id", "main_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"))
- ->get();
- $salary_pool = []; // [emp_month => salary_cents]
- foreach ($salary_details as $val) {
- $m = $month_map[$val['main_id']] ?? '';
- if ($m) {
- $k = $val['employee_id'] . '_' . $m;
- $salary_pool[$k] = (int)round($val['salary'] * 100);
- }
- }
- // 3. 计算个人月总工时及项目计数
- $emp_total_min = [];
- $emp_item_count = [];
- foreach ($month_employee_list as $row) {
- $k = $row['employee_id'] . '_' . $row['order_month'];
- $emp_total_min[$k] = ($emp_total_min[$k] ?? 0) + $row['total_work'];
- $emp_item_count[$k] = ($emp_item_count[$k] ?? 0) + 1;
- }
- // 4. 分摊(保留整数分)
- $rem_salary = $salary_pool;
- $return_item_list = [];
- foreach ($month_employee_list as $item) {
- $k = $item['employee_id'] . '_' . $item['order_month'];
- $total_s = $salary_pool[$k] ?? 0;
- $total_m = $emp_total_min[$k] ?? 0;
- if ($total_m > 0 && --$emp_item_count[$k] > 0) {
- // 非最后一个项目,取地板值避免超支
- $allocated = (int)floor(($item['total_work'] / $total_m) * $total_s);
- $rem_salary[$k] -= $allocated;
- } else {
- // 最后一个项目,取池子剩余所有钱
- $allocated = $rem_salary[$k] ?? 0;
- }
- $res_key = $item['item_id'] . '_' . $item['order_month'];
- $return_item_list[$res_key]['allocated_salary'] = ($return_item_list[$res_key]['allocated_salary'] ?? 0) + $allocated;
- }
- return $return_item_list;
- }
- public function auxiliaryDevice1($user, $data, $month_start, $month_end)
- {
- // 1. 获取设备工时
- $month_device_list = DailyDwOrderDetails::Clear($user, $data)
- ->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)->where('del_time', 0)
- ->select("item_id", "device_id", 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")->get()->toArray();
- // 2. 获取折旧费转为“分”
- $monthly_dd_query = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)
- ->where("month", ">=", $month_start)->where("month", "<", $month_end);
- $monthly_dd_ids = $monthly_dd_query->pluck('id')->toArray();
- $month_map = $monthly_dd_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
- ->pluck("month_str", 'id')->toArray();
- $device_details = MonthlyDdOrderDetails::whereIn('main_id', $monthly_dd_ids)
- ->select("device_id", "depreciation_amount", "main_id")->get();
- $dep_pool = [];
- foreach ($device_details as $val) {
- $m = $month_map[$val['main_id']] ?? '';
- if ($m) {
- $k = $val['device_id'] . '_' . $m;
- $dep_pool[$k] = (int)round($val['depreciation_amount'] * 100);
- }
- }
- // 3. 统计设备月总工时及项目数
- $dev_total_min = [];
- $dev_item_count = [];
- foreach ($month_device_list as $row) {
- $k = $row['device_id'] . '_' . $row['order_month'];
- $dev_total_min[$k] = ($dev_total_min[$k] ?? 0) + $row['total_work'];
- $dev_item_count[$k] = ($dev_item_count[$k] ?? 0) + 1;
- }
- // 4. 分摊
- $rem_dep = $dep_pool;
- $return_item_list = [];
- foreach ($month_device_list as $item) {
- $k = $item['device_id'] . '_' . $item['order_month'];
- $total_d = $dep_pool[$k] ?? 0;
- $total_m = $dev_total_min[$k] ?? 0;
- if ($total_m > 0 && --$dev_item_count[$k] > 0) {
- $allocated = (int)floor(($item['total_work'] / $total_m) * $total_d);
- $rem_dep[$k] -= $allocated;
- } else {
- $allocated = $rem_dep[$k] ?? 0;
- }
- $res_key = $item['item_id'] . '_' . $item['order_month'];
- $return_item_list[$res_key]['depreciation'] = ($return_item_list[$res_key]['depreciation'] ?? 0) + $allocated;
- }
- return $return_item_list;
- }
- /**
- * 将报销明细按照一级费用和项目分类进行分组
- * * @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 itemEmployeeSalaryStatistic($data, $user)
- {
- list($status, $month_start, $month_end) = $this->commonRule($data);
- if (!$status) return [false, $month_start];
- // 1. 获取人员工时记录
- $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
- ->where("order_time", ">=", $month_start)
- ->where("order_time", "<", $month_end)
- ->where('del_time', 0)
- ->select(
- "item_id",
- "employee_id",
- 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();
- // 2. 获取工资及配置
- $monthly_ps_order_query = MonthlyPsOrder::Clear($user, $data)
- ->where('del_time', 0)
- ->where("month", ">=", $month_start)
- ->where("month", "<", $month_end);
- $monthly_ps_order_ids = $monthly_ps_order_query->pluck('id')->toArray();
- $monthly_ps_order_key_list = $monthly_ps_order_query
- ->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();
- // 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" => (int)round($val['salary'] * 100),
- "social_insurance" => (int)round($val['social_insurance'] * 100),
- "public_housing_fund" => (int)round($val['public_housing_fund'] * 100),
- ];
- }
- }
- // 4. 计算月总工时 (分钟) 及 余额池初始化 (小时*100)
- $employee_monthly_total_min = [];
- foreach ($month_employee_list as $row) {
- $key = $row['employee_id'] . '_' . $row['order_month'];
- $employee_monthly_total_min[$key] = ($employee_monthly_total_min[$key] ?? 0) + $row['total_work'];
- }
- $all_min = [];
- foreach ($employee_monthly_total_min as $key => $min) {
- // 重要修复:以总分钟数计算出全月总小时数,作为余额池的起点
- $all_min[$key] = (int)round($min / 60, 2) * 100;
- }
- // 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'];
- $total_min = $employee_monthly_total_min[$key] ?? 0;
- $ratio = $total_min > 0 ? ($item['total_work'] / $total_min) : 0;
- $item_month_list[$item_key] = [
- "month" => $item['order_month'],
- "employee_id" => $item['employee_id'],
- "item_id" => $item['item_id'],
- "salary" => $salary_map[$key]['salary'] ?? 0,
- "social_insurance" => $salary_map[$key]['social_insurance'] ?? 0,
- "public_housing_fund" => $salary_map[$key]['public_housing_fund'] ?? 0,
- "radio" => round($ratio, 4),
- "total_min_raw" => $total_min, // 保留原始分钟,不在这里修改
- "work_min_raw" => $item['total_work'],
- ];
- }
- // 6. 获取项目和人员信息
- $items = collect($item_month_list)->pluck('item_id')->unique()->all();
- $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->all();
- $employee_key_list = Employee::TopClear($user, $data)->wherein('id', $employee_ids)
- ->select("major", "title", "id")->get()->keyBy('id')->toArray();
- $item_info = Item::TopClear($user, $data)->wherein('id', $items)
- ->select("title", "code", "id")->get()->keyBy('id')->toArray();
- // 7. 统计每人项目数用于平账
- $employee_count = collect($item_month_list)->groupBy(fn($i) => $i['employee_id'].'_'.$i['month'])
- ->map(fn($g) => $g->count())->toArray();
- // 8. 核心转换
- $result = collect($item_month_list)->sortBy('month')->values()->transform(function ($item) use ($item_info, $employee_key_list, &$employee_count, &$all_salary, &$all_min) {
- $key = $item['employee_id'] . '_' . $item['month'];
- $item['item_title'] = $item_info[$item['item_id']]['title'] ?? "未知项目";
- $item['item_code'] = $item_info[$item['item_id']]['code'] ?? "-";
- $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员";
- $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "-";
- // 应出勤工时:固定展示,不参与递减
- $item['total_min'] = round($item['total_min_raw'] / 60, 2);
- if (isset($employee_count[$key]) && --$employee_count[$key] > 0) {
- // 非最后一条项目
- $work_h = round($item['work_min_raw'] / 60, 2);
- $item['work_minutes'] = $work_h;
- $all_min[$key] -= ($work_h * 100);
- // 分摊工资
- $ws = round($item['salary'] * ($item['work_min_raw'] / $item['total_min_raw']), 2);
- $item['work_salary'] = $ws;
- if (isset($all_salary[$key])) $all_salary[$key]['salary'] -= ($ws * 100);
- // 分摊社保
- $wsi = round($item['social_insurance'] * ($item['work_min_raw'] / $item['total_min_raw']), 2);
- $item['work_social_insurance'] = $wsi;
- if (isset($all_salary[$key])) $all_salary[$key]['social_insurance'] -= ($wsi * 100);
- // 分摊公积金
- $wphf = round($item['public_housing_fund'] * ($item['work_min_raw'] / $item['total_min_raw']), 2);
- $item['work_public_housing_fund'] = $wphf;
- if (isset($all_salary[$key])) $all_salary[$key]['public_housing_fund'] -= ($wphf * 100);
- } else {
- // 最后一条,从余额池取值(解决精度)
- $item['work_salary'] = isset($all_salary[$key]) ? round($all_salary[$key]['salary'] / 100, 2) : 0;
- $item['work_social_insurance'] = isset($all_salary[$key]) ? round($all_salary[$key]['social_insurance'] / 100, 2) : 0;
- $item['work_public_housing_fund'] = isset($all_salary[$key]) ? round($all_salary[$key]['public_housing_fund'] / 100, 2) : 0;
- $item['work_minutes'] = isset($all_min[$key]) ? round($all_min[$key] / 100, 2) : 0;
- }
- unset($item['total_min_raw'], $item['work_min_raw']);
- return $item;
- })->all();
- return [true, $result];
- }
- public function itemEmployeeSalaryStatistic1($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",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id", "social_insurance", "public_housing_fund")
- ->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;
- $all_salary[$val['employee_id'] . '_' . $month] = [
- "salary" => $val['salary']*100,
- "social_insurance" => $val['social_insurance']*100,
- "public_housing_fund" => $val['public_housing_fund']*100,
- ];
- }
- }
- // var_dump($salary_map);die;
- // 2. 计算每个员工在每个月的全月总工时
- $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])) {
- $employee_monthly_total_min[$key] = 0;
- $all_min[$key] = 0;
- }
- $employee_monthly_total_min[$key] += $row['total_work'];
- $all_min[$key] = round($row['total_work']/60,2)*100;
- }
- // 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::TopClear($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::TopClear($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);
- $employee_count = $collect->groupBy(function ($item) {
- // 这里的 $item 是集合中的每一行数据
- return $item['employee_id'] . '_' . $item['month'];
- })->map(function ($group) {
- return $group->count();
- })->toArray();
- //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
- $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, $employee_key_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_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);
- $key = $item['employee_id'] . '_' . $item['month'];
- 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 {
- $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){
- $model = Item::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->select(Item::$report_field_1)
- ->orderby('id', 'desc');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['state'])) $model->where('state', $data['state']);
- $list = $model->get()->toArray();
- $list = $this->fillEnterpriseRdStatistic($list, $data, $user);
- return [true, $list];
- }
- private function fillEnterpriseRdStatistic($list, $data, $user){
- //项目实际支出 项目费用单
- $expense = ExpenseClaimsDetails::Clear($user, $data);
- $expense_map = $expense->where('del_time', 0)
- ->whereIn('item_id', array_unique(array_column($list,'id')))
- ->selectRaw('item_id, SUM(amount) as total_amount')
- ->groupBy('item_id')
- ->pluck('total_amount', 'item_id') // 这一步直接生成 item_id => total_amount 结构
- ->toArray();
- foreach ($list as $key => $value){
- $list[$key]['actual_expenditure'] = $expense_map[$value['id']] ?? 0;
- $start_time = $value['start_time'] ? date('Y-m-d', $value['start_time']) : '';
- $end_time = $value['end_time'] ? date('Y-m-d', $value['end_time']) : '';
- $list[$key]['time_range'] = $start_time . ' ' . $end_time;
- }
- return $list;
- }
- public function enterpriseRdManStatistic($data, $user){
- $model = Employee::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->where('is_admin', '<=', Employee::IS_ADMIN_ONE)
- ->select(Employee::$report_field)
- ->orderBy('id','desc');
- if(! empty($data['id_card'])) $model->where('id_card', 'LIKE', '%'.$data['id_card'].'%');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['employee_type'])) $model->where('employee_type', $data['employee_type']);
- if(isset($data['education'])) $model->where('education', $data['education']);
- $list = $model->get()->toArray();
- $list = $this->fillEnterpriseRdManStatistic($list, $data, $user);
- return [true, $list];
- }
- private function fillEnterpriseRdManStatistic($list, $data, $user){
- $man = EmployeeDepartPermission::from('employee_depart_permission as a')
- ->join('depart as b', 'b.id', '=', 'a.depart_id')
- ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
- ->select('a.employee_id', 'b.title')
- ->get()->toArray();
- $man_map = [];
- foreach ($man as $value){
- if(isset($man_map[$value['employee_id']])){
- $man_map[$value['employee_id']] .= ',' . $value['title'];
- }else{
- $man_map[$value['employee_id']] = $value['title'];
- }
- }
- foreach ($list as $key => $value){
- $depart_title = $man_map[$value['id']] ?? "";
- $list[$key]['position_new'] = $depart_title . '/' . $value['position'];
- $list[$key]['employee_type_title'] = Employee::E_State_Type[$value['employee_type']] ?? '';
- $list[$key]['education'] = Employee::Education[$value['education']] ?? '';
- }
- return $list;
- }
- public function enterpriseRdItemStatistic($data, $user){
- // 1. 先声明表名和别名
- $model = ItemDetails::from('item_details as i');
- // 2. 再调用 TopClear
- $model = $model->TopClear($user, $data);
- $model = $model->from('item_details as i')
- ->where('i.del_time', 0)
- ->where('i.type', ItemDetails::type_one);
- $model = $model->leftJoin('employee as e', 'i.data_id', '=', 'e.id');
- $fields = ['e.id', 'e.title', 'e.education', 'e.major','e.p_title','i.item_id'];
- $list = $model->select($fields)
- ->orderBy('i.id', 'desc')
- ->get()
- ->toArray();
- $list = $this->fillEnterpriseRdItemStatistic($list, $data, $user);
- return [true, $list];
- }
- private function fillEnterpriseRdItemStatistic($list, $data, $user){
- $man = EmployeeDepartPermission::from('employee_depart_permission as a')
- ->join('depart as b', 'b.id', '=', 'a.depart_id')
- ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
- ->select('a.employee_id', 'b.title')
- ->get()->toArray();
- $man_map = [];
- foreach ($man as $value){
- if(isset($man_map[$value['employee_id']])){
- $man_map[$value['employee_id']] .= ',' . $value['title'];
- }else{
- $man_map[$value['employee_id']] = $value['title'];
- }
- }
- $item_map = Item::whereIn('id',array_unique(array_column($list,'item_id')))
- ->pluck('title','id')
- ->toArray();
- foreach ($list as $key => $value){
- $depart_title = $man_map[$value['id']] ?? "";
- $list[$key]['depart_title'] = $depart_title;
- $item_title = $item_map[$value['item_id']] ?? "";
- $list[$key]['item_title'] = $item_title;
- $list[$key]['education'] = Employee::Education[$value['education']] ?? '';
- }
- return $list;
- }
- }
|