StatisticService.php 19 KB

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