StatisticService.php 92 KB

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