BIService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DailyPwOrder;
  4. use App\Model\DailyPwOrderDetails;
  5. use App\Model\ExpenseClaims;
  6. use App\Model\ExpenseClaimsDetails;
  7. use App\Model\Fee;
  8. use App\Model\Item;
  9. use App\Model\MonthlyDdOrder;
  10. use App\Model\MonthlyDdOrderDetails;
  11. use App\Model\MonthlyPsOrder;
  12. use App\Model\MonthlyPsOrderDetails;
  13. use App\Model\PLeaveOverOrder;
  14. use App\Model\PLeaveOverOrderDetails;
  15. class BIService extends Service
  16. {
  17. private function commonRule($data)
  18. {
  19. if (isset($data['time']) && !empty($data['time'])) {
  20. $start = $this->changeDateToDate($data['time'][0]);
  21. $end = $this->changeDateToDate($data['time'][1], true);
  22. $data['month_start'] = date("Y-m-d", $start);
  23. $data['month_end'] = date("Y-m-d", $end);
  24. }
  25. if (!isset($data['month_start'])) $month_start = date('Y-01-01', strtotime('-1 year'));
  26. else $month_start = date('Y-m-01', strtotime($data['month_start']));
  27. if (!isset($data['month_end'])) $month_end = date('Y-01-01', strtotime('+1 year', strtotime($month_start)));
  28. else {
  29. $start_year = date('Y', strtotime($month_start));
  30. $end_year = date('Y', strtotime($data['month_end']));
  31. if ($start_year != $end_year) return [false, "查询不得跨年!", ""];
  32. $month_end = date('Y-m-01', strtotime($data['month_end'] . ' +1 month'));
  33. }
  34. return [true, strtotime($month_start), strtotime($month_end)];
  35. }
  36. public function homePageData($data, $user)
  37. {
  38. $model = Item::TopClear($user,$data);
  39. $start_time = $model->where('del_time',0)
  40. ->orderby('id', 'desc')->value("start_time");
  41. $year = date('Y-01-01',$start_time);
  42. $data['month_start'] = $year;
  43. // $data['month_start'] = "2024-01-01";
  44. list($status, $month_start, $month_end) = $this->commonRule($data);
  45. if (!$status) return [false, $month_start];
  46. //人员费用
  47. list($total_man, $salary_map) = $this->getEmployeeSalary($month_start, $month_end, $data, $user);
  48. //折旧费用
  49. list($total_zj, $zj_map) = $this->getDeviceSalary($month_start, $month_end, $data, $user);
  50. //费用报销
  51. list($total_other, $other_map, $return_fee) = $this->getFeeItemSalary($month_start, $month_end, $data, $user);
  52. //研发费用总额
  53. $total = bcadd(bcadd($total_man, $total_zj,3), $total_other,3);
  54. //研发费用结构
  55. $rd = [
  56. 0 => [
  57. 'title' => '人工费用',
  58. 'rate' => $total == 0 ? 0 : bcmul(bcdiv($total_man,$total,4),100,2),
  59. 'rate_unit' => '%',
  60. 'total' => $total_man,
  61. 'total_unit' => '¥',
  62. ],
  63. 1 => [
  64. 'title' => '折旧费用',
  65. 'rate' => $total == 0 ? 0 :bcmul(bcdiv($total_zj,$total,4),100,2),
  66. 'rate_unit' => '%',
  67. 'total' => $total_zj,
  68. 'total_unit' => '¥',
  69. ],
  70. 2 => [
  71. 'title' => '费用报销',
  72. 'rate' => $total == 0 ? 0 :bcmul(bcdiv($total_other,$total,4),100,2),
  73. 'rate_unit' => '%',
  74. 'total' => $total_other,
  75. 'total_unit' => '¥',
  76. ],
  77. ];
  78. //研发费用趋势
  79. $rd_trend = $this->makeTrend($month_start, $salary_map, $zj_map, $other_map);
  80. //研发费用报销占比
  81. $rd_rate = $return_fee;
  82. //加班 请假 总时长
  83. list($over_time, $leave_time) = $this->overLeave($month_start, $month_end, $data, $user);
  84. // 项目人员工时汇总
  85. $man_work = $this->manWork($month_start, $month_end, $data, $user);
  86. //加计扣除金额
  87. $statisticService = new StatisticService();
  88. $attendance = $statisticService->employeeAttendanceMonthStatistic(["year"=>$year, "s" => "/api/employeeAttendanceMonthStatistic"],$user);
  89. $discount = 0; //todo
  90. foreach ($attendance[1]['list'] as $v){
  91. $discount += $v['jj_total_amount']*100;
  92. }
  93. return [true, [
  94. 'rd_total' => $total,
  95. 'rd_unit' => '¥',
  96. 'rd_title' => '研发费用总额', //-----
  97. 'discount_total' => round($discount/100,2),
  98. 'discount_unit' => '¥',
  99. 'discount_title' => '加计扣除金额', //------
  100. 'over_time' => $over_time, // 加班
  101. 'leave_time' => $leave_time, // 请假
  102. 'rd' => $rd, //研发费用结构
  103. 'rd_trend' => $rd_trend, //研发费用趋势
  104. 'rd_rate' => $rd_rate, // 研发费用报销占比
  105. 'man_work' => $man_work, //项目人员工时汇总
  106. 'year' => date('Y',strtotime($year)),
  107. ]];
  108. }
  109. private function getEmployeeSalary($month_start, $month_end, $data, $user)
  110. {
  111. $monthly_ps_order = MonthlyPsOrder::Clear($user, $data)
  112. ->where('del_time', 0)
  113. ->where("month", ">=", $month_start)
  114. ->where("month", "<", $month_end)
  115. ->pluck('month','id')
  116. ->toArray();
  117. $monthly_ps_order_ids = array_keys($monthly_ps_order);
  118. $month_employee_salary = MonthlyPsOrderDetails::whereIn('main_id', $monthly_ps_order_ids)
  119. ->select("base_salary","performance_salary","other","bonus", "main_id")
  120. ->get()
  121. ->toArray();
  122. $total = 0;
  123. $salary_map = [];
  124. foreach ($month_employee_salary as $value) {
  125. $currentAmount = bcadd($value['base_salary'], $value['performance_salary'],3);
  126. $currentAmount = bcadd($currentAmount, $value['other'],3);
  127. $currentAmount = bcadd($currentAmount, $value['bonus'],3);
  128. if(isset($monthly_ps_order[$value['main_id']])){
  129. $month = $monthly_ps_order[$value['main_id']];
  130. if(isset($salary_map[$month])){
  131. $salary_map[$month] = bcadd($salary_map[$month], $currentAmount,3);
  132. }else{
  133. $salary_map[$month] = $currentAmount;
  134. }
  135. }
  136. $total = bcadd($total, $currentAmount,3);
  137. }
  138. return [$total, $salary_map];
  139. }
  140. private function getDeviceSalary($month_start, $month_end, $data, $user)
  141. {
  142. $monthly_dd_order= MonthlyDdOrder::Clear($user, $data)
  143. ->where('del_time', 0)
  144. ->where("month", ">=", $month_start)
  145. ->where("month", "<", $month_end)
  146. ->pluck('month','id')
  147. ->toArray();
  148. $monthly_dd_order_ids = array_keys($monthly_dd_order);
  149. $month_device_salary = MonthlyDdOrderDetails::whereIn('main_id', $monthly_dd_order_ids)
  150. ->select("depreciation_amount as amount", "main_id")
  151. ->get()
  152. ->toArray();
  153. $total = 0;
  154. $amount_map = [];
  155. foreach ($month_device_salary as $value) {
  156. $currentAmount = (string)$value['amount'];
  157. if(isset($monthly_dd_order[$value['main_id']])){
  158. $month = $monthly_dd_order[$value['main_id']];
  159. if(isset($amount_map[$month])){
  160. $amount_map[$month] = bcadd($amount_map[$month], $currentAmount,3);
  161. }else{
  162. $amount_map[$month] = $currentAmount;
  163. }
  164. }
  165. $total = bcadd($total, $currentAmount,3);
  166. }
  167. return [$total, $amount_map];
  168. }
  169. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  170. {
  171. $e_order = ExpenseClaims::Clear($user, $data)
  172. ->where('del_time', 0)
  173. ->where("month", ">=", $month_start)
  174. ->where("month", "<", $month_end)
  175. ->pluck('month','id')
  176. ->toArray();
  177. $e_order_ids = array_keys($e_order);
  178. $e_order_detail = ExpenseClaimsDetails::whereIn('expense_claims_id', $e_order_ids)
  179. ->select("amount", "expense_claims_id as main_id", "fee_id")
  180. ->get()
  181. ->toArray();
  182. $fee = Fee::TopClear($user, $data);
  183. $fee_map = $fee->whereIn('id', array_unique(array_column($e_order_detail,'fee_id')))->pluck('title','id')->toArray();
  184. $total = 0;
  185. $amount_map = [];
  186. $fee_amount_map = [];
  187. foreach ($e_order_detail as $value) {
  188. $currentAmount = (string)$value['amount'];
  189. if(isset($e_order[$value['main_id']])){
  190. $month = $e_order[$value['main_id']];
  191. if(isset($amount_map[$month])){
  192. $amount_map[$month] = bcadd($amount_map[$month], $currentAmount,3);
  193. }else{
  194. $amount_map[$month] = $currentAmount;
  195. }
  196. }
  197. $fee_tmp = $fee_map[$value['fee_id']] ?? "";
  198. if(! empty($fee_tmp)){
  199. if(isset($fee_amount_map[$fee_tmp])){
  200. $fee_amount_map[$fee_tmp] = bcadd($fee_amount_map[$fee_tmp], $currentAmount,3);
  201. }else{
  202. $fee_amount_map[$fee_tmp] = $currentAmount;
  203. }
  204. }
  205. $total = bcadd($total, $currentAmount,3);
  206. }
  207. $return_fee = [];
  208. foreach ($fee_amount_map as $key => $value){
  209. $amount = (string)$value;
  210. $return_fee[] = [
  211. "title" => $key,
  212. "total" => $amount,
  213. "total_unit" => "元",
  214. "rate" => bcmul(bcdiv($amount,$total,4),100,2),
  215. "rate_unit" => "%"
  216. ];
  217. }unset($fee_amount_map);
  218. return [$total, $amount_map, $return_fee];
  219. }
  220. private function makeTrend($month_start, $salary_map, $zj_map, $other_map)
  221. {
  222. $trend = [];
  223. for ($i = 0; $i < 12; $i++) {
  224. // 计算每个月第一天的时间戳作为 Key
  225. $timestamp = strtotime("+$i month", $month_start);
  226. $monthName = ($i + 1) . '月';
  227. $trend[$timestamp] = [
  228. 'month' => $monthName,
  229. // 'salary' => "0.000",
  230. // 'zj' => "0.000",
  231. // 'other' => "0.000",
  232. 'total' => "0.000",
  233. ];
  234. }
  235. // 2. 将传入的 Map 数据填充进去
  236. foreach ($trend as $timestamp => &$item) {
  237. // 使用 ?? "0" 容错,并强制转为 string 保证 bcadd 精度
  238. $s = $salary_map[$timestamp] ?? "0";
  239. $z = $zj_map[$timestamp] ?? "0";
  240. $o = $other_map[$timestamp] ?? "0";
  241. // $item['salary'] = bcadd($s, "0", 3);
  242. // $item['zj'] = bcadd($z, "0", 3);
  243. // $item['other'] = bcadd($o, "0", 3);
  244. // 计算该月总计
  245. $item['total'] = bcadd(bcadd($s, $z, 3), $o, 3);
  246. }
  247. return array_values($trend);
  248. }
  249. private function overLeave($month_start, $month_end, $data, $user)
  250. {
  251. $id = PLeaveOverOrder::Clear($user, $data)
  252. ->where('del_time', 0)
  253. ->where("order_time", ">=", $month_start)
  254. ->where("order_time", "<", $month_end)
  255. ->pluck('id')
  256. ->toArray();
  257. $p = PLeaveOverOrderDetails::whereIn('main_id', $id)
  258. ->select("type", "total_min")
  259. ->get()
  260. ->toArray();
  261. $total = $total_1 = 0;
  262. foreach ($p as $value) {
  263. $total_min = (string)$value['total_min'];
  264. if($value['type'] == PLeaveOverOrderDetails::TYPE_ONE){
  265. $total = bcadd($total, $total_min,2);
  266. }else{
  267. $total_1 = bcadd($total_1, $total_min,2);
  268. }
  269. }
  270. // 最终返回或输出前转换
  271. $total_hours = bcdiv($total, 60, 2);
  272. $total_1_hours = bcdiv($total_1, 60, 2);
  273. return [$total_1_hours, $total_hours];
  274. }
  275. private function manWork($month_start, $month_end, $data, $user)
  276. {
  277. $daily_pw = DailyPwOrder::Clear($user, $data)
  278. ->where('del_time', 0)
  279. ->where("order_time", ">=", $month_start)
  280. ->where("order_time", "<", $month_end)
  281. ->select('id','item_id')
  282. ->get()->toArray();
  283. $item_map = Item::whereIn('id',array_unique(array_column($daily_pw,'item_id')))
  284. ->pluck('title','id')
  285. ->toArray();
  286. $p = DailyPwOrderDetails::whereIn('main_id', array_column($daily_pw,'id'))
  287. ->select("item_id", "total_work_min")
  288. ->get()
  289. ->toArray();
  290. $total = 0;
  291. $map = [];
  292. foreach ($p as $value) {
  293. $total_min = (string)$value['total_work_min'];
  294. if(isset($map[$value['item_id']])){
  295. $map[$value['item_id']] = bcadd($map[$value['item_id']], $total_min,2);
  296. }else{
  297. $map[$value['item_id']] = $total_min;
  298. }
  299. $total = bcadd($total, $total_min,2);
  300. }
  301. $return = [];
  302. foreach ($map as $key => $value){
  303. $rate = bcmul(bcdiv($value, $total,4),100,2);
  304. $total_hours = bcdiv($value, 60, 2);
  305. $return[] = [
  306. 'title' => $item_map[$key] ?? "",
  307. 'total_work_hour' => $total_hours,
  308. 'total_work_hour_unit' => "小时",
  309. "rate" => $rate,
  310. "rate_unit" => "%",
  311. ];
  312. }
  313. return $return;
  314. }
  315. }