StatisticService.php 94 KB

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