function testNiceFormatting() { // Test a bunch of different data values and results in Nice() and Whole() $tests = array( // Test basic operation '$50.00' => array('$50.00', '$50'), // Test removal of junk text 'this is -50.29 dollars' => array('($50.29)', '($50)'), 'this is -50.79 dollars' => array('($50.79)', '($51)'), 'this is 50.79 dollars' => array('$50.79', '$51'), // Test negative numbers '-1000' => array('($1,000.00)','($1,000)'), '-$2,000' => array('($2,000.00)', '($2,000)'), // Test thousands comma '5000' => array('$5,000.00', '$5,000'), // Test scientific notation '5.68434188608E-14' => array('$0.00', '$0'), '5.68434188608E7' => array('$56,843,418.86', '$56,843,419'), "Sometimes Es are still bad: 51 dollars, even though they\'re used in scientific notation" => array('$51.00', '$51'), "What about 5.68434188608E7 in the middle of a string" => array('$56,843,418.86', '$56,843,419'), ); foreach($tests as $value => $niceValues) { $c = new Currency('MyField'); $c->setValue($value); $this->assertEquals($niceValues[0], $c->Nice()); $this->assertEquals($niceValues[1], $c->Whole()); } }
public function __construct($controller, $name = "PostagePaymentForm") { // Get delivery data and postage areas from session $delivery_data = Session::get("Commerce.DeliveryDetailsForm.data"); $country = $delivery_data['DeliveryCountry']; $postcode = $delivery_data['DeliveryPostCode']; $postage_areas = $controller->getPostageAreas($country, $postcode); // Loop through all postage areas and generate a new list $postage_array = array(); foreach ($postage_areas as $area) { $area_currency = new Currency("Cost"); $area_currency->setValue($area->Cost); $postage_array[$area->ID] = $area->Title . " (" . $area_currency->Nice() . ")"; } $postage_id = Session::get('Commerce.PostageID') ? Session::get('Commerce.PostageID') : 0; // Setup postage fields $postage_field = CompositeField::create(HeaderField::create("PostageHeader", _t('Commerce.Postage', "Postage")), OptionsetField::create("PostageID", _t('Commerce.PostageSelection', 'Please select your prefered postage'), $postage_array)->setValue($postage_id))->setName("PostageFields")->addExtraClass("unit")->addExtraClass("size1of2")->addExtraClass("unit-50"); // Get available payment methods and setup payment $payment_methods = SiteConfig::current_site_config()->PaymentMethods(); // Deal with payment methods if ($payment_methods->exists()) { $payment_map = $payment_methods->map('ID', 'Label'); $payment_value = $payment_methods->filter('Default', 1)->first()->ID; } else { $payment_map = array(); $payment_value = 0; } $payment_field = CompositeField::create(HeaderField::create('PaymentHeading', _t('Commerce.Payment', 'Payment'), 2), OptionsetField::create('PaymentMethodID', _t('Commerce.PaymentSelection', 'Please choose how you would like to pay'), $payment_map, $payment_value))->setName("PaymentFields")->addExtraClass("unit")->addExtraClass("size1of2")->addExtraClass("unit-50"); $fields = FieldList::create(CompositeField::create($postage_field, $payment_field)->setName("PostagePaymentFields")->addExtraClass("units-row")->addExtraClass("line")); $back_url = $controller->Link("billing"); $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red commerce-action-back">' . _t('Commerce.Back', 'Back') . '</a>'), FormAction::create('doContinue', _t('Commerce.PaymentDetails', 'Enter Payment Details'))->addExtraClass('btn')->addExtraClass('commerce-action-next')->addExtraClass('btn-green')); $validator = RequiredFields::create(array("PostageID", "PaymentMethod")); parent::__construct($controller, $name, $fields, $actions, $validator); }
public function CalGas(GasPlan &$plan) { $daily = $plan->DailyCharge; $rate = $plan->Rate; $ppd = $plan->PPD; $amount = $this->gasAmount; $value = ($daily * 30 / 100 + $rate * $amount) * 1.15 * (1 - $ppd); $cost = new Currency(); $cost->setValue($value); $plan->cost = $cost; return $plan; }
public function Field($properties = array()) { $source = $this->getSource(); $odd = 0; $options = array(); if ($source) { foreach ($source as $item) { $price = new Currency("Price"); $price->setValue($item->Price); $price_diff = new Currency("PriceDiff"); $price_diff->setValue($item->PriceDiff); $options[] = new ArrayData(array('ID' => $item->ID, 'Name' => $this->name, 'Value' => $item->ID, 'Title' => $item->Title, 'isChecked' => $item->ID == $this->value, 'isDisabled' => $this->disabled || in_array($item->ID, $this->disabledItems), 'Price' => $price, 'PriceDiff' => $price_diff)); } } $properties = array_merge($properties, array('Options' => new ArrayList($options))); return $this->customise($properties)->renderWith($this->getTemplates()); }
public function getItemSummary() { $modify_price = $this->ModifyPrice; $price = new Currency(); $tax_id = $this->Parent()->Parent()->TaxRateID; if ($tax_id && Catalogue::config()->price_includes_tax) { $modify_price += $modify_price / 100 * $this->Parent()->Parent()->TaxRate()->Amount; } $price->setValue($modify_price); if ($price->RAW() > 0) { $summary = $this->Title . ' +' . $price->nice(); } elseif ($price->RAW() < 0) { $summary = $this->Title . ' -' . str_replace(array("(", ")"), "", $price->nice()); } else { $summary = $this->Title; } $this->extend('updateItemSummary', $summary); return $summary; }
/** * Total of order including postage * * @return Decimal */ public function getTotal() { $return = new Currency(); $return->setName("Total"); $total = $this->getSubTotal()->RAW() + $this->getPostage()->RAW() - $this->DiscountAmount + $this->getTaxTotal()->RAW(); $return->setValue($total); $this->extend("updateTotal", $return); return $return; }
/** * Form responsible for estimating shipping based on location and * postal code * * @return Form */ public function PostageForm() { $available_postage = Session::get("Commerce.AvailablePostage"); // Setup default postage fields $country_select = CompositeField::create(CountryDropdownField::create('Country', _t('Commerce.Country', 'Country'))->setAttribute("class", 'countrydropdown dropdown btn'), TextField::create("ZipCode", _t('Commerce.ZipCode', "Zip/Postal Code")))->addExtraClass("size1of2")->addExtraClass("unit")->addExtraClass("unit-50"); // If we have stipulated a search, then see if we have any results // otherwise load empty fieldsets if ($available_postage) { $search_text = _t('Commerce.Update', "Update"); // Loop through all postage areas and generate a new list $postage_array = array(); foreach ($available_postage as $area) { $area_currency = new Currency("Cost"); $area_currency->setValue($area->Cost); $postage_array[$area->ID] = $area->Title . " (" . $area_currency->Nice() . ")"; } $postage_select = CompositeField::create(OptionsetField::create("PostageID", _t('Commerce.SelectPostage', "Select Postage"), $postage_array))->addExtraClass("size1of2")->addExtraClass("unit")->addExtraClass("unit-50"); $confirm_action = CompositeField::create(FormAction::create("doSavePostage", _t('Commerce.Confirm', "Confirm"))->addExtraClass('btn')->addExtraClass('btn-green'))->addExtraClass("size1of2")->addExtraClass("unit")->addExtraClass("unit-50"); } else { $search_text = _t('Commerce.Search', "Search"); $postage_select = CompositeField::create()->addExtraClass("size1of2")->addExtraClass("unit")->addExtraClass("unit-50"); $confirm_action = CompositeField::create()->addExtraClass("size1of2")->addExtraClass("unit")->addExtraClass("unit-50"); } // Set search field $search_action = CompositeField::create(FormAction::create("doGetPostage", $search_text)->addExtraClass('btn'))->addExtraClass("size1of2")->addExtraClass("unit")->addExtraClass("unit-50"); // Setup fields and actions $fields = new FieldList(CompositeField::create($country_select, $postage_select)->addExtraClass("line")->addExtraClass("units-row-end")); $actions = new FieldList(CompositeField::create($search_action, $confirm_action)->addExtraClass("line")->addExtraClass("units-row-end")); $required = RequiredFields::create(array("Country", "ZipCode")); $form = Form::create($this, 'PostageForm', $fields, $actions, $required)->addExtraClass('forms')->addExtraClass('forms-columnar'); // Check if the form has been re-posted and load data $data = Session::get("Form.{$form->FormName()}.data"); if (is_array($data)) { $form->loadDataFrom($data); } // Check if the postage area has been set, if so, Set Postage ID $data = array(); $data["PostageID"] = Session::get("Commerce.PostageID"); if (is_array($data)) { $form->loadDataFrom($data); } // Extension call $this->extend("updatePostageForm", $form); return $form; }
protected function parseFloat($floatString) { //hack to clean up currency symbols, etc.... $LocaleInfo = localeconv(); $floatString = str_replace($LocaleInfo["mon_decimal_point"], ".", $floatString); $titleDescriptor = new Currency("titleDescriptor"); $titleDescriptor->setValue(1111111); $titleDescriptorString = $titleDescriptor->Nice(); $titleDescriptorString = str_replace("1", "", $titleDescriptorString); //HACK! $titleDescriptorString = str_replace(".00", "", $titleDescriptorString); for ($i = 0; $i < strlen($titleDescriptorString); $i++) { $char = substr($titleDescriptorString, $i, 1); if ($char != $LocaleInfo["mon_decimal_point"]) { $floatString = str_replace($char, "", $floatString); } } return round(floatval($floatString - 0), 2); }
/** * Form responsible for estimating shipping based on location and * postal code * * @return Form */ public function PostageForm() { if (!Checkout::config()->simple_checkout) { $available_postage = Session::get("Checkout.AvailablePostage"); // Setup form $form = Form::create($this, 'PostageForm', $fields = new FieldList(CountryDropdownField::create('Country', _t('Checkout.Country', 'Country')), TextField::create("ZipCode", _t('Checkout.ZipCode', "Zip/Postal Code"))), $actions = new FieldList(FormAction::create("doSetPostage", _t('Checkout.Search', "Search"))->addExtraClass('btn')->addExtraClass('btn btn-green btn-success')), $required = RequiredFields::create(array("Country", "ZipCode")))->addExtraClass('forms')->addExtraClass('forms-inline')->setLegend(_t("Checkout.EstimateShipping", "Estimate Shipping")); // If we have stipulated a search, then see if we have any results // otherwise load empty fieldsets if ($available_postage && $available_postage->exists()) { // Loop through all postage areas and generate a new list $postage_array = array(); foreach ($available_postage as $area) { $area_currency = new Currency("Cost"); $area_currency->setValue($area->Cost); $postage_array[$area->ID] = $area->Title . " (" . $area_currency->Nice() . ")"; } $fields->add(OptionsetField::create("PostageID", _t('Checkout.SelectPostage', "Select Postage"), $postage_array)); $actions->dataFieldByName("action_doSetPostage")->setTitle(_t('Checkout.Update', "Update")); } // Check if the form has been re-posted and load data $data = Session::get("Form.{$form->FormName()}.data"); if (is_array($data)) { $form->loadDataFrom($data); } // Check if the postage area has been set, if so, Set Postage ID $data = array(); $data["PostageID"] = Session::get("Checkout.PostageID"); if (is_array($data)) { $form->loadDataFrom($data); } // Extension call $this->extend("updatePostageForm", $form); return $form; } }
public function __construct($controller, $name = "PostagePaymentForm") { if (!Checkout::config()->simple_checkout && !ShoppingCart::get()->isCollection()) { // Get delivery data and postage areas from session $delivery_data = Session::get("Checkout.DeliveryDetailsForm.data"); $country = $delivery_data['DeliveryCountry']; $postcode = $delivery_data['DeliveryPostCode']; $cart = ShoppingCart::get(); $postage_areas = new ShippingCalculator($postcode, $country); $postage_areas->setCost($cart->SubTotalCost)->setWeight($cart->TotalWeight)->setItems($cart->TotalItems); $postage_areas = $postage_areas->getPostageAreas(); // Loop through all postage areas and generate a new list $postage_array = array(); foreach ($postage_areas as $area) { $area_currency = new Currency("Cost"); $area_currency->setValue($area->Cost); $postage_array[$area->ID] = $area->Title . " (" . $area_currency->Nice() . ")"; } if (Session::get('Checkout.PostageID')) { $postage_id = Session::get('Checkout.PostageID'); } elseif ($postage_areas->exists()) { $postage_id = $postage_areas->first()->ID; } else { $postage_id = 0; } if (count($postage_array)) { $select_postage_field = OptionsetField::create("PostageID", _t('Checkout.PostageSelection', 'Please select your preferred postage'), $postage_array)->setValue($postage_id); } else { $select_postage_field = ReadonlyField::create("NoPostage", "", _t('Checkout.NoPostageSelection', 'Unfortunately we cannot deliver to your address'))->addExtraClass("label")->addExtraClass("label-red"); } // Setup postage fields $postage_field = CompositeField::create(HeaderField::create("PostageHeader", _t('Checkout.Postage', "Postage")), $select_postage_field)->setName("PostageFields")->addExtraClass("unit")->addExtraClass("size1of2")->addExtraClass("unit-50"); } elseif (ShoppingCart::get()->isCollection()) { $postage_field = CompositeField::create(HeaderField::create("PostageHeader", _t('Checkout.CollectionOnly', "Collection Only")), ReadonlyField::create("CollectionText", "", _t("Checkout.ItemsReservedInstore", "Your items will be held instore until you collect them")))->setName("CollectionFields")->addExtraClass("unit")->addExtraClass("size1of2")->addExtraClass("unit-50"); } else { $postage_field = null; } // Get available payment methods and setup payment $payment_methods = ArrayList::create(); foreach (SiteConfig::current_site_config()->PaymentMethods() as $payment_method) { if ($payment_method->canView()) { $payment_methods->add($payment_method); } } // Deal with payment methods if ($payment_methods->exists()) { $payment_field = OptionsetField::create('PaymentMethodID', _t('Checkout.PaymentSelection', 'Please choose how you would like to pay'), $payment_methods->map('ID', 'Label'), $payment_methods->filter('Default', 1)->first()->ID); } else { $payment_field = ReadonlyField::create("PaymentMethodID", _t('Checkout.PaymentSelection', 'Please choose how you would like to pay'), _t('Checkout.NoPaymentMethods', 'You cannot pay at this time, if you feel there has been an error please contact us.')); } $payment_field = CompositeField::create(HeaderField::create('PaymentHeading', _t('Checkout.Payment', 'Payment'), 2), $payment_field)->setName("PaymentFields")->addExtraClass("unit")->addExtraClass("size1of2")->addExtraClass("unit-50"); $fields = FieldList::create(CompositeField::create($postage_field, $payment_field)->setName("PostagePaymentFields")->addExtraClass("units-row")->addExtraClass("line")); $back_url = $controller->Link("billing"); if ($payment_methods->exists()) { $actions = FieldList::create(LiteralField::create('BackButton', '<a href="' . $back_url . '" class="btn btn-red checkout-action-back">' . _t('Checkout.Back', 'Back') . '</a>'), FormAction::create('doContinue', _t('Checkout.PaymentDetails', 'Enter Payment Details'))->addExtraClass('btn')->addExtraClass('checkout-action-next')->addExtraClass('btn-green')); } else { $actions = FieldList::create(); } $validator = RequiredFields::create(array("PostageID", "PaymentMethod")); parent::__construct($controller, $name, $fields, $actions, $validator); }
/** * @return Currency */ public function getUnitPriceAsMoney() { $out = new Currency('UnitPrice'); $out->setValue($this->getUnitPrice()); return $out; }