1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<?php
namespace Cart;
class Currency {
private $currencies = array();
public function __construct($registry) {
$this->db = $registry->get('db');
$this->language = $registry->get('language');
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency");
foreach ($query->rows as $result) {
$this->currencies[$result['code']] = array(
'currency_id' => $result['currency_id'],
'title' => $result['title'],
'symbol_left' => $result['symbol_left'],
'symbol_right' => $result['symbol_right'],
'decimal_place' => $result['decimal_place'],
'value' => $result['value']
);
}
}
public function format($number, $currency, $value = '', $format = true) {
$symbol_left = $this->currencies[$currency]['symbol_left'];
$symbol_right = $this->currencies[$currency]['symbol_right'];
$decimal_place = $this->currencies[$currency]['decimal_place'];
if (!$value) {
$value = $this->currencies[$currency]['value'];
}
$amount = $value ? (float)$number * $value : (float)$number;
$amount = round($amount, (int)$decimal_place);
if (!$format) {
return $amount;
}
$string = '';
if ($symbol_left) {
$string .= $symbol_left;
}
$string .= number_format($amount, (int)$decimal_place, $this->language->get('decimal_point'), $this->language->get('thousand_point'));
if ($symbol_right) {
$string .= $symbol_right;
}
return $string;
}
public function convert($value, $from, $to) {
if (isset($this->currencies[$from])) {
$from = $this->currencies[$from]['value'];
} else {
$from = 1;
}
if (isset($this->currencies[$to])) {
$to = $this->currencies[$to]['value'];
} else {
$to = 1;
}
return $value * ($to / $from);
}
public function getId($currency) {
if (isset($this->currencies[$currency])) {
return $this->currencies[$currency]['currency_id'];
} else {
return 0;
}
}
public function getSymbolLeft($currency) {
if (isset($this->currencies[$currency])) {
return $this->currencies[$currency]['symbol_left'];
} else {
return '';
}
}
public function getSymbolRight($currency) {
if (isset($this->currencies[$currency])) {
return $this->currencies[$currency]['symbol_right'];
} else {
return '';
}
}
public function getDecimalPlace($currency) {
if (isset($this->currencies[$currency])) {
return $this->currencies[$currency]['decimal_place'];
} else {
return 0;
}
}
public function getValue($currency) {
if (isset($this->currencies[$currency])) {
return $this->currencies[$currency]['value'];
} else {
return 0;
}
}
public function has($currency) {
return isset($this->currencies[$currency]);
}
}
|