StatisticService.php 67 KB

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