StatisticService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 Illuminate\Support\Facades\DB;
  23. class StatisticService extends Service
  24. {
  25. public function employeeDayHourStatistic($data, $user)
  26. {
  27. //传参月份、项目编码、项目名称 不允许跨年查询月份
  28. //项目编码、项目名称、人员名称、工时、日期
  29. list($status, $month_start, $month_end) = $this->commonRule($data);
  30. if (!$status) return [false, $month_start];
  31. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  32. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  33. ->where('del_time', 0)
  34. ->select(
  35. "item_id",
  36. "employee_id",
  37. // 将时间戳转为 Y-m-d 格式并起别名
  38. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m-%d') as order_date"),
  39. // 聚合求和
  40. DB::raw("SUM(total_work_min) as total_work")
  41. )
  42. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m-%d')"), "item_id", "employee_id")
  43. ->orderby("order_date", "asc");
  44. $month_employee_list = $this->limit($month_employee_list, ['*'], $data);
  45. $dataCollection = collect($month_employee_list['data']);
  46. $item_ids = $dataCollection->pluck('item_id')->unique()->values()->all();
  47. $employee_ids = $dataCollection->pluck('employee_id')->unique()->values()->all();
  48. $employee = Employee::Clear($user, $data);
  49. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  50. $item = Item::Clear($user, $data);
  51. $item_title_key_list = $item->wherein('id', $item_ids)->pluck("title", "id")->toArray();
  52. $item_code_key_list = $item->wherein('id', $item_ids)->pluck("code", "id")->toArray();
  53. $month_employee_list['data'] = collect($month_employee_list['data'])->transform(function ($item) use ($employee_key_list, $item_title_key_list, $item_code_key_list) {
  54. $item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
  55. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  56. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  57. $item['total_work_hours'] = round($item['total_work'] / 60, 2);
  58. return $item;
  59. })->all();
  60. return [true, $month_employee_list];
  61. }
  62. public function employeeMonthSalaryStatistic($data, $user)
  63. {
  64. //项目编码、项目名称、天数、工资、日期
  65. list($status, $month_start, $month_end) = $this->commonRule($data);
  66. if (!$status) return [false, $month_start];
  67. //确认所有项目、人员、人员工时
  68. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  69. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  70. ->where('del_time', 0)
  71. ->select(
  72. "item_id",
  73. "employee_id",
  74. // 将时间戳转为 Y-m-d 格式并起别名
  75. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  76. // 聚合求和
  77. DB::raw("SUM(total_work_min) as total_work")
  78. )
  79. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  80. //查询所有人员工资
  81. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  82. ->pluck('id')->toArray();
  83. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  84. ->where('del_time', 0)
  85. ->where("month", ">=", $month_start)
  86. ->where("month", "<", $month_end)
  87. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  88. ->pluck('month_str', 'id')
  89. ->toArray();
  90. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  91. ->select("employee_id", "salary", "main_id")
  92. ->get()->toArray();
  93. //查询所有项目人员的工时比例
  94. //汇总每个人每个月工资
  95. $salary_map = [];
  96. foreach ($month_employee_salary as $val) {
  97. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  98. if ($month) {
  99. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  100. }
  101. }
  102. // var_dump($salary_map);die;
  103. // 2. 计算每个员工在每个月的全月总工时
  104. $employee_monthly_total_min = [];
  105. foreach ($month_employee_list as $row) {
  106. $key = $row['employee_id'] . '_' . $row['order_month'];
  107. if (!isset($employee_monthly_total_min[$key])) {
  108. $employee_monthly_total_min[$key] = 0;
  109. }
  110. $employee_monthly_total_min[$key] += $row['total_work'];
  111. }
  112. // 3. 计算分摊天数与工资
  113. $item_month_list = [];
  114. foreach ($month_employee_list as $item) {
  115. $key = $item['employee_id'] . '_' . $item['order_month'];
  116. $item_key = $item['order_month'] . '_' . $item['item_id'];
  117. if(!isset($item_month_list[$item_key])){
  118. $item_month_list[$item_key] = [
  119. "month" => $item['order_month'],
  120. "allocated_salary" => 0,
  121. "work_minutes" => 0,
  122. "item_id" => $item['item_id'],
  123. ];
  124. }
  125. $total_salary = $salary_map[$key] ?? 0;
  126. $total_min = $employee_monthly_total_min[$key] ?? 0;
  127. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  128. if ($total_min > 0) {
  129. $ratio = $item['total_work'] / $total_min;
  130. $allocated_salary = round($ratio * $total_salary, 2);
  131. } else {
  132. $allocated_salary = 0;
  133. }
  134. $item_month_list[$item_key]['allocated_salary'] += $allocated_salary;
  135. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  136. }
  137. foreach ($item_month_list as $k=>$v){
  138. $item_month_list[$k]['days'] = round($v['work_minutes']/8/60);
  139. unset($item_month_list[$k]['work_minutes']);
  140. }
  141. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  142. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  143. $item = Item::Clear($user, $data);
  144. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  145. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  146. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list) {
  147. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  148. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  149. return $item;
  150. })->all();
  151. return [true,$item_month_list];
  152. }
  153. public function itemDaySalaryStatistic($data, $user)
  154. {
  155. //项目编码、项目名称、人员名称、研发工时、研发工资、当月总工时、总计工资、年月
  156. list($status, $month_start, $month_end) = $this->commonRule($data);
  157. if (!$status) return [false, $month_start];
  158. //确认所有项目、人员、人员工时
  159. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  160. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  161. ->where('del_time', 0)
  162. ->select(
  163. "item_id",
  164. "employee_id",
  165. // 将时间戳转为 Y-m-d 格式并起别名
  166. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  167. // 聚合求和
  168. DB::raw("SUM(total_work_min) as total_work")
  169. )
  170. ->groupBy( DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "employee_id")->get()->toArray();
  171. //查询所有人员工资
  172. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)->pluck('id')->toArray();
  173. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  174. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  175. ->pluck('month_str', 'id')->toArray();
  176. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  177. ->select("employee_id", "salary", "main_id")
  178. ->get()->toArray();
  179. //查询所有项目人员的工时比例
  180. //汇总每个人每个月工资
  181. $salary_map = [];
  182. foreach ($month_employee_salary as $val) {
  183. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  184. if ($month) {
  185. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  186. }
  187. }
  188. // 2. 计算每个员工在每个月的全月总工时
  189. $employee_monthly_total_min = [];
  190. foreach ($month_employee_list as $row) {
  191. $key = $row['employee_id'] . '_' . $row['order_month'];
  192. if (!isset($employee_monthly_total_min[$key])) {
  193. $employee_monthly_total_min[$key] = 0;
  194. }
  195. $employee_monthly_total_min[$key] += $row['total_work'];
  196. }
  197. // 3. 计算分摊天数与工资
  198. $item_month_list = [];
  199. foreach ($month_employee_list as $item) {
  200. $key = $item['employee_id'] . '_' . $item['order_month'];
  201. $employee_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  202. $total_salary = $salary_map[$key] ?? 0;
  203. $total_min = $employee_monthly_total_min[$key] ?? 0;
  204. if(!isset($item_month_list[$employee_key])){
  205. $item_month_list[$employee_key] = [
  206. "month" => $item['order_month'],
  207. "allocated_salary" => 0,
  208. "work_minutes" => 0,
  209. "total_min" => $total_min,
  210. "total_salary" => $total_salary,
  211. "item_id" => $item['item_id'],
  212. "employee_id" => $item['employee_id'],
  213. ];
  214. }
  215. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  216. if ($total_min > 0) {
  217. $ratio = $item['total_work'] / $total_min;
  218. $allocated_salary = round($ratio * $total_salary, 2);
  219. } else {
  220. $allocated_salary = 0;
  221. }
  222. $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
  223. $item_month_list[$employee_key]['work_minutes'] += $item['total_work'];
  224. }
  225. foreach ($item_month_list as $k=>$v){
  226. $item_month_list[$k]['days'] = round($v['work_minutes']/8/60);
  227. unset($item_month_list[$k]['work_minutes']);
  228. }
  229. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  230. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  231. $item = Item::Clear($user, $data);
  232. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  233. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  234. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  235. $employee = Employee::Clear($user, $data);
  236. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  237. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list,$employee_key_list) {
  238. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  239. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  240. $item['employee_title'] = $employee_key_list[$item['employee_id']] ?? "未知人员({$item['employee_id']})";
  241. return $item;
  242. })->all();
  243. return [true,$item_month_list];
  244. }
  245. public function itemDeviceMonthStatistic($data, $user)
  246. {
  247. //项目编码、项目名称、设备名称、项目工时、研发工时占比、设备原值、设备折旧额、本项目帐面归集的折旧额、确定的本项目折旧额、加计调整金额、当月工时、日期
  248. list($status, $month_start, $month_end) = $this->commonRule($data);
  249. if (!$status) return [false, $month_start];
  250. //确认所有项目、设备、设备工时
  251. $month_device = DailyDwOrderDetails::Clear($user, $data);
  252. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  253. ->where('del_time', 0)
  254. ->select(
  255. "item_id",
  256. "device_id",
  257. // 将时间戳转为 Y-m-d 格式并起别名
  258. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  259. // 聚合求和
  260. DB::raw("SUM(total_work_min) as total_work")
  261. )
  262. ->groupBy( DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  263. //查询所有人员工资
  264. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  265. ->pluck('id')->toArray();
  266. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  267. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  268. ->pluck("month_str", 'id')->toArray();
  269. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  270. ->select("device_id", "depreciation_amount", "main_id")
  271. ->get()->toArray();
  272. //查询所有项目人员的工时比例
  273. //汇总每个人每个月工资
  274. $depreciatio_map = [];
  275. foreach ($month_device_salary as $val) {
  276. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  277. if ($month) {
  278. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  279. }
  280. }
  281. // 2. 计算每个员工在每个月的全月总工时
  282. $device_monthly_total_min = [];
  283. foreach ($month_device_list as $row) {
  284. $key = $row['device_id'] . '_' . $row['order_month'];
  285. if (!isset($device_monthly_total_min[$key])) {
  286. $device_monthly_total_min[$key] = 0;
  287. }
  288. $device_monthly_total_min[$key] += $row['total_work'];
  289. }
  290. // 3. 计算分摊天数与工资
  291. $item_month_list = [];
  292. foreach ($month_device_list as $item) {
  293. $key = $item['device_id'] . '_' . $item['order_month'];
  294. $device_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['device_id'];
  295. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  296. $total_min = $device_monthly_total_min[$key] ?? 0;
  297. if(!isset($item_month_list[$device_key])){
  298. $item_month_list[$device_key] = [
  299. "month" => $item['order_month'],
  300. "allocated_depreciatio" => 0,
  301. "work_minutes" => 0,
  302. "total_min" => $total_min,
  303. "total_depreciatio" => $total_depreciatio,
  304. ];
  305. }
  306. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  307. if ($total_min > 0) {
  308. $ratio = round($item['total_work'] / $total_min,3);
  309. $allocated_salary = round($ratio * $total_depreciatio, 2);
  310. } else {
  311. $ratio = 0;
  312. $allocated_salary = 0;
  313. }
  314. $item_month_list[$device_key]['allocated_depreciatio'] += $allocated_salary;
  315. $item_month_list[$device_key]['work_minutes'] += $item['total_work'];
  316. $item_month_list[$device_key]['ratio'] = $ratio;
  317. }
  318. foreach ($item_month_list as $k=>$v){
  319. $item_month_list[$k]['total_hours'] = round($v['total_min']/60,1);
  320. $item_month_list[$k]['hours'] = round($v['work_minutes']/60,1);
  321. unset($item_month_list[$k]['work_minutes']);
  322. }
  323. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  324. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  325. $item = Item::Clear($user, $data);
  326. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  327. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  328. $device_ids = collect($item_month_list)->pluck('device_id')->unique()->values()->all();
  329. $device = Device::Clear($user, $data);
  330. $device_key_list = $device->wherein('id', $device_ids)->pluck("title", "id")->toArray();
  331. $device_original_value_key_list = $device->wherein('id', $device_ids)->pluck("original_value", "id")->toArray();
  332. $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) {
  333. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  334. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  335. $item['device_title'] = $device_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  336. $item['device_original'] = $device_original_value_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  337. return $item;
  338. })->all();
  339. return [true,$item_month_list];
  340. }
  341. private function commonRule($data)
  342. {
  343. if (!isset($data['month_start'])) $month_start = date('Y-01-01');
  344. else $month_start = date('Y-m-01', strtotime($data['month_start']));
  345. if (!isset($data['month_end'])) $month_end = date('Y-01-01', strtotime('+1 year'));
  346. else {
  347. $start_year = date('Y', strtotime($month_start));
  348. $end_year = date('Y', strtotime($data['month_end']));
  349. if ($start_year != $end_year) return [false, "查询不得跨年!", ""];
  350. $month_end = date('Y-m-01', strtotime($data['month_end'] . ' +1 month'));
  351. }
  352. return [true, strtotime($month_start), strtotime($month_end)];
  353. }
  354. }