mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
624 lines
20 KiB
PHP
624 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Dto\PriceCacheDto;
|
|
use App\Enums\Statuses;
|
|
use App\Enums\StockStatus;
|
|
use App\Enums\Trend;
|
|
use App\Filament\Actions\BaseAction;
|
|
use App\Services\Helpers\CurrencyHelper;
|
|
use App\Services\ScrapeUrl;
|
|
use Carbon\Carbon;
|
|
use Database\Factories\ProductFactory;
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @property ?string $title
|
|
* @property ?string $title_short
|
|
* @property string $primary_image
|
|
* @property ?string $image
|
|
* @property array $action_urls
|
|
* @property ?string $view_url
|
|
* @property array $price_cache
|
|
* @property string $average_price
|
|
* @property string $trend
|
|
* @property EloquentCollection<Url> $urls
|
|
* @property EloquentCollection<Tag> $tags
|
|
* @property ?float $notify_price
|
|
* @property ?float $notify_percent
|
|
* @property ?User $user
|
|
* @property int $user_id
|
|
* @property array $price_aggregates
|
|
* @property array $urls_array
|
|
* @property array $ignored_urls
|
|
* @property array $ignored_search_urls
|
|
* @property float $current_price
|
|
* @property bool $is_last_scrape_successful
|
|
* @property bool $is_notified_price
|
|
* @property ?string $unit_of_measure
|
|
* @property Carbon $created_at
|
|
* @property string $first_scrape_date
|
|
*/
|
|
class Product extends Model
|
|
{
|
|
/** @use HasFactory<ProductFactory> */
|
|
use HasFactory;
|
|
|
|
protected $guarded = [
|
|
'price',
|
|
];
|
|
|
|
protected $casts = [
|
|
'status' => Statuses::class,
|
|
'ignored_urls' => 'array',
|
|
'price_cache' => 'array',
|
|
'created_at' => 'datetime',
|
|
'favourite' => 'boolean',
|
|
];
|
|
|
|
public static function booted()
|
|
{
|
|
static::deleted(function (Product $product) {
|
|
// Delete all related urls, should cascade to prices.
|
|
$product->urls()->delete();
|
|
});
|
|
}
|
|
|
|
/***************************************************
|
|
* Relationships.
|
|
**************************************************/
|
|
|
|
/**
|
|
* Product has many urls.
|
|
*/
|
|
public function urls(): HasMany
|
|
{
|
|
return $this->hasMany(Url::class);
|
|
}
|
|
|
|
/**
|
|
* Product has many users.
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* Product has many prices through urls.
|
|
*/
|
|
public function prices(): HasManyThrough
|
|
{
|
|
return $this->hasManyThrough(
|
|
Price::class,
|
|
Url::class,
|
|
'product_id', // Foreign key on the urls table...
|
|
'url_id', // Foreign key on the prices table...
|
|
'id', // Local key on the product table...
|
|
'id' // Local key on the prices table...
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Product has many stores through urls.
|
|
*/
|
|
public function stores(): HasManyThrough
|
|
{
|
|
return $this->hasManyThrough(
|
|
Store::class,
|
|
Url::class,
|
|
'product_id', // Foreign key on the urls table...
|
|
'id', // Foreign key on the store table...
|
|
'id', // Local key on the product table...
|
|
'store_id' // Local key on the stores table...
|
|
);
|
|
}
|
|
|
|
public function tags(): MorphToMany
|
|
{
|
|
return $this->morphToMany(Tag::class, 'taggable');
|
|
}
|
|
|
|
/***************************************************
|
|
* Scopes.
|
|
**************************************************/
|
|
|
|
/**
|
|
* Scope to only published products.
|
|
*/
|
|
public function scopePublished(EloquentBuilder $query): EloquentBuilder
|
|
{
|
|
return $query->where('status', Statuses::Published);
|
|
}
|
|
|
|
/**
|
|
* Scope to only favoured products.
|
|
*/
|
|
public function scopeFavourite(EloquentBuilder $query, bool $favourite = true): EloquentBuilder
|
|
{
|
|
return $query->where('favourite', $favourite);
|
|
}
|
|
|
|
/**
|
|
* Scope to only current user.
|
|
*/
|
|
public function scopeCurrentUser(EloquentBuilder $query): EloquentBuilder
|
|
{
|
|
return $query->where('user_id', auth()->id());
|
|
}
|
|
|
|
/**
|
|
* Scope the lowest price in days.
|
|
*/
|
|
public function scopeLowestPriceInDays(EloquentBuilder $query, int $days = 7): EloquentBuilder
|
|
{
|
|
return $query->whereHas('prices', function ($priceQuery) use ($days) {
|
|
$priceQuery
|
|
->where('prices.created_at', '>=', Carbon::now()->subDays($days)->startOfDay())
|
|
->where('prices.created_at', '<', Carbon::now()->startOfDay())
|
|
->where('prices.price', '>', 0)
|
|
->whereColumn('current_price', '<', 'prices.price');
|
|
});
|
|
}
|
|
|
|
/***************************************************
|
|
* Attributes.
|
|
**************************************************/
|
|
|
|
/**
|
|
* Price trend for lowest priced store.
|
|
*/
|
|
public function trend(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function (): string {
|
|
if ($this->current_price <= 0) {
|
|
return Trend::None->value;
|
|
}
|
|
|
|
return Trend::calculateTrend(
|
|
$this->current_price,
|
|
$this->getPriceCacheAggregate('avg'),
|
|
$this->getPriceCacheAggregate('min'),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Does the product have a price history.
|
|
*/
|
|
public function hasHistory(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function (): bool {
|
|
return $this->trend !== Trend::None->value;
|
|
},
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Price trend for lowest priced store.
|
|
*/
|
|
public function priceAggregates(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn (): Collection => collect(['max', 'avg', 'min'])
|
|
->mapWithKeys(fn ($method) => [$method => $this->getPriceCacheAggregate($method)])
|
|
->filter(fn ($value) => $value > 0)
|
|
->mapWithKeys(fn ($value, $method) => [$method => CurrencyHelper::toString($value)])
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get main image with fallback.
|
|
*/
|
|
public function getPrimaryImageAttribute(): string
|
|
{
|
|
return empty($this->image)
|
|
? asset('/images/placeholder.png')
|
|
: $this->image;
|
|
}
|
|
|
|
/**
|
|
* Short version of the product title.
|
|
*/
|
|
public function titleShort(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => Str::limit($this->title, 20)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get urls array.
|
|
*/
|
|
public function urlsArray(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->urls->pluck('url')->toArray()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get urls array.
|
|
*/
|
|
public function ignoredSearchUrls(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn ($value) => collect($this->ignored_urls)->merge($this->urls_array)->unique()->values()->toArray()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Were all the last scrapes successful.
|
|
*/
|
|
public function isLastScrapeSuccessful(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn ($value): bool => $this->getPriceCache()
|
|
->filter(fn (PriceCacheDto $price) => $price->isLastScrapeSuccessful())
|
|
->count() === $this->getPriceCache()->count()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Does lowest price match notification.
|
|
*/
|
|
public function isNotifiedPrice(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn ($value): bool => $this->shouldNotifyOnPrice(new Price(['price' => $this->current_price]))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the view url for the product.
|
|
*/
|
|
public function getViewUrlAttribute(): ?string
|
|
{
|
|
return $this->action_urls['view'] ?? null;
|
|
}
|
|
|
|
/**
|
|
* Get key urls for the product.
|
|
*/
|
|
public function getActionUrlsAttribute(): array
|
|
{
|
|
return $this->getKey()
|
|
? [
|
|
'edit' => route(BaseAction::ROUTE_NAMESPACE.'products.edit', $this, false),
|
|
'view' => route(BaseAction::ROUTE_NAMESPACE.'products.view', $this, false),
|
|
'fetch' => route(BaseAction::ROUTE_NAMESPACE.'products.fetch', $this, false),
|
|
]
|
|
: [];
|
|
}
|
|
|
|
public function getFirstScrapeDateAttribute(): string
|
|
{
|
|
return $this->getPriceCache()
|
|
->map(fn (PriceCacheDto $price) => $price->getFirstDate())
|
|
->unique()
|
|
->values()
|
|
->sort()
|
|
->first() ?? $this->created_at->toDateTimeString();
|
|
}
|
|
|
|
/**
|
|
* Truncate long titles.
|
|
*/
|
|
public function title(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn ($v) => ScrapeUrl::preSaveTruncate($v),
|
|
set: fn ($v) => ScrapeUrl::preSaveTruncate($v)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Replace long images with null.
|
|
*/
|
|
public function image(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn ($v) => $v,
|
|
set: fn ($v) => ScrapeUrl::preSaveMaxLength($v)
|
|
);
|
|
}
|
|
|
|
/***************************************************
|
|
* Helpers.
|
|
**************************************************/
|
|
|
|
/**
|
|
* Get the price cache for this product.
|
|
*
|
|
* @return Collection<PriceCacheDto>
|
|
*/
|
|
public function getPriceCache(): Collection
|
|
{
|
|
return collect($this->price_cache)
|
|
->sort(function (array $left, array $right): int {
|
|
$leftStatus = StockStatus::fromScrapedValue($left['availability'] ?? null)->getSortOrder();
|
|
$rightStatus = StockStatus::fromScrapedValue($right['availability'] ?? null)->getSortOrder();
|
|
|
|
if ($leftStatus !== $rightStatus) {
|
|
return $leftStatus <=> $rightStatus;
|
|
}
|
|
|
|
$leftPrice = (float) data_get($left, 'unit_price', data_get($left, 'price', 0));
|
|
$rightPrice = (float) data_get($right, 'unit_price', data_get($right, 'price', 0));
|
|
|
|
return $leftPrice <=> $rightPrice;
|
|
})
|
|
->map(fn ($price) => PriceCacheDto::fromArray($price))
|
|
->values();
|
|
}
|
|
|
|
/**
|
|
* Get the price aggregate for this product via getPriceCache().
|
|
*/
|
|
public function getPriceCacheAggregate(string $method = 'avg', ?int $urlId = null): float|int
|
|
{
|
|
$cache = $this->getPriceCache();
|
|
|
|
if ($urlId) {
|
|
$cache = $cache->filter(fn (PriceCacheDto $price) => $price->getUrlId() === $urlId);
|
|
}
|
|
|
|
$values = $cache->map(fn (PriceCacheDto $price) => $price->getHistory()->values()->toArray())
|
|
->flatten()
|
|
->filter(fn ($value) => $value > 0);
|
|
|
|
return $values->isEmpty() ? 0 : round($values->{$method}(), 2);
|
|
}
|
|
|
|
/**
|
|
* Build a price cache array.
|
|
*/
|
|
public function buildPriceCache(): Collection
|
|
{
|
|
$history = $this->getPriceHistory();
|
|
|
|
// Include out-of-stock URLs that have no price history (e.g. price 0).
|
|
/** @var EloquentCollection<int, Url> $outOfStockUrls */
|
|
$outOfStockUrls = $this->urls()
|
|
->whereNotNull('availability')
|
|
->whereNotIn('id', $history->keys())
|
|
->with('store')
|
|
->get();
|
|
|
|
/** @var EloquentCollection<int, Url> $urls */
|
|
$urls = $this->urls()->whereIn('id', $history->keys())->with('store')->get()->merge($outOfStockUrls);
|
|
|
|
/** @var Collection<int, array<string, mixed>> $priceCache */
|
|
$priceCache = $urls
|
|
->filter(function (Url $url): bool {
|
|
return $url->store !== null;
|
|
})
|
|
->map(function (Url $url) use ($history): array {
|
|
/** @var Collection $urlHistory */
|
|
$urlHistory = $history->get($url->getKey(), collect());
|
|
/** @var Store $store */
|
|
$store = $url->store;
|
|
|
|
// Get last scraped price.
|
|
/** @var ?Price $lastScrapedPrice */
|
|
$lastScrapedPrice = $url->prices()->latest('id')->first();
|
|
$lastScrapedTimestamp = $lastScrapedPrice?->created_at;
|
|
|
|
// Build trend, current price vs average price (skip for URLs with no history).
|
|
$trend = $urlHistory->isEmpty()
|
|
? Trend::None->value
|
|
: Trend::calculateTrend(
|
|
$urlHistory->last(),
|
|
$urlHistory->values()->avg(),
|
|
$urlHistory->values()->min(),
|
|
);
|
|
|
|
// Build output suitable for dto.
|
|
return [
|
|
'store_id' => $store->getKey(),
|
|
'store_name' => $store->name,
|
|
'url_id' => $url->getKey(),
|
|
'url' => $url->buy_url,
|
|
'trend' => $trend,
|
|
'price' => $urlHistory->isEmpty() ? 0 : $urlHistory->last(),
|
|
'unit_price' => $lastScrapedPrice->unit_price ?? ($urlHistory->isEmpty() ? 0 : $urlHistory->last()),
|
|
'price_factor' => ($f = $url->price_factor ?: 1) == (int) $f ? (int) $f : $f,
|
|
'history' => $urlHistory->toArray(),
|
|
'last_scrape' => $lastScrapedTimestamp?->toDateTimeString(),
|
|
'locale' => $store->locale,
|
|
'currency' => $store->currency,
|
|
'unit_of_measure' => $this->unit_of_measure,
|
|
'availability' => $url->getAvailabilityStatus()?->value,
|
|
];
|
|
})
|
|
->sort(function ($left, $right): int {
|
|
$leftStatus = StockStatus::fromScrapedValue($left['availability'] ?? null)->getSortOrder();
|
|
$rightStatus = StockStatus::fromScrapedValue($right['availability'] ?? null)->getSortOrder();
|
|
|
|
if ($leftStatus !== $rightStatus) {
|
|
return $leftStatus <=> $rightStatus;
|
|
}
|
|
|
|
$leftPrice = (float) data_get($left, 'unit_price', data_get($left, 'price', 0));
|
|
$rightPrice = (float) data_get($right, 'unit_price', data_get($right, 'price', 0));
|
|
|
|
return $leftPrice <=> $rightPrice;
|
|
})
|
|
->values();
|
|
|
|
return $priceCache;
|
|
}
|
|
|
|
public function getAggregateRange(): array
|
|
{
|
|
return once(function () {
|
|
$dailyPrices = [];
|
|
|
|
$this->getPriceCache()
|
|
->each(function (PriceCacheDto $price) use (&$dailyPrices) {
|
|
$price->getHistory()
|
|
->each(function ($price, $date) use (&$dailyPrices) {
|
|
$dailyPrices[$date][] = $price;
|
|
});
|
|
});
|
|
|
|
$data = [];
|
|
foreach ($dailyPrices as $date => $prices) {
|
|
$min = min($prices);
|
|
$max = max($prices);
|
|
$avg = round((array_sum($prices) / count($prices)), 2);
|
|
|
|
$data['max'][$date] = $max - $avg;
|
|
$data['avg'][$date] = $avg - $min;
|
|
$data['min'][$date] = $min;
|
|
}
|
|
|
|
return $data;
|
|
});
|
|
}
|
|
|
|
public function getAllPricesQuery(): Builder
|
|
{
|
|
return DB::table('prices')
|
|
->select(
|
|
'prices.id',
|
|
'prices.price',
|
|
'prices.unit_price',
|
|
'prices.price_factor',
|
|
'prices.created_at',
|
|
'urls.id as url_id',
|
|
'urls.store_id'
|
|
)
|
|
->join('urls', 'prices.url_id', '=', 'urls.id')
|
|
->where('urls.product_id', $this->id)
|
|
->orderByDesc('prices.created_at');
|
|
}
|
|
|
|
/**
|
|
* Update all prices for this product.
|
|
*/
|
|
public function updatePrices(): bool
|
|
{
|
|
$successful = $this->urls
|
|
->map(fn (Url $url) => $url->updatePrice()) // @phpstan-ignore-line
|
|
->filter();
|
|
|
|
$this->updatePriceCache();
|
|
|
|
return $successful->count() === $this->urls->count();
|
|
}
|
|
|
|
/**
|
|
* Update the price cache for this product.
|
|
*/
|
|
public function updatePriceCache(): void
|
|
{
|
|
$priceCache = $this->buildPriceCache()->toArray();
|
|
$currentPrice = collect($priceCache)
|
|
->first(fn (array $item) => StockStatus::fromScrapedValue($item['availability'] ?? null)->isUnavailable() === false)['unit_price'] ?? 0;
|
|
|
|
$this->update(['price_cache' => $priceCache, 'current_price' => $currentPrice]);
|
|
}
|
|
|
|
/**
|
|
* Get the price history for this product.
|
|
*/
|
|
public function getPriceHistory(string $column = 'price'): Collection
|
|
{
|
|
$priceColumn = $column === 'unit_price' ? 'unit_price' : 'price';
|
|
|
|
return $this->getAllPricesQuery()
|
|
->whereDate('prices.created_at', '>=', now()->subYear())
|
|
->where('prices.price', '>', 0)
|
|
->orderBy('prices.created_at')
|
|
->get()
|
|
->groupBy('url_id')
|
|
->mapWithKeys(function ($prices, int $storeId) use ($priceColumn) {
|
|
// All price entries for the store.
|
|
$storeHistory = $prices->sortBy('created_at')
|
|
->groupBy('created_at')
|
|
->mapWithKeys(fn ($prices, string $date) => [
|
|
Carbon::parse($date)->toDateString() => $prices->pluck($priceColumn)->min(),
|
|
]);
|
|
|
|
// If only one price, extend back one day.
|
|
if ($storeHistory->count() === 1) {
|
|
$storeHistory = $this->extendSinglePriceHistory($storeHistory);
|
|
}
|
|
|
|
return [$storeId => $storeHistory->sortKeys()];
|
|
});
|
|
}
|
|
|
|
/**
|
|
* If we only have one price history, extend it back one day.
|
|
*/
|
|
protected function extendSinglePriceHistory(Collection $history): Collection
|
|
{
|
|
return $history->put(
|
|
Carbon::parse($history->keys()->first())->subDay()->toDateString(),
|
|
$history->values()->first()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the cached price history for this product.
|
|
*/
|
|
public function getPriceHistoryCached(string $column = 'price'): Collection
|
|
{
|
|
if ($column === 'unit_price') {
|
|
return $this->getPriceHistory('unit_price');
|
|
}
|
|
|
|
return $this->getPriceCache()->mapWithKeys(fn (PriceCacheDto $price) => [
|
|
$price->getUrlId() => $price->getHistory(),
|
|
]);
|
|
}
|
|
|
|
public function shouldNotifyOnPrice(Price $price): bool
|
|
{
|
|
$priceValue = (float) $price->price;
|
|
|
|
// Check if price is less than notify price.
|
|
if (! empty($this->notify_price) && $priceValue <= (float) $this->notify_price) {
|
|
return true;
|
|
}
|
|
|
|
if (! empty($this->notify_percent)) {
|
|
// Get first price.
|
|
/** @var ?Price $firstPriceModel */
|
|
$firstPriceModel = $this->prices()->oldest('prices.created_at')->first();
|
|
$firstPrice = $firstPriceModel?->price;
|
|
|
|
// No first price, can't calculate percent.
|
|
if (is_null($firstPrice)) {
|
|
return false;
|
|
}
|
|
|
|
// Calculate percent.
|
|
$notifyPrice = $firstPrice - ($firstPrice * ($this->notify_percent / 100));
|
|
|
|
// Check if percent is greater than notify percent.
|
|
return $priceValue <= $notifyPrice;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|