|  | @@ -2,6 +2,7 @@
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  namespace app\admin\controller\finance;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +use app\admin\service\goods\GoodsService;
 | 
	
		
			
				|  |  |  use app\admin\service\goods\GoodsSkuService;
 | 
	
		
			
				|  |  |  use app\model\DataInout;
 | 
	
		
			
				|  |  |  use app\model\Goods;
 | 
	
	
		
			
				|  | @@ -85,6 +86,7 @@ class GoodsSalesController
 | 
	
		
			
				|  |  |              'combine' => ['total' => 0, 'amount' => 0],
 | 
	
		
			
				|  |  |              'partner' => ['total' => 0, 'amount' => 0],
 | 
	
		
			
				|  |  |              'referrer' => ['total' => 0, 'amount' => 0],
 | 
	
		
			
				|  |  | +            'franchisee' => ['total' => 0, 'amount' => 0],
 | 
	
		
			
				|  |  |          ];
 | 
	
		
			
				|  |  |          foreach ($dataInOuts as $dataInOut) {
 | 
	
		
			
				|  |  |              if (empty($dataInOut['join_data_inout_object_json'])) {
 | 
	
	
		
			
				|  | @@ -237,4 +239,146 @@ class GoodsSalesController
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          return json_success('success', $goods);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public function exportGoodsSales(Request $request)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $days = $request->get('days', [date('Y-m-01 00:00:00'), date('Y-m-t 23:59:59')]);
 | 
	
		
			
				|  |  | +        $classify = $request->get('classify', '');
 | 
	
		
			
				|  |  | +        $categoryId = $request->get('category_id', '');
 | 
	
		
			
				|  |  | +        $goodsName = $request->get('goods_name', '');
 | 
	
		
			
				|  |  | +        $payType = $request->get('pay_type', '');
 | 
	
		
			
				|  |  | +        $paramsGoodsIds = $request->get('goods_ids');
 | 
	
		
			
				|  |  | +        if (!empty($days)) {
 | 
	
		
			
				|  |  | +            $days[0] = strtotime($days[0]);
 | 
	
		
			
				|  |  | +            $days[1] = strtotime($days[1]);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        $searchGoodsIds = [];
 | 
	
		
			
				|  |  | +        if (!empty($classify)) {
 | 
	
		
			
				|  |  | +            if ($classify == 'SERVICE') {
 | 
	
		
			
				|  |  | +                $classify = ['SERVICE', 'CHNMED', 'CHNNCD'];
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                $classify = [$classify];
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $searchGoodsIds = Goods::whereIn('goods_classify', $classify)->pluck('goods_id')->toArray();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (!empty($categoryId)) {
 | 
	
		
			
				|  |  | +            $categorySuperPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
 | 
	
		
			
				|  |  | +            if (empty($categorySuperPath)) {
 | 
	
		
			
				|  |  | +                $categorySuperPath = '#' . $categoryId . '#';
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $searchCategoryIds = SysCategory::where('category_super_path', 'like', $categorySuperPath . '%')->pluck('category_id')->toArray();
 | 
	
		
			
				|  |  | +            $searchCategoryIds[] = $categoryId;
 | 
	
		
			
				|  |  | +            // 按产品类型筛出来的ID
 | 
	
		
			
				|  |  | +            $oldSearchGoodsIds = $searchGoodsIds;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            $searchGoodsIds = Goods::whereIn('join_goods_category_id', $searchCategoryIds)->pluck('goods_id')->toArray();
 | 
	
		
			
				|  |  | +            if (!empty($oldSearchGoodsIds)) {
 | 
	
		
			
				|  |  | +                $searchGoodsIds = array_intersect($searchGoodsIds, $oldSearchGoodsIds);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (!empty($goodsName)) {
 | 
	
		
			
				|  |  | +            // 按产品类型和分类筛出来的ID
 | 
	
		
			
				|  |  | +            $oldSearchGoodsIds = $searchGoodsIds;
 | 
	
		
			
				|  |  | +            $searchGoodsIds = Goods::where('goods_name', 'like', '%' . $goodsName . '%')->pluck("goods_id")->toArray();
 | 
	
		
			
				|  |  | +            if (!empty($oldSearchGoodsIds)) {
 | 
	
		
			
				|  |  | +                $searchGoodsIds = array_intersect($searchGoodsIds, $oldSearchGoodsIds);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $dataInOuts = DataInout::where('data_inout_status', 'VALID')
 | 
	
		
			
				|  |  | +            ->where('data_inout_classify', 'IN')
 | 
	
		
			
				|  |  | +            ->when(!empty($days), function ($query) use ($days) {
 | 
	
		
			
				|  |  | +                $query->whereBetween('data_inout_addtimes', $days);
 | 
	
		
			
				|  |  | +            })->when(!empty($payType), function ($query) use ($payType) {
 | 
	
		
			
				|  |  | +                $query->where('data_inout_pay_type', $payType);
 | 
	
		
			
				|  |  | +            })->select('join_data_inout_object_json')
 | 
	
		
			
				|  |  | +            ->get()
 | 
	
		
			
				|  |  | +            ->toArray();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        $goods = [];
 | 
	
		
			
				|  |  | +        $goodsIds = [];
 | 
	
		
			
				|  |  | +        foreach ($dataInOuts as $dataInOut) {
 | 
	
		
			
				|  |  | +            if (empty($dataInOut['join_data_inout_object_json'])) {
 | 
	
		
			
				|  |  | +                continue;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $intoObjectJson = json_decode($dataInOut['join_data_inout_object_json'], true);
 | 
	
		
			
				|  |  | +            if (empty($intoObjectJson['order'])) {
 | 
	
		
			
				|  |  | +                continue;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            foreach ($intoObjectJson['order'] as $order) {
 | 
	
		
			
				|  |  | +                if (empty($order['goods'])) {
 | 
	
		
			
				|  |  | +                    continue;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                foreach ($order['goods'] as $good) {
 | 
	
		
			
				|  |  | +                    if ((!empty($classify) || !empty($categoryId) || !empty($goodsName)) && !in_array($good['goods_id'], $searchGoodsIds)) {
 | 
	
		
			
				|  |  | +                        continue;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    $goodKey = $good['goods_id'];
 | 
	
		
			
				|  |  | +                    if (!empty($good['goods_sku_id'])) {
 | 
	
		
			
				|  |  | +                        $goodKey .= '_' . $good['goods_sku_id'];
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    if (!key_exists($goodKey, $goods)) {
 | 
	
		
			
				|  |  | +                        $goodsIds[] = $good['goods_id'];
 | 
	
		
			
				|  |  | +                        $goods[$goodKey] = [
 | 
	
		
			
				|  |  | +                            'goods_id' => $good['goods_id'],
 | 
	
		
			
				|  |  | +                            'goods_name' => $good['goods_name'],
 | 
	
		
			
				|  |  | +                            'goods_sku_id' => $good['goods_sku_id'] ?? '',
 | 
	
		
			
				|  |  | +                            'order_sheet_num' => floatval($good['order_sheet_num']),
 | 
	
		
			
				|  |  | +                            'order_sheet_pay' => floatval($good['order_sheet_pay'])
 | 
	
		
			
				|  |  | +                        ];
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        $goods[$goodKey]['order_sheet_num'] += $good['order_sheet_num'];
 | 
	
		
			
				|  |  | +                        $goods[$goodKey]['order_sheet_pay'] += $good['order_sheet_pay'];
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 交集
 | 
	
		
			
				|  |  | +        if (!empty($classify) || !empty($categoryId) || !empty($paramsGoodsIds)) {
 | 
	
		
			
				|  |  | +            $goodsIds = array_intersect($goodsIds, $searchGoodsIds);
 | 
	
		
			
				|  |  | +            if (!empty($paramsGoodsIds) && !empty($goodsIds)){
 | 
	
		
			
				|  |  | +                $goodsIds = array_intersect($goodsIds,$paramsGoodsIds);
 | 
	
		
			
				|  |  | +            }else if (!empty($paramsGoodsIds) && empty($goodsIds)){
 | 
	
		
			
				|  |  | +                $goodsIds = $paramsGoodsIds;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            $goods = array_filter($goods, function ($item) use ($goodsIds) {
 | 
	
		
			
				|  |  | +                return in_array($item['goods_id'], $goodsIds);
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 排序
 | 
	
		
			
				|  |  | +        usort($goods, function ($a, $b) {
 | 
	
		
			
				|  |  | +            return $b['order_sheet_num'] <=> $a['order_sheet_num'];
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        $dataArray = [];
 | 
	
		
			
				|  |  | +        $data = [];
 | 
	
		
			
				|  |  | +        foreach ($goods as $row) {
 | 
	
		
			
				|  |  | +            $data['goods_name'] = $row['goods_name'];
 | 
	
		
			
				|  |  | +            $data['order_sheet_num'] = $row['order_sheet_num'];
 | 
	
		
			
				|  |  | +            $rowGoods = Goods::with([
 | 
	
		
			
				|  |  | +                'category',
 | 
	
		
			
				|  |  | +                'skuOne' => function ($query) use ($row) {
 | 
	
		
			
				|  |  | +                    $query->where('goods_sku_id', $row['goods_sku_id']);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            ])->where('goods_id', $row['goods_id'])->first();
 | 
	
		
			
				|  |  | +            $data['goods_classify'] = !empty($rowGoods->goods_classify) ? GoodsService::$goodsClassify[$rowGoods->goods_classify] : '--';;
 | 
	
		
			
				|  |  | +            $data['goods_sales_price'] = !empty($rowGoods->goods_sales_price) ? $rowGoods->goods_sales_price : '--';
 | 
	
		
			
				|  |  | +            $data['category'] = !empty($rowGoods->category) ? $rowGoods->category : '--';
 | 
	
		
			
				|  |  | +            if (!empty($rowGoods->skuOne)) {
 | 
	
		
			
				|  |  | +                $data['goods_sku_title'] = GoodsSkuService::getSkuTitle($rowGoods->skuOne->goods_sku_specs_json);
 | 
	
		
			
				|  |  | +                $data['goods_sku_sales_price'] = $rowGoods->skuOne->goods_sku_sales_price;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if(!empty($rowGoods->category)){
 | 
	
		
			
				|  |  | +                $data['goods_category'] = $rowGoods->category->category_name;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            $data['order_sheet_pay'] = sprintf('%.2f', $row['order_sheet_pay']);
 | 
	
		
			
				|  |  | +            $dataArray[] = $data;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return json_success('', $dataArray);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  }
 |