StatisticService.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. private function commonRule($data)
  428. {
  429. if (!empty($data['year'])) {
  430. $return = $this->getYearRangeInfo($data['year']);
  431. if (is_null($return)) return [false, '年度格式错误'];
  432. list($data['month_start'], $data['month_end']) = $return;
  433. } else {
  434. if (isset($data['time']) && !empty($data['time'])) {
  435. $start = $this->changeDateToDate($data['time'][0]);
  436. $end = $this->changeDateToDate($data['time'][1], true);
  437. $data['month_start'] = date("Y-m-d", $start);
  438. $data['month_end'] = date("Y-m-d", $end);
  439. }
  440. }
  441. if (!isset($data['month_start'])) $month_start = date('Y-01-01');
  442. else $month_start = date('Y-m-01', strtotime($data['month_start']));
  443. if (!isset($data['month_end'])) $month_end = date('Y-01-01', strtotime('+1 year', strtotime($month_start)));
  444. else {
  445. $start_year = date('Y', strtotime($month_start));
  446. $end_year = date('Y', strtotime($data['month_end']));
  447. if ($start_year != $end_year) return [false, "查询不得跨年!", ""];
  448. $month_end = date('Y-m-01', strtotime($data['month_end'] . ' +1 month'));
  449. }
  450. return [true, strtotime($month_start), strtotime($month_end)];
  451. }
  452. /**
  453. * 根据前端 ISO 时间字符串获取该年份的起止日期和时间戳
  454. * 适配:2019-12-31T16:00:00.000Z 这种带时区的数据
  455. *
  456. * @param string $isoStr 前端传来的时间字符串
  457. * @return array|null
  458. */
  459. public function getYearRangeInfo($isoStr)
  460. {
  461. if (empty($isoStr)) return null;
  462. try {
  463. // 1. 解析 ISO 8601 字符串
  464. $date = new \DateTime($isoStr);
  465. // 2. 强制转为中国时区(PRC),处理 16:00:00Z 这种 UTC 偏移
  466. $date->setTimezone(new \DateTimeZone('PRC'));
  467. // 3. 提取年份
  468. $year = $date->format('Y');
  469. // 4. 构造日期字符串
  470. $startDate = $year . "-01-01";
  471. $endDate = $year . "-12-31";
  472. return [
  473. $startDate,
  474. $endDate,
  475. ];
  476. } catch (\Exception $e) {
  477. // var_dump($e->getMessage());die;
  478. return null;
  479. }
  480. }
  481. public function employeeAttendanceMonthStatistic($data, $user)
  482. {
  483. //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
  484. list($status, $month_start, $month_end) = $this->commonRule($data);
  485. if (!$status) {
  486. return [false, $month_start];
  487. }
  488. //第一步确定项目
  489. $item = Item::TopClear($user, $data);
  490. $item_list = $item->where('del_time', 0)
  491. ->where(function ($query) use ($month_start, $month_end) {
  492. $query->where('start_time', '>=', $month_start)
  493. ->where('end_time', '<', $month_end);
  494. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
  495. $item_key_list = [];
  496. foreach ($item_list as $v) {
  497. $item_key_list[$v['id']] = $v;
  498. }
  499. //第二步确定人员费用
  500. $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
  501. //第三步确定折旧费用
  502. $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
  503. //第四步其他费用
  504. list($item_fee_list, $fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  505. //组合所有数据
  506. $return = [];
  507. foreach ($item_key_list as $v) {
  508. //其他费用是个数组
  509. $item_value = [
  510. "code" => $v['code'],
  511. "title" => $v['title'],
  512. "state" => $v['state'] == 3 ? "完结" : "进行中",
  513. "employee_salary" => isset($item_employee_list[$v['id']]['salary']) ? round($item_employee_list[$v['id']]['salary']/100,2) : 0,
  514. "device_depreciation" => isset($item_device_list[$v['id']]['depreciation']) ? round($item_device_list[$v['id']]['depreciation']/100,2): 0,
  515. "expense_type" => "费用化支出",
  516. "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
  517. ];
  518. $return[] = $item_value;
  519. }
  520. return [true, ["list" => $return, "fee_type_list" => $fee_type_list]];
  521. }
  522. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  523. {
  524. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  525. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  526. ->where('del_time', 0)
  527. ->select(
  528. "item_id",
  529. "employee_id",
  530. // 将时间戳转为 Y-m-d 格式并起别名
  531. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  532. // 聚合求和
  533. DB::raw("SUM(total_work_min) as total_work")
  534. )
  535. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  536. //查询所有人员工资
  537. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  538. ->pluck('id')->toArray();
  539. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  540. ->where('del_time', 0)
  541. ->where("month", ">=", $month_start)
  542. ->where("month", "<", $month_end)
  543. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  544. ->pluck('month_str', 'id')
  545. ->toArray();
  546. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  547. ->select("employee_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  548. ->get()->toArray();
  549. //查询所有项目人员的工时比例
  550. //汇总每个人每个月工资
  551. $salary_map = [];
  552. $all_salary = [];
  553. foreach ($month_employee_salary as $val) {
  554. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  555. if ($month) {
  556. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  557. $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
  558. }
  559. }
  560. // 2. 计算每个员工在每个月的全月总工时
  561. $employee_monthly_total_min = [];
  562. foreach ($month_employee_list as $row) {
  563. $key = $row['employee_id'] . '_' . $row['order_month'];
  564. if (!isset($employee_monthly_total_min[$key])) {
  565. $employee_monthly_total_min[$key] = 0;
  566. }
  567. $employee_monthly_total_min[$key] += $row['total_work'];
  568. }
  569. // 2. 计算分摊天数与工资
  570. $item_list = [];
  571. foreach ($month_employee_list as $item) {
  572. $key = $item['employee_id'] . '_' . $item['order_month'];
  573. // $item_key = $item['item_id'].'_'.$item['order_month'];
  574. // if (!isset($item_list[$item_key])) {
  575. // $item_list[$item_key] = [
  576. // "salary" => 0,
  577. // ];
  578. // }
  579. $total_salary = $salary_map[$key] ?? 0;
  580. $total_min = $employee_monthly_total_min[$key] ?? 0;
  581. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  582. if ($total_min > 0) {
  583. $ratio = $item['total_work'] / $total_min;
  584. $allocated_salary = round($ratio * $total_salary, 2)*100;
  585. } else {
  586. $allocated_salary = 0;
  587. }
  588. $item_list[] = [
  589. 'employee_id' => $item['employee_id'],
  590. 'allocated_salary' => $allocated_salary,
  591. 'month' => $item['order_month'],
  592. 'item_id' => $item['item_id'],
  593. ];
  594. }
  595. $collect = collect($item_list);
  596. $employee_count = $collect->groupBy(function ($item) {
  597. // 这里的 $item 是集合中的每一行数据
  598. return $item['employee_id'] . '_' . $item['month'];
  599. })->map(function ($group) {
  600. return $group->count();
  601. })->toArray();
  602. $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
  603. // 如果不是最后一条
  604. $key = $item['employee_id'] . '_' . $item['month'];
  605. if(!isset($all_salary[$key])) $item['allocated_salary'] = 0;
  606. else{
  607. if (--$employee_count[$key] > 0) {
  608. $all_salary[$key] -= $item['allocated_salary'];
  609. } else {
  610. $item['allocated_salary'] = $all_salary[$key];
  611. }
  612. }
  613. return $item;
  614. })->all();
  615. $return_item_list = [];
  616. foreach ($month_employee_list as $v){
  617. $key = $v['item_id'];
  618. if(!isset($return_item_list[$key])) $return_item_list[$key]['salary'] = 0;
  619. $return_item_list[$key]['salary'] += $v['allocated_salary'];
  620. }
  621. return $return_item_list;
  622. }
  623. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  624. {
  625. //确认所有项目、设备、设备工时
  626. $month_device = DailyDwOrderDetails::Clear($user, $data);
  627. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  628. ->where('del_time', 0)
  629. ->select(
  630. "item_id",
  631. "device_id",
  632. // 将时间戳转为 Y-m-d 格式并起别名
  633. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  634. // 聚合求和
  635. DB::raw("SUM(total_work_min) as total_work")
  636. )
  637. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  638. //查询所有人员工资
  639. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  640. ->pluck('id')->toArray();
  641. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  642. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  643. ->pluck("month_str", 'id')->toArray();
  644. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  645. ->select("device_id", "depreciation_amount", "main_id")
  646. ->get()->toArray();
  647. //查询所有项目人员的工时比例
  648. //汇总每个人每个月工资
  649. $depreciatio_map = [];
  650. $all_depreciatio = [];
  651. foreach ($month_device_salary as $val) {
  652. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  653. if ($month) {
  654. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  655. $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
  656. }
  657. }
  658. // 2. 计算每个员工在每个月的全月总工时
  659. $device_monthly_total_min = [];
  660. foreach ($month_device_list as $row) {
  661. $key = $row['device_id'] . '_' . $row['order_month'];
  662. if (!isset($device_monthly_total_min[$key])) {
  663. $device_monthly_total_min[$key] = 0;
  664. }
  665. $device_monthly_total_min[$key] += $row['total_work'];
  666. }
  667. // 3. 计算分摊天数与工资
  668. $item_list = [];
  669. foreach ($month_device_list as $item) {
  670. $key = $item['device_id'] . '_' . $item['order_month'];
  671. // $device_key = $item['item_id'];
  672. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  673. $total_min = $device_monthly_total_min[$key] ?? 0;
  674. // if (!isset($item_list[$device_key])) {
  675. // $item_list[$device_key] = [
  676. // "depreciation" => 0,
  677. // ];
  678. // }
  679. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  680. if ($total_min > 0) {
  681. $ratio = round($item['total_work'] / $total_min, 3);
  682. $allocated_salary = round($ratio * $total_depreciatio, 2)*100;
  683. } else {
  684. $allocated_salary = 0;
  685. }
  686. $item_list[] = [
  687. 'device_id' => $item['device_id'],
  688. 'depreciation' => $allocated_salary,
  689. 'month' => $item['order_month'],
  690. 'item_id' => $item['item_id'],
  691. ];
  692. }
  693. $collect = collect($item_list);
  694. $device_count = $collect->groupBy(function ($item) {
  695. // 这里的 $item 是集合中的每一行数据
  696. return $item['device_id'] . '_' . $item['month'];
  697. })->map(function ($group) {
  698. return $group->count();
  699. })->toArray();
  700. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
  701. // 如果不是最后一条
  702. $key = $item['device_id'] . '_' . $item['month'];
  703. if (--$device_count[$key] > 0) {
  704. $all_depreciatio[$key] -= $item['depreciation'];
  705. } else {
  706. $item['depreciation'] = $all_depreciatio[$key];
  707. }
  708. return $item;
  709. })->all();
  710. $return_item_list = [];
  711. foreach ($month_employee_list as $v){
  712. $key = $v['item_id'];
  713. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  714. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  715. }
  716. // var_dump($return_item_list);die;
  717. return $return_item_list;
  718. }
  719. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  720. {
  721. //确认所有项目、费用
  722. $expense = ExpenseClaimsDetails::Clear($user, $data);
  723. $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
  724. ->where('del_time', 0)
  725. ->select(
  726. "fee_id",
  727. "amount",
  728. "item_id",
  729. "entrust_type"
  730. )->get()->toArray();
  731. //需要根据分类去汇总
  732. $fee = Fee::TopClear($user, $data);
  733. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  734. return $this->groupListByRoot($expense_list, $fee);
  735. }
  736. /**
  737. * 将报销明细按照一级费用和项目分类进行分组
  738. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  739. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  740. * @return array
  741. */
  742. public function groupListByRoot(array $list, array $fee_type_list)
  743. {
  744. // 1. 建立 ID 索引,方便快速查找
  745. $idMap = array_column($fee_type_list, null, 'id');
  746. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  747. $childToRoot = [];
  748. foreach ($fee_type_list as $type) {
  749. $current = $type;
  750. // 向上追溯直到 parent_id 为 0,即找到一级分类
  751. while ($current['parent_id'] != 0) {
  752. $current = $idMap[$current['parent_id']];
  753. }
  754. $childToRoot[$type['id']] = [
  755. 'id' => $current['id'],
  756. 'title' => $current['title'],
  757. 'sort' => $current['sort']
  758. ];
  759. }
  760. // 3. 遍历明细数据进行分组
  761. $item_key_list = [];
  762. $type_list = [];
  763. foreach ($list as $item) {
  764. $feeId = $item['fee_id'];
  765. // 获取该费用对应的一级分类信息
  766. if (!isset($childToRoot[$feeId])) continue;
  767. $rootId = $childToRoot[$feeId]['id'];
  768. $title = $childToRoot[$feeId]['title'];
  769. $sort = $childToRoot[$feeId]['sort'];
  770. $key = $item['item_id'];
  771. if (!isset($item_key_list[$key][$rootId])) {
  772. $item_key_list[$key][$rootId] = [
  773. 'id' => $rootId,
  774. 'total_amount' => 0,
  775. 'entrust1_amount' => 0, //委内
  776. 'entrust2_amount' => 0, //委外
  777. ];
  778. }
  779. if ($item['entrust_type'] == 1) {
  780. $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount']*100;
  781. } elseif ($item['entrust_type'] == 2) {
  782. $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount']*100;
  783. }
  784. $item_key_list[$key][$rootId]['total_amount'] += $item['amount']*100;
  785. //这边需要拿到头部所有的一级费用类型
  786. if (!isset($type_list[$rootId])) {
  787. $type_list[$rootId] = [
  788. 'sort' => $sort,
  789. 'id' => $rootId,
  790. 'title' => $title,
  791. ];
  792. }
  793. }
  794. // var_dump($item_key_list);die;
  795. foreach ($item_key_list as $k=>$v){
  796. foreach ($v as $kk=>$vv){
  797. $item_key_list[$k][$kk]['entrust1_amount'] = round( $vv['entrust1_amount']/100,2);
  798. $item_key_list[$k][$kk]['entrust2_amount'] = round( $vv['entrust2_amount']/100,2);
  799. $item_key_list[$k][$kk]['total_amount'] = round( $vv['total_amount']/100,2);
  800. }
  801. }
  802. // $item_key_list = collect($item_key_list)->transform(function ($item) {
  803. // $item['entrust1_amount'] = round( $item['entrust1_amount']/100,2);
  804. // $item['entrust2_amount'] = round( $item['entrust2_amount']/100,2);
  805. // $item['total_amount'] = round( $item['total_amount']/100,2);
  806. // return $item;
  807. // })->all();
  808. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  809. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  810. // 4. 重置数组索引并返回
  811. return [$item_key_list, $type_list];
  812. }
  813. public function auxiliaryStatistic($data, $user)
  814. {
  815. list($status, $month_start, $month_end) = $this->commonRule($data);
  816. if (!$status) return [false, $month_start];
  817. //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
  818. //确认所有项目
  819. $item = Item::TopClear($user, $data);
  820. $item_list = $item->where('del_time', 0)
  821. ->where(function ($query) use ($month_start, $month_end) {
  822. $query->where('start_time', '>=', $month_start)
  823. ->where('end_time', '<', $month_end);
  824. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
  825. $item_key_list = [];
  826. foreach ($item_list as $v) {
  827. $item_key_list[$v['id']] = $v;
  828. }
  829. //获取该区间内所有项目人工费、折旧费
  830. $item_salary = $this->auxiliaryEmployee($user, $data, $month_start, $month_end);
  831. $device_depreciation = $this->auxiliaryDevice($user, $data, $month_start, $month_end);
  832. $fee = Fee::TopClear($user, $data);
  833. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  834. $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
  835. $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  836. ->where('del_time', 0)
  837. ->select(
  838. "item_id",
  839. "voucher_date",
  840. "voucher_type",
  841. "voucher_no",
  842. "voucher_remark",
  843. "voucher_amount",
  844. "aggregation_amount",
  845. "entrust_type",
  846. "entrust1_amount",
  847. "entrust2_amount",
  848. "entrust2_amount",
  849. "total_amount",
  850. "fee_id",
  851. "type"
  852. )->get()->toArray();
  853. list($fee_amount, $fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list, $fee);
  854. //找到项目和设备的费用然后进行比例计算
  855. $return = [];
  856. foreach ($fee_amount as $v) {
  857. //人工费用
  858. if ($v['type'] == 1 || $v['type'] == 2) {
  859. // var_dump();
  860. foreach ($item_key_list as $vv) {
  861. if(!isset($item_salary[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_salary'])) continue;
  862. $detail = [
  863. "code" => $vv['code'],
  864. "title" => $vv['title'],
  865. "state" => $vv['state'] == 3 ? "进行中" : "已完成",
  866. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  867. "voucher_type" => $v['voucher_type'],
  868. "voucher_no" => $v['voucher_no'],
  869. "voucher_remark" => $v['voucher_remark'],
  870. "voucher_amount" => $v['voucher_amount'],
  871. "aggregation_amount" => $v['aggregation_amount'],
  872. "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),
  873. "fee_id" => $v['fee_id'],
  874. "entrust1_amount" => $v['entrust1_amount'],
  875. "entrust2_amount" => $v['entrust2_amount'],
  876. "type" => $v['type'],
  877. ];
  878. $return[] = $detail;
  879. }
  880. } else {
  881. if (!isset($item_key_list[$v['item_id']])) continue;
  882. $item_value = $item_key_list[$v['item_id']];
  883. $detail = [
  884. "code" => $item_value['code'],
  885. "title" => $item_value['title'],
  886. "state" => $item_value['state'] == 3 ? "进行中" : "已完成",
  887. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  888. "voucher_type" => $v['voucher_type'],
  889. "voucher_no" => $v['voucher_no'],
  890. "voucher_remark" => $v['voucher_remark'],
  891. "voucher_amount" => $v['voucher_amount'],
  892. "aggregation_amount" => $v['aggregation_amount'],
  893. "total_amount" => $v['total_amount'],
  894. "fee_id" => $v['fee_id'],
  895. "entrust1_amount" => $v['entrust1_amount'],
  896. "entrust2_amount" => $v['entrust2_amount'],
  897. "type" => $v['type'],
  898. ];
  899. $return[] = $detail;
  900. }
  901. }
  902. return [true, [
  903. 'fee_type_list' => $fee_type_list,
  904. 'list' => $return,
  905. ]];
  906. }
  907. /**
  908. * 将报销明细按照一级费用和项目分类进行分组
  909. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  910. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  911. * @return array
  912. */
  913. public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
  914. {
  915. // 1. 建立 ID 索引,方便快速查找
  916. $idMap = array_column($fee_type_list, null, 'id');
  917. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  918. $childToRoot = [];
  919. foreach ($fee_type_list as $type) {
  920. $current = $type;
  921. // 向上追溯直到 parent_id 为 0,即找到一级分类
  922. while ($current['parent_id'] != 0) {
  923. $current = $idMap[$current['parent_id']];
  924. }
  925. $childToRoot[$type['id']] = [
  926. 'id' => $current['id'],
  927. 'title' => $current['title'],
  928. 'sort' => $current['sort']
  929. ];
  930. }
  931. // 3. 遍历明细数据进行分组
  932. $type_list = [];
  933. foreach ($list as $k => $item) {
  934. if ($item['item_id'] == 0 || $item['fee_id'] == 0) {
  935. continue;
  936. }
  937. $feeId = $item['fee_id'];
  938. // 获取该费用对应的一级分类信息
  939. if (!isset($childToRoot[$feeId])) continue;
  940. $rootId = $childToRoot[$feeId]['id'];
  941. $title = $childToRoot[$feeId]['title'];
  942. $sort = $childToRoot[$feeId]['sort'];
  943. $item['fee_id'] = $rootId;
  944. $list[$k] = $item;
  945. //这边需要拿到头部所有的一级费用类型
  946. if (!isset($type_list[$rootId])) {
  947. $type_list[$rootId] = [
  948. 'sort' => $sort,
  949. 'id' => $rootId,
  950. 'title' => $title,
  951. ];
  952. }
  953. }
  954. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  955. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  956. // 4. 重置数组索引并返回
  957. return [$list, $type_list];
  958. }
  959. public function auxiliaryEmployee($user, $data, $month_start, $month_end)
  960. {
  961. //确认所有项目、人员、人员工时
  962. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  963. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  964. ->where('del_time', 0)
  965. ->select(
  966. "item_id",
  967. "employee_id",
  968. // 将时间戳转为 Y-m-d 格式并起别名
  969. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  970. // 聚合求和
  971. DB::raw("SUM(total_work_min) as total_work")
  972. )
  973. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  974. //查询所有人员工资
  975. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  976. ->pluck('id')->toArray();
  977. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  978. ->where('del_time', 0)
  979. ->where("month", ">=", $month_start)
  980. ->where("month", "<", $month_end)
  981. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  982. ->pluck('month_str', 'id')
  983. ->toArray();
  984. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  985. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
  986. ->get()->toArray();
  987. //查询所有项目人员的工时比例
  988. //汇总每个人每个月工资
  989. $salary_map = [];
  990. $all_salary = [];
  991. foreach ($month_employee_salary as $val) {
  992. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  993. if ($month) {
  994. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  995. $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
  996. }
  997. }
  998. // var_dump($salary_map);die;
  999. // 2. 计算每个员工在每个月的全月总工时
  1000. $employee_monthly_total_min = [];
  1001. foreach ($month_employee_list as $row) {
  1002. $key = $row['employee_id'] . '_' . $row['order_month'];
  1003. if (!isset($employee_monthly_total_min[$key])) {
  1004. $employee_monthly_total_min[$key] = 0;
  1005. }
  1006. $employee_monthly_total_min[$key] += $row['total_work'];
  1007. }
  1008. // 3. 计算分摊天数与工资
  1009. // $item_year_list = [];
  1010. $item_list = [];
  1011. foreach ($month_employee_list as $item) {
  1012. $key = $item['employee_id'] . '_' . $item['order_month'];
  1013. // $item_key = $item['item_id'] . '_' . $item['order_month'];
  1014. // if (!isset($item_year_list[$item_key])) {
  1015. // $item_year_list[$item_key] = [
  1016. // "allocated_salary" => 0,
  1017. // "item_id" => $item['item_id'],
  1018. // ];
  1019. // }
  1020. $total_salary = $salary_map[$key] ?? 0;
  1021. $total_min = $employee_monthly_total_min[$key] ?? 0;
  1022. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1023. if ($total_min > 0) {
  1024. $ratio = $item['total_work'] / $total_min;
  1025. $allocated_salary = round($ratio * $total_salary, 2)*100;
  1026. } else {
  1027. $allocated_salary = 0;
  1028. }
  1029. $item_list[] = [
  1030. 'employee_id' => $item['employee_id'],
  1031. 'allocated_salary' => $allocated_salary,
  1032. 'month' => $item['order_month'],
  1033. 'item_id' => $item['item_id'],
  1034. ];
  1035. }
  1036. $collect = collect($item_list);
  1037. $employee_count = $collect->groupBy(function ($item) {
  1038. // 这里的 $item 是集合中的每一行数据
  1039. return $item['employee_id'] . '_' . $item['month'];
  1040. })->map(function ($group) {
  1041. return $group->count();
  1042. })->toArray();
  1043. $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
  1044. // 如果不是最后一条
  1045. $key = $item['employee_id'] . '_' . $item['month'];
  1046. if(!isset($all_salary[$key])) $item['allocated_salary'] = 0;
  1047. else {
  1048. if (--$employee_count[$key] > 0) {
  1049. $all_salary[$key] -= $item['allocated_salary'];
  1050. } else {
  1051. $item['allocated_salary'] = $all_salary[$key];
  1052. }
  1053. }
  1054. return $item;
  1055. })->all();
  1056. $return_item_list = [];
  1057. foreach ($month_employee_list as $v){
  1058. $key = $v['item_id'] . '_' . $v['month'];
  1059. if(!isset($return_item_list[$key])) $return_item_list[$key]['allocated_salary'] = 0;
  1060. $return_item_list[$key]['allocated_salary'] += $v['allocated_salary'];
  1061. }
  1062. return $return_item_list;
  1063. }
  1064. public function auxiliaryDevice($user, $data, $month_start, $month_end)
  1065. {
  1066. $month_device = DailyDwOrderDetails::Clear($user, $data);
  1067. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1068. ->where('del_time', 0)
  1069. ->select(
  1070. "item_id",
  1071. "device_id",
  1072. // 将时间戳转为 Y-m-d 格式并起别名
  1073. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1074. // 聚合求和
  1075. DB::raw("SUM(total_work_min) as total_work")
  1076. )
  1077. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  1078. //查询所有人员工资
  1079. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1080. ->pluck('id')->toArray();
  1081. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1082. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1083. ->pluck("month_str", 'id')->toArray();
  1084. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  1085. ->select("device_id", "depreciation_amount", "main_id")
  1086. ->get()->toArray();
  1087. //查询所有项目人员的工时比例
  1088. //汇总每个人每个月工资
  1089. $depreciatio_map = [];
  1090. $all_depreciatio = [];
  1091. foreach ($month_device_salary as $val) {
  1092. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  1093. if ($month) {
  1094. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  1095. $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
  1096. }
  1097. }
  1098. // 2. 计算每个员工在每个月的全月总工时
  1099. $device_monthly_total_min = [];
  1100. foreach ($month_device_list as $row) {
  1101. $key = $row['device_id'] . '_' . $row['order_month'];
  1102. if (!isset($device_monthly_total_min[$key])) {
  1103. $device_monthly_total_min[$key] = 0;
  1104. }
  1105. $device_monthly_total_min[$key] += $row['total_work'];
  1106. }
  1107. // 3. 计算分摊天数与工资
  1108. $item_year_list = [];
  1109. $item_list = [];
  1110. foreach ($month_device_list as $item) {
  1111. $key = $item['device_id'] . '_' . $item['order_month'];
  1112. // $device_key = $item['item_id'] . '_' . $item['order_month'];
  1113. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  1114. $total_min = $device_monthly_total_min[$key] ?? 0;
  1115. // if (!isset($item_year_list[$device_key])) {
  1116. // $item_year_list[$device_key] = [
  1117. // "allocated_depreciation" => 0,
  1118. // ];
  1119. // }
  1120. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1121. if ($total_min > 0) {
  1122. $ratio = round($item['total_work'] / $total_min, 3);
  1123. $allocated_salary = round($ratio * $total_depreciatio, 2)*100;
  1124. } else {
  1125. $allocated_salary = 0;
  1126. }
  1127. // $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
  1128. $item_list[] = [
  1129. 'device_id' => $item['device_id'],
  1130. 'depreciation' => $allocated_salary,
  1131. 'month' => $item['order_month'],
  1132. 'item_id' => $item['item_id'],
  1133. ];
  1134. }
  1135. $collect = collect($item_list);
  1136. $device_count = $collect->groupBy(function ($item) {
  1137. // 这里的 $item 是集合中的每一行数据
  1138. return $item['device_id'] . '_' . $item['month'];
  1139. })->map(function ($group) {
  1140. return $group->count();
  1141. })->toArray();
  1142. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
  1143. // 如果不是最后一条
  1144. $key = $item['device_id'] . '_' . $item['month'];
  1145. if (--$device_count[$key] > 0) {
  1146. $all_depreciatio[$key] -= $item['depreciation'];
  1147. } else {
  1148. $item['depreciation'] = $all_depreciatio[$key];
  1149. }
  1150. return $item;
  1151. })->all();
  1152. $return_item_list = [];
  1153. foreach ($month_employee_list as $v){
  1154. $key = $v['item_id']. '_' . $v['month'];
  1155. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  1156. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  1157. }
  1158. // dd($return_item_list);
  1159. return $return_item_list;
  1160. }
  1161. public function itemEmployeeSalaryStatistic($data, $user)
  1162. {
  1163. list($status, $month_start, $month_end) = $this->commonRule($data);
  1164. if (!$status) return [false, $month_start];
  1165. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  1166. //获取人员工资项目相关分组数据
  1167. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  1168. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1169. ->where('del_time', 0)
  1170. ->select(
  1171. "item_id",
  1172. "employee_id",
  1173. // 将时间戳转为 Y-m-d 格式并起别名
  1174. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1175. // 聚合求和
  1176. DB::raw("SUM(total_work_min) as total_work")
  1177. )
  1178. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  1179. //查询所有人员工资
  1180. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1181. ->pluck('id')->toArray();
  1182. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  1183. ->where('del_time', 0)
  1184. ->where("month", ">=", $month_start)
  1185. ->where("month", "<", $month_end)
  1186. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1187. ->pluck('month_str', 'id')
  1188. ->toArray();
  1189. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  1190. ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id", "social_insurance", "public_housing_fund")
  1191. ->get()->toArray();
  1192. //查询所有项目人员的工时比例
  1193. //汇总每个人每个月工资
  1194. $salary_map = [];
  1195. $all_salary = [];
  1196. foreach ($month_employee_salary as $val) {
  1197. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  1198. if ($month) {
  1199. $salary_map[$val['employee_id'] . '_' . $month] = $val;
  1200. $all_salary[$val['employee_id'] . '_' . $month] = [
  1201. "salary" => $val['salary']*100,
  1202. "social_insurance" => $val['social_insurance']*100,
  1203. "public_housing_fund" => $val['public_housing_fund']*100,
  1204. ];
  1205. }
  1206. }
  1207. // var_dump($salary_map);die;
  1208. // 2. 计算每个员工在每个月的全月总工时
  1209. $employee_monthly_total_min = [];
  1210. $all_min = [];
  1211. foreach ($month_employee_list as $row) {
  1212. $key = $row['employee_id'] . '_' . $row['order_month'];
  1213. if (!isset($employee_monthly_total_min[$key])) {
  1214. $employee_monthly_total_min[$key] = 0;
  1215. $all_min[$key] = 0;
  1216. }
  1217. $employee_monthly_total_min[$key] += $row['total_work'];
  1218. $all_min[$key] = round($row['total_work']/60,2)*100;
  1219. }
  1220. // 3. 计算分摊天数与工资
  1221. $item_month_list = [];
  1222. foreach ($month_employee_list as $item) {
  1223. $key = $item['employee_id'] . '_' . $item['order_month'];
  1224. $item_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  1225. if (!isset($item_month_list[$item_key])) {
  1226. $item_month_list[$item_key] = [
  1227. "month" => $item['order_month'],
  1228. "allocated_salary" => 0,
  1229. "employee_id" => $item['employee_id'],
  1230. "work_minutes" => 0,
  1231. "salary" => ($salary_map[$key]['salary'] ?? 0),
  1232. "social_insurance" => ($salary_map[$key]['social_insurance'] ?? 0),
  1233. "public_housing_fund" => ($salary_map[$key]['public_housing_fund'] ?? 0),
  1234. "item_id" => $item['item_id'],
  1235. ];
  1236. }
  1237. $total_min = $employee_monthly_total_min[$key] ?? 0;
  1238. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1239. if ($total_min > 0) {
  1240. $ratio = round($item['total_work'] / $total_min, 4);
  1241. } else {
  1242. $ratio = 0;
  1243. }
  1244. $item_month_list[$item_key]['radio'] = $ratio;
  1245. $item_month_list[$item_key]['total_min'] = $total_min;
  1246. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  1247. }
  1248. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  1249. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  1250. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  1251. $employee = Employee::TopClear($user, $data);
  1252. $employee_list = $employee->wherein('id', $employee_ids)->select("major", "title", "id")->get()->toArray();
  1253. $employee_key_list = [];
  1254. foreach ($employee_list as $v) {
  1255. $employee_key_list[$v['id']] = $v;
  1256. }
  1257. $item = Item::TopClear($user, $data);
  1258. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  1259. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  1260. $collect = collect($item_month_list);
  1261. $employee_count = $collect->groupBy(function ($item) {
  1262. // 这里的 $item 是集合中的每一行数据
  1263. return $item['employee_id'] . '_' . $item['month'];
  1264. })->map(function ($group) {
  1265. return $group->count();
  1266. })->toArray();
  1267. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  1268. $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) {
  1269. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  1270. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  1271. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['employee_id']})";
  1272. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
  1273. $item['total_min'] = round($item['total_min'] / 60, 2);
  1274. $key = $item['employee_id'] . '_' . $item['month'];
  1275. if (--$employee_count[$key] > 0) {
  1276. $work_minutes = round($item['work_minutes'] / 60, 2);
  1277. $item['work_minutes'] = $work_minutes;
  1278. $all_min[$key] -= ($work_minutes*100);
  1279. $work_salary = round($item['salary'] * $item['radio'], 2);
  1280. $all_salary[$key]['salary'] -= ($work_salary*100);
  1281. $item['work_salary'] = $work_salary;
  1282. $social_insurance = round($item['social_insurance'] * $item['radio'], 2);
  1283. $all_salary[$key]['social_insurance'] -= ($social_insurance*100);
  1284. $item['work_social_insurance'] = $social_insurance;
  1285. $work_public_housing_fund = round($item['public_housing_fund'] * $item['radio'], 2);
  1286. $all_salary[$key]['public_housing_fund'] -= ($work_public_housing_fund*100);
  1287. $item['work_public_housing_fund'] = $work_public_housing_fund;
  1288. } else {
  1289. $item['work_salary'] = round($all_salary[$key]['salary']/100,2);
  1290. $item['work_social_insurance'] = round($all_salary[$key]['social_insurance'] /100,2);
  1291. $item['work_public_housing_fund'] = round( $all_salary[$key]['public_housing_fund']/100,2);
  1292. $item['work_minutes'] = round( $all_min[$key]/100,2);
  1293. }
  1294. return $item;
  1295. })->all();
  1296. return [true, $item_month_list];
  1297. }
  1298. }