Source of file DiscountTier.php
Size: 3,824 Bytes - Last Modified: 2021-12-24T06:46:11+00:00
/var/www/docs.ssmods.com/process/src/src/Model/DiscountTier.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 | <?php namespace Dynamic\Foxy\Discounts\Model; use Dynamic\Foxy\Coupons\Model\Coupon; use SilverStripe\Forms\FieldList; use SilverStripe\ORM\DataObject; /** * Class DiscountTier * @package Dynamic\Foxy\Discounts\Model */ class DiscountTier extends DataObject { /** * @var array */ private static $db = [ 'Quantity' => 'Int', 'Percentage' => 'Int', 'Amount' => 'Currency', ]; /** * @var array */ private static $has_one = [ 'Discount' => Discount::class, ]; /** * @var array */ private static $defaults = [ 'Quantity' => 1, ]; /** * @var array */ private static $summary_fields = [ 'DiscountLabel' => [ 'title' => 'Discount', ], 'Quantity', ]; /** * @var string */ private static $table_name = 'FoxyDiscountTier'; /** * @var array */ private static $default_sort = [ 'Quantity', ]; /** * @return FieldList|void */ public function getCMSFields() { $this->beforeUpdateCMSFields(function (FieldList $fields) { $fields->removeByName([ 'DiscountID', ]); $quantity = $fields->dataFieldByName('Quantity'); $quantity->setTitle('Quantity to trigger discount'); /** @var Discount|Coupon $type */ if ($parent = $this->getParent()) { $type = $parent->Type; $percentage = $fields->dataFieldByName('Percentage') ->setTitle('Percent discount'); $amount = $fields->dataFieldByName('Amount') ->setTitle('Amount to discount'); $fields->removeByName([ 'Percentage', 'Amount', ]); if ($type == 'Percent') { $fields->addFieldToTab( 'Root.Main', $percentage ); } elseif ($type == 'Amount') { $fields->addFieldToTab( 'Root.Main', $amount ); } } }); return parent::getCMSFields(); } /** * @return string */ protected function getParent() { foreach ($this->hasOne() as $relationName => $className) { $field = "{$relationName}ID"; if ($this->{$field} > 0) { return $className::get()->byID($this->{$field}); } } return false; } /** * @return \SilverStripe\ORM\ValidationResult|void */ public function validate() { $response = parent::validate(); if ($this->exists()) { $exclude['ID'] = $this->ID; } /** @var Discount $discount */ if ($discount = Discount::get()->byID($this->DiscountID)) { $existing = $discount->DiscountTiers()->filter('Quantity', $this->Quantity); if (isset($exclude)) { $existing = $existing->exclude($exclude); } if ($existing->count() > 0) { $response->addError("A discount tier already has the quantity {$this->Quantity} set"); } } return $response; } /** * @return string */ public function getDiscountLabel() { $type = $this->Discount()->Type; $label = ''; if ($type == 'Percent') { $label = "{$this->Percentage}%"; } elseif ($type == 'Amount') { $label = $this->dbObject('Amount')->Nice(); } $this->extend('updateDiscountLabel', $label); return $label; } } |