StatisticService.php 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  1. <?php
  2. namespace App\Service;
  3. use App\Model\AuxiliaryAccountDetails;
  4. use App\Model\DailyDwOrderDetails;
  5. use App\Model\DailyPwOrderDetails;
  6. use App\Model\Device;
  7. use App\Model\Employee;
  8. use App\Model\EmployeeDepartPermission;
  9. use App\Model\ExpenseClaimsDetails;
  10. use App\Model\Fee;
  11. use App\Model\Item;
  12. use App\Model\ItemDetails;
  13. use App\Model\MonthlyDdOrder;
  14. use App\Model\MonthlyDdOrderDetails;
  15. use App\Model\MonthlyPsOrder;
  16. use App\Model\MonthlyPsOrderDetails;
  17. use App\Service\Statistic\StatisticCommonService;
  18. use Illuminate\Support\Facades\DB;
  19. class StatisticService extends StatisticCommonService
  20. {
  21. public function employeeDayHourStatistic($data, $user)
  22. {
  23. //传参月份、项目编码、项目名称 不允许跨年查询月份
  24. //项目编码、项目名称、人员名称、工时、日期
  25. //传参月份、项目编码、项目名称 不允许跨年查询月份
  26. //项目编码、项目名称、人员名称、工时、日期
  27. list($status, $month_start, $month_end) = $this->commonRule($data);
  28. if (!$status) return [false, $month_start];
  29. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  30. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  31. ->where('del_time', 0)
  32. ->select(
  33. "item_id",
  34. "employee_id",
  35. // 将时间戳转为 Y-m-d 格式并起别名
  36. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m-%d') as order_date"),
  37. // 聚合求和
  38. DB::raw("SUM(total_work_min) as total_work")
  39. )
  40. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m-%d')"), "item_id", "employee_id")
  41. ->orderby("order_date", "asc")->get()->toArray();
  42. // $month_employee_list = $this->limit($month_employee_list, ['*'], $data);
  43. $dataCollection = collect($month_employee_list);
  44. $item_ids = $dataCollection->pluck('item_id')->unique()->values()->all();
  45. $employee_ids = $dataCollection->pluck('employee_id')->unique()->values()->all();
  46. $employee = Employee::TopClear($user, $data);
  47. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  48. $item = Item::TopClear($user, $data);
  49. $item_title_key_list = $item->wherein('id', $item_ids)->pluck("title", "id")->toArray();
  50. $item_code_key_list = $item->wherein('id', $item_ids)->pluck("code", "id")->toArray();
  51. $collect = collect($month_employee_list);
  52. $employee_count = $collect->groupBy(function ($item) {
  53. // 这里的 $item 是集合中的每一行数据
  54. return $item['employee_id'] . '_' . $item['order_date'];
  55. })->map(function ($group) {
  56. return $group->count();
  57. })->toArray();
  58. $employee_work_count = $collect->groupBy(fn($item) => $item['employee_id'] . '_' . $item['order_date'])
  59. ->map(function ($group) {
  60. // 1. 算出小时并保留两位小数(例如 26.08)
  61. $hours = round($group->sum('total_work') / 60, 2);
  62. // 2. 乘以 100 并强转为整数(例如 2608)
  63. // 使用 round 是为了再次修正 26.08 * 100 可能产生的 2607.9999 误差
  64. return (int)round($hours * 100);
  65. })
  66. ->toArray();
  67. $collection = collect($month_employee_list);
  68. // 注意:这里使用 transform,参数名建议写准确
  69. // 关键点:use 后面加了 & 符号
  70. $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) {
  71. $item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
  72. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  73. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  74. // 如果不是最后一条
  75. $key = $item['employee_id'] . '_' . $item['order_date'];
  76. if (--$employee_count[$key] > 0) {
  77. $current_hours = round($item['total_work'] / 60, 2);
  78. $item['total_work_hours'] = $current_hours;
  79. // 关键:递减外部的 $absolute_total_hours
  80. $employee_work_count[$key] -= $current_hours * 100;
  81. } else {
  82. $item['total_work_hours'] = round($employee_work_count[$key] / 100, 2);
  83. }
  84. return $item;
  85. })->all();
  86. return [true, $month_employee_list];
  87. }
  88. public function itemDaySalaryStatistic($data, $user)
  89. {
  90. list($status, $month_start, $month_end) = $this->commonRule($data);
  91. if (!$status) return [false, $month_start];
  92. // 1. 获取工时 (复用逻辑)
  93. $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
  94. ->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)->where('del_time', 0)
  95. ->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"))
  96. ->groupBy("order_month", "item_id", "employee_id")->get();
  97. // 2. 获取工资 (复用逻辑)
  98. $ps_query = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end);
  99. $monthly_ps_order_ids = $ps_query->pluck('id')->toArray();
  100. $month_map = $ps_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))->pluck('month_str', 'id')->toArray();
  101. $salary_details = MonthlyPsOrderDetails::whereIn('main_id', $monthly_ps_order_ids)
  102. ->select("employee_id", "main_id", DB::raw("(base_salary + performance_salary + bonus + other) as s_orig"))->get();
  103. $salary_map = [];
  104. foreach ($salary_details as $val) {
  105. $m = $month_map[$val['main_id']] ?? '';
  106. if ($m) {
  107. $k = $val['employee_id'] . '_' . $m;
  108. $salary_map[$k] = ($salary_map[$k] ?? 0) + (int)round($val['s_orig'] * 100);
  109. }
  110. }
  111. // 3. 统计月总工时与项目数
  112. $emp_total_min = [];
  113. $emp_item_count = [];
  114. foreach ($month_employee_list as $row) {
  115. $k = $row->employee_id . '_' . $row->order_month;
  116. $emp_total_min[$k] = ($emp_total_min[$k] ?? 0) + $row->total_work;
  117. $emp_item_count[$k] = ($emp_item_count[$k] ?? 0) + 1;
  118. }
  119. // 4. 分摊并生成明细
  120. $rem_salary_pool = $salary_map;
  121. $detail_list = [];
  122. foreach ($month_employee_list as $item) {
  123. $k = $item->employee_id . '_' . $item->order_month;
  124. $total_s = $salary_map[$k] ?? 0;
  125. $total_m = $emp_total_min[$k] ?? 0;
  126. if ($total_m > 0 && --$emp_item_count[$k] > 0) {
  127. $allocated = (int)floor(($item->total_work / $total_m) * $total_s);
  128. $rem_salary_pool[$k] -= $allocated;
  129. } else {
  130. $allocated = $rem_salary_pool[$k] ?? 0;
  131. }
  132. $detail_list[] = [
  133. 'month' => $item->order_month,
  134. 'item_id' => $item->item_id,
  135. 'employee_id' => $item->employee_id,
  136. 'allocated_salary' => round($allocated / 100, 2),
  137. 'work_hours' => round($item->total_work / 60, 2),
  138. 'total_hours' => round($total_m / 60, 2),
  139. 'total_salary' => round($total_s / 100, 2)
  140. ];
  141. }
  142. // 5. 补充名称信息
  143. $i_ids = collect($detail_list)->pluck('item_id')->unique()->all();
  144. $e_ids = collect($detail_list)->pluck('employee_id')->unique()->all();
  145. $i_info = Item::TopClear($user, $data)->whereIn('id', $i_ids)->get()->keyBy('id');
  146. $e_info = Employee::TopClear($user, $data)->whereIn('id', $e_ids)->get()->keyBy('id');
  147. $result = collect($detail_list)->sortBy('month')->transform(function ($v) use ($i_info, $e_info) {
  148. $v['item_title'] = $i_info[$v['item_id']]->title ?? "未知({$v['item_id']})";
  149. $v['item_code'] = $i_info[$v['item_id']]->code ?? "-";
  150. $v['employee_title'] = $e_info[$v['employee_id']]->title ?? "未知({$v['employee_id']})";
  151. return $v;
  152. })->values()->all();
  153. return [true, $result];
  154. }
  155. public function employeeMonthSalaryStatistic($data, $user)
  156. {
  157. // 1. 基础规则验证
  158. list($status, $month_start, $month_end) = $this->commonRule($data);
  159. if (!$status) return [false, $month_start];
  160. // 2. 获取所有工时明细(按月、项目、人聚合)
  161. $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
  162. ->where("order_time", ">=", $month_start)
  163. ->where("order_time", "<", $month_end)
  164. ->where('del_time', 0)
  165. ->select(
  166. "item_id",
  167. "employee_id",
  168. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  169. DB::raw("SUM(total_work_min) as total_work")
  170. )
  171. ->groupBy("order_month", "item_id", "employee_id")
  172. ->get();
  173. if ($month_employee_list->isEmpty()) {
  174. return [true, []];
  175. }
  176. // 3. 获取工资数据
  177. $ps_query = MonthlyPsOrder::Clear($user, $data)
  178. ->where('del_time', 0)
  179. ->where("month", ">=", $month_start)
  180. ->where("month", "<", $month_end);
  181. $monthly_ps_order_ids = $ps_query->pluck('id')->toArray();
  182. $month_map = $ps_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  183. ->pluck('month_str', 'id')->toArray();
  184. $salary_details = MonthlyPsOrderDetails::whereIn('main_id', $monthly_ps_order_ids)
  185. ->select("employee_id", "main_id", DB::raw("(base_salary + performance_salary + bonus + other) as s_orig"))
  186. ->get();
  187. // 4. 建立工资映射(统一使用分单位,避免浮点误差)
  188. $salary_map = [];
  189. foreach ($salary_details as $val) {
  190. $m = $month_map[$val['main_id']] ?? '';
  191. if ($m) {
  192. $k = $val['employee_id'] . '_' . $m;
  193. // 采用与 itemDaySalaryStatistic 一致的整数处理
  194. $salary_map[$k] = ($salary_map[$k] ?? 0) + (int)round($val['s_orig'] * 100);
  195. }
  196. }
  197. // 5. 统计每人每月总工时和参与的项目数(用于精准分摊尾差)
  198. $emp_total_min = [];
  199. $emp_item_count = [];
  200. foreach ($month_employee_list as $row) {
  201. $k = $row->employee_id . '_' . $row->order_month;
  202. $emp_total_min[$k] = ($emp_total_min[$k] ?? 0) + $row->total_work;
  203. $emp_item_count[$k] = ($emp_item_count[$k] ?? 0) + 1;
  204. }
  205. // 6. 执行分摊逻辑并按项目汇总
  206. $item_summary = []; // 存放项目维度的汇总结果
  207. $rem_salary_pool = $salary_map; // 余额池,按人+月控制
  208. foreach ($month_employee_list as $item) {
  209. $k = $item->employee_id . '_' . $item->order_month;
  210. $total_s = $salary_map[$k] ?? 0;
  211. $total_m = $emp_total_min[$k] ?? 0;
  212. // 计算当前员工在该项目上的分摊金额(单位:分)
  213. if ($total_m > 0 && --$emp_item_count[$k] > 0) {
  214. // 非该员工的最后一个项目:计算分摊
  215. $allocated = (int)floor(($item->total_work / $total_m) * $total_s);
  216. $rem_salary_pool[$k] -= $allocated;
  217. } else {
  218. // 该员工的最后一个项目:取余额清零
  219. $allocated = $rem_salary_pool[$k] ?? 0;
  220. }
  221. // 按项目+月份进行累加
  222. $item_key = $item->order_month . '_' . $item->item_id;
  223. if (!isset($item_summary[$item_key])) {
  224. $item_summary[$item_key] = [
  225. 'month' => $item->order_month,
  226. 'item_id' => $item->item_id,
  227. 'allocated_salary' => 0,
  228. 'work_minutes' => 0,
  229. ];
  230. }
  231. $item_summary[$item_key]['allocated_salary'] += $allocated;
  232. $item_summary[$item_key]['work_minutes'] += $item['total_work'];
  233. }
  234. // 7. 获取项目信息并进行最终格式化
  235. $i_ids = collect($item_summary)->pluck('item_id')->unique()->all();
  236. $i_info = Item::TopClear($user, $data)->whereIn('id', $i_ids)->get()->keyBy('id');
  237. $result = collect($item_summary)->sortBy('month')->transform(function ($v) use ($i_info) {
  238. $v['item_title'] = $i_info[$v['item_id']]->title ?? "未知项目({$v['item_id']})";
  239. $v['item_code'] = $i_info[$v['item_id']]->code ?? "-";
  240. // 将“分”转回“元”
  241. $v['allocated_salary'] = round($v['allocated_salary'] / 100, 2);
  242. // 计算天数(按 8 小时/天)
  243. $v['days'] = round($v['work_minutes'] / 480);
  244. unset($v['work_minutes']); // 移除中间计算字段
  245. return $v;
  246. })->values()->all();
  247. return [true, $result];
  248. }
  249. public function itemDeviceMonthStatistic($data, $user)
  250. {
  251. // 1. 基础校验与时间初始化
  252. list($status, $month_start, $month_end) = $this->commonRule($data);
  253. if (!$status) return [false, $month_start];
  254. // 2. 获取设备工时数据 (项目+设备+月)
  255. $month_device_list = DailyDwOrderDetails::Clear($user, $data)
  256. ->where("order_time", ">=", $month_start)
  257. ->where("order_time", "<", $month_end)
  258. ->where('del_time', 0)
  259. ->select(
  260. "item_id",
  261. "device_id",
  262. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  263. DB::raw("SUM(total_work_min) as total_work")
  264. )
  265. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  266. // 3. 获取折旧费主表及详情
  267. $monthly_dd_query = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)
  268. ->where("month", ">=", $month_start)->where("month", "<", $month_end);
  269. $monthly_dd_ids = $monthly_dd_query->pluck('id')->toArray();
  270. $monthly_dd_months = $monthly_dd_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  271. ->pluck("month_str", 'id')->toArray();
  272. $device_salary_details = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_ids)
  273. ->select("device_id", "depreciation_amount", "main_id")->get()->toArray();
  274. // 4. 构建高精度折旧池 (单位:分)
  275. $depreciation_pool = [];
  276. foreach ($device_salary_details as $val) {
  277. $m = $monthly_dd_months[$val['main_id']] ?? '';
  278. if ($m) {
  279. $key = $val['device_id'] . '_' . $m;
  280. // 同一个设备在同一个月可能存在多条折旧记录,需累加
  281. $depreciation_pool[$key] = ($depreciation_pool[$key] ?? 0) + (int)round($val['depreciation_amount'] * 100);
  282. }
  283. }
  284. // 5. 计算全月总工时及初始化余额池
  285. $device_monthly_total_min = [];
  286. foreach ($month_device_list as $row) {
  287. $key = $row['device_id'] . '_' . $row['order_month'];
  288. $device_monthly_total_min[$key] = ($device_monthly_total_min[$key] ?? 0) + $row['total_work'];
  289. }
  290. $rem_depreciation_cents = $depreciation_pool; // 折旧余额池
  291. $rem_hours_cents = []; // 工时余额池 (小时*100)
  292. foreach ($device_monthly_total_min as $key => $min) {
  293. $rem_hours_cents[$key] = (int)round(($min / 60), 2) * 100;
  294. }
  295. // 6. 统计项目数用于判断“最后一条”
  296. $device_count = collect($month_device_list)->groupBy(fn($i) => $i['device_id'].'_'.$i['order_month'])
  297. ->map(fn($g) => $g->count())->toArray();
  298. // 7. 预加载设备与项目基础信息
  299. $items_ids = collect($month_device_list)->pluck('item_id')->unique()->all();
  300. $device_ids = collect($month_device_list)->pluck('device_id')->unique()->all();
  301. $item_info = Item::TopClear($user, $data)->whereIn('id', $items_ids)->get()->keyBy('id');
  302. $device_info = Device::TopClear($user, $data)->whereIn('id', $device_ids)->get()->keyBy('id');
  303. // 8. 核心转换逻辑
  304. $item_month_list = collect($month_device_list)->sortBy('order_month')->values()->transform(function ($item) use (
  305. $item_info, $device_info, $depreciation_pool, $device_monthly_total_min,
  306. &$device_count, &$rem_depreciation_cents, &$rem_hours_cents
  307. ) {
  308. $key = $item['device_id'] . '_' . $item['order_month'];
  309. $total_min = $device_monthly_total_min[$key] ?? 0;
  310. $item['item_title'] = $item_info[$item['item_id']]->title ?? "未知项目";
  311. $item['item_code'] = $item_info[$item['item_id']]->code ?? "-";
  312. $item['device_title'] = $device_info[$item['device_id']]->title ?? "未知设备";
  313. $item['device_original'] = $device_info[$item['device_id']]->original_value ?? 0;
  314. $item['month'] = $item['order_month'];
  315. $item['total_depreciatio'] = ($depreciation_pool[$key] ?? 0) / 100;
  316. $item['total_hours'] = round($total_min / 60, 2);
  317. // 分摊比例计算
  318. $ratio = $total_min > 0 ? ($item['total_work'] / $total_min) : 0;
  319. $item['ratio'] = round($ratio, 4);
  320. if (--$device_count[$key] > 0) {
  321. // 非最后一条项目
  322. $cur_hours = round($item['total_work'] / 60, 2);
  323. $item['hours'] = $cur_hours;
  324. $rem_hours_cents[$key] -= (int)round($cur_hours * 100);
  325. $cur_depre = round($item['total_depreciatio'] * $ratio, 2);
  326. $item['allocated_depreciatio'] = $cur_depre;
  327. $rem_depreciation_cents[$key] -= (int)round($cur_depre * 100);
  328. } else {
  329. // 最后一条项目,余额补平
  330. $item['hours'] = round(($rem_hours_cents[$key] ?? 0) / 100, 2);
  331. $item['allocated_depreciatio'] = round(($rem_depreciation_cents[$key] ?? 0) / 100, 2);
  332. // 比例也做个平账展示
  333. $item['ratio'] = round($item['hours'] / ($item['total_hours'] ?: 1), 4);
  334. }
  335. return $item;
  336. })->all();
  337. return [true, $item_month_list];
  338. }
  339. //原方法---
  340. public function employeeMonthSalaryStatistic1($data, $user)
  341. {
  342. //项目编码、项目名称、天数、工资、日期
  343. list($status, $month_start, $month_end) = $this->commonRule($data);
  344. if (!$status) return [false, $month_start];
  345. //确认所有项目、人员、人员工时
  346. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  347. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  348. ->where('del_time', 0)
  349. ->select(
  350. "item_id",
  351. "employee_id",
  352. // 将时间戳转为 Y-m-d 格式并起别名
  353. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  354. // 聚合求和
  355. DB::raw("SUM(total_work_min) as total_work")
  356. )
  357. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  358. //查询所有人员工资
  359. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  360. ->pluck('id')->toArray();
  361. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  362. ->where('del_time', 0)
  363. ->where("month", ">=", $month_start)
  364. ->where("month", "<", $month_end)
  365. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  366. ->pluck('month_str', 'id')
  367. ->toArray();
  368. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  369. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  370. ->get()->toArray();
  371. //查询所有项目人员的工时比例
  372. //汇总每个人每个月工资
  373. $salary_map = [];
  374. $all_salary = [];
  375. foreach ($month_employee_salary as $val) {
  376. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  377. if ($month) {
  378. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  379. }
  380. if (!isset($all_salary[$month])) $all_salary[$month] = 0;
  381. $all_salary[$month] += $val['salary'] * 100;
  382. }
  383. // var_dump($salary_map);die;
  384. // 2. 计算每个员工在每个月的全月总工时
  385. $employee_monthly_total_min = [];
  386. foreach ($month_employee_list as $row) {
  387. $key = $row['employee_id'] . '_' . $row['order_month'];
  388. if (!isset($employee_monthly_total_min[$key])) {
  389. $employee_monthly_total_min[$key] = 0;
  390. }
  391. $employee_monthly_total_min[$key] += $row['total_work'];
  392. }
  393. // 3. 计算分摊天数与工资
  394. $item_month_list = [];
  395. foreach ($month_employee_list as $item) {
  396. $key = $item['employee_id'] . '_' . $item['order_month'];
  397. $item_key = $item['order_month'] . '_' . $item['item_id'];
  398. if (!isset($item_month_list[$item_key])) {
  399. $item_month_list[$item_key] = [
  400. "month" => $item['order_month'],
  401. "allocated_salary" => 0,
  402. "work_minutes" => 0,
  403. "item_id" => $item['item_id'],
  404. ];
  405. }
  406. $total_salary = $salary_map[$key] ?? 0;
  407. $total_min = $employee_monthly_total_min[$key] ?? 0;
  408. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  409. if ($total_min > 0) {
  410. $ratio = $item['total_work'] / $total_min;
  411. $allocated_salary = round($ratio * $total_salary, 2);
  412. } else {
  413. $allocated_salary = 0;
  414. }
  415. $item_month_list[$item_key]['allocated_salary'] += $allocated_salary;
  416. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  417. }
  418. foreach ($item_month_list as $k => $v) {
  419. $item_month_list[$k]['days'] = round($v['work_minutes'] / 8 / 60);
  420. }
  421. $collect = collect($item_month_list);
  422. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  423. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  424. $item = Item::TopClear($user, $data);
  425. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  426. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  427. $collect = collect($item_month_list);
  428. $item_count = $collect->groupBy(function ($item) {
  429. // 这里的 $item 是集合中的每一行数据
  430. return $item['month'];
  431. })->map(function ($group) {
  432. return $group->count();
  433. })->toArray();
  434. $item_day_count = $collect->groupBy(fn($item) => $item['month'])
  435. ->map(fn($group) => round($group->sum('work_minutes') / 8 / 60))
  436. ->toArray();
  437. $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) {
  438. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  439. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  440. $item['allocated_salary'] = round($item['allocated_salary'], 2);
  441. // 如果不是最后一条
  442. $key = $item['month'];
  443. if (--$item_count[$key] > 0) {
  444. // 关键:递减外部的 $absolute_total_hours
  445. $item_day_count[$key] -= $item['days'];
  446. $all_salary[$key] -= $item['allocated_salary'] * 100;
  447. } else {
  448. $item['days'] = $item_day_count[$key];
  449. $item['allocated_salary'] = round($all_salary[$key] / 100, 2);
  450. }
  451. // if ($index < $count - 1) {
  452. // $total_day -= $item['days'];
  453. // // 关键:递减外部的 $absolute_total_hours
  454. // $all_salary -= $item['allocated_salary'];
  455. // } else {
  456. // // 最后一条:直接拿剩下的“余额”
  457. // $item['days'] = $total_day;
  458. // $item['allocated_salary'] = $all_salary;
  459. // }
  460. return $item;
  461. })->all();
  462. return [true, $item_month_list];
  463. }
  464. public function itemDaySalaryStatistic1($data, $user)
  465. {
  466. //项目编码、项目名称、人员名称、研发工时、研发工资、当月总工时、总计工资、年月
  467. list($status, $month_start, $month_end) = $this->commonRule($data);
  468. if (!$status) return [false, $month_start];
  469. //确认所有项目、人员、人员工时
  470. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  471. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  472. ->where('del_time', 0)
  473. ->select(
  474. "item_id",
  475. "employee_id",
  476. // 将时间戳转为 Y-m-d 格式并起别名
  477. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  478. // 聚合求和
  479. DB::raw("SUM(total_work_min) as total_work")
  480. )
  481. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "employee_id")->get()->toArray();
  482. //查询所有人员工资
  483. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)->pluck('id')->toArray();
  484. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  485. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  486. ->pluck('month_str', 'id')->toArray();
  487. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  488. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  489. ->get()->toArray();
  490. //查询所有项目人员的工时比例
  491. //汇总每个人每个月工资
  492. $salary_map = [];
  493. $all_salary = [];
  494. foreach ($month_employee_salary as $val) {
  495. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  496. if ($month) {
  497. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  498. }
  499. $key = $val['employee_id'] . '_' . $month;
  500. if (!isset($all_salary[$key])) {
  501. $all_salary[$key] = 0;
  502. }
  503. $all_salary[$key] += $val['salary'] * 100;
  504. }
  505. // 2. 计算每个员工在每个月的全月总工时
  506. $employee_monthly_total_min = [];
  507. foreach ($month_employee_list as $row) {
  508. $key = $row['employee_id'] . '_' . $row['order_month'];
  509. if (!isset($employee_monthly_total_min[$key])) {
  510. $employee_monthly_total_min[$key] = 0;
  511. }
  512. $employee_monthly_total_min[$key] += $row['total_work'];
  513. }
  514. // 3. 计算分摊天数与工资
  515. $item_month_list = [];
  516. $total_work_min = [];
  517. foreach ($month_employee_list as $item) {
  518. $key = $item['employee_id'] . '_' . $item['order_month'];
  519. $employee_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  520. $total_salary = $salary_map[$key] ?? 0;
  521. $total_min = $employee_monthly_total_min[$key] ?? 0;
  522. if (!isset($item_month_list[$employee_key])) {
  523. $item_month_list[$employee_key] = [
  524. "month" => $item['order_month'],
  525. "allocated_salary" => 0,
  526. "work_minutes" => 0,
  527. "total_min" => $total_min,
  528. "total_salary" => $total_salary,
  529. "item_id" => $item['item_id'],
  530. "employee_id" => $item['employee_id'],
  531. ];
  532. }
  533. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  534. if ($total_min > 0) {
  535. $ratio = $item['total_work'] / $total_min;
  536. $allocated_salary = round($ratio * $total_salary, 2);
  537. } else {
  538. $allocated_salary = 0;
  539. }
  540. $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
  541. $item_month_list[$employee_key]['work_minutes'] += $item['total_work'];
  542. $key = $item['employee_id'] . '_' . $item['order_month'];
  543. if (!isset($total_work_min[$key])) $total_work_min[$key] = 0;
  544. $total_work_min[$key] += $item['total_work'];
  545. }
  546. foreach ($total_work_min as $k => $v) {
  547. $total_work_min[$k] = round($v / 60, 2) * 100;
  548. }
  549. $collect = collect($item_month_list);
  550. $employee_count = $collect->groupBy(function ($item) {
  551. // 这里的 $item 是集合中的每一行数据
  552. return $item['employee_id'] . '_' . $item['month'];
  553. })->map(function ($group) {
  554. return $group->count();
  555. })->toArray();
  556. // $collect = collect($item_month_list);
  557. // $employee_count = $collect->groupBy('employee_id')
  558. // ->map(function ($group) {
  559. // return $group->count(); // 统计每个分组里的数量
  560. // })
  561. // ->toArray();
  562. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  563. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  564. $item = Item::TopClear($user, $data);
  565. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  566. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  567. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  568. $employee = Employee::TopClear($user, $data);
  569. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  570. $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) {
  571. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  572. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  573. $item['employee_title'] = $employee_key_list[$item['employee_id']] ?? "未知人员({$item['employee_id']})";
  574. $item['work_hours'] = round($item['work_minutes'] / 60, 2);
  575. $item['total_hours'] = round($item['total_min'] / 60);
  576. $key = $item['employee_id'] . '_' . $item['month'];
  577. if (--$employee_count[$key] > 0) {
  578. $all_salary[$key] -= $item['allocated_salary'] * 100;
  579. $total_work_min[$key] -= $item['work_hours'] * 100;
  580. } else {
  581. $item['allocated_salary'] = round($all_salary[$key] / 100, 2);
  582. $item['work_hours'] = round($total_work_min[$key] / 100, 2);
  583. }
  584. return $item;
  585. })->all();
  586. return [true, $item_month_list];
  587. }
  588. public function itemDeviceMonthStatistic1($data, $user)
  589. {
  590. //项目编码、项目名称、设备名称、项目工时、研发工时占比、设备原值、设备折旧额、本项目帐面归集的折旧额、确定的本项目折旧额、加计调整金额、当月工时、日期
  591. list($status, $month_start, $month_end) = $this->commonRule($data);
  592. if (!$status) return [false, $month_start];
  593. //确认所有项目、设备、设备工时
  594. $month_device = DailyDwOrderDetails::Clear($user, $data);
  595. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  596. ->where('del_time', 0)
  597. ->select(
  598. "item_id",
  599. "device_id",
  600. // 将时间戳转为 Y-m-d 格式并起别名
  601. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  602. // 聚合求和
  603. DB::raw("SUM(total_work_min) as total_work")
  604. )
  605. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  606. //查询所有人员工资
  607. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  608. ->pluck('id')->toArray();
  609. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  610. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  611. ->pluck("month_str", 'id')->toArray();
  612. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  613. ->select("device_id", "depreciation_amount", "main_id")
  614. ->get()->toArray();
  615. //查询所有项目人员的工时比例
  616. //汇总每个人每个月工资
  617. $depreciatio_map = [];
  618. foreach ($month_device_salary as $val) {
  619. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  620. if ($month) {
  621. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  622. }
  623. }
  624. // 2. 计算每个员工在每个月的全月总工时
  625. $device_monthly_total_min = [];
  626. foreach ($month_device_list as $row) {
  627. $key = $row['device_id'] . '_' . $row['order_month'];
  628. if (!isset($device_monthly_total_min[$key])) {
  629. $device_monthly_total_min[$key] = 0;
  630. }
  631. $device_monthly_total_min[$key] += $row['total_work'];
  632. }
  633. // 3. 计算分摊天数与工资
  634. $item_month_list = [];
  635. $device_total_depreciatio = [];
  636. foreach ($month_device_list as $item) {
  637. $key = $item['device_id'] . '_' . $item['order_month'];
  638. $device_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['device_id'];
  639. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  640. $total_min = $device_monthly_total_min[$key] ?? 0;
  641. if (!isset($item_month_list[$device_key])) {
  642. $item_month_list[$device_key] = [
  643. "month" => $item['order_month'],
  644. "allocated_depreciatio" => 0,
  645. "work_minutes" => 0,
  646. "total_min" => $total_min,
  647. "item_id" => $item['item_id'],
  648. "device_id" => $item['device_id'],
  649. "total_depreciatio" => $total_depreciatio,
  650. ];
  651. $key = $item['device_id'] . '_' . $item['order_month'];
  652. $device_total_depreciatio[$key] = [
  653. 'total_hours' => round($total_min/60,2)*100,
  654. 'total_depreciatio' => $total_depreciatio*100
  655. ];
  656. }
  657. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  658. if ($total_min > 0) {
  659. $ratio = round($item['total_work'] / $total_min, 3);
  660. $allocated_salary = round($ratio * $total_depreciatio, 2);
  661. } else {
  662. $ratio = 0;
  663. $allocated_salary = 0;
  664. }
  665. $item_month_list[$device_key]['allocated_depreciatio'] += $allocated_salary;
  666. $item_month_list[$device_key]['work_minutes'] += $item['total_work'];
  667. $item_month_list[$device_key]['ratio'] = $ratio;
  668. }
  669. foreach ($item_month_list as $k => $v) {
  670. $item_month_list[$k]['total_hours'] = round($v['total_min'] / 60, 1);
  671. $item_month_list[$k]['hours'] = round($v['work_minutes'] / 60, 1);
  672. unset($item_month_list[$k]['work_minutes']);
  673. }
  674. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  675. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  676. $item = Item::TopClear($user, $data);
  677. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  678. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  679. $device_ids = collect($item_month_list)->pluck('device_id')->unique()->values()->all();
  680. $device = Device::TopClear($user, $data);
  681. $device_key_list = $device->wherein('id', $device_ids)->pluck("title", "id")->toArray();
  682. $device_original_value_key_list = $device->wherein('id', $device_ids)->pluck("original_value", "id")->toArray();
  683. $collect = collect($item_month_list);
  684. $device_count = $collect->groupBy(function ($item) {
  685. // 这里的 $item 是集合中的每一行数据
  686. return $item['device_id'] . '_' . $item['month'];
  687. })->map(function ($group) {
  688. return $group->count();
  689. })->toArray();
  690. $rate_list = [];
  691. $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) {
  692. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  693. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  694. $item['device_title'] = $device_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  695. $item['device_original'] = $device_original_value_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  696. $key = $item['device_id'] . '_' . $item['month'];
  697. if(!isset($rate_list[$key])) $rate_list[$key] = 100;
  698. if (--$device_count[$key] > 0) {
  699. $rate_list[$key] -= $item['ratio'] * 100;
  700. $device_total_depreciatio[$key]['total_hours'] -= $item['hours'] * 100;
  701. $device_total_depreciatio[$key]['total_depreciatio'] -= $item['allocated_depreciatio'] * 100;
  702. } else {
  703. $item['ratio'] = round($rate_list[$key] / 100, 2);
  704. $item['hours'] = round( $device_total_depreciatio[$key]['total_hours']/ 100, 2);
  705. $item['allocated_depreciatio'] = round( $device_total_depreciatio[$key]['total_depreciatio']/ 100, 2);
  706. }
  707. return $item;
  708. })->all();
  709. return [true, $item_month_list];
  710. }
  711. public function employeeAttendanceMonthStatistic($data, $user)
  712. {
  713. // 1. 基础规则验证
  714. list($status, $month_start, $month_end) = $this->commonRule($data);
  715. if (!$status) return [false, $month_start];
  716. // 2. 确定该区间内涉及的项目
  717. $item_list = Item::TopClear($user, $data)
  718. ->where('del_time', 0)
  719. ->where(function ($query) use ($month_start, $month_end) {
  720. $query->where('start_time', '>=', $month_start)
  721. ->where('end_time', '<', $month_end);
  722. })
  723. ->select("code", "title", "start_time", "end_time", "id", "state")
  724. ->orderby("start_time", "asc")
  725. ->get();
  726. $item_key_list = $item_list->keyBy('id')->toArray();
  727. // 3. 获取精准分摊的人员费用(内部已处理精度,返回单位为:分)
  728. $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
  729. // 4. 获取精准分摊的折旧费用(内部已处理精度,返回单位为:分)
  730. $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
  731. // 5. 获取其他报销费用
  732. list($item_fee_list, $fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  733. // 6. 组合所有数据
  734. $result = [];
  735. foreach ($item_key_list as $itemId => $v) {
  736. // 从分摊结果中取值,注意单位换算回“元”
  737. $salary_cents = $item_employee_list[$itemId]['salary'] ?? 0;
  738. $dep_cents = $item_device_list[$itemId]['depreciation'] ?? 0;
  739. $item_value = [
  740. "code" => $v['code'],
  741. "title" => $v['title'],
  742. "state" => $v['state'] == 3 ? "完结" : "进行中",
  743. "employee_salary" => round($salary_cents / 100, 2),
  744. "device_depreciation" => round($dep_cents / 100, 2),
  745. "expense_type" => "费用化支出",
  746. "fee_list" => collect($item_fee_list[$itemId] ?? [])->values()->all(),
  747. ];
  748. $result[] = $item_value;
  749. }
  750. return [true, ["list" => $result, "fee_type_list" => $fee_type_list]];
  751. }
  752. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  753. {
  754. // 获取工时明细(按月、人、项目分组)
  755. $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
  756. ->where("order_time", ">=", $month_start)
  757. ->where("order_time", "<", $month_end)
  758. ->where('del_time', 0)
  759. ->select(
  760. "item_id",
  761. "employee_id",
  762. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  763. DB::raw("SUM(total_work_min) as total_work")
  764. )
  765. ->groupBy("order_month", "item_id", "employee_id")
  766. ->get()->toArray();
  767. // 获取工资主表并建立月份映射
  768. $ps_query = MonthlyPsOrder::Clear($user, $data)
  769. ->where('del_time', 0)
  770. ->where("month", ">=", $month_start)
  771. ->where("month", "<", $month_end);
  772. $ps_ids = $ps_query->pluck('id')->toArray();
  773. $month_map = $ps_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  774. ->pluck('month_str', 'id')->toArray();
  775. // 获取工资明细并转为整数“分”
  776. $salary_details = MonthlyPsOrderDetails::whereIn('main_id', $ps_ids)
  777. ->select("employee_id", "main_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"))
  778. ->get();
  779. $all_salary_pool = []; // 存放 [人员_月份 => 总金额分]
  780. foreach ($salary_details as $val) {
  781. $m = $month_map[$val['main_id']] ?? '';
  782. if ($m) {
  783. $k = $val['employee_id'] . '_' . $m;
  784. $all_salary_pool[$k] = (int)round($val['salary'] * 100);
  785. }
  786. }
  787. // 统计每个人每月的总工时和参与的项目数(用于尾差平账)
  788. $emp_monthly_total_min = [];
  789. $emp_item_count = [];
  790. foreach ($month_employee_list as $row) {
  791. $k = $row['employee_id'] . '_' . $row['order_month'];
  792. $emp_monthly_total_min[$k] = ($emp_monthly_total_min[$k] ?? 0) + $row['total_work'];
  793. $emp_item_count[$k] = ($emp_item_count[$k] ?? 0) + 1;
  794. }
  795. $rem_salary = $all_salary_pool; // 剩余金额池
  796. $return_item_list = [];
  797. foreach ($month_employee_list as $item) {
  798. $k = $item['employee_id'] . '_' . $item['order_month'];
  799. $total_s = $all_salary_pool[$k] ?? 0;
  800. $total_m = $emp_monthly_total_min[$k] ?? 0;
  801. if ($total_m > 0 && --$emp_item_count[$k] > 0) {
  802. // 非最后一个项目:按比例计算分值(取整,余额存入池子)
  803. $allocated = (int)floor(($item['total_work'] / $total_m) * $total_s);
  804. $rem_salary[$k] -= $allocated;
  805. } else {
  806. // 最后一个项目:直接取剩余全部金额
  807. $allocated = $rem_salary[$k] ?? 0;
  808. }
  809. $itemId = $item['item_id'];
  810. if (!isset($return_item_list[$itemId])) $return_item_list[$itemId]['salary'] = 0;
  811. $return_item_list[$itemId]['salary'] += $allocated;
  812. }
  813. return $return_item_list;
  814. }
  815. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  816. {
  817. // 获取设备工时明细
  818. $month_device_list = DailyDwOrderDetails::Clear($user, $data)
  819. ->where("order_time", ">=", $month_start)
  820. ->where("order_time", "<", $month_end)
  821. ->where('del_time', 0)
  822. ->select(
  823. "item_id",
  824. "device_id",
  825. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  826. DB::raw("SUM(total_work_min) as total_work")
  827. )
  828. ->groupBy("order_month", "item_id", "device_id")
  829. ->get()->toArray();
  830. // 获取折旧主表
  831. $dd_query = MonthlyDdOrder::Clear($user, $data)
  832. ->where('del_time', 0)
  833. ->where("month", ">=", $month_start)
  834. ->where("month", "<", $month_end);
  835. $dd_ids = $dd_query->pluck('id')->toArray();
  836. $month_map = $dd_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  837. ->pluck("month_str", 'id')->toArray();
  838. // 获取折旧明细并转为整数“分”
  839. $device_details = MonthlyDdOrderDetails::whereIn('main_id', $dd_ids)
  840. ->select("device_id", "depreciation_amount", "main_id")
  841. ->get();
  842. $all_dep_pool = [];
  843. foreach ($device_details as $val) {
  844. $m = $month_map[$val['main_id']] ?? '';
  845. if ($m) {
  846. $k = $val['device_id'] . '_' . $m;
  847. $all_dep_pool[$k] = (int)round($val['depreciation_amount'] * 100);
  848. }
  849. }
  850. // 统计设备每月总工时和项目数
  851. $dev_monthly_total_min = [];
  852. $dev_item_count = [];
  853. foreach ($month_device_list as $row) {
  854. $k = $row['device_id'] . '_' . $row['order_month'];
  855. $dev_monthly_total_min[$k] = ($dev_monthly_total_min[$k] ?? 0) + $row['total_work'];
  856. $dev_item_count[$k] = ($dev_item_count[$k] ?? 0) + 1;
  857. }
  858. $rem_dep = $all_dep_pool;
  859. $return_item_list = [];
  860. foreach ($month_device_list as $item) {
  861. $k = $item['device_id'] . '_' . $item['order_month'];
  862. $total_d = $all_dep_pool[$k] ?? 0;
  863. $total_m = $dev_monthly_total_min[$k] ?? 0;
  864. if ($total_m > 0 && --$dev_item_count[$k] > 0) {
  865. $allocated = (int)floor(($item['total_work'] / $total_m) * $total_d);
  866. $rem_dep[$k] -= $allocated;
  867. } else {
  868. $allocated = $rem_dep[$k] ?? 0;
  869. }
  870. $itemId = $item['item_id'];
  871. if (!isset($return_item_list[$itemId])) $return_item_list[$itemId]['depreciation'] = 0;
  872. $return_item_list[$itemId]['depreciation'] += $allocated;
  873. }
  874. return $return_item_list;
  875. }
  876. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  877. {
  878. //确认所有项目、费用
  879. $expense = ExpenseClaimsDetails::Clear($user, $data);
  880. $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
  881. ->where('del_time', 0)
  882. ->select(
  883. "fee_id",
  884. "amount",
  885. "item_id",
  886. "entrust_type"
  887. )->get()->toArray();
  888. //需要根据分类去汇总
  889. $fee = Fee::TopClear($user, $data);
  890. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  891. return $this->groupListByRoot($expense_list, $fee);
  892. }
  893. //原方法-------
  894. public function employeeAttendanceMonthStatistic1($data, $user)
  895. {
  896. //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
  897. list($status, $month_start, $month_end) = $this->commonRule($data);
  898. if (!$status) return [false, $month_start];
  899. //第一步确定项目
  900. $item = Item::TopClear($user, $data);
  901. $item_list = $item->where('del_time', 0)
  902. ->where(function ($query) use ($month_start, $month_end) {
  903. $query->where('start_time', '>=', $month_start)
  904. ->where('end_time', '<', $month_end);
  905. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
  906. $item_key_list = [];
  907. foreach ($item_list as $v) {
  908. $item_key_list[$v['id']] = $v;
  909. }
  910. //第二步确定人员费用
  911. $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
  912. //第三步确定折旧费用
  913. $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
  914. //第四步其他费用
  915. list($item_fee_list, $fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  916. //组合所有数据
  917. $return = [];
  918. foreach ($item_key_list as $v) {
  919. //其他费用是个数组
  920. $item_value = [
  921. "code" => $v['code'],
  922. "title" => $v['title'],
  923. "state" => $v['state'] == 3 ? "完结" : "进行中",
  924. "employee_salary" => isset($item_employee_list[$v['id']]['salary']) ? round($item_employee_list[$v['id']]['salary']/100,2) : 0,
  925. "device_depreciation" => isset($item_device_list[$v['id']]['depreciation']) ? round($item_device_list[$v['id']]['depreciation']/100,2): 0,
  926. "expense_type" => "费用化支出",
  927. "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
  928. ];
  929. $return[] = $item_value;
  930. }
  931. return [true, ["list" => $return, "fee_type_list" => $fee_type_list]];
  932. }
  933. private function getEmployeeItemSalary1($month_start, $month_end, $data, $user)
  934. {
  935. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  936. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  937. ->where('del_time', 0)
  938. ->select(
  939. "item_id",
  940. "employee_id",
  941. // 将时间戳转为 Y-m-d 格式并起别名
  942. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  943. // 聚合求和
  944. DB::raw("SUM(total_work_min) as total_work")
  945. )
  946. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  947. //查询所有人员工资
  948. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  949. ->pluck('id')->toArray();
  950. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  951. ->where('del_time', 0)
  952. ->where("month", ">=", $month_start)
  953. ->where("month", "<", $month_end)
  954. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  955. ->pluck('month_str', 'id')
  956. ->toArray();
  957. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  958. ->select("employee_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  959. ->get()->toArray();
  960. //查询所有项目人员的工时比例
  961. //汇总每个人每个月工资
  962. $salary_map = [];
  963. $all_salary = [];
  964. foreach ($month_employee_salary as $val) {
  965. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  966. if ($month) {
  967. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  968. $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
  969. }
  970. }
  971. // 2. 计算每个员工在每个月的全月总工时
  972. $employee_monthly_total_min = [];
  973. foreach ($month_employee_list as $row) {
  974. $key = $row['employee_id'] . '_' . $row['order_month'];
  975. if (!isset($employee_monthly_total_min[$key])) {
  976. $employee_monthly_total_min[$key] = 0;
  977. }
  978. $employee_monthly_total_min[$key] += $row['total_work'];
  979. }
  980. // 2. 计算分摊天数与工资
  981. $item_list = [];
  982. foreach ($month_employee_list as $item) {
  983. $key = $item['employee_id'] . '_' . $item['order_month'];
  984. // $item_key = $item['item_id'].'_'.$item['order_month'];
  985. // if (!isset($item_list[$item_key])) {
  986. // $item_list[$item_key] = [
  987. // "salary" => 0,
  988. // ];
  989. // }
  990. $total_salary = $salary_map[$key] ?? 0;
  991. $total_min = $employee_monthly_total_min[$key] ?? 0;
  992. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  993. if ($total_min > 0) {
  994. $ratio = $item['total_work'] / $total_min;
  995. $allocated_salary = round($ratio * $total_salary, 2)*100;
  996. } else {
  997. $allocated_salary = 0;
  998. }
  999. $item_list[] = [
  1000. 'employee_id' => $item['employee_id'],
  1001. 'allocated_salary' => $allocated_salary,
  1002. 'month' => $item['order_month'],
  1003. 'item_id' => $item['item_id'],
  1004. ];
  1005. }
  1006. $collect = collect($item_list);
  1007. $employee_count = $collect->groupBy(function ($item) {
  1008. // 这里的 $item 是集合中的每一行数据
  1009. return $item['employee_id'] . '_' . $item['month'];
  1010. })->map(function ($group) {
  1011. return $group->count();
  1012. })->toArray();
  1013. $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
  1014. // 如果不是最后一条
  1015. $key = $item['employee_id'] . '_' . $item['month'];
  1016. if(!isset($all_salary[$key])) $item['allocated_salary'] = 0;
  1017. else{
  1018. if (--$employee_count[$key] > 0) {
  1019. $all_salary[$key] -= $item['allocated_salary'];
  1020. } else {
  1021. $item['allocated_salary'] = $all_salary[$key];
  1022. }
  1023. }
  1024. return $item;
  1025. })->all();
  1026. $return_item_list = [];
  1027. foreach ($month_employee_list as $v){
  1028. $key = $v['item_id'];
  1029. if(!isset($return_item_list[$key])) $return_item_list[$key]['salary'] = 0;
  1030. $return_item_list[$key]['salary'] += $v['allocated_salary'];
  1031. }
  1032. return $return_item_list;
  1033. }
  1034. private function getDeviceItemSalary1($month_start, $month_end, $data, $user)
  1035. {
  1036. //确认所有项目、设备、设备工时
  1037. $month_device = DailyDwOrderDetails::Clear($user, $data);
  1038. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1039. ->where('del_time', 0)
  1040. ->select(
  1041. "item_id",
  1042. "device_id",
  1043. // 将时间戳转为 Y-m-d 格式并起别名
  1044. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1045. // 聚合求和
  1046. DB::raw("SUM(total_work_min) as total_work")
  1047. )
  1048. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  1049. //查询所有人员工资
  1050. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1051. ->pluck('id')->toArray();
  1052. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1053. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1054. ->pluck("month_str", 'id')->toArray();
  1055. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  1056. ->select("device_id", "depreciation_amount", "main_id")
  1057. ->get()->toArray();
  1058. //查询所有项目人员的工时比例
  1059. //汇总每个人每个月工资
  1060. $depreciatio_map = [];
  1061. $all_depreciatio = [];
  1062. foreach ($month_device_salary as $val) {
  1063. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  1064. if ($month) {
  1065. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  1066. $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
  1067. }
  1068. }
  1069. // 2. 计算每个员工在每个月的全月总工时
  1070. $device_monthly_total_min = [];
  1071. foreach ($month_device_list as $row) {
  1072. $key = $row['device_id'] . '_' . $row['order_month'];
  1073. if (!isset($device_monthly_total_min[$key])) {
  1074. $device_monthly_total_min[$key] = 0;
  1075. }
  1076. $device_monthly_total_min[$key] += $row['total_work'];
  1077. }
  1078. // 3. 计算分摊天数与工资
  1079. $item_list = [];
  1080. foreach ($month_device_list as $item) {
  1081. $key = $item['device_id'] . '_' . $item['order_month'];
  1082. // $device_key = $item['item_id'];
  1083. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  1084. $total_min = $device_monthly_total_min[$key] ?? 0;
  1085. // if (!isset($item_list[$device_key])) {
  1086. // $item_list[$device_key] = [
  1087. // "depreciation" => 0,
  1088. // ];
  1089. // }
  1090. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1091. if ($total_min > 0) {
  1092. $ratio = round($item['total_work'] / $total_min, 3);
  1093. $allocated_salary = round($ratio * $total_depreciatio, 2)*100;
  1094. } else {
  1095. $allocated_salary = 0;
  1096. }
  1097. $item_list[] = [
  1098. 'device_id' => $item['device_id'],
  1099. 'depreciation' => $allocated_salary,
  1100. 'month' => $item['order_month'],
  1101. 'item_id' => $item['item_id'],
  1102. ];
  1103. }
  1104. $collect = collect($item_list);
  1105. $device_count = $collect->groupBy(function ($item) {
  1106. // 这里的 $item 是集合中的每一行数据
  1107. return $item['device_id'] . '_' . $item['month'];
  1108. })->map(function ($group) {
  1109. return $group->count();
  1110. })->toArray();
  1111. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
  1112. // 如果不是最后一条
  1113. $key = $item['device_id'] . '_' . $item['month'];
  1114. if (--$device_count[$key] > 0) {
  1115. $all_depreciatio[$key] -= $item['depreciation'];
  1116. } else {
  1117. $item['depreciation'] = $all_depreciatio[$key];
  1118. }
  1119. return $item;
  1120. })->all();
  1121. $return_item_list = [];
  1122. foreach ($month_employee_list as $v){
  1123. $key = $v['item_id'];
  1124. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  1125. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  1126. }
  1127. // var_dump($return_item_list);die;
  1128. return $return_item_list;
  1129. }
  1130. /**
  1131. * 将报销明细按照一级费用和项目分类进行分组
  1132. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  1133. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  1134. * @return array
  1135. */
  1136. public function groupListByRoot(array $list, array $fee_type_list)
  1137. {
  1138. // 1. 建立 ID 索引,方便快速查找
  1139. $idMap = array_column($fee_type_list, null, 'id');
  1140. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  1141. $childToRoot = [];
  1142. foreach ($fee_type_list as $type) {
  1143. $current = $type;
  1144. // 向上追溯直到 parent_id 为 0,即找到一级分类
  1145. while ($current['parent_id'] != 0) {
  1146. $current = $idMap[$current['parent_id']];
  1147. }
  1148. $childToRoot[$type['id']] = [
  1149. 'id' => $current['id'],
  1150. 'title' => $current['title'],
  1151. 'sort' => $current['sort'],
  1152. 'is_other' => $type['is_other']
  1153. ];
  1154. }
  1155. // 3. 遍历明细数据进行分组
  1156. $item_key_list = [];
  1157. $type_list = [];
  1158. foreach ($list as $item) {
  1159. $feeId = $item['fee_id'];
  1160. // 获取该费用对应的一级分类信息
  1161. if (!isset($childToRoot[$feeId])) continue;
  1162. $rootId = $childToRoot[$feeId]['id'];
  1163. $is_other = $childToRoot[$feeId]['is_other'];
  1164. $title = $childToRoot[$feeId]['title'];
  1165. $sort = $childToRoot[$feeId]['sort'];
  1166. $key = $item['item_id'];
  1167. if (!isset($item_key_list[$key][$rootId])) {
  1168. $item_key_list[$key][$rootId] = [
  1169. 'id' => $rootId,
  1170. 'total_amount' => 0,
  1171. 'entrust1_amount' => 0, //委内
  1172. 'entrust2_amount' => 0, //委外
  1173. 'is_other' => $is_other //是否其他费用
  1174. ];
  1175. }
  1176. if ($item['entrust_type'] == 1) {
  1177. $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount']*100;
  1178. } elseif ($item['entrust_type'] == 2) {
  1179. $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount']*100;
  1180. }
  1181. $item_key_list[$key][$rootId]['total_amount'] += $item['amount']*100;
  1182. //这边需要拿到头部所有的一级费用类型
  1183. if (!isset($type_list[$rootId])) {
  1184. $type_list[$rootId] = [
  1185. 'sort' => $sort,
  1186. 'id' => $rootId,
  1187. 'title' => $title,
  1188. ];
  1189. }
  1190. }
  1191. // var_dump($item_key_list);die;
  1192. foreach ($item_key_list as $k=>$v){
  1193. foreach ($v as $kk=>$vv){
  1194. $item_key_list[$k][$kk]['entrust1_amount'] = round( $vv['entrust1_amount']/100,2);
  1195. $item_key_list[$k][$kk]['entrust2_amount'] = round( $vv['entrust2_amount']/100,2);
  1196. $item_key_list[$k][$kk]['total_amount'] = round( $vv['total_amount']/100,2);
  1197. }
  1198. }
  1199. // $item_key_list = collect($item_key_list)->transform(function ($item) {
  1200. // $item['entrust1_amount'] = round( $item['entrust1_amount']/100,2);
  1201. // $item['entrust2_amount'] = round( $item['entrust2_amount']/100,2);
  1202. // $item['total_amount'] = round( $item['total_amount']/100,2);
  1203. // return $item;
  1204. // })->all();
  1205. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  1206. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  1207. // 4. 重置数组索引并返回
  1208. return [$item_key_list, $type_list];
  1209. }
  1210. public function auxiliaryStatistic($data, $user)
  1211. {
  1212. list($status, $month_start, $month_end) = $this->commonRule($data);
  1213. if (!$status) return [false, $month_start];
  1214. // 1. 获取项目列表
  1215. $item_list = Item::TopClear($user, $data)->where('del_time', 0)
  1216. ->where(function ($query) use ($month_start, $month_end) {
  1217. $query->where('start_time', '>=', $month_start)->where('end_time', '<', $month_end);
  1218. })->select("code", "title", "id", "state")->orderby("start_time", "asc")->get()->keyBy('id')->toArray();
  1219. // 2. 获取分摊的人工和折旧 (此时结果内部已是“分”)
  1220. $item_salary = $this->auxiliaryEmployee($user, $data, $month_start, $month_end);
  1221. $device_depreciation = $this->auxiliaryDevice($user, $data, $month_start, $month_end);
  1222. // 3. 处理凭证费用
  1223. $fee = Fee::TopClear($user, $data)->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  1224. $auxiliary_list = AuxiliaryAccountDetails::Clear($user, $data)
  1225. ->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  1226. ->where('del_time', 0)->get()->toArray();
  1227. list($fee_amount, $fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list, $fee);
  1228. $return = [];
  1229. foreach ($fee_amount as $v) {
  1230. $v_month = date("Y-m", $v['voucher_date']);
  1231. // 类型 1(人工) 或 2(折旧) 需要遍历项目进行分摊显示
  1232. if ($v['type'] == 1 || $v['type'] == 2) {
  1233. foreach ($item_list as $itemId => $vv) {
  1234. $search_key = $itemId . "_" . $v_month;
  1235. // 只有当该项目在该月有实际分摊金额时才产生记录
  1236. $allocated_amount = 0;
  1237. if ($v['type'] == 1) {
  1238. $allocated_amount = $item_salary[$search_key]['allocated_salary'] ?? 0;
  1239. } else {
  1240. $allocated_amount = $device_depreciation[$search_key]['depreciation'] ?? 0;
  1241. }
  1242. if ($allocated_amount <= 0) continue;
  1243. $return[] = [
  1244. "code" => $vv['code'],
  1245. "title" => $vv['title'],
  1246. "state" => $vv['state'] == 3 ? "进行中" : "已完成",
  1247. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  1248. "voucher_type" => $v['voucher_type'],
  1249. "voucher_no" => $v['voucher_no'],
  1250. "voucher_remark" => $v['voucher_remark'],
  1251. "voucher_amount" => $v['voucher_amount'],
  1252. "aggregation_amount" => $v['aggregation_amount'],
  1253. "total_amount" => round($allocated_amount / 100, 2), // 转回元
  1254. "fee_id" => $v['fee_id'],
  1255. "entrust1_amount" => $v['entrust1_amount'],
  1256. "entrust2_amount" => $v['entrust2_amount'],
  1257. "type" => $v['type'],
  1258. ];
  1259. }
  1260. } else {
  1261. // 普通凭证费用
  1262. if (!isset($item_list[$v['item_id']])) continue;
  1263. $item_value = $item_list[$v['item_id']];
  1264. $return[] = [
  1265. "code" => $item_value['code'],
  1266. "title" => $item_value['title'],
  1267. "state" => $item_value['state'] == 3 ? "进行中" : "已完成",
  1268. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  1269. "voucher_type" => $v['voucher_type'],
  1270. "voucher_no" => $v['voucher_no'],
  1271. "voucher_remark" => $v['voucher_remark'],
  1272. "voucher_amount" => $v['voucher_amount'],
  1273. "aggregation_amount" => $v['aggregation_amount'],
  1274. "total_amount" => $v['total_amount'],
  1275. "fee_id" => $v['fee_id'],
  1276. "entrust1_amount" => $v['entrust1_amount'],
  1277. "entrust2_amount" => $v['entrust2_amount'],
  1278. "type" => $v['type'],
  1279. ];
  1280. }
  1281. }
  1282. return [true, [
  1283. 'fee_type_list' => $fee_type_list,
  1284. 'list' => $return,
  1285. ]];
  1286. }
  1287. public function auxiliaryEmployee($user, $data, $month_start, $month_end)
  1288. {
  1289. //确认所有项目、人员、人员工时
  1290. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  1291. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1292. ->where('del_time', 0)
  1293. ->select(
  1294. "item_id",
  1295. "employee_id",
  1296. // 将时间戳转为 Y-m-d 格式并起别名
  1297. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1298. // 聚合求和
  1299. DB::raw("SUM(total_work_min) as total_work")
  1300. )
  1301. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  1302. //查询所有人员工资
  1303. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1304. ->pluck('id')->toArray();
  1305. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  1306. ->where('del_time', 0)
  1307. ->where("month", ">=", $month_start)
  1308. ->where("month", "<", $month_end)
  1309. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1310. ->pluck('month_str', 'id')
  1311. ->toArray();
  1312. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  1313. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  1314. ->get()->toArray();
  1315. //查询所有项目人员的工时比例
  1316. //汇总每个人每个月工资
  1317. $salary_map = [];
  1318. $all_salary = [];
  1319. foreach ($month_employee_salary as $val) {
  1320. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  1321. if ($month) {
  1322. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  1323. $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
  1324. }
  1325. }
  1326. // var_dump($salary_map);die;
  1327. // 2. 计算每个员工在每个月的全月总工时
  1328. $employee_monthly_total_min = [];
  1329. foreach ($month_employee_list as $row) {
  1330. $key = $row['employee_id'] . '_' . $row['order_month'];
  1331. if (!isset($employee_monthly_total_min[$key])) {
  1332. $employee_monthly_total_min[$key] = 0;
  1333. }
  1334. $employee_monthly_total_min[$key] += $row['total_work'];
  1335. }
  1336. // 3. 计算分摊天数与工资
  1337. // $item_year_list = [];
  1338. $item_list = [];
  1339. foreach ($month_employee_list as $item) {
  1340. $key = $item['employee_id'] . '_' . $item['order_month'];
  1341. // $item_key = $item['item_id'] . '_' . $item['order_month'];
  1342. // if (!isset($item_year_list[$item_key])) {
  1343. // $item_year_list[$item_key] = [
  1344. // "allocated_salary" => 0,
  1345. // "item_id" => $item['item_id'],
  1346. // ];
  1347. // }
  1348. $total_salary = $salary_map[$key] ?? 0;
  1349. $total_min = $employee_monthly_total_min[$key] ?? 0;
  1350. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1351. if ($total_min > 0) {
  1352. $ratio = $item['total_work'] / $total_min;
  1353. $allocated_salary = round($ratio * $total_salary, 2)*100;
  1354. } else {
  1355. $allocated_salary = 0;
  1356. }
  1357. $item_list[] = [
  1358. 'employee_id' => $item['employee_id'],
  1359. 'allocated_salary' => $allocated_salary,
  1360. 'month' => $item['order_month'],
  1361. 'item_id' => $item['item_id'],
  1362. ];
  1363. }
  1364. $collect = collect($item_list);
  1365. $employee_count = $collect->groupBy(function ($item) {
  1366. // 这里的 $item 是集合中的每一行数据
  1367. return $item['employee_id'] . '_' . $item['month'];
  1368. })->map(function ($group) {
  1369. return $group->count();
  1370. })->toArray();
  1371. $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
  1372. // 如果不是最后一条
  1373. $key = $item['employee_id'] . '_' . $item['month'];
  1374. if(!isset($all_salary[$key])) $item['allocated_salary'] = 0;
  1375. else {
  1376. if (--$employee_count[$key] > 0) {
  1377. $all_salary[$key] -= $item['allocated_salary'];
  1378. } else {
  1379. $item['allocated_salary'] = $all_salary[$key];
  1380. }
  1381. }
  1382. return $item;
  1383. })->all();
  1384. $return_item_list = [];
  1385. foreach ($month_employee_list as $v){
  1386. $key = $v['item_id'] . '_' . $v['month'];
  1387. if(!isset($return_item_list[$key])) $return_item_list[$key]['allocated_salary'] = 0;
  1388. $return_item_list[$key]['allocated_salary'] += $v['allocated_salary'];
  1389. }
  1390. return $return_item_list;
  1391. }
  1392. public function auxiliaryDevice($user, $data, $month_start, $month_end)
  1393. {
  1394. $month_device = DailyDwOrderDetails::Clear($user, $data);
  1395. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1396. ->where('del_time', 0)
  1397. ->select(
  1398. "item_id",
  1399. "device_id",
  1400. // 将时间戳转为 Y-m-d 格式并起别名
  1401. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1402. // 聚合求和
  1403. DB::raw("SUM(total_work_min) as total_work")
  1404. )
  1405. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  1406. //查询所有人员工资
  1407. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1408. ->pluck('id')->toArray();
  1409. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1410. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1411. ->pluck("month_str", 'id')->toArray();
  1412. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  1413. ->select("device_id", "depreciation_amount", "main_id")
  1414. ->get()->toArray();
  1415. //查询所有项目人员的工时比例
  1416. //汇总每个人每个月工资
  1417. $depreciatio_map = [];
  1418. $all_depreciatio = [];
  1419. foreach ($month_device_salary as $val) {
  1420. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  1421. if ($month) {
  1422. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  1423. $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
  1424. }
  1425. }
  1426. // 2. 计算每个员工在每个月的全月总工时
  1427. $device_monthly_total_min = [];
  1428. foreach ($month_device_list as $row) {
  1429. $key = $row['device_id'] . '_' . $row['order_month'];
  1430. if (!isset($device_monthly_total_min[$key])) {
  1431. $device_monthly_total_min[$key] = 0;
  1432. }
  1433. $device_monthly_total_min[$key] += $row['total_work'];
  1434. }
  1435. // 3. 计算分摊天数与工资
  1436. $item_year_list = [];
  1437. $item_list = [];
  1438. foreach ($month_device_list as $item) {
  1439. $key = $item['device_id'] . '_' . $item['order_month'];
  1440. // $device_key = $item['item_id'] . '_' . $item['order_month'];
  1441. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  1442. $total_min = $device_monthly_total_min[$key] ?? 0;
  1443. // if (!isset($item_year_list[$device_key])) {
  1444. // $item_year_list[$device_key] = [
  1445. // "allocated_depreciation" => 0,
  1446. // ];
  1447. // }
  1448. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1449. if ($total_min > 0) {
  1450. $ratio = round($item['total_work'] / $total_min, 3);
  1451. $allocated_salary = round($ratio * $total_depreciatio, 2)*100;
  1452. } else {
  1453. $allocated_salary = 0;
  1454. }
  1455. // $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
  1456. $item_list[] = [
  1457. 'device_id' => $item['device_id'],
  1458. 'depreciation' => $allocated_salary,
  1459. 'month' => $item['order_month'],
  1460. 'item_id' => $item['item_id'],
  1461. ];
  1462. }
  1463. $collect = collect($item_list);
  1464. $device_count = $collect->groupBy(function ($item) {
  1465. // 这里的 $item 是集合中的每一行数据
  1466. return $item['device_id'] . '_' . $item['month'];
  1467. })->map(function ($group) {
  1468. return $group->count();
  1469. })->toArray();
  1470. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
  1471. // 如果不是最后一条
  1472. $key = $item['device_id'] . '_' . $item['month'];
  1473. if (--$device_count[$key] > 0) {
  1474. $all_depreciatio[$key] -= $item['depreciation'];
  1475. } else {
  1476. $item['depreciation'] = $all_depreciatio[$key];
  1477. }
  1478. return $item;
  1479. })->all();
  1480. $return_item_list = [];
  1481. foreach ($month_employee_list as $v){
  1482. $key = $v['item_id']. '_' . $v['month'];
  1483. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  1484. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  1485. }
  1486. // dd($return_item_list);
  1487. return $return_item_list;
  1488. }
  1489. //原方法----
  1490. public function auxiliaryStatistic1($data, $user)
  1491. {
  1492. list($status, $month_start, $month_end) = $this->commonRule($data);
  1493. if (!$status) return [false, $month_start];
  1494. //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
  1495. //确认所有项目
  1496. $item = Item::TopClear($user, $data);
  1497. $item_list = $item->where('del_time', 0)
  1498. ->where(function ($query) use ($month_start, $month_end) {
  1499. $query->where('start_time', '>=', $month_start)
  1500. ->where('end_time', '<', $month_end);
  1501. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
  1502. $item_key_list = [];
  1503. foreach ($item_list as $v) {
  1504. $item_key_list[$v['id']] = $v;
  1505. }
  1506. //获取该区间内所有项目人工费、折旧费
  1507. $item_salary = $this->auxiliaryEmployee($user, $data, $month_start, $month_end);
  1508. $device_depreciation = $this->auxiliaryDevice($user, $data, $month_start, $month_end);
  1509. $fee = Fee::TopClear($user, $data);
  1510. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  1511. $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
  1512. $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  1513. ->where('del_time', 0)
  1514. ->select(
  1515. "item_id",
  1516. "voucher_date",
  1517. "voucher_type",
  1518. "voucher_no",
  1519. "voucher_remark",
  1520. "voucher_amount",
  1521. "aggregation_amount",
  1522. "entrust_type",
  1523. "entrust1_amount",
  1524. "entrust2_amount",
  1525. "entrust2_amount",
  1526. "total_amount",
  1527. "fee_id",
  1528. "type"
  1529. )->get()->toArray();
  1530. list($fee_amount, $fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list, $fee);
  1531. //找到项目和设备的费用然后进行比例计算
  1532. $return = [];
  1533. foreach ($fee_amount as $v) {
  1534. //人工费用
  1535. if ($v['type'] == 1 || $v['type'] == 2) {
  1536. // var_dump();
  1537. foreach ($item_key_list as $vv) {
  1538. if(!isset($item_salary[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_salary'])) continue;
  1539. $detail = [
  1540. "code" => $vv['code'],
  1541. "title" => $vv['title'],
  1542. "state" => $vv['state'] == 3 ? "进行中" : "已完成",
  1543. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  1544. "voucher_type" => $v['voucher_type'],
  1545. "voucher_no" => $v['voucher_no'],
  1546. "voucher_remark" => $v['voucher_remark'],
  1547. "voucher_amount" => $v['voucher_amount'],
  1548. "aggregation_amount" => $v['aggregation_amount'],
  1549. "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),
  1550. "fee_id" => $v['fee_id'],
  1551. "entrust1_amount" => $v['entrust1_amount'],
  1552. "entrust2_amount" => $v['entrust2_amount'],
  1553. "type" => $v['type'],
  1554. ];
  1555. $return[] = $detail;
  1556. }
  1557. } else {
  1558. if (!isset($item_key_list[$v['item_id']])) continue;
  1559. $item_value = $item_key_list[$v['item_id']];
  1560. $detail = [
  1561. "code" => $item_value['code'],
  1562. "title" => $item_value['title'],
  1563. "state" => $item_value['state'] == 3 ? "进行中" : "已完成",
  1564. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  1565. "voucher_type" => $v['voucher_type'],
  1566. "voucher_no" => $v['voucher_no'],
  1567. "voucher_remark" => $v['voucher_remark'],
  1568. "voucher_amount" => $v['voucher_amount'],
  1569. "aggregation_amount" => $v['aggregation_amount'],
  1570. "total_amount" => $v['total_amount'],
  1571. "fee_id" => $v['fee_id'],
  1572. "entrust1_amount" => $v['entrust1_amount'],
  1573. "entrust2_amount" => $v['entrust2_amount'],
  1574. "type" => $v['type'],
  1575. ];
  1576. $return[] = $detail;
  1577. }
  1578. }
  1579. return [true, [
  1580. 'fee_type_list' => $fee_type_list,
  1581. 'list' => $return,
  1582. ]];
  1583. }
  1584. public function auxiliaryEmployee1($user, $data, $month_start, $month_end)
  1585. {
  1586. // 1. 获取工时明细
  1587. $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
  1588. ->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1589. ->where('del_time', 0)
  1590. ->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"))
  1591. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  1592. // 2. 获取工资并转为“分”
  1593. $monthly_ps_order_query = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)
  1594. ->where("month", ">=", $month_start)->where("month", "<", $month_end);
  1595. $monthly_ps_order_ids = $monthly_ps_order_query->pluck('id')->toArray();
  1596. $month_map = $monthly_ps_order_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1597. ->pluck('month_str', 'id')->toArray();
  1598. $salary_details = MonthlyPsOrderDetails::whereIn('main_id', $monthly_ps_order_ids)
  1599. ->select("employee_id", "main_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"))
  1600. ->get();
  1601. $salary_pool = []; // [emp_month => salary_cents]
  1602. foreach ($salary_details as $val) {
  1603. $m = $month_map[$val['main_id']] ?? '';
  1604. if ($m) {
  1605. $k = $val['employee_id'] . '_' . $m;
  1606. $salary_pool[$k] = (int)round($val['salary'] * 100);
  1607. }
  1608. }
  1609. // 3. 计算个人月总工时及项目计数
  1610. $emp_total_min = [];
  1611. $emp_item_count = [];
  1612. foreach ($month_employee_list as $row) {
  1613. $k = $row['employee_id'] . '_' . $row['order_month'];
  1614. $emp_total_min[$k] = ($emp_total_min[$k] ?? 0) + $row['total_work'];
  1615. $emp_item_count[$k] = ($emp_item_count[$k] ?? 0) + 1;
  1616. }
  1617. // 4. 分摊(保留整数分)
  1618. $rem_salary = $salary_pool;
  1619. $return_item_list = [];
  1620. foreach ($month_employee_list as $item) {
  1621. $k = $item['employee_id'] . '_' . $item['order_month'];
  1622. $total_s = $salary_pool[$k] ?? 0;
  1623. $total_m = $emp_total_min[$k] ?? 0;
  1624. if ($total_m > 0 && --$emp_item_count[$k] > 0) {
  1625. // 非最后一个项目,取地板值避免超支
  1626. $allocated = (int)floor(($item['total_work'] / $total_m) * $total_s);
  1627. $rem_salary[$k] -= $allocated;
  1628. } else {
  1629. // 最后一个项目,取池子剩余所有钱
  1630. $allocated = $rem_salary[$k] ?? 0;
  1631. }
  1632. $res_key = $item['item_id'] . '_' . $item['order_month'];
  1633. $return_item_list[$res_key]['allocated_salary'] = ($return_item_list[$res_key]['allocated_salary'] ?? 0) + $allocated;
  1634. }
  1635. return $return_item_list;
  1636. }
  1637. public function auxiliaryDevice1($user, $data, $month_start, $month_end)
  1638. {
  1639. // 1. 获取设备工时
  1640. $month_device_list = DailyDwOrderDetails::Clear($user, $data)
  1641. ->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)->where('del_time', 0)
  1642. ->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"))
  1643. ->groupBy("order_month", "item_id", "device_id")->get()->toArray();
  1644. // 2. 获取折旧费转为“分”
  1645. $monthly_dd_query = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)
  1646. ->where("month", ">=", $month_start)->where("month", "<", $month_end);
  1647. $monthly_dd_ids = $monthly_dd_query->pluck('id')->toArray();
  1648. $month_map = $monthly_dd_query->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1649. ->pluck("month_str", 'id')->toArray();
  1650. $device_details = MonthlyDdOrderDetails::whereIn('main_id', $monthly_dd_ids)
  1651. ->select("device_id", "depreciation_amount", "main_id")->get();
  1652. $dep_pool = [];
  1653. foreach ($device_details as $val) {
  1654. $m = $month_map[$val['main_id']] ?? '';
  1655. if ($m) {
  1656. $k = $val['device_id'] . '_' . $m;
  1657. $dep_pool[$k] = (int)round($val['depreciation_amount'] * 100);
  1658. }
  1659. }
  1660. // 3. 统计设备月总工时及项目数
  1661. $dev_total_min = [];
  1662. $dev_item_count = [];
  1663. foreach ($month_device_list as $row) {
  1664. $k = $row['device_id'] . '_' . $row['order_month'];
  1665. $dev_total_min[$k] = ($dev_total_min[$k] ?? 0) + $row['total_work'];
  1666. $dev_item_count[$k] = ($dev_item_count[$k] ?? 0) + 1;
  1667. }
  1668. // 4. 分摊
  1669. $rem_dep = $dep_pool;
  1670. $return_item_list = [];
  1671. foreach ($month_device_list as $item) {
  1672. $k = $item['device_id'] . '_' . $item['order_month'];
  1673. $total_d = $dep_pool[$k] ?? 0;
  1674. $total_m = $dev_total_min[$k] ?? 0;
  1675. if ($total_m > 0 && --$dev_item_count[$k] > 0) {
  1676. $allocated = (int)floor(($item['total_work'] / $total_m) * $total_d);
  1677. $rem_dep[$k] -= $allocated;
  1678. } else {
  1679. $allocated = $rem_dep[$k] ?? 0;
  1680. }
  1681. $res_key = $item['item_id'] . '_' . $item['order_month'];
  1682. $return_item_list[$res_key]['depreciation'] = ($return_item_list[$res_key]['depreciation'] ?? 0) + $allocated;
  1683. }
  1684. return $return_item_list;
  1685. }
  1686. /**
  1687. * 将报销明细按照一级费用和项目分类进行分组
  1688. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  1689. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  1690. * @return array
  1691. */
  1692. public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
  1693. {
  1694. // 1. 建立 ID 索引,方便快速查找
  1695. $idMap = array_column($fee_type_list, null, 'id');
  1696. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  1697. $childToRoot = [];
  1698. foreach ($fee_type_list as $type) {
  1699. $current = $type;
  1700. // 向上追溯直到 parent_id 为 0,即找到一级分类
  1701. while ($current['parent_id'] != 0) {
  1702. $current = $idMap[$current['parent_id']];
  1703. }
  1704. $childToRoot[$type['id']] = [
  1705. 'id' => $current['id'],
  1706. 'title' => $current['title'],
  1707. 'sort' => $current['sort']
  1708. ];
  1709. }
  1710. // 3. 遍历明细数据进行分组
  1711. $type_list = [];
  1712. foreach ($list as $k => $item) {
  1713. if ($item['item_id'] == 0 || $item['fee_id'] == 0) {
  1714. continue;
  1715. }
  1716. $feeId = $item['fee_id'];
  1717. // 获取该费用对应的一级分类信息
  1718. if (!isset($childToRoot[$feeId])) continue;
  1719. $rootId = $childToRoot[$feeId]['id'];
  1720. $title = $childToRoot[$feeId]['title'];
  1721. $sort = $childToRoot[$feeId]['sort'];
  1722. $item['fee_id'] = $rootId;
  1723. $list[$k] = $item;
  1724. //这边需要拿到头部所有的一级费用类型
  1725. if (!isset($type_list[$rootId])) {
  1726. $type_list[$rootId] = [
  1727. 'sort' => $sort,
  1728. 'id' => $rootId,
  1729. 'title' => $title,
  1730. ];
  1731. }
  1732. }
  1733. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  1734. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  1735. // 4. 重置数组索引并返回
  1736. return [$list, $type_list];
  1737. }
  1738. public function itemEmployeeSalaryStatistic($data, $user)
  1739. {
  1740. list($status, $month_start, $month_end) = $this->commonRule($data);
  1741. if (!$status) return [false, $month_start];
  1742. // 1. 获取人员工时记录
  1743. $month_employee_list = DailyPwOrderDetails::Clear($user, $data)
  1744. ->where("order_time", ">=", $month_start)
  1745. ->where("order_time", "<", $month_end)
  1746. ->where('del_time', 0)
  1747. ->select(
  1748. "item_id",
  1749. "employee_id",
  1750. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1751. DB::raw("SUM(total_work_min) as total_work")
  1752. )
  1753. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  1754. // 2. 获取工资及配置
  1755. $monthly_ps_order_query = MonthlyPsOrder::Clear($user, $data)
  1756. ->where('del_time', 0)
  1757. ->where("month", ">=", $month_start)
  1758. ->where("month", "<", $month_end);
  1759. $monthly_ps_order_ids = $monthly_ps_order_query->pluck('id')->toArray();
  1760. $monthly_ps_order_key_list = $monthly_ps_order_query
  1761. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1762. ->pluck('month_str', 'id')->toArray();
  1763. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  1764. ->select("employee_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id", "social_insurance", "public_housing_fund")
  1765. ->get()->toArray();
  1766. // 3. 汇总每个人每个月的工资 (单位:分)
  1767. $salary_map = [];
  1768. $all_salary = [];
  1769. foreach ($month_employee_salary as $val) {
  1770. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  1771. if ($month) {
  1772. $key = $val['employee_id'] . '_' . $month;
  1773. $salary_map[$key] = $val;
  1774. $all_salary[$key] = [
  1775. "salary" => (int)round($val['salary'] * 100),
  1776. "social_insurance" => (int)round($val['social_insurance'] * 100),
  1777. "public_housing_fund" => (int)round($val['public_housing_fund'] * 100),
  1778. ];
  1779. }
  1780. }
  1781. // 4. 计算月总工时 (分钟) 及 余额池初始化 (小时*100)
  1782. $employee_monthly_total_min = [];
  1783. foreach ($month_employee_list as $row) {
  1784. $key = $row['employee_id'] . '_' . $row['order_month'];
  1785. $employee_monthly_total_min[$key] = ($employee_monthly_total_min[$key] ?? 0) + $row['total_work'];
  1786. }
  1787. $all_min = [];
  1788. foreach ($employee_monthly_total_min as $key => $min) {
  1789. // 重要修复:以总分钟数计算出全月总小时数,作为余额池的起点
  1790. $all_min[$key] = (int)round($min / 60, 2) * 100;
  1791. }
  1792. // 5. 初步构建待分摊列表
  1793. $item_month_list = [];
  1794. foreach ($month_employee_list as $item) {
  1795. $key = $item['employee_id'] . '_' . $item['order_month'];
  1796. $item_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  1797. $total_min = $employee_monthly_total_min[$key] ?? 0;
  1798. $ratio = $total_min > 0 ? ($item['total_work'] / $total_min) : 0;
  1799. $item_month_list[$item_key] = [
  1800. "month" => $item['order_month'],
  1801. "employee_id" => $item['employee_id'],
  1802. "item_id" => $item['item_id'],
  1803. "salary" => $salary_map[$key]['salary'] ?? 0,
  1804. "social_insurance" => $salary_map[$key]['social_insurance'] ?? 0,
  1805. "public_housing_fund" => $salary_map[$key]['public_housing_fund'] ?? 0,
  1806. "radio" => round($ratio, 4),
  1807. "total_min_raw" => $total_min, // 保留原始分钟,不在这里修改
  1808. "work_min_raw" => $item['total_work'],
  1809. ];
  1810. }
  1811. // 6. 获取项目和人员信息
  1812. $items = collect($item_month_list)->pluck('item_id')->unique()->all();
  1813. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->all();
  1814. $employee_key_list = Employee::TopClear($user, $data)->wherein('id', $employee_ids)
  1815. ->select("major", "title", "id")->get()->keyBy('id')->toArray();
  1816. $item_info = Item::TopClear($user, $data)->wherein('id', $items)
  1817. ->select("title", "code", "id")->get()->keyBy('id')->toArray();
  1818. // 7. 统计每人项目数用于平账
  1819. $employee_count = collect($item_month_list)->groupBy(fn($i) => $i['employee_id'].'_'.$i['month'])
  1820. ->map(fn($g) => $g->count())->toArray();
  1821. // 8. 核心转换
  1822. $result = collect($item_month_list)->sortBy('month')->values()->transform(function ($item) use ($item_info, $employee_key_list, &$employee_count, &$all_salary, &$all_min) {
  1823. $key = $item['employee_id'] . '_' . $item['month'];
  1824. $item['item_title'] = $item_info[$item['item_id']]['title'] ?? "未知项目";
  1825. $item['item_code'] = $item_info[$item['item_id']]['code'] ?? "-";
  1826. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员";
  1827. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "-";
  1828. // 应出勤工时:固定展示,不参与递减
  1829. $item['total_min'] = round($item['total_min_raw'] / 60, 2);
  1830. if (isset($employee_count[$key]) && --$employee_count[$key] > 0) {
  1831. // 非最后一条项目
  1832. $work_h = round($item['work_min_raw'] / 60, 2);
  1833. $item['work_minutes'] = $work_h;
  1834. $all_min[$key] -= ($work_h * 100);
  1835. // 分摊工资
  1836. $ws = round($item['salary'] * ($item['work_min_raw'] / $item['total_min_raw']), 2);
  1837. $item['work_salary'] = $ws;
  1838. if (isset($all_salary[$key])) $all_salary[$key]['salary'] -= ($ws * 100);
  1839. // 分摊社保
  1840. $wsi = round($item['social_insurance'] * ($item['work_min_raw'] / $item['total_min_raw']), 2);
  1841. $item['work_social_insurance'] = $wsi;
  1842. if (isset($all_salary[$key])) $all_salary[$key]['social_insurance'] -= ($wsi * 100);
  1843. // 分摊公积金
  1844. $wphf = round($item['public_housing_fund'] * ($item['work_min_raw'] / $item['total_min_raw']), 2);
  1845. $item['work_public_housing_fund'] = $wphf;
  1846. if (isset($all_salary[$key])) $all_salary[$key]['public_housing_fund'] -= ($wphf * 100);
  1847. } else {
  1848. // 最后一条,从余额池取值(解决精度)
  1849. $item['work_salary'] = isset($all_salary[$key]) ? round($all_salary[$key]['salary'] / 100, 2) : 0;
  1850. $item['work_social_insurance'] = isset($all_salary[$key]) ? round($all_salary[$key]['social_insurance'] / 100, 2) : 0;
  1851. $item['work_public_housing_fund'] = isset($all_salary[$key]) ? round($all_salary[$key]['public_housing_fund'] / 100, 2) : 0;
  1852. $item['work_minutes'] = isset($all_min[$key]) ? round($all_min[$key] / 100, 2) : 0;
  1853. }
  1854. unset($item['total_min_raw'], $item['work_min_raw']);
  1855. return $item;
  1856. })->all();
  1857. return [true, $result];
  1858. }
  1859. public function itemEmployeeSalaryStatistic1($data, $user)
  1860. {
  1861. list($status, $month_start, $month_end) = $this->commonRule($data);
  1862. if (!$status) return [false, $month_start];
  1863. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  1864. //获取人员工资项目相关分组数据
  1865. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  1866. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1867. ->where('del_time', 0)
  1868. ->select(
  1869. "item_id",
  1870. "employee_id",
  1871. // 将时间戳转为 Y-m-d 格式并起别名
  1872. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1873. // 聚合求和
  1874. DB::raw("SUM(total_work_min) as total_work")
  1875. )
  1876. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  1877. //查询所有人员工资
  1878. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1879. ->pluck('id')->toArray();
  1880. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  1881. ->where('del_time', 0)
  1882. ->where("month", ">=", $month_start)
  1883. ->where("month", "<", $month_end)
  1884. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1885. ->pluck('month_str', 'id')
  1886. ->toArray();
  1887. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  1888. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id", "social_insurance", "public_housing_fund")
  1889. ->get()->toArray();
  1890. //查询所有项目人员的工时比例
  1891. //汇总每个人每个月工资
  1892. $salary_map = [];
  1893. $all_salary = [];
  1894. foreach ($month_employee_salary as $val) {
  1895. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  1896. if ($month) {
  1897. $salary_map[$val['employee_id'] . '_' . $month] = $val;
  1898. $all_salary[$val['employee_id'] . '_' . $month] = [
  1899. "salary" => $val['salary']*100,
  1900. "social_insurance" => $val['social_insurance']*100,
  1901. "public_housing_fund" => $val['public_housing_fund']*100,
  1902. ];
  1903. }
  1904. }
  1905. // var_dump($salary_map);die;
  1906. // 2. 计算每个员工在每个月的全月总工时
  1907. $employee_monthly_total_min = [];
  1908. $all_min = [];
  1909. foreach ($month_employee_list as $row) {
  1910. $key = $row['employee_id'] . '_' . $row['order_month'];
  1911. if (!isset($employee_monthly_total_min[$key])) {
  1912. $employee_monthly_total_min[$key] = 0;
  1913. $all_min[$key] = 0;
  1914. }
  1915. $employee_monthly_total_min[$key] += $row['total_work'];
  1916. $all_min[$key] = round($row['total_work']/60,2)*100;
  1917. }
  1918. // 3. 计算分摊天数与工资
  1919. $item_month_list = [];
  1920. foreach ($month_employee_list as $item) {
  1921. $key = $item['employee_id'] . '_' . $item['order_month'];
  1922. $item_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  1923. if (!isset($item_month_list[$item_key])) {
  1924. $item_month_list[$item_key] = [
  1925. "month" => $item['order_month'],
  1926. "allocated_salary" => 0,
  1927. "employee_id" => $item['employee_id'],
  1928. "work_minutes" => 0,
  1929. "salary" => ($salary_map[$key]['salary'] ?? 0),
  1930. "social_insurance" => ($salary_map[$key]['social_insurance'] ?? 0),
  1931. "public_housing_fund" => ($salary_map[$key]['public_housing_fund'] ?? 0),
  1932. "item_id" => $item['item_id'],
  1933. ];
  1934. }
  1935. $total_min = $employee_monthly_total_min[$key] ?? 0;
  1936. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1937. if ($total_min > 0) {
  1938. $ratio = round($item['total_work'] / $total_min, 4);
  1939. } else {
  1940. $ratio = 0;
  1941. }
  1942. $item_month_list[$item_key]['radio'] = $ratio;
  1943. $item_month_list[$item_key]['total_min'] = $total_min;
  1944. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  1945. }
  1946. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  1947. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  1948. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  1949. $employee = Employee::TopClear($user, $data);
  1950. $employee_list = $employee->wherein('id', $employee_ids)->select("major", "title", "id")->get()->toArray();
  1951. $employee_key_list = [];
  1952. foreach ($employee_list as $v) {
  1953. $employee_key_list[$v['id']] = $v;
  1954. }
  1955. $item = Item::TopClear($user, $data);
  1956. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  1957. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  1958. $collect = collect($item_month_list);
  1959. $employee_count = $collect->groupBy(function ($item) {
  1960. // 这里的 $item 是集合中的每一行数据
  1961. return $item['employee_id'] . '_' . $item['month'];
  1962. })->map(function ($group) {
  1963. return $group->count();
  1964. })->toArray();
  1965. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  1966. $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) {
  1967. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  1968. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  1969. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['employee_id']})";
  1970. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
  1971. $item['total_min'] = round($item['total_min'] / 60, 2);
  1972. $key = $item['employee_id'] . '_' . $item['month'];
  1973. if (--$employee_count[$key] > 0) {
  1974. $work_minutes = round($item['work_minutes'] / 60, 2);
  1975. $item['work_minutes'] = $work_minutes;
  1976. $all_min[$key] -= ($work_minutes*100);
  1977. $work_salary = round($item['salary'] * $item['radio'], 2);
  1978. $all_salary[$key]['salary'] -= ($work_salary*100);
  1979. $item['work_salary'] = $work_salary;
  1980. $social_insurance = round($item['social_insurance'] * $item['radio'], 2);
  1981. $all_salary[$key]['social_insurance'] -= ($social_insurance*100);
  1982. $item['work_social_insurance'] = $social_insurance;
  1983. $work_public_housing_fund = round($item['public_housing_fund'] * $item['radio'], 2);
  1984. $all_salary[$key]['public_housing_fund'] -= ($work_public_housing_fund*100);
  1985. $item['work_public_housing_fund'] = $work_public_housing_fund;
  1986. } else {
  1987. $item['work_salary'] = round($all_salary[$key]['salary']/100,2);
  1988. $item['work_social_insurance'] = round($all_salary[$key]['social_insurance'] /100,2);
  1989. $item['work_public_housing_fund'] = round( $all_salary[$key]['public_housing_fund']/100,2);
  1990. $item['work_minutes'] = round( $all_min[$key]/100,2);
  1991. }
  1992. return $item;
  1993. })->all();
  1994. return [true, $item_month_list];
  1995. }
  1996. public function enterpriseRdStatistic($data, $user){
  1997. $model = Item::TopClear($user,$data);
  1998. $model = $model->where('del_time',0)
  1999. ->select(Item::$report_field_1)
  2000. ->orderby('id', 'desc');
  2001. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  2002. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  2003. if(! empty($data['state'])) $model->where('state', $data['state']);
  2004. $list = $model->get()->toArray();
  2005. $list = $this->fillEnterpriseRdStatistic($list, $data, $user);
  2006. return [true, $list];
  2007. }
  2008. private function fillEnterpriseRdStatistic($list, $data, $user){
  2009. //项目实际支出 项目费用单
  2010. $expense = ExpenseClaimsDetails::Clear($user, $data);
  2011. $expense_map = $expense->where('del_time', 0)
  2012. ->whereIn('item_id', array_unique(array_column($list,'id')))
  2013. ->selectRaw('item_id, SUM(amount) as total_amount')
  2014. ->groupBy('item_id')
  2015. ->pluck('total_amount', 'item_id') // 这一步直接生成 item_id => total_amount 结构
  2016. ->toArray();
  2017. foreach ($list as $key => $value){
  2018. $list[$key]['actual_expenditure'] = $expense_map[$value['id']] ?? 0;
  2019. $start_time = $value['start_time'] ? date('Y-m-d', $value['start_time']) : '';
  2020. $end_time = $value['end_time'] ? date('Y-m-d', $value['end_time']) : '';
  2021. $list[$key]['time_range'] = $start_time . ' ' . $end_time;
  2022. }
  2023. return $list;
  2024. }
  2025. public function enterpriseRdManStatistic($data, $user){
  2026. $model = Employee::TopClear($user,$data);
  2027. $model = $model->where('del_time',0)
  2028. ->where('is_admin', '<=', Employee::IS_ADMIN_ONE)
  2029. ->select(Employee::$report_field)
  2030. ->orderBy('id','desc');
  2031. if(! empty($data['id_card'])) $model->where('id_card', 'LIKE', '%'.$data['id_card'].'%');
  2032. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  2033. if(! empty($data['employee_type'])) $model->where('employee_type', $data['employee_type']);
  2034. if(isset($data['education'])) $model->where('education', $data['education']);
  2035. $list = $model->get()->toArray();
  2036. $list = $this->fillEnterpriseRdManStatistic($list, $data, $user);
  2037. return [true, $list];
  2038. }
  2039. private function fillEnterpriseRdManStatistic($list, $data, $user){
  2040. $man = EmployeeDepartPermission::from('employee_depart_permission as a')
  2041. ->join('depart as b', 'b.id', '=', 'a.depart_id')
  2042. ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
  2043. ->select('a.employee_id', 'b.title')
  2044. ->get()->toArray();
  2045. $man_map = [];
  2046. foreach ($man as $value){
  2047. if(isset($man_map[$value['employee_id']])){
  2048. $man_map[$value['employee_id']] .= ',' . $value['title'];
  2049. }else{
  2050. $man_map[$value['employee_id']] = $value['title'];
  2051. }
  2052. }
  2053. foreach ($list as $key => $value){
  2054. $depart_title = $man_map[$value['id']] ?? "";
  2055. $list[$key]['position_new'] = $depart_title . '/' . $value['position'];
  2056. $list[$key]['employee_type_title'] = Employee::E_State_Type[$value['employee_type']] ?? '';
  2057. $list[$key]['education'] = Employee::Education[$value['education']] ?? '';
  2058. }
  2059. return $list;
  2060. }
  2061. public function enterpriseRdItemStatistic($data, $user){
  2062. // 1. 先声明表名和别名
  2063. $model = ItemDetails::from('item_details as i');
  2064. // 2. 再调用 TopClear
  2065. $model = $model->TopClear($user, $data);
  2066. $model = $model->from('item_details as i')
  2067. ->where('i.del_time', 0)
  2068. ->where('i.type', ItemDetails::type_one);
  2069. $model = $model->leftJoin('employee as e', 'i.data_id', '=', 'e.id');
  2070. $fields = ['e.id', 'e.title', 'e.education', 'e.major','e.p_title','i.item_id'];
  2071. $list = $model->select($fields)
  2072. ->orderBy('i.id', 'desc')
  2073. ->get()
  2074. ->toArray();
  2075. $list = $this->fillEnterpriseRdItemStatistic($list, $data, $user);
  2076. return [true, $list];
  2077. }
  2078. private function fillEnterpriseRdItemStatistic($list, $data, $user){
  2079. $man = EmployeeDepartPermission::from('employee_depart_permission as a')
  2080. ->join('depart as b', 'b.id', '=', 'a.depart_id')
  2081. ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
  2082. ->select('a.employee_id', 'b.title')
  2083. ->get()->toArray();
  2084. $man_map = [];
  2085. foreach ($man as $value){
  2086. if(isset($man_map[$value['employee_id']])){
  2087. $man_map[$value['employee_id']] .= ',' . $value['title'];
  2088. }else{
  2089. $man_map[$value['employee_id']] = $value['title'];
  2090. }
  2091. }
  2092. $item_map = Item::whereIn('id',array_unique(array_column($list,'item_id')))
  2093. ->pluck('title','id')
  2094. ->toArray();
  2095. foreach ($list as $key => $value){
  2096. $depart_title = $man_map[$value['id']] ?? "";
  2097. $list[$key]['depart_title'] = $depart_title;
  2098. $item_title = $item_map[$value['item_id']] ?? "";
  2099. $list[$key]['item_title'] = $item_title;
  2100. $list[$key]['education'] = Employee::Education[$value['education']] ?? '';
  2101. }
  2102. return $list;
  2103. }
  2104. }