StatisticService.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. "expense_type" => "费用化支出",
  392. "fee_list" => collect($item_fee_list[$v['id']] ?? [])->values()->all(),
  393. ];
  394. $return[] = $item_value;
  395. }
  396. return [true,["list"=>$return,"fee_type_list"=>$fee_type_list]];
  397. }
  398. private function getEmployeeItemSalary($month_start, $month_end, $data, $user)
  399. {
  400. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  401. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  402. ->where('del_time', 0)
  403. ->select(
  404. "item_id",
  405. "employee_id",
  406. // 将时间戳转为 Y-m-d 格式并起别名
  407. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  408. // 聚合求和
  409. DB::raw("SUM(total_work_min) as total_work")
  410. )
  411. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  412. //查询所有人员工资
  413. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  414. ->pluck('id')->toArray();
  415. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  416. ->where('del_time', 0)
  417. ->where("month", ">=", $month_start)
  418. ->where("month", "<", $month_end)
  419. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  420. ->pluck('month_str', 'id')
  421. ->toArray();
  422. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  423. ->select("employee_id", "salary", "main_id")
  424. ->get()->toArray();
  425. //查询所有项目人员的工时比例
  426. //汇总每个人每个月工资
  427. $salary_map = [];
  428. foreach ($month_employee_salary as $val) {
  429. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  430. if ($month) {
  431. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  432. }
  433. }
  434. // 2. 计算分摊天数与工资
  435. $item_list = [];
  436. foreach ($month_employee_list as $item) {
  437. $key = $item['employee_id'] . '_' . $item['order_month'];
  438. $item_key = $item['item_id'];
  439. if (!isset($item_list[$item_key])) {
  440. $item_list[$item_key] = [
  441. "salary" => 0,
  442. ];
  443. }
  444. $total_salary = $salary_map[$key] ?? 0;
  445. $total_min = $employee_monthly_total_min[$key] ?? 0;
  446. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  447. if ($total_min > 0) {
  448. $ratio = $item['total_work'] / $total_min;
  449. $allocated_salary = round($ratio * $total_salary, 2);
  450. } else {
  451. $allocated_salary = 0;
  452. }
  453. $item_list[$item_key]['salary'] += $allocated_salary;
  454. }
  455. return $item_list;
  456. }
  457. private function getDeviceItemSalary($month_start, $month_end, $data, $user)
  458. {
  459. //确认所有项目、设备、设备工时
  460. $month_device = DailyDwOrderDetails::Clear($user, $data);
  461. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  462. ->where('del_time', 0)
  463. ->select(
  464. "item_id",
  465. "device_id",
  466. // 将时间戳转为 Y-m-d 格式并起别名
  467. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  468. // 聚合求和
  469. DB::raw("SUM(total_work_min) as total_work")
  470. )
  471. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  472. //查询所有人员工资
  473. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  474. ->pluck('id')->toArray();
  475. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  476. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  477. ->pluck("month_str", 'id')->toArray();
  478. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  479. ->select("device_id", "depreciation_amount", "main_id")
  480. ->get()->toArray();
  481. //查询所有项目人员的工时比例
  482. //汇总每个人每个月工资
  483. $depreciatio_map = [];
  484. foreach ($month_device_salary as $val) {
  485. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  486. if ($month) {
  487. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  488. }
  489. }
  490. // 2. 计算每个员工在每个月的全月总工时
  491. $device_monthly_total_min = [];
  492. foreach ($month_device_list as $row) {
  493. $key = $row['device_id'] . '_' . $row['order_month'];
  494. if (!isset($device_monthly_total_min[$key])) {
  495. $device_monthly_total_min[$key] = 0;
  496. }
  497. $device_monthly_total_min[$key] += $row['total_work'];
  498. }
  499. // 3. 计算分摊天数与工资
  500. $item_list = [];
  501. foreach ($month_device_list as $item) {
  502. $key = $item['device_id'] . '_' . $item['order_month'];
  503. $device_key = $item['item_id'];
  504. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  505. $total_min = $device_monthly_total_min[$key] ?? 0;
  506. if (!isset($item_list[$device_key])) {
  507. $item_list[$device_key] = [
  508. "depreciation" => 0,
  509. ];
  510. }
  511. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  512. if ($total_min > 0) {
  513. $ratio = round($item['total_work'] / $total_min, 3);
  514. $allocated_salary = round($ratio * $total_depreciatio, 2);
  515. } else {
  516. $allocated_salary = 0;
  517. }
  518. $item_list[$device_key]['depreciation'] += $allocated_salary;
  519. }
  520. return $item_list;
  521. }
  522. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  523. {
  524. //确认所有项目、费用
  525. $expense = ExpenseClaimsDetails::Clear($user, $data);
  526. $expense_list = $expense->where("claim_date", ">=", $month_start)->where("claim_date", "<", $month_end)
  527. ->where('del_time', 0)
  528. ->select(
  529. "fee_id",
  530. "amount",
  531. "item_id",
  532. "entrust_type"
  533. )->get()->toArray();
  534. //需要根据分类去汇总
  535. $fee = Fee::Clear($user, $data);
  536. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  537. return $this->groupListByRoot($expense_list, $fee);
  538. }
  539. /**
  540. * 将报销明细按照一级费用和项目分类进行分组
  541. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  542. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  543. * @return array
  544. */
  545. public function groupListByRoot(array $list, array $fee_type_list)
  546. {
  547. // 1. 建立 ID 索引,方便快速查找
  548. $idMap = array_column($fee_type_list, null, 'id');
  549. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  550. $childToRoot = [];
  551. foreach ($fee_type_list as $type) {
  552. $current = $type;
  553. // 向上追溯直到 parent_id 为 0,即找到一级分类
  554. while ($current['parent_id'] != 0) {
  555. $current = $idMap[$current['parent_id']];
  556. }
  557. $childToRoot[$type['id']] = [
  558. 'id' => $current['id'],
  559. 'title' => $current['title'],
  560. 'sort' => $current['sort']
  561. ];
  562. }
  563. // 3. 遍历明细数据进行分组
  564. $item_key_list = [];
  565. $type_list = [];
  566. foreach ($list as $item) {
  567. $feeId = $item['fee_id'];
  568. // 获取该费用对应的一级分类信息
  569. if (!isset($childToRoot[$feeId])) continue;
  570. $rootId = $childToRoot[$feeId]['id'];
  571. $title = $childToRoot[$feeId]['title'];
  572. $sort = $childToRoot[$feeId]['sort'];
  573. $key = $item['item_id'];
  574. if (!isset($item_key_list[$key][$rootId])) {
  575. $item_key_list[$key][$rootId] = [
  576. 'id' => $rootId,
  577. 'total_amount' => 0,
  578. 'entrust1_amount' => 0, //委内
  579. 'entrust2_amount' => 0, //委外
  580. ];
  581. }
  582. if($item['entrust_type'] == 1){
  583. $item_key_list[$key][$rootId]['entrust1_amount'] += $item['amount'];
  584. }elseif ($item['entrust_type'] == 2){
  585. $item_key_list[$key][$rootId]['entrust2_amount'] += $item['amount'];
  586. }
  587. $item_key_list[$key][$rootId]['total_amount'] += $item['amount'];
  588. //这边需要拿到头部所有的一级费用类型
  589. if(!isset($type_list[$rootId])){
  590. $type_list[$rootId] = [
  591. 'sort' => $sort,
  592. 'id' => $rootId,
  593. 'title' => $title,
  594. ];
  595. }
  596. }
  597. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  598. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  599. // 4. 重置数组索引并返回
  600. return [$item_key_list,$type_list];
  601. }
  602. public function auxiliaryStatistic($data,$user){
  603. list($status, $month_start, $month_end) = $this->commonRule($data);
  604. if (!$status) return [false, $month_start];
  605. //项目编码、项目名称、项目状态、凭证日期、凭证种类、凭证号数、凭证摘要、会计凭证归集金额、N个一级费用类型、委内、委外
  606. //确认所有项目
  607. $item = Item::Clear($user, $data);
  608. $item_list = $item->where('del_time', 0)
  609. ->where(function ($query) use ($month_start, $month_end) {
  610. $query->where('start_time', '>=', $month_start)
  611. ->orWhere('end_time', '<=', $month_end);
  612. })->select("code", "title", "start_time", "end_time", "id", "state")->orderby("start_time","asc")->get()->toArray();
  613. $item_key_list = [];
  614. foreach ($item_list as $v) {
  615. $item_key_list[$v['id']] = $v;
  616. }
  617. //获取该区间内所有项目人工费、折旧费
  618. $item_salary = $this->auxiliaryEmployee($user,$data,$month_start,$month_end);
  619. $device_depreciation = $this->auxiliaryDevice($user,$data,$month_start,$month_end);
  620. $fee = Fee::Clear($user, $data);
  621. $fee = $fee->where('del_time', 0)->orderBy("sort", 'desc')->get()->toArray();
  622. $auxiliary = AuxiliaryAccountDetails::Clear($user, $data);
  623. $auxiliary_list = $auxiliary->where("voucher_date", ">=", $month_start)->where("voucher_date", "<", $month_end)
  624. ->where('del_time', 0)
  625. ->select(
  626. "item_id",
  627. "voucher_date",
  628. "voucher_type",
  629. "voucher_no",
  630. "voucher_remark",
  631. "voucher_amount",
  632. "aggregation_amount",
  633. "entrust_type",
  634. "entrust1_amount",
  635. "entrust2_amount",
  636. "fee_id",
  637. "type"
  638. )->get()->toArray();
  639. list($fee_amount,$fee_type_list) = $this->auxiliaryGroupListByRoot($auxiliary_list,$fee);
  640. // dd($item_key_list);
  641. $return = [];
  642. foreach ($fee_amount as $v){
  643. if(!isset($item_key_list[$v['item_id']])) continue;
  644. if(!isset($item_salary[$v['item_id']])) continue;
  645. if(!isset($device_depreciation[$v['item_id']])) continue;
  646. $item_value = $item_key_list[$v['item_id']];
  647. $detail = [
  648. "code" => $item_value['code'],
  649. "title" => $item_value['title'],
  650. "state" => $item_value['state'] == 0 ? "进行中":"已完成" ,
  651. "voucher_date" => date("Y-m-d",$v['voucher_date']) ,
  652. "voucher_type" => $v['voucher_type'] ,
  653. "voucher_no" => $v['voucher_no'] ,
  654. "voucher_remark" => $v['voucher_remark'] ,
  655. "voucher_amount" => $v['voucher_amount'] ,
  656. "aggregation_amount" => $v['aggregation_amount'] ,
  657. "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']) ,
  658. "fee_id" => $v['fee_id'],
  659. "entrust1_amount" => $v['entrust1_amount'],
  660. "entrust2_amount" => $v['entrust2_amount'],
  661. "type" => $v['type'],
  662. ];
  663. $return[] = $detail;
  664. }
  665. return [true,[
  666. 'fee_type_list' => $fee_type_list,
  667. 'list' => $return,
  668. ]];
  669. }
  670. /**
  671. * 将报销明细按照一级费用和项目分类进行分组
  672. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  673. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  674. * @return array
  675. */
  676. public function auxiliaryGroupListByRoot(array $list, array $fee_type_list)
  677. {
  678. // 1. 建立 ID 索引,方便快速查找
  679. $idMap = array_column($fee_type_list, null, 'id');
  680. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  681. $childToRoot = [];
  682. foreach ($fee_type_list as $type) {
  683. $current = $type;
  684. // 向上追溯直到 parent_id 为 0,即找到一级分类
  685. while ($current['parent_id'] != 0) {
  686. $current = $idMap[$current['parent_id']];
  687. }
  688. $childToRoot[$type['id']] = [
  689. 'id' => $current['id'],
  690. 'title' => $current['title'],
  691. 'sort' => $current['sort']
  692. ];
  693. }
  694. // 3. 遍历明细数据进行分组
  695. $type_list = [];
  696. foreach ($list as $k=>$item) {
  697. if($item['item_id'] == 0||$item['fee_id'] == 0) {
  698. unset($list[$k]);
  699. continue;
  700. }
  701. $feeId = $item['fee_id'];
  702. // 获取该费用对应的一级分类信息
  703. if (!isset($childToRoot[$feeId])) continue;
  704. $rootId = $childToRoot[$feeId]['id'];
  705. $title = $childToRoot[$feeId]['title'];
  706. $sort = $childToRoot[$feeId]['sort'];
  707. $item['fee_id'] = $rootId;
  708. $list[$k] = $item;
  709. //这边需要拿到头部所有的一级费用类型
  710. if(!isset($type_list[$rootId])){
  711. $type_list[$rootId] = [
  712. 'sort' => $sort,
  713. 'id' => $rootId,
  714. 'title' => $title,
  715. ];
  716. }
  717. }
  718. // 使用 values() 丢弃原始键名,重新从 0 开始建立索引
  719. $type_list = collect($type_list)->sortBy('sort')->values()->all();
  720. // 4. 重置数组索引并返回
  721. return [$list,$type_list];
  722. }
  723. public function auxiliaryEmployee($user,$data,$month_start,$month_end){
  724. //确认所有项目、人员、人员工时
  725. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  726. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  727. ->where('del_time', 0)
  728. ->select(
  729. "item_id",
  730. "employee_id",
  731. // 将时间戳转为 Y-m-d 格式并起别名
  732. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  733. // 聚合求和
  734. DB::raw("SUM(total_work_min) as total_work")
  735. )
  736. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  737. //查询所有人员工资
  738. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  739. ->pluck('id')->toArray();
  740. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  741. ->where('del_time', 0)
  742. ->where("month", ">=", $month_start)
  743. ->where("month", "<", $month_end)
  744. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  745. ->pluck('month_str', 'id')
  746. ->toArray();
  747. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  748. ->select("employee_id", "salary", "main_id")
  749. ->get()->toArray();
  750. //查询所有项目人员的工时比例
  751. //汇总每个人每个月工资
  752. $salary_map = [];
  753. foreach ($month_employee_salary as $val) {
  754. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  755. if ($month) {
  756. $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
  757. }
  758. }
  759. // var_dump($salary_map);die;
  760. // 2. 计算每个员工在每个月的全月总工时
  761. $employee_monthly_total_min = [];
  762. foreach ($month_employee_list as $row) {
  763. $key = $row['employee_id'] . '_' . $row['order_month'];
  764. if (!isset($employee_monthly_total_min[$key])) {
  765. $employee_monthly_total_min[$key] = 0;
  766. }
  767. $employee_monthly_total_min[$key] += $row['total_work'];
  768. }
  769. // 3. 计算分摊天数与工资
  770. $item_year_list = [];
  771. foreach ($month_employee_list as $item) {
  772. $key = $item['employee_id'] . '_' . $item['order_month'];
  773. $item_key = $item['item_id'];
  774. if (!isset($item_year_list[$item_key])) {
  775. $item_year_list[$item_key] = [
  776. "allocated_salary" => 0,
  777. "item_id" => $item['item_id'],
  778. ];
  779. }
  780. $total_salary = $salary_map[$key] ?? 0;
  781. $total_min = $employee_monthly_total_min[$key] ?? 0;
  782. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  783. if ($total_min > 0) {
  784. $ratio = $item['total_work'] / $total_min;
  785. $allocated_salary = round($ratio * $total_salary, 2);
  786. } else {
  787. $allocated_salary = 0;
  788. }
  789. $item_year_list[$item_key]['allocated_salary'] += $allocated_salary;
  790. }
  791. return $item_year_list;
  792. }
  793. public function auxiliaryDevice($user,$data,$month_start,$month_end){
  794. $month_device = DailyDwOrderDetails::Clear($user, $data);
  795. $month_device_list = $month_device->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  796. ->where('del_time', 0)
  797. ->select(
  798. "item_id",
  799. "device_id",
  800. // 将时间戳转为 Y-m-d 格式并起别名
  801. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  802. // 聚合求和
  803. DB::raw("SUM(total_work_min) as total_work")
  804. )
  805. ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m')"), "item_id", "device_id")->get()->toArray();
  806. //查询所有人员工资
  807. $monthly_dd_order_ids = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  808. ->pluck('id')->toArray();
  809. $monthly_dd_order_key_list = MonthlyDdOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  810. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  811. ->pluck("month_str", 'id')->toArray();
  812. $month_device_salary = MonthlyDdOrderDetails::wherein('main_id', $monthly_dd_order_ids)
  813. ->select("device_id", "depreciation_amount", "main_id")
  814. ->get()->toArray();
  815. //查询所有项目人员的工时比例
  816. //汇总每个人每个月工资
  817. $depreciatio_map = [];
  818. foreach ($month_device_salary as $val) {
  819. $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
  820. if ($month) {
  821. $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
  822. }
  823. }
  824. // 2. 计算每个员工在每个月的全月总工时
  825. $device_monthly_total_min = [];
  826. foreach ($month_device_list as $row) {
  827. $key = $row['device_id'] . '_' . $row['order_month'];
  828. if (!isset($device_monthly_total_min[$key])) {
  829. $device_monthly_total_min[$key] = 0;
  830. }
  831. $device_monthly_total_min[$key] += $row['total_work'];
  832. }
  833. // 3. 计算分摊天数与工资
  834. $item_year_list = [];
  835. foreach ($month_device_list as $item) {
  836. $key = $item['device_id'] . '_' . $row['order_month'] ;
  837. $device_key = $item['item_id'];
  838. $total_depreciatio = $depreciatio_map[$key] ?? 0;
  839. $total_min = $device_monthly_total_min[$key] ?? 0;
  840. if (!isset($item_year_list[$device_key])) {
  841. $item_year_list[$device_key] = [
  842. "allocated_depreciation" => 0,
  843. ];
  844. }
  845. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  846. if ($total_min > 0) {
  847. $ratio = round($item['total_work'] / $total_min, 3);
  848. $allocated_salary = round($ratio * $total_depreciatio, 2);
  849. } else {
  850. $allocated_salary = 0;
  851. }
  852. $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
  853. }
  854. return $item_year_list;
  855. }
  856. public function itemEmployeeSalaryStatistic($data,$user){
  857. list($status, $month_start, $month_end) = $this->commonRule($data);
  858. if (!$status) return [false, $month_start];
  859. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  860. //获取人员工资项目相关分组数据
  861. $month_employee = DailyPwOrderDetails::Clear($user, $data);
  862. $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
  863. ->where('del_time', 0)
  864. ->select(
  865. "item_id",
  866. "employee_id",
  867. // 将时间戳转为 Y-m-d 格式并起别名
  868. DB::raw("FROM_UNIXTIME(order_time, '%Y-%m') as order_month"),
  869. // 聚合求和
  870. DB::raw("SUM(total_work_min) as total_work")
  871. )
  872. ->groupBy("order_month", "item_id", "employee_id")->get()->toArray();
  873. //查询所有人员工资
  874. $monthly_ps_order_ids = MonthlyPsOrder::Clear($user, $data)->where('del_time', 0)->where("month", ">=", $month_start)->where("month", "<", $month_end)
  875. ->pluck('id')->toArray();
  876. $monthly_ps_order_key_list = MonthlyPsOrder::Clear($user, $data)
  877. ->where('del_time', 0)
  878. ->where("month", ">=", $month_start)
  879. ->where("month", "<", $month_end)
  880. ->select('id', DB::raw("FROM_UNIXTIME(month, '%Y-%m') as month_str"))
  881. ->pluck('month_str', 'id')
  882. ->toArray();
  883. $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
  884. ->select("employee_id", "salary", "main_id","social_insurance","public_housing_fund")
  885. ->get()->toArray();
  886. //查询所有项目人员的工时比例
  887. //汇总每个人每个月工资
  888. $salary_map = [];
  889. foreach ($month_employee_salary as $val) {
  890. $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
  891. if ($month) {
  892. $salary_map[$val['employee_id'] . '_' . $month] = $val;
  893. }
  894. }
  895. // var_dump($salary_map);die;
  896. // 2. 计算每个员工在每个月的全月总工时
  897. $employee_monthly_total_min = [];
  898. foreach ($month_employee_list as $row) {
  899. $key = $row['employee_id'] . '_' . $row['order_month'];
  900. if (!isset($employee_monthly_total_min[$key])) {
  901. $employee_monthly_total_min[$key] = 0;
  902. }
  903. $employee_monthly_total_min[$key] += $row['total_work'];
  904. }
  905. // 3. 计算分摊天数与工资
  906. $item_month_list = [];
  907. foreach ($month_employee_list as $item) {
  908. $key = $item['employee_id'] . '_' . $item['order_month'];
  909. $item_key = $item['order_month'] . '_' . $item['item_id'].'_'.$item['employee_id'] ;
  910. if (!isset($item_month_list[$item_key])) {
  911. $item_month_list[$item_key] = [
  912. "month" => $item['order_month'],
  913. "allocated_salary" => 0,
  914. "employee_id" => $item['employee_id'],
  915. "work_minutes" => 0,
  916. "salary" => $salary_map[$key]['salary'] ?? 0,
  917. "social_insurance" => $salary_map[$key]['social_insurance']??0,
  918. "public_housing_fund" => $salary_map[$key]['public_housing_fund']??0,
  919. "item_id" => $item['item_id'],
  920. ];
  921. }
  922. $total_min = $employee_monthly_total_min[$key] ?? 0;
  923. // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
  924. if ($total_min > 0) {
  925. $ratio = round($item['total_work'] / $total_min,4);
  926. }else{
  927. $ratio = 0;
  928. }
  929. $item_month_list[$item_key]['radio'] = $ratio;
  930. $item_month_list[$item_key]['total_min'] = $total_min;
  931. $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
  932. }
  933. $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
  934. $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
  935. $employee_ids = collect($item_month_list)->pluck('employee_id')->unique()->values()->all();
  936. $employee = Employee::Clear($user, $data);
  937. $employee_list = $employee->wherein('id', $employee_ids)->select("major","title", "id")->get()->toArray();
  938. $employee_key_list = [];
  939. foreach ($employee_list as $v){
  940. $employee_key_list[$v['id']] = $v;
  941. }
  942. $item = Item::Clear($user, $data);
  943. $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
  944. $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
  945. //项目编码、项目名称、姓名、人员类别、应出勤工时、研发出勤工时、研发工时占比、归集工资总额、归集社保金额、归集公积金、研发工资总额、研发社保金额、研发公积金
  946. $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list,$employee_key_list) {
  947. $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  948. $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
  949. $item['employee_title'] = $employee_key_list[$item['employee_id']]['title'] ?? "未知人员({$item['employee_id']})";
  950. $item['major'] = $employee_key_list[$item['employee_id']]['major'] ?? "未知人员({$item['employee_id']})";
  951. $item['total_min'] = round($item['total_min']/60,2);
  952. $item['work_minutes'] = round($item['work_minutes']/60,2);
  953. $item['work_salary'] = round($item['salary']*$item['radio'],2);
  954. $item['work_social_insurance'] = round($item['social_insurance']*$item['radio'],2);
  955. $item['work_public_housing_fund'] = round($item['public_housing_fund']*$item['radio'],2);
  956. return $item;
  957. })->all();
  958. return [true, $item_month_list];
  959. }
  960. }