StatisticService.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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 employeeMonthSalaryStatistic($data, $user)
  90. {
  91. //项目编码、项目名称、天数、工资、日期
  92. list($status, $month_start, $month_end) = $this->commonRule($data);
  93. if (!$status) return [false, $month_start];
  94. //确认所有项目、人员、人员工时
  95. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  96. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  97. ->where('del_time', 0)
  98. ->select(
  99. "item_id",
  100. "employee_id",
  101. // 将时间戳转为 Y-m-d 格式并起别名
  102. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  103. // 聚合求和
  104. DB::raw("SUM(total_work_min) as total_work")
  105. )
  106. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  107. //查询所有人员工资
  108. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  109. ->pluck('id')->toArray();
  110. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  111. ->where('del_time', 0)
  112. ->where("month", ">=", $month_start)
  113. ->where("month", "<", $month_end)
  114. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  115. ->pluck('month_str', 'id')
  116. ->toArray();
  117. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  118. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  119. ->get()->toArray();
  120. //查询所有项目人员的工时比例
  121. //汇总每个人每个月工资
  122. $salary_map = [];
  123. $all_salary = [];
  124. foreach ($month_employee_salary as $val) {
  125. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  126. if ($month) {
  127. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  128. }
  129. if (!isset($all_salary[$month])) $all_salary[$month] = 0;
  130. $all_salary[$month] += $val['salary'] * 100;
  131. }
  132. // var_dump($salary_map);die;
  133. // 2. 计算每个员工在每个月的全月总工时
  134. $employee_monthly_total_min = [];
  135. foreach ($month_employee_list as $row) {
  136. $key = $row['employee_id'] . '_' . $row['order_month'];
  137. if (!isset($employee_monthly_total_min[$key])) {
  138. $employee_monthly_total_min[$key] = 0;
  139. }
  140. $employee_monthly_total_min[$key] += $row['total_work'];
  141. }
  142. // 3. 计算分摊天数与工资
  143. $item_month_list = [];
  144. foreach ($month_employee_list as $item) {
  145. $key = $item['employee_id'] . '_' . $item['order_month'];
  146. $item_key = $item['order_month'] . '_' . $item['item_id'];
  147. if (!isset($item_month_list[$item_key])) {
  148. $item_month_list[$item_key] = [
  149. "month" => $item['order_month'],
  150. "allocated_salary" => 0,
  151. "work_minutes" => 0,
  152. "item_id" => $item['item_id'],
  153. ];
  154. }
  155. $total_salary = $salary_map[$key] ?? 0;
  156. $total_min = $employee_monthly_total_min[$key] ?? 0;
  157. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  158. if ($total_min > 0) {
  159. $ratio = $item['total_work'] / $total_min;
  160. $allocated_salary = round($ratio * $total_salary, 2);
  161. } else {
  162. $allocated_salary = 0;
  163. }
  164. $item_month_list[$item_key]['allocated_salary'] += $allocated_salary;
  165. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  166. }
  167. foreach ($item_month_list as $k => $v) {
  168. $item_month_list[$k]['days'] = round($v['work_minutes'] / 8 / 60);
  169. }
  170. $collect = collect($item_month_list);
  171. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  172. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  173. $item = Item::TopClear($user, $data);
  174. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  175. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  176. $collect = collect($item_month_list);
  177. $item_count = $collect->groupBy(function ($item) {
  178. // 这里的 $item 是集合中的每一行数据
  179. return $item['month'];
  180. })->map(function ($group) {
  181. return $group->count();
  182. })->toArray();
  183. $item_day_count = $collect->groupBy(fn($item) => $item['month'])
  184. ->map(fn($group) => round($group->sum('work_minutes') / 8 / 60))
  185. ->toArray();
  186. $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) {
  187. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  188. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  189. $item['allocated_salary'] = round($item['allocated_salary'], 2);
  190. // 如果不是最后一条
  191. $key = $item['month'];
  192. if (--$item_count[$key] > 0) {
  193. // 关键:递减外部的 $absolute_total_hours
  194. $item_day_count[$key] -= $item['days'];
  195. $all_salary[$key] -= $item['allocated_salary'] * 100;
  196. } else {
  197. $item['days'] = $item_day_count[$key];
  198. $item['allocated_salary'] = round($all_salary[$key] / 100, 2);
  199. }
  200. // if ($index < $count - 1) {
  201. // $total_day -= $item['days'];
  202. // // 关键:递减外部的 $absolute_total_hours
  203. // $all_salary -= $item['allocated_salary'];
  204. // } else {
  205. // // 最后一条:直接拿剩下的“余额”
  206. // $item['days'] = $total_day;
  207. // $item['allocated_salary'] = $all_salary;
  208. // }
  209. return $item;
  210. })->all();
  211. return [true, $item_month_list];
  212. }
  213. public function itemDaySalaryStatistic($data, $user)
  214. {
  215. //项目编码、项目名称、人员名称、研发工时、研发工资、当月总工时、总计工资、年月
  216. list($status, $month_start, $month_end) = $this->commonRule($data);
  217. if (!$status) return [false, $month_start];
  218. //确认所有项目、人员、人员工时
  219. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  220. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  221. ->where('del_time', 0)
  222. ->select(
  223. "item_id",
  224. "employee_id",
  225. // 将时间戳转为 Y-m-d 格式并起别名
  226. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  227. // 聚合求和
  228. DB::raw("SUM(total_work_min) as total_work")
  229. )
  230. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "employee_id")->get()->toArray();
  231. //查询所有人员工资
  232. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)->pluck('id')->toArray();
  233. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  234. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  235. ->pluck('month_str', 'id')->toArray();
  236. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  237. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  238. ->get()->toArray();
  239. //查询所有项目人员的工时比例
  240. //汇总每个人每个月工资
  241. $salary_map = [];
  242. $all_salary = [];
  243. foreach ($month_employee_salary as $val) {
  244. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  245. if ($month) {
  246. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  247. }
  248. $key = $val['employee_id'] . '_' . $month;
  249. if (!isset($all_salary[$key])) {
  250. $all_salary[$key] = 0;
  251. }
  252. $all_salary[$key] += $val['salary'] * 100;
  253. }
  254. // 2. 计算每个员工在每个月的全月总工时
  255. $employee_monthly_total_min = [];
  256. foreach ($month_employee_list as $row) {
  257. $key = $row['employee_id'] . '_' . $row['order_month'];
  258. if (!isset($employee_monthly_total_min[$key])) {
  259. $employee_monthly_total_min[$key] = 0;
  260. }
  261. $employee_monthly_total_min[$key] += $row['total_work'];
  262. }
  263. // 3. 计算分摊天数与工资
  264. $item_month_list = [];
  265. $total_work_min = [];
  266. foreach ($month_employee_list as $item) {
  267. $key = $item['employee_id'] . '_' . $item['order_month'];
  268. $employee_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  269. $total_salary = $salary_map[$key] ?? 0;
  270. $total_min = $employee_monthly_total_min[$key] ?? 0;
  271. if (!isset($item_month_list[$employee_key])) {
  272. $item_month_list[$employee_key] = [
  273. "month" => $item['order_month'],
  274. "allocated_salary" => 0,
  275. "work_minutes" => 0,
  276. "total_min" => $total_min,
  277. "total_salary" => $total_salary,
  278. "item_id" => $item['item_id'],
  279. "employee_id" => $item['employee_id'],
  280. ];
  281. }
  282. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  283. if ($total_min > 0) {
  284. $ratio = $item['total_work'] / $total_min;
  285. $allocated_salary = round($ratio * $total_salary, 2);
  286. } else {
  287. $allocated_salary = 0;
  288. }
  289. $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
  290. $item_month_list[$employee_key]['work_minutes'] += $item['total_work'];
  291. $key = $item['employee_id'] . '_' . $item['order_month'];
  292. if (!isset($total_work_min[$key])) $total_work_min[$key] = 0;
  293. $total_work_min[$key] += $item['total_work'];
  294. }
  295. foreach ($total_work_min as $k => $v) {
  296. $total_work_min[$k] = round($v / 60, 2) * 100;
  297. }
  298. $collect = collect($item_month_list);
  299. $employee_count = $collect->groupBy(function ($item) {
  300. // 这里的 $item 是集合中的每一行数据
  301. return $item['employee_id'] . '_' . $item['month'];
  302. })->map(function ($group) {
  303. return $group->count();
  304. })->toArray();
  305. // $collect = collect($item_month_list);
  306. // $employee_count = $collect->groupBy('employee_id')
  307. // ->map(function ($group) {
  308. // return $group->count(); // 统计每个分组里的数量
  309. // })
  310. // ->toArray();
  311. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  312. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  313. $item = Item::TopClear($user, $data);
  314. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  315. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  316. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  317. $employee = Employee::TopClear($user, $data);
  318. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  319. $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) {
  320. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  321. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  322. $item['employee_title'] = $employee_key_list[$item['employee_id']] ?? "未知人员({$item['employee_id']})";
  323. $item['work_hours'] = round($item['work_minutes'] / 60, 2);
  324. $item['total_hours'] = round($item['total_min'] / 60);
  325. $key = $item['employee_id'] . '_' . $item['month'];
  326. if (--$employee_count[$key] > 0) {
  327. $all_salary[$key] -= $item['allocated_salary'] * 100;
  328. $total_work_min[$key] -= $item['work_hours'] * 100;
  329. } else {
  330. $item['allocated_salary'] = round($all_salary[$key] / 100, 2);
  331. $item['work_hours'] = round($total_work_min[$key] / 100, 2);
  332. }
  333. return $item;
  334. })->all();
  335. return [true, $item_month_list];
  336. }
  337. public function itemDeviceMonthStatistic($data, $user)
  338. {
  339. //项目编码、项目名称、设备名称、项目工时、研发工时占比、设备原值、设备折旧额、本项目帐面归集的折旧额、确定的本项目折旧额、加计调整金额、当月工时、日期
  340. list($status, $month_start, $month_end) = $this->commonRule($data);
  341. if (!$status) return [false, $month_start];
  342. //确认所有项目、设备、设备工时
  343. $month_device = DailyDwOrderDetails::Clear($user, $data);
  344. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  345. ->where('del_time', 0)
  346. ->select(
  347. "item_id",
  348. "device_id",
  349. // 将时间戳转为 Y-m-d 格式并起别名
  350. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  351. // 聚合求和
  352. DB::raw("SUM(total_work_min) as total_work")
  353. )
  354. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  355. //查询所有人员工资
  356. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  357. ->pluck('id')->toArray();
  358. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  359. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  360. ->pluck("month_str", 'id')->toArray();
  361. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  362. ->select("device_id", "depreciation_amount", "main_id")
  363. ->get()->toArray();
  364. //查询所有项目人员的工时比例
  365. //汇总每个人每个月工资
  366. $depreciatio_map = [];
  367. foreach ($month_device_salary as $val) {
  368. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  369. if ($month) {
  370. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  371. }
  372. }
  373. // 2. 计算每个员工在每个月的全月总工时
  374. $device_monthly_total_min = [];
  375. foreach ($month_device_list as $row) {
  376. $key = $row['device_id'] . '_' . $row['order_month'];
  377. if (!isset($device_monthly_total_min[$key])) {
  378. $device_monthly_total_min[$key] = 0;
  379. }
  380. $device_monthly_total_min[$key] += $row['total_work'];
  381. }
  382. // 3. 计算分摊天数与工资
  383. $item_month_list = [];
  384. $device_total_depreciatio = [];
  385. foreach ($month_device_list as $item) {
  386. $key = $item['device_id'] . '_' . $item['order_month'];
  387. $device_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['device_id'];
  388. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  389. $total_min = $device_monthly_total_min[$key] ?? 0;
  390. if (!isset($item_month_list[$device_key])) {
  391. $item_month_list[$device_key] = [
  392. "month" => $item['order_month'],
  393. "allocated_depreciatio" => 0,
  394. "work_minutes" => 0,
  395. "total_min" => $total_min,
  396. "item_id" => $item['item_id'],
  397. "device_id" => $item['device_id'],
  398. "total_depreciatio" => $total_depreciatio,
  399. ];
  400. $key = $item['device_id'] . '_' . $item['order_month'];
  401. $device_total_depreciatio[$key] = [
  402. 'total_hours' => round($total_min/60,2)*100,
  403. 'total_depreciatio' => $total_depreciatio*100
  404. ];
  405. }
  406. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  407. if ($total_min > 0) {
  408. $ratio = round($item['total_work'] / $total_min, 3);
  409. $allocated_salary = round($ratio * $total_depreciatio, 2);
  410. } else {
  411. $ratio = 0;
  412. $allocated_salary = 0;
  413. }
  414. $item_month_list[$device_key]['allocated_depreciatio'] += $allocated_salary;
  415. $item_month_list[$device_key]['work_minutes'] += $item['total_work'];
  416. $item_month_list[$device_key]['ratio'] = $ratio;
  417. }
  418. foreach ($item_month_list as $k => $v) {
  419. $item_month_list[$k]['total_hours'] = round($v['total_min'] / 60, 1);
  420. $item_month_list[$k]['hours'] = round($v['work_minutes'] / 60, 1);
  421. unset($item_month_list[$k]['work_minutes']);
  422. }
  423. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  424. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  425. $item = Item::TopClear($user, $data);
  426. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  427. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  428. $device_ids = collect($item_month_list)->pluck('device_id')->unique()->values()->all();
  429. $device = Device::TopClear($user, $data);
  430. $device_key_list = $device->wherein('id', $device_ids)->pluck("title", "id")->toArray();
  431. $device_original_value_key_list = $device->wherein('id', $device_ids)->pluck("original_value", "id")->toArray();
  432. $collect = collect($item_month_list);
  433. $device_count = $collect->groupBy(function ($item) {
  434. // 这里的 $item 是集合中的每一行数据
  435. return $item['device_id'] . '_' . $item['month'];
  436. })->map(function ($group) {
  437. return $group->count();
  438. })->toArray();
  439. $rate_list = [];
  440. $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) {
  441. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  442. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  443. $item['device_title'] = $device_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  444. $item['device_original'] = $device_original_value_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  445. $key = $item['device_id'] . '_' . $item['month'];
  446. if(!isset($rate_list[$key])) $rate_list[$key] = 100;
  447. if (--$device_count[$key] > 0) {
  448. $rate_list[$key] -= $item['ratio'] * 100;
  449. $device_total_depreciatio[$key]['total_hours'] -= $item['hours'] * 100;
  450. $device_total_depreciatio[$key]['total_depreciatio'] -= $item['allocated_depreciatio'] * 100;
  451. } else {
  452. $item['ratio'] = round($rate_list[$key] / 100, 2);
  453. $item['hours'] = round( $device_total_depreciatio[$key]['total_hours']/ 100, 2);
  454. $item['allocated_depreciatio'] = round( $device_total_depreciatio[$key]['total_depreciatio']/ 100, 2);
  455. }
  456. return $item;
  457. })->all();
  458. return [true, $item_month_list];
  459. }
  460. /**
  461. * 根据前端 ISO 时间字符串获取该年份的起止日期和时间戳
  462. * 适配:2019-12-31T16:00:00.000Z 这种带时区的数据
  463. *
  464. * @param string $isoStr 前端传来的时间字符串
  465. * @return array|null
  466. */
  467. public function getYearRangeInfo($isoStr)
  468. {
  469. if (empty($isoStr)) return null;
  470. try {
  471. // 1. 解析 ISO 8601 字符串
  472. $date = new \DateTime($isoStr);
  473. // 2. 强制转为中国时区(PRC),处理 16:00:00Z 这种 UTC 偏移
  474. $date->setTimezone(new \DateTimeZone('PRC'));
  475. // 3. 提取年份
  476. $year = $date->format('Y');
  477. // 4. 构造日期字符串
  478. $startDate = $year . "-01-01";
  479. $endDate = $year . "-12-31";
  480. return [
  481. $startDate,
  482. $endDate,
  483. ];
  484. } catch (\Exception $e) {
  485. // var_dump($e->getMessage());die;
  486. return null;
  487. }
  488. }
  489. public function employeeAttendanceMonthStatistic($data, $user)
  490. {
  491. //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
  492. list($status, $month_start, $month_end) = $this->commonRule($data);
  493. if (!$status) {
  494. return [false, $month_start];
  495. }
  496. //第一步确定项目
  497. $item = Item::TopClear($user, $data);
  498. $item_list = $item->where('del_time', 0)
  499. ->where(function ($query) use ($month_start, $month_end) {
  500. $query->where('start_time', '>=', $month_start)
  501. ->where('end_time', '<', $month_end);
  502. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
  503. $item_key_list = [];
  504. foreach ($item_list as $v) {
  505. $item_key_list[$v['id']] = $v;
  506. }
  507. //第二步确定人员费用
  508. $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
  509. //第三步确定折旧费用
  510. $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
  511. //第四步其他费用
  512. list($item_fee_list, $fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  513. //组合所有数据
  514. $return = [];
  515. foreach ($item_key_list as $v) {
  516. //其他费用是个数组
  517. $item_value = [
  518. "code" => $v['code'],
  519. "title" => $v['title'],
  520. "state" => $v['state'] == 3 ? "完结" : "进行中",
  521. "employee_salary" => isset($item_employee_list[$v['id']]['salary']) ? round($item_employee_list[$v['id']]['salary']/100,2) : 0,
  522. "device_depreciation" => isset($item_device_list[$v['id']]['depreciation']) ? round($item_device_list[$v['id']]['depreciation']/100,2): 0,
  523. "expense_type" => "费用化支出",
  524. "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
  525. ];
  526. $return[] = $item_value;
  527. }
  528. return [true, ["list" => $return, "fee_type_list" => $fee_type_list]];
  529. }
  530. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  531. {
  532. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  533. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  534. ->where('del_time', 0)
  535. ->select(
  536. "item_id",
  537. "employee_id",
  538. // 将时间戳转为 Y-m-d 格式并起别名
  539. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  540. // 聚合求和
  541. DB::raw("SUM(total_work_min) as total_work")
  542. )
  543. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  544. //查询所有人员工资
  545. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  546. ->pluck('id')->toArray();
  547. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  548. ->where('del_time', 0)
  549. ->where("month", ">=", $month_start)
  550. ->where("month", "<", $month_end)
  551. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  552. ->pluck('month_str', 'id')
  553. ->toArray();
  554. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  555. ->select("employee_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  556. ->get()->toArray();
  557. //查询所有项目人员的工时比例
  558. //汇总每个人每个月工资
  559. $salary_map = [];
  560. $all_salary = [];
  561. foreach ($month_employee_salary as $val) {
  562. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  563. if ($month) {
  564. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  565. $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
  566. }
  567. }
  568. // 2. 计算每个员工在每个月的全月总工时
  569. $employee_monthly_total_min = [];
  570. foreach ($month_employee_list as $row) {
  571. $key = $row['employee_id'] . '_' . $row['order_month'];
  572. if (!isset($employee_monthly_total_min[$key])) {
  573. $employee_monthly_total_min[$key] = 0;
  574. }
  575. $employee_monthly_total_min[$key] += $row['total_work'];
  576. }
  577. // 2. 计算分摊天数与工资
  578. $item_list = [];
  579. foreach ($month_employee_list as $item) {
  580. $key = $item['employee_id'] . '_' . $item['order_month'];
  581. // $item_key = $item['item_id'].'_'.$item['order_month'];
  582. // if (!isset($item_list[$item_key])) {
  583. // $item_list[$item_key] = [
  584. // "salary" => 0,
  585. // ];
  586. // }
  587. $total_salary = $salary_map[$key] ?? 0;
  588. $total_min = $employee_monthly_total_min[$key] ?? 0;
  589. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  590. if ($total_min > 0) {
  591. $ratio = $item['total_work'] / $total_min;
  592. $allocated_salary = round($ratio * $total_salary, 2)*100;
  593. } else {
  594. $allocated_salary = 0;
  595. }
  596. $item_list[] = [
  597. 'employee_id' => $item['employee_id'],
  598. 'allocated_salary' => $allocated_salary,
  599. 'month' => $item['order_month'],
  600. 'item_id' => $item['item_id'],
  601. ];
  602. }
  603. $collect = collect($item_list);
  604. $employee_count = $collect->groupBy(function ($item) {
  605. // 这里的 $item 是集合中的每一行数据
  606. return $item['employee_id'] . '_' . $item['month'];
  607. })->map(function ($group) {
  608. return $group->count();
  609. })->toArray();
  610. $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
  611. // 如果不是最后一条
  612. $key = $item['employee_id'] . '_' . $item['month'];
  613. if(!isset($all_salary[$key])) $item['allocated_salary'] = 0;
  614. else{
  615. if (--$employee_count[$key] > 0) {
  616. $all_salary[$key] -= $item['allocated_salary'];
  617. } else {
  618. $item['allocated_salary'] = $all_salary[$key];
  619. }
  620. }
  621. return $item;
  622. })->all();
  623. $return_item_list = [];
  624. foreach ($month_employee_list as $v){
  625. $key = $v['item_id'];
  626. if(!isset($return_item_list[$key])) $return_item_list[$key]['salary'] = 0;
  627. $return_item_list[$key]['salary'] += $v['allocated_salary'];
  628. }
  629. return $return_item_list;
  630. }
  631. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  632. {
  633. //确认所有项目、设备、设备工时
  634. $month_device = DailyDwOrderDetails::Clear($user, $data);
  635. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  636. ->where('del_time', 0)
  637. ->select(
  638. "item_id",
  639. "device_id",
  640. // 将时间戳转为 Y-m-d 格式并起别名
  641. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  642. // 聚合求和
  643. DB::raw("SUM(total_work_min) as total_work")
  644. )
  645. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  646. //查询所有人员工资
  647. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  648. ->pluck('id')->toArray();
  649. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  650. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  651. ->pluck("month_str", 'id')->toArray();
  652. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  653. ->select("device_id", "depreciation_amount", "main_id")
  654. ->get()->toArray();
  655. //查询所有项目人员的工时比例
  656. //汇总每个人每个月工资
  657. $depreciatio_map = [];
  658. $all_depreciatio = [];
  659. foreach ($month_device_salary as $val) {
  660. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  661. if ($month) {
  662. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  663. $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
  664. }
  665. }
  666. // 2. 计算每个员工在每个月的全月总工时
  667. $device_monthly_total_min = [];
  668. foreach ($month_device_list as $row) {
  669. $key = $row['device_id'] . '_' . $row['order_month'];
  670. if (!isset($device_monthly_total_min[$key])) {
  671. $device_monthly_total_min[$key] = 0;
  672. }
  673. $device_monthly_total_min[$key] += $row['total_work'];
  674. }
  675. // 3. 计算分摊天数与工资
  676. $item_list = [];
  677. foreach ($month_device_list as $item) {
  678. $key = $item['device_id'] . '_' . $item['order_month'];
  679. // $device_key = $item['item_id'];
  680. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  681. $total_min = $device_monthly_total_min[$key] ?? 0;
  682. // if (!isset($item_list[$device_key])) {
  683. // $item_list[$device_key] = [
  684. // "depreciation" => 0,
  685. // ];
  686. // }
  687. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  688. if ($total_min > 0) {
  689. $ratio = round($item['total_work'] / $total_min, 3);
  690. $allocated_salary = round($ratio * $total_depreciatio, 2)*100;
  691. } else {
  692. $allocated_salary = 0;
  693. }
  694. $item_list[] = [
  695. 'device_id' => $item['device_id'],
  696. 'depreciation' => $allocated_salary,
  697. 'month' => $item['order_month'],
  698. 'item_id' => $item['item_id'],
  699. ];
  700. }
  701. $collect = collect($item_list);
  702. $device_count = $collect->groupBy(function ($item) {
  703. // 这里的 $item 是集合中的每一行数据
  704. return $item['device_id'] . '_' . $item['month'];
  705. })->map(function ($group) {
  706. return $group->count();
  707. })->toArray();
  708. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
  709. // 如果不是最后一条
  710. $key = $item['device_id'] . '_' . $item['month'];
  711. if (--$device_count[$key] > 0) {
  712. $all_depreciatio[$key] -= $item['depreciation'];
  713. } else {
  714. $item['depreciation'] = $all_depreciatio[$key];
  715. }
  716. return $item;
  717. })->all();
  718. $return_item_list = [];
  719. foreach ($month_employee_list as $v){
  720. $key = $v['item_id'];
  721. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  722. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  723. }
  724. // var_dump($return_item_list);die;
  725. return $return_item_list;
  726. }
  727. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  728. {
  729. //确认所有项目、费用
  730. $expense = ExpenseClaimsDetails::Clear($user, $data);
  731. $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
  732. ->where('del_time', 0)
  733. ->select(
  734. "fee_id",
  735. "amount",
  736. "item_id",
  737. "entrust_type"
  738. )->get()->toArray();
  739. //需要根据分类去汇总
  740. $fee = Fee::TopClear($user, $data);
  741. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  742. return $this->groupListByRoot($expense_list, $fee);
  743. }
  744. /**
  745. * 将报销明细按照一级费用和项目分类进行分组
  746. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  747. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  748. * @return array
  749. */
  750. public function groupListByRoot(array $list, array $fee_type_list)
  751. {
  752. // 1. 建立 ID 索引,方便快速查找
  753. $idMap = array_column($fee_type_list, null, 'id');
  754. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  755. $childToRoot = [];
  756. foreach ($fee_type_list as $type) {
  757. $current = $type;
  758. // 向上追溯直到 parent_id 为 0,即找到一级分类
  759. while ($current['parent_id'] != 0) {
  760. $current = $idMap[$current['parent_id']];
  761. }
  762. $childToRoot[$type['id']] = [
  763. 'id' => $current['id'],
  764. 'title' => $current['title'],
  765. 'sort' => $current['sort']
  766. ];
  767. }
  768. // 3. 遍历明细数据进行分组
  769. $item_key_list = [];
  770. $type_list = [];
  771. foreach ($list as $item) {
  772. $feeId = $item['fee_id'];
  773. // 获取该费用对应的一级分类信息
  774. if (!isset($childToRoot[$feeId])) continue;
  775. $rootId = $childToRoot[$feeId]['id'];
  776. $title = $childToRoot[$feeId]['title'];
  777. $sort = $childToRoot[$feeId]['sort'];
  778. $key = $item['item_id'];
  779. if (!isset($item_key_list[$key][$rootId])) {
  780. $item_key_list[$key][$rootId] = [
  781. 'id' => $rootId,
  782. 'total_amount' => 0,
  783. 'entrust1_amount' => 0, //委内
  784. 'entrust2_amount' => 0, //委外
  785. ];
  786. }
  787. if ($item['entrust_type'] == 1) {
  788. $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount']*100;
  789. } elseif ($item['entrust_type'] == 2) {
  790. $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount']*100;
  791. }
  792. $item_key_list[$key][$rootId]['total_amount'] += $item['amount']*100;
  793. //这边需要拿到头部所有的一级费用类型
  794. if (!isset($type_list[$rootId])) {
  795. $type_list[$rootId] = [
  796. 'sort' => $sort,
  797. 'id' => $rootId,
  798. 'title' => $title,
  799. ];
  800. }
  801. }
  802. // var_dump($item_key_list);die;
  803. foreach ($item_key_list as $k=>$v){
  804. foreach ($v as $kk=>$vv){
  805. $item_key_list[$k][$kk]['entrust1_amount'] = round( $vv['entrust1_amount']/100,2);
  806. $item_key_list[$k][$kk]['entrust2_amount'] = round( $vv['entrust2_amount']/100,2);
  807. $item_key_list[$k][$kk]['total_amount'] = round( $vv['total_amount']/100,2);
  808. }
  809. }
  810. // $item_key_list = collect($item_key_list)->transform(function ($item) {
  811. // $item['entrust1_amount'] = round( $item['entrust1_amount']/100,2);
  812. // $item['entrust2_amount'] = round( $item['entrust2_amount']/100,2);
  813. // $item['total_amount'] = round( $item['total_amount']/100,2);
  814. // return $item;
  815. // })->all();
  816. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  817. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  818. // 4. 重置数组索引并返回
  819. return [$item_key_list, $type_list];
  820. }
  821. public function auxiliaryStatistic($data, $user)
  822. {
  823. list($status, $month_start, $month_end) = $this->commonRule($data);
  824. if (!$status) return [false, $month_start];
  825. //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
  826. //确认所有项目
  827. $item = Item::TopClear($user, $data);
  828. $item_list = $item->where('del_time', 0)
  829. ->where(function ($query) use ($month_start, $month_end) {
  830. $query->where('start_time', '>=', $month_start)
  831. ->where('end_time', '<', $month_end);
  832. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
  833. $item_key_list = [];
  834. foreach ($item_list as $v) {
  835. $item_key_list[$v['id']] = $v;
  836. }
  837. //获取该区间内所有项目人工费、折旧费
  838. $item_salary = $this->auxiliaryEmployee($user, $data, $month_start, $month_end);
  839. $device_depreciation = $this->auxiliaryDevice($user, $data, $month_start, $month_end);
  840. $fee = Fee::TopClear($user, $data);
  841. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  842. $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
  843. $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  844. ->where('del_time', 0)
  845. ->select(
  846. "item_id",
  847. "voucher_date",
  848. "voucher_type",
  849. "voucher_no",
  850. "voucher_remark",
  851. "voucher_amount",
  852. "aggregation_amount",
  853. "entrust_type",
  854. "entrust1_amount",
  855. "entrust2_amount",
  856. "entrust2_amount",
  857. "total_amount",
  858. "fee_id",
  859. "type"
  860. )->get()->toArray();
  861. list($fee_amount, $fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list, $fee);
  862. //找到项目和设备的费用然后进行比例计算
  863. $return = [];
  864. foreach ($fee_amount as $v) {
  865. //人工费用
  866. if ($v['type'] == 1 || $v['type'] == 2) {
  867. // var_dump();
  868. foreach ($item_key_list as $vv) {
  869. if(!isset($item_salary[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_salary'])) continue;
  870. $detail = [
  871. "code" => $vv['code'],
  872. "title" => $vv['title'],
  873. "state" => $vv['state'] == 3 ? "进行中" : "已完成",
  874. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  875. "voucher_type" => $v['voucher_type'],
  876. "voucher_no" => $v['voucher_no'],
  877. "voucher_remark" => $v['voucher_remark'],
  878. "voucher_amount" => $v['voucher_amount'],
  879. "aggregation_amount" => $v['aggregation_amount'],
  880. "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),
  881. "fee_id" => $v['fee_id'],
  882. "entrust1_amount" => $v['entrust1_amount'],
  883. "entrust2_amount" => $v['entrust2_amount'],
  884. "type" => $v['type'],
  885. ];
  886. $return[] = $detail;
  887. }
  888. } else {
  889. if (!isset($item_key_list[$v['item_id']])) continue;
  890. $item_value = $item_key_list[$v['item_id']];
  891. $detail = [
  892. "code" => $item_value['code'],
  893. "title" => $item_value['title'],
  894. "state" => $item_value['state'] == 3 ? "进行中" : "已完成",
  895. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  896. "voucher_type" => $v['voucher_type'],
  897. "voucher_no" => $v['voucher_no'],
  898. "voucher_remark" => $v['voucher_remark'],
  899. "voucher_amount" => $v['voucher_amount'],
  900. "aggregation_amount" => $v['aggregation_amount'],
  901. "total_amount" => $v['total_amount'],
  902. "fee_id" => $v['fee_id'],
  903. "entrust1_amount" => $v['entrust1_amount'],
  904. "entrust2_amount" => $v['entrust2_amount'],
  905. "type" => $v['type'],
  906. ];
  907. $return[] = $detail;
  908. }
  909. }
  910. return [true, [
  911. 'fee_type_list' => $fee_type_list,
  912. 'list' => $return,
  913. ]];
  914. }
  915. /**
  916. * 将报销明细按照一级费用和项目分类进行分组
  917. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  918. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  919. * @return array
  920. */
  921. public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
  922. {
  923. // 1. 建立 ID 索引,方便快速查找
  924. $idMap = array_column($fee_type_list, null, 'id');
  925. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  926. $childToRoot = [];
  927. foreach ($fee_type_list as $type) {
  928. $current = $type;
  929. // 向上追溯直到 parent_id 为 0,即找到一级分类
  930. while ($current['parent_id'] != 0) {
  931. $current = $idMap[$current['parent_id']];
  932. }
  933. $childToRoot[$type['id']] = [
  934. 'id' => $current['id'],
  935. 'title' => $current['title'],
  936. 'sort' => $current['sort']
  937. ];
  938. }
  939. // 3. 遍历明细数据进行分组
  940. $type_list = [];
  941. foreach ($list as $k => $item) {
  942. if ($item['item_id'] == 0 || $item['fee_id'] == 0) {
  943. continue;
  944. }
  945. $feeId = $item['fee_id'];
  946. // 获取该费用对应的一级分类信息
  947. if (!isset($childToRoot[$feeId])) continue;
  948. $rootId = $childToRoot[$feeId]['id'];
  949. $title = $childToRoot[$feeId]['title'];
  950. $sort = $childToRoot[$feeId]['sort'];
  951. $item['fee_id'] = $rootId;
  952. $list[$k] = $item;
  953. //这边需要拿到头部所有的一级费用类型
  954. if (!isset($type_list[$rootId])) {
  955. $type_list[$rootId] = [
  956. 'sort' => $sort,
  957. 'id' => $rootId,
  958. 'title' => $title,
  959. ];
  960. }
  961. }
  962. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  963. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  964. // 4. 重置数组索引并返回
  965. return [$list, $type_list];
  966. }
  967. public function auxiliaryEmployee($user, $data, $month_start, $month_end)
  968. {
  969. //确认所有项目、人员、人员工时
  970. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  971. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  972. ->where('del_time', 0)
  973. ->select(
  974. "item_id",
  975. "employee_id",
  976. // 将时间戳转为 Y-m-d 格式并起别名
  977. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  978. // 聚合求和
  979. DB::raw("SUM(total_work_min) as total_work")
  980. )
  981. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  982. //查询所有人员工资
  983. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  984. ->pluck('id')->toArray();
  985. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  986. ->where('del_time', 0)
  987. ->where("month", ">=", $month_start)
  988. ->where("month", "<", $month_end)
  989. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  990. ->pluck('month_str', 'id')
  991. ->toArray();
  992. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  993. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  994. ->get()->toArray();
  995. //查询所有项目人员的工时比例
  996. //汇总每个人每个月工资
  997. $salary_map = [];
  998. $all_salary = [];
  999. foreach ($month_employee_salary as $val) {
  1000. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  1001. if ($month) {
  1002. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  1003. $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
  1004. }
  1005. }
  1006. // var_dump($salary_map);die;
  1007. // 2. 计算每个员工在每个月的全月总工时
  1008. $employee_monthly_total_min = [];
  1009. foreach ($month_employee_list as $row) {
  1010. $key = $row['employee_id'] . '_' . $row['order_month'];
  1011. if (!isset($employee_monthly_total_min[$key])) {
  1012. $employee_monthly_total_min[$key] = 0;
  1013. }
  1014. $employee_monthly_total_min[$key] += $row['total_work'];
  1015. }
  1016. // 3. 计算分摊天数与工资
  1017. // $item_year_list = [];
  1018. $item_list = [];
  1019. foreach ($month_employee_list as $item) {
  1020. $key = $item['employee_id'] . '_' . $item['order_month'];
  1021. // $item_key = $item['item_id'] . '_' . $item['order_month'];
  1022. // if (!isset($item_year_list[$item_key])) {
  1023. // $item_year_list[$item_key] = [
  1024. // "allocated_salary" => 0,
  1025. // "item_id" => $item['item_id'],
  1026. // ];
  1027. // }
  1028. $total_salary = $salary_map[$key] ?? 0;
  1029. $total_min = $employee_monthly_total_min[$key] ?? 0;
  1030. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1031. if ($total_min > 0) {
  1032. $ratio = $item['total_work'] / $total_min;
  1033. $allocated_salary = round($ratio * $total_salary, 2)*100;
  1034. } else {
  1035. $allocated_salary = 0;
  1036. }
  1037. $item_list[] = [
  1038. 'employee_id' => $item['employee_id'],
  1039. 'allocated_salary' => $allocated_salary,
  1040. 'month' => $item['order_month'],
  1041. 'item_id' => $item['item_id'],
  1042. ];
  1043. }
  1044. $collect = collect($item_list);
  1045. $employee_count = $collect->groupBy(function ($item) {
  1046. // 这里的 $item 是集合中的每一行数据
  1047. return $item['employee_id'] . '_' . $item['month'];
  1048. })->map(function ($group) {
  1049. return $group->count();
  1050. })->toArray();
  1051. $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
  1052. // 如果不是最后一条
  1053. $key = $item['employee_id'] . '_' . $item['month'];
  1054. if(!isset($all_salary[$key])) $item['allocated_salary'] = 0;
  1055. else {
  1056. if (--$employee_count[$key] > 0) {
  1057. $all_salary[$key] -= $item['allocated_salary'];
  1058. } else {
  1059. $item['allocated_salary'] = $all_salary[$key];
  1060. }
  1061. }
  1062. return $item;
  1063. })->all();
  1064. $return_item_list = [];
  1065. foreach ($month_employee_list as $v){
  1066. $key = $v['item_id'] . '_' . $v['month'];
  1067. if(!isset($return_item_list[$key])) $return_item_list[$key]['allocated_salary'] = 0;
  1068. $return_item_list[$key]['allocated_salary'] += $v['allocated_salary'];
  1069. }
  1070. return $return_item_list;
  1071. }
  1072. public function auxiliaryDevice($user, $data, $month_start, $month_end)
  1073. {
  1074. $month_device = DailyDwOrderDetails::Clear($user, $data);
  1075. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1076. ->where('del_time', 0)
  1077. ->select(
  1078. "item_id",
  1079. "device_id",
  1080. // 将时间戳转为 Y-m-d 格式并起别名
  1081. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1082. // 聚合求和
  1083. DB::raw("SUM(total_work_min) as total_work")
  1084. )
  1085. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  1086. //查询所有人员工资
  1087. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1088. ->pluck('id')->toArray();
  1089. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1090. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1091. ->pluck("month_str", 'id')->toArray();
  1092. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  1093. ->select("device_id", "depreciation_amount", "main_id")
  1094. ->get()->toArray();
  1095. //查询所有项目人员的工时比例
  1096. //汇总每个人每个月工资
  1097. $depreciatio_map = [];
  1098. $all_depreciatio = [];
  1099. foreach ($month_device_salary as $val) {
  1100. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  1101. if ($month) {
  1102. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  1103. $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
  1104. }
  1105. }
  1106. // 2. 计算每个员工在每个月的全月总工时
  1107. $device_monthly_total_min = [];
  1108. foreach ($month_device_list as $row) {
  1109. $key = $row['device_id'] . '_' . $row['order_month'];
  1110. if (!isset($device_monthly_total_min[$key])) {
  1111. $device_monthly_total_min[$key] = 0;
  1112. }
  1113. $device_monthly_total_min[$key] += $row['total_work'];
  1114. }
  1115. // 3. 计算分摊天数与工资
  1116. $item_year_list = [];
  1117. $item_list = [];
  1118. foreach ($month_device_list as $item) {
  1119. $key = $item['device_id'] . '_' . $item['order_month'];
  1120. // $device_key = $item['item_id'] . '_' . $item['order_month'];
  1121. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  1122. $total_min = $device_monthly_total_min[$key] ?? 0;
  1123. // if (!isset($item_year_list[$device_key])) {
  1124. // $item_year_list[$device_key] = [
  1125. // "allocated_depreciation" => 0,
  1126. // ];
  1127. // }
  1128. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1129. if ($total_min > 0) {
  1130. $ratio = round($item['total_work'] / $total_min, 3);
  1131. $allocated_salary = round($ratio * $total_depreciatio, 2)*100;
  1132. } else {
  1133. $allocated_salary = 0;
  1134. }
  1135. // $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
  1136. $item_list[] = [
  1137. 'device_id' => $item['device_id'],
  1138. 'depreciation' => $allocated_salary,
  1139. 'month' => $item['order_month'],
  1140. 'item_id' => $item['item_id'],
  1141. ];
  1142. }
  1143. $collect = collect($item_list);
  1144. $device_count = $collect->groupBy(function ($item) {
  1145. // 这里的 $item 是集合中的每一行数据
  1146. return $item['device_id'] . '_' . $item['month'];
  1147. })->map(function ($group) {
  1148. return $group->count();
  1149. })->toArray();
  1150. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
  1151. // 如果不是最后一条
  1152. $key = $item['device_id'] . '_' . $item['month'];
  1153. if (--$device_count[$key] > 0) {
  1154. $all_depreciatio[$key] -= $item['depreciation'];
  1155. } else {
  1156. $item['depreciation'] = $all_depreciatio[$key];
  1157. }
  1158. return $item;
  1159. })->all();
  1160. $return_item_list = [];
  1161. foreach ($month_employee_list as $v){
  1162. $key = $v['item_id']. '_' . $v['month'];
  1163. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  1164. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  1165. }
  1166. // dd($return_item_list);
  1167. return $return_item_list;
  1168. }
  1169. public function itemEmployeeSalaryStatistic($data, $user)
  1170. {
  1171. list($status, $month_start, $month_end) = $this->commonRule($data);
  1172. if (!$status) return [false, $month_start];
  1173. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  1174. //获取人员工资项目相关分组数据
  1175. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  1176. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1177. ->where('del_time', 0)
  1178. ->select(
  1179. "item_id",
  1180. "employee_id",
  1181. // 将时间戳转为 Y-m-d 格式并起别名
  1182. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1183. // 聚合求和
  1184. DB::raw("SUM(total_work_min) as total_work")
  1185. )
  1186. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  1187. //查询所有人员工资
  1188. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1189. ->pluck('id')->toArray();
  1190. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  1191. ->where('del_time', 0)
  1192. ->where("month", ">=", $month_start)
  1193. ->where("month", "<", $month_end)
  1194. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1195. ->pluck('month_str', 'id')
  1196. ->toArray();
  1197. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  1198. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id", "social_insurance", "public_housing_fund")
  1199. ->get()->toArray();
  1200. //查询所有项目人员的工时比例
  1201. //汇总每个人每个月工资
  1202. $salary_map = [];
  1203. $all_salary = [];
  1204. foreach ($month_employee_salary as $val) {
  1205. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  1206. if ($month) {
  1207. $salary_map[$val['employee_id'] . '_' . $month] = $val;
  1208. $all_salary[$val['employee_id'] . '_' . $month] = [
  1209. "salary" => $val['salary']*100,
  1210. "social_insurance" => $val['social_insurance']*100,
  1211. "public_housing_fund" => $val['public_housing_fund']*100,
  1212. ];
  1213. }
  1214. }
  1215. // var_dump($salary_map);die;
  1216. // 2. 计算每个员工在每个月的全月总工时
  1217. $employee_monthly_total_min = [];
  1218. $all_min = [];
  1219. foreach ($month_employee_list as $row) {
  1220. $key = $row['employee_id'] . '_' . $row['order_month'];
  1221. if (!isset($employee_monthly_total_min[$key])) {
  1222. $employee_monthly_total_min[$key] = 0;
  1223. $all_min[$key] = 0;
  1224. }
  1225. $employee_monthly_total_min[$key] += $row['total_work'];
  1226. $all_min[$key] = round($row['total_work']/60,2)*100;
  1227. }
  1228. // 3. 计算分摊天数与工资
  1229. $item_month_list = [];
  1230. foreach ($month_employee_list as $item) {
  1231. $key = $item['employee_id'] . '_' . $item['order_month'];
  1232. $item_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  1233. if (!isset($item_month_list[$item_key])) {
  1234. $item_month_list[$item_key] = [
  1235. "month" => $item['order_month'],
  1236. "allocated_salary" => 0,
  1237. "employee_id" => $item['employee_id'],
  1238. "work_minutes" => 0,
  1239. "salary" => ($salary_map[$key]['salary'] ?? 0),
  1240. "social_insurance" => ($salary_map[$key]['social_insurance'] ?? 0),
  1241. "public_housing_fund" => ($salary_map[$key]['public_housing_fund'] ?? 0),
  1242. "item_id" => $item['item_id'],
  1243. ];
  1244. }
  1245. $total_min = $employee_monthly_total_min[$key] ?? 0;
  1246. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1247. if ($total_min > 0) {
  1248. $ratio = round($item['total_work'] / $total_min, 4);
  1249. } else {
  1250. $ratio = 0;
  1251. }
  1252. $item_month_list[$item_key]['radio'] = $ratio;
  1253. $item_month_list[$item_key]['total_min'] = $total_min;
  1254. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  1255. }
  1256. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  1257. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  1258. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  1259. $employee = Employee::TopClear($user, $data);
  1260. $employee_list = $employee->wherein('id', $employee_ids)->select("major", "title", "id")->get()->toArray();
  1261. $employee_key_list = [];
  1262. foreach ($employee_list as $v) {
  1263. $employee_key_list[$v['id']] = $v;
  1264. }
  1265. $item = Item::TopClear($user, $data);
  1266. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  1267. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  1268. $collect = collect($item_month_list);
  1269. $employee_count = $collect->groupBy(function ($item) {
  1270. // 这里的 $item 是集合中的每一行数据
  1271. return $item['employee_id'] . '_' . $item['month'];
  1272. })->map(function ($group) {
  1273. return $group->count();
  1274. })->toArray();
  1275. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  1276. $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) {
  1277. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  1278. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  1279. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['employee_id']})";
  1280. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
  1281. $item['total_min'] = round($item['total_min'] / 60, 2);
  1282. $key = $item['employee_id'] . '_' . $item['month'];
  1283. if (--$employee_count[$key] > 0) {
  1284. $work_minutes = round($item['work_minutes'] / 60, 2);
  1285. $item['work_minutes'] = $work_minutes;
  1286. $all_min[$key] -= ($work_minutes*100);
  1287. $work_salary = round($item['salary'] * $item['radio'], 2);
  1288. $all_salary[$key]['salary'] -= ($work_salary*100);
  1289. $item['work_salary'] = $work_salary;
  1290. $social_insurance = round($item['social_insurance'] * $item['radio'], 2);
  1291. $all_salary[$key]['social_insurance'] -= ($social_insurance*100);
  1292. $item['work_social_insurance'] = $social_insurance;
  1293. $work_public_housing_fund = round($item['public_housing_fund'] * $item['radio'], 2);
  1294. $all_salary[$key]['public_housing_fund'] -= ($work_public_housing_fund*100);
  1295. $item['work_public_housing_fund'] = $work_public_housing_fund;
  1296. } else {
  1297. $item['work_salary'] = round($all_salary[$key]['salary']/100,2);
  1298. $item['work_social_insurance'] = round($all_salary[$key]['social_insurance'] /100,2);
  1299. $item['work_public_housing_fund'] = round( $all_salary[$key]['public_housing_fund']/100,2);
  1300. $item['work_minutes'] = round( $all_min[$key]/100,2);
  1301. }
  1302. return $item;
  1303. })->all();
  1304. return [true, $item_month_list];
  1305. }
  1306. public function enterpriseRdStatistic($data, $user){
  1307. $model = Item::TopClear($user,$data);
  1308. $model = $model->where('del_time',0)
  1309. ->select(Item::$report_field_1)
  1310. ->orderby('id', 'desc');
  1311. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  1312. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  1313. if(! empty($data['state'])) $model->where('state', $data['state']);
  1314. $list = $model->get()->toArray();
  1315. $list = $this->fillEnterpriseRdStatistic($list, $data, $user);
  1316. return [true, $list];
  1317. }
  1318. private function fillEnterpriseRdStatistic($list, $data, $user){
  1319. //项目实际支出 项目费用单
  1320. $expense = ExpenseClaimsDetails::Clear($user, $data);
  1321. $expense_map = $expense->where('del_time', 0)
  1322. ->whereIn('item_id', array_unique(array_column($list,'id')))
  1323. ->selectRaw('item_id, SUM(amount) as total_amount')
  1324. ->groupBy('item_id')
  1325. ->pluck('total_amount', 'item_id') // 这一步直接生成 item_id => total_amount 结构
  1326. ->toArray();
  1327. foreach ($list as $key => $value){
  1328. $list[$key]['actual_expenditure'] = $expense_map[$value['id']] ?? 0;
  1329. $start_time = $value['start_time'] ? date('Y-m-d', $value['start_time']) : '';
  1330. $end_time = $value['end_time'] ? date('Y-m-d', $value['end_time']) : '';
  1331. $list[$key]['time_range'] = $start_time . ' ' . $end_time;
  1332. }
  1333. return $list;
  1334. }
  1335. public function enterpriseRdManStatistic($data, $user){
  1336. $model = Employee::TopClear($user,$data);
  1337. $model = $model->where('del_time',0)
  1338. ->where('is_admin', '<>', Employee::IS_ADMIN_TWO)
  1339. ->select(Employee::$report_field)
  1340. ->orderBy('id','desc');
  1341. if(! empty($data['id_card'])) $model->where('id_card', 'LIKE', '%'.$data['id_card'].'%');
  1342. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  1343. if(! empty($data['state'])) $model->where('state', $data['state']);
  1344. if(isset($data['education'])) $model->where('education', $data['education']);
  1345. $list = $model->get()->toArray();
  1346. $list = $this->fillEnterpriseRdManStatistic($list, $data, $user);
  1347. return [true, $list];
  1348. }
  1349. private function fillEnterpriseRdManStatistic($list, $data, $user){
  1350. $man = EmployeeDepartPermission::from('employee_depart_permission as a')
  1351. ->join('depart as b', 'b.id', '=', 'a.depart_id')
  1352. ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
  1353. ->select('a.employee_id', 'b.title')
  1354. ->get()->toArray();
  1355. $man_map = [];
  1356. foreach ($man as $value){
  1357. if(isset($man_map[$value['employee_id']])){
  1358. $man_map[$value['employee_id']] .= ',' . $value['title'];
  1359. }else{
  1360. $man_map[$value['employee_id']] = $value['title'];
  1361. }
  1362. }
  1363. foreach ($list as $key => $value){
  1364. $depart_title = $man_map[$value['id']] ?? "";
  1365. $list[$key]['position_new'] = $depart_title . '/' . $value['position'];
  1366. }
  1367. return $list;
  1368. }
  1369. public function enterpriseRdItemStatistic($data, $user){
  1370. // 1. 先声明表名和别名
  1371. $model = ItemDetails::from('item_details as i');
  1372. // 2. 再调用 TopClear
  1373. $model = $model->TopClear($user, $data);
  1374. $model = $model->from('item_details as i')
  1375. ->where('i.del_time', 0)
  1376. ->where('i.type', ItemDetails::type_one);
  1377. $model = $model->leftJoin('employee as e', 'i.data_id', '=', 'e.id');
  1378. $fields = ['e.id', 'e.title', 'e.education', 'e.major','e.p_title','i.item_id'];
  1379. $list = $model->select($fields)
  1380. ->orderBy('i.id', 'desc')
  1381. ->get()
  1382. ->toArray();
  1383. $list = $this->fillEnterpriseRdItemStatistic($list, $data, $user);
  1384. return [true, $list];
  1385. }
  1386. private function fillEnterpriseRdItemStatistic($list, $data, $user){
  1387. $man = EmployeeDepartPermission::from('employee_depart_permission as a')
  1388. ->join('depart as b', 'b.id', '=', 'a.depart_id')
  1389. ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
  1390. ->select('a.employee_id', 'b.title')
  1391. ->get()->toArray();
  1392. $man_map = [];
  1393. foreach ($man as $value){
  1394. if(isset($man_map[$value['employee_id']])){
  1395. $man_map[$value['employee_id']] .= ',' . $value['title'];
  1396. }else{
  1397. $man_map[$value['employee_id']] = $value['title'];
  1398. }
  1399. }
  1400. $item_map = Item::whereIn('id',array_unique(array_column($list,'item_id')))
  1401. ->pluck('title','id')
  1402. ->toArray();
  1403. foreach ($list as $key => $value){
  1404. $depart_title = $man_map[$value['id']] ?? "";
  1405. $list[$key]['depart_title'] = $depart_title;
  1406. $item_title = $item_map[$value['item_id']] ?? "";
  1407. $list[$key]['item_title'] = $item_title;
  1408. }
  1409. return $list;
  1410. }
  1411. }