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,2);
  365. if($other_amount <= $jj_other_amount) $jj_other_amount = $other_amount;
  366. //加计其他费用
  367. $jj_amount = round(($total_amount+$other_amount)*0.1,2);
  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. ];
  384. $return[] = $item_value;
  385. }
  386. //修改
  387. foreach ($fee_type_list as $key => $value){
  388. if($value['is_after']) unset($fee_type_list[$key]);
  389. }
  390. return [true, ["list" => $return, "fee_type_list" => array_values($fee_type_list)]];
  391. }
  392. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  393. {
  394. $month_employee_list = $this->getItemEmployeeMonthWorkList($user, $data, $month_start, $month_end);
  395. //查询所有人员工资
  396. $salary_map = $this->getEmployeeSalary($user, $data, $month_start, $month_end);
  397. // 2. 计算每个员工在每个月的全月总工时
  398. $employee_monthly_total_min = $this->calculateSum($month_employee_list,["employee_id","order_month"],"total_work");
  399. // 2. 计算分摊天数与工资
  400. list($item_list,) = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,["employee_id","order_month"],["order_month","item_id","employee_id"]);
  401. $collect = collect($item_list);
  402. $employee_count = $collect->groupBy(function ($item) {
  403. // 这里的 $item 是集合中的每一行数据
  404. return $item['employee_id'] . '_' . $item['month'];
  405. })->map(function ($group) {
  406. return $group->count();
  407. })->toArray();
  408. $sums = [
  409. "salary_map" => $salary_map
  410. ];
  411. $employee_counts = [
  412. "salary_map" => $employee_count
  413. ];
  414. $word_keys = [
  415. "salary_map" => [
  416. "key" => "allocated_salary",
  417. "value" => "allocated_salary",
  418. "type" => "money",
  419. ]
  420. ];
  421. $month_employee_list = $collect->transform(function ($item) use (&$employee_counts,&$sums,$word_keys) {
  422. // 如果不是最后一条
  423. $key = $item['employee_id'] . '_' . $item['month'];
  424. $this->calculateClosure($key,$item,$employee_counts,$sums,$word_keys);
  425. return $item;
  426. })->all();
  427. $return_item_list = [];
  428. foreach ($month_employee_list as $v){
  429. $key = $v['item_id'];
  430. if(!isset($return_item_list[$key])) $return_item_list[$key]['salary'] = 0;
  431. $return_item_list[$key]['salary'] += $v['allocated_salary'];
  432. }
  433. return $return_item_list;
  434. }
  435. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  436. {
  437. //确认所有项目、设备、设备工时
  438. $month_device_list = $this->getItemDeviceMonthWorkList($user, $data, $month_start, $month_end);
  439. //查询所有设备工资
  440. $month_device_salary = $this->getDeviceAmount($user, $data, $month_start, $month_end);
  441. //查询所有项目人员的工时比例
  442. // 2. 计算每个设备在每个月的全月总工时
  443. $keys = ["device_id","order_month"];
  444. $device_monthly_total_min = $this->calculateSum($month_device_list,$keys,"total_work");
  445. //汇总设备每个月损耗金额
  446. 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"]);
  447. $collect = collect($item_list);
  448. $device_count = $collect->groupBy(function ($item) {
  449. // 这里的 $item 是集合中的每一行数据
  450. return $item['device_id'] . '_' . $item['month'];
  451. })->map(function ($group) {
  452. return $group->count();
  453. })->toArray();
  454. $sums = [
  455. "salary_map" => $month_device_salary
  456. ];
  457. $word_keys = [
  458. "salary_map" => [
  459. "key" => "allocated_depreciation",
  460. "value" => "allocated_depreciation",
  461. "type" => "money",
  462. ]
  463. ];
  464. $device_counts = [
  465. "salary_map" => $device_count
  466. ];
  467. $month_employee_list = $collect->transform(function ($item) use (&$device_counts,&$sums,$word_keys) {
  468. // 如果不是最后一条
  469. $key = $item['device_id'] . '_' . $item['month'];
  470. $this->calculateClosure($key,$item,$device_counts,$sums,$word_keys);
  471. return $item;
  472. })->all();
  473. $return_item_list = [];
  474. foreach ($month_employee_list as $v){
  475. $key = $v['item_id'];
  476. if(!isset($return_item_list[$key])) $return_item_list[$key]['allocated_depreciation'] = 0;
  477. $return_item_list[$key]['allocated_depreciation'] += $v['allocated_depreciation'];
  478. }
  479. return $return_item_list;
  480. }
  481. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  482. {
  483. //确认所有项目、费用
  484. $expense = ExpenseClaimsDetails::Clear($user, $data);
  485. $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
  486. ->where('del_time', 0)
  487. ->select(
  488. "fee_id",
  489. "amount",
  490. "item_id",
  491. "entrust_type"
  492. )->get()->toArray();
  493. //需要根据分类去汇总
  494. $fee = Fee::TopClear($user, $data);
  495. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  496. return $this->groupListByRoot($expense_list, $fee);
  497. }
  498. /**
  499. * 将报销明细按照一级费用和项目分类进行分组
  500. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  501. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  502. * @return array
  503. */
  504. public function groupListByRoot(array $list, array $fee_type_list)
  505. {
  506. // 1. 建立 ID 索引,方便快速查找
  507. $idMap = array_column($fee_type_list, null, 'id');
  508. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  509. $childToRoot = [];
  510. foreach ($fee_type_list as $type) {
  511. $current = $type;
  512. // 向上追溯直到 parent_id 为 0,即找到一级分类
  513. while ($current['parent_id'] != 0) {
  514. $current = $idMap[$current['parent_id']];
  515. }
  516. $childToRoot[$type['id']] = [
  517. 'id' => $current['id'],
  518. 'title' => $current['title'],
  519. 'sort' => $current['sort'],
  520. 'is_other' => $current['is_other'],
  521. ];
  522. }
  523. // 3. 遍历明细数据进行分组
  524. $item_key_list = [];
  525. $type_list = [];
  526. foreach ($list as $item) {
  527. $feeId = $item['fee_id'];
  528. // 获取该费用对应的一级分类信息
  529. if (!isset($childToRoot[$feeId])) continue;
  530. $rootId = $childToRoot[$feeId]['id'];
  531. $title = $childToRoot[$feeId]['title'];
  532. $sort = $childToRoot[$feeId]['sort'];
  533. $other = $childToRoot[$feeId]['is_other'];
  534. $key = $item['item_id'];
  535. if (!isset($item_key_list[$key][$rootId])) {
  536. $item_key_list[$key][$rootId] = [
  537. 'id' => $rootId,
  538. 'total_amount' => 0,
  539. 'entrust1_amount' => 0, //委内
  540. 'entrust2_amount' => 0, //委外
  541. 'is_other' => $other,
  542. ];
  543. }
  544. if ($item['entrust_type'] == 1) {
  545. $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount']*100;
  546. } elseif ($item['entrust_type'] == 2) {
  547. $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount']*100;
  548. }else{
  549. $item_key_list[$key][$rootId]['total_amount'] += $item['amount']*100;
  550. }
  551. //这边需要拿到头部所有的一级费用类型
  552. if (!isset($type_list[$rootId])) {
  553. $type_list[$rootId] = [
  554. 'sort' => $sort,
  555. 'id' => $rootId,
  556. 'title' => $title,
  557. 'is_after' => $other,
  558. ];
  559. }
  560. }
  561. // var_dump($item_key_list);die;
  562. foreach ($item_key_list as $k=>$v){
  563. foreach ($v as $kk=>$vv){
  564. $item_key_list[$k][$kk]['entrust1_amount'] = round( $vv['entrust1_amount']/100,2);
  565. $item_key_list[$k][$kk]['entrust2_amount'] = round( $vv['entrust2_amount']/100,2);
  566. $item_key_list[$k][$kk]['total_amount'] = round( $vv['total_amount']/100,2);
  567. }
  568. }
  569. // $item_key_list = collect($item_key_list)->transform(function ($item) {
  570. // $item['entrust1_amount'] = round( $item['entrust1_amount']/100,2);
  571. // $item['entrust2_amount'] = round( $item['entrust2_amount']/100,2);
  572. // $item['total_amount'] = round( $item['total_amount']/100,2);
  573. // return $item;
  574. // })->all();
  575. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  576. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  577. // 4. 重置数组索引并返回
  578. return [$item_key_list, $type_list];
  579. }
  580. public function auxiliaryStatistic($data, $user)
  581. {
  582. list($status, $month_start, $month_end) = $this->commonRule($data);
  583. if (!$status) return [false, $month_start];
  584. //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
  585. //确认所有项目
  586. $item = Item::TopClear($user, $data);
  587. $item_list = $item->where('del_time', 0)
  588. /*->where(function ($query) use ($month_start, $month_end) {
  589. $query->where('start_time', '>=', $month_start)
  590. ->orWhere('end_time', '<', $month_end);
  591. })*/->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();
  592. $item_key_list = [];
  593. foreach ($item_list as $v) {
  594. $item_key_list[$v['id']] = $v;
  595. }
  596. //获取该区间内所有项目人工费、折旧费
  597. $item_salary = $this->auxiliaryEmployee($user, $data, $month_start, $month_end);
  598. $device_depreciation = $this->auxiliaryDevice($user, $data, $month_start, $month_end);
  599. $fee = Fee::TopClear($user, $data);
  600. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  601. $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
  602. $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  603. ->where('del_time', 0)
  604. ->select(
  605. "item_id",
  606. "voucher_date",
  607. "voucher_type",
  608. "voucher_no",
  609. "voucher_remark",
  610. "voucher_amount",
  611. "aggregation_amount",
  612. "entrust_type",
  613. "entrust1_amount",
  614. "entrust2_amount",
  615. "entrust2_amount",
  616. "total_amount",
  617. "fee_id",
  618. "type"
  619. )->get()->toArray();
  620. list($fee_amount, $fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list, $fee);
  621. // dd($item_key_list);
  622. //找到项目和设备的费用然后进行比例计算
  623. $return = [];
  624. foreach ($fee_amount as $v) {
  625. //人工费用
  626. if ($v['type'] == 1 || $v['type'] == 2) {
  627. // var_dump();
  628. foreach ($item_key_list as $vv) {
  629. if(!isset($item_salary[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_salary'])) continue;
  630. $detail = [
  631. "code" => $vv['code'],
  632. "title" => $vv['title'],
  633. "state" => $vv['state'] == 3 ? "进行中" : "已完成",
  634. "voucher_date" => date("Y-m-d", $v['voucher_date']),
  635. "voucher_type" => $v['voucher_type'],
  636. "voucher_no" => $v['voucher_no'],
  637. "voucher_remark" => $v['voucher_remark'],
  638. "voucher_amount" => $v['voucher_amount'],
  639. "aggregation_amount" => $v['aggregation_amount'],
  640. "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),
  641. "fee_id" => $v['fee_id'],
  642. "entrust1_amount" => $v['entrust1_amount'],
  643. "entrust2_amount" => $v['entrust2_amount'],
  644. "type" => $v['type'],
  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. ];
  667. $return[] = $detail;
  668. }
  669. }
  670. return [true, [
  671. 'fee_type_list' => $fee_type_list,
  672. 'list' => $return,
  673. ]];
  674. }
  675. /**
  676. * 将报销明细按照一级费用和项目分类进行分组
  677. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  678. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  679. * @return array
  680. */
  681. public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
  682. {
  683. // 1. 建立 ID 索引,方便快速查找
  684. $idMap = array_column($fee_type_list, null, 'id');
  685. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  686. $childToRoot = [];
  687. foreach ($fee_type_list as $type) {
  688. $current = $type;
  689. // 向上追溯直到 parent_id 为 0,即找到一级分类
  690. while ($current['parent_id'] != 0) {
  691. $current = $idMap[$current['parent_id']];
  692. }
  693. $childToRoot[$type['id']] = [
  694. 'id' => $current['id'],
  695. 'title' => $current['title'],
  696. 'sort' => $current['sort']
  697. ];
  698. }
  699. // 3. 遍历明细数据进行分组
  700. $type_list = [];
  701. foreach ($list as $k => $item) {
  702. if ($item['item_id'] == 0 || $item['fee_id'] == 0) {
  703. continue;
  704. }
  705. $feeId = $item['fee_id'];
  706. // 获取该费用对应的一级分类信息
  707. if (!isset($childToRoot[$feeId])) continue;
  708. $rootId = $childToRoot[$feeId]['id'];
  709. $title = $childToRoot[$feeId]['title'];
  710. $sort = $childToRoot[$feeId]['sort'];
  711. $item['fee_id'] = $rootId;
  712. $list[$k] = $item;
  713. //这边需要拿到头部所有的一级费用类型
  714. if (!isset($type_list[$rootId])) {
  715. $type_list[$rootId] = [
  716. 'sort' => $sort,
  717. 'id' => $rootId,
  718. 'title' => $title,
  719. ];
  720. }
  721. }
  722. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  723. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  724. // 4. 重置数组索引并返回
  725. return [$list, $type_list];
  726. }
  727. public function auxiliaryEmployee($user, $data, $month_start, $month_end)
  728. {
  729. //确认所有项目、人员、人员工时
  730. $month_employee_list = $this->getItemEmployeeMonthWorkList($user, $data, $month_start, $month_end);
  731. //查询所有人员工资
  732. $salary_map = $this->getEmployeeSalary($user, $data, $month_start, $month_end);
  733. // 2. 计算每个员工在每个月的全月总工时
  734. $key = ["employee_id","order_month"];
  735. $employee_monthly_total_min = $this->calculateSum($month_employee_list,$key,"total_work");
  736. // 3. 计算分摊天数与工资
  737. list($item_list,) = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,$key,["employee_id","month","item_id"]);
  738. $collect = collect($item_list);
  739. $employee_count = $collect->groupBy(function ($item) {
  740. // 这里的 $item 是集合中的每一行数据
  741. return $item['employee_id'] . '_' . $item['month'];
  742. })->map(function ($group) {
  743. return $group->count();
  744. })->toArray();
  745. $sums = [
  746. "salary_map" => $salary_map
  747. ];
  748. $word_keys = [
  749. "salary_map" => [
  750. "key" => "allocated_salary",
  751. "value" => "allocated_salary",
  752. "type" => "money",
  753. ]
  754. ];
  755. $employee_count = [
  756. 'salary_map' => $employee_count
  757. ];
  758. $month_employee_list = $collect->transform(function ($item) use (&$employee_count,&$sums,$word_keys) {
  759. // 如果不是最后一条
  760. $key = $item['employee_id'] . '_' . $item['month'];
  761. $this->calculateClosure($key,$item,$employee_count,$sums,$word_keys);
  762. return $item;
  763. })->all();
  764. $return_item_list = [];
  765. foreach ($month_employee_list as $v){
  766. $key = $v['item_id'] . '_' . $v['month'];
  767. if(!isset($return_item_list[$key])) $return_item_list[$key]['allocated_salary'] = 0;
  768. $return_item_list[$key]['allocated_salary'] += $v['allocated_salary'];
  769. }
  770. return $return_item_list;
  771. }
  772. public function auxiliaryDevice($user, $data, $month_start, $month_end)
  773. {
  774. $month_device_list = $this->getItemDeviceMonthWorkList($user, $data, $month_start, $month_end);
  775. //查询所有设备工资
  776. //查询所有项目人员的工时比例
  777. //汇总每个人每个月工资
  778. $depreciation_map = $this->getDeviceAmount($user, $data, $month_start, $month_end);
  779. // 2. 计算每个员工在每个月的全月总工时
  780. $key = ["device_id","order_month"];
  781. $device_monthly_total_min = $this->calculateSum($month_device_list,$key,"total_work");
  782. // 3. 计算分摊天数与工资
  783. list($item_list,) = $this->calculateDeviceRatioForMonth($month_device_list,$device_monthly_total_min,$depreciation_map,$key,["device_id","month","item_id"]);
  784. $collect = collect($item_list);
  785. $device_count = $collect->groupBy(function ($item) {
  786. // 这里的 $item 是集合中的每一行数据
  787. return $item['device_id'] . '_' . $item['month'];
  788. })->map(function ($group) {
  789. return $group->count();
  790. })->toArray();
  791. $sums = [
  792. "salary_map" => $depreciation_map
  793. ];
  794. $word_keys = [
  795. "salary_map" => [
  796. "key" => "allocated_depreciation",
  797. "value" => "depreciation",
  798. "type" => "money",
  799. ]
  800. ];
  801. $device_count = [
  802. 'salary_map' => $device_count
  803. ];
  804. $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$sums,$word_keys) {
  805. $key = $item['device_id'] . '_' . $item['month'];
  806. $this->calculateClosure($key,$item,$device_count,$sums,$word_keys);
  807. return $item;
  808. })->all();
  809. $return_item_list = [];
  810. foreach ($month_employee_list as $v){
  811. $key = $v['item_id']. '_' . $v['month'];
  812. if(!isset($return_item_list[$key])) $return_item_list[$key]['depreciation'] = 0;
  813. $return_item_list[$key]['depreciation'] += $v['depreciation'];
  814. }
  815. // dd($return_item_list);
  816. return $return_item_list;
  817. }
  818. public function itemEmployeeSalaryStatistic($data, $user)
  819. {
  820. list($status, $month_start, $month_end) = $this->commonRule($data);
  821. if (!$status) return [false, $month_start];
  822. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  823. //获取人员工资项目相关分组数据
  824. //确认所有项目、人员、人员工时
  825. $month_employee_list = $this->getItemEmployeeMonthWorkList($user, $data, $month_start, $month_end);
  826. //查询所有人员工资
  827. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  828. ->pluck('id')->toArray();
  829. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  830. ->where('del_time', 0)
  831. ->where("month", ">=", $month_start)
  832. ->where("month", "<", $month_end)
  833. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  834. ->pluck('month_str', 'id')
  835. ->toArray();
  836. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  837. ->select("employee_id", DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id", "social_insurance", "public_housing_fund")
  838. ->get()->toArray();
  839. //查询所有项目人员的工时比例
  840. //汇总每个人每个月工资
  841. $salary_map = [];
  842. $all_salary = [];
  843. foreach ($month_employee_salary as $val) {
  844. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  845. if ($month) {
  846. $salary_map[$val['employee_id'] . '_' . $month] = $val;
  847. $all_salary[$val['employee_id'] . '_' . $month] = [
  848. "salary" => $val['salary']*100,
  849. "social_insurance" => $val['social_insurance']*100,
  850. "public_housing_fund" => $val['public_housing_fund']*100,
  851. ];
  852. }
  853. }
  854. // 2. 计算每个员工在每个月的全月总工时
  855. $employee_monthly_total_min = [];
  856. $all_min = [];
  857. foreach ($month_employee_list as $row) {
  858. $key = $row['employee_id'] . '_' . $row['order_month'];
  859. if (!isset($employee_monthly_total_min[$key])) {
  860. $employee_monthly_total_min[$key] = 0;
  861. $all_min[$key] = 0;
  862. }
  863. $employee_monthly_total_min[$key] += $row['total_work'];
  864. $all_min[$key] += round($row['total_work']/60,2)*100;
  865. }
  866. // 3. 计算分摊天数与工资
  867. $item_month_list = [];
  868. foreach ($month_employee_list as $item) {
  869. $key = $item['employee_id'] . '_' . $item['order_month'];
  870. $item_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  871. if (!isset($item_month_list[$item_key])) {
  872. $item_month_list[$item_key] = [
  873. "month" => $item['order_month'],
  874. "allocated_salary" => 0,
  875. "employee_id" => $item['employee_id'],
  876. "work_minutes" => 0,
  877. "salary" => ($salary_map[$key]['salary'] ?? 0),
  878. "social_insurance" => ($salary_map[$key]['social_insurance'] ?? 0),
  879. "public_housing_fund" => ($salary_map[$key]['public_housing_fund'] ?? 0),
  880. "item_id" => $item['item_id'],
  881. ];
  882. }
  883. $total_min = $employee_monthly_total_min[$key] ?? 0;
  884. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  885. if ($total_min > 0) {
  886. $ratio = round((round($item['total_work']/60,2) / round($total_min/60,2)), 4);
  887. } else {
  888. $ratio = 0;
  889. }
  890. $item_month_list[$item_key]['radio'] = $ratio;
  891. $item_month_list[$item_key]['total_min'] = $total_min;
  892. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  893. }
  894. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  895. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  896. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  897. $employee = Employee::TopClear($user, $data);
  898. $employee_list = $employee->wherein('id', $employee_ids)->select("major", "title", "id")->get()->toArray();
  899. $employee_key_list = [];
  900. foreach ($employee_list as $v) {
  901. $employee_key_list[$v['id']] = $v;
  902. }
  903. $item = Item::TopClear($user, $data);
  904. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  905. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  906. $collect = collect($item_month_list);
  907. $employee_count = $collect->groupBy(function ($item) {
  908. // 这里的 $item 是集合中的每一行数据
  909. return $item['employee_id'] . '_' . $item['month'];
  910. })->map(function ($group) {
  911. return $group->count();
  912. })->toArray();
  913. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  914. $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) {
  915. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  916. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  917. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['employee_id']})";
  918. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
  919. $item['total_min'] = round($item['total_min'] / 60, 2);
  920. $key = $item['employee_id'] . '_' . $item['month'];
  921. if (--$employee_count[$key] > 0) {
  922. $work_minutes = round($item['work_minutes'] / 60, 2);
  923. $item['work_minutes'] = $work_minutes;
  924. $all_min[$key] -= ($work_minutes*100);
  925. $work_salary = round($item['salary'] * $item['radio'], 2);
  926. $all_salary[$key]['salary'] -= ($work_salary*100);
  927. $item['work_salary'] = $work_salary;
  928. $social_insurance = round($item['social_insurance'] * $item['radio'], 2);
  929. $all_salary[$key]['social_insurance'] -= ($social_insurance*100);
  930. $item['work_social_insurance'] = $social_insurance;
  931. $work_public_housing_fund = round($item['public_housing_fund'] * $item['radio'], 2);
  932. $all_salary[$key]['public_housing_fund'] -= ($work_public_housing_fund*100);
  933. $item['work_public_housing_fund'] = $work_public_housing_fund;
  934. } else {
  935. $item['work_salary'] = round($all_salary[$key]['salary']/100,2);
  936. $item['work_social_insurance'] = round($all_salary[$key]['social_insurance'] /100,2);
  937. $item['work_public_housing_fund'] = round( $all_salary[$key]['public_housing_fund']/100,2);
  938. $item['work_minutes'] = round( $all_min[$key]/100,2);
  939. }
  940. return $item;
  941. })->all();
  942. return [true, $item_month_list];
  943. }
  944. public function enterpriseRdStatistic($data, $user){
  945. $model = Item::TopClear($user,$data);
  946. $model = $model->where('del_time',0)
  947. ->select(Item::$report_field_1)
  948. ->orderby('id', 'desc');
  949. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  950. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  951. if(! empty($data['state'])) $model->where('state', $data['state']);
  952. if(! empty($data['year'])) {
  953. $return = $this->getYearRangeInfo($data['year']);
  954. if (is_null($return)) return [false, '年度格式错误'];
  955. list($month_start, $month_end) = $return;
  956. $model->where("start_time", "<", strtotime($month_end)) // 项目开始得比月份结束早
  957. ->where("end_time", ">", strtotime($month_start)); // 项目结束得比月份开始晚
  958. }
  959. $list = $model->get()->toArray();
  960. if(empty($list)) return [true,[]];
  961. $datetime = $list[0]['start_time'];
  962. $list = $this->fillEnterpriseRdStatistic($list, $data, $user,$datetime);
  963. return [true, $list];
  964. }
  965. private function fillEnterpriseRdStatistic($list, $data, $user,$time){
  966. //项目实际支出 项目费用单
  967. // $expense = ExpenseClaimsDetails::Clear($user, $data);
  968. // $expense_map = $expense->where('del_time', 0)
  969. // ->whereIn('item_id', array_unique(array_column($list,'id')))
  970. // ->selectRaw('item_id, SUM(amount) as total_amount')
  971. // ->groupBy('item_id')
  972. // ->pluck('total_amount', 'item_id') // 这一步直接生成 item_id => total_amount 结构
  973. // ->toArray();
  974. $attendance = $this->employeeAttendanceMonthStatistic(["year"=>date("Y-m-d",$time), "s" => "/api/employeeAttendanceMonthStatistic"],$user);
  975. $attendance_key_list = [];
  976. foreach ($attendance[1]['list'] as $v){
  977. $attendance_key_list[$v['code']] = $v['employee_salary']*100+$v['device_depreciation']*100;
  978. foreach ($v['fee_list'] as $vv){
  979. $attendance_key_list[$v['code']] += $vv['total_amount']*100;
  980. $attendance_key_list[$v['code']] += $vv['entrust1_amount']*100;
  981. $attendance_key_list[$v['code']] += $vv['entrust2_amount']*100;
  982. }
  983. }
  984. // dd($attendance_key_list);
  985. foreach ($list as $key => $value){
  986. $list[$key]['actual_expenditure'] = isset($attendance_key_list[$value['code']]) ? round($attendance_key_list[$value['code']]/100,2) : 0;
  987. $start_time = $value['start_time'] ? date('Y-m-d', $value['start_time']) : '';
  988. $end_time = $value['end_time'] ? date('Y-m-d', $value['end_time']) : '';
  989. $list[$key]['time_range'] = $start_time . ' ' . $end_time;
  990. }
  991. return $list;
  992. }
  993. public function enterpriseRdManStatistic($data, $user){
  994. $model = Employee::TopClear($user,$data);
  995. $model = $model->where('del_time',0)
  996. ->where('is_admin', '<=', Employee::IS_ADMIN_ONE)
  997. ->select(Employee::$report_field)
  998. ->orderBy('id','desc');
  999. if(! empty($data['id_card'])) $model->where('id_card', 'LIKE', '%'.$data['id_card'].'%');
  1000. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  1001. if(! empty($data['employee_type'])) $model->where('employee_type', $data['employee_type']);
  1002. if(isset($data['education'])) $model->where('education', $data['education']);
  1003. $list = $model->get()->toArray();
  1004. $list = $this->fillEnterpriseRdManStatistic($list, $data, $user);
  1005. return [true, $list];
  1006. }
  1007. private function fillEnterpriseRdManStatistic($list, $data, $user){
  1008. $man = EmployeeDepartPermission::from('employee_depart_permission as a')
  1009. ->join('depart as b', 'b.id', '=', 'a.depart_id')
  1010. ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
  1011. ->select('a.employee_id', 'b.title')
  1012. ->get()->toArray();
  1013. $man_map = [];
  1014. foreach ($man as $value){
  1015. if(isset($man_map[$value['employee_id']])){
  1016. $man_map[$value['employee_id']] .= ',' . $value['title'];
  1017. }else{
  1018. $man_map[$value['employee_id']] = $value['title'];
  1019. }
  1020. }
  1021. foreach ($list as $key => $value){
  1022. $depart_title = $man_map[$value['id']] ?? "";
  1023. $list[$key]['position_new'] = $depart_title . '/' . $value['position'];
  1024. $list[$key]['employee_type_title'] = Employee::E_State_Type[$value['employee_type']] ?? '';
  1025. $list[$key]['education'] = Employee::Education[$value['education']] ?? '';
  1026. }
  1027. return $list;
  1028. }
  1029. public function enterpriseRdItemStatistic($data, $user){
  1030. // 1. 先声明表名和别名
  1031. $model = ItemDetails::from('item_details as i');
  1032. // 2. 再调用 TopClear
  1033. $model = $model->TopClear($user, $data);
  1034. $model = $model->from('item_details as i')
  1035. ->where('i.del_time', 0)
  1036. ->where('i.type', ItemDetails::type_one);
  1037. $model = $model->leftJoin('employee as e', 'i.data_id', '=', 'e.id');
  1038. $model = $model->leftJoin('item as it', 'it.id', '=', 'i.item_id');
  1039. if(! empty($data['title'])) $model->where('e.title', 'LIKE', '%'.$data['title'].'%');
  1040. if(! empty($data['item_title'])) $model->where('it.title', 'LIKE', '%'.$data['item_title'].'%');
  1041. $fields = ['e.id', 'e.title', 'e.education', 'e.major','e.p_title','i.item_id','it.title as item_title'];
  1042. $list = $model->select($fields)
  1043. ->orderBy('i.id', 'desc')
  1044. ->get()
  1045. ->toArray();
  1046. $list = $this->fillEnterpriseRdItemStatistic($list, $data, $user);
  1047. return [true, $list];
  1048. }
  1049. private function fillEnterpriseRdItemStatistic($list, $data, $user){
  1050. $man = EmployeeDepartPermission::from('employee_depart_permission as a')
  1051. ->join('depart as b', 'b.id', '=', 'a.depart_id')
  1052. ->whereIn('a.employee_id', array_unique(array_column($list,'id')))
  1053. ->select('a.employee_id', 'b.title')
  1054. ->get()->toArray();
  1055. $man_map = [];
  1056. foreach ($man as $value){
  1057. if(isset($man_map[$value['employee_id']])){
  1058. $man_map[$value['employee_id']] .= ',' . $value['title'];
  1059. }else{
  1060. $man_map[$value['employee_id']] = $value['title'];
  1061. }
  1062. }
  1063. foreach ($list as $key => $value){
  1064. $depart_title = $man_map[$value['id']] ?? "";
  1065. $list[$key]['depart_title'] = $depart_title;
  1066. $list[$key]['education'] = Employee::Education[$value['education']] ?? '';
  1067. }
  1068. return $list;
  1069. }
  1070. public function employeeMonthSalaryStatisticCountCommon($data,$user){
  1071. $model = MonthlyPsOrder::Clear($user,$data);
  1072. $model->leftJoin('monthly_ps_order_details as b', 'b.main_id', 'monthly_ps_order.id')
  1073. ->where('monthly_ps_order.del_time',0)
  1074. ->where('b.del_time',0)
  1075. ->select('monthly_ps_order.month', 'b.employee_id', 'b.base_salary','b.performance_salary','b.bonus','b.other')
  1076. ->orderby('monthly_ps_order.month','desc');
  1077. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  1078. $return = $this->changeDateToTimeStampAboutRange($data['time']);
  1079. $model->where('monthly_ps_order.month','>=',$return[0]);
  1080. $model->where('monthly_ps_order.month','<=',$return[1]);
  1081. }
  1082. if(! empty($data['employee_title'])){
  1083. $model_2 = Employee::TopClear($user,$data);
  1084. $id = $model_2->where('del_time',0)
  1085. ->where('title', 'LIKE', '%'.$data['employee_title'].'%')
  1086. ->pluck('id')
  1087. ->toArray();
  1088. $model->whereIn('b.employee_id', $id);
  1089. }
  1090. return $model;
  1091. }
  1092. public function employeeMonthSalaryStatisticCount($data, $user){
  1093. $model = $this->employeeMonthSalaryStatisticCountCommon($data, $user);
  1094. $list = $this->limit($model,'',$data);
  1095. $list = $this->fillEmployeeMonthSalaryStatisticCount($list, $data, $user);
  1096. return [true, $list];
  1097. }
  1098. public function fillEmployeeMonthSalaryStatisticCount($data, $ergs, $user){
  1099. if(empty($data['data'])) return $data;
  1100. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'employee_id')));
  1101. foreach ($data['data'] as $key => $value){
  1102. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  1103. $data['data'][$key]['employee_title'] = $emp[$value['employee_id']] ?? '';
  1104. $totalSalary = (float)($value['base_salary'] ?? 0)
  1105. + (float)($value['performance_salary'] ?? 0)
  1106. + (float)($value['bonus'] ?? 0)
  1107. + (float)($value['other'] ?? 0);
  1108. $data['data'][$key]['total_salary'] = number_format($totalSalary, 2, '.', '');
  1109. }
  1110. return $data;
  1111. }
  1112. }