StatisticService.php 44 KB

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