StatisticService.php 50 KB

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