StatisticService.php 72 KB

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