StatisticService.php 65 KB

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