src/DataObject/Product.php line 5

Open in your IDE?
  1. <?php
  2. namespace App\DataObject;
  3. class Product extends \Pimcore\Model\DataObject\Product
  4. {
  5.     use HasArrayExport;
  6.     public function exportForCalculator($consumption$addons): array
  7.     {
  8.         $allFields $this->export();
  9.         $allFields['price'] = $this->calculatePrices($consumption);
  10.         return $allFields;
  11.     }
  12.     /**
  13.      * @param int $consumption
  14.      *
  15.      * @return array{basePrice: float, unitPrice: float}|null
  16.      */
  17.     public function getPriceForConsumption(int $consumption): ?array
  18.     {
  19.         $prices array_filter($this->getPrices(), fn ($price) => isset($price['consumption']));
  20.         if (!empty($prices)) {
  21.             usort($prices, function ($a$b) {
  22.                 return $a['consumption']->getData() <=> $b['consumption']->getData();
  23.             });
  24.             $x 0;
  25.             while ($consumption $prices[$x]['consumption']->getData()) {
  26.                 if (!isset($prices[$x 1])) break;
  27.                 $x++;
  28.             }
  29.             if (!isset($prices[$x])) {
  30.                 return null;
  31.             }
  32.             return [
  33.                 'basePrice' => $prices[$x]['basePrice']->getData(),
  34.                 'unitPrice' => $prices[$x]['unitPrice']->getData(),
  35.                 'consumption' => $prices[$x]['consumption']->getData(),
  36.             ];
  37.         }
  38.         return null;
  39.     }
  40.     public function calculatePrices(int $consumption): bool|array
  41.     {
  42.         $price $this->getPriceForConsumption($consumption);
  43.         $basePriceGross $price['basePrice'];
  44.         $unitPriceGross $price['unitPrice'];
  45.         $basePrice round($basePriceGross 100 1052);
  46.         $unitPrice round($unitPriceGross 100 1052);
  47.         $monthlyGross round($basePriceGross + (($unitPriceGross $consumption) / 12), 2);
  48.         $yearlyGross round($basePriceGross + ($unitPriceGross $consumption), 2);
  49.         $monthly round($monthlyGross 100 1052);
  50.         $yearly round($yearlyGross 100 1052);
  51.         if (!empty($price)) {
  52.             return [
  53.                 "unitPrice" => (float) $unitPriceGross,
  54.                 "consumption" => $consumption,
  55.                 "active" => true,
  56.                 "workingPrice" => $unitPrice,
  57.                 "workingPriceGross" => (float) $unitPriceGross,
  58.                 "basePrice" => $basePrice,
  59.                 "basePriceGross" => (float) $basePriceGross,
  60.                 "monthly" => $monthly,
  61.                 "monthlyGross" => $monthlyGross,
  62.                 "yearly" => $yearly,
  63.                 "yearlyGross" => $yearlyGross,
  64.                 "tax" => 1.05,
  65.                 "taxOperation" => "*",
  66.                 "isMarginalPrice" => false,
  67.             ];
  68.         }
  69.         return false;
  70.     }
  71. }