public function getCMSFields()
 {
     $fields = parent::getCMSFields();
     $fields->replaceField("From", $from = new DateField("From", "Valid From - add any date and time"));
     $fields->replaceField("Until", $until = new DateField("Until", "Valid Until - add any date and time"));
     $fields->replaceField("NewPrice", new CurrencyField("NewPrice", "PRICE (OPTION 1 / 3) - only enter if there is a set new price independent of the 'standard' price."));
     $fields->replaceField("Percentage", new NumericField("Percentage", "PERCENTAGE (OPTIONAL 2/ 3) discount from 0 (0% discount) to 100 (100% discount)."));
     $fields->replaceField("Reduction", new CurrencyField("Reduction", "REDUCTION (OPTION 3 /3 ) - e.g. if you enter 2.00 then the new price will be the standard product price minus 2."));
     if (!$this->ID) {
         $fields->addFieldToTab("Root.Main", new LiteralField("SaveFirst", "<p>Please save first - and then select security groups / countries</p>"));
         $fields->removeByName("NoLongerValid");
     }
     if ($groups = Group::get()->count()) {
         $groups = Group::get();
         $fields->replaceField("Groups", new CheckboxSetField("Groups", "Who", $groups->map()->toArray()));
     } else {
         $fields->removeByName("Groups");
     }
     if ($ecommerceCountries = EcommerceCountry::get()) {
         $fields->replaceField("EcommerceCountries", new CheckboxSetField("EcommerceCountries", "Where", $ecommerceCountries->map()->toArray()));
     } else {
         $fields->removeByName("EcommerceCountries");
     }
     if (DiscountCouponOption::get()->count()) {
         $fields->replaceField("DiscountCouponOptions", new CheckboxSetField("DiscountCouponOptions", "Discount Coupons", DiscountCouponOption::get()->map()->toArray()));
     } else {
         $fields->removeByName("DiscountCouponOptions");
     }
     $from->setConfig('showcalendar', true);
     $until->setConfig('showcalendar', true);
     return $fields;
 }
 /**
  * returns the discount coupon, if any ...
  * @return DiscountCouponOption | null
  **/
 protected function myDiscountCouponOption()
 {
     $coupon = null;
     if ($id = $this->LiveDiscountCouponOptionID()) {
         $coupon = DiscountCouponOption::get()->byID($id);
         if ($coupon) {
             if ($coupon->ApplyPercentageToApplicableProducts) {
                 $arrayOfOrderItemsToWhichThisCouponApplies = $this->applicableProductsArray($coupon);
                 if (count($arrayOfOrderItemsToWhichThisCouponApplies)) {
                     return $coupon;
                 }
             } else {
                 return $coupon;
             }
         }
     }
     return null;
 }
 private function addmymodifiers()
 {
     if (!PickUpOrDeliveryModifierOptions::get()->where("Code = 'pickup'")->First()) {
         $obj = new PickUpOrDeliveryModifierOptions();
         $obj->IsDefault = 1;
         $obj->Code = "pickup";
         $obj->Name = "pickup from Store";
         $obj->MinimumDeliveryCharge = 0;
         $obj->MaximumDeliveryCharge = 0;
         $obj->MinimumOrderAmountForZeroRate = 0;
         $obj->WeightMultiplier = 0;
         $obj->WeightUnit = 0;
         $obj->Percentage = 0;
         $obj->FixedCost = 0;
         $obj->Sort = 0;
         $obj->write();
     }
     $obj = null;
     if (!PickUpOrDeliveryModifierOptions::get()->where("Code = 'delivery'")->First()) {
         $obj = new PickUpOrDeliveryModifierOptions();
         $obj->IsDefault = 0;
         $obj->Code = "delivery";
         $obj->Name = "delivery via Courier Bob";
         $obj->MinimumDeliveryCharge = 0;
         $obj->MaximumDeliveryCharge = 0;
         $obj->MinimumOrderAmountForZeroRate = 0;
         $obj->WeightMultiplier = 0;
         $obj->WeightUnit = 0;
         $obj->Percentage = 0;
         $obj->FixedCost = 13;
         $obj->Sort = 100;
         $obj->write();
     }
     $obj = null;
     if (!PickUpOrDeliveryModifierOptions::get()->where("Code = 'personal'")->First()) {
         $obj = new PickUpOrDeliveryModifierOptions();
         $obj->IsDefault = 1;
         $obj->Code = "personal";
         $obj->Name = "personal delivery";
         $obj->MinimumDeliveryCharge = 0;
         $obj->MaximumDeliveryCharge = 0;
         $obj->MinimumOrderAmountForZeroRate = 0;
         $obj->WeightMultiplier = 0;
         $obj->WeightUnit = 0;
         $obj->Percentage = 0.1;
         $obj->FixedCost = 0;
         $obj->Sort = 0;
         $obj->write();
     }
     $obj = null;
     if (!GSTTaxModifierOptions::get()->where("Code = 'GST'")->First()) {
         $obj = new GSTTaxModifierOptions();
         $obj->CountryCode = "NZ";
         $obj->Code = "GST";
         $obj->Name = "Goods and Services Tax";
         $obj->InclusiveOrExclusive = "Inclusive";
         $obj->Rate = 0.15;
         $obj->PriceSuffix = "";
         $obj->AppliesToAllCountries = false;
         $obj->write();
     }
     $obj = null;
     if (!GSTTaxModifierOptions::get()->where("Code = 'ACT'")->First()) {
         $obj = new GSTTaxModifierOptions();
         $obj->CountryCode = "AU";
         $obj->Code = "ACT";
         $obj->Name = "Australian Carbon Tax";
         $obj->InclusiveOrExclusive = "Exclusive";
         $obj->Rate = 0.05;
         $obj->PriceSuffix = "";
         $obj->AppliesToAllCountries = false;
         $obj->write();
     }
     if (!GSTTaxModifierOptions::get()->where("Code = 'ADD'")->First()) {
         $obj = new GSTTaxModifierOptions();
         $obj->CountryCode = "";
         $obj->Code = "ADD";
         $obj->Name = "Additional Tax";
         $obj->InclusiveOrExclusive = "Inclusive";
         $obj->Rate = 0.65;
         $obj->PriceSuffix = "";
         $obj->DoesNotApplyToAllProducts = true;
         $obj->AppliesToAllCountries = true;
         $obj->write();
     }
     $obj = null;
     if (!DiscountCouponOption::get()->where("\"Code\" = 'AAA'")->First()) {
         $obj = new DiscountCouponOption();
         $obj->Title = "Example Coupon";
         $obj->Code = "AAA";
         $obj->StartDate = date("Y-m-d", strtotime("Yesterday"));
         $obj->EndDate = date("Y-m-d", strtotime("Next Year"));
         $obj->DiscountAbsolute = 10;
         $obj->DiscountPercentage = 7.5;
         $obj->CanOnlyBeUsedOnce = false;
         $obj->write();
     }
 }
 /**
  * Checks if there are coupons with the same code as the current one
  * @return Boolean
  */
 protected function thereAreCouponsWithTheSameCode()
 {
     return DiscountCouponOption::get()->exclude(array("ID" => $this->ID))->filter(array("Code" => $this->Code))->count() ? true : false;
 }