/aoliz/core/model/system/mdl.cur.php
PHP | 230 lines | 189 code | 19 blank | 22 comment | 26 complexity | 2b2f6dc2c0166ed2c39a89a8d44d4152 MD5 | raw file
- <?php
- include_once('shopObject.php');
- class mdl_cur extends shopObject {
- var $tableName = 'sdb_currency';
- var $idColumn = 'cur_code';
- var $textColumn = 'cur_name';
- function mdl_cur($system){
- parent::modelFactory($system);
- if (defined('IN_INSTALLER')) return;
- $this->_money_format=array(
- 'decimals'=>$this->system->getConf('system.money.operation.decimals'),
- 'dec_point'=>$this->system->getConf('system.money.dec_point'),
- 'thousands_sep'=>$this->system->getConf('system.money.thousands_sep'),
- 'fonttend_decimal_type'=>$this->system->getConf('system.money.operation.carryset'),
- 'fonttend_decimal_remain'=>$this->system->getConf('system.money.operation.decimals')
- );
- }
- /**
- * getSysCur
- *
- * @access privite
- * @return void
- */
- function getSysCur(){
- $CON_CURRENCY['CNY'] = __("???");
- $CON_CURRENCY['USD'] = __("??");
- $CON_CURRENCY['EUR'] = __("??");
- $CON_CURRENCY['GBP'] = __("??");
- $CON_CURRENCY['CAD'] = __("????");
- $CON_CURRENCY['AUD'] = __("??");
- $CON_CURRENCY['RUB'] = __("??");
- $CON_CURRENCY['HKD'] = __("??");
- $CON_CURRENCY['TWD'] = __("???");
- $CON_CURRENCY['KRW'] = __("??");
- $CON_CURRENCY['SGD'] = __("????");
- $CON_CURRENCY['NZD'] = __("????");
- $CON_CURRENCY['JPY'] = __("??");
- $CON_CURRENCY['MYR'] = __("??");
- $CON_CURRENCY['CHF'] = __("????");
- $CON_CURRENCY['SEK'] = __("????");
- $CON_CURRENCY['DKK'] = __("????");
- $CON_CURRENCY['PLZ'] = __("???");
- $CON_CURRENCY['NOK'] = __("????");
- $CON_CURRENCY['HUF'] = __("??");
- $CON_CURRENCY['CSK'] = __("????");
- $CON_CURRENCY['MOP'] = __("??");
- return $CON_CURRENCY;
- }
- function curAdd($data){
- if ($data['def_cur']=='true') {
- $sql="select cur_code from sdb_currency where def_cur=1 and cur_code<>'{$data['cur_code']}'";
- $dat=$this->db->select($sql);
- if (!empty($dat[0]['cur_code'])) {
- $this->setError(2005001);
- trigger_error(__('??????????'),E_USER_ERROR);
- return false;
- }
- }
- $rs=$this->db->query('select * from sdb_currency where 0=1');
- $sql=$this->db->GetInsertSQL($rs,$data);
- if(!$sql || $this->db->query($sql)){
- return true;
- }else{
- $this->setError(2005002);
- trigger_error(__('???????'));
- return false;
- }
- }
- function curAll(){
- return $this->db->select('select * from sdb_currency');
- }
- function curDel($id){
- $sql = 'delete from sdb_currency where cur_code="'.$id.'"';
- if($this->db->exec($sql)){
- return true;
- }else{
- return false;
- }
- }
- function getcur($id, $getDef=false){
- $aCur = $this->db->selectrow('select * FROM sdb_currency where cur_code="'.$id.'"');
- if($aCur['cur_code'] || !$getDef){
- return $this->_in_cur[$id] = $aCur;
- }elseif($this->_default_cur){
- return $this->_default_cur;
- }else{
- $this->_default_cur = $this->getDefault();
- return $this->_in_cur[$this->_default_cur['cur_code']] = &$this->_default_cur;
- }
- }
- function getDefault(){
- if($cur = $this->db->selectrow('select * from sdb_currency where def_cur=1')){
- return $cur;
- }else{ //if have no default currency, read the first currency as default value
- return $this->db->selectrow('select * FROM sdb_currency');
- }
- }
- function curEdit($id,$data,$old_cur_code){
- if ($data['def_cur']=='true') {
- $sql="select cur_code from sdb_currency where def_cur=1 and cur_code<>'{$old_cur_code}'";
- $dat=$this->db->select($sql);
- if (!empty($dat[0]['cur_code'])) {
- $this->setError(2005003);
- trigger_error(__('??????????'),E_USER_ERROR);
- return false;
- }
- }
- $rs=$this->db->query('select * from sdb_currency where cur_code="'.$old_cur_code.'"');
- $sql=$this->db->GetUpdateSQL($rs,$data);
- if($sql){
- if($this->db->exec($sql)){
- return true;
- }else{
- trigger_error(__('??????'),E_USER_ERROR);
- return false;
- }
- }else return true;
- }
- //????????
- function getFormat($cur){
- $ret = array();
- $cursign = $this->getcur($cur, true);
- $ret = $this->_money_format;
- $ret['sign'] = $cursign['cur_sign'];
- return $ret;
- }
- /**
- * ??????????????
- * @param float $money ??
- * @param string $currency ?????????CNY/USD
- * @param bool $is_sign ??????????
- * @param bool $is_rate ???????
- * @return bool
- */
- function changer($money, $currency='', $is_sign=false, $is_rate=false)
- {
- $cur_money = $this->get_cur_money($money, $currency);
- if($is_rate){
- $cur_money = $money;
- }
- if($is_sign){
- return $this->formatNumber($cur_money, false);
- }else{
- return $this->_in_cur['cur_sign'].$this->formatNumber($cur_money);
- }
- }
- /**
- * ??????
- * @param float $money ??
- * @param string $currency ??????CNY/USD
- * @return bool
- */
- function get_cur_money($money, $currency=''){
- if(empty($currency)) $currency = $this->system->request['cur'];
- if($currency || empty($this->_in_cur['cur_rate'])){
- $this->_in_cur = $this->getcur($currency, true);
- }
- return $money*($this->_in_cur['cur_rate'] ? $this->_in_cur['cur_rate'] : 1);
- }
- function amount($money,$currency='',$basicFormat=false,$chgval=true,$is_order){
- if(empty($currency)) $currency = $this->system->request['cur'];
- if($currency || empty($this->_in_cur['cur_rate'])){
- $this->_in_cur = $this->getcur($currency, true);
- }
- if($chgval){
- $money = $money*($this->_in_cur['cur_rate'] ? $this->_in_cur['cur_rate'] : 1);
- }
- $oMath = $this->system->loadModel('system/math');
- if($is_order){
- $this->is_order = true;
- $oMath->operationCarryset=$this->system->getConf("site.decimal_type");
- $oMath->getFunc();
- }
- $money = $oMath->getOperationNumber($money);
- $money = $this->formatNumber($money);
- if($basicFormat){
- return $money;
- }
- $precision = $this->system->getConf('site.decimal_digit');
- $decimal_type = $this->system->getConf('site.decimal_type');
- $mul = '1'.str_repeat('0',$precision);
- switch($decimal_type){
- case 0:
- $money = round($money,$precision);
- break;
- case 1:
- $money = ceil(trim($money)*$mul) / $mul;
- break;
- case 2:
- $money = floor(trim($money)*$mul) / $mul;
- break;
- }
- return $this->_in_cur['cur_sign'].sprintf("%.0{$precision}f",$money);
- }
- function formatNumber($number, $is_str=true){
- //??????????????
- $oMath = $this->system->loadModel('system/math');
- if($this->is_order){
- $oMath->operationCarryset=$this->system->getConf("site.decimal_type");
- $oMath->getFunc();
- }
- $number = $oMath->getOperationNumber($number);
- if($is_str)
- return number_format(trim($number),
- $this->_money_format['decimals'],
- $this->_money_format['dec_point'],$this->_money_format['thousands_sep']);
- else
- return number_format(trim($number), $this->_money_format['decimals'], '.', '');
- }
- }
- ?>