StatisticService.php 58 KB

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