StatisticsService.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Service;
  3. use App\Model\RevenueCost;
  4. class StatisticsService extends Service
  5. {
  6. public function statisticsRevenueCostCommon($data,$user, $field = []){
  7. if(empty($data['order_time']) || ! is_array($data['order_time'])) return [false, '单据日期不能为空'];
  8. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  9. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  10. list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
  11. if(! $bool) return [false, $bool_msg];
  12. if(empty($field)) $field = RevenueCost::$field;
  13. $model = RevenueCost::where('del_time',0)
  14. ->select($field)
  15. ->orderby('id', 'desc');
  16. $model->where('order_time', '>=', $start_time)
  17. ->where('order_time', '<', $end_time);
  18. return [true, $model];
  19. }
  20. public function statisticsRevenueCost($data,$user){
  21. list($status, $model) = $this->statisticsRevenueCostCommon($data, $user);
  22. if(! $status) return [false, $model];
  23. $result = $model->get()->toArray();
  24. $return = $this->statisticsRevenueCostFillData($result);
  25. return [true, $return];
  26. }
  27. public function statisticsRevenueCostFillData($data){
  28. if(empty($data)) return $data;
  29. $return = [];
  30. foreach ($data as $key => $value){
  31. $time = date("Y-m", $value['order_time']);
  32. if($value['order_type'] == RevenueCost::ORDER_ONE){
  33. $income = $value['price_3_total'];
  34. }elseif ($value['order_type'] == RevenueCost::ORDER_TWO){
  35. $income = $value['price_1_total'];
  36. }else{
  37. $income = $value['payment_amount'];
  38. }
  39. $adjust = $income < 0 ? $income : 0;
  40. $business = $value['price_4_total'];
  41. if(isset($return[$time][$value['order_type']])){
  42. $income_total = bcadd($return[$time][$value['order_type']]['income'], $income,2);
  43. $adjust_total = bcadd($return[$time][$value['order_type']]['adjust'], $adjust,2);
  44. $business_total = bcadd($return[$time][$value['order_type']]['business'], $business,2);
  45. $return[$time][$value['order_type']]['income'] = $income_total;
  46. $return[$time][$value['order_type']]['adjust'] = $adjust_total;
  47. $return[$time][$value['order_type']]['business'] = $business_total;
  48. }else{
  49. $return[$time][$value['order_type']] = [
  50. 'income' => $value['price_3_total'],
  51. 'adjust' => $adjust,
  52. 'business' => $business,
  53. 'time' => $time,
  54. ];
  55. }
  56. }
  57. $final = [];
  58. foreach ($return as $value){
  59. foreach ($value as $key => $val){
  60. $title = RevenueCost::$order_type[$key] ?? "";
  61. $val['title'] = $title;
  62. $profit = bcsub($val['income'], $val['business'],2);
  63. $profit_rate = $val['income'] > 0 ? bcdiv($profit, $val['income'],2) : 0;
  64. $val['profit'] = $profit;
  65. $val['profit_rate'] = $profit_rate;
  66. $val['order_type'] = $key;
  67. $final[] = $val;
  68. }
  69. }
  70. return $final;
  71. }
  72. }