StatisticService.php 50 KB

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