StatisticService.php 19 KB

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