Compare commits
2 Commits
8a520b11d6
...
feature/ad
| Author | SHA1 | Date | |
|---|---|---|---|
| 67019e3933 | |||
| a545bfe2ab |
@@ -181,19 +181,72 @@ class Migrate extends Command
|
|||||||
$table->integer('stock')->nullable();
|
$table->integer('stock')->nullable();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (!Capsule::schema()->hasColumn('products', 'promotions')) {
|
||||||
|
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||||
|
$table->json('promotions')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function index(): void
|
public function index(): void
|
||||||
{
|
{
|
||||||
if (!count(Capsule::select('SELECT name FROM sqlite_master WHERE type = "index" and name = "products_skuid_country_id_unique"'))) {
|
if (!$this->hasIndex('products_skuid_country_id_unique')) {
|
||||||
Capsule::schema()->table('products', function (Blueprint $table) {
|
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||||
|
if ($this->hasIndex('products_skuid_unique')) {
|
||||||
$table->integer('skuID')->unique(false)->change();
|
$table->integer('skuID')->unique(false)->change();
|
||||||
|
}
|
||||||
$table->unique(['skuID', 'country_id']);
|
$table->unique(['skuID', 'country_id']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$this->hasIndex('prices_product_id_index')) {
|
||||||
|
Capsule::schema()->table('prices', function (Blueprint $table) {
|
||||||
|
$table->index('product_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->hasIndex('products_id_index')) {
|
||||||
|
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||||
|
$table->index('id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->hasIndex('stocks_product_id_stock_index')) {
|
||||||
|
Capsule::schema()->table('stocks', function (Blueprint $table) {
|
||||||
|
$table->index(['product_id', 'stock']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->isFK('products', 'id', 'stocks', 'product_id')) {
|
||||||
Capsule::schema()->table('products', function (Blueprint $table) {
|
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||||
$table->foreign('id')->references('product_id')->on('stocks');
|
$table->foreign('id')->references('product_id')->on('stocks');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$this->isFK('products', 'id', 'prices', 'product_id')) {
|
||||||
|
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||||
|
$table->foreign('id')->references('product_id')->on('prices');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!$this->isFK('products', 'country_id', 'countries', 'id')) {
|
||||||
|
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||||
|
$table->foreign('country_id')->references('id')->on('countries');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isFK(string $table, string $column, string $fTable, string $fColumn): bool
|
||||||
|
{
|
||||||
|
$fkColumns = Capsule::schema()->getForeignKeys($table);
|
||||||
|
|
||||||
|
return !empty(array_filter($fkColumns, fn($fkColumn) => ($fkColumn['foreign_table'] == $fTable &&
|
||||||
|
in_array($fColumn, $fkColumn['foreign_columns']) &&
|
||||||
|
in_array($column, $fkColumn['columns']))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function hasIndex(string $indexName): bool
|
||||||
|
{
|
||||||
|
return !!count(Capsule::select('SELECT name FROM sqlite_master WHERE type = "index" and name = ?', [$indexName]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,6 +120,7 @@ class ScrapeWebsite extends Command
|
|||||||
$productModel->lastSeen = date("Y-m-d");
|
$productModel->lastSeen = date("Y-m-d");
|
||||||
$productModel->touch('updated_at');
|
$productModel->touch('updated_at');
|
||||||
$productModel->country()->associate($country);
|
$productModel->country()->associate($country);
|
||||||
|
$productModel->promotions = $product->promotions;
|
||||||
$productModel->save();
|
$productModel->save();
|
||||||
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||||
|
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ final class IndexController extends BaseController
|
|||||||
if ($this->cache->getItem('list_all')->isHit()) {
|
if ($this->cache->getItem('list_all')->isHit()) {
|
||||||
return $this->render('productList.html.twig', ['listType' => 'all']);
|
return $this->render('productList.html.twig', ['listType' => 'all']);
|
||||||
}
|
}
|
||||||
$products = Product::with(['currentStock'])
|
$products = Product::orderByDesc('starred')
|
||||||
->orderByDesc('starred')
|
|
||||||
->orderByDesc('created_by')
|
->orderByDesc('created_by')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
|
|
||||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'all']);
|
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'all']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,18 +8,25 @@ use Symfony\Component\Routing\Attribute\Route;
|
|||||||
|
|
||||||
final class PromosController extends BaseController
|
final class PromosController extends BaseController
|
||||||
{
|
{
|
||||||
#[Route('/promos', name: 'app_promos')]
|
#[Route('/promos/{promo?}', name: 'app_promos')]
|
||||||
public function __invoke(): Response
|
public function __invoke(?string $promo): Response
|
||||||
{
|
{
|
||||||
if ($this->cache->getItem('list_promos')->isHit()) {
|
if ($this->cache->getItem('list_promos')->isHit()) {
|
||||||
return $this->render('productList.html.twig', ['listType' => 'promos']);
|
return $this->render('productList.html.twig', ['listType' => 'promos' . $promo]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$products = Product::whereRaw('priceCurrent < productStandardPrice')
|
$products = Product::when(is_null($promo), fn($q) => $q->whereRaw('priceCurrent < productStandardPrice'))
|
||||||
->orderByDesc('starred')
|
->orderByDesc('starred')
|
||||||
->orderByDesc('created_by')
|
->orderByDesc('created_by')
|
||||||
->with(['currentPrice', 'lowestPrice'])
|
->with(['currentPrice', 'lowestPrice'])
|
||||||
|
->when(!is_null($promo), fn($q) => $q->whereRaw("json_extract(promotions, '$.slug') LIKE ?", $promo))
|
||||||
->get();
|
->get();
|
||||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'promos']);
|
|
||||||
|
|
||||||
|
$promos = Product::select($this->database->getConnection()->raw("distinct json_extract(promotions, '$.slug') as slug, json_extract(promotions, '$.tag') as tag"))
|
||||||
|
->whereRaw("json_extract(promotions, '$.tag') is not null")
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'promos' . $promo, 'promos' => $promos->toArray()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use function Symfony\Component\Clock\now;
|
use function Symfony\Component\Clock\now;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,6 +29,7 @@ use function Symfony\Component\Clock\now;
|
|||||||
* @property float $lowestProductPrice30Days
|
* @property float $lowestProductPrice30Days
|
||||||
* @property Date $lastSeen
|
* @property Date $lastSeen
|
||||||
* @property integer $stock
|
* @property integer $stock
|
||||||
|
* @property Object $promotions
|
||||||
*/
|
*/
|
||||||
class Product extends Model
|
class Product extends Model
|
||||||
{
|
{
|
||||||
@@ -58,9 +60,10 @@ class Product extends Model
|
|||||||
{
|
{
|
||||||
return $this->hasOne(Price::class)->ofMany('price', 'MIN');
|
return $this->hasOne(Price::class)->ofMany('price', 'MIN');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function newestPrice(): HasOne
|
public function newestPrice(): HasOne
|
||||||
{
|
{
|
||||||
return $this->hasOne(Price::class)->latest();
|
return $this->hasOne(Price::class)->latest()->take(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stock(): HasMany
|
public function stock(): HasMany
|
||||||
@@ -70,7 +73,7 @@ class Product extends Model
|
|||||||
|
|
||||||
public function currentStock(): HasOne
|
public function currentStock(): HasOne
|
||||||
{
|
{
|
||||||
return $this->stock()->one()->ofMany()->withDefault(fn (Stock $stock) => $stock->stock = 0);
|
return $this->stock()->one()->ofMany()->withDefault(fn(Stock $stock) => $stock->stock = 0)->take(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toggleStarred(): self
|
public function toggleStarred(): self
|
||||||
@@ -88,10 +91,23 @@ class Product extends Model
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function promotions(): Attribute
|
||||||
|
{
|
||||||
|
return Attribute::make(
|
||||||
|
get: fn(?string $value) => json_decode($value ?? '{"hasPromotion": false}', 1),
|
||||||
|
set: function (\stdClass $value) {
|
||||||
|
$value->slug = Str::slug($value->tag);
|
||||||
|
|
||||||
|
return json_encode($value);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function isDiscontinued(): bool
|
public function isDiscontinued(): bool
|
||||||
{
|
{
|
||||||
return $this->lastSeen < now()->format('Y-m-d');
|
return $this->lastSeen < now()->format('Y-m-d');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isNew(): bool
|
public function isNew(): bool
|
||||||
{
|
{
|
||||||
return $this->created_at->format('Y-m-d') > now()->modify('-30 days')->format('Y-m-d');
|
return $this->created_at->format('Y-m-d') > now()->modify('-30 days')->format('Y-m-d');
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
<td>
|
<td>
|
||||||
<a href='{{ path('app_product', {'productId': product.id}) }}' class="text-decoration-none">{{ product.name }}</a>
|
<a href='{{ path('app_product', {'productId': product.id}) }}' class="text-decoration-none">{{ product.name }}</a>
|
||||||
<span class="badge text-bg-light"><a href="{{ path('app_search', {'search': product.subTitle}) }}" class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span>
|
<span class="badge text-bg-light"><a href="{{ path('app_search', {'search': product.subTitle}) }}" class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span>
|
||||||
|
{% if product.promotions is not null and product.promotions.hasPromotion %}<a href="{{ path('app_promos', {'promo': product.promotions.slug}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span></a>{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">
|
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">
|
||||||
|
|||||||
@@ -7,6 +7,12 @@
|
|||||||
{{ renderCategoryTree(categoryTree, category) | raw }}
|
{{ renderCategoryTree(categoryTree, category) | raw }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% if listType starts with 'promos' %}
|
||||||
|
{% for promo in promos %}
|
||||||
|
<a href="{{ path('app_promos', {'promo': promo.slug}) }}"><span class="badge bg-info">PROMO: {{ promo.tag }}</span></a>
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
|
||||||
{% if (listType starts with 'category_' and category == null) or not (listType starts with 'category_') or (listType starts with 'category_' and category is not null) %}
|
{% if (listType starts with 'category_' and category == null) or not (listType starts with 'category_') or (listType starts with 'category_' and category is not null) %}
|
||||||
<div class="table-responsive">
|
<div class="table-responsive">
|
||||||
@@ -49,6 +55,7 @@
|
|||||||
<span class="badge text-bg-light"><a
|
<span class="badge text-bg-light"><a
|
||||||
href="{{ path('app_search', {'search': product.subTitle}) }}"
|
href="{{ path('app_search', {'search': product.subTitle}) }}"
|
||||||
class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span>
|
class="link-underline link-underline-opacity-0 link-dark">{{ product.subTitle }}</a></span>
|
||||||
|
{% if product.promotions is not null and product.promotions.hasPromotion %}<a href="{{ path('app_promos', {'promo': product.promotions.slug}) }}"><span class="badge bg-info">PROMO: {{ product.promotions.tag }}</span></a>{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td class="align-middle">
|
<td class="align-middle">
|
||||||
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">
|
<nav aria-label="breadcrumb" style="--bs-breadcrumb-divider: '>';">
|
||||||
|
|||||||
Reference in New Issue
Block a user