|
@@ -3,6 +3,7 @@
|
|
|
namespace App\Service;
|
|
|
|
|
|
use App\Model\Depart;
|
|
|
+use App\Model\DepartIndex;
|
|
|
use App\Model\InOutRecord;
|
|
|
use App\Model\InvoiceOrder;
|
|
|
use App\Model\InvoiceOrderInfo;
|
|
@@ -49,53 +50,180 @@ class StatisticsService extends Service
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
- //大区 订货统计
|
|
|
+ //省 订单类型细分统计
|
|
|
+ public function statisticsProvince($data,$user){
|
|
|
+ if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
|
+ $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
+
|
|
|
+ //销售订单类型
|
|
|
+ $model_type = 0;
|
|
|
+ if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
|
+
|
|
|
+ //门店设置的指标 按照区域汇总
|
|
|
+ $index_map = DepartIndex::where('del_time',0)
|
|
|
+ ->pluck('param_one','top_depart_id')
|
|
|
+ ->toArray();
|
|
|
+ //分社所属省
|
|
|
+ $depart_map = $province_map = [];
|
|
|
+ $depart = Depart::where('parent_id',0)
|
|
|
+ ->where('province','<>','')
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->select('province','id')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($depart as $value){
|
|
|
+ $depart_map[$value['id']] = $value['province'];
|
|
|
+ $province_map[$value['province']][] = $value['id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $address_map = config('address');
|
|
|
+ $address_map = array_column($address_map,'label','value');
|
|
|
+
|
|
|
+ //省
|
|
|
+ $final_address_map = [];
|
|
|
+ foreach ($address_map as $key => $value){
|
|
|
+ $tmp = [
|
|
|
+ 'key' => $key,
|
|
|
+ 'title' => $value,
|
|
|
+ 'total' => 0,
|
|
|
+ 'index' => 0
|
|
|
+ ];
|
|
|
+ $top_depart_id = $province_map[$key] ?? [];
|
|
|
+ if(! empty($top_depart_id)){
|
|
|
+ foreach ($top_depart_id as $depart_id){
|
|
|
+ $index = $index_map[$depart_id] ?? 0;
|
|
|
+ $tmp['index'] = bcadd($tmp['index'], $index,2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $final_address_map[] = $tmp;
|
|
|
+ }
|
|
|
+
|
|
|
+ $model = SalesOrder::Clear($user,$data);
|
|
|
+ $sale_order = $model->where("del_time",0)
|
|
|
+ ->where('crt_time','>=',$return[0])
|
|
|
+ ->where('crt_time','<=',$return[1])
|
|
|
+ ->when(! empty($model_type), function ($query) use ($model_type) {
|
|
|
+ return $query->where('model_type',$model_type);
|
|
|
+ })
|
|
|
+ ->select('id','top_depart_id','contract_fee')
|
|
|
+ ->get()->toArray();
|
|
|
+
|
|
|
+ //合同
|
|
|
+ $purchase_map = [];
|
|
|
+ foreach ($sale_order as $value){
|
|
|
+ $area = $depart_map[$value['top_depart_id']] ?? 0;
|
|
|
+ if(! $area) continue;
|
|
|
+ if(isset($purchase_map[$area])){
|
|
|
+ $total = bcadd($purchase_map[$area], $value['contract_fee'],2);
|
|
|
+ $purchase_map[$area] = $total;
|
|
|
+ }else{
|
|
|
+ $purchase_map[$area] = $value['contract_fee'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //退货的差异
|
|
|
+ $returnExchange_map = [];
|
|
|
+ $returnExchange = ReturnExchangeOrder::where('del_time',0)
|
|
|
+ ->where('type',ReturnExchangeOrder::Order_type)
|
|
|
+ ->whereIn('data_id',array_column($sale_order,'id'))
|
|
|
+ ->select('top_depart_id','difference_amount')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($returnExchange as $value){
|
|
|
+ $area = $depart_map[$value['top_depart_id']] ?? 0;
|
|
|
+ if(! $area) continue;
|
|
|
+ if(isset($returnExchange_map[$area])){
|
|
|
+ $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
|
|
|
+ $returnExchange_map[$area] = $total;
|
|
|
+ }else{
|
|
|
+ $returnExchange_map[$area] = $value['difference_amount'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($final_address_map as $key => $value){
|
|
|
+ $purchase_tmp = $purchase_map[$value['key']] ?? 0;
|
|
|
+ $return_tmp = $returnExchange_map[$value['key']] ?? 0;
|
|
|
+ $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
|
|
|
+ }
|
|
|
+
|
|
|
+ usort($final_address_map, function($a, $b) {
|
|
|
+ return $b['total'] - $a['total'];
|
|
|
+ });
|
|
|
+
|
|
|
+ return [true, $final_address_map];
|
|
|
+ }
|
|
|
+
|
|
|
+ //大区 订单类型细分统计
|
|
|
public function statisticsArea($data,$user){
|
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
|
$return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
|
|
|
+ //销售订单类型
|
|
|
+ $model_type = 0;
|
|
|
+ if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
|
+
|
|
|
+ //门店设置的指标 按照区域汇总
|
|
|
+ $index_map = DepartIndex::where('del_time',0)
|
|
|
+ ->pluck('param_one','top_depart_id')
|
|
|
+ ->toArray();
|
|
|
+ //分社所属大区
|
|
|
+ $depart_map = $area_map = [];
|
|
|
+ $depart = Depart::where('parent_id',0)
|
|
|
+ ->where('area','>',0)
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->select('area','id')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($depart as $value){
|
|
|
+ $depart_map[$value['id']] = $value['area'];
|
|
|
+ $area_map[$value['area']][] = $value['id'];
|
|
|
+ }
|
|
|
+
|
|
|
//大区
|
|
|
$area = Depart::$area;
|
|
|
$area_map = [];
|
|
|
foreach ($area as $key => $value){
|
|
|
- $area_map[] = [
|
|
|
+ $tmp = [
|
|
|
'key' => $key,
|
|
|
'title' => $value,
|
|
|
- 'total' => 0
|
|
|
+ 'total' => 0,
|
|
|
+ 'index' => 0
|
|
|
];
|
|
|
+ $top_depart_id = $area_map[$key] ?? [];
|
|
|
+ if(! empty($top_depart_id)){
|
|
|
+ foreach ($top_depart_id as $depart_id){
|
|
|
+ $index = $index_map[$depart_id] ?? 0;
|
|
|
+ $tmp['index'] = bcadd($tmp['index'], $index,2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $area_map[] = $tmp;
|
|
|
}
|
|
|
- //分社所属大区
|
|
|
- $depart_map = Depart::where('parent_id',0)
|
|
|
- ->where('del_time',0)
|
|
|
- ->pluck('area','id')
|
|
|
- ->toArray();
|
|
|
|
|
|
- //向总社采购的钱
|
|
|
- $purchase_map = [];
|
|
|
- $purchase = PurchaseOrder::where('del_time',0)
|
|
|
- ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
|
|
|
+ //合同
|
|
|
+ $model = SalesOrder::Clear($user,$data);
|
|
|
+ $sale_order = $model->where("del_time",0)
|
|
|
->where('crt_time','>=',$return[0])
|
|
|
->where('crt_time','<=',$return[1])
|
|
|
- ->select('top_depart_id','purchase_total')
|
|
|
+ ->when(! empty($model_type), function ($query) use ($model_type) {
|
|
|
+ return $query->where('model_type',$model_type);
|
|
|
+ })
|
|
|
+ ->select('top_depart_id','contract_fee')
|
|
|
->get()->toArray();
|
|
|
- foreach ($purchase as $value){
|
|
|
+
|
|
|
+ $purchase_map = [];
|
|
|
+ foreach ($sale_order as $value){
|
|
|
$area = $depart_map[$value['top_depart_id']] ?? 0;
|
|
|
if(! $area) continue;
|
|
|
if(isset($purchase_map[$area])){
|
|
|
- $total = bcadd($purchase_map[$area], $value['purchase_total'],2);
|
|
|
+ $total = bcadd($purchase_map[$area], $value['contract_fee'],2);
|
|
|
$purchase_map[$area] = $total;
|
|
|
}else{
|
|
|
- $purchase_map[$area] = $value['purchase_total'];
|
|
|
+ $purchase_map[$area] = $value['contract_fee'];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//退货的差异
|
|
|
$returnExchange_map = [];
|
|
|
$returnExchange = ReturnExchangeOrder::where('del_time',0)
|
|
|
- ->where('type',ReturnExchangeOrder::Order_type2)
|
|
|
- ->where('crt_time','>=',$return[0])
|
|
|
- ->where('crt_time','<=',$return[1])
|
|
|
- ->where('crt_time','<=',$return[1])
|
|
|
+ ->where('type',ReturnExchangeOrder::Order_type)
|
|
|
+ ->whereIn('data_id',array_column($sale_order,'id'))
|
|
|
->select('top_depart_id','difference_amount')
|
|
|
->get()->toArray();
|
|
|
foreach ($returnExchange as $value){
|
|
@@ -115,38 +243,70 @@ class StatisticsService extends Service
|
|
|
$area_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
|
|
|
}
|
|
|
|
|
|
+ usort($area_map, function($a, $b) {
|
|
|
+ return $b['total'] - $a['total'];
|
|
|
+ });
|
|
|
+
|
|
|
return [true, $area_map];
|
|
|
}
|
|
|
|
|
|
- // 大区下的门店 订货统计
|
|
|
+ // 省|大区下的门店 订货统计
|
|
|
public function statisticsAreaDepart($data,$user){
|
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
|
$return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
- if(empty($data['area']) || ! isset(Depart::$area[$data['area']])) return [false, '该大区不存在'];
|
|
|
|
|
|
- $depart = Depart::where('parent_id',0)
|
|
|
- ->where('del_time',0)
|
|
|
- ->where('area',$data['area'])
|
|
|
- ->select('id','title')
|
|
|
- ->get()
|
|
|
- ->toArray();
|
|
|
- if(empty($depart)) return [false, '该大区下不存在门店'];
|
|
|
+ if(empty($data['area']) && empty($data['province'])) return [false, '大区或省份必须选择一个'];
|
|
|
+ if(! empty($data['area']) && ! empty($data['province']))return [false, '大区或省份有且只能有一个'];
|
|
|
+ if(! empty($data['area']) && ! isset(Depart::$area[$data['area']])) return [false, '该大区不存在'];
|
|
|
+ $address_map = config('address');
|
|
|
+ $address_map = array_column($address_map,'label','value');
|
|
|
+ if(! empty($data['province']) && ! isset($address_map[$data['province']])) return [false, '该省不存在'];
|
|
|
+
|
|
|
+ //销售订单类型
|
|
|
+ $model_type = 0;
|
|
|
+ if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
|
+
|
|
|
+ if(! empty($data['area'])){
|
|
|
+ $depart = Depart::where('parent_id',0)
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->where('area',$data['area'])
|
|
|
+ ->select('id','title')
|
|
|
+ ->get()
|
|
|
+ ->toArray();
|
|
|
+ if(empty($depart)) return [false, '该大区下不存在门店'];
|
|
|
+ }else{
|
|
|
+ $depart = Depart::where('parent_id',0)
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->where('province',$data['province'])
|
|
|
+ ->select('id','title')
|
|
|
+ ->get()
|
|
|
+ ->toArray();
|
|
|
+ if(empty($depart)) return [false, '该省下不存在门店'];
|
|
|
+ }
|
|
|
|
|
|
- //向总社采购的钱
|
|
|
- $purchase_map = [];
|
|
|
- $purchase = PurchaseOrder::where('del_time',0)
|
|
|
+ //门店设置的指标
|
|
|
+ $index_map = DepartIndex::where('del_time',0)
|
|
|
->whereIn('top_depart_id',array_column($depart,'id'))
|
|
|
- ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
|
|
|
+ ->pluck('param_one','top_depart_id')
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ //合同
|
|
|
+ $sale_order = SalesOrder::where("del_time",0)
|
|
|
->where('crt_time','>=',$return[0])
|
|
|
->where('crt_time','<=',$return[1])
|
|
|
- ->select('top_depart_id','purchase_total')
|
|
|
+ ->when(! empty($model_type), function ($query) use ($model_type) {
|
|
|
+ return $query->where('model_type',$model_type);
|
|
|
+ })
|
|
|
+ ->select('top_depart_id','contract_fee')
|
|
|
->get()->toArray();
|
|
|
- foreach ($purchase as $value){
|
|
|
+ //向总社采购的钱
|
|
|
+ $purchase_map = [];
|
|
|
+ foreach ($sale_order as $value){
|
|
|
if(isset($purchase_map[$value['top_depart_id']])){
|
|
|
- $total = bcadd($purchase_map[$value['top_depart_id']], $value['purchase_total'],2);
|
|
|
+ $total = bcadd($purchase_map[$value['top_depart_id']], $value['contract_fee'],2);
|
|
|
$purchase_map[$value['top_depart_id']] = $total;
|
|
|
}else{
|
|
|
- $purchase_map[$value['top_depart_id']] = $value['purchase_total'];
|
|
|
+ $purchase_map[$value['top_depart_id']] = $value['contract_fee'];
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -154,10 +314,8 @@ class StatisticsService extends Service
|
|
|
$returnExchange_map = [];
|
|
|
$returnExchange = ReturnExchangeOrder::where('del_time',0)
|
|
|
->whereIn('top_depart_id',array_column($depart,'id'))
|
|
|
- ->where('type',ReturnExchangeOrder::Order_type2)
|
|
|
- ->where('crt_time','>=',$return[0])
|
|
|
- ->where('crt_time','<=',$return[1])
|
|
|
- ->where('crt_time','<=',$return[1])
|
|
|
+ ->where('type',ReturnExchangeOrder::Order_type)
|
|
|
+ ->whereIn('data_id',array_column($sale_order,'id'))
|
|
|
->select('top_depart_id','difference_amount')
|
|
|
->get()->toArray();
|
|
|
foreach ($returnExchange as $value){
|
|
@@ -173,39 +331,48 @@ class StatisticsService extends Service
|
|
|
$purchase_tmp = $purchase_map[$value['id']] ?? 0;
|
|
|
$return_tmp = $returnExchange_map[$value['id']] ?? 0;
|
|
|
$depart[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
|
|
|
+ $depart[$key]['index'] = $index_map[$value['id']] ?? 0;
|
|
|
}
|
|
|
|
|
|
return [true, $depart];
|
|
|
}
|
|
|
|
|
|
- // 大区下的门店 某个门店 关于产品以及分类的统计 订货统计
|
|
|
+ //某个门店 关于产品以及分类的统计 订货统计
|
|
|
public function statisticsAreaDepartProduct($data,$user){
|
|
|
if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
|
$return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
if(empty($data['top_depart_id'])) return [false, '请选择门店'];
|
|
|
|
|
|
+ //销售订单类型
|
|
|
+ $model_type = 0;
|
|
|
+ if(! empty($data['model_type'])) $model_type = $data['model_type'];
|
|
|
+
|
|
|
//时间 =》 钱
|
|
|
$purchase_map1 = $this->getTime($return);
|
|
|
//产品 =》 数量
|
|
|
$purchase_category_map = $purchase_map = [];
|
|
|
- $purchase = PurchaseOrder::where('del_time',0)
|
|
|
+
|
|
|
+ $sale_order = SalesOrder::where("del_time",0)
|
|
|
->where('top_depart_id',$data['top_depart_id'])
|
|
|
- ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
|
|
|
->where('crt_time','>=',$return[0])
|
|
|
->where('crt_time','<=',$return[1])
|
|
|
+ ->when(! empty($model_type), function ($query) use ($model_type) {
|
|
|
+ return $query->where('model_type',$model_type);
|
|
|
+ })
|
|
|
->select('id','crt_time')
|
|
|
->get()->toArray();
|
|
|
- $purchase_for_time = array_column($purchase,'crt_time','id');
|
|
|
- $purchase_product = PurchaseOrderInfo::where("del_time",0)
|
|
|
- ->whereIn('purchase_order_id',array_column($purchase,'id'))
|
|
|
- ->select('purchase_order_id','product_id','number','price','final_amount','sports_bag_id')
|
|
|
+ $purchase_for_time = array_column($sale_order,'crt_time','id');
|
|
|
+ $sale_order_id = array_column($sale_order,'id');
|
|
|
+ $purchase_product = SalesOrderProductInfo::where("del_time",0)
|
|
|
+ ->whereIn('sales_order_id',$sale_order_id)
|
|
|
+ ->select('sales_order_id','product_id','number','price','final_amount','sports_bag_id')
|
|
|
->get()->toArray();
|
|
|
|
|
|
//退换货
|
|
|
- $returnExchange_map = $this->returnExchange($data,$return);
|
|
|
+ $returnExchange_map = $this->returnExchange($data,$sale_order_id);
|
|
|
//产品
|
|
|
$product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys($returnExchange_map,'product_id'))))
|
|
|
- ->select('id','product_category','title')
|
|
|
+ ->select('id','product_category','title','code')
|
|
|
->get()->toArray();
|
|
|
$product_list_map = array_column($product_list,null,'id');
|
|
|
|
|
@@ -221,7 +388,7 @@ class StatisticsService extends Service
|
|
|
}else{
|
|
|
$money = bcmul($value['price'],$value['number'],2);
|
|
|
}
|
|
|
- $crt_time = $purchase_for_time[$value['purchase_order_id']];
|
|
|
+ $crt_time = $purchase_for_time[$value['sales_order_id']];
|
|
|
$crt_time = date("Y-m-d",$crt_time);
|
|
|
|
|
|
//产品信息
|
|
@@ -236,11 +403,14 @@ class StatisticsService extends Service
|
|
|
//数量
|
|
|
if(isset($purchase_map[$value['product_id']])){
|
|
|
$tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
|
|
|
+ $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
|
|
|
$purchase_map[$value['product_id']]['number'] = $tmp_number;
|
|
|
+ $purchase_map[$value['product_id']]['total'] = $tmp_money;
|
|
|
}else{
|
|
|
$purchase_map[$value['product_id']] = [
|
|
|
- 'title' => $product_tmp['title'],
|
|
|
- 'number' => $value['number']
|
|
|
+ 'title' => $product_tmp['title'] . "(" . $product_tmp['code'] .")",
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'total' => $money
|
|
|
];
|
|
|
}
|
|
|
//-------根据产品
|
|
@@ -248,15 +418,18 @@ class StatisticsService extends Service
|
|
|
//-------根据产品大类
|
|
|
//数量
|
|
|
$category_tmp = json_decode($product_tmp['product_category']);
|
|
|
- $category_tmp = $category_tmp[0];
|
|
|
+ $category_tmp = min($category_tmp);
|
|
|
|
|
|
if(isset($purchase_category_map[$category_tmp])){
|
|
|
$tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
|
|
|
+ $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
|
|
|
$purchase_category_map[$category_tmp]['number'] = $tmp_number;
|
|
|
+ $purchase_category_map[$category_tmp]['total'] = $tmp_money;
|
|
|
}else{
|
|
|
$purchase_category_map[$category_tmp] = [
|
|
|
'number' => $value['number'],
|
|
|
- 'title' => $category_return[$category_tmp] ?? ""
|
|
|
+ 'title' => $category_return[$category_tmp] ?? "",
|
|
|
+ 'total' => $money
|
|
|
];
|
|
|
}
|
|
|
}
|
|
@@ -291,18 +464,17 @@ class StatisticsService extends Service
|
|
|
return $return;
|
|
|
}
|
|
|
|
|
|
- public function returnExchange($data,$return){ return [];
|
|
|
+ public function returnExchange($data,$sale_order_id){return [];
|
|
|
$returnExchange_map = [];
|
|
|
$returnExchange = ReturnExchangeOrder::where('del_time',0)
|
|
|
->where('top_depart_id',$data['top_depart_id'])
|
|
|
- ->where('type',ReturnExchangeOrder::Order_type2)
|
|
|
- ->where('crt_time','>=',$return[0])
|
|
|
- ->where('crt_time','<=',$return[1])
|
|
|
- ->select('id')
|
|
|
+ ->where('type',ReturnExchangeOrder::Order_type)
|
|
|
+ ->whereIn('data_id',$sale_order_id)
|
|
|
+ ->select('id','difference_amount')
|
|
|
->get()->toArray();
|
|
|
$returnExchange_product = ReturnExchangeOrderProductInfo::where("del_time",0)
|
|
|
->whereIn('return_exchange_id',array_column($returnExchange,'id'))
|
|
|
- ->select('product_id','number','return_exchange_price as price')
|
|
|
+ ->select('return_exchange_id','product_id','number')
|
|
|
->get()->toArray();
|
|
|
foreach ($returnExchange_product as $value){
|
|
|
$money = bcmul($value['price'],$value['number'],2);
|
|
@@ -326,7 +498,6 @@ class StatisticsService extends Service
|
|
|
}
|
|
|
|
|
|
public function statisticsJc($data,$user){
|
|
|
- if(empty($data['top_depart_id'])) return [false, '请选择门店'];
|
|
|
$model = Product::ProductClear2($user,$data);
|
|
|
$model = $model->where('del_time',0)
|
|
|
->select('title','id','code','depart_id','top_depart_id','product_attribute')
|
|
@@ -730,10 +901,10 @@ class StatisticsService extends Service
|
|
|
}
|
|
|
|
|
|
//退货的差异
|
|
|
- $returnExchange_map = $this->returnExchange($data,$return);
|
|
|
+// $returnExchange_map = $this->returnExchange($data,$return);
|
|
|
|
|
|
//产品
|
|
|
- $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys($returnExchange_map,'product_id'))))
|
|
|
+ $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys([],'product_id'))))
|
|
|
->select('id','product_category','title')
|
|
|
->get()->toArray();
|
|
|
$product_list_map = array_column($product_list,null,'id');
|
|
@@ -786,4 +957,141 @@ class StatisticsService extends Service
|
|
|
|
|
|
return [true, ''];
|
|
|
}
|
|
|
+
|
|
|
+ public function statisticsProvince2222($data,$user){
|
|
|
+ if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
|
|
|
+ $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
+
|
|
|
+ //门店设置的指标 按照区域汇总
|
|
|
+ $index_map = DepartIndex::where('del_time',0)
|
|
|
+ ->pluck('param_one','top_depart_id')
|
|
|
+ ->toArray();
|
|
|
+ //分社所属省
|
|
|
+ $depart_map = $province_map = [];
|
|
|
+ $depart = Depart::where('parent_id',0)
|
|
|
+ ->where('province','<>','')
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->select('province','id')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($depart as $value){
|
|
|
+ $depart_map[$value['id']] = $value['province'];
|
|
|
+ $province_map[$value['province']][] = $value['id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $address_map = config('address');
|
|
|
+ $address_map = array_column($address_map,'label','value');
|
|
|
+
|
|
|
+ //大区
|
|
|
+ $final_address_map = [];
|
|
|
+ foreach ($address_map as $key => $value){
|
|
|
+ $tmp = [
|
|
|
+ 'key' => $key,
|
|
|
+ 'title' => $value,
|
|
|
+ 'total' => 0,
|
|
|
+ 'index' => 0
|
|
|
+ ];
|
|
|
+ $top_depart_id = $province_map[$key] ?? [];
|
|
|
+ if(! empty($top_depart_id)){
|
|
|
+ foreach ($top_depart_id as $depart_id){
|
|
|
+ $index = $index_map[$depart_id] ?? 0;
|
|
|
+ $tmp['index'] = bcadd($tmp['index'], $index,2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $final_address_map[] = $tmp;
|
|
|
+ }
|
|
|
+
|
|
|
+ //向总社采购的钱
|
|
|
+ $purchase_map = [];
|
|
|
+ $purchase = PurchaseOrder::where('del_time',0)
|
|
|
+ ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
|
|
|
+ ->where('crt_time','>=',$return[0])
|
|
|
+ ->where('crt_time','<=',$return[1])
|
|
|
+ ->select('top_depart_id','purchase_total')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($purchase as $value){
|
|
|
+ $area = $depart_map[$value['top_depart_id']] ?? 0;
|
|
|
+ if(! $area) continue;
|
|
|
+ if(isset($purchase_map[$area])){
|
|
|
+ $total = bcadd($purchase_map[$area], $value['purchase_total'],2);
|
|
|
+ $purchase_map[$area] = $total;
|
|
|
+ }else{
|
|
|
+ $purchase_map[$area] = $value['purchase_total'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //退货的差异
|
|
|
+ $returnExchange_map = [];
|
|
|
+ $returnExchange = ReturnExchangeOrder::where('del_time',0)
|
|
|
+ ->where('type',ReturnExchangeOrder::Order_type2)
|
|
|
+ ->where('crt_time','>=',$return[0])
|
|
|
+ ->where('crt_time','<=',$return[1])
|
|
|
+ ->where('crt_time','<=',$return[1])
|
|
|
+ ->select('top_depart_id','difference_amount')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($returnExchange as $value){
|
|
|
+ $area = $depart_map[$value['top_depart_id']] ?? 0;
|
|
|
+ if(! $area) continue;
|
|
|
+ if(isset($returnExchange_map[$area])){
|
|
|
+ $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
|
|
|
+ $returnExchange_map[$area] = $total;
|
|
|
+ }else{
|
|
|
+ $returnExchange_map[$area] = $value['difference_amount'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($final_address_map as $key => $value){
|
|
|
+ $purchase_tmp = $purchase_map[$value['key']] ?? 0;
|
|
|
+ $return_tmp = $returnExchange_map[$value['key']] ?? 0;
|
|
|
+ $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, $final_address_map];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fillStatisticsBt1($data){
|
|
|
+ if(empty($data)) return $data;
|
|
|
+
|
|
|
+ $total = floatval(array_sum(array_column($data,'total')));
|
|
|
+
|
|
|
+ // 设置一个最小显示阈值,比如0.5%
|
|
|
+ $threshold = 0.5;
|
|
|
+
|
|
|
+ // 用于存储调整后的数据
|
|
|
+ $adjustedData = [];
|
|
|
+ $otherTotal = 0;
|
|
|
+ $other = [];
|
|
|
+
|
|
|
+ foreach ($data as $value) {
|
|
|
+ $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
|
|
|
+ $rate = $total ? $value['total'] / $total : 0;
|
|
|
+ // 检查是否低于阈值
|
|
|
+ if ($rate * 100 < $threshold) {
|
|
|
+ // 小于阈值的部分加到"其他"中
|
|
|
+ $otherTotal += $value['total'];
|
|
|
+ $other[] = $model_type_title;
|
|
|
+ } else {
|
|
|
+ // 保留更多小数位数
|
|
|
+ $adjustedData[] = [
|
|
|
+ 'model_type' => $value['model_type'],
|
|
|
+ 'total' => $value['total'],
|
|
|
+ 'model_type_title' => $model_type_title,
|
|
|
+ 'rate' => round($rate * 100, 2)
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有"其他"值,则添加到数据集中
|
|
|
+ if ($otherTotal > 0) {
|
|
|
+ $other_title = implode(',',$other);
|
|
|
+ $adjustedData[] = [
|
|
|
+ 'model_type' => -1, // 自定义一个类型标识
|
|
|
+ 'total' => $otherTotal,
|
|
|
+ 'model_type_title' => '其他(' . $other_title . ')',
|
|
|
+ 'rate' => round($otherTotal / $total * 100, 2)
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return $adjustedData;
|
|
|
+ }
|
|
|
}
|