StatisticService.php 49 KB

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