StatisticService.php 50 KB

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