| 1: | <?php
|
| 2: | namespace Opencart\System\Library\Cart;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Weight {
|
| 9: | |
| 10: | |
| 11: |
|
| 12: | private object $db;
|
| 13: | |
| 14: | |
| 15: |
|
| 16: | private object $config;
|
| 17: | |
| 18: | |
| 19: |
|
| 20: | private array $weights = [];
|
| 21: |
|
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: |
|
| 27: | public function __construct(\Opencart\System\Engine\Registry $registry) {
|
| 28: | $this->db = $registry->get('db');
|
| 29: | $this->config = $registry->get('config');
|
| 30: |
|
| 31: | $weight_class_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "weight_class` `wc` LEFT JOIN `" . DB_PREFIX . "weight_class_description` `wcd` ON (`wc`.`weight_class_id` = `wcd`.`weight_class_id`) WHERE `wcd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
|
| 32: |
|
| 33: | foreach ($weight_class_query->rows as $result) {
|
| 34: | $this->weights[$result['weight_class_id']] = [
|
| 35: | 'weight_class_id' => $result['weight_class_id'],
|
| 36: | 'title' => $result['title'],
|
| 37: | 'unit' => $result['unit'],
|
| 38: | 'value' => $result['value']
|
| 39: | ];
|
| 40: | }
|
| 41: | }
|
| 42: |
|
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: |
|
| 52: | public function convert(float $value, int $from, int $to): float {
|
| 53: | if ($from == $to) {
|
| 54: | return $value;
|
| 55: | }
|
| 56: |
|
| 57: | if (isset($this->weights[$from])) {
|
| 58: | $from = $this->weights[$from]['value'];
|
| 59: | } else {
|
| 60: | $from = 1;
|
| 61: | }
|
| 62: |
|
| 63: | if (isset($this->weights[$to])) {
|
| 64: | $to = $this->weights[$to]['value'];
|
| 65: | } else {
|
| 66: | $to = 1;
|
| 67: | }
|
| 68: |
|
| 69: | return $value * ($to / $from);
|
| 70: | }
|
| 71: |
|
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: |
|
| 82: | public function format(float $value, int $weight_class_id, string $decimal_point = '.', string $thousand_point = ','): string {
|
| 83: | if (isset($this->weights[$weight_class_id])) {
|
| 84: | return number_format($value, 2, $decimal_point, $thousand_point) . $this->weights[$weight_class_id]['unit'];
|
| 85: | } else {
|
| 86: | return number_format($value, 2, $decimal_point, $thousand_point);
|
| 87: | }
|
| 88: | }
|
| 89: |
|
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: |
|
| 97: | public function getUnit(int $weight_class_id): string {
|
| 98: | if (isset($this->weights[$weight_class_id])) {
|
| 99: | return $this->weights[$weight_class_id]['unit'];
|
| 100: | } else {
|
| 101: | return '';
|
| 102: | }
|
| 103: | }
|
| 104: | }
|
| 105: | |