|
@@ -6,6 +6,7 @@ use App\Model\BasicType;
|
|
use App\Model\Customer;
|
|
use App\Model\Customer;
|
|
use App\Model\Depart;
|
|
use App\Model\Depart;
|
|
use App\Model\DepartIndex;
|
|
use App\Model\DepartIndex;
|
|
|
|
+use App\Model\Employee;
|
|
use App\Model\InOutRecord;
|
|
use App\Model\InOutRecord;
|
|
use App\Model\InvoiceOrder;
|
|
use App\Model\InvoiceOrder;
|
|
use App\Model\InvoiceOrderInfo;
|
|
use App\Model\InvoiceOrderInfo;
|
|
@@ -42,8 +43,9 @@ class StatisticsService extends Service
|
|
$list = $model->get()->toArray();
|
|
$list = $model->get()->toArray();
|
|
$list1 = $this->fillStatisticsCustomer($list);
|
|
$list1 = $this->fillStatisticsCustomer($list);
|
|
$list2 = $this->fillStatisticsCustomer2($list);
|
|
$list2 = $this->fillStatisticsCustomer2($list);
|
|
|
|
+ $list3 = $this->fillStatisticsCustomer3($list);
|
|
|
|
|
|
- return [true, ['bt' => $list1, 'zz' => $list2]];
|
|
|
|
|
|
+ return [true, ['bt' => $list1, 'zz' => $list2, 'table' => $list3]];
|
|
}
|
|
}
|
|
|
|
|
|
public function fillStatisticsCustomer($data){
|
|
public function fillStatisticsCustomer($data){
|
|
@@ -116,11 +118,136 @@ class StatisticsService extends Service
|
|
return $return;
|
|
return $return;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public function fillStatisticsCustomer3($data){
|
|
|
|
+ if(empty($data)) return $data;
|
|
|
|
+
|
|
|
|
+ $array = array_unique(array_column($data,'customer_from'));
|
|
|
|
+ $basic_map = BasicType::whereIn('id',$array)
|
|
|
|
+ ->pluck('title','id')
|
|
|
|
+ ->toArray();
|
|
|
|
+ $return_first = $customer_from_map = $header = [];
|
|
|
|
+ foreach ($data as $value){
|
|
|
|
+ $title = $basic_map[$value['customer_from']] ?? "";
|
|
|
|
+ if(! $title) continue;
|
|
|
|
+ if(isset($return_first[$title])){
|
|
|
|
+ $return_first[$title]['total'] += 1;
|
|
|
|
+ }else{
|
|
|
|
+ $return_first[$title] = [
|
|
|
|
+ 'title' => $title,
|
|
|
|
+ 'total' => 1,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ $customer_from_map[$value['id']] = $value['customer_from'];
|
|
|
|
+ if(! in_array($title, $header)) $header[] = $title;
|
|
|
|
+ }
|
|
|
|
+ $header_tmp = $header_return = [];
|
|
|
|
+ foreach ($header as $value){
|
|
|
|
+ $header_tmp[] = [
|
|
|
|
+ "title" => $value,
|
|
|
|
+ "total" => 0,
|
|
|
|
+ ];
|
|
|
|
+ $header_return[] = [
|
|
|
|
+ "title" => $value
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $return_first = array_values($return_first);
|
|
|
|
+
|
|
|
|
+ $depart = Depart::where('del_time',0)
|
|
|
|
+ ->where('parent_id',0)
|
|
|
|
+ ->where('is_main',0)
|
|
|
|
+ ->pluck('title','id')
|
|
|
|
+ ->toArray();
|
|
|
|
+ $return = [];
|
|
|
|
+ foreach ($depart as $key => $value){
|
|
|
|
+ $return[$key] = [
|
|
|
|
+ 'title' => $value,
|
|
|
|
+ 'total' => 0,
|
|
|
|
+ 'customer_from' => $header_tmp,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $top_depart_id = SeeRange::where('del_time',0)
|
|
|
|
+ ->where('data_type',SeeRange::type_one)
|
|
|
|
+ ->where('type',SeeRange::data_three)
|
|
|
|
+ ->whereIn('data_id', array_column($data,'id'))
|
|
|
|
+ ->select('param_id as fp_top_depart_id','data_id as customer_id')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ $top_depart_map = $top_depart_map2 = [];
|
|
|
|
+ foreach ($top_depart_id as $value){
|
|
|
|
+ if(isset($top_depart_map[$value['fp_top_depart_id']])){
|
|
|
|
+ $top_depart_map[$value['fp_top_depart_id']] += 1;
|
|
|
|
+ }else{
|
|
|
|
+ $top_depart_map[$value['fp_top_depart_id']] = 1;
|
|
|
|
+ }
|
|
|
|
+ $customer_from_tmp = $customer_from_map[$value['customer_id']] ?? 0;
|
|
|
|
+ if($customer_from_tmp){
|
|
|
|
+ if(isset($top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp])){
|
|
|
|
+ $top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp] += 1;
|
|
|
|
+ }else{
|
|
|
|
+ $top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp] = 1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach ($return as $key => $value){
|
|
|
|
+ if(isset($top_depart_map[$key])) {
|
|
|
|
+ $num = $top_depart_map[$key];
|
|
|
|
+ if($num <= 0) {
|
|
|
|
+ unset($return[$key]);
|
|
|
|
+ continue;
|
|
|
|
+ }else{
|
|
|
|
+ $return[$key]['total'] = $top_depart_map[$key];
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ unset($return[$key]);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if(isset($top_depart_map2[$key])) {
|
|
|
|
+ $tmp = $top_depart_map2[$key];
|
|
|
|
+ foreach ($tmp as $k => $v){
|
|
|
|
+ $title = $basic_map[$k] ?? "";
|
|
|
|
+ if(! $title) continue;
|
|
|
|
+
|
|
|
|
+ foreach ($return[$key]['customer_from'] as $kk => $vv){
|
|
|
|
+ if($vv['title'] == $title){
|
|
|
|
+ $return[$key]['customer_from'][$kk]['total'] = $v;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $return = array_values($return);
|
|
|
|
+ usort($return, function($a, $b) {
|
|
|
|
+ return $b['total'] - $a['total'];
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return ['header' => $header_return,'left' => $return_first, 'right' => $return];
|
|
|
|
+ }
|
|
|
|
+
|
|
//销售统计饼图 根据合同类型区分(例如线上合同、线下合同)
|
|
//销售统计饼图 根据合同类型区分(例如线上合同、线下合同)
|
|
public function statisticsBt($data,$user){
|
|
public function statisticsBt($data,$user){
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择销售订单时间区间'];
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择销售订单时间区间'];
|
|
|
|
|
|
|
|
+ //总社id
|
|
|
|
+ $head = $user['head']['id'] ?? 0;
|
|
|
|
+ //当前门店
|
|
|
|
+ $current_top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
|
+ $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
|
|
|
|
+
|
|
|
|
+ //需要所有数据的门店
|
|
|
|
+ $setting = Setting::where('setting_name','bt_top_depart_id')->first();
|
|
|
|
+ $bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
|
|
+ $bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
|
|
+
|
|
|
|
+ //当前门店是否在设置门店中
|
|
|
|
+ $bool = in_array($current_top_depart_id,$bt_top_depart_id);
|
|
|
|
+
|
|
$model = SalesOrder::where('del_time',0)
|
|
$model = SalesOrder::where('del_time',0)
|
|
|
|
+ ->when(! $bool, function ($query) use ($current_top_depart_id) {
|
|
|
|
+ return $query->where('top_depart_id', $current_top_depart_id);
|
|
|
|
+ })
|
|
->select('model_type','contract_fee as total','top_depart_id');
|
|
->select('model_type','contract_fee as total','top_depart_id');
|
|
|
|
|
|
if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
|
|
if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
|
|
@@ -130,23 +257,23 @@ class StatisticsService extends Service
|
|
}
|
|
}
|
|
|
|
|
|
$list = $model->get()->toArray();
|
|
$list = $model->get()->toArray();
|
|
- $list = $this->fillStatisticsBt($list);
|
|
|
|
|
|
+ $list = $this->fillStatisticsBt($list,$bt_top_depart_id,$bool);
|
|
|
|
|
|
return [true, $list];
|
|
return [true, $list];
|
|
}
|
|
}
|
|
|
|
|
|
- public function fillStatisticsBt($data){
|
|
|
|
|
|
+ public function fillStatisticsBt($data,$bt_top_depart_id,$bool){
|
|
if(empty($data)) return $data;
|
|
if(empty($data)) return $data;
|
|
|
|
|
|
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
|
|
|
|
- $bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
|
|
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
|
|
-
|
|
|
|
$return = [];
|
|
$return = [];
|
|
foreach ($data as $value){
|
|
foreach ($data as $value){
|
|
$model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
|
|
$model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
|
|
- if($value['model_type'] != SalesOrder::Model_type_two){
|
|
|
|
- if(in_array($value['top_depart_id'], $bt_top_depart_id)){
|
|
|
|
|
|
+ if($bool){
|
|
|
|
+ if($value['model_type'] != SalesOrder::Model_type_two){
|
|
|
|
+ if(in_array($value['top_depart_id'], $bt_top_depart_id)){
|
|
|
|
+ $this->makeThis($return,$value,$model_type_title);
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
$this->makeThis($return,$value,$model_type_title);
|
|
$this->makeThis($return,$value,$model_type_title);
|
|
}
|
|
}
|
|
}else{
|
|
}else{
|
|
@@ -175,6 +302,12 @@ class StatisticsService extends Service
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
$return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
$return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
|
|
|
|
|
+ //总社id
|
|
|
|
+ $head = $user['head']['id'] ?? 0;
|
|
|
|
+ //当前门店
|
|
|
|
+ $current_top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
|
+ $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
|
|
|
|
+
|
|
//销售订单类型
|
|
//销售订单类型
|
|
$model_type = 0;
|
|
$model_type = 0;
|
|
if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
@@ -222,9 +355,15 @@ class StatisticsService extends Service
|
|
$bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
$bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
$bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
$bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
|
|
|
|
|
|
+ //当前门店是否在设置门店中
|
|
|
|
+ $bool = in_array($current_top_depart_id,$bt_top_depart_id);
|
|
|
|
+
|
|
$sale_order = SalesOrder::where("del_time",0)
|
|
$sale_order = SalesOrder::where("del_time",0)
|
|
->where('crt_time','>=',$return[0])
|
|
->where('crt_time','>=',$return[0])
|
|
->where('crt_time','<=',$return[1])
|
|
->where('crt_time','<=',$return[1])
|
|
|
|
+ ->when(! $bool, function ($query) use ($current_top_depart_id) {
|
|
|
|
+ return $query->where('top_depart_id', $current_top_depart_id);
|
|
|
|
+ })
|
|
->when(! empty($model_type), function ($query) use ($model_type) {
|
|
->when(! empty($model_type), function ($query) use ($model_type) {
|
|
return $query->where('model_type',$model_type);
|
|
return $query->where('model_type',$model_type);
|
|
})
|
|
})
|
|
@@ -293,6 +432,12 @@ class StatisticsService extends Service
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
$return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
$return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
|
|
|
|
|
+ //总社id
|
|
|
|
+ $head = $user['head']['id'] ?? 0;
|
|
|
|
+ //当前门店
|
|
|
|
+ $current_top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
|
+ $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
|
|
|
|
+
|
|
//销售订单类型
|
|
//销售订单类型
|
|
$model_type = 0;
|
|
$model_type = 0;
|
|
if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
@@ -338,9 +483,15 @@ class StatisticsService extends Service
|
|
$bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
$bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
$bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
$bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
|
|
|
|
|
|
+ //当前门店是否在设置门店中
|
|
|
|
+ $bool = in_array($current_top_depart_id,$bt_top_depart_id);
|
|
|
|
+
|
|
$sale_order = SalesOrder::where("del_time",0)
|
|
$sale_order = SalesOrder::where("del_time",0)
|
|
->where('crt_time','>=',$return[0])
|
|
->where('crt_time','>=',$return[0])
|
|
->where('crt_time','<=',$return[1])
|
|
->where('crt_time','<=',$return[1])
|
|
|
|
+ ->when(! $bool, function ($query) use ($current_top_depart_id) {
|
|
|
|
+ return $query->where('top_depart_id', $current_top_depart_id);
|
|
|
|
+ })
|
|
->when(! empty($model_type), function ($query) use ($model_type) {
|
|
->when(! empty($model_type), function ($query) use ($model_type) {
|
|
return $query->where('model_type',$model_type);
|
|
return $query->where('model_type',$model_type);
|
|
})
|
|
})
|
|
@@ -406,6 +557,12 @@ class StatisticsService extends Service
|
|
$address_map = array_column($address_map,'label','value');
|
|
$address_map = array_column($address_map,'label','value');
|
|
if(! empty($data['province']) && ! isset($address_map[$data['province']])) return [false, '该省不存在'];
|
|
if(! empty($data['province']) && ! isset($address_map[$data['province']])) return [false, '该省不存在'];
|
|
|
|
|
|
|
|
+ //总社id
|
|
|
|
+ $head = $user['head']['id'] ?? 0;
|
|
|
|
+ //当前门店
|
|
|
|
+ $current_top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
|
+ $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
|
|
|
|
+
|
|
//销售订单类型
|
|
//销售订单类型
|
|
$model_type = 0;
|
|
$model_type = 0;
|
|
if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
@@ -439,10 +596,16 @@ class StatisticsService extends Service
|
|
$bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
$bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
$bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
$bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
|
|
|
|
|
|
+ //当前门店是否在设置门店中
|
|
|
|
+ $bool = in_array($current_top_depart_id,$bt_top_depart_id);
|
|
|
|
+
|
|
//合同
|
|
//合同
|
|
$sale_order = SalesOrder::where("del_time",0)
|
|
$sale_order = SalesOrder::where("del_time",0)
|
|
->where('crt_time','>=',$return[0])
|
|
->where('crt_time','>=',$return[0])
|
|
->where('crt_time','<=',$return[1])
|
|
->where('crt_time','<=',$return[1])
|
|
|
|
+ ->when(! $bool, function ($query) use ($current_top_depart_id) {
|
|
|
|
+ return $query->where('top_depart_id', $current_top_depart_id);
|
|
|
|
+ })
|
|
->when(! empty($model_type), function ($query) use ($model_type) {
|
|
->when(! empty($model_type), function ($query) use ($model_type) {
|
|
return $query->where('model_type',$model_type);
|
|
return $query->where('model_type',$model_type);
|
|
})
|
|
})
|
|
@@ -495,6 +658,20 @@ class StatisticsService extends Service
|
|
$return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
$return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
if(empty($data['top_depart_id'])) return [false, '请选择门店'];
|
|
if(empty($data['top_depart_id'])) return [false, '请选择门店'];
|
|
|
|
|
|
|
|
+ //总社id
|
|
|
|
+ $head = $user['head']['id'] ?? 0;
|
|
|
|
+ //当前门店
|
|
|
|
+ $current_top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
|
+ $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
|
|
|
|
+
|
|
|
|
+ //特殊的门店
|
|
|
|
+ $setting = Setting::where('setting_name','bt_top_depart_id')->first();
|
|
|
|
+ $bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
|
|
+ $bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
|
|
+
|
|
|
|
+ //当前门店是否在设置门店中
|
|
|
|
+ $bool = in_array($current_top_depart_id,$bt_top_depart_id);
|
|
|
|
+
|
|
//销售订单类型
|
|
//销售订单类型
|
|
$model_type = 0;
|
|
$model_type = 0;
|
|
if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
@@ -508,6 +685,9 @@ class StatisticsService extends Service
|
|
->where('top_depart_id',$data['top_depart_id'])
|
|
->where('top_depart_id',$data['top_depart_id'])
|
|
->where('crt_time','>=',$return[0])
|
|
->where('crt_time','>=',$return[0])
|
|
->where('crt_time','<=',$return[1])
|
|
->where('crt_time','<=',$return[1])
|
|
|
|
+ ->when(! $bool, function ($query) use ($current_top_depart_id) {
|
|
|
|
+ return $query->where('top_depart_id', $current_top_depart_id);
|
|
|
|
+ })
|
|
->when(! empty($model_type), function ($query) use ($model_type) {
|
|
->when(! empty($model_type), function ($query) use ($model_type) {
|
|
return $query->where('model_type',$model_type);
|
|
return $query->where('model_type',$model_type);
|
|
})
|
|
})
|
|
@@ -682,6 +862,287 @@ class StatisticsService extends Service
|
|
return [$returnExchange_map,$returnExchange_map_2];
|
|
return [$returnExchange_map,$returnExchange_map_2];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //线下订单
|
|
|
|
+ public function statisticsModelTypeOne($data,$user){
|
|
|
|
+ if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
|
|
+ $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
|
+ if(empty($data['x'])) return [false, '请选择X轴维度'];
|
|
|
|
+
|
|
|
|
+ //总社id
|
|
|
|
+ $head = $user['head']['id'] ?? 0;
|
|
|
|
+ //当前门店
|
|
|
|
+ $current_top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
|
+ $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
|
|
|
|
+
|
|
|
|
+ //线下 销售订单类型
|
|
|
|
+ $model_type = SalesOrder::Model_type_one;
|
|
|
|
+
|
|
|
|
+ //特殊的门店
|
|
|
|
+ $setting = Setting::where('setting_name','bt_top_depart_id')->first();
|
|
|
|
+ $bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
|
|
+ $bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
|
|
+
|
|
|
|
+ $sale_order = SalesOrder::where("del_time",0)
|
|
|
|
+ ->where('crt_time','>=',$return[0])
|
|
|
|
+ ->where('crt_time','<=',$return[1])
|
|
|
|
+ ->where('model_type',$model_type)
|
|
|
|
+ ->when(! empty($current_top_depart_id) && $current_top_depart_id != $head, function ($query) use ($current_top_depart_id) {
|
|
|
|
+ return $query->where('top_depart_id', $current_top_depart_id);
|
|
|
|
+ })
|
|
|
|
+ ->select('id','top_depart_id','contract_fee','crt_id')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ //合同
|
|
|
|
+ $sale_order_id = array_column($sale_order,'id');
|
|
|
|
+
|
|
|
|
+ $statistics = [];
|
|
|
|
+ if($data['x'] == "crt_id"){
|
|
|
|
+ $emp = Employee::whereIn('id', array_unique(array_column($sale_order,'crt_id')))
|
|
|
|
+ ->pluck('emp_name','id')
|
|
|
|
+ ->toArray();
|
|
|
|
+
|
|
|
|
+ //退货的差异
|
|
|
|
+ $returnExchange_map = [];
|
|
|
|
+ $returnExchange = ReturnExchangeOrder::where('del_time',0)
|
|
|
|
+ ->where('type',ReturnExchangeOrder::Order_type)
|
|
|
|
+ ->whereIn('data_id',array_unique($sale_order_id))
|
|
|
|
+ ->select('top_depart_id','difference_amount','data_id')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ foreach ($returnExchange as $value){
|
|
|
|
+ if(isset($returnExchange_map[$value['data_id']])){
|
|
|
|
+ $total = bcadd($returnExchange_map[$value['data_id']], $value['difference_amount'],2);
|
|
|
|
+ $returnExchange_map[$value['data_id']] = $total;
|
|
|
|
+ }else{
|
|
|
|
+ $returnExchange_map[$value['data_id']] = $value['difference_amount'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach ($sale_order as $value){
|
|
|
|
+ $crt_name = $emp[$value['crt_id']] ?? "";
|
|
|
|
+ if(! $crt_name) continue;
|
|
|
|
+ $return_money = $returnExchange_map[$value['id']] ?? 0;
|
|
|
|
+ $tmp = bcsub($value['contract_fee'], $return_money,2);
|
|
|
|
+ if(isset($statistics[$value['crt_id']])){
|
|
|
|
+ $total = bcadd($tmp,$statistics[$value['crt_id']]['total'],2);
|
|
|
|
+ $statistics[$value['crt_id']]['total'] = $total;
|
|
|
|
+ }else{
|
|
|
|
+ $statistics[$value['crt_id']] = [
|
|
|
|
+ 'title' => $crt_name,
|
|
|
|
+ 'total' => $tmp,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }elseif($data['x'] == "product"){
|
|
|
|
+ $sales_product = SalesOrderProductInfo::where('del_time',0)
|
|
|
|
+ ->whereIn('sales_order_id',$sale_order_id)
|
|
|
|
+ ->select('product_id','number','retail_price','price')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ $product = Product::whereIn('id',array_unique(array_column($sales_product,'product_id')))
|
|
|
|
+ ->select('id','title','code')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ $product_map = [];
|
|
|
|
+ foreach ($product as $value){
|
|
|
|
+ $product_map[$value['id']] = $value['title'] . "(" . $value['code']. ")";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //退货的差异
|
|
|
|
+ $returnExchange_map = [];
|
|
|
|
+ $returnExchange = ReturnExchangeOrder::where('del_time',0)
|
|
|
|
+ ->where('type',ReturnExchangeOrder::Order_type)
|
|
|
|
+ ->whereIn('data_id',array_unique($sale_order_id))
|
|
|
|
+ ->select('id')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
|
|
|
|
+ ->whereIn('return_exchange_id', array_column($returnExchange,'id'))
|
|
|
|
+ ->select('product_id','number','return_exchange_price')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ foreach ($return_product as $value){
|
|
|
|
+ $tmp = bcmul($value['number'],$value['return_exchange_price'],2);
|
|
|
|
+ if(isset($returnExchange_map[$value['product_id']])){
|
|
|
|
+ $total = bcadd($returnExchange_map[$value['product_id']], $tmp,2);
|
|
|
|
+ $returnExchange_map[$value['product_id']] = $total;
|
|
|
|
+ }else{
|
|
|
|
+ $returnExchange_map[$value['product_id']] = $tmp;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach ($sales_product as $value){
|
|
|
|
+ $product_tmp = $product_map[$value['product_id']] ?? "";
|
|
|
|
+ if(! $product_tmp) continue;
|
|
|
|
+ if($value['price'] > 0){
|
|
|
|
+ $tmp = bcmul($value['price'], $value['number'],2);
|
|
|
|
+ }else{
|
|
|
|
+ $tmp = bcmul($value['retail_price'], $value['number'],2);
|
|
|
|
+ }
|
|
|
|
+ $return_tmp = 0;
|
|
|
|
+ if(isset($returnExchange_map[$value['product_id']])){
|
|
|
|
+ $return_tmp = $returnExchange_map[$value['product_id']] ?? 0;
|
|
|
|
+ unset($returnExchange_map[$value['product_id']]);
|
|
|
|
+ }
|
|
|
|
+ $tmp = bcsub($tmp,$return_tmp,2);
|
|
|
|
+ if(isset($statistics[$value['product_id']])){
|
|
|
|
+ $total = bcadd($tmp,$statistics[$value['product_id']]['total'],2);
|
|
|
|
+ $statistics[$value['product_id']]['total'] = $total;
|
|
|
|
+ }else{
|
|
|
|
+ $statistics[$value['product_id']] = [
|
|
|
|
+ 'title' => $product_tmp,
|
|
|
|
+ 'total' => $tmp,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach ($statistics as $key => $value){
|
|
|
|
+ if(floatval($value['total']) <= 0) unset($statistics[$key]);
|
|
|
|
+ }
|
|
|
|
+ $statistics = array_values($statistics);
|
|
|
|
+ usort($statistics, function($a, $b) {
|
|
|
|
+ return $b['total'] - $a['total'];
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return [true, $statistics];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //线上订单
|
|
|
|
+ public function statisticsModelTypeFour($data,$user){
|
|
|
|
+ if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
|
|
+ $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
|
+ if(empty($data['x'])) return [false, '请选择X轴维度'];
|
|
|
|
+
|
|
|
|
+ //总社id
|
|
|
|
+ $head = $user['head']['id'] ?? 0;
|
|
|
|
+ //当前门店
|
|
|
|
+ $current_top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
|
+ $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
|
|
|
|
+
|
|
|
|
+ //线下 销售订单类型
|
|
|
|
+ $model_type = SalesOrder::Model_type_four;
|
|
|
|
+
|
|
|
|
+ //特殊的门店
|
|
|
|
+ $setting = Setting::where('setting_name','bt_top_depart_id')->first();
|
|
|
|
+ $bt_top_depart_id = $setting['setting_value'] ?? [];
|
|
|
|
+ $bt_top_depart_id = json_decode($bt_top_depart_id,true);
|
|
|
|
+
|
|
|
|
+ $sale_order = SalesOrder::where("del_time",0)
|
|
|
|
+ ->where('crt_time','>=',$return[0])
|
|
|
|
+ ->where('crt_time','<=',$return[1])
|
|
|
|
+ ->where('model_type',$model_type)
|
|
|
|
+ ->when(! empty($current_top_depart_id) && $current_top_depart_id != $head, function ($query) use ($current_top_depart_id) {
|
|
|
|
+ return $query->where('top_depart_id', $current_top_depart_id);
|
|
|
|
+ })
|
|
|
|
+ ->select('id','top_depart_id','contract_fee','plat_type')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ //合同
|
|
|
|
+ $sale_order_id = array_column($sale_order,'id');
|
|
|
|
+
|
|
|
|
+ $statistics = [];
|
|
|
|
+ if($data['x'] == "order_from"){
|
|
|
|
+ $array = array_unique(array_column($sale_order,'plat_type'));
|
|
|
|
+ $basic_map = BasicType::whereIn('id',$array)
|
|
|
|
+ ->pluck('title','id')
|
|
|
|
+ ->toArray();
|
|
|
|
+
|
|
|
|
+ //退货的差异
|
|
|
|
+ $returnExchange_map = [];
|
|
|
|
+ $returnExchange = ReturnExchangeOrder::where('del_time',0)
|
|
|
|
+ ->where('type',ReturnExchangeOrder::Order_type)
|
|
|
|
+ ->whereIn('data_id',array_unique($sale_order_id))
|
|
|
|
+ ->select('top_depart_id','difference_amount','data_id')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ foreach ($returnExchange as $value){
|
|
|
|
+ if(isset($returnExchange_map[$value['data_id']])){
|
|
|
|
+ $total = bcadd($returnExchange_map[$value['data_id']], $value['difference_amount'],2);
|
|
|
|
+ $returnExchange_map[$value['data_id']] = $total;
|
|
|
|
+ }else{
|
|
|
|
+ $returnExchange_map[$value['data_id']] = $value['difference_amount'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach ($sale_order as $value){
|
|
|
|
+ $plat_type = $basic_map[$value['plat_type']] ?? "";
|
|
|
|
+ if(! $plat_type) continue;
|
|
|
|
+ $return_money = $returnExchange_map[$value['id']] ?? 0;
|
|
|
|
+ $tmp = bcsub($value['contract_fee'], $return_money,2);
|
|
|
|
+ if(isset($statistics[$value['plat_type']])){
|
|
|
|
+ $total = bcadd($tmp,$statistics[$value['plat_type']]['total'],2);
|
|
|
|
+ $statistics[$value['plat_type']]['total'] = $total;
|
|
|
|
+ }else{
|
|
|
|
+ $statistics[$value['plat_type']] = [
|
|
|
|
+ 'title' => $plat_type,
|
|
|
|
+ 'total' => $tmp,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }elseif($data['x'] == "product"){
|
|
|
|
+ $sales_product = SalesOrderProductInfo::where('del_time',0)
|
|
|
|
+ ->whereIn('sales_order_id',$sale_order_id)
|
|
|
|
+ ->select('product_id','number','retail_price','price')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ $product = Product::whereIn('id',array_unique(array_column($sales_product,'product_id')))
|
|
|
|
+ ->select('id','title','code')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ $product_map = [];
|
|
|
|
+ foreach ($product as $value){
|
|
|
|
+ $product_map[$value['id']] = $value['title'] . "(" . $value['code']. ")";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //退货的差异
|
|
|
|
+ $returnExchange_map = [];
|
|
|
|
+ $returnExchange = ReturnExchangeOrder::where('del_time',0)
|
|
|
|
+ ->where('type',ReturnExchangeOrder::Order_type)
|
|
|
|
+ ->whereIn('data_id',array_unique($sale_order_id))
|
|
|
|
+ ->select('id')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
|
|
|
|
+ ->whereIn('return_exchange_id', array_column($returnExchange,'id'))
|
|
|
|
+ ->select('product_id','number','return_exchange_price')
|
|
|
|
+ ->get()->toArray();
|
|
|
|
+ foreach ($return_product as $value){
|
|
|
|
+ $tmp = bcmul($value['number'],$value['return_exchange_price'],2);
|
|
|
|
+ if(isset($returnExchange_map[$value['product_id']])){
|
|
|
|
+ $total = bcadd($returnExchange_map[$value['product_id']], $tmp,2);
|
|
|
|
+ $returnExchange_map[$value['product_id']] = $total;
|
|
|
|
+ }else{
|
|
|
|
+ $returnExchange_map[$value['product_id']] = $tmp;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach ($sales_product as $value){
|
|
|
|
+ $product_tmp = $product_map[$value['product_id']] ?? "";
|
|
|
|
+ if(! $product_tmp) continue;
|
|
|
|
+ if($value['price'] > 0){
|
|
|
|
+ $tmp = bcmul($value['price'], $value['number'],2);
|
|
|
|
+ }else{
|
|
|
|
+ $tmp = bcmul($value['retail_price'], $value['number'],2);
|
|
|
|
+ }
|
|
|
|
+ $return_tmp = 0;
|
|
|
|
+ if(isset($returnExchange_map[$value['product_id']])){
|
|
|
|
+ $return_tmp = $returnExchange_map[$value['product_id']] ?? 0;
|
|
|
|
+ unset($returnExchange_map[$value['product_id']]);
|
|
|
|
+ }
|
|
|
|
+ $tmp = bcsub($tmp,$return_tmp,2);
|
|
|
|
+ if(isset($statistics[$value['product_id']])){
|
|
|
|
+ $total = bcadd($tmp,$statistics[$value['product_id']]['total'],2);
|
|
|
|
+ $statistics[$value['product_id']]['total'] = $total;
|
|
|
|
+ }else{
|
|
|
|
+ $statistics[$value['product_id']] = [
|
|
|
|
+ 'title' => $product_tmp,
|
|
|
|
+ 'total' => $tmp,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach ($statistics as $key => $value){
|
|
|
|
+ if(floatval($value['total']) <= 0) unset($statistics[$key]);
|
|
|
|
+ }
|
|
|
|
+ $statistics = array_values($statistics);
|
|
|
|
+ usort($statistics, function($a, $b) {
|
|
|
|
+ return $b['total'] - $a['total'];
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return [true, $statistics];
|
|
|
|
+ }
|
|
|
|
+
|
|
public function statisticsJc($data,$user){
|
|
public function statisticsJc($data,$user){
|
|
$model = Product::ProductClear2($user,$data);
|
|
$model = Product::ProductClear2($user,$data);
|
|
$model = $model->where('del_time',0)
|
|
$model = $model->where('del_time',0)
|