StatisticService.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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['data']);
  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. $month_employee_list['data'] = collect($month_employee_list['data'])->transform(function ($item) use ($employee_key_list, $item_title_key_list, $item_code_key_list) {
  55. $item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
  56. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  57. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  58. $item['total_work_hours'] = round($item['total_work'] / 60, 2);
  59. return $item;
  60. })->all();
  61. return [true, $month_employee_list];
  62. }
  63. public function employeeMonthSalaryStatistic($data, $user)
  64. {
  65. //项目编码、项目名称、天数、工资、日期
  66. list($status, $month_start, $month_end) = $this->commonRule($data);
  67. if (!$status) return [false, $month_start];
  68. //确认所有项目、人员、人员工时
  69. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  70. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  71. ->where('del_time', 0)
  72. ->select(
  73. "item_id",
  74. "employee_id",
  75. // 将时间戳转为 Y-m-d 格式并起别名
  76. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  77. // 聚合求和
  78. DB::raw("SUM(total_work_min) as total_work")
  79. )
  80. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  81. //查询所有人员工资
  82. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  83. ->pluck('id')->toArray();
  84. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  85. ->where('del_time', 0)
  86. ->where("month", ">=", $month_start)
  87. ->where("month", "<", $month_end)
  88. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  89. ->pluck('month_str', 'id')
  90. ->toArray();
  91. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  92. ->select("employee_id", "salary", "main_id")
  93. ->get()->toArray();
  94. //查询所有项目人员的工时比例
  95. //汇总每个人每个月工资
  96. $salary_map = [];
  97. foreach ($month_employee_salary as $val) {
  98. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  99. if ($month) {
  100. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  101. }
  102. }
  103. // var_dump($salary_map);die;
  104. // 2. 计算每个员工在每个月的全月总工时
  105. $employee_monthly_total_min = [];
  106. foreach ($month_employee_list as $row) {
  107. $key = $row['employee_id'] . '_' . $row['order_month'];
  108. if (!isset($employee_monthly_total_min[$key])) {
  109. $employee_monthly_total_min[$key] = 0;
  110. }
  111. $employee_monthly_total_min[$key] += $row['total_work'];
  112. }
  113. // 3. 计算分摊天数与工资
  114. $item_month_list = [];
  115. foreach ($month_employee_list as $item) {
  116. $key = $item['employee_id'] . '_' . $item['order_month'];
  117. $item_key = $item['order_month'] . '_' . $item['item_id'];
  118. if (!isset($item_month_list[$item_key])) {
  119. $item_month_list[$item_key] = [
  120. "month" => $item['order_month'],
  121. "allocated_salary" => 0,
  122. "work_minutes" => 0,
  123. "item_id" => $item['item_id'],
  124. ];
  125. }
  126. $total_salary = $salary_map[$key] ?? 0;
  127. $total_min = $employee_monthly_total_min[$key] ?? 0;
  128. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  129. if ($total_min > 0) {
  130. $ratio = $item['total_work'] / $total_min;
  131. $allocated_salary = round($ratio * $total_salary, 2);
  132. } else {
  133. $allocated_salary = 0;
  134. }
  135. $item_month_list[$item_key]['allocated_salary'] += $allocated_salary;
  136. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  137. }
  138. foreach ($item_month_list as $k => $v) {
  139. $item_month_list[$k]['days'] = round($v['work_minutes'] / 8 / 60);
  140. unset($item_month_list[$k]['work_minutes']);
  141. }
  142. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  143. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  144. $item = Item::Clear($user, $data);
  145. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  146. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  147. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list) {
  148. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  149. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  150. return $item;
  151. })->all();
  152. return [true, $item_month_list];
  153. }
  154. public function itemDaySalaryStatistic($data, $user)
  155. {
  156. //项目编码、项目名称、人员名称、研发工时、研发工资、当月总工时、总计工资、年月
  157. list($status, $month_start, $month_end) = $this->commonRule($data);
  158. if (!$status) return [false, $month_start];
  159. //确认所有项目、人员、人员工时
  160. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  161. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  162. ->where('del_time', 0)
  163. ->select(
  164. "item_id",
  165. "employee_id",
  166. // 将时间戳转为 Y-m-d 格式并起别名
  167. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  168. // 聚合求和
  169. DB::raw("SUM(total_work_min) as total_work")
  170. )
  171. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "employee_id")->get()->toArray();
  172. //查询所有人员工资
  173. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)->pluck('id')->toArray();
  174. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  175. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  176. ->pluck('month_str', 'id')->toArray();
  177. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  178. ->select("employee_id", "salary", "main_id")
  179. ->get()->toArray();
  180. //查询所有项目人员的工时比例
  181. //汇总每个人每个月工资
  182. $salary_map = [];
  183. foreach ($month_employee_salary as $val) {
  184. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  185. if ($month) {
  186. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  187. }
  188. }
  189. // 2. 计算每个员工在每个月的全月总工时
  190. $employee_monthly_total_min = [];
  191. foreach ($month_employee_list as $row) {
  192. $key = $row['employee_id'] . '_' . $row['order_month'];
  193. if (!isset($employee_monthly_total_min[$key])) {
  194. $employee_monthly_total_min[$key] = 0;
  195. }
  196. $employee_monthly_total_min[$key] += $row['total_work'];
  197. }
  198. // 3. 计算分摊天数与工资
  199. $item_month_list = [];
  200. foreach ($month_employee_list as $item) {
  201. $key = $item['employee_id'] . '_' . $item['order_month'];
  202. $employee_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['employee_id'];
  203. $total_salary = $salary_map[$key] ?? 0;
  204. $total_min = $employee_monthly_total_min[$key] ?? 0;
  205. if (!isset($item_month_list[$employee_key])) {
  206. $item_month_list[$employee_key] = [
  207. "month" => $item['order_month'],
  208. "allocated_salary" => 0,
  209. "work_minutes" => 0,
  210. "total_min" => $total_min,
  211. "total_salary" => $total_salary,
  212. "item_id" => $item['item_id'],
  213. "employee_id" => $item['employee_id'],
  214. ];
  215. }
  216. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  217. if ($total_min > 0) {
  218. $ratio = $item['total_work'] / $total_min;
  219. $allocated_salary = round($ratio * $total_salary, 2);
  220. } else {
  221. $allocated_salary = 0;
  222. }
  223. $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
  224. $item_month_list[$employee_key]['work_minutes'] += $item['total_work'];
  225. }
  226. foreach ($item_month_list as $k => $v) {
  227. $item_month_list[$k]['days'] = round($v['work_minutes'] / 8 / 60);
  228. unset($item_month_list[$k]['work_minutes']);
  229. }
  230. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  231. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  232. $item = Item::Clear($user, $data);
  233. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  234. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  235. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  236. $employee = Employee::Clear($user, $data);
  237. $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
  238. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, $employee_key_list) {
  239. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  240. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  241. $item['employee_title'] = $employee_key_list[$item['employee_id']] ?? "未知人员({$item['employee_id']})";
  242. return $item;
  243. })->all();
  244. return [true, $item_month_list];
  245. }
  246. public function itemDeviceMonthStatistic($data, $user)
  247. {
  248. //项目编码、项目名称、设备名称、项目工时、研发工时占比、设备原值、设备折旧额、本项目帐面归集的折旧额、确定的本项目折旧额、加计调整金额、当月工时、日期
  249. list($status, $month_start, $month_end) = $this->commonRule($data);
  250. if (!$status) return [false, $month_start];
  251. //确认所有项目、设备、设备工时
  252. $month_device = DailyDwOrderDetails::Clear($user, $data);
  253. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  254. ->where('del_time', 0)
  255. ->select(
  256. "item_id",
  257. "device_id",
  258. // 将时间戳转为 Y-m-d 格式并起别名
  259. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  260. // 聚合求和
  261. DB::raw("SUM(total_work_min) as total_work")
  262. )
  263. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  264. //查询所有人员工资
  265. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  266. ->pluck('id')->toArray();
  267. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  268. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  269. ->pluck("month_str", 'id')->toArray();
  270. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  271. ->select("device_id", "depreciation_amount", "main_id")
  272. ->get()->toArray();
  273. //查询所有项目人员的工时比例
  274. //汇总每个人每个月工资
  275. $depreciatio_map = [];
  276. foreach ($month_device_salary as $val) {
  277. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  278. if ($month) {
  279. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  280. }
  281. }
  282. // 2. 计算每个员工在每个月的全月总工时
  283. $device_monthly_total_min = [];
  284. foreach ($month_device_list as $row) {
  285. $key = $row['device_id'] . '_' . $row['order_month'];
  286. if (!isset($device_monthly_total_min[$key])) {
  287. $device_monthly_total_min[$key] = 0;
  288. }
  289. $device_monthly_total_min[$key] += $row['total_work'];
  290. }
  291. // 3. 计算分摊天数与工资
  292. $item_month_list = [];
  293. foreach ($month_device_list as $item) {
  294. $key = $item['device_id'] . '_' . $item['order_month'];
  295. $device_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['device_id'];
  296. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  297. $total_min = $device_monthly_total_min[$key] ?? 0;
  298. if (!isset($item_month_list[$device_key])) {
  299. $item_month_list[$device_key] = [
  300. "month" => $item['order_month'],
  301. "allocated_depreciatio" => 0,
  302. "work_minutes" => 0,
  303. "total_min" => $total_min,
  304. "item_id" => $item['item_id'],
  305. "device_id" => $item['device_id'],
  306. "total_depreciatio" => $total_depreciatio,
  307. ];
  308. }
  309. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  310. if ($total_min > 0) {
  311. $ratio = round($item['total_work'] / $total_min, 3);
  312. $allocated_salary = round($ratio * $total_depreciatio, 2);
  313. } else {
  314. $ratio = 0;
  315. $allocated_salary = 0;
  316. }
  317. $item_month_list[$device_key]['allocated_depreciatio'] += $allocated_salary;
  318. $item_month_list[$device_key]['work_minutes'] += $item['total_work'];
  319. $item_month_list[$device_key]['ratio'] = $ratio;
  320. }
  321. foreach ($item_month_list as $k => $v) {
  322. $item_month_list[$k]['total_hours'] = round($v['total_min'] / 60, 1);
  323. $item_month_list[$k]['hours'] = round($v['work_minutes'] / 60, 1);
  324. unset($item_month_list[$k]['work_minutes']);
  325. }
  326. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  327. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  328. $item = Item::Clear($user, $data);
  329. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  330. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  331. $device_ids = collect($item_month_list)->pluck('device_id')->unique()->values()->all();
  332. $device = Device::Clear($user, $data);
  333. $device_key_list = $device->wherein('id', $device_ids)->pluck("title", "id")->toArray();
  334. $device_original_value_key_list = $device->wherein('id', $device_ids)->pluck("original_value", "id")->toArray();
  335. $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) {
  336. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  337. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  338. $item['device_title'] = $device_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  339. $item['device_original'] = $device_original_value_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
  340. return $item;
  341. })->all();
  342. return [true, $item_month_list];
  343. }
  344. private function commonRule($data)
  345. {
  346. if (!isset($data['month_start'])) $month_start = date('Y-01-01');
  347. else $month_start = date('Y-m-01', strtotime($data['month_start']));
  348. if (!isset($data['month_end'])) $month_end = date('Y-01-01', strtotime('+1 year', strtotime($month_start)));
  349. else {
  350. $start_year = date('Y', strtotime($month_start));
  351. $end_year = date('Y', strtotime($data['month_end']));
  352. if ($start_year != $end_year) return [false, "查询不得跨年!", ""];
  353. $month_end = date('Y-m-01', strtotime($data['month_end'] . ' +1 month'));
  354. }
  355. return [true, strtotime($month_start), strtotime($month_end)];
  356. }
  357. public function employeeAttendanceMonthStatistic($data, $user)
  358. {
  359. //项目编码、项目名称、项目状态、支出类型、允许加计扣除金额合计、人员人工费用、折旧费用、其他费用、前N项小计、其他相关费用合计、委内费用、委外费用、
  360. list($status, $month_start, $month_end) = $this->commonRule($data);
  361. if (!$status) {
  362. return [false, $month_start];
  363. }
  364. //第一步确定项目
  365. $item = Item::Clear($user, $data);
  366. $item_list = $item->where('del_time', 0)
  367. ->where(function ($query) use ($month_start, $month_end) {
  368. $query->where('start_time', '>=', $month_start)
  369. ->orWhere('end_time', '<=', $month_end);
  370. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time","asc")->get()->toArray();
  371. $item_key_list = [];
  372. foreach ($item_list as $v) {
  373. $item_key_list[$v['id']] = $v;
  374. }
  375. //第二步确定人员费用
  376. $item_employee_list = $this->getEmployeeItemSalary($month_start, $month_end, $data, $user);
  377. //第三步确定折旧费用
  378. $item_device_list = $this->getDeviceItemSalary($month_start, $month_end, $data, $user);
  379. //第四步其他费用
  380. list($item_fee_list,$fee_type_list) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  381. //组合所有数据
  382. $return = [];
  383. foreach ($item_key_list as $v){
  384. //其他费用是个数组
  385. $item_value = [
  386. "code" => $v['code'],
  387. "title" => $v['title'],
  388. "state" => $v['state'] == 0 ? "进行中" : "完结",
  389. "employee_salary" => $item_employee_list[$v['id']]['salary']??0,
  390. "device_depreciation" => $item_device_list[$v['id']]['depreciation']??0,
  391. "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
  392. ];
  393. $return[] = $item_value;
  394. }
  395. return [true,["list"=>$return,"fee_type_list"=>$fee_type_list]];
  396. }
  397. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  398. {
  399. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  400. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  401. ->where('del_time', 0)
  402. ->select(
  403. "item_id",
  404. "employee_id",
  405. // 将时间戳转为 Y-m-d 格式并起别名
  406. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  407. // 聚合求和
  408. DB::raw("SUM(total_work_min) as total_work")
  409. )
  410. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  411. //查询所有人员工资
  412. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  413. ->pluck('id')->toArray();
  414. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  415. ->where('del_time', 0)
  416. ->where("month", ">=", $month_start)
  417. ->where("month", "<", $month_end)
  418. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  419. ->pluck('month_str', 'id')
  420. ->toArray();
  421. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  422. ->select("employee_id", "salary", "main_id")
  423. ->get()->toArray();
  424. //查询所有项目人员的工时比例
  425. //汇总每个人每个月工资
  426. $salary_map = [];
  427. foreach ($month_employee_salary as $val) {
  428. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  429. if ($month) {
  430. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  431. }
  432. }
  433. // 2. 计算分摊天数与工资
  434. $item_list = [];
  435. foreach ($month_employee_list as $item) {
  436. $key = $item['employee_id'] . '_' . $item['order_month'];
  437. $item_key = $item['item_id'];
  438. if (!isset($item_list[$item_key])) {
  439. $item_list[$item_key] = [
  440. "salary" => 0,
  441. ];
  442. }
  443. $total_salary = $salary_map[$key] ?? 0;
  444. $total_min = $employee_monthly_total_min[$key] ?? 0;
  445. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  446. if ($total_min > 0) {
  447. $ratio = $item['total_work'] / $total_min;
  448. $allocated_salary = round($ratio * $total_salary, 2);
  449. } else {
  450. $allocated_salary = 0;
  451. }
  452. $item_list[$item_key]['salary'] += $allocated_salary;
  453. }
  454. return $item_list;
  455. }
  456. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  457. {
  458. //确认所有项目、设备、设备工时
  459. $month_device = DailyDwOrderDetails::Clear($user, $data);
  460. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  461. ->where('del_time', 0)
  462. ->select(
  463. "item_id",
  464. "device_id",
  465. // 将时间戳转为 Y-m-d 格式并起别名
  466. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  467. // 聚合求和
  468. DB::raw("SUM(total_work_min) as total_work")
  469. )
  470. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  471. //查询所有人员工资
  472. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  473. ->pluck('id')->toArray();
  474. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  475. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  476. ->pluck("month_str", 'id')->toArray();
  477. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  478. ->select("device_id", "depreciation_amount", "main_id")
  479. ->get()->toArray();
  480. //查询所有项目人员的工时比例
  481. //汇总每个人每个月工资
  482. $depreciatio_map = [];
  483. foreach ($month_device_salary as $val) {
  484. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  485. if ($month) {
  486. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  487. }
  488. }
  489. // 2. 计算每个员工在每个月的全月总工时
  490. $device_monthly_total_min = [];
  491. foreach ($month_device_list as $row) {
  492. $key = $row['device_id'] . '_' . $row['order_month'];
  493. if (!isset($device_monthly_total_min[$key])) {
  494. $device_monthly_total_min[$key] = 0;
  495. }
  496. $device_monthly_total_min[$key] += $row['total_work'];
  497. }
  498. // 3. 计算分摊天数与工资
  499. $item_list = [];
  500. foreach ($month_device_list as $item) {
  501. $key = $item['device_id'] . '_' . $item['order_month'];
  502. $device_key = $item['item_id'];
  503. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  504. $total_min = $device_monthly_total_min[$key] ?? 0;
  505. if (!isset($item_list[$device_key])) {
  506. $item_list[$device_key] = [
  507. "depreciation" => 0,
  508. ];
  509. }
  510. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  511. if ($total_min > 0) {
  512. $ratio = round($item['total_work'] / $total_min, 3);
  513. $allocated_salary = round($ratio * $total_depreciatio, 2);
  514. } else {
  515. $allocated_salary = 0;
  516. }
  517. $item_list[$device_key]['depreciation'] += $allocated_salary;
  518. }
  519. return $item_list;
  520. }
  521. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  522. {
  523. //确认所有项目、费用
  524. $expense = ExpenseClaimsDetails::Clear($user, $data);
  525. $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
  526. ->where('del_time', 0)
  527. ->select(
  528. "fee_id",
  529. "amount",
  530. "item_id",
  531. "entrust_type"
  532. )->get()->toArray();
  533. //需要根据分类去汇总
  534. $fee = Fee::Clear($user, $data);
  535. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  536. return $this->groupListByRoot($expense_list, $fee);
  537. }
  538. /**
  539. * 将报销明细按照一级费用和项目分类进行分组
  540. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  541. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  542. * @return array
  543. */
  544. public function groupListByRoot(array $list, array $fee_type_list)
  545. {
  546. // 1. 建立 ID 索引,方便快速查找
  547. $idMap = array_column($fee_type_list, null, 'id');
  548. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  549. $childToRoot = [];
  550. foreach ($fee_type_list as $type) {
  551. $current = $type;
  552. // 向上追溯直到 parent_id 为 0,即找到一级分类
  553. while ($current['parent_id'] != 0) {
  554. $current = $idMap[$current['parent_id']];
  555. }
  556. $childToRoot[$type['id']] = [
  557. 'id' => $current['id'],
  558. 'title' => $current['title'],
  559. 'sort' => $current['sort']
  560. ];
  561. }
  562. // 3. 遍历明细数据进行分组
  563. $item_key_list = [];
  564. $type_list = [];
  565. foreach ($list as $item) {
  566. $feeId = $item['fee_id'];
  567. // 获取该费用对应的一级分类信息
  568. if (!isset($childToRoot[$feeId])) continue;
  569. $rootId = $childToRoot[$feeId]['id'];
  570. $title = $childToRoot[$feeId]['title'];
  571. $sort = $childToRoot[$feeId]['sort'];
  572. $key = $item['item_id'];
  573. if (!isset($item_key_list[$key][$rootId])) {
  574. $item_key_list[$key][$rootId] = [
  575. 'id' => $rootId,
  576. 'total_amount' => 0,
  577. 'entrust1_amount' => 0, //委内
  578. 'entrust2_amount' => 0, //委外
  579. ];
  580. }
  581. if($item['entrust_type'] == 1){
  582. $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount'];
  583. }elseif ($item['entrust_type'] == 2){
  584. $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount'];
  585. }
  586. $item_key_list[$key][$rootId]['total_amount'] += $item['amount'];
  587. //这边需要拿到头部所有的一级费用类型
  588. if(!isset($type_list[$rootId])){
  589. $type_list[$rootId] = [
  590. 'sort' => $sort,
  591. 'id' => $rootId,
  592. 'title' => $title,
  593. ];
  594. }
  595. }
  596. // dd($type_list);
  597. $type_list = collect($type_list)->sortBy('sort')->all();
  598. // 4. 重置数组索引并返回
  599. return [$item_key_list,$type_list];
  600. }
  601. public function auxiliaryStatistic($data,$user){
  602. list($status, $month_start, $month_end) = $this->commonRule($data);
  603. if (!$status) return [false, $month_start];
  604. //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
  605. //确认所有项目
  606. $item = Item::Clear($user, $data);
  607. $item_list = $item->where('del_time', 0)
  608. ->where(function ($query) use ($month_start, $month_end) {
  609. $query->where('start_time', '>=', $month_start)
  610. ->orWhere('end_time', '<=', $month_end);
  611. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time","asc")->get()->toArray();
  612. $item_key_list = [];
  613. foreach ($item_list as $v) {
  614. $item_key_list[$v['id']] = $v;
  615. }
  616. //获取该区间内所有项目人工费、折旧费
  617. $item_salary = $this->auxiliaryEmployee($user,$data,$month_start,$month_end);
  618. $device_depreciation = $this->auxiliaryDevice($user,$data,$month_start,$month_end);
  619. $fee = Fee::Clear($user, $data);
  620. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  621. $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
  622. $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  623. ->where('del_time', 0)
  624. ->select(
  625. "item_id",
  626. "voucher_date",
  627. "voucher_type",
  628. "voucher_no",
  629. "voucher_remark",
  630. "voucher_amount",
  631. "aggregation_amount",
  632. "entrust_type",
  633. "entrust1_amount",
  634. "entrust2_amount",
  635. "fee_id",
  636. "type"
  637. )->get()->toArray();
  638. list($fee_amount,$fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list,$fee);
  639. // dd($item_key_list);
  640. $return = [];
  641. foreach ($fee_amount as $v){
  642. if(!isset($item_key_list[$v['item_id']])) continue;
  643. if(!isset($item_salary[$v['item_id']])) continue;
  644. if(!isset($device_depreciation[$v['item_id']])) continue;
  645. $item_value = $item_key_list[$v['item_id']];
  646. $detail = [
  647. "code" => $item_value['code'],
  648. "title" => $item_value['title'],
  649. "state" => $item_value['state'] == 0 ? "进行中":"已完成" ,
  650. "voucher_date" => date("Y-m-d",$v['voucher_date']) ,
  651. "voucher_type" => $v['voucher_type'] ,
  652. "voucher_no" => $v['voucher_no'] ,
  653. "voucher_remark" => $v['voucher_remark'] ,
  654. "voucher_amount" => $v['voucher_amount'] ,
  655. "aggregation_amount" => $v['aggregation_amount'] ,
  656. "total_amount" => $v['type'] == 1 ? $item_salary[$v['item_id']]['allocated_salary'] : ($v['type'] == 2 ? $device_depreciation[$v['item_id']]['allocated_depreciation'] : $v['total_amount']) ,
  657. "fee_id" => $v['fee_id'],
  658. "entrust1_amount" => $v['entrust1_amount'],
  659. "entrust2_amount" => $v['entrust2_amount'],
  660. ];
  661. $return[] = $detail;
  662. }
  663. return [true,[
  664. 'fee_type_list' => $fee_type_list,
  665. 'list' => $return,
  666. ]];
  667. }
  668. /**
  669. * 将报销明细按照一级费用和项目分类进行分组
  670. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  671. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  672. * @return array
  673. */
  674. public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
  675. {
  676. // 1. 建立 ID 索引,方便快速查找
  677. $idMap = array_column($fee_type_list, null, 'id');
  678. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  679. $childToRoot = [];
  680. foreach ($fee_type_list as $type) {
  681. $current = $type;
  682. // 向上追溯直到 parent_id 为 0,即找到一级分类
  683. while ($current['parent_id'] != 0) {
  684. $current = $idMap[$current['parent_id']];
  685. }
  686. $childToRoot[$type['id']] = [
  687. 'id' => $current['id'],
  688. 'title' => $current['title'],
  689. 'sort' => $current['sort']
  690. ];
  691. }
  692. // 3. 遍历明细数据进行分组
  693. $type_list = [];
  694. foreach ($list as $k=>$item) {
  695. if($item['item_id'] == 0||$item['fee_id'] == 0) {
  696. unset($list[$k]);
  697. continue;
  698. }
  699. $feeId = $item['fee_id'];
  700. // 获取该费用对应的一级分类信息
  701. if (!isset($childToRoot[$feeId])) continue;
  702. $rootId = $childToRoot[$feeId]['id'];
  703. $title = $childToRoot[$feeId]['title'];
  704. $sort = $childToRoot[$feeId]['sort'];
  705. $item['fee_id'] = $rootId;
  706. $list[$k] = $item;
  707. //这边需要拿到头部所有的一级费用类型
  708. if(!isset($type_list[$rootId])){
  709. $type_list[$rootId] = [
  710. 'sort' => $sort,
  711. 'id' => $rootId,
  712. 'title' => $title,
  713. ];
  714. }
  715. }
  716. $type_list = collect($type_list)->sortBy('sort')->all();
  717. // 4. 重置数组索引并返回
  718. return [$list,$type_list];
  719. }
  720. public function auxiliaryEmployee($user,$data,$month_start,$month_end){
  721. //确认所有项目、人员、人员工时
  722. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  723. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  724. ->where('del_time', 0)
  725. ->select(
  726. "item_id",
  727. "employee_id",
  728. // 将时间戳转为 Y-m-d 格式并起别名
  729. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  730. // 聚合求和
  731. DB::raw("SUM(total_work_min) as total_work")
  732. )
  733. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  734. //查询所有人员工资
  735. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  736. ->pluck('id')->toArray();
  737. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  738. ->where('del_time', 0)
  739. ->where("month", ">=", $month_start)
  740. ->where("month", "<", $month_end)
  741. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  742. ->pluck('month_str', 'id')
  743. ->toArray();
  744. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  745. ->select("employee_id", "salary", "main_id")
  746. ->get()->toArray();
  747. //查询所有项目人员的工时比例
  748. //汇总每个人每个月工资
  749. $salary_map = [];
  750. foreach ($month_employee_salary as $val) {
  751. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  752. if ($month) {
  753. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  754. }
  755. }
  756. // var_dump($salary_map);die;
  757. // 2. 计算每个员工在每个月的全月总工时
  758. $employee_monthly_total_min = [];
  759. foreach ($month_employee_list as $row) {
  760. $key = $row['employee_id'] . '_' . $row['order_month'];
  761. if (!isset($employee_monthly_total_min[$key])) {
  762. $employee_monthly_total_min[$key] = 0;
  763. }
  764. $employee_monthly_total_min[$key] += $row['total_work'];
  765. }
  766. // 3. 计算分摊天数与工资
  767. $item_year_list = [];
  768. foreach ($month_employee_list as $item) {
  769. $key = $item['employee_id'] . '_' . $item['order_month'];
  770. $item_key = $item['item_id'];
  771. if (!isset($item_year_list[$item_key])) {
  772. $item_year_list[$item_key] = [
  773. "allocated_salary" => 0,
  774. "item_id" => $item['item_id'],
  775. ];
  776. }
  777. $total_salary = $salary_map[$key] ?? 0;
  778. $total_min = $employee_monthly_total_min[$key] ?? 0;
  779. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  780. if ($total_min > 0) {
  781. $ratio = $item['total_work'] / $total_min;
  782. $allocated_salary = round($ratio * $total_salary, 2);
  783. } else {
  784. $allocated_salary = 0;
  785. }
  786. $item_year_list[$item_key]['allocated_salary'] += $allocated_salary;
  787. }
  788. return $item_year_list;
  789. }
  790. public function auxiliaryDevice($user,$data,$month_start,$month_end){
  791. $month_device = DailyDwOrderDetails::Clear($user, $data);
  792. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  793. ->where('del_time', 0)
  794. ->select(
  795. "item_id",
  796. "device_id",
  797. // 将时间戳转为 Y-m-d 格式并起别名
  798. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  799. // 聚合求和
  800. DB::raw("SUM(total_work_min) as total_work")
  801. )
  802. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  803. //查询所有人员工资
  804. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  805. ->pluck('id')->toArray();
  806. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  807. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  808. ->pluck("month_str", 'id')->toArray();
  809. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  810. ->select("device_id", "depreciation_amount", "main_id")
  811. ->get()->toArray();
  812. //查询所有项目人员的工时比例
  813. //汇总每个人每个月工资
  814. $depreciatio_map = [];
  815. foreach ($month_device_salary as $val) {
  816. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  817. if ($month) {
  818. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  819. }
  820. }
  821. // 2. 计算每个员工在每个月的全月总工时
  822. $device_monthly_total_min = [];
  823. foreach ($month_device_list as $row) {
  824. $key = $row['device_id'] . '_' . $row['order_month'];
  825. if (!isset($device_monthly_total_min[$key])) {
  826. $device_monthly_total_min[$key] = 0;
  827. }
  828. $device_monthly_total_min[$key] += $row['total_work'];
  829. }
  830. // 3. 计算分摊天数与工资
  831. $item_year_list = [];
  832. foreach ($month_device_list as $item) {
  833. $key = $item['device_id'] . '_' . $row['order_month'] ;
  834. $device_key = $item['item_id'];
  835. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  836. $total_min = $device_monthly_total_min[$key] ?? 0;
  837. if (!isset($item_year_list[$device_key])) {
  838. $item_year_list[$device_key] = [
  839. "allocated_depreciation" => 0,
  840. ];
  841. }
  842. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  843. if ($total_min > 0) {
  844. $ratio = round($item['total_work'] / $total_min, 3);
  845. $allocated_salary = round($ratio * $total_depreciatio, 2);
  846. } else {
  847. $allocated_salary = 0;
  848. }
  849. $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
  850. }
  851. return $item_year_list;
  852. }
  853. }