StatisticService.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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::Clear($user, $data);
  50. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  51. $item = Item::Clear($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. $month_employee_list = collect($month_employee_list)->transform(function ($item) use ($employee_key_list, $item_title_key_list, $item_code_key_list) {
  55. $item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
  56. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  57. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  58. $item['total_work_hours'] = round($item['total_work'] / 60, 2);
  59. return $item;
  60. })->all();
  61. return [true, $month_employee_list];
  62. }
  63. public function employeeMonthSalaryStatistic($data, $user)
  64. {
  65. //项目编码、项目名称、天数、工资、日期
  66. list($status, $month_start, $month_end) = $this->commonRule($data);
  67. if (!$status) return [false, $month_start];
  68. //确认所有项目、人员、人员工时
  69. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  70. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  71. ->where('del_time', 0)
  72. ->select(
  73. "item_id",
  74. "employee_id",
  75. // 将时间戳转为 Y-m-d 格式并起别名
  76. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  77. // 聚合求和
  78. DB::raw("SUM(total_work_min) as total_work")
  79. )
  80. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  81. //查询所有人员工资
  82. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  83. ->pluck('id')->toArray();
  84. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  85. ->where('del_time', 0)
  86. ->where("month", ">=", $month_start)
  87. ->where("month", "<", $month_end)
  88. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  89. ->pluck('month_str', 'id')
  90. ->toArray();
  91. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  92. ->select("employee_id", "salary", "main_id")
  93. ->get()->toArray();
  94. //查询所有项目人员的工时比例
  95. //汇总每个人每个月工资
  96. $salary_map = [];
  97. foreach ($month_employee_salary as $val) {
  98. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  99. if ($month) {
  100. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  101. }
  102. }
  103. // var_dump($salary_map);die;
  104. // 2. 计算每个员工在每个月的全月总工时
  105. $employee_monthly_total_min = [];
  106. foreach ($month_employee_list as $row) {
  107. $key = $row['employee_id'] . '_' . $row['order_month'];
  108. if (!isset($employee_monthly_total_min[$key])) {
  109. $employee_monthly_total_min[$key] = 0;
  110. }
  111. $employee_monthly_total_min[$key] += $row['total_work'];
  112. }
  113. // 3. 计算分摊天数与工资
  114. $item_month_list = [];
  115. foreach ($month_employee_list as $item) {
  116. $key = $item['employee_id'] . '_' . $item['order_month'];
  117. $item_key = $item['order_month'] . '_' . $item['item_id'];
  118. if (!isset($item_month_list[$item_key])) {
  119. $item_month_list[$item_key] = [
  120. "month" => $item['order_month'],
  121. "allocated_salary" => 0,
  122. "work_minutes" => 0,
  123. "item_id" => $item['item_id'],
  124. ];
  125. }
  126. $total_salary = $salary_map[$key] ?? 0;
  127. $total_min = $employee_monthly_total_min[$key] ?? 0;
  128. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  129. if ($total_min > 0) {
  130. $ratio = $item['total_work'] / $total_min;
  131. $allocated_salary = round($ratio * $total_salary, 2);
  132. } else {
  133. $allocated_salary = 0;
  134. }
  135. $item_month_list[$item_key]['allocated_salary'] += $allocated_salary;
  136. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  137. }
  138. foreach ($item_month_list as $k => $v) {
  139. $item_month_list[$k]['days'] = round($v['work_minutes'] / 8 / 60);
  140. unset($item_month_list[$k]['work_minutes']);
  141. }
  142. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  143. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  144. $item = Item::Clear($user, $data);
  145. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  146. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  147. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list) {
  148. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  149. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  150. return $item;
  151. })->all();
  152. return [true, $item_month_list];
  153. }
  154. public function itemDaySalaryStatistic($data, $user)
  155. {
  156. //项目编码、项目名称、人员名称、研发工时、研发工资、当月总工时、总计工资、年月
  157. list($status, $month_start, $month_end) = $this->commonRule($data);
  158. if (!$status) return [false, $month_start];
  159. //确认所有项目、人员、人员工时
  160. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  161. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  162. ->where('del_time', 0)
  163. ->select(
  164. "item_id",
  165. "employee_id",
  166. // 将时间戳转为 Y-m-d 格式并起别名
  167. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  168. // 聚合求和
  169. DB::raw("SUM(total_work_min) as total_work")
  170. )
  171. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "employee_id")->get()->toArray();
  172. //查询所有人员工资
  173. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)->pluck('id')->toArray();
  174. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  175. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  176. ->pluck('month_str', 'id')->toArray();
  177. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  178. ->select("employee_id", "salary", "main_id")
  179. ->get()->toArray();
  180. //查询所有项目人员的工时比例
  181. //汇总每个人每个月工资
  182. $salary_map = [];
  183. foreach ($month_employee_salary as $val) {
  184. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  185. if ($month) {
  186. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  187. }
  188. }
  189. // 2. 计算每个员工在每个月的全月总工时
  190. $employee_monthly_total_min = [];
  191. foreach ($month_employee_list as $row) {
  192. $key = $row['employee_id'] . '_' . $row['order_month'];
  193. if (!isset($employee_monthly_total_min[$key])) {
  194. $employee_monthly_total_min[$key] = 0;
  195. }
  196. $employee_monthly_total_min[$key] += $row['total_work'];
  197. }
  198. // 3. 计算分摊天数与工资
  199. $item_month_list = [];
  200. foreach ($month_employee_list as $item) {
  201. $key = $item['employee_id'] . '_' . $item['order_month'];
  202. $employee_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  203. $total_salary = $salary_map[$key] ?? 0;
  204. $total_min = $employee_monthly_total_min[$key] ?? 0;
  205. if (!isset($item_month_list[$employee_key])) {
  206. $item_month_list[$employee_key] = [
  207. "month" => $item['order_month'],
  208. "allocated_salary" => 0,
  209. "work_minutes" => 0,
  210. "total_min" => $total_min,
  211. "total_salary" => $total_salary,
  212. "item_id" => $item['item_id'],
  213. "employee_id" => $item['employee_id'],
  214. ];
  215. }
  216. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  217. if ($total_min > 0) {
  218. $ratio = $item['total_work'] / $total_min;
  219. $allocated_salary = round($ratio * $total_salary, 2);
  220. } else {
  221. $allocated_salary = 0;
  222. }
  223. $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
  224. $item_month_list[$employee_key]['work_minutes'] += $item['total_work'];
  225. }
  226. foreach ($item_month_list as $k => $v) {
  227. $item_month_list[$k]['hours'] = round($v['work_minutes'] / 60);
  228. unset($item_month_list[$k]['work_minutes']);
  229. }
  230. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  231. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  232. $item = Item::Clear($user, $data);
  233. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  234. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  235. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  236. $employee = Employee::Clear($user, $data);
  237. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  238. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, $employee_key_list) {
  239. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  240. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  241. $item['employee_title'] = $employee_key_list[$item['employee_id']] ?? "未知人员({$item['employee_id']})";
  242. return $item;
  243. })->all();
  244. return [true, $item_month_list];
  245. }
  246. public function itemDeviceMonthStatistic($data, $user)
  247. {
  248. //项目编码、项目名称、设备名称、项目工时、研发工时占比、设备原值、设备折旧额、本项目帐面归集的折旧额、确定的本项目折旧额、加计调整金额、当月工时、日期
  249. list($status, $month_start, $month_end) = $this->commonRule($data);
  250. if (!$status) return [false, $month_start];
  251. //确认所有项目、设备、设备工时
  252. $month_device = DailyDwOrderDetails::Clear($user, $data);
  253. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  254. ->where('del_time', 0)
  255. ->select(
  256. "item_id",
  257. "device_id",
  258. // 将时间戳转为 Y-m-d 格式并起别名
  259. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  260. // 聚合求和
  261. DB::raw("SUM(total_work_min) as total_work")
  262. )
  263. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  264. //查询所有人员工资
  265. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  266. ->pluck('id')->toArray();
  267. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  268. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  269. ->pluck("month_str", 'id')->toArray();
  270. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  271. ->select("device_id", "depreciation_amount", "main_id")
  272. ->get()->toArray();
  273. //查询所有项目人员的工时比例
  274. //汇总每个人每个月工资
  275. $depreciatio_map = [];
  276. foreach ($month_device_salary as $val) {
  277. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  278. if ($month) {
  279. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  280. }
  281. }
  282. // 2. 计算每个员工在每个月的全月总工时
  283. $device_monthly_total_min = [];
  284. foreach ($month_device_list as $row) {
  285. $key = $row['device_id'] . '_' . $row['order_month'];
  286. if (!isset($device_monthly_total_min[$key])) {
  287. $device_monthly_total_min[$key] = 0;
  288. }
  289. $device_monthly_total_min[$key] += $row['total_work'];
  290. }
  291. // 3. 计算分摊天数与工资
  292. $item_month_list = [];
  293. foreach ($month_device_list as $item) {
  294. $key = $item['device_id'] . '_' . $item['order_month'];
  295. $device_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['device_id'];
  296. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  297. $total_min = $device_monthly_total_min[$key] ?? 0;
  298. if (!isset($item_month_list[$device_key])) {
  299. $item_month_list[$device_key] = [
  300. "month" => $item['order_month'],
  301. "allocated_depreciatio" => 0,
  302. "work_minutes" => 0,
  303. "total_min" => $total_min,
  304. "item_id" => $item['item_id'],
  305. "device_id" => $item['device_id'],
  306. "total_depreciatio" => $total_depreciatio,
  307. ];
  308. }
  309. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  310. if ($total_min > 0) {
  311. $ratio = round($item['total_work'] / $total_min, 3);
  312. $allocated_salary = round($ratio * $total_depreciatio, 2);
  313. } else {
  314. $ratio = 0;
  315. $allocated_salary = 0;
  316. }
  317. $item_month_list[$device_key]['allocated_depreciatio'] += $allocated_salary;
  318. $item_month_list[$device_key]['work_minutes'] += $item['total_work'];
  319. $item_month_list[$device_key]['ratio'] = $ratio;
  320. }
  321. foreach ($item_month_list as $k => $v) {
  322. $item_month_list[$k]['total_hours'] = round($v['total_min'] / 60, 1);
  323. $item_month_list[$k]['hours'] = round($v['work_minutes'] / 60, 1);
  324. unset($item_month_list[$k]['work_minutes']);
  325. }
  326. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  327. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  328. $item = Item::Clear($user, $data);
  329. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  330. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  331. $device_ids = collect($item_month_list)->pluck('device_id')->unique()->values()->all();
  332. $device = Device::Clear($user, $data);
  333. $device_key_list = $device->wherein('id', $device_ids)->pluck("title", "id")->toArray();
  334. $device_original_value_key_list = $device->wherein('id', $device_ids)->pluck("original_value", "id")->toArray();
  335. $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) {
  336. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  337. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  338. $item['device_title'] = $device_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  339. $item['device_original'] = $device_original_value_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  340. return $item;
  341. })->all();
  342. return [true, $item_month_list];
  343. }
  344. private function commonRule($data)
  345. {
  346. if(isset($data['time_range'])){
  347. $data['month_start'] = $data['time_range'][0] ?? null;
  348. $data['month_end'] = $data['time_range'][1] ?? null;
  349. }
  350. if (!isset($data['month_start'])) $month_start = date('Y-01-01');
  351. else $month_start = date('Y-m-01', strtotime($data['month_start']));
  352. if (!isset($data['month_end'])) $month_end = date('Y-01-01', strtotime('+1 year', strtotime($month_start)));
  353. else {
  354. $start_year = date('Y', strtotime($month_start));
  355. $end_year = date('Y', strtotime($data['month_end']));
  356. if ($start_year != $end_year) return [false, "查询不得跨年!", ""];
  357. $month_end = date('Y-m-01', strtotime($data['month_end'] . ' +1 month'));
  358. }
  359. return [true, strtotime($month_start), strtotime($month_end)];
  360. }
  361. public function employeeAttendanceMonthStatistic($data, $user)
  362. {
  363. //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
  364. list($status, $month_start, $month_end) = $this->commonRule($data);
  365. if (!$status) {
  366. return [false, $month_start];
  367. }
  368. //第一步确定项目
  369. $item = Item::Clear($user, $data);
  370. $item_list = $item->where('del_time', 0)
  371. ->where(function ($query) use ($month_start, $month_end) {
  372. $query->where('start_time', '>=', $month_start)
  373. ->orWhere('end_time', '<=', $month_end);
  374. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time","asc")->get()->toArray();
  375. $item_key_list = [];
  376. foreach ($item_list as $v) {
  377. $item_key_list[$v['id']] = $v;
  378. }
  379. //第二步确定人员费用
  380. $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
  381. //第三步确定折旧费用
  382. $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
  383. //第四步其他费用
  384. list($item_fee_list,$fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  385. //组合所有数据
  386. $return = [];
  387. foreach ($item_key_list as $v){
  388. //其他费用是个数组
  389. $item_value = [
  390. "code" => $v['code'],
  391. "title" => $v['title'],
  392. "state" => $v['state'] == 0 ? "进行中" : "完结",
  393. "employee_salary" => $item_employee_list[$v['id']]['salary']??0,
  394. "device_depreciation" => $item_device_list[$v['id']]['depreciation']??0,
  395. "expense_type" => "费用化支出",
  396. "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
  397. ];
  398. $return[] = $item_value;
  399. }
  400. return [true,["list"=>$return,"fee_type_list"=>$fee_type_list]];
  401. }
  402. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  403. {
  404. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  405. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  406. ->where('del_time', 0)
  407. ->select(
  408. "item_id",
  409. "employee_id",
  410. // 将时间戳转为 Y-m-d 格式并起别名
  411. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  412. // 聚合求和
  413. DB::raw("SUM(total_work_min) as total_work")
  414. )
  415. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  416. //查询所有人员工资
  417. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  418. ->pluck('id')->toArray();
  419. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  420. ->where('del_time', 0)
  421. ->where("month", ">=", $month_start)
  422. ->where("month", "<", $month_end)
  423. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  424. ->pluck('month_str', 'id')
  425. ->toArray();
  426. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  427. ->select("employee_id", "salary", "main_id")
  428. ->get()->toArray();
  429. //查询所有项目人员的工时比例
  430. //汇总每个人每个月工资
  431. $salary_map = [];
  432. foreach ($month_employee_salary as $val) {
  433. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  434. if ($month) {
  435. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  436. }
  437. }
  438. // 2. 计算分摊天数与工资
  439. $item_list = [];
  440. foreach ($month_employee_list as $item) {
  441. $key = $item['employee_id'] . '_' . $item['order_month'];
  442. $item_key = $item['item_id'];
  443. if (!isset($item_list[$item_key])) {
  444. $item_list[$item_key] = [
  445. "salary" => 0,
  446. ];
  447. }
  448. $total_salary = $salary_map[$key] ?? 0;
  449. $total_min = $employee_monthly_total_min[$key] ?? 0;
  450. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  451. if ($total_min > 0) {
  452. $ratio = $item['total_work'] / $total_min;
  453. $allocated_salary = round($ratio * $total_salary, 2);
  454. } else {
  455. $allocated_salary = 0;
  456. }
  457. $item_list[$item_key]['salary'] += $allocated_salary;
  458. }
  459. return $item_list;
  460. }
  461. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  462. {
  463. //确认所有项目、设备、设备工时
  464. $month_device = DailyDwOrderDetails::Clear($user, $data);
  465. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  466. ->where('del_time', 0)
  467. ->select(
  468. "item_id",
  469. "device_id",
  470. // 将时间戳转为 Y-m-d 格式并起别名
  471. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  472. // 聚合求和
  473. DB::raw("SUM(total_work_min) as total_work")
  474. )
  475. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  476. //查询所有人员工资
  477. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  478. ->pluck('id')->toArray();
  479. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  480. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  481. ->pluck("month_str", 'id')->toArray();
  482. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  483. ->select("device_id", "depreciation_amount", "main_id")
  484. ->get()->toArray();
  485. //查询所有项目人员的工时比例
  486. //汇总每个人每个月工资
  487. $depreciatio_map = [];
  488. foreach ($month_device_salary as $val) {
  489. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  490. if ($month) {
  491. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  492. }
  493. }
  494. // 2. 计算每个员工在每个月的全月总工时
  495. $device_monthly_total_min = [];
  496. foreach ($month_device_list as $row) {
  497. $key = $row['device_id'] . '_' . $row['order_month'];
  498. if (!isset($device_monthly_total_min[$key])) {
  499. $device_monthly_total_min[$key] = 0;
  500. }
  501. $device_monthly_total_min[$key] += $row['total_work'];
  502. }
  503. // 3. 计算分摊天数与工资
  504. $item_list = [];
  505. foreach ($month_device_list as $item) {
  506. $key = $item['device_id'] . '_' . $item['order_month'];
  507. $device_key = $item['item_id'];
  508. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  509. $total_min = $device_monthly_total_min[$key] ?? 0;
  510. if (!isset($item_list[$device_key])) {
  511. $item_list[$device_key] = [
  512. "depreciation" => 0,
  513. ];
  514. }
  515. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  516. if ($total_min > 0) {
  517. $ratio = round($item['total_work'] / $total_min, 3);
  518. $allocated_salary = round($ratio * $total_depreciatio, 2);
  519. } else {
  520. $allocated_salary = 0;
  521. }
  522. $item_list[$device_key]['depreciation'] += $allocated_salary;
  523. }
  524. return $item_list;
  525. }
  526. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  527. {
  528. //确认所有项目、费用
  529. $expense = ExpenseClaimsDetails::Clear($user, $data);
  530. $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
  531. ->where('del_time', 0)
  532. ->select(
  533. "fee_id",
  534. "amount",
  535. "item_id",
  536. "entrust_type"
  537. )->get()->toArray();
  538. //需要根据分类去汇总
  539. $fee = Fee::Clear($user, $data);
  540. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  541. return $this->groupListByRoot($expense_list, $fee);
  542. }
  543. /**
  544. * 将报销明细按照一级费用和项目分类进行分组
  545. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  546. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  547. * @return array
  548. */
  549. public function groupListByRoot(array $list, array $fee_type_list)
  550. {
  551. // 1. 建立 ID 索引,方便快速查找
  552. $idMap = array_column($fee_type_list, null, 'id');
  553. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  554. $childToRoot = [];
  555. foreach ($fee_type_list as $type) {
  556. $current = $type;
  557. // 向上追溯直到 parent_id 为 0,即找到一级分类
  558. while ($current['parent_id'] != 0) {
  559. $current = $idMap[$current['parent_id']];
  560. }
  561. $childToRoot[$type['id']] = [
  562. 'id' => $current['id'],
  563. 'title' => $current['title'],
  564. 'sort' => $current['sort']
  565. ];
  566. }
  567. // 3. 遍历明细数据进行分组
  568. $item_key_list = [];
  569. $type_list = [];
  570. foreach ($list as $item) {
  571. $feeId = $item['fee_id'];
  572. // 获取该费用对应的一级分类信息
  573. if (!isset($childToRoot[$feeId])) continue;
  574. $rootId = $childToRoot[$feeId]['id'];
  575. $title = $childToRoot[$feeId]['title'];
  576. $sort = $childToRoot[$feeId]['sort'];
  577. $key = $item['item_id'];
  578. if (!isset($item_key_list[$key][$rootId])) {
  579. $item_key_list[$key][$rootId] = [
  580. 'id' => $rootId,
  581. 'total_amount' => 0,
  582. 'entrust1_amount' => 0, //委内
  583. 'entrust2_amount' => 0, //委外
  584. ];
  585. }
  586. if($item['entrust_type'] == 1){
  587. $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount'];
  588. }elseif ($item['entrust_type'] == 2){
  589. $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount'];
  590. }
  591. $item_key_list[$key][$rootId]['total_amount'] += $item['amount'];
  592. //这边需要拿到头部所有的一级费用类型
  593. if(!isset($type_list[$rootId])){
  594. $type_list[$rootId] = [
  595. 'sort' => $sort,
  596. 'id' => $rootId,
  597. 'title' => $title,
  598. ];
  599. }
  600. }
  601. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  602. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  603. // 4. 重置数组索引并返回
  604. return [$item_key_list,$type_list];
  605. }
  606. public function auxiliaryStatistic($data,$user){
  607. list($status, $month_start, $month_end) = $this->commonRule($data);
  608. if (!$status) return [false, $month_start];
  609. //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
  610. //确认所有项目
  611. $item = Item::Clear($user, $data);
  612. $item_list = $item->where('del_time', 0)
  613. ->where(function ($query) use ($month_start, $month_end) {
  614. $query->where('start_time', '>=', $month_start)
  615. ->orWhere('end_time', '<=', $month_end);
  616. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time","asc")->get()->toArray();
  617. $item_key_list = [];
  618. foreach ($item_list as $v) {
  619. $item_key_list[$v['id']] = $v;
  620. }
  621. //获取该区间内所有项目人工费、折旧费
  622. $item_salary = $this->auxiliaryEmployee($user,$data,$month_start,$month_end);
  623. $device_depreciation = $this->auxiliaryDevice($user,$data,$month_start,$month_end);
  624. $fee = Fee::Clear($user, $data);
  625. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  626. $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
  627. $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  628. ->where('del_time', 0)
  629. ->select(
  630. "item_id",
  631. "voucher_date",
  632. "voucher_type",
  633. "voucher_no",
  634. "voucher_remark",
  635. "voucher_amount",
  636. "aggregation_amount",
  637. "entrust_type",
  638. "entrust1_amount",
  639. "entrust2_amount",
  640. "fee_id",
  641. "type"
  642. )->get()->toArray();
  643. list($fee_amount,$fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list,$fee);
  644. // dd($item_key_list);
  645. $return = [];
  646. foreach ($fee_amount as $v){
  647. if(!isset($item_key_list[$v['item_id']])) continue;
  648. if(!isset($item_salary[$v['item_id']])) continue;
  649. if(!isset($device_depreciation[$v['item_id']])) continue;
  650. $item_value = $item_key_list[$v['item_id']];
  651. $detail = [
  652. "code" => $item_value['code'],
  653. "title" => $item_value['title'],
  654. "state" => $item_value['state'] == 0 ? "进行中":"已完成" ,
  655. "voucher_date" => date("Y-m-d",$v['voucher_date']) ,
  656. "voucher_type" => $v['voucher_type'] ,
  657. "voucher_no" => $v['voucher_no'] ,
  658. "voucher_remark" => $v['voucher_remark'] ,
  659. "voucher_amount" => $v['voucher_amount'] ,
  660. "aggregation_amount" => $v['aggregation_amount'] ,
  661. "total_amount" => $v['type'] == 1 ? $item_salary[$v['item_id']]['allocated_salary'] : ($v['type'] == 2 ? $device_depreciation[$v['item_id']]['allocated_depreciation'] : $v['total_amount']) ,
  662. "fee_id" => $v['fee_id'],
  663. "entrust1_amount" => $v['entrust1_amount'],
  664. "entrust2_amount" => $v['entrust2_amount'],
  665. "type" => $v['type'],
  666. ];
  667. $return[] = $detail;
  668. }
  669. return [true,[
  670. 'fee_type_list' => $fee_type_list,
  671. 'list' => $return,
  672. ]];
  673. }
  674. /**
  675. * 将报销明细按照一级费用和项目分类进行分组
  676. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  677. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  678. * @return array
  679. */
  680. public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
  681. {
  682. // 1. 建立 ID 索引,方便快速查找
  683. $idMap = array_column($fee_type_list, null, 'id');
  684. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  685. $childToRoot = [];
  686. foreach ($fee_type_list as $type) {
  687. $current = $type;
  688. // 向上追溯直到 parent_id 为 0,即找到一级分类
  689. while ($current['parent_id'] != 0) {
  690. $current = $idMap[$current['parent_id']];
  691. }
  692. $childToRoot[$type['id']] = [
  693. 'id' => $current['id'],
  694. 'title' => $current['title'],
  695. 'sort' => $current['sort']
  696. ];
  697. }
  698. // 3. 遍历明细数据进行分组
  699. $type_list = [];
  700. foreach ($list as $k=>$item) {
  701. if($item['item_id'] == 0||$item['fee_id'] == 0) {
  702. unset($list[$k]);
  703. continue;
  704. }
  705. $feeId = $item['fee_id'];
  706. // 获取该费用对应的一级分类信息
  707. if (!isset($childToRoot[$feeId])) continue;
  708. $rootId = $childToRoot[$feeId]['id'];
  709. $title = $childToRoot[$feeId]['title'];
  710. $sort = $childToRoot[$feeId]['sort'];
  711. $item['fee_id'] = $rootId;
  712. $list[$k] = $item;
  713. //这边需要拿到头部所有的一级费用类型
  714. if(!isset($type_list[$rootId])){
  715. $type_list[$rootId] = [
  716. 'sort' => $sort,
  717. 'id' => $rootId,
  718. 'title' => $title,
  719. ];
  720. }
  721. }
  722. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  723. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  724. // 4. 重置数组索引并返回
  725. return [$list,$type_list];
  726. }
  727. public function auxiliaryEmployee($user,$data,$month_start,$month_end){
  728. //确认所有项目、人员、人员工时
  729. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  730. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  731. ->where('del_time', 0)
  732. ->select(
  733. "item_id",
  734. "employee_id",
  735. // 将时间戳转为 Y-m-d 格式并起别名
  736. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  737. // 聚合求和
  738. DB::raw("SUM(total_work_min) as total_work")
  739. )
  740. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  741. //查询所有人员工资
  742. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  743. ->pluck('id')->toArray();
  744. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  745. ->where('del_time', 0)
  746. ->where("month", ">=", $month_start)
  747. ->where("month", "<", $month_end)
  748. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  749. ->pluck('month_str', 'id')
  750. ->toArray();
  751. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  752. ->select("employee_id", "salary", "main_id")
  753. ->get()->toArray();
  754. //查询所有项目人员的工时比例
  755. //汇总每个人每个月工资
  756. $salary_map = [];
  757. foreach ($month_employee_salary as $val) {
  758. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  759. if ($month) {
  760. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  761. }
  762. }
  763. // var_dump($salary_map);die;
  764. // 2. 计算每个员工在每个月的全月总工时
  765. $employee_monthly_total_min = [];
  766. foreach ($month_employee_list as $row) {
  767. $key = $row['employee_id'] . '_' . $row['order_month'];
  768. if (!isset($employee_monthly_total_min[$key])) {
  769. $employee_monthly_total_min[$key] = 0;
  770. }
  771. $employee_monthly_total_min[$key] += $row['total_work'];
  772. }
  773. // 3. 计算分摊天数与工资
  774. $item_year_list = [];
  775. foreach ($month_employee_list as $item) {
  776. $key = $item['employee_id'] . '_' . $item['order_month'];
  777. $item_key = $item['item_id'];
  778. if (!isset($item_year_list[$item_key])) {
  779. $item_year_list[$item_key] = [
  780. "allocated_salary" => 0,
  781. "item_id" => $item['item_id'],
  782. ];
  783. }
  784. $total_salary = $salary_map[$key] ?? 0;
  785. $total_min = $employee_monthly_total_min[$key] ?? 0;
  786. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  787. if ($total_min > 0) {
  788. $ratio = $item['total_work'] / $total_min;
  789. $allocated_salary = round($ratio * $total_salary, 2);
  790. } else {
  791. $allocated_salary = 0;
  792. }
  793. $item_year_list[$item_key]['allocated_salary'] += $allocated_salary;
  794. }
  795. return $item_year_list;
  796. }
  797. public function auxiliaryDevice($user,$data,$month_start,$month_end){
  798. $month_device = DailyDwOrderDetails::Clear($user, $data);
  799. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  800. ->where('del_time', 0)
  801. ->select(
  802. "item_id",
  803. "device_id",
  804. // 将时间戳转为 Y-m-d 格式并起别名
  805. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  806. // 聚合求和
  807. DB::raw("SUM(total_work_min) as total_work")
  808. )
  809. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  810. //查询所有人员工资
  811. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  812. ->pluck('id')->toArray();
  813. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  814. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  815. ->pluck("month_str", 'id')->toArray();
  816. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  817. ->select("device_id", "depreciation_amount", "main_id")
  818. ->get()->toArray();
  819. //查询所有项目人员的工时比例
  820. //汇总每个人每个月工资
  821. $depreciatio_map = [];
  822. foreach ($month_device_salary as $val) {
  823. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  824. if ($month) {
  825. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  826. }
  827. }
  828. // 2. 计算每个员工在每个月的全月总工时
  829. $device_monthly_total_min = [];
  830. foreach ($month_device_list as $row) {
  831. $key = $row['device_id'] . '_' . $row['order_month'];
  832. if (!isset($device_monthly_total_min[$key])) {
  833. $device_monthly_total_min[$key] = 0;
  834. }
  835. $device_monthly_total_min[$key] += $row['total_work'];
  836. }
  837. // 3. 计算分摊天数与工资
  838. $item_year_list = [];
  839. foreach ($month_device_list as $item) {
  840. $key = $item['device_id'] . '_' . $row['order_month'] ;
  841. $device_key = $item['item_id'];
  842. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  843. $total_min = $device_monthly_total_min[$key] ?? 0;
  844. if (!isset($item_year_list[$device_key])) {
  845. $item_year_list[$device_key] = [
  846. "allocated_depreciation" => 0,
  847. ];
  848. }
  849. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  850. if ($total_min > 0) {
  851. $ratio = round($item['total_work'] / $total_min, 3);
  852. $allocated_salary = round($ratio * $total_depreciatio, 2);
  853. } else {
  854. $allocated_salary = 0;
  855. }
  856. $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
  857. }
  858. return $item_year_list;
  859. }
  860. public function itemEmployeeSalaryStatistic($data,$user){
  861. list($status, $month_start, $month_end) = $this->commonRule($data);
  862. if (!$status) return [false, $month_start];
  863. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  864. //获取人员工资项目相关分组数据
  865. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  866. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  867. ->where('del_time', 0)
  868. ->select(
  869. "item_id",
  870. "employee_id",
  871. // 将时间戳转为 Y-m-d 格式并起别名
  872. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  873. // 聚合求和
  874. DB::raw("SUM(total_work_min) as total_work")
  875. )
  876. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  877. //查询所有人员工资
  878. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  879. ->pluck('id')->toArray();
  880. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  881. ->where('del_time', 0)
  882. ->where("month", ">=", $month_start)
  883. ->where("month", "<", $month_end)
  884. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  885. ->pluck('month_str', 'id')
  886. ->toArray();
  887. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  888. ->select("employee_id", "salary", "main_id","social_insurance","public_housing_fund")
  889. ->get()->toArray();
  890. //查询所有项目人员的工时比例
  891. //汇总每个人每个月工资
  892. $salary_map = [];
  893. foreach ($month_employee_salary as $val) {
  894. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  895. if ($month) {
  896. $salary_map[$val['employee_id'] . '_' . $month] = $val;
  897. }
  898. }
  899. // var_dump($salary_map);die;
  900. // 2. 计算每个员工在每个月的全月总工时
  901. $employee_monthly_total_min = [];
  902. foreach ($month_employee_list as $row) {
  903. $key = $row['employee_id'] . '_' . $row['order_month'];
  904. if (!isset($employee_monthly_total_min[$key])) {
  905. $employee_monthly_total_min[$key] = 0;
  906. }
  907. $employee_monthly_total_min[$key] += $row['total_work'];
  908. }
  909. // 3. 计算分摊天数与工资
  910. $item_month_list = [];
  911. foreach ($month_employee_list as $item) {
  912. $key = $item['employee_id'] . '_' . $item['order_month'];
  913. $item_key = $item['order_month'] . '_' . $item['item_id'].'_'.$item['employee_id'] ;
  914. if (!isset($item_month_list[$item_key])) {
  915. $item_month_list[$item_key] = [
  916. "month" => $item['order_month'],
  917. "allocated_salary" => 0,
  918. "employee_id" => $item['employee_id'],
  919. "work_minutes" => 0,
  920. "salary" => $salary_map[$key]['salary'] ?? 0,
  921. "social_insurance" => $salary_map[$key]['social_insurance']??0,
  922. "public_housing_fund" => $salary_map[$key]['public_housing_fund']??0,
  923. "item_id" => $item['item_id'],
  924. ];
  925. }
  926. $total_min = $employee_monthly_total_min[$key] ?? 0;
  927. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  928. if ($total_min > 0) {
  929. $ratio = round($item['total_work'] / $total_min,4);
  930. }else{
  931. $ratio = 0;
  932. }
  933. $item_month_list[$item_key]['radio'] = $ratio;
  934. $item_month_list[$item_key]['total_min'] = $total_min;
  935. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  936. }
  937. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  938. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  939. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  940. $employee = Employee::Clear($user, $data);
  941. $employee_list = $employee->wherein('id', $employee_ids)->select("major","title", "id")->get()->toArray();
  942. $employee_key_list = [];
  943. foreach ($employee_list as $v){
  944. $employee_key_list[$v['id']] = $v;
  945. }
  946. $item = Item::Clear($user, $data);
  947. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  948. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  949. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  950. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list,$employee_key_list) {
  951. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  952. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  953. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['employee_id']})";
  954. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
  955. $item['total_min'] = round($item['total_min']/60,2);
  956. $item['work_minutes'] = round($item['work_minutes']/60,2);
  957. $item['work_salary'] = round($item['salary']*$item['radio'],2);
  958. $item['work_social_insurance'] = round($item['social_insurance']*$item['radio'],2);
  959. $item['work_public_housing_fund'] = round($item['public_housing_fund']*$item['radio'],2);
  960. return $item;
  961. })->all();
  962. return [true, $item_month_list];
  963. }
  964. }