StatisticService.php 54 KB

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