StatisticService.php 58 KB

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