StatisticService.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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['data']);
  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['data'] = collect($month_employee_list['data'])->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]['days'] = round($v['work_minutes'] / 8 / 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['month_start'])) $month_start = date('Y-01-01');
  347. else $month_start = date('Y-m-01', strtotime($data['month_start']));
  348. if (!isset($data['month_end'])) $month_end = date('Y-01-01', strtotime('+1 year', strtotime($month_start)));
  349. else {
  350. $start_year = date('Y', strtotime($month_start));
  351. $end_year = date('Y', strtotime($data['month_end']));
  352. if ($start_year != $end_year) return [false, "查询不得跨年!", ""];
  353. $month_end = date('Y-m-01', strtotime($data['month_end'] . ' +1 month'));
  354. }
  355. return [true, strtotime($month_start), strtotime($month_end)];
  356. }
  357. public function employeeAttendanceMonthStatistic($data, $user)
  358. {
  359. //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
  360. list($status, $month_start, $month_end) = $this->commonRule($data);
  361. if (!$status) {
  362. return [false, $month_start];
  363. }
  364. //第一步确定项目
  365. $item = Item::Clear($user, $data);
  366. $item_list = $item->where('del_time', 0)
  367. ->where(function ($query) use ($month_start, $month_end) {
  368. $query->where('start_time', '>=', $month_start)
  369. ->orWhere('end_time', '<=', $month_end);
  370. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time","asc")->get()->toArray();
  371. $item_key_list = [];
  372. foreach ($item_list as $v) {
  373. $item_key_list[$v['id']] = $v;
  374. }
  375. //第二步确定人员费用
  376. $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
  377. //第三步确定折旧费用
  378. $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
  379. //第四步其他费用
  380. list($item_fee_list,$fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  381. //组合所有数据
  382. $return = [];
  383. foreach ($item_key_list as $v){
  384. //其他费用是个数组
  385. $item_value = [
  386. "code" => $v['code'],
  387. "title" => $v['title'],
  388. "state" => $v['state'] == 0 ? "进行中" : "完结",
  389. "employee_salary" => $item_employee_list[$v['id']]['salary']??0,
  390. "device_depreciation" => $item_device_list[$v['id']]['depreciation']??0,
  391. "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
  392. ];
  393. $return[] = $item_value;
  394. }
  395. return [true,["list"=>$return,"fee_type_list"=>$fee_type_list]];
  396. }
  397. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  398. {
  399. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  400. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  401. ->where('del_time', 0)
  402. ->select(
  403. "item_id",
  404. "employee_id",
  405. // 将时间戳转为 Y-m-d 格式并起别名
  406. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  407. // 聚合求和
  408. DB::raw("SUM(total_work_min) as total_work")
  409. )
  410. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  411. //查询所有人员工资
  412. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  413. ->pluck('id')->toArray();
  414. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  415. ->where('del_time', 0)
  416. ->where("month", ">=", $month_start)
  417. ->where("month", "<", $month_end)
  418. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  419. ->pluck('month_str', 'id')
  420. ->toArray();
  421. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  422. ->select("employee_id", "salary", "main_id")
  423. ->get()->toArray();
  424. //查询所有项目人员的工时比例
  425. //汇总每个人每个月工资
  426. $salary_map = [];
  427. foreach ($month_employee_salary as $val) {
  428. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  429. if ($month) {
  430. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  431. }
  432. }
  433. // 2. 计算分摊天数与工资
  434. $item_list = [];
  435. foreach ($month_employee_list as $item) {
  436. $key = $item['employee_id'] . '_' . $item['order_month'];
  437. $item_key = $item['item_id'];
  438. if (!isset($item_list[$item_key])) {
  439. $item_list[$item_key] = [
  440. "salary" => 0,
  441. ];
  442. }
  443. $total_salary = $salary_map[$key] ?? 0;
  444. $total_min = $employee_monthly_total_min[$key] ?? 0;
  445. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  446. if ($total_min > 0) {
  447. $ratio = $item['total_work'] / $total_min;
  448. $allocated_salary = round($ratio * $total_salary, 2);
  449. } else {
  450. $allocated_salary = 0;
  451. }
  452. $item_list[$item_key]['salary'] += $allocated_salary;
  453. }
  454. return $item_list;
  455. }
  456. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  457. {
  458. //确认所有项目、设备、设备工时
  459. $month_device = DailyDwOrderDetails::Clear($user, $data);
  460. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  461. ->where('del_time', 0)
  462. ->select(
  463. "item_id",
  464. "device_id",
  465. // 将时间戳转为 Y-m-d 格式并起别名
  466. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  467. // 聚合求和
  468. DB::raw("SUM(total_work_min) as total_work")
  469. )
  470. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  471. //查询所有人员工资
  472. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  473. ->pluck('id')->toArray();
  474. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  475. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  476. ->pluck("month_str", 'id')->toArray();
  477. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  478. ->select("device_id", "depreciation_amount", "main_id")
  479. ->get()->toArray();
  480. //查询所有项目人员的工时比例
  481. //汇总每个人每个月工资
  482. $depreciatio_map = [];
  483. foreach ($month_device_salary as $val) {
  484. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  485. if ($month) {
  486. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  487. }
  488. }
  489. // 2. 计算每个员工在每个月的全月总工时
  490. $device_monthly_total_min = [];
  491. foreach ($month_device_list as $row) {
  492. $key = $row['device_id'] . '_' . $row['order_month'];
  493. if (!isset($device_monthly_total_min[$key])) {
  494. $device_monthly_total_min[$key] = 0;
  495. }
  496. $device_monthly_total_min[$key] += $row['total_work'];
  497. }
  498. // 3. 计算分摊天数与工资
  499. $item_list = [];
  500. foreach ($month_device_list as $item) {
  501. $key = $item['device_id'] . '_' . $item['order_month'];
  502. $device_key = $item['item_id'];
  503. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  504. $total_min = $device_monthly_total_min[$key] ?? 0;
  505. if (!isset($item_list[$device_key])) {
  506. $item_list[$device_key] = [
  507. "depreciation" => 0,
  508. ];
  509. }
  510. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  511. if ($total_min > 0) {
  512. $ratio = round($item['total_work'] / $total_min, 3);
  513. $allocated_salary = round($ratio * $total_depreciatio, 2);
  514. } else {
  515. $allocated_salary = 0;
  516. }
  517. $item_list[$device_key]['depreciation'] += $allocated_salary;
  518. }
  519. return $item_list;
  520. }
  521. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  522. {
  523. //确认所有项目、费用
  524. $expense = ExpenseClaimsDetails::Clear($user, $data);
  525. $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
  526. ->where('del_time', 0)
  527. ->select(
  528. "fee_id",
  529. "amount",
  530. "item_id",
  531. "entrust_type"
  532. )->get()->toArray();
  533. //需要根据分类去汇总
  534. $fee = Fee::Clear($user, $data);
  535. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  536. return $this->groupListByRoot($expense_list, $fee);
  537. }
  538. /**
  539. * 将报销明细按照一级费用和项目分类进行分组
  540. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  541. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  542. * @return array
  543. */
  544. public function groupListByRoot(array $list, array $fee_type_list)
  545. {
  546. // 1. 建立 ID 索引,方便快速查找
  547. $idMap = array_column($fee_type_list, null, 'id');
  548. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  549. $childToRoot = [];
  550. foreach ($fee_type_list as $type) {
  551. $current = $type;
  552. // 向上追溯直到 parent_id 为 0,即找到一级分类
  553. while ($current['parent_id'] != 0) {
  554. $current = $idMap[$current['parent_id']];
  555. }
  556. $childToRoot[$type['id']] = [
  557. 'id' => $current['id'],
  558. 'title' => $current['title'],
  559. 'sort' => $current['sort']
  560. ];
  561. }
  562. // 3. 遍历明细数据进行分组
  563. $item_key_list = [];
  564. $type_list = [];
  565. foreach ($list as $item) {
  566. $feeId = $item['fee_id'];
  567. // 获取该费用对应的一级分类信息
  568. if (!isset($childToRoot[$feeId])) continue;
  569. $rootId = $childToRoot[$feeId]['id'];
  570. $title = $childToRoot[$feeId]['title'];
  571. $sort = $childToRoot[$feeId]['sort'];
  572. $key = $item['item_id'];
  573. if (!isset($item_key_list[$key][$rootId])) {
  574. $item_key_list[$key][$rootId] = [
  575. 'id' => $rootId,
  576. 'total_amount' => 0,
  577. 'entrust1_amount' => 0, //委内
  578. 'entrust2_amount' => 0, //委外
  579. ];
  580. }
  581. if($item['entrust_type'] == 1){
  582. $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount'];
  583. }elseif ($item['entrust_type'] == 2){
  584. $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount'];
  585. }
  586. $item_key_list[$key][$rootId]['total_amount'] += $item['amount'];
  587. //这边需要拿到头部所有的一级费用类型
  588. if(!isset($type_list[$rootId])){
  589. $type_list[$rootId] = [
  590. 'sort' => $sort,
  591. 'id' => $rootId,
  592. 'title' => $title,
  593. ];
  594. }
  595. }
  596. // dd($type_list);
  597. $type_list = collect($type_list)->sortBy('sort')->all();
  598. // 4. 重置数组索引并返回
  599. return [$item_key_list,$type_list];
  600. }
  601. public function auxiliaryStatistic($data,$user){
  602. list($status, $month_start, $month_end) = $this->commonRule($data);
  603. if (!$status) return [false, $month_start];
  604. //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
  605. //确认所有项目
  606. $item = Item::Clear($user, $data);
  607. $item_list = $item->where('del_time', 0)
  608. ->where(function ($query) use ($month_start, $month_end) {
  609. $query->where('start_time', '>=', $month_start)
  610. ->orWhere('end_time', '<=', $month_end);
  611. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time","asc")->get()->toArray();
  612. $item_key_list = [];
  613. foreach ($item_list as $v) {
  614. $item_key_list[$v['id']] = $v;
  615. }
  616. //获取该区间内所有项目人工费、折旧费
  617. $item_salary = $this->auxiliaryEmployee($user,$data,$month_start,$month_end);
  618. $device_depreciation = $this->auxiliaryDevice($user,$data,$month_start,$month_end);
  619. $fee = Fee::Clear($user, $data);
  620. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  621. $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
  622. $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  623. ->where('del_time', 0)
  624. ->select(
  625. "item_id",
  626. "voucher_date",
  627. "voucher_type",
  628. "voucher_no",
  629. "voucher_remark",
  630. "voucher_amount",
  631. "aggregation_amount",
  632. "entrust_type",
  633. "entrust1_amount",
  634. "entrust2_amount",
  635. "fee_id",
  636. "type"
  637. )->get()->toArray();
  638. list($fee_amount,$fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list,$fee);
  639. // dd($item_key_list);
  640. $return = [];
  641. foreach ($fee_amount as $v){
  642. if(!isset($item_key_list[$v['item_id']])) continue;
  643. if(!isset($item_salary[$v['item_id']])) continue;
  644. if(!isset($device_depreciation[$v['item_id']])) continue;
  645. $item_value = $item_key_list[$v['item_id']];
  646. $detail = [
  647. "code" => $item_value['code'],
  648. "title" => $item_value['title'],
  649. "state" => $item_value['state'] == 0 ? "进行中":"已完成" ,
  650. "voucher_date" => date("Y-m-d",$v['voucher_date']) ,
  651. "voucher_type" => $v['voucher_type'] ,
  652. "voucher_no" => $v['voucher_no'] ,
  653. "voucher_remark" => $v['voucher_remark'] ,
  654. "voucher_amount" => $v['voucher_amount'] ,
  655. "aggregation_amount" => $v['aggregation_amount'] ,
  656. "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']) ,
  657. "fee_id" => $v['fee_id'],
  658. "entrust1_amount" => $v['entrust1_amount'],
  659. "entrust2_amount" => $v['entrust2_amount'],
  660. "type" => $v['type'],
  661. ];
  662. $return[] = $detail;
  663. }
  664. return [true,[
  665. 'fee_type_list' => $fee_type_list,
  666. 'list' => $return,
  667. ]];
  668. }
  669. /**
  670. * 将报销明细按照一级费用和项目分类进行分组
  671. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  672. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  673. * @return array
  674. */
  675. public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
  676. {
  677. // 1. 建立 ID 索引,方便快速查找
  678. $idMap = array_column($fee_type_list, null, 'id');
  679. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  680. $childToRoot = [];
  681. foreach ($fee_type_list as $type) {
  682. $current = $type;
  683. // 向上追溯直到 parent_id 为 0,即找到一级分类
  684. while ($current['parent_id'] != 0) {
  685. $current = $idMap[$current['parent_id']];
  686. }
  687. $childToRoot[$type['id']] = [
  688. 'id' => $current['id'],
  689. 'title' => $current['title'],
  690. 'sort' => $current['sort']
  691. ];
  692. }
  693. // 3. 遍历明细数据进行分组
  694. $type_list = [];
  695. foreach ($list as $k=>$item) {
  696. if($item['item_id'] == 0||$item['fee_id'] == 0) {
  697. unset($list[$k]);
  698. continue;
  699. }
  700. $feeId = $item['fee_id'];
  701. // 获取该费用对应的一级分类信息
  702. if (!isset($childToRoot[$feeId])) continue;
  703. $rootId = $childToRoot[$feeId]['id'];
  704. $title = $childToRoot[$feeId]['title'];
  705. $sort = $childToRoot[$feeId]['sort'];
  706. $item['fee_id'] = $rootId;
  707. $list[$k] = $item;
  708. //这边需要拿到头部所有的一级费用类型
  709. if(!isset($type_list[$rootId])){
  710. $type_list[$rootId] = [
  711. 'sort' => $sort,
  712. 'id' => $rootId,
  713. 'title' => $title,
  714. ];
  715. }
  716. }
  717. $type_list = collect($type_list)->sortBy('sort')->all();
  718. // 4. 重置数组索引并返回
  719. return [$list,$type_list];
  720. }
  721. public function auxiliaryEmployee($user,$data,$month_start,$month_end){
  722. //确认所有项目、人员、人员工时
  723. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  724. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  725. ->where('del_time', 0)
  726. ->select(
  727. "item_id",
  728. "employee_id",
  729. // 将时间戳转为 Y-m-d 格式并起别名
  730. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  731. // 聚合求和
  732. DB::raw("SUM(total_work_min) as total_work")
  733. )
  734. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  735. //查询所有人员工资
  736. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  737. ->pluck('id')->toArray();
  738. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  739. ->where('del_time', 0)
  740. ->where("month", ">=", $month_start)
  741. ->where("month", "<", $month_end)
  742. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  743. ->pluck('month_str', 'id')
  744. ->toArray();
  745. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  746. ->select("employee_id", "salary", "main_id")
  747. ->get()->toArray();
  748. //查询所有项目人员的工时比例
  749. //汇总每个人每个月工资
  750. $salary_map = [];
  751. foreach ($month_employee_salary as $val) {
  752. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  753. if ($month) {
  754. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  755. }
  756. }
  757. // var_dump($salary_map);die;
  758. // 2. 计算每个员工在每个月的全月总工时
  759. $employee_monthly_total_min = [];
  760. foreach ($month_employee_list as $row) {
  761. $key = $row['employee_id'] . '_' . $row['order_month'];
  762. if (!isset($employee_monthly_total_min[$key])) {
  763. $employee_monthly_total_min[$key] = 0;
  764. }
  765. $employee_monthly_total_min[$key] += $row['total_work'];
  766. }
  767. // 3. 计算分摊天数与工资
  768. $item_year_list = [];
  769. foreach ($month_employee_list as $item) {
  770. $key = $item['employee_id'] . '_' . $item['order_month'];
  771. $item_key = $item['item_id'];
  772. if (!isset($item_year_list[$item_key])) {
  773. $item_year_list[$item_key] = [
  774. "allocated_salary" => 0,
  775. "item_id" => $item['item_id'],
  776. ];
  777. }
  778. $total_salary = $salary_map[$key] ?? 0;
  779. $total_min = $employee_monthly_total_min[$key] ?? 0;
  780. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  781. if ($total_min > 0) {
  782. $ratio = $item['total_work'] / $total_min;
  783. $allocated_salary = round($ratio * $total_salary, 2);
  784. } else {
  785. $allocated_salary = 0;
  786. }
  787. $item_year_list[$item_key]['allocated_salary'] += $allocated_salary;
  788. }
  789. return $item_year_list;
  790. }
  791. public function auxiliaryDevice($user,$data,$month_start,$month_end){
  792. $month_device = DailyDwOrderDetails::Clear($user, $data);
  793. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  794. ->where('del_time', 0)
  795. ->select(
  796. "item_id",
  797. "device_id",
  798. // 将时间戳转为 Y-m-d 格式并起别名
  799. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  800. // 聚合求和
  801. DB::raw("SUM(total_work_min) as total_work")
  802. )
  803. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  804. //查询所有人员工资
  805. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  806. ->pluck('id')->toArray();
  807. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  808. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  809. ->pluck("month_str", 'id')->toArray();
  810. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  811. ->select("device_id", "depreciation_amount", "main_id")
  812. ->get()->toArray();
  813. //查询所有项目人员的工时比例
  814. //汇总每个人每个月工资
  815. $depreciatio_map = [];
  816. foreach ($month_device_salary as $val) {
  817. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  818. if ($month) {
  819. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  820. }
  821. }
  822. // 2. 计算每个员工在每个月的全月总工时
  823. $device_monthly_total_min = [];
  824. foreach ($month_device_list as $row) {
  825. $key = $row['device_id'] . '_' . $row['order_month'];
  826. if (!isset($device_monthly_total_min[$key])) {
  827. $device_monthly_total_min[$key] = 0;
  828. }
  829. $device_monthly_total_min[$key] += $row['total_work'];
  830. }
  831. // 3. 计算分摊天数与工资
  832. $item_year_list = [];
  833. foreach ($month_device_list as $item) {
  834. $key = $item['device_id'] . '_' . $row['order_month'] ;
  835. $device_key = $item['item_id'];
  836. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  837. $total_min = $device_monthly_total_min[$key] ?? 0;
  838. if (!isset($item_year_list[$device_key])) {
  839. $item_year_list[$device_key] = [
  840. "allocated_depreciation" => 0,
  841. ];
  842. }
  843. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  844. if ($total_min > 0) {
  845. $ratio = round($item['total_work'] / $total_min, 3);
  846. $allocated_salary = round($ratio * $total_depreciatio, 2);
  847. } else {
  848. $allocated_salary = 0;
  849. }
  850. $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
  851. }
  852. return $item_year_list;
  853. }
  854. public function itemEmployeeSalaryStatistic($data,$user){
  855. list($status, $month_start, $month_end) = $this->commonRule($data);
  856. if (!$status) return [false, $month_start];
  857. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  858. //获取人员工资项目相关分组数据
  859. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  860. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  861. ->where('del_time', 0)
  862. ->select(
  863. "item_id",
  864. "employee_id",
  865. // 将时间戳转为 Y-m-d 格式并起别名
  866. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  867. // 聚合求和
  868. DB::raw("SUM(total_work_min) as total_work")
  869. )
  870. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  871. //查询所有人员工资
  872. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  873. ->pluck('id')->toArray();
  874. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  875. ->where('del_time', 0)
  876. ->where("month", ">=", $month_start)
  877. ->where("month", "<", $month_end)
  878. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  879. ->pluck('month_str', 'id')
  880. ->toArray();
  881. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  882. ->select("employee_id", "salary", "main_id","social_insurance","public_housing_fund")
  883. ->get()->toArray();
  884. //查询所有项目人员的工时比例
  885. //汇总每个人每个月工资
  886. $salary_map = [];
  887. foreach ($month_employee_salary as $val) {
  888. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  889. if ($month) {
  890. $salary_map[$val['employee_id'] . '_' . $month] = $val;
  891. }
  892. }
  893. // var_dump($salary_map);die;
  894. // 2. 计算每个员工在每个月的全月总工时
  895. $employee_monthly_total_min = [];
  896. foreach ($month_employee_list as $row) {
  897. $key = $row['employee_id'] . '_' . $row['order_month'];
  898. if (!isset($employee_monthly_total_min[$key])) {
  899. $employee_monthly_total_min[$key] = 0;
  900. }
  901. $employee_monthly_total_min[$key] += $row['total_work'];
  902. }
  903. // 3. 计算分摊天数与工资
  904. $item_month_list = [];
  905. foreach ($month_employee_list as $item) {
  906. $key = $item['employee_id'] . '_' . $item['order_month'];
  907. $item_key = $item['order_month'] . '_' . $item['item_id'].'_'.$item['employee_id'] ;
  908. if (!isset($item_month_list[$item_key])) {
  909. $item_month_list[$item_key] = [
  910. "month" => $item['order_month'],
  911. "allocated_salary" => 0,
  912. "employee_id" => 0,
  913. "work_minutes" => 0,
  914. "salary" => $salary_map[$key]['salary'],
  915. "social_insurance" => $salary_map[$key]['social_insurance'],
  916. "public_housing_fund" => $salary_map[$key]['public_housing_fund'],
  917. "item_id" => $item['item_id'],
  918. ];
  919. }
  920. $total_min = $employee_monthly_total_min[$key] ?? 0;
  921. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  922. if ($total_min > 0) {
  923. $ratio = round($item['total_work'] / $total_min,4);
  924. }else{
  925. $ratio = 0;
  926. }
  927. $item_month_list[$item_key]['ratio'] = $ratio;
  928. $item_month_list[$item_key]['total_min'] = $total_min;
  929. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  930. }
  931. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  932. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  933. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  934. $employee = Employee::Clear($user, $data);
  935. $employee_list = $employee->wherein('id', $employee_ids)->select("major","title", "id")->get()->toArray();
  936. $employee_key_list = [];
  937. foreach ($employee_list as $v){
  938. $employee_key_list[$v['id']] = $v;
  939. }
  940. $item = Item::Clear($user, $data);
  941. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  942. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  943. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  944. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list,$employee_key_list) {
  945. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  946. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  947. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['item_id']})";
  948. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['item_id']})";
  949. $item['total_min'] = round($item['total_min']/60,2);
  950. $item['work_minutes'] = round($item['work_minutes']/60,2);
  951. $item['work_salary'] = round($item['work_salary']*$item['radio'],2);
  952. $item['work_social_insurance'] = round($item['social_insurance']*$item['radio'],2);
  953. $item['work_public_housing_fund'] = round($item['public_housing_fund']*$item['radio'],2);
  954. return $item;
  955. })->all();
  956. return [true, $item_month_list];
  957. }
  958. }