From d9d5b5398de298404fdc7fd5fa6f4a90ecb94a7a Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Sat, 24 Mar 2012 22:29:08 +0100 Subject: [PATCH] money formatting for web interface --- www/inc/classes/language_manager.php | 101 ++++++++++++++++++++++++++++++++- www/inc/rendering/twig_extensions.php | 9 +++- www/template/en/ordermail.txt | 14 ++-- 3 files changed, 114 insertions(+), 10 deletions(-) diff --git a/www/inc/classes/language_manager.php b/www/inc/classes/language_manager.php index e5870de..d981777 100644 --- a/www/inc/classes/language_manager.php +++ b/www/inc/classes/language_manager.php @@ -253,10 +253,107 @@ class LanguageManager return $this->formatDate($time,$this->formatcfg->gettimeformat()); } + ///show the thoursand separator in numbers and money + const UseThousandSeparator=1; + ///format an integer number + public function formatIntNumber($num,$flags=0) + { + $ts=$this->formatcfg->getthousandseparator(); + $td=$this->formatcfg->getthousanddigits(); + $num=round($num,0); + //any reason to abbreviate this? + if($td===null || $td<=0 || $ts==null || $ts=="")return $num.""; + if(($flags&self::UseThousandSeparator)==0)return $num.""; + //start formatting + if($num<0){$isneg=true;$num*=-1;}else $isneg=false; + $s=$num.""; + //insert dividers + $r=""; + $l=strlen($s); + for($i=0;$i<$l;$i++){ + if($i>0 && (($l-$i)%$td)==0)$r.=$ts; + $r.=$s[$i]; + } + if($isneg)$r="-"+$r; + return $r; + } + ///format a float number + public function formatFloatNumber($num,$digits=2,$flags=0) + { + if($num<0){$isneg=true;$num*=-1;}else $isneg=false; + //format int part + $num=round($num,$digits); + $num1=floor($num); + $r=$this->formatIntNumber($num1,$flags); + //add sign + if($isneg)$r="-".$r; + if($digits<=0)return $r; + //add fraction + $num-=$num1; + for($i=0;$i<$digits;$i++)$num*=10.; + $r.=$this->formatcfg->getdecimaldot(); + $r.=round($num,0); + return $r; + } + + ///show the currency symbol + const MoneyShowCurrency=2; + ///mask for currency symbol choice + const MoneyCurrencyMask=0xf0; + ///show the HTML version of the currency symbol + const MoneyCurrencyHTML=0; + ///show the plain ASCII version of the currency symbol + const MoneyCurrencyPlain=0x10; + ///show the clients (Unicode) version of the currency symbol + const MoneyCurrencyClient=0x20; + /** returns price in current language */ - public function getPrice($price) + public function getPrice($price,$flags=3) { - return number_format($price/100, 2, i18n("."), i18n(",")); + $digits=$this->formatcfg->getmoneydecimals(); + //adjust number + if($price<0){$isnet=true;$price*=-1;}else $isneg=false; + for($i=0;$i<$digits;$i++)$price/=10.; + //create number part + $np=$this->formatFloatNumber($price,$digits,$flags); + //get currency + if($flags&self::MoneyShowCurrency){ + switch($flags&self::MoneyCurrencyMask){ + case self::MoneyCurrencyPlain: + $curr=$this->formatcfg->getcurrencysymbolplain(); + break; + case self::MoneyCurrencyClient: + $curr=$this->formatcfg->getcurrencysymbol(); + break; + default: + $curr=$this->formatcfg->getcurrencysymbolhtml(); + break; + } + }else $curr=""; + //add sign + if($isneg){ + $sign=$this->formatcfg->getmoneynegative(); + $pos=$this->formatcfg->getmoneynegativepos(); + }else{ + $sign=$this->formatcfg->getmoneypositive(); + $pos=$this->formatcfg->getmoneypositivepos(); + } + switch($pos){ + case WOServerFormat::NoSign:break; + case WOServerFormat::SignAfterNum:$np.=$sign;break; + case WOServerFormat::SignBeforeSym:$curr=$sign.$curr;break; + case WOServerFormat::SignAfterSym:$curr.=$sign;break; + case WOServerFormat::SignParen:$np="(".$np.")";break; + default://fallback and before number + $np=$sign.$np; + break; + } + //connect and return + if($curr!=""){ + if($this->formatcfg->getcurrencysymbolpos())return $np." ".$curr; + else return $curr." ".$np; + } + return $np; } /** returns value for specified key in current language */ diff --git a/www/inc/rendering/twig_extensions.php b/www/inc/rendering/twig_extensions.php index 872f2d3..744d4f3 100644 --- a/www/inc/rendering/twig_extensions.php +++ b/www/inc/rendering/twig_extensions.php @@ -15,8 +15,10 @@ class LangFilterExtension extends Twig_Extension public function getFilters() { + $safe=array('is_safe'=>array('html')); return array( - 'asMoney' => new Twig_Filter_Method($this, 'getPrice'), + 'asMoney' => new Twig_Filter_Method($this, 'getPrice', $safe), + 'asMoneyPlain' => new Twig_Filter_Method($this, 'getPricePlain', $safe), 'asDate' => new Twig_Filter_Method($this, 'getDate'), 'asTime' => new Twig_Filter_Method($this, 'getTime'), 'asDateTime' => new Twig_Filter_Method($this, 'getDateTime'), @@ -25,6 +27,11 @@ class LangFilterExtension extends Twig_Extension } public function getPrice($i){return $this->lang->getPrice($i);} + public function getPricePlain($i) + { + $flags=LanguageManager::UseThousandSeparator | LanguageManager::MoneyShowCurrency | LanguageManager::MoneyCurrencyPlain; + return $this->lang->getPrice($i,$flags); + } public function getDate($i){return $this->lang->getDate($i);} public function getTime($i){return $this->lang->getTime($i);} public function getDateTime($i){return $this->lang->getDateTime($i);} diff --git a/www/template/en/ordermail.txt b/www/template/en/ordermail.txt index 7a36469..9525d07 100644 --- a/www/template/en/ordermail.txt +++ b/www/template/en/ordermail.txt @@ -35,24 +35,24 @@ Your order will be delivered to the following address: -total price : {{order.totalprice|asMoney}} -already paid: {{order.amountpaid|asMoney}} -payment due : {{order.amountdue|asMoney}} +total price : {{order.totalprice|asMoneyPlain}} +already paid: {{order.amountpaid|asMoneyPlain}} +payment due : {{order.amountdue|asMoneyPlain}} Detailed listing: {% for ticket in order.tickets %} -Ticket: {{ticket.event.title}} ({{ticket.pricecategory.name}}) {{ticket.price|asMoney}} ({{ticket.str_status}}) +Ticket: {{ticket.event.title}} ({{ticket.pricecategory.name}}) {{ticket.price|asMoneyPlain}} ({{ticket.str_status}}) {% endfor %} {% for voucher in order.vouchers %} -Voucher (Value: {{voucher.value|asMoney}}): {{voucher.price|asMoney}} +Voucher (Value: {{voucher.value|asMoneyPlain}}): {{voucher.price|asMoneyPlain}} {% endfor %} {% for item in order.items %} -{{item.amount}}x {{item.productname}} {{item.totalprice}} +{{item.amount}}x {{item.productname}} {{item.totalprice|asMoneyPlain}} {% endfor %} {% if order.shippingtype|isObject %} -Shipping: {{order.shippingtype.description}} {{order.shippingcosts|asMoney}} +Shipping: {{order.shippingtype.description}} {{order.shippingcosts|asMoneyPlain}} {% endif %} -- 1.7.2.5