/**
  * Field for editing a {@link FlatFeeTaxRate}.
  * 
  * @return FieldSet
  */
 public function getCMSFields_forPopup()
 {
     $fields = new FieldSet();
     $fields->push(new TextField('Title', _t('FlatFeeTaxRate.LABEL', 'Label')));
     $fields->push(new TextField('Description', _t('FlatFeeTaxRate.DESCRIPTION', 'Description')));
     $countryField = new DropdownField('CountryID', _t('FlatFeeTaxRate.COUNTRY', 'Country'), Country::shipping_countries());
     $fields->push($countryField);
     $rateField = new NumericField('Rate', _t('FlatFeeTaxRate.TAX_RATE', 'Tax rate as a percentage'));
     $fields->push($rateField);
     return $fields;
 }
 /**
  * Field for editing a {@link FlatFeeShippingRate}.
  * 
  * @return FieldSet
  */
 public function getCMSFields_forPopup()
 {
     $fields = new FieldSet();
     $fields->push(new TextField('Title', _t('FlatFeeShippingRate.LABEL', 'Label')));
     $fields->push(new TextField('Description', _t('FlatFeeShippingRate.DESCRIPTION', 'Description')));
     $amountField = new MoneyField(_t('FlatFeeShippingRate.AMOUNT', 'Amount'));
     $amountField->setAllowedCurrencies(Product::$allowed_currency);
     $fields->push($amountField);
     $countryField = new DropdownField('CountryID', _t('FlatFeeShippingRate.COUNTRY', 'Country'), Country::shipping_countries());
     $fields->push($countryField);
     return $fields;
 }
 /**
  * Add fields for shipping address and required fields to the validator.
  * 
  * @param Array $fields Array of fields
  * @param OrderFormValidator $validator Checkout form validator
  */
 private function addShippingAddressFields(&$fields, &$validator)
 {
     $firstNameField = new TextField('Shipping[FirstName]', _t('CheckoutPage.FIRSTNAME', "First Name"));
     $firstNameField->addExtraClass('shipping-firstname');
     $firstNameField->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_FIRSTNAME', "Please enter a first name."));
     $surnameField = new TextField('Shipping[Surname]', _t('CheckoutPage.SURNAME', "Surname"));
     $surnameField->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_SURNAME', "Please enter a surname."));
     $addressField = new TextField('Shipping[Address]', _t('CheckoutPage.ADDRESS1', "Address 1"));
     $addressField->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_ADDRESS', "Please enter an address."));
     $cityField = new TextField('Shipping[City]', _t('CheckoutPage.CITY', "City"));
     $cityField->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_CITY', "Please enter a city."));
     $countryField = new DropdownField('Shipping[Country]', _t('CheckoutPage.COUNTRY', "Country"), Country::shipping_countries());
     $countryField->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_COUNTRY', "Please enter a country."));
     if (!Member::currentUserID() && Geoip::$default_country_code) {
         $countryField->setValue(Geoip::$default_country_code);
     }
     $regions = Region::shipping_regions();
     $regionField = null;
     if (!empty($regions)) {
         $regionField = new RegionField('Shipping[Region]', _t('CheckoutPage.REGION', "Region"));
         $regionField->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_REGION', "Please enter a country."));
     }
     $sameAddressField = new CheckboxField('ShipToBillingAddress', _t('CheckoutPage.SAME_ADDRESS', "to same address?"));
     $sameAddressField->addExtraClass('shipping-same-address');
     $shippingAddressFields = new CompositeField(new HeaderField(_t('CheckoutPage.SHIPPING_ADDRESS', "Shipping Address"), 3), $sameAddressField, $firstNameField, $surnameField, new TextField('Shipping[Company]', _t('CheckoutPage.COMPANY', "Company")), $addressField, new TextField('Shipping[AddressLine2]', _t('CheckoutPage.ADDRESS2', "Address 2")), $cityField, new TextField('Shipping[PostalCode]', _t('CheckoutPage.POSTAL_CODE', "Postal Code")), new TextField('Shipping[State]', _t('CheckoutPage.STATE', "State")), $countryField);
     if ($regionField) {
         $shippingAddressFields->push($regionField);
     }
     $shippingAddressFields->setID('ShippingAddress');
     $fields['ShippingAddress'][] = $shippingAddressFields;
     $validator->addRequiredField('Shipping[FirstName]');
     $validator->addRequiredField('Shipping[Surname]');
     $validator->addRequiredField('Shipping[Address]');
     $validator->addRequiredField('Shipping[City]');
     $validator->addRequiredField('Shipping[Country]');
 }
 /**
  * Fields for CRUD of shipping regions
  * 
  * @see DataObject::getCMSFields()
  */
 function getCMSFields()
 {
     $fields = parent::getCMSFields();
     $countryField = new DropdownField('CountryID', 'Country', Country::shipping_countries());
     $fields->replaceField('CountryID', $countryField);
     return $fields;
 }
 /**
  * Add addresses to this Order at the checkout.
  * 
  * @param Array $data
  */
 function addAddressesAtCheckout(array $data)
 {
     $member = Customer::currentUser() ? Customer::currentUser() : singleton('Customer');
     $order = CartControllerExtension::get_current_order();
     $billingCountries = Country::billing_countries();
     $shippingCountries = Country::shipping_countries();
     $shippingRegions = Region::shipping_regions();
     //If there is a current billing and shipping address, update them, otherwise create new ones
     $existingBillingAddress = $this->BillingAddress();
     $existingShippingAddress = $this->ShippingAddress();
     if ($existingBillingAddress && $existingBillingAddress->exists()) {
         $newData = array();
         if (isset($data['Billing']) && is_array($data['Billing'])) {
             foreach ($data['Billing'] as $fieldName => $value) {
                 $newData[$fieldName] = $value;
             }
         }
         $newData['CountryID'] = $data['Billing']['Country'];
         $newData['CountryName'] = in_array($newData['CountryID'], array_keys($billingCountries)) ? $billingCountries[$newData['CountryID']] : null;
         if ($member->ID) {
             $newData['MemberID'] = $member->ID;
         }
         $existingBillingAddress->update($newData);
         $existingBillingAddress->write();
     } else {
         $billingAddress = new Address();
         $billingAddress->OrderID = $order->ID;
         if ($member->ID) {
             $billingAddress->MemberID = $member->ID;
         }
         $billingAddress->FirstName = $data['Billing']['FirstName'];
         $billingAddress->Surname = $data['Billing']['Surname'];
         $billingAddress->Company = $data['Billing']['Company'];
         $billingAddress->Address = $data['Billing']['Address'];
         $billingAddress->AddressLine2 = $data['Billing']['AddressLine2'];
         $billingAddress->City = $data['Billing']['City'];
         $billingAddress->PostalCode = $data['Billing']['PostalCode'];
         $billingAddress->State = $data['Billing']['State'];
         $billingAddress->CountryID = $data['Billing']['Country'];
         $billingAddress->CountryName = in_array($data['Billing']['Country'], array_keys($billingCountries)) ? $billingCountries[$data['Billing']['Country']] : null;
         $billingAddress->Type = 'Billing';
         $billingAddress->write();
     }
     if ($existingShippingAddress && $existingShippingAddress->exists()) {
         $newData = array();
         if (isset($data['Shipping']) && is_array($data['Shipping'])) {
             foreach ($data['Shipping'] as $fieldName => $value) {
                 $newData[$fieldName] = $value;
             }
         }
         $newData['CountryID'] = $data['Shipping']['Country'];
         $newData['CountryName'] = in_array($newData['CountryID'], array_keys($shippingCountries)) ? $shippingCountries[$newData['CountryID']] : null;
         if (isset($newData['Region']) && isset($shippingRegions[$newData['Country']])) {
             if (in_array($newData['Region'], array_keys($shippingRegions[$newData['Country']]))) {
                 $newData['RegionName'] = $shippingRegions[$newData['Country']][$newData['Region']];
             }
         } else {
             $newData['RegionName'] = null;
         }
         if ($member->ID) {
             $newData['MemberID'] = $member->ID;
         }
         $existingShippingAddress->update($newData);
         $existingShippingAddress->write();
     } else {
         $shippingAddress = new Address();
         $shippingAddress->OrderID = $order->ID;
         if ($member->ID) {
             $shippingAddress->MemberID = $member->ID;
         }
         $shippingAddress->FirstName = $data['Shipping']['FirstName'];
         $shippingAddress->Surname = $data['Shipping']['Surname'];
         $shippingAddress->Company = $data['Shipping']['Company'];
         $shippingAddress->Address = $data['Shipping']['Address'];
         $shippingAddress->AddressLine2 = $data['Shipping']['AddressLine2'];
         $shippingAddress->City = $data['Shipping']['City'];
         $shippingAddress->PostalCode = $data['Shipping']['PostalCode'];
         $shippingAddress->State = $data['Shipping']['State'];
         $shippingAddress->CountryID = $data['Shipping']['Country'];
         $shippingAddress->Region = isset($data['Shipping']['Region']) ? $data['Shipping']['Region'] : null;
         $shippingAddress->CountryName = in_array($data['Shipping']['Country'], array_keys($shippingCountries)) ? $shippingCountries[$data['Shipping']['Country']] : null;
         $shippingAddress->RegionName = isset($data['Shipping']['Region']) && isset($shippingRegions[$data['Shipping']['Country']]) && in_array($data['Shipping']['Region'], array_keys($shippingRegions[$data['Shipping']['Country']])) ? $shippingRegions[$data['Shipping']['Country']][$data['Shipping']['Region']] : null;
         $shippingAddress->Type = 'Shipping';
         $shippingAddress->write();
     }
 }