BIService.php 13 KB

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