| 1: | <?php
|
| 2: | namespace Opencart\System\Library\Cart;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Currency {
|
| 9: | |
| 10: | |
| 11: |
|
| 12: | private object $db;
|
| 13: | |
| 14: | |
| 15: |
|
| 16: | private object $language;
|
| 17: | |
| 18: | |
| 19: |
|
| 20: | private array $currencies = [];
|
| 21: |
|
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: |
|
| 27: | public function __construct(\Opencart\System\Engine\Registry $registry) {
|
| 28: | $this->db = $registry->get('db');
|
| 29: | $this->language = $registry->get('language');
|
| 30: |
|
| 31: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "currency`");
|
| 32: |
|
| 33: | foreach ($query->rows as $result) {
|
| 34: | $this->currencies[$result['code']] = ['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']];
|
| 35: | }
|
| 36: | }
|
| 37: |
|
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: |
|
| 48: | public function format(float $number, string $currency, float $value = 0, bool $format = true) {
|
| 49: | if (!isset($this->currencies[$currency])) {
|
| 50: | return '';
|
| 51: | }
|
| 52: |
|
| 53: | $symbol_left = $this->currencies[$currency]['symbol_left'];
|
| 54: | $symbol_right = $this->currencies[$currency]['symbol_right'];
|
| 55: | $decimal_place = $this->currencies[$currency]['decimal_place'];
|
| 56: |
|
| 57: | if (!$value) {
|
| 58: | $value = $this->currencies[$currency]['value'];
|
| 59: | }
|
| 60: |
|
| 61: | $amount = $value ? (float)$number * $value : (float)$number;
|
| 62: |
|
| 63: | $amount = round($amount, $decimal_place);
|
| 64: |
|
| 65: | if (!$format) {
|
| 66: | return $amount;
|
| 67: | }
|
| 68: |
|
| 69: | $string = '';
|
| 70: |
|
| 71: | if ($symbol_left) {
|
| 72: | $string .= $symbol_left;
|
| 73: | }
|
| 74: |
|
| 75: | $string .= number_format($amount, $decimal_place, $this->language->get('decimal_point'), $this->language->get('thousand_point'));
|
| 76: |
|
| 77: | if ($symbol_right) {
|
| 78: | $string .= $symbol_right;
|
| 79: | }
|
| 80: |
|
| 81: | return $string;
|
| 82: | }
|
| 83: |
|
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: |
|
| 93: | public function convert(float $value, string $from, string $to): float {
|
| 94: | if (isset($this->currencies[$from])) {
|
| 95: | $from = $this->currencies[$from]['value'];
|
| 96: | } else {
|
| 97: | $from = 1;
|
| 98: | }
|
| 99: |
|
| 100: | if (isset($this->currencies[$to])) {
|
| 101: | $to = $this->currencies[$to]['value'];
|
| 102: | } else {
|
| 103: | $to = 1;
|
| 104: | }
|
| 105: |
|
| 106: | return $value * ($to / $from);
|
| 107: | }
|
| 108: |
|
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: |
|
| 116: | public function getId(string $currency): int {
|
| 117: | if (isset($this->currencies[$currency])) {
|
| 118: | return $this->currencies[$currency]['currency_id'];
|
| 119: | } else {
|
| 120: | return 0;
|
| 121: | }
|
| 122: | }
|
| 123: |
|
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: |
|
| 131: | public function getSymbolLeft(string $currency): string {
|
| 132: | if (isset($this->currencies[$currency])) {
|
| 133: | return $this->currencies[$currency]['symbol_left'];
|
| 134: | } else {
|
| 135: | return '';
|
| 136: | }
|
| 137: | }
|
| 138: |
|
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: |
|
| 146: | public function getSymbolRight(string $currency): string {
|
| 147: | if (isset($this->currencies[$currency])) {
|
| 148: | return $this->currencies[$currency]['symbol_right'];
|
| 149: | } else {
|
| 150: | return '';
|
| 151: | }
|
| 152: | }
|
| 153: |
|
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: |
|
| 161: | public function getDecimalPlace(string $currency): int {
|
| 162: | if (isset($this->currencies[$currency])) {
|
| 163: | return (int)$this->currencies[$currency]['decimal_place'];
|
| 164: | } else {
|
| 165: | return 0;
|
| 166: | }
|
| 167: | }
|
| 168: |
|
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: |
|
| 176: | public function getValue(string $currency): float {
|
| 177: | if (isset($this->currencies[$currency])) {
|
| 178: | return $this->currencies[$currency]['value'];
|
| 179: | } else {
|
| 180: | return 0;
|
| 181: | }
|
| 182: | }
|
| 183: |
|
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: |
|
| 191: | public function has(string $currency): bool {
|
| 192: | return isset($this->currencies[$currency]);
|
| 193: | }
|
| 194: | }
|
| 195: | |