StatisticService.php 54 KB

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