money formatting for web interface
authorKonrad Rosenbaum <konrad@silmor.de>
Sat, 24 Mar 2012 21:29:08 +0000 (22:29 +0100)
committerKonrad Rosenbaum <konrad@silmor.de>
Sat, 24 Mar 2012 21:29:08 +0000 (22:29 +0100)
www/inc/classes/language_manager.php
www/inc/rendering/twig_extensions.php
www/template/en/ordermail.txt

index e5870de..d981777 100644 (file)
@@ -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 */
index 872f2d3..744d4f3 100644 (file)
@@ -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);}
index 7a36469..9525d07 100644 (file)
@@ -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 %}