StatisticService.php 57 KB

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