StatisticService.php 58 KB

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