BIService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DailyPwOrder;
  4. use App\Model\ExpenseClaims;
  5. use App\Model\ExpenseClaimsDetails;
  6. use App\Model\Fee;
  7. use App\Model\Item;
  8. use App\Model\MonthlyDdOrder;
  9. use App\Model\MonthlyDdOrderDetails;
  10. use App\Model\MonthlyPsOrder;
  11. use App\Model\MonthlyPsOrderDetails;
  12. use App\Model\PLeaveOverOrder;
  13. use App\Model\PLeaveOverOrderDetails;
  14. use Illuminate\Support\Facades\DB;
  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');
  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' => 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' => 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' => 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("salary", "main_id")
  108. ->get()
  109. ->toArray();
  110. $total = 0;
  111. $salary_map = [];
  112. foreach ($month_employee_salary as $value) {
  113. $currentAmount = (string)$value['salary'];
  114. if(isset($monthly_ps_order[$value['main_id']])){
  115. $month = $monthly_ps_order[$value['main_id']];
  116. if(isset($salary_map[$month])){
  117. $salary_map[$month] = bcadd($salary_map[$month], $currentAmount,3);
  118. }else{
  119. $salary_map[$month] = $currentAmount;
  120. }
  121. }
  122. $total = bcadd($total, $currentAmount,3);
  123. }
  124. return [$total, $salary_map];
  125. }
  126. private function getDeviceSalary($month_start, $month_end, $data, $user)
  127. {
  128. $monthly_dd_order= MonthlyDdOrder::Clear($user, $data)
  129. ->where('del_time', 0)
  130. ->where("month", ">=", $month_start)
  131. ->where("month", "<", $month_end)
  132. ->pluck('month','id')
  133. ->toArray();
  134. $monthly_dd_order_ids = array_keys($monthly_dd_order);
  135. $month_device_salary = MonthlyDdOrderDetails::whereIn('main_id', $monthly_dd_order_ids)
  136. ->select("depreciation_amount as amount", "main_id")
  137. ->get()
  138. ->toArray();
  139. $total = 0;
  140. $amount_map = [];
  141. foreach ($month_device_salary as $value) {
  142. $currentAmount = (string)$value['amount'];
  143. if(isset($monthly_dd_order[$value['main_id']])){
  144. $month = $monthly_dd_order[$value['main_id']];
  145. if(isset($amount_map[$month])){
  146. $amount_map[$month] = bcadd($amount_map[$month], $currentAmount,3);
  147. }else{
  148. $amount_map[$month] = $currentAmount;
  149. }
  150. }
  151. $total = bcadd($total, $currentAmount,3);
  152. }
  153. return [$total, $amount_map];
  154. }
  155. private function getFeeItemSalary($month_start, $month_end, $data, $user)
  156. {
  157. $e_order = ExpenseClaims::Clear($user, $data)
  158. ->where('del_time', 0)
  159. ->where("month", ">=", $month_start)
  160. ->where("month", "<", $month_end)
  161. ->pluck('month','id')
  162. ->toArray();
  163. $e_order_ids = array_keys($e_order);
  164. $e_order_detail = ExpenseClaimsDetails::whereIn('expense_claims_id', $e_order_ids)
  165. ->select("amount", "expense_claims_id as main_id", "fee_id")
  166. ->get()
  167. ->toArray();
  168. $fee = Fee::TopClear($user, $data);
  169. $fee_map = $fee->whereIn('id', array_unique(array_column($e_order_detail,'fee_id')))->pluck('title','id')->toArray();
  170. $total = 0;
  171. $amount_map = [];
  172. $fee_amount_map = [];
  173. foreach ($e_order_detail as $value) {
  174. $currentAmount = (string)$value['amount'];
  175. if(isset($e_order[$value['main_id']])){
  176. $month = $e_order[$value['main_id']];
  177. if(isset($amount_map[$month])){
  178. $amount_map[$month] = bcadd($amount_map[$month], $currentAmount,3);
  179. }else{
  180. $amount_map[$month] = $currentAmount;
  181. }
  182. }
  183. $fee_tmp = $fee_map[$value['fee_id']] ?? "";
  184. if(! empty($fee_tmp)){
  185. if(isset($fee_amount_map[$fee_tmp])){
  186. $fee_amount_map[$fee_tmp] = bcadd($fee_amount_map[$fee_tmp], $currentAmount,3);
  187. }else{
  188. $fee_amount_map[$fee_tmp] = $currentAmount;
  189. }
  190. }
  191. $total = bcadd($total, $currentAmount,3);
  192. }
  193. $return_fee = [];
  194. foreach ($fee_amount_map as $key => $value){
  195. $amount = (string)$value;
  196. $return_fee[] = [
  197. "title" => $key,
  198. "total" => $amount,
  199. "total_unit" => "元",
  200. "rate" => bcmul(bcdiv($amount,$total,4),100,2),
  201. "rate_unit" => "%"
  202. ];
  203. }unset($fee_amount_map);
  204. return [$total, $amount_map, $return_fee];
  205. }
  206. private function makeTrend($month_start, $salary_map, $zj_map, $other_map)
  207. {
  208. $trend = [];
  209. for ($i = 0; $i < 12; $i++) {
  210. // 计算每个月第一天的时间戳作为 Key
  211. $timestamp = strtotime("+$i month", $month_start);
  212. $monthName = ($i + 1) . '月';
  213. $trend[$timestamp] = [
  214. 'month' => $monthName,
  215. // 'salary' => "0.000",
  216. // 'zj' => "0.000",
  217. // 'other' => "0.000",
  218. 'total' => "0.000",
  219. ];
  220. }
  221. // 2. 将传入的 Map 数据填充进去
  222. foreach ($trend as $timestamp => &$item) {
  223. // 使用 ?? "0" 容错,并强制转为 string 保证 bcadd 精度
  224. $s = $salary_map[$timestamp] ?? "0";
  225. $z = $zj_map[$timestamp] ?? "0";
  226. $o = $other_map[$timestamp] ?? "0";
  227. // $item['salary'] = bcadd($s, "0", 3);
  228. // $item['zj'] = bcadd($z, "0", 3);
  229. // $item['other'] = bcadd($o, "0", 3);
  230. // 计算该月总计
  231. $item['total'] = bcadd(bcadd($s, $z, 3), $o, 3);
  232. }
  233. return array_values($trend);
  234. }
  235. private function overLeave($month_start, $month_end, $data, $user)
  236. {
  237. $id = PLeaveOverOrder::Clear($user, $data)
  238. ->where('del_time', 0)
  239. ->where("order_time", ">=", $month_start)
  240. ->where("order_time", "<", $month_end)
  241. ->pluck('id')
  242. ->toArray();
  243. $p = PLeaveOverOrderDetails::whereIn('main_id', $id)
  244. ->select("type", "total_min")
  245. ->get()
  246. ->toArray();
  247. $total = $total_1 = 0;
  248. foreach ($p as $value) {
  249. $total_min = (string)$value['total_min'];
  250. if($value['type'] == PLeaveOverOrderDetails::TYPE_ONE){
  251. $total = bcadd($total, $total_min,2);
  252. }else{
  253. $total_1 = bcadd($total_1, $total_min,2);
  254. }
  255. }
  256. // 最终返回或输出前转换
  257. $total_hours = bcdiv($total, 60, 2);
  258. $total_1_hours = bcdiv($total_1, 60, 2);
  259. return [$total_1_hours, $total_hours];
  260. }
  261. private function manWork($month_start, $month_end, $data, $user)
  262. {
  263. $daily_pw = DailyPwOrder::Clear($user, $data)
  264. ->where('del_time', 0)
  265. ->where("order_time", ">=", $month_start)
  266. ->where("order_time", "<", $month_end)
  267. ->select('id','item_id')
  268. ->get()->toArray();
  269. $item_map = Item::whereIn('id',array_unique(array_column($daily_pw,'item_id')))
  270. ->pluck('title','id')
  271. ->toArray();
  272. $p = PLeaveOverOrderDetails::whereIn('main_id', array_column($daily_pw,'id'))
  273. ->select("item_id", "total_work_min")
  274. ->get()
  275. ->toArray();
  276. $total = 0;
  277. $map = [];
  278. foreach ($p as $value) {
  279. $total_min = (string)$value['total_min'];
  280. if(isset($map[$value['item_id']])){
  281. $map[$value['item_id']] = bcadd($map[$value['item_id']], $total_min,2);
  282. }else{
  283. $map[$value['item_id']] = $total_min;
  284. }
  285. $total = bcadd($total, $total_min,2);
  286. }
  287. foreach ($map as $key => $value){
  288. $rate = bcmul(bcdiv($value, $total,4),100,2);
  289. $total_hours = bcdiv($value, 60, 2);
  290. $return[] = [
  291. 'title' => $item_map[$key] ?? "",
  292. 'total_work_hour' => $total_hours,
  293. 'total_work_hour_unit' => "小时",
  294. "rate" => $rate,
  295. "rate_unit" => "%",
  296. ];
  297. }
  298. return $return;
  299. }
  300. }