StatisticService.php 50 KB

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