StatisticService.php 93 KB

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