StatisticService.php 55 KB

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