BIService.php 12 KB

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