StatisticService.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. 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"]);
  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_count = [
  103. "all_salary" => $item_count,
  104. "item_day_sum" => $item_count,
  105. ];
  106. $sums = ["item_day_sum"=>$item_day_count,"all_salary"=>$all_salary];
  107. $word_keys = [
  108. "all_salary" =>
  109. [
  110. "key" => "allocated_salary",
  111. "value" => "allocated_salary",
  112. "type" => "money",
  113. ],
  114. "item_day_sum" =>
  115. [
  116. "key" => "days",
  117. "value" => "days",
  118. "type" => "",
  119. ],
  120. ];
  121. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, &$item_count,&$sums,&$word_keys) {
  122. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  123. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  124. // $item['allocated_salary'] = round($item['allocated_salary']/100, 2);
  125. $key = $item['month'];
  126. $this->calculateClosure($key,$item,$item_count,$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,$salary_all) = $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_all];
  167. $word_keys = [
  168. "allocated_salary" =>
  169. [
  170. "key" => "allocated_salary",
  171. "value" => "allocated_salary",
  172. "type" => "money",
  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['total_min'] / 60);
  190. $key = $item['employee_id'] . '_' . $item['month'];
  191. // // 如果不是最后一条
  192. $this->calculateClosure($key,$item,$counts,$sums,$word_keys);
  193. return $item;
  194. })->all();
  195. return [true, $item_month_list];
  196. }
  197. public function itemDeviceMonthStatistic($data, $user)
  198. {
  199. //项目编码、项目名称、设备名称、项目工时、研发工时占比、设备原值、设备折旧额、本项目帐面归集的折旧额、确定的本项目折旧额、加计调整金额、当月工时、日期
  200. list($status, $month_start, $month_end) = $this->commonRule($data);
  201. if (!$status) return [false, $month_start];
  202. //确认所有项目、设备、设备工时
  203. $month_device_list = $this->getItemDeviceMonthWorkList($user, $data, $month_start, $month_end);
  204. //查询所有设备工资
  205. $depreciation_map = $this->getDeviceAmount($user, $data, $month_start, $month_end);
  206. // 2. 计算每个设备在每个月的全月总工时
  207. $keys = ["device_id","order_month"];
  208. $device_monthly_total_min = $this->calculateSum($month_device_list,$keys,"total_work");
  209. // 3. 计算设备分摊天数与工资
  210. 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"]);
  211. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  212. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  213. $item = Item::TopClear($user, $data);
  214. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  215. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  216. $device_ids = collect($item_month_list)->pluck('device_id')->unique()->values()->all();
  217. $device = Device::TopClear($user, $data);
  218. $device_key_list = $device->wherein('id', $device_ids)->pluck("title", "id")->toArray();
  219. $device_original_value_key_list = $device->wherein('id', $device_ids)->pluck("original_value", "id")->toArray();
  220. $collect = collect($item_month_list);
  221. $word_keys = [
  222. "total_depreciation" =>
  223. [
  224. "key" => "allocated_depreciation",
  225. "value" => "allocated_depreciation",
  226. "type" => "money",
  227. ],
  228. "total_hours" =>
  229. [
  230. "key" => "hours",
  231. "value" => "hours",
  232. "type" => "",
  233. ],
  234. "ratio" =>
  235. [
  236. "key" => "ratio",
  237. "value" => "ratio",
  238. "type" => "ratio",
  239. ],
  240. ];
  241. $device_count = $collect->groupBy(function ($item) {
  242. // 这里的 $item 是集合中的每一行数据
  243. return $item['device_id'] . '_' . $item['month'];
  244. })->map(function ($group) {
  245. return $group->count();
  246. })->toArray();
  247. $device_counts = [];
  248. foreach ($word_keys as $k=>$v){
  249. $device_counts[$k] = $device_count;
  250. }
  251. $device_total_depreciation['ratio'] = [];
  252. // dd($item_month_list);
  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['device_original'] = $device_original_value_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  258. $item['hours'] = round($item['work_minutes'] / 60, 1);
  259. $item['total_hours'] = round($item['total_min'] / 60, 1);
  260. $key = $item['device_id'] . '_' . $item['month'];
  261. $this->calculateClosure($key,$item,$device_counts,$device_total_depreciation,$word_keys);
  262. return $item;
  263. })->all();
  264. return [true, $item_month_list];
  265. }
  266. public function employeeAttendanceMonthStatistic($data, $user)
  267. {
  268. //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
  269. list($status, $month_start, $month_end) = $this->commonRule($data);
  270. if (!$status) {
  271. return [false, $month_start];
  272. }
  273. //第一步确定项目
  274. $item = Item::TopClear($user, $data);
  275. $item_list = $item->where('del_time', 0)
  276. ->where(function ($query) use ($month_start, $month_end) {
  277. $query->where('start_time', '>=', $month_start)
  278. ->where('end_time', '<', $month_end);
  279. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time", "asc")->get()->toArray();
  280. $item_key_list = [];
  281. foreach ($item_list as $v) {
  282. $item_key_list[$v['id']] = $v;
  283. }
  284. //第二步确定人员费用
  285. $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
  286. //第三步确定折旧费用
  287. $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
  288. //第四步其他费用
  289. list($item_fee_list, $fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  290. //组合所有数据
  291. $return = [];
  292. foreach ($item_key_list as $v) {
  293. //其他费用是个数组
  294. $item_value = [
  295. "code" => $v['code'],
  296. "title" => $v['title'],
  297. "state" => $v['state'] == 3 ? "完结" : "进行中",
  298. "employee_salary" => isset($item_employee_list[$v['id']]['salary']) ? round($item_employee_list[$v['id']]['salary']/100,2) : 0,
  299. "device_depreciation" => isset($item_device_list[$v['id']]['depreciation']) ? round($item_device_list[$v['id']]['depreciation']/100,2): 0,
  300. "expense_type" => "费用化支出",
  301. "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
  302. ];
  303. $return[] = $item_value;
  304. }
  305. return [true, ["list" => $return, "fee_type_list" => $fee_type_list]];
  306. }
  307. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  308. {
  309. $month_employee_list = $this->getItemEmployeeMonthWorkList($user, $data, $month_start, $month_end);
  310. //查询所有人员工资
  311. $salary_map = $this->getEmployeeSalary($user, $data, $month_start, $month_end);
  312. // 2. 计算每个员工在每个月的全月总工时
  313. $employee_monthly_total_min = $this->calculateSum($month_employee_list,["employee_id","order_month"],"total_work");
  314. // 2. 计算分摊天数与工资
  315. list($item_list,) = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,["employee_id","order_month"],["order_month","item_id","employee_id"]);
  316. $collect = collect($item_list);
  317. $employee_count = $collect->groupBy(function ($item) {
  318. // 这里的 $item 是集合中的每一行数据
  319. return $item['employee_id'] . '_' . $item['month'];
  320. })->map(function ($group) {
  321. return $group->count();
  322. })->toArray();
  323. $month_employee_list = $collect->transform(function ($item) use (&$employee_count,&$salary_map) {
  324. // 如果不是最后一条
  325. $key = $item['employee_id'] . '_' . $item['month'];
  326. $sums = [
  327. "salary_map" => $salary_map
  328. ];
  329. $word_keys = [
  330. "salary_map" => [
  331. "key" => "allocated_salary",
  332. "value" => "allocated_salary",
  333. "type" => "money",
  334. ]
  335. ];
  336. $this->calculateClosure($key,$item,$employee_count,$sums,$word_keys);
  337. // if(!isset($all_salary[$key])) $item['allocated_salary'] = 0;
  338. // else{
  339. // if (--$employee_count[$key] > 0) {
  340. // $all_salary[$key] -= $item['allocated_salary'];
  341. // } else {
  342. // $item['allocated_salary'] = $all_salary[$key];
  343. // }
  344. // }
  345. return $item;
  346. })->all();
  347. $return_item_list = [];
  348. foreach ($month_employee_list as $v){
  349. $key = $v['item_id'];
  350. if(!isset($return_item_list[$key])) $return_item_list[$key]['salary'] = 0;
  351. $return_item_list[$key]['salary'] += $v['allocated_salary'];
  352. }
  353. return $return_item_list;
  354. }
  355. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  356. {
  357. //确认所有项目、设备、设备工时
  358. $month_device_list = $this->getItemDeviceMonthWorkList($user, $data, $month_start, $month_end);
  359. //查询所有设备工资
  360. $month_device_salary = $this->getDeviceAmount($user, $data, $month_start, $month_end);
  361. //查询所有项目人员的工时比例
  362. // 2. 计算每个设备在每个月的全月总工时
  363. $keys = ["device_id","order_month"];
  364. $device_monthly_total_min = $this->calculateSum($month_device_list,$keys,"total_work");
  365. //汇总设备每个月损耗金额
  366. 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"]);
  367. $collect = collect($item_list);
  368. $device_count = $collect->groupBy(function ($item) {
  369. // 这里的 $item 是集合中的每一行数据
  370. return $item['device_id'] . '_' . $item['month'];
  371. })->map(function ($group) {
  372. return $group->count();
  373. })->toArray();
  374. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$month_device_salary) {
  375. // 如果不是最后一条
  376. $key = $item['device_id'] . '_' . $item['month'];
  377. $sums = [
  378. "salary_map" => $month_device_salary
  379. ];
  380. $word_keys = [
  381. "salary_map" => [
  382. "key" => "allocated_depreciation",
  383. "value" => "allocated_depreciation",
  384. "type" => "money",
  385. ]
  386. ];
  387. $this->calculateClosure($key,$item,$device_count,$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. $month_employee_list = $collect->transform(function ($item) use (&$employee_count,&$salary_map) {
  660. // 如果不是最后一条
  661. $key = $item['employee_id'] . '_' . $item['month'];
  662. $sums = [
  663. "salary_map" => $salary_map
  664. ];
  665. $word_keys = [
  666. "salary_map" => [
  667. "key" => "allocated_salary",
  668. "value" => "allocated_salary",
  669. "type" => "money",
  670. ]
  671. ];
  672. $this->calculateClosure($key,$item,$employee_count,$sums,$word_keys);
  673. return $item;
  674. })->all();
  675. $return_item_list = [];
  676. foreach ($month_employee_list as $v){
  677. $key = $v['item_id'] . '_' . $v['month'];
  678. if(!isset($return_item_list[$key])) $return_item_list[$key]['allocated_salary'] = 0;
  679. $return_item_list[$key]['allocated_salary'] += $v['allocated_salary'];
  680. }
  681. return $return_item_list;
  682. }
  683. public function auxiliaryDevice($user, $data, $month_start, $month_end)
  684. {
  685. $month_device_list = $this->getItemDeviceMonthWorkList($user, $data, $month_start, $month_end);
  686. //查询所有设备工资
  687. //查询所有项目人员的工时比例
  688. //汇总每个人每个月工资
  689. $depreciation_map = $this->getDeviceAmount($user, $data, $month_start, $month_end);
  690. // 2. 计算每个员工在每个月的全月总工时
  691. $key = ["device_id","order_month"];
  692. $device_monthly_total_min = $this->calculateSum($month_device_list,$key,"total_work");
  693. // 3. 计算分摊天数与工资
  694. list($item_list,) = $this->calculateDeviceRatioForMonth($month_device_list,$device_monthly_total_min,$depreciation_map,$key,["device_id","month","item_id"]);
  695. $collect = collect($item_list);
  696. $device_count = $collect->groupBy(function ($item) {
  697. // 这里的 $item 是集合中的每一行数据
  698. return $item['device_id'] . '_' . $item['month'];
  699. })->map(function ($group) {
  700. return $group->count();
  701. })->toArray();
  702. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$depreciation_map) {
  703. $key = $item['device_id'] . '_' . $item['month'];
  704. $sums = [
  705. "salary_map" => $depreciation_map
  706. ];
  707. $word_keys = [
  708. "salary_map" => [
  709. "key" => "allocated_depreciation",
  710. "value" => "depreciation",
  711. "type" => "money",
  712. ]
  713. ];
  714. $this->calculateClosure($key,$item,$device_count,$sums,$word_keys);
  715. return $item;
  716. })->all();
  717. $return_item_list = [];
  718. foreach ($month_employee_list as $v){
  719. $key = $v['item_id']. '_' . $v['month'];
  720. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  721. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  722. }
  723. // dd($return_item_list);
  724. return $return_item_list;
  725. }
  726. public function itemEmployeeSalaryStatistic($data, $user)
  727. {
  728. list($status, $month_start, $month_end) = $this->commonRule($data);
  729. if (!$status) return [false, $month_start];
  730. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  731. //获取人员工资项目相关分组数据
  732. //确认所有项目、人员、人员工时
  733. $month_employee_list = $this->getItemEmployeeMonthWorkList($user, $data, $month_start, $month_end);
  734. //查询所有人员工资
  735. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  736. ->pluck('id')->toArray();
  737. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  738. ->where('del_time', 0)
  739. ->where("month", ">=", $month_start)
  740. ->where("month", "<", $month_end)
  741. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  742. ->pluck('month_str', 'id')
  743. ->toArray();
  744. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  745. ->select("employee_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id", "social_insurance", "public_housing_fund")
  746. ->get()->toArray();
  747. //查询所有项目人员的工时比例
  748. //汇总每个人每个月工资
  749. $salary_map = [];
  750. $all_salary = [];
  751. foreach ($month_employee_salary as $val) {
  752. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  753. if ($month) {
  754. $salary_map[$val['employee_id'] . '_' . $month] = $val;
  755. $all_salary[$val['employee_id'] . '_' . $month] = [
  756. "salary" => $val['salary']*100,
  757. "social_insurance" => $val['social_insurance']*100,
  758. "public_housing_fund" => $val['public_housing_fund']*100,
  759. ];
  760. }
  761. }
  762. // 2. 计算每个员工在每个月的全月总工时
  763. $employee_monthly_total_min = [];
  764. $all_min = [];
  765. foreach ($month_employee_list as $row) {
  766. $key = $row['employee_id'] . '_' . $row['order_month'];
  767. if (!isset($employee_monthly_total_min[$key])) {
  768. $employee_monthly_total_min[$key] = 0;
  769. $all_min[$key] = 0;
  770. }
  771. $employee_monthly_total_min[$key] += $row['total_work'];
  772. $all_min[$key] += round($row['total_work']/60,2)*100;
  773. }
  774. // 3. 计算分摊天数与工资
  775. $item_month_list = [];
  776. foreach ($month_employee_list as $item) {
  777. $key = $item['employee_id'] . '_' . $item['order_month'];
  778. $item_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  779. if (!isset($item_month_list[$item_key])) {
  780. $item_month_list[$item_key] = [
  781. "month" => $item['order_month'],
  782. "allocated_salary" => 0,
  783. "employee_id" => $item['employee_id'],
  784. "work_minutes" => 0,
  785. "salary" => ($salary_map[$key]['salary'] ?? 0),
  786. "social_insurance" => ($salary_map[$key]['social_insurance'] ?? 0),
  787. "public_housing_fund" => ($salary_map[$key]['public_housing_fund'] ?? 0),
  788. "item_id" => $item['item_id'],
  789. ];
  790. }
  791. $total_min = $employee_monthly_total_min[$key] ?? 0;
  792. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  793. if ($total_min > 0) {
  794. $ratio = round($item['total_work'] / $total_min, 4);
  795. } else {
  796. $ratio = 0;
  797. }
  798. $item_month_list[$item_key]['radio'] = $ratio;
  799. $item_month_list[$item_key]['total_min'] = $total_min;
  800. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  801. }
  802. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  803. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  804. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  805. $employee = Employee::TopClear($user, $data);
  806. $employee_list = $employee->wherein('id', $employee_ids)->select("major", "title", "id")->get()->toArray();
  807. $employee_key_list = [];
  808. foreach ($employee_list as $v) {
  809. $employee_key_list[$v['id']] = $v;
  810. }
  811. $item = Item::TopClear($user, $data);
  812. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  813. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  814. $collect = collect($item_month_list);
  815. $employee_count = $collect->groupBy(function ($item) {
  816. // 这里的 $item 是集合中的每一行数据
  817. return $item['employee_id'] . '_' . $item['month'];
  818. })->map(function ($group) {
  819. return $group->count();
  820. })->toArray();
  821. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  822. $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) {
  823. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  824. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  825. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['employee_id']})";
  826. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
  827. $item['total_min'] = round($item['total_min'] / 60, 2);
  828. $key = $item['employee_id'] . '_' . $item['month'];
  829. if (--$employee_count[$key] > 0) {
  830. $work_minutes = round($item['work_minutes'] / 60, 2);
  831. $item['work_minutes'] = $work_minutes;
  832. $all_min[$key] -= ($work_minutes*100);
  833. $work_salary = round($item['salary'] * $item['radio'], 2);
  834. $all_salary[$key]['salary'] -= ($work_salary*100);
  835. $item['work_salary'] = $work_salary;
  836. $social_insurance = round($item['social_insurance'] * $item['radio'], 2);
  837. $all_salary[$key]['social_insurance'] -= ($social_insurance*100);
  838. $item['work_social_insurance'] = $social_insurance;
  839. $work_public_housing_fund = round($item['public_housing_fund'] * $item['radio'], 2);
  840. $all_salary[$key]['public_housing_fund'] -= ($work_public_housing_fund*100);
  841. $item['work_public_housing_fund'] = $work_public_housing_fund;
  842. } else {
  843. $item['work_salary'] = round($all_salary[$key]['salary']/100,2);
  844. $item['work_social_insurance'] = round($all_salary[$key]['social_insurance'] /100,2);
  845. $item['work_public_housing_fund'] = round( $all_salary[$key]['public_housing_fund']/100,2);
  846. $item['work_minutes'] = round( $all_min[$key]/100,2);
  847. }
  848. return $item;
  849. })->all();
  850. return [true, $item_month_list];
  851. }
  852. public function enterpriseRdStatistic($data, $user){
  853. $model = Item::TopClear($user,$data);
  854. $model = $model->where('del_time',0)
  855. ->select(Item::$report_field_1)
  856. ->orderby('id', 'desc');
  857. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  858. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  859. if(! empty($data['state'])) $model->where('state', $data['state']);
  860. $list = $model->get()->toArray();
  861. $list = $this->fillEnterpriseRdStatistic($list, $data, $user);
  862. return [true, $list];
  863. }
  864. private function fillEnterpriseRdStatistic($list, $data, $user){
  865. //项目实际支出 项目费用单
  866. $expense = ExpenseClaimsDetails::Clear($user, $data);
  867. $expense_map = $expense->where('del_time', 0)
  868. ->whereIn('item_id', array_unique(array_column($list,'id')))
  869. ->selectRaw('item_id, SUM(amount) as total_amount')
  870. ->groupBy('item_id')
  871. ->pluck('total_amount', 'item_id') // 这一步直接生成 item_id => total_amount 结构
  872. ->toArray();
  873. foreach ($list as $key => $value){
  874. $list[$key]['actual_expenditure'] = $expense_map[$value['id']] ?? 0;
  875. $start_time = $value['start_time'] ? date('Y-m-d', $value['start_time']) : '';
  876. $end_time = $value['end_time'] ? date('Y-m-d', $value['end_time']) : '';
  877. $list[$key]['time_range'] = $start_time . ' ' . $end_time;
  878. }
  879. return $list;
  880. }
  881. public function enterpriseRdManStatistic($data, $user){
  882. $model = Employee::TopClear($user,$data);
  883. $model = $model->where('del_time',0)
  884. ->where('is_admin', '<=', Employee::IS_ADMIN_ONE)
  885. ->select(Employee::$report_field)
  886. ->orderBy('id','desc');
  887. if(! empty($data['id_card'])) $model->where('id_card', 'LIKE', '%'.$data['id_card'].'%');
  888. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  889. if(! empty($data['employee_type'])) $model->where('employee_type', $data['employee_type']);
  890. if(isset($data['education'])) $model->where('education', $data['education']);
  891. $list = $model->get()->toArray();
  892. $list = $this->fillEnterpriseRdManStatistic($list, $data, $user);
  893. return [true, $list];
  894. }
  895. private function fillEnterpriseRdManStatistic($list, $data, $user){
  896. $man = EmployeeDepartPermission::from('employee_depart_permission as a')
  897. ->join('depart as b', 'b.id', '=', 'a.depart_id')
  898. ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
  899. ->select('a.employee_id', 'b.title')
  900. ->get()->toArray();
  901. $man_map = [];
  902. foreach ($man as $value){
  903. if(isset($man_map[$value['employee_id']])){
  904. $man_map[$value['employee_id']] .= ',' . $value['title'];
  905. }else{
  906. $man_map[$value['employee_id']] = $value['title'];
  907. }
  908. }
  909. foreach ($list as $key => $value){
  910. $depart_title = $man_map[$value['id']] ?? "";
  911. $list[$key]['position_new'] = $depart_title . '/' . $value['position'];
  912. $list[$key]['employee_type_title'] = Employee::E_State_Type[$value['employee_type']] ?? '';
  913. $list[$key]['education'] = Employee::Education[$value['education']] ?? '';
  914. }
  915. return $list;
  916. }
  917. public function enterpriseRdItemStatistic($data, $user){
  918. // 1. 先声明表名和别名
  919. $model = ItemDetails::from('item_details as i');
  920. // 2. 再调用 TopClear
  921. $model = $model->TopClear($user, $data);
  922. $model = $model->from('item_details as i')
  923. ->where('i.del_time', 0)
  924. ->where('i.type', ItemDetails::type_one);
  925. $model = $model->leftJoin('employee as e', 'i.data_id', '=', 'e.id');
  926. $fields = ['e.id', 'e.title', 'e.education', 'e.major','e.p_title','i.item_id'];
  927. $list = $model->select($fields)
  928. ->orderBy('i.id', 'desc')
  929. ->get()
  930. ->toArray();
  931. $list = $this->fillEnterpriseRdItemStatistic($list, $data, $user);
  932. return [true, $list];
  933. }
  934. private function fillEnterpriseRdItemStatistic($list, $data, $user){
  935. $man = EmployeeDepartPermission::from('employee_depart_permission as a')
  936. ->join('depart as b', 'b.id', '=', 'a.depart_id')
  937. ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
  938. ->select('a.employee_id', 'b.title')
  939. ->get()->toArray();
  940. $man_map = [];
  941. foreach ($man as $value){
  942. if(isset($man_map[$value['employee_id']])){
  943. $man_map[$value['employee_id']] .= ',' . $value['title'];
  944. }else{
  945. $man_map[$value['employee_id']] = $value['title'];
  946. }
  947. }
  948. $item_map = Item::whereIn('id',array_unique(array_column($list,'item_id')))
  949. ->pluck('title','id')
  950. ->toArray();
  951. foreach ($list as $key => $value){
  952. $depart_title = $man_map[$value['id']] ?? "";
  953. $list[$key]['depart_title'] = $depart_title;
  954. $item_title = $item_map[$value['item_id']] ?? "";
  955. $list[$key]['item_title'] = $item_title;
  956. $list[$key]['education'] = Employee::Education[$value['education']] ?? '';
  957. }
  958. return $list;
  959. }
  960. }