Source of file DiscountCouponProductDataExtension.php
Size: 5,071 Bytes - Last Modified: 2021-12-23T10:40:47+00:00
/var/www/docs.ssmods.com/process/src/src/Model/Buyables/DiscountCouponProductDataExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 | <?php namespace Sunnysideup\EcommerceDiscountCoupon\Model\Buyables; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\FieldType\DBDate; use SilverStripe\ORM\FieldType\DBField; use Sunnysideup\Ecommerce\Model\Money\EcommerceCurrency; use Sunnysideup\EcommerceDiscountCoupon\Model\DiscountCouponOption; class DiscountCouponProductDataExtension extends DataExtension { protected static $buyable_to_be_excluded_from_discounts = []; /** * stadard SS declaration. * * @var array */ private static $belongs_many_many = [ 'ApplicableDiscountCoupons' => DiscountCouponOption::class, ]; private $discountCouponAmount; /** * Update Fields. */ public function updateCMSFields(FieldList $fields) { $fields->addFieldsToTab( 'Root.Discount', GridField::create( 'ApplicableDiscountCoupons', 'Discount Coupons', $this->getOwner()->ApplicableDiscountCoupons(), GridFieldConfig_RelationEditor::create() ) ); } public static function add_buyable_to_be_excluded($buyableOrBuyableID) { $id = 0; if (is_object($buyableOrBuyableID)) { $id = $buyableOrBuyableID->ID; } elseif ((int) $buyableOrBuyableID) { $id = (int) $buyableOrBuyableID; } self::$buyable_to_be_excluded_from_discounts[$id] = $id; } public function setCanBeNotDiscounted() { self::$buyable_to_be_excluded_from_discounts[$this->getOwner()->ID] = $this->getOwner()->ID; return $this; } public function getCanBeDiscounted() { return ! isset(self::$buyable_to_be_excluded_from_discounts[$this->getOwner()->ID]); } /** * @param float $price * * @return null|float */ public function updateCalculatedPrice(?float $price = null) { if ($this->getCanBeDiscounted()) { $hasDiscount = false; $coupons = $this->getOwner()->DirectlyApplicableDiscountCoupons(); if ($coupons->exists()) { $discountPercentage = 0; $discountAbsolute = 0; foreach ($coupons as $coupon) { if ($coupon->isValid()) { $hasDiscount = true; if ($coupon->DiscountPercentage > $discountPercentage) { $discountPercentage = $coupon->DiscountPercentage; } if ($coupon->DiscountAbsolute > $discountAbsolute) { $discountAbsolute = $coupon->DiscountAbsolute; } } } if ($hasDiscount && $price) { $priceWithPercentageDiscount = $price - ($price * ($discountPercentage / 100)); $priceWithAbsoluteDiscount = $price - $discountAbsolute; if ($priceWithPercentageDiscount < $priceWithAbsoluteDiscount) { return $priceWithPercentageDiscount; } return $priceWithAbsoluteDiscount; } } } return null; } public function DirectlyApplicableDiscountCoupons() { return $this->getOwner()->ApplicableDiscountCoupons() ->filter(['ApplyPercentageToApplicableProducts' => 1, 'ApplyEvenWithoutCode' => 1]) ; } public function DiscountCouponAmount() { if (null === $this->discountCouponAmount) { $this->discountCouponAmount = 0; $amount = floatval($this->getOwner()->Price) - floatval($this->getOwner()->CalculatedPrice()); if ($amount > 1) { $this->discountCouponAmount = $amount; } } return EcommerceCurrency::get_money_object_from_order_currency($this->discountCouponAmount); } /** * @return null|\SilverStripe\ORM\FieldType\DBDate */ public function DiscountsAvailableUntil() { $coupons = $this->DirectlyApplicableDiscountCoupons(); $next = strtotime('+100 years'); $obj = null; if ($coupons->exists()) { $discount = 0; foreach ($coupons as $coupon) { if ($coupon->isValid()) { if ($coupon->EndDate && $coupon->DiscountAbsolute > $discount) { $discount = $coupon->DiscountAbsolute; $maxDate = strtotime($coupon->EndDate); if ($maxDate < $next) { $next = $maxDate; } } } } } if ($next) { /** @var DBDate $obj */ $obj = DBField::create_field(DBDate::class, $next); } return $obj; } } |