From 5e40519bc5af0be5d571339a78b3af881dfba11f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Thu, 20 Feb 2025 16:29:37 +0100 Subject: [PATCH] Allow to select if VAT should be included or not --- .env | 2 ++ .../Providers/ReicheltProvider.php | 25 +++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.env b/.env index a985eac5..678430d2 100644 --- a/.env +++ b/.env @@ -227,6 +227,8 @@ PROVIDER_REICHELT_ENABLED=0 PROVIDER_REICHELT_COUNTRY=DE # The language to get results in (en, de, fr, nl, pl, it, es) PROVIDER_REICHELT_LANGUAGE=en +# Include VAT in prices (set to 1 to include VAT, 0 to exclude VAT) +PROVIDER_REICHELT_INCLUDE_VAT=1 ################################################################################## # EDA integration related settings diff --git a/src/Services/InfoProviderSystem/Providers/ReicheltProvider.php b/src/Services/InfoProviderSystem/Providers/ReicheltProvider.php index 1678a4c6..89ba48d8 100644 --- a/src/Services/InfoProviderSystem/Providers/ReicheltProvider.php +++ b/src/Services/InfoProviderSystem/Providers/ReicheltProvider.php @@ -46,7 +46,10 @@ class ReicheltProvider implements InfoProviderInterface #[Autowire(env: "PROVIDER_REICHELT_LANGUAGE")] private readonly string $language = "en", #[Autowire(env: "PROVIDER_REICHELT_COUNTRY")] - private readonly string $country = "DE") + private readonly string $country = "DE", + #[Autowire(env: "PROVIDER_REICHELT_INCLUDE_VAT")] + private bool $includeVAT = false + ) { } @@ -128,7 +131,11 @@ class ReicheltProvider implements InfoProviderInterface $productPage = $this->getBaseURL() . '/shop/product' . $json[0]['article_path']; - $response = $this->client->request('GET', $productPage); + $response = $this->client->request('GET', $productPage, [ + 'query' => [ + 'CCTYPE' => $this->includeVAT ? 'private' : 'business', + ], + ]); $html = $response->getContent(); $dom = new Crawler($html); @@ -141,13 +148,17 @@ class ReicheltProvider implements InfoProviderInterface $datasheets[] = new FileDTO($element->attr('href'), $element->filter('span')->text()); }); + //Determine price for one unit + $priceString = $dom->filter('meta[itemprop="price"]')->attr('content'); + $currency = $dom->filter('meta[itemprop="priceCurrency"]')->attr('content', 'EUR'); + //Create purchase info $purchaseInfo = new PurchaseInfoDTO( distributor_name: self::DISTRIBUTOR_NAME, order_number: $json[0]['article_artnr'], prices: [ - new PriceDTO(1.0, (string) $json[0]['article_price'], 'EUR') - ] + $this->parseBatchPrices($dom), + new PriceDTO(1.0, $priceString, $currency, $this->includeVAT) + ] + $this->parseBatchPrices($dom, $currency), product_url: $productPage ); @@ -183,11 +194,11 @@ class ReicheltProvider implements InfoProviderInterface return $element->filter('span')->text(); } - private function parseBatchPrices(Crawler $dom): array + private function parseBatchPrices(Crawler $dom, string $currency): array { //Iterate over each a.inline-block element in div.discountValue $prices = []; - $dom->filter('div.discountValue a.inline-block')->each(function (Crawler $element) use (&$prices) { + $dom->filter('div.discountValue a.inline-block')->each(function (Crawler $element) use (&$prices, $currency) { //The minimum amount is the number in the span.block element $minAmountText = $element->filter('span.block')->text(); @@ -206,7 +217,7 @@ class ReicheltProvider implements InfoProviderInterface //Strip any non-numeric characters $priceString = preg_replace('/[^0-9.]/', '', $priceString); - $prices[] = new PriceDTO($minAmount, $priceString, 'EUR'); + $prices[] = new PriceDTO($minAmount, $priceString, $currency, $this->includeVAT); }); return $prices;