/**
  * Returns the default tax rate
  * 
  * @return SilvercartTax
  */
 public static function getDefault()
 {
     $defaultTaxRate = SilvercartTax::get()->filter('IsDefault', '1')->first();
     return $defaultTaxRate;
 }
 /**
  * Returns the SilvercartTax object with the highest tax value for the
  * given taxes.
  *
  * @param array $taxes The tax rates array (associative)
  *
  * @return SilvercartTax
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 16.06.2014
  */
 public function getMostValuableTaxRate($taxes = null)
 {
     if (is_null($taxes)) {
         $taxes = $this->getTaxRatesWithoutFeesAndCharges('SilvercartVoucher');
     }
     $highestTaxValue = 0;
     $mostValuableTaxRate = null;
     $originalMostValuableTaxRate = null;
     foreach ($taxes as $tax) {
         if ($tax->AmountRaw >= $highestTaxValue) {
             $highestTaxValue = $tax->AmountRaw;
             $mostValuableTaxRate = $tax->Rate;
             $originalMostValuableTaxRate = $tax->OriginalRate;
         }
     }
     if (!is_null($originalMostValuableTaxRate)) {
         $silvercartTax = SilvercartTax::get()->filter('Rate', $originalMostValuableTaxRate)->first();
         if ($silvercartTax) {
             if ($originalMostValuableTaxRate != $mostValuableTaxRate) {
                 $silvercartTax->Rate = $mostValuableTaxRate;
                 $silvercartTax->setI18nTitle($mostValuableTaxRate . '%');
             }
             return $silvercartTax;
         }
     }
     return false;
 }
 /**
  * creates test tax rates on /dev/build or creating test data in ModelAdmin.
  *
  * @return void
  *
  * @author Sebastian Diel <*****@*****.**>
  * @since 02.07.2011
  */
 public static function createTestTaxRates()
 {
     if (self::$enableTestData === true) {
         // create two standard german tax rates if no tax rate exists
         $taxRate = SilvercartTax::get()->first();
         if (!$taxRate) {
             $taxrates = array('19' => array('en_US' => '19%', 'en_GB' => '19%', 'de_DE' => '19%'), '7' => array('en_US' => '7%', 'en_GB' => '7%', 'de_DE' => '7%'));
             $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) {
                 $taxrates[0][$fallbackLocale] = $taxrates[0]['en_US'];
                 $taxrates[1][$fallbackLocale] = $taxrates[1]['en_US'];
             }
             foreach ($taxrates as $taxrate => $languages) {
                 $rateObj = new SilvercartTax();
                 $rateObj->Rate = $taxrate;
                 $rateObj->write();
                 foreach ($languages as $locale => $title) {
                     $rateLanguage = SilvercartTaxLanguage::get()->filter(array('Locale' => $locale, 'SilvercartTaxID' => $rateObj->ID))->first();
                     if (!$rateLanguage) {
                         $rateLanguage = new SilvercartTaxLanguage();
                         $rateLanguage->Locale = $locale;
                         $rateLanguage->SilvercartTaxID = $rateObj->ID;
                     }
                     $rateLanguage->Title = $title;
                     $rateLanguage->write();
                 }
             }
         }
     }
 }
 /**
  * Returns handling costs for this payment method
  *
  * @return Money a money object
  */
 public function getHandlingCost()
 {
     $controller = Controller::curr();
     $member = SilvercartCustomer::currentRegisteredCustomer();
     $handlingCostToUse = false;
     if (method_exists($controller, 'getAddress')) {
         // 1) Use shipping address from checkout
         $shippingAddress = $controller->getAddress('Shipping');
     } else {
         if ($member && $member->ShippingAddressID > 0) {
             // 2) Use customer's default shipping address
             $shippingAddress = $member->ShippingAddress();
         } else {
             // 3) Generate shipping address with shop's default country
             $currentShopLocale = i18n::get_lang_from_locale(i18n::get_locale());
             $shippingAddress = new SilvercartAddress();
             $shippingAddress->SilvercartCountry = SilvercartCountry::get()->filter('ISO2', strtoupper($currentShopLocale))->first();
         }
     }
     if (!$shippingAddress) {
         return false;
     }
     $zonesDefined = false;
     foreach ($this->SilvercartHandlingCosts() as $handlingCost) {
         $zone = $handlingCost->SilvercartZone();
         if ($zone->SilvercartCountries()->find('ISO3', $shippingAddress->SilvercartCountry()->ISO3)) {
             $handlingCostToUse = $handlingCost;
             $zonesDefined = true;
             break;
         }
     }
     // Fallback if SilvercartHandlingCosts are available but no zone is defined
     if (!$zonesDefined) {
         if ($this->SilvercartHandlingCosts()->Count() > 0) {
             $handlingCostToUse = $this->SilvercartHandlingCosts()->First();
         } else {
             $silvercartTax = SilvercartTax::get()->filter('isDefault', 1)->first();
             $handlingCostToUse = new SilvercartHandlingCost();
             $handlingCostToUse->SilvercartPaymentMethod = $this;
             $handlingCostToUse->SilvercartTax = $silvercartTax;
             $handlingCostToUse->SilvercartTaxID = $silvercartTax->ID;
             $handlingCostToUse->amount = new Money();
             $handlingCostToUse->amount->setAmount(0);
             $handlingCostToUse->amount->setCurrency(SilvercartConfig::DefaultCurrency());
         }
     }
     return $handlingCostToUse;
 }