StatisticService.php 57 KB

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