批量修改在售库存

This commit is contained in:
杨建炊 2024-10-30 15:15:54 +08:00
parent 44163c5c5c
commit ae5cc03c17
4 changed files with 53 additions and 22 deletions

View File

@ -4,6 +4,7 @@ namespace App\Exports;
use App\Http\Enum\TargetTypeEnum; use App\Http\Enum\TargetTypeEnum;
use App\Models\DailyStockRecord; use App\Models\DailyStockRecord;
use App\Models\Goods;
use App\Models\Log; use App\Models\Log;
use App\Models\PurchaseRecords; use App\Models\PurchaseRecords;
use App\Utils\ArrayUtils; use App\Utils\ArrayUtils;
@ -37,8 +38,8 @@ class GoodsSkusExport implements FromCollection, ShouldAutoSize
$headTitle = [ $headTitle = [
'商品编码', '商品编码',
'商品名称', '商品名称',
'商品品种', '在售库存',
'规格名称', '创建时间',
]; ];
$map = [ $map = [
'cost' => ['当前成本', '更新前成本', "更新后成本"], 'cost' => ['当前成本', '更新前成本', "更新后成本"],
@ -51,29 +52,47 @@ class GoodsSkusExport implements FromCollection, ShouldAutoSize
if ('inventory' === $this->type) { if ('inventory' === $this->type) {
$update = $this->getInventoryRecord(); $update = $this->getInventoryRecord();
} }
$ids = array_keys($update); if ($this->type === "goods_sku" || $this->type === "goods_combination") {
if (empty($ids)) { $builder = GoodsSku::query();
return [$headTitle]; if (request()->get('type_id')) {
} $goodsIds = Goods::query()->filter()->pluck('id')->toArray();
$model = GoodsSku::query() $builder->whereIn('goods_id', $goodsIds);
->when($ids, function ($query, $ids) { }
return $query->whereIn('id', $ids); if (request()->get('goods_title')) {
}) $builder->where('name', 'like', '%' . request()->get('goods_title') . '%');
->with(['goods' => function ($query) { }
$query->with(['type:id,name', 'brand:id,name']) if ($this->type === "goods_combination") {
->orderBy('type_id') $builder->where('is_combination', 1);
->orderBy('brand_id'); }else{
}]); $builder->where('is_combination', 0);
$data = $model->get()->toArray(); }
if (empty($data)) { $data = $builder->filter()->orderByDesc('id')->get()->toArray();
return [$headTitle]; } 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 = []; $bodyData = [];
foreach ($data as $item) { foreach ($data as $item) {
$arr[0] = $item['external_sku_id']; $arr[0] = $item['external_sku_id'];
$arr[1] = $item['name']; $arr[1] = $item['name'];
$arr[2] = !empty($item['goods']['type']['name']) ? $item['goods']['type']['name'] : ''; $arr[2] = $item['sale_stock'] ?? 0;
$arr[3] = $item['title']; $arr[3] = $item['created_at'];
if ('cost' === $this->type) { if ('cost' === $this->type) {
$arr[4] = (string)$item['cost']; $arr[4] = (string)$item['cost'];
$arr[5] = (string)$update[$item['id']]['before_update']; $arr[5] = (string)$update[$item['id']]['before_update'];

View File

@ -28,4 +28,15 @@ class GoodsSkuFilter extends Filters
{ {
return $this->builder->where('is_combination', $value); 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);
}
} }

View File

@ -616,6 +616,7 @@ class GoodsSkusController extends Controller
$endDate = Carbon::now()->subWeek()->endOfWeek()->toDateString(); $endDate = Carbon::now()->subWeek()->endOfWeek()->toDateString();
return Excel::download(new WeekDataExport($startDate, $endDate), $startDate . '~' . $endDate . '.xlsx'); return Excel::download(new WeekDataExport($startDate, $endDate), $startDate . '~' . $endDate . '.xlsx');
} }
return Excel::download(new GoodsSkusExport($type), $type . '.xlsx'); return Excel::download(new GoodsSkusExport($type), $type . '.xlsx');
} }

View File

@ -15,6 +15,8 @@ class GoodsSku extends Model
'exclude_ids', 'exclude_ids',
'external_sku_id', 'external_sku_id',
'is_combination', 'is_combination',
"create_time_start",
"create_time_end"
]; ];
protected $fillable = [ protected $fillable = [
@ -37,8 +39,6 @@ class GoodsSku extends Model
'attribute' 'attribute'
]; ];
protected $hidden = ['created_at'];
public static $STATUS_ON_SALE = 1; public static $STATUS_ON_SALE = 1;
public static $STATUS_DOWN = 0; public static $STATUS_DOWN = 0;