diff --git a/src/Command/Migrate.php b/src/Command/Migrate.php index d19b68c..af77e95 100644 --- a/src/Command/Migrate.php +++ b/src/Command/Migrate.php @@ -38,7 +38,7 @@ class Migrate extends Command $this->createProductsTable(); $this->createPricesTable(); $this->createStocksTable(); - $this->updateProductsTableAddPrices(); + $this->addColumns(); return Command::SUCCESS; } @@ -95,7 +95,7 @@ class Migrate extends Command } } - public function updateProductsTableAddPrices(): void + public function addColumns(): void { if (!Capsule::schema()->hasColumn('products', 'priceCurrent')) { Capsule::schema()->table('products', function (Blueprint $table) { @@ -120,5 +120,11 @@ class Migrate extends Command $table->float('lowestProductPrice30Days')->default(0); }); } + + if (!Capsule::schema()->hasColumn('products', 'lastSeen')) { + Capsule::schema()->table('products', function (Blueprint $table) { + $table->date('last_seen')->nullable(); + }); + } } } diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index 63c8c4a..1c094b6 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -7,6 +7,7 @@ namespace Krzysiej\RyobiCrawler\Command; use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; use Illuminate\Database\Capsule\Manager as Capsule; +use Illuminate\Support\Facades\Date; use Krzysiej\RyobiCrawler\Models\Price; use Krzysiej\RyobiCrawler\Models\Product; use Krzysiej\RyobiCrawler\Models\Stock; @@ -51,10 +52,12 @@ class ScrapeWebsite extends Command $progress->setMaxSteps(count($products)); $progress->start(); foreach($products as $product) { - $product->priceCurrent = $product->newestPrice->price; - $product->productStandardPrice = $product->newestPrice->productStandardPrice; - $product->lowestProductPrice30Days = $product->newestPrice->lowestProductPrice30Days; + $newestPrice = $product->newestPrice; + $product->priceCurrent = $newestPrice->price; + $product->productStandardPrice = $newestPrice->productStandardPrice; + $product->lowestProductPrice30Days = $newestPrice->lowestProductPrice30Days; $product->priceLowest = $product->lowestPrice->price; + $product->lastSeen = $newestPrice->created_at->format('Y-m-d'); $product->save(['timestamps' => false]); $progress->advance(); } @@ -106,6 +109,7 @@ class ScrapeWebsite extends Command $productModel->variantCode = $product->variantCode; $productModel->modelCode = $product->modelCode; $productModel->url = $product->url; + $productModel->lastSeen = date("Y-m-d"); $productModel->touch('updated_at'); $productModel->save(); $priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists(); diff --git a/src/Controller/DiscontinuedController.php b/src/Controller/DiscontinuedController.php index d45acc5..4c5b96b 100644 --- a/src/Controller/DiscontinuedController.php +++ b/src/Controller/DiscontinuedController.php @@ -17,10 +17,9 @@ final class DiscontinuedController extends BaseController return $this->render('productList.html.twig', ['listType' => 'discontinued']); } - $products = Product::where('updated_at', '<', now()->format('Y-m-d')) + $products = Product::where('lastSeen', '<', now()->format('Y-m-d')) ->orderByDesc('starred') ->orderByDesc('created_by') - ->with(['currentPrice', 'lowestPrice']) ->get(); return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'discontinued']); } diff --git a/src/Models/Product.php b/src/Models/Product.php index 3b77e8d..a44f846 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -2,6 +2,7 @@ namespace Krzysiej\RyobiCrawler\Models; +use Carbon\Traits\Date; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -24,6 +25,7 @@ use function Symfony\Component\Clock\now; * @property float $priceLowest * @property float $productStandardPrice * @property float $lowestProductPrice30Days + * @property Date $lastSeen */ class Product extends Model { @@ -81,7 +83,7 @@ class Product extends Model public function isDiscontinued(): bool { - return $this->updated_at->format('Y-m-d') < now()->format('Y-m-d'); + return $this->lastSeen < now()->format('Y-m-d'); } public function isNew(): bool { diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index f0b1ac9..f742553 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -49,7 +49,7 @@ class AppExtension extends AbstractExtension public function discontinuedCount(): int { - return Product::where('updated_at', '<', now()->format('Y-m-d'))->count(); + return Product::where('lastSeen', '<>', now()->format('Y-m-d'))->count(); } public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null diff --git a/templates/productList.html.twig b/templates/productList.html.twig index d40a721..94a4248 100644 --- a/templates/productList.html.twig +++ b/templates/productList.html.twig @@ -29,7 +29,7 @@ out of stock {% endif %} {% if product.isDiscontinued() %} - is discontinued + is discontinued {% endif %} {% if product.isNew() %} is new @@ -46,7 +46,13 @@ link - {% if product.priceLowest != product.priceCurrent %}{{ product.priceLowest | format_currency('PLN', {}, 'pl') }}{%else%}now lowest{% endif %} + + {% if product.isDiscontinued() %} + {{ product.priceLowest | format_currency('PLN', {}, 'pl') }} + {% else %} + {% if product.priceLowest != product.priceCurrent %}{{ product.priceLowest | format_currency('PLN', {}, 'pl') }}{%else%}now lowest{% endif %} + {% endif %} + {{ product.priceCurrent | format_currency('PLN', {}, 'pl') }}