feat: refactor getForRestock and getForTransaction methods to improve media handling and streamline data retrieval
This commit is contained in:
parent
0d1e6ba1fe
commit
41a32b08f2
@ -14,6 +14,7 @@
|
||||
use App\Services\StockMutationService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
class ProductVariantService
|
||||
{
|
||||
@ -26,7 +27,7 @@ public function __construct(
|
||||
|
||||
public function getForRestock(): array
|
||||
{
|
||||
return Product::query()
|
||||
$products = Product::query()
|
||||
->select(['id', 'name', 'status'])
|
||||
->with([
|
||||
'productVariants:id,product_id,name,stock,reject_stock',
|
||||
@ -34,28 +35,42 @@ public function getForRestock(): array
|
||||
])
|
||||
->active()
|
||||
->orderBy('name')
|
||||
->get()
|
||||
->each(function (Product $product) {
|
||||
$product->productVariants->each(function (ProductVariant $variant) {
|
||||
$media = $variant->getFirstMedia('images');
|
||||
$variant->photo_url = $media
|
||||
? $this->s3Service->getTemporaryUrl($media->getPath())
|
||||
: null;
|
||||
->get();
|
||||
|
||||
$capitalPrice = $variant->productPrices
|
||||
->first(fn ($price) => $price->type === PriceType::CAPITAL);
|
||||
$variant->capital_price = $capitalPrice?->price ?? 0;
|
||||
$allVariantIds = $products->pluck('productVariants.*.id')->flatten()->filter()->all();
|
||||
|
||||
$rejectPrice = $variant->productPrices
|
||||
->first(fn ($price) => $price->type === PriceType::REJECT);
|
||||
$variant->reject_price = $rejectPrice?->price ?? 0;
|
||||
});
|
||||
})->toArray();
|
||||
if ($allVariantIds !== []) {
|
||||
$mediaByVariant = Media::query()
|
||||
->whereIn('mediable_id', $allVariantIds)
|
||||
->where('mediable_type', ProductVariant::class)
|
||||
->where('collection_name', 'images')
|
||||
->get()
|
||||
->groupBy('mediable_id');
|
||||
} else {
|
||||
$mediaByVariant = collect();
|
||||
}
|
||||
|
||||
return $products->each(function (Product $product) use ($mediaByVariant) {
|
||||
$product->productVariants->each(function (ProductVariant $variant) use ($mediaByVariant) {
|
||||
$media = $mediaByVariant->get($variant->id, collect())->first();
|
||||
$variant->photo_url = $media
|
||||
? $this->s3Service->getTemporaryUrl($media->getPath())
|
||||
: null;
|
||||
|
||||
$capitalPrice = $variant->productPrices
|
||||
->first(fn ($price) => $price->type === PriceType::CAPITAL);
|
||||
$variant->capital_price = $capitalPrice?->price ?? 0;
|
||||
|
||||
$rejectPrice = $variant->productPrices
|
||||
->first(fn ($price) => $price->type === PriceType::REJECT);
|
||||
$variant->reject_price = $rejectPrice?->price ?? 0;
|
||||
});
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
public function getForTransaction(): array
|
||||
{
|
||||
return Product::query()
|
||||
$products = Product::query()
|
||||
->select(['id', 'name', 'status'])
|
||||
->with([
|
||||
'productVariants:id,product_id,name,stock,reject_stock',
|
||||
@ -63,18 +78,32 @@ public function getForTransaction(): array
|
||||
])
|
||||
->active()
|
||||
->orderBy('name')
|
||||
->get()
|
||||
->each(function (Product $product) {
|
||||
$product->productVariants->each(function (ProductVariant $variant) {
|
||||
$media = $variant->getFirstMedia('images');
|
||||
$variant->photo_url = $media
|
||||
? $this->s3Service->getTemporaryUrl($media->getPath())
|
||||
: null;
|
||||
->get();
|
||||
|
||||
$prices = $variant->productPrices->mapWithKeys(fn ($p) => [$p->type->value => $p->price]);
|
||||
$variant->prices = $prices;
|
||||
});
|
||||
})->toArray();
|
||||
$allVariantIds = $products->pluck('productVariants.*.id')->flatten()->filter()->all();
|
||||
|
||||
if ($allVariantIds !== []) {
|
||||
$mediaByVariant = Media::query()
|
||||
->whereIn('mediable_id', $allVariantIds)
|
||||
->where('mediable_type', ProductVariant::class)
|
||||
->where('collection_name', 'images')
|
||||
->get()
|
||||
->groupBy('mediable_id');
|
||||
} else {
|
||||
$mediaByVariant = collect();
|
||||
}
|
||||
|
||||
return $products->each(function (Product $product) use ($mediaByVariant) {
|
||||
$product->productVariants->each(function (ProductVariant $variant) use ($mediaByVariant) {
|
||||
$media = $mediaByVariant->get($variant->id, collect())->first();
|
||||
$variant->photo_url = $media
|
||||
? $this->s3Service->getTemporaryUrl($media->getPath())
|
||||
: null;
|
||||
|
||||
$prices = $variant->productPrices->mapWithKeys(fn ($p) => [$p->type->value => $p->price]);
|
||||
$variant->prices = $prices;
|
||||
});
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
public function getForEdit(ProductVariant $variant): array
|
||||
|
||||
Loading…
Reference in New Issue
Block a user