diff --git a/src/Command/Migrate.php b/src/Command/Migrate.php index 434e768..468aad7 100644 --- a/src/Command/Migrate.php +++ b/src/Command/Migrate.php @@ -246,6 +246,16 @@ class Migrate extends Command $table->json('promotions')->nullable(); }); } + if (!Capsule::schema()->hasColumn('prices', 'conversionRate')) { + Capsule::schema()->table('prices', function (Blueprint $table) { + $table->float('conversionRate')->nullable(); + }); + } + if (!Capsule::schema()->hasColumn('products', 'conversionRate')) { + Capsule::schema()->table('products', function (Blueprint $table) { + $table->float('conversionRate')->nullable(); + }); + } } public function index(): void diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index 8178b64..dffd416 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -23,15 +23,17 @@ class ScrapeWebsite extends Command { const COUNTRY_ID = 'country'; private Client $client; + private array $rates; public function __construct(protected Capsule $database) { parent::__construct(); + $this->client = new Client(); + $this->rates = $this->getCurrencyExchange(); } protected function configure(): void { - $this->client = new Client(); $this->addOption(self::COUNTRY_ID, 'c', InputOption::VALUE_OPTIONAL, 'Country id'); } @@ -68,6 +70,7 @@ class ScrapeWebsite extends Command $product->priceLowest = $product->lowestPrice->price; $product->lastSeen = $newestPrice->created_at->format('Y-m-d'); $product->stock = $currentStock->stock; + $product->conversionRate = $newestPrice->conversionRate; $product->save(['timestamps' => false]); $progress->advance(); } @@ -98,7 +101,7 @@ class ScrapeWebsite extends Command $products = array_merge($products, $responseObject->products); $page++; $canLoadMore = $responseObject->canLoadMore; - } catch (GuzzleException $e) { + } catch (GuzzleException) { return $products; } } while ($canLoadMore); @@ -132,6 +135,7 @@ class ScrapeWebsite extends Command $price->price = $product->productPrice; $price->productStandardPrice = $product->productStandardPrice; $price->lowestProductPrice30Days = $product->lowestProductPrice30Days; + $price->conversionRate = $this->getConversionRate($country->currency); $productModel->price()->save($price); } $stockExist = $productModel->stock()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists(); @@ -147,4 +151,22 @@ class ScrapeWebsite extends Command $productModel->stock()->save($stock); } } + + public function getCurrencyExchange(): array + { + $result = $this->client->request('GET', 'https://api.nbp.pl/api/exchangerates/tables/A/?format=json'); + $rates = ['PLN' => 1.0]; + foreach(json_decode($result->getBody()->getContents(),true)[0]['rates'] as $rate){ + $rates[$rate['code']] = $rate['mid']; + } + + return $rates; + } + + private function getConversionRate(string $currency): float + { + $currency = strtoupper($currency); + + return $this->rates[$currency]; + } } diff --git a/src/Models/Price.php b/src/Models/Price.php index a80c94a..9c4d75c 100644 --- a/src/Models/Price.php +++ b/src/Models/Price.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @property float $price * @property float $productStandardPrice * @property float $lowestProductPrice30Days + * @property float $conversionRate */ class Price extends Model { diff --git a/src/Models/Product.php b/src/Models/Product.php index 3f5d456..21f2e38 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -27,6 +27,7 @@ use function Symfony\Component\Clock\now; * @property float $priceLowest * @property float $productStandardPrice * @property float $lowestProductPrice30Days + * @property float $conversionRate * @property Date $lastSeen * @property integer $stock * @property Object $promotions @@ -103,6 +104,11 @@ class Product extends Model ); } + public function conversionRate(): ?float + { + return $this->conversionRate; + } + public function isDiscontinued(): bool { return $this->lastSeen < now()->format('Y-m-d'); diff --git a/templates/productList.html.twig b/templates/productList.html.twig index 0f5f595..23b6226 100644 --- a/templates/productList.html.twig +++ b/templates/productList.html.twig @@ -80,19 +80,30 @@