StatisticService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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\DailyPwOrderDetails;
  7. use App\Model\Device;
  8. use App\Model\Employee;
  9. use App\Model\ExpenseClaims;
  10. use App\Model\ExpenseClaimsDetails;
  11. use App\Model\Fee;
  12. use App\Model\Item;
  13. use App\Model\ItemDetails;
  14. use App\Model\MonthlyDdOrder;
  15. use App\Model\MonthlyPsOrder;
  16. use App\Model\MonthlyPsOrderDetails;
  17. use App\Model\MonthlyPwOrderDetails;
  18. use App\Model\RuleSet;
  19. use App\Model\RuleSetDetails;
  20. use Illuminate\Support\Facades\DB;
  21. class StatisticService extends Service
  22. {
  23. public function employeeDayHourStatistic($data, $user)
  24. {
  25. //传参月份、项目编码、项目名称 不允许跨年查询月份
  26. //项目编码、项目名称、人员名称、工时、日期
  27. list($status, $month_start, $month_end) = $this->commonRule($data);
  28. if (!$status) return [false, $month_start];
  29. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  30. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  31. ->where('del_time', 0)
  32. ->select(
  33. "item_id",
  34. "employee_id",
  35. // 将时间戳转为 Y-m-d 格式并起别名
  36. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m-%d') as order_date"),
  37. // 聚合求和
  38. DB::raw("SUM(total_work_min) as total_work")
  39. )
  40. ->groupBy("order_date", "item_id", "employee_id")
  41. ->orderby("order_date", "asc");
  42. $month_employee_list = $this->limit($month_employee_list, ['*'], $data);
  43. $dataCollection = collect($month_employee_list['data']);
  44. $item_ids = $dataCollection->pluck('item_id')->unique()->values()->all();
  45. $employee_ids = $dataCollection->pluck('employee_id')->unique()->values()->all();
  46. $employee = Employee::Clear($user, $data);
  47. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  48. $item = Item::Clear($user, $data);
  49. $item_title_key_list = $item->wherein('id', $item_ids)->pluck("title", "id")->toArray();
  50. $item_code_key_list = $item->wherein('id', $item_ids)->pluck("code", "id")->toArray();
  51. $month_employee_list['data'] = collect($month_employee_list['data'])->transform(function ($item) use ($employee_key_list, $item_title_key_list, $item_code_key_list) {
  52. $item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
  53. $item['item_title'] = $item_key_list[$item_title_key_list['item_id']] ?? "未知项目({$item['item_id']})";
  54. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  55. $item['total_work_hours'] = round($item['total_work'] / 60, 2);
  56. return $item;
  57. })->all();
  58. return [true, $month_employee_list];
  59. }
  60. public function employeeMonthSalaryStatistic($data, $user)
  61. {
  62. //项目编码、项目名称、天数、工资、日期
  63. list($status, $month_start, $month_end) = $this->commonRule($data);
  64. if (!$status) return [false, $month_start];
  65. //确认所有项目、人员、人员工时
  66. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  67. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  68. ->where('del_time', 0)
  69. ->select(
  70. "item_id",
  71. "employee_id",
  72. // 将时间戳转为 Y-m-d 格式并起别名
  73. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  74. // 聚合求和
  75. DB::raw("SUM(total_work_min) as total_work")
  76. )
  77. ->groupBy("order_month", "item_id", "employee_id")->toArray();
  78. //查询所有人员工资
  79. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  80. ->pluck('id')->toArray();
  81. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  82. ->pluck(DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month"), 'id')->toArray();
  83. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  84. ->select("employee_id", "salary", "main_id")
  85. ->get()->toArray();
  86. //查询所有项目人员的工时比例
  87. //汇总每个人每个月工资
  88. $salary_map = [];
  89. foreach ($month_employee_salary as $val) {
  90. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  91. if ($month) {
  92. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  93. }
  94. }
  95. // 2. 计算每个员工在每个月的全月总工时
  96. $employee_monthly_total_min = [];
  97. foreach ($month_employee_list as $row) {
  98. $key = $row['employee_id'] . '_' . $row['order_month'];
  99. if (!isset($employee_monthly_total_min[$key])) {
  100. $employee_monthly_total_min[$key] = 0;
  101. }
  102. $employee_monthly_total_min[$key] += $row['total_work'];
  103. }
  104. // 3. 计算分摊天数与工资
  105. $item_month_list = [];
  106. foreach ($month_employee_list as $item) {
  107. $key = $item['employee_id'] . '_' . $item['order_month'];
  108. $item_key = $item['order_month'] . '_' . $item['item_id'];
  109. if(!isset($item_month_list[$item_key])){
  110. $item_month_list[$item_key] = [
  111. "month" => $item['order_month'],
  112. "allocated_salary" => 0,
  113. "work_minutes" => 0,
  114. ];
  115. }
  116. $total_salary = $salary_map[$key] ?? 0;
  117. $total_min = $employee_monthly_total_min[$key] ?? 0;
  118. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  119. if ($total_min > 0) {
  120. $ratio = $item['total_work'] / $total_min;
  121. $allocated_salary = round($ratio * $total_salary, 2);
  122. } else {
  123. $allocated_salary = 0;
  124. }
  125. $item_month_list[$item_key]['allocated_salary'] += $allocated_salary;
  126. $item_month_list[$item_key]['work_minutes'] += $total_min;
  127. }
  128. foreach ($item_month_list as $k=>$v){
  129. $item_month_list[$k]['days'] = round($v['work_minutes']/8/60);
  130. unset($item_month_list[$k]['work_minutes']);
  131. }
  132. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  133. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  134. $item = Item::Clear($user, $data);
  135. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  136. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  137. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list) {
  138. $item['item_title'] = $item_key_list[$item_title_key_list['item_id']] ?? "未知项目({$item['item_id']})";
  139. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  140. return $item;
  141. })->all();
  142. return [true,$item_month_list];
  143. }
  144. public function itemDaySalaryStatistic($data, $user)
  145. {
  146. //项目编码、项目名称、人员名称、研发工时、研发工资、当月总工时、总计工资、年月
  147. list($status, $month_start, $month_end) = $this->commonRule($data);
  148. if (!$status) return [false, $month_start];
  149. //确认所有项目、人员、人员工时
  150. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  151. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  152. ->where('del_time', 0)
  153. ->select(
  154. "item_id",
  155. "employee_id",
  156. // 将时间戳转为 Y-m-d 格式并起别名
  157. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  158. // 聚合求和
  159. DB::raw("SUM(total_work_min) as total_work")
  160. )
  161. ->groupBy("order_month", "item_id", "employee_id")->toArray();
  162. //查询所有人员工资
  163. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  164. ->pluck('id')->toArray();
  165. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  166. ->pluck(DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month"), 'id')->toArray();
  167. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  168. ->select("employee_id", "salary", "main_id")
  169. ->get()->toArray();
  170. //查询所有项目人员的工时比例
  171. //汇总每个人每个月工资
  172. $salary_map = [];
  173. foreach ($month_employee_salary as $val) {
  174. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  175. if ($month) {
  176. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  177. }
  178. }
  179. // 2. 计算每个员工在每个月的全月总工时
  180. $employee_monthly_total_min = [];
  181. foreach ($month_employee_list as $row) {
  182. $key = $row['employee_id'] . '_' . $row['order_month'];
  183. if (!isset($employee_monthly_total_min[$key])) {
  184. $employee_monthly_total_min[$key] = 0;
  185. }
  186. $employee_monthly_total_min[$key] += $row['total_work'];
  187. }
  188. // 3. 计算分摊天数与工资
  189. $item_month_list = [];
  190. foreach ($month_employee_list as $item) {
  191. $key = $item['employee_id'] . '_' . $item['order_month'];
  192. $employee_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  193. $total_salary = $salary_map[$key] ?? 0;
  194. $total_min = $employee_monthly_total_min[$key] ?? 0;
  195. if(!isset($item_month_list[$employee_key])){
  196. $item_month_list[$employee_key] = [
  197. "month" => $item['order_month'],
  198. "allocated_salary" => 0,
  199. "work_minutes" => 0,
  200. "total_min" => $total_min,
  201. "total_salary" => $total_salary,
  202. ];
  203. }
  204. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  205. if ($total_min > 0) {
  206. $ratio = $item['total_work'] / $total_min;
  207. $allocated_salary = round($ratio * $total_salary, 2);
  208. } else {
  209. $allocated_salary = 0;
  210. }
  211. $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
  212. $item_month_list[$employee_key]['work_minutes'] += $total_min;
  213. }
  214. foreach ($item_month_list as $k=>$v){
  215. $item_month_list[$k]['days'] = round($v['work_minutes']/8/60);
  216. unset($item_month_list[$k]['work_minutes']);
  217. }
  218. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  219. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  220. $item = Item::Clear($user, $data);
  221. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  222. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  223. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  224. $employee = Employee::Clear($user, $data);
  225. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  226. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list) {
  227. $item['item_title'] = $item_key_list[$item_title_key_list['item_id']] ?? "未知项目({$item['item_id']})";
  228. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  229. $item['employee_title'] = $employee_key_list[$item['employee_id']] ?? "未知人员({$item['employee_id']})";
  230. return $item;
  231. })->all();
  232. return [true,$item_month_list];
  233. }
  234. private function commonRule($data)
  235. {
  236. if (!isset($data['month_start'])) $month_start = date('Y-01-01');
  237. else $month_start = date('Y-m-01', strtotime($data['month_start']));
  238. if (!isset($data['month_end'])) $month_end = date('Y-01-01', strtotime('+1 year'));
  239. else {
  240. $start_year = date('Y', strtotime($month_start));
  241. $end_year = date('Y', strtotime($data['month_end']));
  242. if ($start_year != $end_year) return [false, "查询不得跨年!", ""];
  243. $month_end = date('Y-m-01', strtotime($data['month_end'] . ' +1 month'));
  244. }
  245. return [true, $month_start, $month_end];
  246. }
  247. }