Source of file Discount.php
Size: 12,854 Bytes - Last Modified: 2021-12-24T06:40:39+00:00
/var/www/docs.ssmods.com/process/src/src/model/Discount.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 | <?php namespace Cita\eCommerce\Model; use SilverStripe\Control\Controller; use SilverStripe\Dev\Debug; use SilverStripe\View\Requirements; use SilverStripe\Forms\HeaderField; use SilverStripe\ORM\DataObject; use SilverStripe\Forms\TextField; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\LiteralField; use SilverStripe\Security\Group; use Cita\eCommerce\Model\Product; use Cita\eCommerce\Model\Variant; use SilverStripe\View\ViewableData; /** * Description * * @package silverstripe * @subpackage mysite */ class Discount extends DataObject { /** * Defines the database table name * @var string */ private static $table_name = 'Cita_eCommerce_Discount'; /** * Singular name for CMS * @var string */ private static $singular_name = 'Discount'; /** * Plural name for CMS * @var string */ private static $plural_name = 'Discounts'; /** * Database fields * @var array */ private static $db = [ 'Title' => 'Varchar(128)', 'DiscountBy' => 'Enum("ByPercentage,ByValue")', 'DiscountRate' => 'Decimal', 'Type' => 'Varchar(128)', 'CouponCode' => 'Varchar(128)', 'NumItemsToMeetCondition' => 'Int', 'NumCopies' => 'Int', 'Used' => 'Boolean', 'InfiniteUse' => 'Boolean', 'ValidFrom' => 'Datetime', 'ValidUntil' => 'Datetime', 'LifePoint' => 'Int' ]; private static $types = [ 'Member Type' => 'Member Type', 'Coupon' => 'Coupon', 'Item Count' => 'Item Count', 'Product' => 'Product', ]; private static $indexes = [ 'CouponCode' => [ 'type' => 'unique', 'columns' => ['CouponCode'], ] ]; /** * Defines summary fields commonly used in table columns * as a quick overview of the data for this dataobject * @var array */ private static $summary_fields = [ 'Title', 'Type', 'CouponCode', 'Used' ]; /** * Belongs_to relationship * @var array */ private static $belongs_to = [ 'Group' => Group::class ]; private static $many_many = [ 'Products' => Product::class, 'Variants' => Variant::class ]; public function populateDefaults() { $this->Type = 'Coupon'; $this->LifePoint = 1; $this->CouponCode = strtoupper(substr(sha1(time()), 0, 8)); } /** * CMS Fields * @return FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $coupon = $fields->fieldByName('Root.Main.CouponCode'); $fields->removeByName([ 'Type' ]); $fields->fieldByName('Root.Main.DiscountRate')->setDescription('If "Discount By" is set to "By Percentage", it will be x% off; if set to "By Value", it will be $x off.'); $type = DropdownField::create( 'Type', 'Type', $this->config()->types )->setEmptyString('- select one -'); $fields->addFieldToTab( 'Root.Main', $type, 'CouponCode' ); if ($this->Type == 'Coupon') { $fields->addFieldToTab( 'Root.Main', $coupon ); } elseif ($this->Type == 'Member Type') { if ($this->Group()->exists()) { $field = LiteralField::create('Group', 'This discount has been bound to <a href="/admin/security/EditForm/field/Groups/item/' . $this->Group()->ID . '/edit">' . $this->Group()->Title . '</a> group'); } else { $field = LiteralField::create('Group', 'Please go to the <a href="/admin/security/groups">Group panel</a>, and bind the discount to the desired group'); } $fields->addFieldToTab( 'Root.Main', $field ); } $fields->addFieldToTab( 'Root.Main', $fields->fieldByName('Root.Main.Used') ); $fields->addFieldToTab( 'Root.Main', TextField::create( 'NumCopies', 'Create another "n" copies of promotion coupon.' )->setDescription('Leave blank or input 0 if you only wish to create one') ); $fields->fieldByName('Root.Main.InfiniteUse')->setTitle('This coupon can be used infinitely'); if ($lp = $fields->fieldByName('Root.Main.LifePoint')) { $lp->setTitle('How many times this discount coupon can be used?'); } $fields->removeByName([ 'Products', 'Variants' ]); if ($this->exists()) { if ($this->Type != 'Product') { $fields->addFieldsToTab( 'Root.Products&Variants', [ HeaderField::create( 'ProductDiscountWarning', 'Because the discount type is set to "' . $this->Type . '", the products (and their variants) selected in this tab will not get invovled in the discount calculation.' ) ] ); } $fields->addFieldsToTab( 'Root.Products&Variants', [ HeaderField::create( 'PVHeading', 'Apply this discount to products and their vairants' ), LiteralField::create( 'PSF', ViewableData::create()->customise([ 'DiscountID' => $this->ID, 'Existings' => $this->getBoundProductData(), 'Variants' => json_encode($this->Variants()->column('ID')) ])->renderWith("{$this->ClassName}_Vue") ) ] ); } $fields->fieldByName('Root.Main.NumItemsToMeetCondition')->displayIf('Type')->isEqualTo('Item Count')->end(); $fields->fieldByName('Root.Main.CouponCode')->hideIf('Type')->isEqualTo('Item Count')->end(); $fields->fieldByName('Root.Main.InfiniteUse')->hideIf('Type')->isEqualTo('Item Count')->end(); $fields->fieldByName('Root.Main.NumCopies')->hideIf('Type')->isEqualTo('Item Count')->end(); $fields->fieldByName('Root.Main.LifePoint')->hideIf('Type')->isEqualTo('Item Count')->orIf('InfiniteUse')->isChecked()->end(); $fields->fieldByName('Root.Main.Used')->hideIf('Type')->isEqualTo('Item Count')->orIf('InfiniteUse')->isChecked()->end(); return $fields; } private function getBoundProductData() { $products = $this->Products(); $data = []; foreach ($products as $product) { $data[] = [ 'id' => $product->ID, 'title' => $product->Title, 'variants' => $product->Variants()->Data ]; } return json_encode($data); } public function calc_discount($amount, $order = null) { if ($this->hasMethod('ExtendedDiscountCalculator')) { return $this->ExtendedDiscountCalculator($amount, $order); } if ($order && empty($amount)) { $amount = $order->TotalAmount; } if ($this->DiscountBy == 'ByPercentage') { return $amount * $this->DiscountRate * 0.01; } return $this->DiscountRate; } public function getData() { if (!$this->exists()) return null; $data = [ 'title' => $this->Title, 'by' => $this->DiscountBy == 'ByPercentage' ? '%' : '-', 'rate' => (float) $this->DiscountRate, 'code' => $this->CouponCode, 'desc' => $this->getDescription(), 'cancellable' => $this->Type != 'Item Count' ]; $this->extend('CustomGetData', $data); return $data; } public function getDescription() { return $this->DiscountBy == 'ByPercentage' ? (((float) $this->DiscountRate) . '% off') : ('-$' . number_format($this->DiscountRate, 2)); } public function isValid() { if ($this->Used) { return false; } $valid_from = strtotime($coupon->ValidFrom); $valid_until = strtotime($coupon->ValidUntil); if (!empty($valid_from) && $valid_from > time()) { return false; } if (!empty($valid_until) && $valid_until < time()) { return false; } if (!$this->InfiniteUse && $this->LifePoint <= 0) { return false; } return true; } public function validate() { $result = parent::validate(); if ($this->Type == 'Item Count' && ($discount = Discount::get()->filter(['Type' => 'Item Count'])->first())) { if ($discount->ID != $this->ID) { $result->addError("You already have one 'Item count' type of discount!"); } } return $result; } public function CheckOrder(&$order) { if ($this->NumItemsToMeetCondition <= $order->ItemCount()) { return true; } return false; } public static function check_valid($promo_code) { if ($coupon = Discount::get()->filter(['CouponCode' => $promo_code, 'Used' => false])->first()) { $valid_from = strtotime($coupon->ValidFrom); $valid_until = strtotime($coupon->ValidUntil); if (!empty($valid_from) && $valid_from > time()) { return null; } if (!empty($valid_until) && $valid_until < time()) { return null; } if (!$coupon->InfiniteUse && $coupon->LifePoint <= 0) { return null; } return $coupon; } return null; } /** * Event handler called before writing to the database. */ public function onBeforeWrite() { parent::onBeforeWrite(); if ($this->Type == 'Item Count') { $this->InfiniteUse = true; $this->Used = false; } else { if ($this->InfiniteUse && $this->Used) { $this->Used = false; } if (!$this->InfiniteUse && $this->LifePoint <= 0) { $this->Used = true; } } } /** * Event handler called after writing to the database. */ public function onAfterWrite() { parent::onAfterWrite(); if (!empty($this->NumCopies)) { $n = $this->NumCopies; for ($i = 0; $i < $n; $i++) { $coupon = Discount::create(); $coupon->Title = $this->Title; $coupon->DiscountBy = $this->DiscountBy; $coupon->DiscountRate = $this->DiscountRate; $coupon->CouponCode = strtoupper(substr(sha1(rand()), 0, 8)); $coupon->write(); } $this->NumCopies = 0; $this->write(); } } public function doProductDiscount($order) { $eligible_variants = $this->Variants()->column('ID'); $list = ['discounted_items' => []]; foreach ($this->Products() as $product) { $product_variants = $product->Variants()->column('ID'); if (empty(array_intersect($eligible_variants, $product_variants))) { $eligible_variants = array_merge($eligible_variants, $product_variants); } } foreach ($order->Variants() as $item) { if ($item->NoDiscount) { continue; } if (in_array($item->ID, $eligible_variants)) { $amount = $item->Quantity * $item->Price; if ($this->owner->DiscountBy == 'ByPercentage') { $amount = $amount * $this->owner->DiscountRate * 0.01; } else { $amount = ($amount - $this->owner->DiscountRate >= 0) ? $amount - $this->owner->DiscountRate : 0; } $list['discounted_items'][] = array_merge( $item->Data, ['DiscountedAmount' => number_format($amount, 2)] ); } } if (empty($list['discounted_items'])) { return Controller::curr()->httpError(402, 'No eligible items found in your cart!'); } return $list; } } |