From ae5cc03c1711dcf994314fa32ec2a9ad15d5ac2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=BB=BA=E7=82=8A?= <924182103@qq.com> Date: Wed, 30 Oct 2024 15:15:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=B9=E9=87=8F=E4=BF=AE=E6=94=B9=E5=9C=A8?= =?UTF-8?q?=E5=94=AE=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exports/GoodsSkusExport.php | 59 ++++++++++++------- app/Filters/GoodsSkuFilter.php | 11 ++++ .../Controllers/Goods/GoodsSkusController.php | 1 + app/Models/GoodsSku.php | 4 +- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/app/Exports/GoodsSkusExport.php b/app/Exports/GoodsSkusExport.php index f224fab..2cd448d 100644 --- a/app/Exports/GoodsSkusExport.php +++ b/app/Exports/GoodsSkusExport.php @@ -4,6 +4,7 @@ namespace App\Exports; use App\Http\Enum\TargetTypeEnum; use App\Models\DailyStockRecord; +use App\Models\Goods; use App\Models\Log; use App\Models\PurchaseRecords; use App\Utils\ArrayUtils; @@ -37,8 +38,8 @@ class GoodsSkusExport implements FromCollection, ShouldAutoSize $headTitle = [ '商品编码', '商品名称', - '商品品种', - '规格名称', + '在售库存', + '创建时间', ]; $map = [ 'cost' => ['当前成本', '更新前成本', "更新后成本"], @@ -51,29 +52,47 @@ class GoodsSkusExport implements FromCollection, ShouldAutoSize if ('inventory' === $this->type) { $update = $this->getInventoryRecord(); } - $ids = array_keys($update); - if (empty($ids)) { - return [$headTitle]; - } - $model = GoodsSku::query() - ->when($ids, function ($query, $ids) { - return $query->whereIn('id', $ids); - }) - ->with(['goods' => function ($query) { - $query->with(['type:id,name', 'brand:id,name']) - ->orderBy('type_id') - ->orderBy('brand_id'); - }]); - $data = $model->get()->toArray(); - if (empty($data)) { - return [$headTitle]; + if ($this->type === "goods_sku" || $this->type === "goods_combination") { + $builder = GoodsSku::query(); + if (request()->get('type_id')) { + $goodsIds = Goods::query()->filter()->pluck('id')->toArray(); + $builder->whereIn('goods_id', $goodsIds); + } + if (request()->get('goods_title')) { + $builder->where('name', 'like', '%' . request()->get('goods_title') . '%'); + } + if ($this->type === "goods_combination") { + $builder->where('is_combination', 1); + }else{ + $builder->where('is_combination', 0); + } + $data = $builder->filter()->orderByDesc('id')->get()->toArray(); + } else { + $ids = array_keys($update); + if (empty($ids)) { + return [$headTitle]; + } + $model = GoodsSku::query() + ->when($ids, function ($query, $ids) { + return $query->whereIn('id', $ids); + }) + ->with(['goods' => function ($query) { + $query->with(['type:id,name', 'brand:id,name']) + ->orderBy('type_id') + ->orderBy('brand_id'); + }]); + $data = $model->get()->toArray(); + if (empty($data)) { + return [$headTitle]; + } } + $bodyData = []; foreach ($data as $item) { $arr[0] = $item['external_sku_id']; $arr[1] = $item['name']; - $arr[2] = !empty($item['goods']['type']['name']) ? $item['goods']['type']['name'] : ''; - $arr[3] = $item['title']; + $arr[2] = $item['sale_stock'] ?? 0; + $arr[3] = $item['created_at']; if ('cost' === $this->type) { $arr[4] = (string)$item['cost']; $arr[5] = (string)$update[$item['id']]['before_update']; diff --git a/app/Filters/GoodsSkuFilter.php b/app/Filters/GoodsSkuFilter.php index 57a8751..b55f9e4 100644 --- a/app/Filters/GoodsSkuFilter.php +++ b/app/Filters/GoodsSkuFilter.php @@ -28,4 +28,15 @@ class GoodsSkuFilter extends Filters { return $this->builder->where('is_combination', $value); } + + protected function createTimeStart($value) + { + + return $this->builder->where('created_at',">=", $value); + } + + protected function createTimeEnd($value) + { + return $this->builder->where('created_at',"<=", $value); + } } diff --git a/app/Http/Controllers/Goods/GoodsSkusController.php b/app/Http/Controllers/Goods/GoodsSkusController.php index a8b96ca..9401d3d 100644 --- a/app/Http/Controllers/Goods/GoodsSkusController.php +++ b/app/Http/Controllers/Goods/GoodsSkusController.php @@ -616,6 +616,7 @@ class GoodsSkusController extends Controller $endDate = Carbon::now()->subWeek()->endOfWeek()->toDateString(); return Excel::download(new WeekDataExport($startDate, $endDate), $startDate . '~' . $endDate . '.xlsx'); } + return Excel::download(new GoodsSkusExport($type), $type . '.xlsx'); } diff --git a/app/Models/GoodsSku.php b/app/Models/GoodsSku.php index 5a962df..debf7eb 100644 --- a/app/Models/GoodsSku.php +++ b/app/Models/GoodsSku.php @@ -15,6 +15,8 @@ class GoodsSku extends Model 'exclude_ids', 'external_sku_id', 'is_combination', + "create_time_start", + "create_time_end" ]; protected $fillable = [ @@ -37,8 +39,6 @@ class GoodsSku extends Model 'attribute' ]; - protected $hidden = ['created_at']; - public static $STATUS_ON_SALE = 1; public static $STATUS_DOWN = 0;