StatisticService.php 50 KB

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