/**
  * This function returns back the default list of regions, filtered by the currently selected country.
  * @return Array - array of CountryCode => Country
  **/
 protected static function get_default_array()
 {
     $defaultArray = array();
     $regions = DataObject::get("EcommerceRegion", "\"DoNotAllowSales\" <> 1 AND \"CountryID\"  = '" . EcommerceCountry::get_country_id() . "'");
     if ($regions) {
         foreach ($regions as $region) {
             $defaultArray[$region->Code] = $region->Name;
         }
     }
     return $defaultArray;
 }
 function updateCalculatedPrice(&$startingPrice)
 {
     $newPrice = -1;
     $fieldName = $this->owner->ClassName . "ID";
     $singleton = ComplexPriceObject::get()->first();
     if ($singleton) {
         // Check that ComplexPriceObject can be joined to this type of object
         if (!$singleton->hasField($fieldName)) {
             $ancestorArray = ClassInfo::ancestry($this->owner, true);
             foreach ($ancestorArray as $ancestor) {
                 $fieldName = $ancestor . "ID";
                 if ($singleton->hasField($fieldName)) {
                     break;
                 }
             }
         }
         // Load up the alternate prices for this product
         $prices = ComplexPriceObject::get()->filter(array($fieldName => $this->owner->ID, "NoLongerValid" => 0))->sort("NewPrice", "DESC");
         $memberGroupsArray = array();
         if ($prices->count()) {
             // Load up the groups for the current memeber, if any
             if ($member = Member::currentUser()) {
                 if ($memberGroupComponents = $member->getManyManyComponents('Groups')) {
                     if ($memberGroupComponents && $memberGroupComponents->count()) {
                         $memberGroupsArray = $memberGroupComponents->column("ID");
                         if (!is_array($memberGroupsArray)) {
                             $memberGroupsArray = array();
                         }
                     }
                 }
             }
             $countryID = EcommerceCountry::get_country_id();
             // Look at each price and see if it can be used
             foreach ($prices as $price) {
                 $priceCanBeUsed = true;
                 // Does it pass the group filter?
                 if ($priceGroupComponents = $price->getManyManyComponents('Groups')) {
                     if ($priceGroupComponents && $priceGroupComponents->count()) {
                         $priceCanBeUsed = false;
                         $priceGroupArray = $priceGroupComponents->column("ID");
                         if (!is_array($priceGroupArray)) {
                             $priceGroupArray = array();
                         }
                         $interSectionArray = array_intersect($priceGroupArray, $memberGroupsArray);
                         if (is_array($interSectionArray) && count($interSectionArray)) {
                             $priceCanBeUsed = true;
                         }
                     }
                 }
                 // Does it pass the country filter?
                 if ($priceCanBeUsed) {
                     if ($priceCountryComponents = $price->getManyManyComponents('EcommerceCountries')) {
                         if ($priceCountryComponents && $priceCountryComponents->count()) {
                             $priceCanBeUsed = false;
                             $priceCountryArray = $priceCountryComponents->column("ID");
                             if (!is_array($priceCountryArray)) {
                                 $priceCountryArray = array();
                             }
                             if ($countryID && in_array($countryID, $priceCountryArray)) {
                                 $priceCanBeUsed = true;
                             }
                         }
                     }
                 }
                 // Does it pass the date filter?
                 if ($priceCanBeUsed) {
                     $nowTS = strtotime("now");
                     if ($price->From) {
                         $priceCanBeUsed = false;
                         $fromTS = strtotime($price->From);
                         if ($fromTS && $fromTS < $nowTS) {
                             $priceCanBeUsed = true;
                         }
                     }
                 }
                 if ($priceCanBeUsed) {
                     if ($price->Until) {
                         $priceCanBeUsed = false;
                         $untilTS = strtotime($price->Until);
                         if ($untilTS && $untilTS > $nowTS) {
                             $priceCanBeUsed = true;
                         }
                     }
                 }
                 // If so, apply the price
                 if ($priceCanBeUsed) {
                     $newPrice = $price->getCalculatedPrice();
                 }
             }
         }
     }
     if ($newPrice > -1) {
         $startingPrice = $newPrice;
     }
     return $startingPrice;
 }
 /**
  * This function returns back the default list of regions, filtered by the currently selected country.
  * @return Array - array of Region.ID => Region.Name
  **/
 protected static function get_default_array()
 {
     $defaultArray = array();
     $regions = EcommerceRegion::get()->Exclude(array("DoNotAllowSales" => 1));
     $defaultRegion = EcommerceCountry::get_country_id();
     if ($defaultRegion) {
         $regions = $regions->Filter(array("CountryID" => EcommerceCountry::get_country_id()));
     }
     if ($regions && $regions->count()) {
         foreach ($regions as $region) {
             $defaultArray[$region->ID] = $region->Name;
         }
     }
     return $defaultArray;
 }
 /**
  * description of region and country being shipped to.
  * @return String
  */
 protected function LiveRegionAndCountry()
 {
     $details = array();
     $option = $this->Option();
     if ($option) {
         $regionID = EcommerceRegion::get_region_id();
         if ($regionID) {
             $region = EcommerceRegion::get()->byID($regionID);
             if ($region) {
                 $details[] = $region->Name;
             }
         }
         $countryID = EcommerceCountry::get_country_id();
         if ($countryID) {
             $country = EcommerceCountry::get()->byID($countryID);
             if ($country) {
                 $details[] = $country->Name;
             }
         }
     } else {
         return _t("PickUpOrDeliveryModifier.NOTSELECTED", "No delivery option has been selected");
     }
     if (count($details)) {
         return implode(", ", $details);
     }
 }