/**
  * Customizes the backends fields, mainly for ModelAdmin
  *
  * @return FieldList the fields for the backend
  * 
  * @author Roland Lehmann <*****@*****.**>, Sebastian Diel <*****@*****.**>
  * @since 13.02.2013
  */
 public function getCMSFields()
 {
     $this->getCMSFieldsIsCalled = true;
     $fields = parent::getCMSFields();
     $postPricingField = $fields->dataFieldByName('PostPricing');
     $postPricingField->setTitle($postPricingField->Title() . ' (' . $this->fieldLabel('PostPricingInfo') . ')');
     SilvercartTax::presetDropdownWithDefault($fields->dataFieldByName('SilvercartTaxID'), $this);
     $fieldGroup = new SilvercartFieldGroup('ShippingFeeGroup', '', $fields);
     $fieldGroup->push($fields->dataFieldByName('MaximumWeight'));
     $fieldGroup->pushAndBreak($fields->dataFieldByName('UnlimitedWeight'));
     $fieldGroup->push($fields->dataFieldByName('Price'));
     $fieldGroup->pushAndBreak($fields->dataFieldByName('SilvercartTaxID'));
     $fieldGroup->pushAndBreak($postPricingField);
     // only the carriers zones must be selectable
     $leftJoinTable = 'SilvercartZone_SilvercartCarriers';
     $leftJoinOn = '"SilvercartZone"."ID" = "SilvercartZone_SilvercartCarriers"."SilvercartZoneID"';
     $where = sprintf("\"SilvercartZone_SilvercartCarriers\".\"SilvercartCarrierID\" = %s", $this->SilvercartShippingMethod()->SilvercartCarrier()->ID);
     $zones = SilvercartZone::get()->leftJoin($leftJoinTable, $leftJoinOn)->where($where);
     if ($zones->exists()) {
         $zonesMap = $zones->map('ID', 'Title');
         $zonesField = new DropdownField('SilvercartZoneID', _t('SilvercartShippingFee.ZONE_WITH_DESCRIPTION', 'zone (only carrier\'s zones available)'), $zonesMap->toArray());
         $fieldGroup->push($zonesField);
     }
     $fieldGroup->breakAndPush($fields->dataFieldByName('freeOfShippingCostsDisabled'));
     $fieldGroup->breakAndPush($fields->dataFieldByName('freeOfShippingCostsFrom'));
     $fields->dataFieldByName('DeliveryTimeMin')->setRightTitle($this->fieldLabel('DeliveryTimeMinDesc'));
     $fields->dataFieldByName('DeliveryTimeMax')->setRightTitle($this->fieldLabel('DeliveryTimeMaxDesc'));
     $fields->dataFieldByName('DeliveryTimeText')->setRightTitle($this->fieldLabel('DeliveryTimeTextDesc'));
     $parentDeliveryTime = '';
     if ($this->SilvercartShippingMethod()->exists()) {
         $parentDeliveryTime = '<br/>(' . SilvercartShippingMethod::get_delivery_time($this->SilvercartShippingMethod(), true) . ')';
     }
     $fieldGroup->pushAndBreak(new LiteralField('DeliveryTimeHint', '<strong>' . $this->fieldLabel('DeliveryTimeHint') . $parentDeliveryTime . '</strong>'));
     $fieldGroup->push($fields->dataFieldByName('DeliveryTimeMin'));
     $fieldGroup->pushAndBreak($fields->dataFieldByName('DeliveryTimeMax'));
     $fieldGroup->pushAndBreak($fields->dataFieldByName('DeliveryTimeText'));
     $fields->addFieldToTab('Root.Main', $fieldGroup);
     return $fields;
 }
 /**
  * creates test configuration data on /dev/build or by adding test
  * configuration in ModelAdmin.
  *
  * @return bool
  *
  * @author Sebastian Diel <*****@*****.**>
  * @since 02.07.2011
  */
 public static function createTestConfiguration()
 {
     if (self::$enableTestData === true) {
         //create a carrier and an associated zone and shipping method
         $carrier = SilvercartCarrier::get()->first();
         if (!$carrier) {
             self::createTestTaxRates();
             $carrier = new SilvercartCarrier();
             $carrier->Title = 'DHL';
             $carrier->FullTitle = 'DHL International GmbH';
             $carrier->write();
             $carrierLanguages = array('en_GB' => array('Title' => 'DHL', 'FullTitle' => 'DHL International GmbH'), 'en_US' => array('Title' => 'DHL', 'FullTitle' => 'DHL International GmbH'), 'de_DE' => array('Title' => 'DHL', 'FullTitle' => 'DHL International GmbH'));
             $locales = array('de_DE', 'en_GB', 'en_US');
             $fallbackLocale = false;
             if (!in_array(Translatable::get_current_locale(), $locales)) {
                 $locales[] = Translatable::get_current_locale();
                 $fallbackLocale = Translatable::get_current_locale();
             }
             if ($fallbackLocale !== false) {
                 $carrierLanguages[$fallbackLocale] = $carrierLanguages['en_US'];
             }
             foreach ($carrierLanguages as $locale => $attributes) {
                 $languageObj = SilvercartCarrierLanguage::get()->filter(array('SilvercartCarrierID' => $carrier->ID, 'Locale' => $locale))->first();
                 if (!$languageObj) {
                     $languageObj = new SilvercartCarrierLanguage();
                     $languageObj->Locale = $locale;
                     $languageObj->SilvercartCarrierID = $carrier->ID;
                 }
                 foreach ($attributes as $attribute => $value) {
                     $languageObj->{$attribute} = $value;
                 }
                 $languageObj->write();
             }
             //relate carrier to zones
             $zoneDomestic = SilvercartZone::get()->first();
             if (!$zoneDomestic) {
                 $zones = array(array('en_GB' => 'Domestic', 'en_US' => 'Domestic', 'de_DE' => 'Inland'), array('en_GB' => 'EU', 'en_US' => 'European Union', 'de_DE' => 'EU'));
                 $locales = array('de_DE', 'en_GB', 'en_US');
                 $fallbackLocale = false;
                 if (!in_array(Translatable::get_current_locale(), $locales)) {
                     $locales[] = Translatable::get_current_locale();
                     $fallbackLocale = Translatable::get_current_locale();
                 }
                 if ($fallbackLocale !== false) {
                     $zones[0][$fallbackLocale] = $zones[0]['en_US'];
                     $zones[1][$fallbackLocale] = $zones[1]['en_US'];
                 }
                 foreach ($zones as $zone) {
                     $zoneObj = new SilvercartZone();
                     $zoneObj->write();
                     $zoneObj->SilvercartCarriers()->add($carrier);
                     $zoneObj->write();
                     foreach ($zone as $locale => $title) {
                         $zoneLanguage = SilvercartZoneLanguage::get()->filter(array('SilvercartZoneID' => $zoneObj->ID, 'Locale' => $locale))->first();
                         if (!$zoneLanguage) {
                             $zoneLanguage = new SilvercartZoneLanguage();
                             $zoneLanguage->SilvercartZoneID = $zoneObj->ID;
                             $zoneLanguage->Locale = $locale;
                         }
                         $zoneLanguage->Title = $title;
                         $zoneLanguage->write();
                     }
                 }
             }
             //Retrieve the active country if exists
             $country = SilvercartCountry::get()->filter('Active', '1')->first();
             if (!$country) {
                 //Retrieve the country dynamically depending on the installation language
                 $installationLanguage = i18n::get_locale();
                 $ISO2 = substr($installationLanguage, -2);
                 $country = SilvercartCountry::get()->filter('ISO2', $ISO2)->first();
                 if (!$country) {
                     $country = new SilvercartCountry();
                     $country->Title = 'Testcountry';
                     $country->ISO2 = $ISO2;
                     $country->ISO3 = $ISO2;
                 }
                 $country->Active = true;
                 $country->write();
             }
             $zoneDomestic = DataObject::get_by_id('SilvercartZone', 1);
             $zoneDomestic->SilvercartCountries()->add($country);
             // create if not exists, activate and relate payment method
             $paymentMethod = SilvercartPaymentPrepayment::get()->first();
             if (!$paymentMethod) {
                 $paymentMethodHandler = new SilvercartPaymentMethod();
                 $paymentMethodHandler->requireDefaultRecords();
             }
             $paymentMethod = SilvercartPaymentPrepayment::get()->first();
             $paymentMethod->isActive = true;
             $orderStatusPending = SilvercartOrderStatus::get()->filter('Code', 'pending')->first();
             if ($orderStatusPending) {
                 $paymentMethod->orderStatus = $orderStatusPending->Code;
             }
             $paymentMethod->write();
             $country->SilvercartPaymentMethods()->add($paymentMethod);
             // create a shipping method
             $shippingMethod = SilvercartShippingMethod::get()->first();
             if (!$shippingMethod) {
                 $shippingMethod = new SilvercartShippingMethod();
                 //relate shipping method to carrier
                 $shippingMethod->SilvercartCarrierID = $carrier->ID;
             }
             $shippingMethod->isActive = 1;
             $shippingMethod->write();
             $shippingMethod->SilvercartZones()->add($zoneDomestic);
             //create the language objects for the shipping method
             $shippingMethodTranslations = array('de_DE' => 'Paket', 'en_GB' => 'Package', 'en_US' => 'Package');
             $locales = array('de_DE', 'en_GB', 'en_US');
             $fallbackLocale = false;
             if (!in_array(Translatable::get_current_locale(), $locales)) {
                 $locales[] = Translatable::get_current_locale();
                 $fallbackLocale = Translatable::get_current_locale();
             }
             if ($fallbackLocale !== false) {
                 $shippingMethodTranslations[$fallbackLocale] = $shippingMethodTranslations['en_US'];
             }
             foreach ($shippingMethodTranslations as $locale => $title) {
                 $shippingMethodLanguage = SilvercartShippingMethodLanguage::get()->filter(array('Locale' => $locale, 'SilvercartShippingMethodID' => $shippingMethod->ID))->first();
                 if (!$shippingMethodLanguage) {
                     $shippingMethodLanguage = new SilvercartShippingMethodLanguage();
                     $shippingMethodLanguage->Locale = $locale;
                     $shippingMethodLanguage->SilvercartShippingMethodID = $shippingMethod->ID;
                 }
                 $shippingMethodLanguage->Title = $title;
                 $shippingMethodLanguage->write();
             }
             // create a shipping fee and relate it to zone, tax and shipping method
             $shippingFee = SilvercartShippingFee::get()->first();
             if (!$shippingFee) {
                 $shippingFee = new SilvercartShippingFee();
                 $shippingFee->MaximumWeight = '100000';
                 $shippingFee->UnlimitedWeight = true;
                 $shippingFee->Price = new Money();
                 $shippingFee->Price->setAmount('3.9');
                 $shippingFee->Price->setCurrency('EUR');
             }
             $shippingFee->SilvercartShippingMethodID = $shippingMethod->ID;
             $shippingFee->SilvercartZoneID = $zoneDomestic->ID;
             $higherTaxRate = SilvercartTax::get()->filter('Rate', '19')->first();
             $shippingFee->SilvercartTaxID = $higherTaxRate->ID;
             $shippingFee->write();
             return true;
         }
     }
     return false;
 }
 /**
  * determins the right shipping fee for a shipping method depending on the
  * cart's weight and the country of the customers shipping address
  * 
  * @param int $weight Weight in gramm to get fee for
  *
  * @return SilvercartShippingFee the most convenient shipping fee for this shipping method
  * 
  * @author Roland Lehmann <*****@*****.**>,
  *         Sebastian Diel <*****@*****.**>
  * @since 15.11.2014
  */
 public function getShippingFee($weight = null)
 {
     $fee = false;
     if (is_null($weight)) {
         if (!SilvercartCustomer::currentUser() || !SilvercartCustomer::currentUser()->getCart()) {
             return $fee;
         }
         $weight = SilvercartCustomer::currentUser()->getCart()->getWeightTotal();
     }
     $shippingCountry = $this->getShippingCountry();
     if (is_null($shippingCountry)) {
         $shippingAddress = $this->getShippingAddress();
         if (is_null($shippingAddress) && method_exists(Controller::curr(), 'getShippingAddress')) {
             $shippingAddress = Controller::curr()->getShippingAddress();
             $this->setShippingAddress($shippingAddress);
             SilvercartTools::Log('getShippingFee', 'CAUTION: shipping address was not preset! Fallback to current controller ' . Controller::curr()->class, 'SilvercartShippingMethod');
         }
         if ($shippingAddress instanceof SilvercartAddress) {
             $shippingCountry = $shippingAddress->SilvercartCountry();
             $this->setShippingCountry($shippingCountry);
         }
     }
     if ($shippingCountry instanceof SilvercartCountry) {
         $zones = SilvercartZone::getZonesFor($shippingCountry->ID);
         if ($zones->exists()) {
             $zoneMap = $zones->map('ID', 'ID');
             $zoneIDs = $zoneMap->toArray();
             $zoneIDsAsString = "'" . implode("','", $zoneIDs) . "'";
             $filter = array("SilvercartShippingMethodID" => $this->ID);
             $fees = SilvercartShippingFee::get()->filter($filter)->where(sprintf('("MaximumWeight" >= ' . $weight . ' OR "UnlimitedWeight" = 1) AND "SilvercartZoneID" IN (%s)', $zoneIDsAsString))->sort('PostPricing, PriceAmount');
             if ($fees->exists()) {
                 $fee = $fees->first();
             }
         }
     }
     return $fee;
 }