StatisticService.php 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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. // 确定起始时间:默认当年 1 月 1 日
  477. if (!isset($data['month_start'])) {
  478. $month_start = date('Y-01-01 00:00:00');
  479. } else {
  480. $month_start = date('Y-m-01 00:00:00', strtotime($data['month_start']));
  481. }
  482. // 确定结束时间
  483. if (!isset($data['month_end'])) {
  484. $month_end = date('Y-12-31 23:59:59', strtotime($month_start));
  485. } else {
  486. $start_year = date('Y', strtotime($month_start));
  487. $end_year = date('Y', strtotime($data['month_end']));
  488. if ($start_year != $end_year) return [false, "查询不得跨年!", ""];
  489. // 取传入月份的最后一天最后一秒
  490. $month_end = date('Y-m-t 23:59:59', strtotime($data['month_end']));
  491. }
  492. return [true, strtotime($month_start), strtotime($month_end)];
  493. }
  494. /**
  495. * 根据前端 ISO 时间字符串获取该年份的起止日期和时间戳
  496. * 适配:2019-12-31T16:00:00.000Z 这种带时区的数据
  497. *
  498. * @param string $isoStr 前端传来的时间字符串
  499. * @return array|null
  500. */
  501. public function getYearRangeInfo($isoStr)
  502. {
  503. if (empty($isoStr)) return null;
  504. try {
  505. // 1. 解析 ISO 8601 字符串
  506. $date = new \DateTime($isoStr);
  507. // 2. 强制转为中国时区(PRC),处理 16:00:00Z 这种 UTC 偏移
  508. $date->setTimezone(new \DateTimeZone('PRC'));
  509. // 3. 提取年份
  510. $year = $date->format('Y');
  511. // 4. 构造日期字符串
  512. $startDate = $year . "-01-01";
  513. $endDate = $year . "-12-31";
  514. return [
  515. $startDate,
  516. $endDate,
  517. ];
  518. } catch (\Exception $e) {
  519. // var_dump($e->getMessage());die;
  520. return null;
  521. }
  522. }
  523. public function employeeAttendanceMonthStatistic($data, $user)
  524. {
  525. //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
  526. list($status, $month_start, $month_end) = $this->commonRule($data);
  527. if (!$status) {
  528. return [false, $month_start];
  529. }
  530. //第一步确定项目
  531. $item = Item::TopClear($user, $data);
  532. $item_list = $item->where('del_time', 0)
  533. ->where(function ($query) use ($month_start, $month_end) {
  534. $query->where('start_time', '<=', $month_start)
  535. ->orWhere('end_time', '>=', $month_end);
  536. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
  537. $item_key_list = [];
  538. foreach ($item_list as $v) {
  539. $item_key_list[$v['id']] = $v;
  540. }
  541. //第二步确定人员费用
  542. $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
  543. //第三步确定折旧费用
  544. $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
  545. //第四步其他费用
  546. list($item_fee_list, $fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  547. //组合所有数据
  548. $return = [];
  549. foreach ($item_key_list as $v) {
  550. //其他费用是个数组
  551. $item_value = [
  552. "code" => $v['code'],
  553. "title" => $v['title'],
  554. "state" => $v['state'] == 3 ? "完结" : "进行中",
  555. "employee_salary" => isset($item_employee_list[$v['id']]['salary']) ? round($item_employee_list[$v['id']]['salary']/100,2) : 0,
  556. "device_depreciation" => isset($item_device_list[$v['id']]['depreciation']) ? round($item_device_list[$v['id']]['depreciation']/100,2): 0,
  557. "expense_type" => "费用化支出",
  558. "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
  559. ];
  560. $return[] = $item_value;
  561. }
  562. return [true, ["list" => $return, "fee_type_list" => $fee_type_list]];
  563. }
  564. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  565. {
  566. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  567. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  568. ->where('del_time', 0)
  569. ->select(
  570. "item_id",
  571. "employee_id",
  572. // 将时间戳转为 Y-m-d 格式并起别名
  573. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  574. // 聚合求和
  575. DB::raw("SUM(total_work_min) as total_work")
  576. )
  577. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  578. //查询所有人员工资
  579. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  580. ->pluck('id')->toArray();
  581. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  582. ->where('del_time', 0)
  583. ->where("month", ">=", $month_start)
  584. ->where("month", "<", $month_end)
  585. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  586. ->pluck('month_str', 'id')
  587. ->toArray();
  588. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  589. ->select("employee_id", "salary", "main_id")
  590. ->get()->toArray();
  591. //查询所有项目人员的工时比例
  592. //汇总每个人每个月工资
  593. $salary_map = [];
  594. $all_salary = [];
  595. foreach ($month_employee_salary as $val) {
  596. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  597. if ($month) {
  598. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  599. $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
  600. }
  601. }
  602. // 2. 计算每个员工在每个月的全月总工时
  603. $employee_monthly_total_min = [];
  604. foreach ($month_employee_list as $row) {
  605. $key = $row['employee_id'] . '_' . $row['order_month'];
  606. if (!isset($employee_monthly_total_min[$key])) {
  607. $employee_monthly_total_min[$key] = 0;
  608. }
  609. $employee_monthly_total_min[$key] += $row['total_work'];
  610. }
  611. // 2. 计算分摊天数与工资
  612. $item_list = [];
  613. foreach ($month_employee_list as $item) {
  614. $key = $item['employee_id'] . '_' . $item['order_month'];
  615. // $item_key = $item['item_id'].'_'.$item['order_month'];
  616. // if (!isset($item_list[$item_key])) {
  617. // $item_list[$item_key] = [
  618. // "salary" => 0,
  619. // ];
  620. // }
  621. $total_salary = $salary_map[$key] ?? 0;
  622. $total_min = $employee_monthly_total_min[$key] ?? 0;
  623. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  624. if ($total_min > 0) {
  625. $ratio = $item['total_work'] / $total_min;
  626. $allocated_salary = round($ratio * $total_salary, 2)*100;
  627. } else {
  628. $allocated_salary = 0;
  629. }
  630. $item_list[] = [
  631. 'employee_id' => $item['employee_id'],
  632. 'allocated_salary' => $allocated_salary,
  633. 'month' => $item['order_month'],
  634. 'item_id' => $item['item_id'],
  635. ];
  636. }
  637. $collect = collect($item_list);
  638. $employee_count = $collect->groupBy(function ($item) {
  639. // 这里的 $item 是集合中的每一行数据
  640. return $item['employee_id'] . '_' . $item['month'];
  641. })->map(function ($group) {
  642. return $group->count();
  643. })->toArray();
  644. $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
  645. // 如果不是最后一条
  646. $key = $item['employee_id'] . '_' . $item['month'];
  647. if (--$employee_count[$key] > 0) {
  648. $all_salary[$key] -= $item['allocated_salary'];
  649. } else {
  650. $item['allocated_salary'] = $all_salary[$key];
  651. }
  652. return $item;
  653. })->all();
  654. $return_item_list = [];
  655. foreach ($month_employee_list as $v){
  656. $key = $v['item_id'];
  657. if(!isset($return_item_list[$key])) $return_item_list[$key]['salary'] = 0;
  658. $return_item_list[$key]['salary'] += $v['allocated_salary'];
  659. }
  660. return $return_item_list;
  661. }
  662. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  663. {
  664. //确认所有项目、设备、设备工时
  665. $month_device = DailyDwOrderDetails::Clear($user, $data);
  666. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  667. ->where('del_time', 0)
  668. ->select(
  669. "item_id",
  670. "device_id",
  671. // 将时间戳转为 Y-m-d 格式并起别名
  672. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  673. // 聚合求和
  674. DB::raw("SUM(total_work_min) as total_work")
  675. )
  676. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  677. //查询所有人员工资
  678. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  679. ->pluck('id')->toArray();
  680. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  681. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  682. ->pluck("month_str", 'id')->toArray();
  683. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  684. ->select("device_id", "depreciation_amount", "main_id")
  685. ->get()->toArray();
  686. //查询所有项目人员的工时比例
  687. //汇总每个人每个月工资
  688. $depreciatio_map = [];
  689. $all_depreciatio = [];
  690. foreach ($month_device_salary as $val) {
  691. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  692. if ($month) {
  693. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  694. $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
  695. }
  696. }
  697. // 2. 计算每个员工在每个月的全月总工时
  698. $device_monthly_total_min = [];
  699. foreach ($month_device_list as $row) {
  700. $key = $row['device_id'] . '_' . $row['order_month'];
  701. if (!isset($device_monthly_total_min[$key])) {
  702. $device_monthly_total_min[$key] = 0;
  703. }
  704. $device_monthly_total_min[$key] += $row['total_work'];
  705. }
  706. // 3. 计算分摊天数与工资
  707. $item_list = [];
  708. foreach ($month_device_list as $item) {
  709. $key = $item['device_id'] . '_' . $item['order_month'];
  710. // $device_key = $item['item_id'];
  711. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  712. $total_min = $device_monthly_total_min[$key] ?? 0;
  713. // if (!isset($item_list[$device_key])) {
  714. // $item_list[$device_key] = [
  715. // "depreciation" => 0,
  716. // ];
  717. // }
  718. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  719. if ($total_min > 0) {
  720. $ratio = round($item['total_work'] / $total_min, 3);
  721. $allocated_salary = round($ratio * $total_depreciatio, 2)*100;
  722. } else {
  723. $allocated_salary = 0;
  724. }
  725. $item_list[] = [
  726. 'device_id' => $item['device_id'],
  727. 'depreciation' => $allocated_salary,
  728. 'month' => $item['order_month'],
  729. 'item_id' => $item['item_id'],
  730. ];
  731. }
  732. $collect = collect($item_list);
  733. $device_count = $collect->groupBy(function ($item) {
  734. // 这里的 $item 是集合中的每一行数据
  735. return $item['device_id'] . '_' . $item['month'];
  736. })->map(function ($group) {
  737. return $group->count();
  738. })->toArray();
  739. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
  740. // 如果不是最后一条
  741. $key = $item['device_id'] . '_' . $item['month'];
  742. if (--$device_count[$key] > 0) {
  743. $all_depreciatio[$key] -= $item['depreciation'];
  744. } else {
  745. $item['depreciation'] = $all_depreciatio[$key];
  746. }
  747. return $item;
  748. })->all();
  749. $return_item_list = [];
  750. foreach ($month_employee_list as $v){
  751. $key = $v['item_id'];
  752. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  753. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  754. }
  755. // var_dump($return_item_list);die;
  756. return $return_item_list;
  757. }
  758. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  759. {
  760. //确认所有项目、费用
  761. $expense = ExpenseClaimsDetails::Clear($user, $data);
  762. $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
  763. ->where('del_time', 0)
  764. ->select(
  765. "fee_id",
  766. "amount",
  767. "item_id",
  768. "entrust_type"
  769. )->get()->toArray();
  770. //需要根据分类去汇总
  771. $fee = Fee::TopClear($user, $data);
  772. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  773. return $this->groupListByRoot($expense_list, $fee);
  774. }
  775. /**
  776. * 将报销明细按照一级费用和项目分类进行分组
  777. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  778. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  779. * @return array
  780. */
  781. public function groupListByRoot(array $list, array $fee_type_list)
  782. {
  783. // 1. 建立 ID 索引,方便快速查找
  784. $idMap = array_column($fee_type_list, null, 'id');
  785. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  786. $childToRoot = [];
  787. foreach ($fee_type_list as $type) {
  788. $current = $type;
  789. // 向上追溯直到 parent_id 为 0,即找到一级分类
  790. while ($current['parent_id'] != 0) {
  791. $current = $idMap[$current['parent_id']];
  792. }
  793. $childToRoot[$type['id']] = [
  794. 'id' => $current['id'],
  795. 'title' => $current['title'],
  796. 'sort' => $current['sort']
  797. ];
  798. }
  799. // 3. 遍历明细数据进行分组
  800. $item_key_list = [];
  801. $type_list = [];
  802. foreach ($list as $item) {
  803. $feeId = $item['fee_id'];
  804. // 获取该费用对应的一级分类信息
  805. if (!isset($childToRoot[$feeId])) continue;
  806. $rootId = $childToRoot[$feeId]['id'];
  807. $title = $childToRoot[$feeId]['title'];
  808. $sort = $childToRoot[$feeId]['sort'];
  809. $key = $item['item_id'];
  810. if (!isset($item_key_list[$key][$rootId])) {
  811. $item_key_list[$key][$rootId] = [
  812. 'id' => $rootId,
  813. 'total_amount' => 0,
  814. 'entrust1_amount' => 0, //委内
  815. 'entrust2_amount' => 0, //委外
  816. ];
  817. }
  818. if ($item['entrust_type'] == 1) {
  819. $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount']*100;
  820. } elseif ($item['entrust_type'] == 2) {
  821. $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount']*100;
  822. }
  823. $item_key_list[$key][$rootId]['total_amount'] += $item['amount']*100;
  824. //这边需要拿到头部所有的一级费用类型
  825. if (!isset($type_list[$rootId])) {
  826. $type_list[$rootId] = [
  827. 'sort' => $sort,
  828. 'id' => $rootId,
  829. 'title' => $title,
  830. ];
  831. }
  832. }
  833. // var_dump($item_key_list);die;
  834. foreach ($item_key_list as $k=>$v){
  835. foreach ($v as $kk=>$vv){
  836. $item_key_list[$k][$kk]['entrust1_amount'] = round( $vv['entrust1_amount']/100,2);
  837. $item_key_list[$k][$kk]['entrust2_amount'] = round( $vv['entrust2_amount']/100,2);
  838. $item_key_list[$k][$kk]['total_amount'] = round( $vv['total_amount']/100,2);
  839. }
  840. }
  841. // $item_key_list = collect($item_key_list)->transform(function ($item) {
  842. // $item['entrust1_amount'] = round( $item['entrust1_amount']/100,2);
  843. // $item['entrust2_amount'] = round( $item['entrust2_amount']/100,2);
  844. // $item['total_amount'] = round( $item['total_amount']/100,2);
  845. // return $item;
  846. // })->all();
  847. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  848. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  849. // 4. 重置数组索引并返回
  850. return [$item_key_list, $type_list];
  851. }
  852. public function auxiliaryStatistic($data, $user)
  853. {
  854. list($status, $month_start, $month_end) = $this->commonRule($data);
  855. if (!$status) return [false, $month_start];
  856. //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
  857. //确认所有项目
  858. $item = Item::TopClear($user, $data);
  859. $item_list = $item->where('del_time', 0)
  860. ->where(function ($query) use ($month_start, $month_end) {
  861. $query->where('start_time', '<=', $month_start)
  862. ->orWhere('end_time', '>', $month_end);
  863. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
  864. $item_key_list = [];
  865. foreach ($item_list as $v) {
  866. $item_key_list[$v['id']] = $v;
  867. }
  868. //获取该区间内所有项目人工费、折旧费
  869. $item_salary = $this->auxiliaryEmployee($user, $data, $month_start, $month_end);
  870. $device_depreciation = $this->auxiliaryDevice($user, $data, $month_start, $month_end);
  871. $fee = Fee::TopClear($user, $data);
  872. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  873. $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
  874. $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  875. ->where('del_time', 0)
  876. ->select(
  877. "item_id",
  878. "voucher_date",
  879. "voucher_type",
  880. "voucher_no",
  881. "voucher_remark",
  882. "voucher_amount",
  883. "aggregation_amount",
  884. "entrust_type",
  885. "entrust1_amount",
  886. "entrust2_amount",
  887. "entrust2_amount",
  888. "total_amount",
  889. "fee_id",
  890. "type"
  891. )->get()->toArray();
  892. list($fee_amount, $fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list, $fee);
  893. //找到项目和设备的费用然后进行比例计算
  894. $return = [];
  895. foreach ($fee_amount as $v) {
  896. //人工费用
  897. if ($v['type'] == 1 || $v['type'] == 2) {
  898. foreach ($item_key_list as $vv) {
  899. $detail = [
  900. "code" => $vv['code'],
  901. "title" => $vv['title'],
  902. "state" => $vv['state'] == 3 ? "进行中" : "已完成",
  903. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  904. "voucher_type" => $v['voucher_type'],
  905. "voucher_no" => $v['voucher_no'],
  906. "voucher_remark" => $v['voucher_remark'],
  907. "voucher_amount" => $v['voucher_amount'],
  908. "aggregation_amount" => $v['aggregation_amount'],
  909. "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),
  910. "fee_id" => $v['fee_id'],
  911. "entrust1_amount" => $v['entrust1_amount'],
  912. "entrust2_amount" => $v['entrust2_amount'],
  913. "type" => $v['type'],
  914. ];
  915. $return[] = $detail;
  916. }
  917. } else {
  918. if (!isset($item_key_list[$v['item_id']])) continue;
  919. $item_value = $item_key_list[$v['item_id']];
  920. $detail = [
  921. "code" => $item_value['code'],
  922. "title" => $item_value['title'],
  923. "state" => $item_value['state'] == 3 ? "进行中" : "已完成",
  924. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  925. "voucher_type" => $v['voucher_type'],
  926. "voucher_no" => $v['voucher_no'],
  927. "voucher_remark" => $v['voucher_remark'],
  928. "voucher_amount" => $v['voucher_amount'],
  929. "aggregation_amount" => $v['aggregation_amount'],
  930. "total_amount" => $v['total_amount'],
  931. "fee_id" => $v['fee_id'],
  932. "entrust1_amount" => $v['entrust1_amount'],
  933. "entrust2_amount" => $v['entrust2_amount'],
  934. "type" => $v['type'],
  935. ];
  936. $return[] = $detail;
  937. }
  938. }
  939. return [true, [
  940. 'fee_type_list' => $fee_type_list,
  941. 'list' => $return,
  942. ]];
  943. }
  944. /**
  945. * 将报销明细按照一级费用和项目分类进行分组
  946. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  947. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  948. * @return array
  949. */
  950. public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
  951. {
  952. // 1. 建立 ID 索引,方便快速查找
  953. $idMap = array_column($fee_type_list, null, 'id');
  954. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  955. $childToRoot = [];
  956. foreach ($fee_type_list as $type) {
  957. $current = $type;
  958. // 向上追溯直到 parent_id 为 0,即找到一级分类
  959. while ($current['parent_id'] != 0) {
  960. $current = $idMap[$current['parent_id']];
  961. }
  962. $childToRoot[$type['id']] = [
  963. 'id' => $current['id'],
  964. 'title' => $current['title'],
  965. 'sort' => $current['sort']
  966. ];
  967. }
  968. // 3. 遍历明细数据进行分组
  969. $type_list = [];
  970. foreach ($list as $k => $item) {
  971. if ($item['item_id'] == 0 || $item['fee_id'] == 0) {
  972. continue;
  973. }
  974. $feeId = $item['fee_id'];
  975. // 获取该费用对应的一级分类信息
  976. if (!isset($childToRoot[$feeId])) continue;
  977. $rootId = $childToRoot[$feeId]['id'];
  978. $title = $childToRoot[$feeId]['title'];
  979. $sort = $childToRoot[$feeId]['sort'];
  980. $item['fee_id'] = $rootId;
  981. $list[$k] = $item;
  982. //这边需要拿到头部所有的一级费用类型
  983. if (!isset($type_list[$rootId])) {
  984. $type_list[$rootId] = [
  985. 'sort' => $sort,
  986. 'id' => $rootId,
  987. 'title' => $title,
  988. ];
  989. }
  990. }
  991. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  992. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  993. // 4. 重置数组索引并返回
  994. return [$list, $type_list];
  995. }
  996. public function auxiliaryEmployee($user, $data, $month_start, $month_end)
  997. {
  998. //确认所有项目、人员、人员工时
  999. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  1000. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1001. ->where('del_time', 0)
  1002. ->select(
  1003. "item_id",
  1004. "employee_id",
  1005. // 将时间戳转为 Y-m-d 格式并起别名
  1006. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1007. // 聚合求和
  1008. DB::raw("SUM(total_work_min) as total_work")
  1009. )
  1010. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  1011. //查询所有人员工资
  1012. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1013. ->pluck('id')->toArray();
  1014. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  1015. ->where('del_time', 0)
  1016. ->where("month", ">=", $month_start)
  1017. ->where("month", "<", $month_end)
  1018. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1019. ->pluck('month_str', 'id')
  1020. ->toArray();
  1021. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  1022. ->select("employee_id", "salary", "main_id")
  1023. ->get()->toArray();
  1024. //查询所有项目人员的工时比例
  1025. //汇总每个人每个月工资
  1026. $salary_map = [];
  1027. $all_salary = [];
  1028. foreach ($month_employee_salary as $val) {
  1029. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  1030. if ($month) {
  1031. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  1032. $all_salary[$val['employee_id'] . '_' . $month] = $val['salary']*100;
  1033. }
  1034. }
  1035. // var_dump($salary_map);die;
  1036. // 2. 计算每个员工在每个月的全月总工时
  1037. $employee_monthly_total_min = [];
  1038. foreach ($month_employee_list as $row) {
  1039. $key = $row['employee_id'] . '_' . $row['order_month'];
  1040. if (!isset($employee_monthly_total_min[$key])) {
  1041. $employee_monthly_total_min[$key] = 0;
  1042. }
  1043. $employee_monthly_total_min[$key] += $row['total_work'];
  1044. }
  1045. // 3. 计算分摊天数与工资
  1046. // $item_year_list = [];
  1047. $item_list = [];
  1048. foreach ($month_employee_list as $item) {
  1049. $key = $item['employee_id'] . '_' . $item['order_month'];
  1050. // $item_key = $item['item_id'] . '_' . $item['order_month'];
  1051. // if (!isset($item_year_list[$item_key])) {
  1052. // $item_year_list[$item_key] = [
  1053. // "allocated_salary" => 0,
  1054. // "item_id" => $item['item_id'],
  1055. // ];
  1056. // }
  1057. $total_salary = $salary_map[$key] ?? 0;
  1058. $total_min = $employee_monthly_total_min[$key] ?? 0;
  1059. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1060. if ($total_min > 0) {
  1061. $ratio = $item['total_work'] / $total_min;
  1062. $allocated_salary = round($ratio * $total_salary, 2)*100;
  1063. } else {
  1064. $allocated_salary = 0;
  1065. }
  1066. $item_list[] = [
  1067. 'employee_id' => $item['employee_id'],
  1068. 'allocated_salary' => $allocated_salary,
  1069. 'month' => $item['order_month'],
  1070. 'item_id' => $item['item_id'],
  1071. ];
  1072. }
  1073. $collect = collect($item_list);
  1074. $employee_count = $collect->groupBy(function ($item) {
  1075. // 这里的 $item 是集合中的每一行数据
  1076. return $item['employee_id'] . '_' . $item['month'];
  1077. })->map(function ($group) {
  1078. return $group->count();
  1079. })->toArray();
  1080. $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
  1081. // 如果不是最后一条
  1082. $key = $item['employee_id'] . '_' . $item['month'];
  1083. if (--$employee_count[$key] > 0) {
  1084. $all_salary[$key] -= $item['allocated_salary'];
  1085. } else {
  1086. $item['allocated_salary'] = $all_salary[$key];
  1087. }
  1088. return $item;
  1089. })->all();
  1090. $return_item_list = [];
  1091. foreach ($month_employee_list as $v){
  1092. $key = $v['item_id'] . '_' . $v['month'];
  1093. if(!isset($return_item_list[$key])) $return_item_list[$key]['allocated_salary'] = 0;
  1094. $return_item_list[$key]['allocated_salary'] += $v['allocated_salary'];
  1095. }
  1096. return $return_item_list;
  1097. }
  1098. public function auxiliaryDevice($user, $data, $month_start, $month_end)
  1099. {
  1100. $month_device = DailyDwOrderDetails::Clear($user, $data);
  1101. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1102. ->where('del_time', 0)
  1103. ->select(
  1104. "item_id",
  1105. "device_id",
  1106. // 将时间戳转为 Y-m-d 格式并起别名
  1107. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1108. // 聚合求和
  1109. DB::raw("SUM(total_work_min) as total_work")
  1110. )
  1111. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  1112. //查询所有人员工资
  1113. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1114. ->pluck('id')->toArray();
  1115. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1116. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1117. ->pluck("month_str", 'id')->toArray();
  1118. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  1119. ->select("device_id", "depreciation_amount", "main_id")
  1120. ->get()->toArray();
  1121. //查询所有项目人员的工时比例
  1122. //汇总每个人每个月工资
  1123. $depreciatio_map = [];
  1124. $all_depreciatio = [];
  1125. foreach ($month_device_salary as $val) {
  1126. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  1127. if ($month) {
  1128. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  1129. $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
  1130. }
  1131. }
  1132. // 2. 计算每个员工在每个月的全月总工时
  1133. $device_monthly_total_min = [];
  1134. foreach ($month_device_list as $row) {
  1135. $key = $row['device_id'] . '_' . $row['order_month'];
  1136. if (!isset($device_monthly_total_min[$key])) {
  1137. $device_monthly_total_min[$key] = 0;
  1138. }
  1139. $device_monthly_total_min[$key] += $row['total_work'];
  1140. }
  1141. // 3. 计算分摊天数与工资
  1142. $item_year_list = [];
  1143. $item_list = [];
  1144. foreach ($month_device_list as $item) {
  1145. $key = $item['device_id'] . '_' . $item['order_month'];
  1146. // $device_key = $item['item_id'] . '_' . $item['order_month'];
  1147. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  1148. $total_min = $device_monthly_total_min[$key] ?? 0;
  1149. // if (!isset($item_year_list[$device_key])) {
  1150. // $item_year_list[$device_key] = [
  1151. // "allocated_depreciation" => 0,
  1152. // ];
  1153. // }
  1154. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1155. if ($total_min > 0) {
  1156. $ratio = round($item['total_work'] / $total_min, 3);
  1157. $allocated_salary = round($ratio * $total_depreciatio, 2)*100;
  1158. } else {
  1159. $allocated_salary = 0;
  1160. }
  1161. // $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
  1162. $item_list[] = [
  1163. 'device_id' => $item['device_id'],
  1164. 'depreciation' => $allocated_salary,
  1165. 'month' => $item['order_month'],
  1166. 'item_id' => $item['item_id'],
  1167. ];
  1168. }
  1169. $collect = collect($item_list);
  1170. $device_count = $collect->groupBy(function ($item) {
  1171. // 这里的 $item 是集合中的每一行数据
  1172. return $item['device_id'] . '_' . $item['month'];
  1173. })->map(function ($group) {
  1174. return $group->count();
  1175. })->toArray();
  1176. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
  1177. // 如果不是最后一条
  1178. $key = $item['device_id'] . '_' . $item['month'];
  1179. if (--$device_count[$key] > 0) {
  1180. $all_depreciatio[$key] -= $item['depreciation'];
  1181. } else {
  1182. $item['depreciation'] = $all_depreciatio[$key];
  1183. }
  1184. return $item;
  1185. })->all();
  1186. $return_item_list = [];
  1187. foreach ($month_employee_list as $v){
  1188. $key = $v['item_id']. '_' . $v['month'];
  1189. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  1190. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  1191. }
  1192. // dd($return_item_list);
  1193. return $return_item_list;
  1194. }
  1195. public function itemEmployeeSalaryStatistic($data, $user)
  1196. {
  1197. list($status, $month_start, $month_end) = $this->commonRule($data);
  1198. if (!$status) return [false, $month_start];
  1199. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  1200. //获取人员工资项目相关分组数据
  1201. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  1202. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  1203. ->where('del_time', 0)
  1204. ->select(
  1205. "item_id",
  1206. "employee_id",
  1207. // 将时间戳转为 Y-m-d 格式并起别名
  1208. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  1209. // 聚合求和
  1210. DB::raw("SUM(total_work_min) as total_work")
  1211. )
  1212. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  1213. //查询所有人员工资
  1214. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  1215. ->pluck('id')->toArray();
  1216. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  1217. ->where('del_time', 0)
  1218. ->where("month", ">=", $month_start)
  1219. ->where("month", "<", $month_end)
  1220. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  1221. ->pluck('month_str', 'id')
  1222. ->toArray();
  1223. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  1224. ->select("employee_id", "salary", "main_id", "social_insurance", "public_housing_fund")
  1225. ->get()->toArray();
  1226. //查询所有项目人员的工时比例
  1227. //汇总每个人每个月工资
  1228. $salary_map = [];
  1229. $all_salary = [];
  1230. foreach ($month_employee_salary as $val) {
  1231. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  1232. if ($month) {
  1233. $salary_map[$val['employee_id'] . '_' . $month] = $val;
  1234. $all_salary[$val['employee_id'] . '_' . $month] = [
  1235. "salary" => $val['salary']*100,
  1236. "social_insurance" => $val['social_insurance']*100,
  1237. "public_housing_fund" => $val['public_housing_fund']*100,
  1238. ];
  1239. }
  1240. }
  1241. // var_dump($salary_map);die;
  1242. // 2. 计算每个员工在每个月的全月总工时
  1243. $employee_monthly_total_min = [];
  1244. $all_min = [];
  1245. foreach ($month_employee_list as $row) {
  1246. $key = $row['employee_id'] . '_' . $row['order_month'];
  1247. if (!isset($employee_monthly_total_min[$key])) {
  1248. $employee_monthly_total_min[$key] = 0;
  1249. $all_min[$key] = 0;
  1250. }
  1251. $employee_monthly_total_min[$key] += $row['total_work'];
  1252. $all_min[$key] = round($row['total_work']/60,2)*100;
  1253. }
  1254. // 3. 计算分摊天数与工资
  1255. $item_month_list = [];
  1256. foreach ($month_employee_list as $item) {
  1257. $key = $item['employee_id'] . '_' . $item['order_month'];
  1258. $item_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  1259. if (!isset($item_month_list[$item_key])) {
  1260. $item_month_list[$item_key] = [
  1261. "month" => $item['order_month'],
  1262. "allocated_salary" => 0,
  1263. "employee_id" => $item['employee_id'],
  1264. "work_minutes" => 0,
  1265. "salary" => ($salary_map[$key]['salary'] ?? 0),
  1266. "social_insurance" => ($salary_map[$key]['social_insurance'] ?? 0),
  1267. "public_housing_fund" => ($salary_map[$key]['public_housing_fund'] ?? 0),
  1268. "item_id" => $item['item_id'],
  1269. ];
  1270. }
  1271. $total_min = $employee_monthly_total_min[$key] ?? 0;
  1272. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  1273. if ($total_min > 0) {
  1274. $ratio = round($item['total_work'] / $total_min, 4);
  1275. } else {
  1276. $ratio = 0;
  1277. }
  1278. $item_month_list[$item_key]['radio'] = $ratio;
  1279. $item_month_list[$item_key]['total_min'] = $total_min;
  1280. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  1281. }
  1282. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  1283. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  1284. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  1285. $employee = Employee::TopClear($user, $data);
  1286. $employee_list = $employee->wherein('id', $employee_ids)->select("major", "title", "id")->get()->toArray();
  1287. $employee_key_list = [];
  1288. foreach ($employee_list as $v) {
  1289. $employee_key_list[$v['id']] = $v;
  1290. }
  1291. $item = Item::TopClear($user, $data);
  1292. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  1293. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  1294. $collect = collect($item_month_list);
  1295. $employee_count = $collect->groupBy(function ($item) {
  1296. // 这里的 $item 是集合中的每一行数据
  1297. return $item['employee_id'] . '_' . $item['month'];
  1298. })->map(function ($group) {
  1299. return $group->count();
  1300. })->toArray();
  1301. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  1302. $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) {
  1303. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  1304. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  1305. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['employee_id']})";
  1306. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
  1307. $item['total_min'] = round($item['total_min'] / 60, 2);
  1308. $key = $item['employee_id'] . '_' . $item['month'];
  1309. if (--$employee_count[$key] > 0) {
  1310. $work_minutes = round($item['work_minutes'] / 60, 2);
  1311. $item['work_minutes'] = $work_minutes;
  1312. $all_min[$key] -= ($work_minutes*100);
  1313. $work_salary = round($item['salary'] * $item['radio'], 2);
  1314. $all_salary[$key]['salary'] -= ($work_salary*100);
  1315. $item['work_salary'] = $work_salary;
  1316. $social_insurance = round($item['social_insurance'] * $item['radio'], 2);
  1317. $all_salary[$key]['social_insurance'] -= ($social_insurance*100);
  1318. $item['work_social_insurance'] = $social_insurance;
  1319. $work_public_housing_fund = round($item['public_housing_fund'] * $item['radio'], 2);
  1320. $all_salary[$key]['public_housing_fund'] -= ($work_public_housing_fund*100);
  1321. $item['work_public_housing_fund'] = $work_public_housing_fund;
  1322. } else {
  1323. $item['work_salary'] = round($all_salary[$key]['salary']/100,2);
  1324. $item['work_social_insurance'] = round($all_salary[$key]['social_insurance'] /100,2);
  1325. $item['work_public_housing_fund'] = round( $all_salary[$key]['public_housing_fund']/100,2);
  1326. $item['work_minutes'] = round( $all_min[$key]/100,2);
  1327. }
  1328. return $item;
  1329. })->all();
  1330. return [true, $item_month_list];
  1331. }
  1332. }