function run($request)
 {
     $db = DB::getConn();
     if ($db instanceof PostgreSQLDatabase) {
         $exist = DB::query("SELECT column_name FROM information_schema.columns WHERE table_name ='PickUpOrDeliveryModifier' AND column_name = 'PickupOrDeliveryType'")->numRecords();
     } else {
         // default is MySQL - broken for others, each database conn type supported must be checked for!
         $exist = DB::query("SHOW COLUMNS FROM \"PickUpOrDeliveryModifier\" LIKE 'PickupOrDeliveryType'")->numRecords();
     }
     if ($exist > 0) {
         $defaultOption = PickUpOrDeliveryModifierOptions::get()->filter(array("IsDefault" => 1))->First();
         $modifiers = PickUpOrDeliveryModifier::get()->filter(array("OptionID" => 0));
         if ($modifiers->count()) {
             foreach ($modifiers as $modifier) {
                 if (!isset($modifier->OptionID) || !$modifier->OptionID) {
                     if (!isset(self::$options_old_to_new[$modifier->Code])) {
                         $option = PickUpOrDeliveryModifierOptions::get()->filter(array("Code" => $modifier->Code))->First();
                         if (!$option) {
                             $option = $defaultOption;
                         }
                         self::$options_old_to_new[$modifier->Code] = $option->ID;
                     }
                     $myOption = self::$options_old_to_new[$modifier->Code];
                     // USING QUERY TO UPDATE
                     DB::query("UPDATE \"PickUpOrDeliveryModifier\" SET \"OptionID\" = " . $myOption . " WHERE \"PickUpOrDeliveryModifier\".\"ID\" = " . $modifier->ID);
                     DB::alteration_message('Updated modifier #' . $modifier->ID . ' from code to option ID ' . $myOption, 'edited');
                 }
             }
         }
     }
     DB::alteration_message("<hr />COMPLETED<hr />", "created");
 }
 /**
  * make sure all are unique codes
  */
 function onBeforeWrite()
 {
     parent::onBeforeWrite();
     $this->Code = trim(preg_replace("/[^a-zA-Z0-9]+/", "", $this->Code));
     $i = 0;
     if (!$this->Code) {
         $defaults = $this->Config()->get("Code");
         $this->Code = empty($defaults["Code"]) ? "CODE" : $defaults["Code"];
     }
     $baseCode = $this->Code;
     while (PickUpOrDeliveryModifierOptions::get()->filter(array("Code" => $this->Code))->exclude(array("ID" => $this->ID))->count() && $i < 100) {
         $i++;
         $this->Code = $baseCode . '_' . $i;
     }
     if ($this->MinimumDeliveryCharge && $this->MaximumDeliveryCharge) {
         if ($this->MinimumDeliveryCharge > $this->MaximumDeliveryCharge) {
             $this->MinimumDeliveryCharge = $this->MaximumDeliveryCharge;
         }
     }
 }
 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();
     }
 }
 function processOrderModifier($data, $form = null)
 {
     if (isset($data['PickupOrDeliveryType'])) {
         $newOption = intval($data['PickupOrDeliveryType']);
         $newOptionObj = PickUpOrDeliveryModifierOptions::get()->byID($newOption);
         if ($newOptionObj) {
             $order = ShoppingCart::current_order();
             if ($order) {
                 if ($modifiers = $order->Modifiers("PickUpOrDeliveryModifier")) {
                     foreach ($modifiers as $modifier) {
                         $modifier->setOption($newOption);
                         $modifier->runUpdate();
                     }
                     return ShoppingCart::singleton()->setMessageAndReturn(_t("PickUpOrDeliveryModifier.UPDATED", "Delivery option updated"), "good");
                 }
             }
         }
     }
     return ShoppingCart::singleton()->setMessageAndReturn(_t("PickUpOrDeliveryModifier.UPDATED", "Delivery option could NOT be updated"), "bad");
 }