StatisticService.php 56 KB

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