StatisticService.php 56 KB

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