Source of file DiscountHelper.php
Size: 3,683 Bytes - Last Modified: 2021-12-24T06:46:11+00:00
/var/www/docs.ssmods.com/process/src/src/DiscountHelper.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 | <?php namespace Dynamic\Foxy\Discounts; use Dynamic\Foxy\Discounts\Model\Discount; use Dynamic\Foxy\Discounts\Model\DiscountTier; use Dynamic\Foxy\Model\ProductOption; use SilverStripe\Core\Injector\Injectable; use SilverStripe\ORM\FieldType\DBCurrency; use SilverStripe\ORM\FieldType\DBField; /** * Class DiscountHelper * @package Dynamic\Foxy\Discounts */ class DiscountHelper { use Injectable; /** * @var */ private $product; /** * @var ProductOption */ private $product_option; /** * @var Discount */ private $discount; /** * @var DBField|DBCurrency */ private $discounted_price; /** * @var DiscountTier */ private $discount_tier; /** * DiscountHelper constructor. * @param $product * @param $discount * @param ProductOption|string|null $productOption */ public function __construct($product, $discount, $productOption = null) { $this->setProduct($product); $this->setDiscount($discount); if ($productOption instanceof ProductOption || is_string($productOption)) { $this->setProductOption($productOption); } $this->setDiscountedPrice(); } /** * @return mixed */ public function getProduct() { return $this->product; } /** * @param $product * @return $this */ public function setProduct($product): self { $this->product = $product; return $this; } /** * @return mixed */ public function getProductOption() { return $this->product_option; } /** * @param ProductOption $productOption * @return $this */ public function setProductOption($productOption): self { $this->product_option = ($productOption instanceof ProductOption) ? $productOption : $this->getProduct()->Options()->filter('OptionModifierKey', $productOption)->first(); return $this; } /** * @return mixed */ public function getDiscount() { return $this->discount; } /** * @param Discount $discount * @return $this */ public function setDiscount(Discount $discount): self { $this->discount = $discount; return $this; } /** * @param int $quantity * @return $this */ public function setDiscountTier($quantity = 1) { $this->discount_tier = $this->getDiscount()->DiscountTiers() ->filter('Quantity:LessThanOrEqual', $quantity) ->sort('Quantity DESC')->first(); return $this; } /** * @return DiscountTier */ public function getDiscountTier() { if (!$this->discount_tier) { $this->setDiscountTier(); } return $this->discount_tier; } /** * @return DBField|DBCurrency */ public function setDiscountedPrice() { $price = ($this->getProductOption()) ? $this->getProductOption()->getPrice($this->getProduct()) : $this->getProduct()->Price; $tier = $this->getDiscountTier(); $price = ($this->getDiscount()->Type == 'Percent') ? $price - ($price * ($tier->Percentage / 100)) : $price - $tier->Amount; return $this->discounted_price = DBField::create_field(DBCurrency::class, $price); } /** * @return mixed */ public function getDiscountedPrice() { if (!$this->discounted_price) { $this->setDiscountedPrice(); } return $this->discounted_price; } } |