StatisticService.php 63 KB

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