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