BIService.php 13 KB

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