PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/aoliz/core/model/system/mdl.cur.php

http://phpfor.googlecode.com/
PHP | 230 lines | 189 code | 19 blank | 22 comment | 26 complexity | 2b2f6dc2c0166ed2c39a89a8d44d4152 MD5 | raw file
  1. <?php
  2. include_once('shopObject.php');
  3. class mdl_cur extends shopObject {
  4. var $tableName = 'sdb_currency';
  5. var $idColumn = 'cur_code';
  6. var $textColumn = 'cur_name';
  7. function mdl_cur($system){
  8. parent::modelFactory($system);
  9. if (defined('IN_INSTALLER')) return;
  10. $this->_money_format=array(
  11. 'decimals'=>$this->system->getConf('system.money.operation.decimals'),
  12. 'dec_point'=>$this->system->getConf('system.money.dec_point'),
  13. 'thousands_sep'=>$this->system->getConf('system.money.thousands_sep'),
  14. 'fonttend_decimal_type'=>$this->system->getConf('system.money.operation.carryset'),
  15. 'fonttend_decimal_remain'=>$this->system->getConf('system.money.operation.decimals')
  16. );
  17. }
  18. /**
  19. * getSysCur
  20. *
  21. * @access privite
  22. * @return void
  23. */
  24. function getSysCur(){
  25. $CON_CURRENCY['CNY'] = __("???");
  26. $CON_CURRENCY['USD'] = __("??");
  27. $CON_CURRENCY['EUR'] = __("??");
  28. $CON_CURRENCY['GBP'] = __("??");
  29. $CON_CURRENCY['CAD'] = __("????");
  30. $CON_CURRENCY['AUD'] = __("??");
  31. $CON_CURRENCY['RUB'] = __("??");
  32. $CON_CURRENCY['HKD'] = __("??");
  33. $CON_CURRENCY['TWD'] = __("???");
  34. $CON_CURRENCY['KRW'] = __("??");
  35. $CON_CURRENCY['SGD'] = __("????");
  36. $CON_CURRENCY['NZD'] = __("????");
  37. $CON_CURRENCY['JPY'] = __("??");
  38. $CON_CURRENCY['MYR'] = __("??");
  39. $CON_CURRENCY['CHF'] = __("????");
  40. $CON_CURRENCY['SEK'] = __("????");
  41. $CON_CURRENCY['DKK'] = __("????");
  42. $CON_CURRENCY['PLZ'] = __("???");
  43. $CON_CURRENCY['NOK'] = __("????");
  44. $CON_CURRENCY['HUF'] = __("??");
  45. $CON_CURRENCY['CSK'] = __("????");
  46. $CON_CURRENCY['MOP'] = __("??");
  47. return $CON_CURRENCY;
  48. }
  49. function curAdd($data){
  50. if ($data['def_cur']=='true') {
  51. $sql="select cur_code from sdb_currency where def_cur=1 and cur_code<>'{$data['cur_code']}'";
  52. $dat=$this->db->select($sql);
  53. if (!empty($dat[0]['cur_code'])) {
  54. $this->setError(2005001);
  55. trigger_error(__('??????????'),E_USER_ERROR);
  56. return false;
  57. }
  58. }
  59. $rs=$this->db->query('select * from sdb_currency where 0=1');
  60. $sql=$this->db->GetInsertSQL($rs,$data);
  61. if(!$sql || $this->db->query($sql)){
  62. return true;
  63. }else{
  64. $this->setError(2005002);
  65. trigger_error(__('???????'));
  66. return false;
  67. }
  68. }
  69. function curAll(){
  70. return $this->db->select('select * from sdb_currency');
  71. }
  72. function curDel($id){
  73. $sql = 'delete from sdb_currency where cur_code="'.$id.'"';
  74. if($this->db->exec($sql)){
  75. return true;
  76. }else{
  77. return false;
  78. }
  79. }
  80. function getcur($id, $getDef=false){
  81. $aCur = $this->db->selectrow('select * FROM sdb_currency where cur_code="'.$id.'"');
  82. if($aCur['cur_code'] || !$getDef){
  83. return $this->_in_cur[$id] = $aCur;
  84. }elseif($this->_default_cur){
  85. return $this->_default_cur;
  86. }else{
  87. $this->_default_cur = $this->getDefault();
  88. return $this->_in_cur[$this->_default_cur['cur_code']] = &$this->_default_cur;
  89. }
  90. }
  91. function getDefault(){
  92. if($cur = $this->db->selectrow('select * from sdb_currency where def_cur=1')){
  93. return $cur;
  94. }else{ //if have no default currency, read the first currency as default value
  95. return $this->db->selectrow('select * FROM sdb_currency');
  96. }
  97. }
  98. function curEdit($id,$data,$old_cur_code){
  99. if ($data['def_cur']=='true') {
  100. $sql="select cur_code from sdb_currency where def_cur=1 and cur_code<>'{$old_cur_code}'";
  101. $dat=$this->db->select($sql);
  102. if (!empty($dat[0]['cur_code'])) {
  103. $this->setError(2005003);
  104. trigger_error(__('??????????'),E_USER_ERROR);
  105. return false;
  106. }
  107. }
  108. $rs=$this->db->query('select * from sdb_currency where cur_code="'.$old_cur_code.'"');
  109. $sql=$this->db->GetUpdateSQL($rs,$data);
  110. if($sql){
  111. if($this->db->exec($sql)){
  112. return true;
  113. }else{
  114. trigger_error(__('??????'),E_USER_ERROR);
  115. return false;
  116. }
  117. }else return true;
  118. }
  119. //????????
  120. function getFormat($cur){
  121. $ret = array();
  122. $cursign = $this->getcur($cur, true);
  123. $ret = $this->_money_format;
  124. $ret['sign'] = $cursign['cur_sign'];
  125. return $ret;
  126. }
  127. /**
  128. * ??????????????
  129. * @param float $money ??
  130. * @param string $currency ?????????CNY/USD
  131. * @param bool $is_sign ??????????
  132. * @param bool $is_rate ???????
  133. * @return bool
  134. */
  135. function changer($money, $currency='', $is_sign=false, $is_rate=false)
  136. {
  137. $cur_money = $this->get_cur_money($money, $currency);
  138. if($is_rate){
  139. $cur_money = $money;
  140. }
  141. if($is_sign){
  142. return $this->formatNumber($cur_money, false);
  143. }else{
  144. return $this->_in_cur['cur_sign'].$this->formatNumber($cur_money);
  145. }
  146. }
  147. /**
  148. * ??????
  149. * @param float $money ??
  150. * @param string $currency ??????CNY/USD
  151. * @return bool
  152. */
  153. function get_cur_money($money, $currency=''){
  154. if(empty($currency)) $currency = $this->system->request['cur'];
  155. if($currency || empty($this->_in_cur['cur_rate'])){
  156. $this->_in_cur = $this->getcur($currency, true);
  157. }
  158. return $money*($this->_in_cur['cur_rate'] ? $this->_in_cur['cur_rate'] : 1);
  159. }
  160. function amount($money,$currency='',$basicFormat=false,$chgval=true,$is_order){
  161. if(empty($currency)) $currency = $this->system->request['cur'];
  162. if($currency || empty($this->_in_cur['cur_rate'])){
  163. $this->_in_cur = $this->getcur($currency, true);
  164. }
  165. if($chgval){
  166. $money = $money*($this->_in_cur['cur_rate'] ? $this->_in_cur['cur_rate'] : 1);
  167. }
  168. $oMath = $this->system->loadModel('system/math');
  169. if($is_order){
  170. $this->is_order = true;
  171. $oMath->operationCarryset=$this->system->getConf("site.decimal_type");
  172. $oMath->getFunc();
  173. }
  174. $money = $oMath->getOperationNumber($money);
  175. $money = $this->formatNumber($money);
  176. if($basicFormat){
  177. return $money;
  178. }
  179. $precision = $this->system->getConf('site.decimal_digit');
  180. $decimal_type = $this->system->getConf('site.decimal_type');
  181. $mul = '1'.str_repeat('0',$precision);
  182. switch($decimal_type){
  183. case 0:
  184. $money = round($money,$precision);
  185. break;
  186. case 1:
  187. $money = ceil(trim($money)*$mul) / $mul;
  188. break;
  189. case 2:
  190. $money = floor(trim($money)*$mul) / $mul;
  191. break;
  192. }
  193. return $this->_in_cur['cur_sign'].sprintf("%.0{$precision}f",$money);
  194. }
  195. function formatNumber($number, $is_str=true){
  196. //??????????????
  197. $oMath = $this->system->loadModel('system/math');
  198. if($this->is_order){
  199. $oMath->operationCarryset=$this->system->getConf("site.decimal_type");
  200. $oMath->getFunc();
  201. }
  202. $number = $oMath->getOperationNumber($number);
  203. if($is_str)
  204. return number_format(trim($number),
  205. $this->_money_format['decimals'],
  206. $this->_money_format['dec_point'],$this->_money_format['thousands_sep']);
  207. else
  208. return number_format(trim($number), $this->_money_format['decimals'], '.', '');
  209. }
  210. }
  211. ?>