changeDateToTimeStampAboutRange($data['order_time'],false); if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"]; list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time); if(! $bool) return [false, $bool_msg]; if(empty($field)) $field = RevenueCost::$field; $model = RevenueCost::where('del_time',0) ->select($field) ->orderby('id', 'desc'); $model->where('order_time', '>=', $start_time) ->where('order_time', '<', $end_time); return [true, $model]; } public function statisticsRevenueCost($data,$user){ list($status, $model) = $this->statisticsRevenueCostCommon($data, $user); if(! $status) return [false, $model]; $result = $model->get()->toArray(); $return = $this->statisticsRevenueCostFillData($result); return [true, $return]; } public function statisticsRevenueCostFillData($data){ if(empty($data)) return $data; $return = []; foreach ($data as $key => $value){ $time = date("Y-m", $value['order_time']); if($value['order_type'] == RevenueCost::ORDER_ONE){ $income = $value['price_3_total']; }elseif ($value['order_type'] == RevenueCost::ORDER_TWO){ $income = $value['price_1_total']; }else{ $income = $value['payment_amount']; } $adjust = $income < 0 ? $income : 0; $business = $value['price_4_total']; if(isset($return[$time][$value['order_type']])){ $income_total = bcadd($return[$time][$value['order_type']]['income'], $income,2); $adjust_total = bcadd($return[$time][$value['order_type']]['adjust'], $adjust,2); $business_total = bcadd($return[$time][$value['order_type']]['business'], $business,2); $return[$time][$value['order_type']]['income'] = $income_total; $return[$time][$value['order_type']]['adjust'] = $adjust_total; $return[$time][$value['order_type']]['business'] = $business_total; }else{ $return[$time][$value['order_type']] = [ 'income' => $value['price_3_total'], 'adjust' => $adjust, 'business' => $business, 'time' => $time, ]; } } $final = []; foreach ($return as $value){ foreach ($value as $key => $val){ $title = RevenueCost::$order_type[$key] ?? ""; $val['title'] = $title; $profit = bcsub($val['income'], $val['business'],2); $profit_rate = $val['income'] > 0 ? bcdiv($profit, $val['income'],2) : 0; $val['profit'] = $profit; $val['profit_rate'] = $profit_rate; $val['order_type'] = $key; $final[] = $val; } } return $final; } }