diff options
Diffstat (limited to 'public/system/library/cart')
-rw-r--r-- | public/system/library/cart/cart.php | 407 | ||||
-rw-r--r-- | public/system/library/cart/currency.php | 115 | ||||
-rw-r--r-- | public/system/library/cart/customer.php | 132 | ||||
-rw-r--r-- | public/system/library/cart/length.php | 57 | ||||
-rw-r--r-- | public/system/library/cart/tax.php | 128 | ||||
-rw-r--r-- | public/system/library/cart/user.php | 95 | ||||
-rw-r--r-- | public/system/library/cart/weight.php | 57 |
7 files changed, 991 insertions, 0 deletions
diff --git a/public/system/library/cart/cart.php b/public/system/library/cart/cart.php new file mode 100644 index 0000000..b283c65 --- /dev/null +++ b/public/system/library/cart/cart.php @@ -0,0 +1,407 @@ +<?php +namespace Cart; +class Cart { + private $data = array(); + + public function __construct($registry) { + $this->config = $registry->get('config'); + $this->customer = $registry->get('customer'); + $this->session = $registry->get('session'); + $this->db = $registry->get('db'); + $this->tax = $registry->get('tax'); + $this->weight = $registry->get('weight'); + + // Remove all the expired carts with no customer ID + $this->db->query("DELETE FROM " . DB_PREFIX . "cart WHERE (api_id > '0' OR customer_id = '0') AND date_added < DATE_SUB(NOW(), INTERVAL 1 HOUR)"); + + if ($this->customer->getId()) { + // We want to change the session ID on all the old items in the customers cart + $this->db->query("UPDATE " . DB_PREFIX . "cart SET session_id = '" . $this->db->escape($this->session->getId()) . "' WHERE api_id = '0' AND customer_id = '" . (int)$this->customer->getId() . "'"); + + // Once the customer is logged in we want to update the customers cart + $cart_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '0' AND customer_id = '0' AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); + + foreach ($cart_query->rows as $cart) { + $this->db->query("DELETE FROM " . DB_PREFIX . "cart WHERE cart_id = '" . (int)$cart['cart_id'] . "'"); + + // The advantage of using $this->add is that it will check if the products already exist and increaser the quantity if necessary. + $this->add($cart['product_id'], $cart['quantity'], json_decode($cart['option']), $cart['recurring_id']); + } + } + } + + public function getProducts() { + $product_data = array(); + + $cart_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); + + foreach ($cart_query->rows as $cart) { + $stock = true; + + $product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_store p2s LEFT JOIN " . DB_PREFIX . "product p ON (p2s.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND p2s.product_id = '" . (int)$cart['product_id'] . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.date_available <= NOW() AND p.status = '1'"); + + if ($product_query->num_rows && ($cart['quantity'] > 0)) { + $option_price = 0; + $option_points = 0; + $option_weight = 0; + + $option_data = array(); + + foreach (json_decode($cart['option']) as $product_option_id => $value) { + $option_query = $this->db->query("SELECT po.product_option_id, po.option_id, od.name, o.type FROM " . DB_PREFIX . "product_option po LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id) LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id) WHERE po.product_option_id = '" . (int)$product_option_id . "' AND po.product_id = '" . (int)$cart['product_id'] . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'"); + + if ($option_query->num_rows) { + if ($option_query->row['type'] == 'select' || $option_query->row['type'] == 'radio') { + $option_value_query = $this->db->query("SELECT pov.option_value_id, ovd.name, pov.quantity, pov.subtract, pov.price, pov.price_prefix, pov.points, pov.points_prefix, pov.weight, pov.weight_prefix FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_option_value_id = '" . (int)$value . "' AND pov.product_option_id = '" . (int)$product_option_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'"); + + if ($option_value_query->num_rows) { + if ($option_value_query->row['price_prefix'] == '+') { + $option_price += $option_value_query->row['price']; + } elseif ($option_value_query->row['price_prefix'] == '-') { + $option_price -= $option_value_query->row['price']; + } + + if ($option_value_query->row['points_prefix'] == '+') { + $option_points += $option_value_query->row['points']; + } elseif ($option_value_query->row['points_prefix'] == '-') { + $option_points -= $option_value_query->row['points']; + } + + if ($option_value_query->row['weight_prefix'] == '+') { + $option_weight += $option_value_query->row['weight']; + } elseif ($option_value_query->row['weight_prefix'] == '-') { + $option_weight -= $option_value_query->row['weight']; + } + + if ($option_value_query->row['subtract'] && (!$option_value_query->row['quantity'] || ($option_value_query->row['quantity'] < $cart['quantity']))) { + $stock = false; + } + + $option_data[] = array( + 'product_option_id' => $product_option_id, + 'product_option_value_id' => $value, + 'option_id' => $option_query->row['option_id'], + 'option_value_id' => $option_value_query->row['option_value_id'], + 'name' => $option_query->row['name'], + 'value' => $option_value_query->row['name'], + 'type' => $option_query->row['type'], + 'quantity' => $option_value_query->row['quantity'], + 'subtract' => $option_value_query->row['subtract'], + 'price' => $option_value_query->row['price'], + 'price_prefix' => $option_value_query->row['price_prefix'], + 'points' => $option_value_query->row['points'], + 'points_prefix' => $option_value_query->row['points_prefix'], + 'weight' => $option_value_query->row['weight'], + 'weight_prefix' => $option_value_query->row['weight_prefix'] + ); + } + } elseif ($option_query->row['type'] == 'checkbox' && is_array($value)) { + foreach ($value as $product_option_value_id) { + $option_value_query = $this->db->query("SELECT pov.option_value_id, pov.quantity, pov.subtract, pov.price, pov.price_prefix, pov.points, pov.points_prefix, pov.weight, pov.weight_prefix, ovd.name FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (pov.option_value_id = ovd.option_value_id) WHERE pov.product_option_value_id = '" . (int)$product_option_value_id . "' AND pov.product_option_id = '" . (int)$product_option_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'"); + + if ($option_value_query->num_rows) { + if ($option_value_query->row['price_prefix'] == '+') { + $option_price += $option_value_query->row['price']; + } elseif ($option_value_query->row['price_prefix'] == '-') { + $option_price -= $option_value_query->row['price']; + } + + if ($option_value_query->row['points_prefix'] == '+') { + $option_points += $option_value_query->row['points']; + } elseif ($option_value_query->row['points_prefix'] == '-') { + $option_points -= $option_value_query->row['points']; + } + + if ($option_value_query->row['weight_prefix'] == '+') { + $option_weight += $option_value_query->row['weight']; + } elseif ($option_value_query->row['weight_prefix'] == '-') { + $option_weight -= $option_value_query->row['weight']; + } + + if ($option_value_query->row['subtract'] && (!$option_value_query->row['quantity'] || ($option_value_query->row['quantity'] < $cart['quantity']))) { + $stock = false; + } + + $option_data[] = array( + 'product_option_id' => $product_option_id, + 'product_option_value_id' => $product_option_value_id, + 'option_id' => $option_query->row['option_id'], + 'option_value_id' => $option_value_query->row['option_value_id'], + 'name' => $option_query->row['name'], + 'value' => $option_value_query->row['name'], + 'type' => $option_query->row['type'], + 'quantity' => $option_value_query->row['quantity'], + 'subtract' => $option_value_query->row['subtract'], + 'price' => $option_value_query->row['price'], + 'price_prefix' => $option_value_query->row['price_prefix'], + 'points' => $option_value_query->row['points'], + 'points_prefix' => $option_value_query->row['points_prefix'], + 'weight' => $option_value_query->row['weight'], + 'weight_prefix' => $option_value_query->row['weight_prefix'] + ); + } + } + } elseif ($option_query->row['type'] == 'text' || $option_query->row['type'] == 'textarea' || $option_query->row['type'] == 'file' || $option_query->row['type'] == 'date' || $option_query->row['type'] == 'datetime' || $option_query->row['type'] == 'time') { + $option_data[] = array( + 'product_option_id' => $product_option_id, + 'product_option_value_id' => '', + 'option_id' => $option_query->row['option_id'], + 'option_value_id' => '', + 'name' => $option_query->row['name'], + 'value' => $value, + 'type' => $option_query->row['type'], + 'quantity' => '', + 'subtract' => '', + 'price' => '', + 'price_prefix' => '', + 'points' => '', + 'points_prefix' => '', + 'weight' => '', + 'weight_prefix' => '' + ); + } + } + } + + $price = $product_query->row['price']; + + // Product Discounts + $discount_quantity = 0; + + foreach ($cart_query->rows as $cart_2) { + if ($cart_2['product_id'] == $cart['product_id']) { + $discount_quantity += $cart_2['quantity']; + } + } + + $product_discount_query = $this->db->query("SELECT price FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$cart['product_id'] . "' AND customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND quantity <= '" . (int)$discount_quantity . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY quantity DESC, priority ASC, price ASC LIMIT 1"); + + if ($product_discount_query->num_rows) { + $price = $product_discount_query->row['price']; + } + + // Product Specials + $product_special_query = $this->db->query("SELECT price FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$cart['product_id'] . "' AND customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY priority ASC, price ASC LIMIT 1"); + + if ($product_special_query->num_rows) { + $price = $product_special_query->row['price']; + } + + // Reward Points + $product_reward_query = $this->db->query("SELECT points FROM " . DB_PREFIX . "product_reward WHERE product_id = '" . (int)$cart['product_id'] . "' AND customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "'"); + + if ($product_reward_query->num_rows) { + $reward = $product_reward_query->row['points']; + } else { + $reward = 0; + } + + // Downloads + $download_data = array(); + + $download_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_download p2d LEFT JOIN " . DB_PREFIX . "download d ON (p2d.download_id = d.download_id) LEFT JOIN " . DB_PREFIX . "download_description dd ON (d.download_id = dd.download_id) WHERE p2d.product_id = '" . (int)$cart['product_id'] . "' AND dd.language_id = '" . (int)$this->config->get('config_language_id') . "'"); + + foreach ($download_query->rows as $download) { + $download_data[] = array( + 'download_id' => $download['download_id'], + 'name' => $download['name'], + 'filename' => $download['filename'], + 'mask' => $download['mask'] + ); + } + + // Stock + if (!$product_query->row['quantity'] || ($product_query->row['quantity'] < $cart['quantity'])) { + $stock = false; + } + + $recurring_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "recurring r LEFT JOIN " . DB_PREFIX . "product_recurring pr ON (r.recurring_id = pr.recurring_id) LEFT JOIN " . DB_PREFIX . "recurring_description rd ON (r.recurring_id = rd.recurring_id) WHERE r.recurring_id = '" . (int)$cart['recurring_id'] . "' AND pr.product_id = '" . (int)$cart['product_id'] . "' AND rd.language_id = " . (int)$this->config->get('config_language_id') . " AND r.status = 1 AND pr.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "'"); + + if ($recurring_query->num_rows) { + $recurring = array( + 'recurring_id' => $cart['recurring_id'], + 'name' => $recurring_query->row['name'], + 'frequency' => $recurring_query->row['frequency'], + 'price' => $recurring_query->row['price'], + 'cycle' => $recurring_query->row['cycle'], + 'duration' => $recurring_query->row['duration'], + 'trial' => $recurring_query->row['trial_status'], + 'trial_frequency' => $recurring_query->row['trial_frequency'], + 'trial_price' => $recurring_query->row['trial_price'], + 'trial_cycle' => $recurring_query->row['trial_cycle'], + 'trial_duration' => $recurring_query->row['trial_duration'] + ); + } else { + $recurring = false; + } + + $product_data[] = array( + 'cart_id' => $cart['cart_id'], + 'product_id' => $product_query->row['product_id'], + 'name' => $product_query->row['name'], + 'model' => $product_query->row['model'], + 'shipping' => $product_query->row['shipping'], + 'image' => $product_query->row['image'], + 'option' => $option_data, + 'download' => $download_data, + 'quantity' => $cart['quantity'], + 'minimum' => $product_query->row['minimum'], + 'subtract' => $product_query->row['subtract'], + 'stock' => $stock, + 'price' => ($price + $option_price), + 'total' => ($price + $option_price) * $cart['quantity'], + 'reward' => $reward * $cart['quantity'], + 'points' => ($product_query->row['points'] ? ($product_query->row['points'] + $option_points) * $cart['quantity'] : 0), + 'tax_class_id' => $product_query->row['tax_class_id'], + 'weight' => ($product_query->row['weight'] + $option_weight) * $cart['quantity'], + 'weight_class_id' => $product_query->row['weight_class_id'], + 'length' => $product_query->row['length'], + 'width' => $product_query->row['width'], + 'height' => $product_query->row['height'], + 'length_class_id' => $product_query->row['length_class_id'], + 'recurring' => $recurring + ); + } else { + $this->remove($cart['cart_id']); + } + } + + return $product_data; + } + + public function add($product_id, $quantity = 1, $option = array(), $recurring_id = 0) { + $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'"); + + if (!$query->row['total']) { + $this->db->query("INSERT " . DB_PREFIX . "cart SET api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "', customer_id = '" . (int)$this->customer->getId() . "', session_id = '" . $this->db->escape($this->session->getId()) . "', product_id = '" . (int)$product_id . "', recurring_id = '" . (int)$recurring_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', quantity = '" . (int)$quantity . "', date_added = NOW()"); + } else { + $this->db->query("UPDATE " . DB_PREFIX . "cart SET quantity = (quantity + " . (int)$quantity . ") WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'"); + } + } + + public function update($cart_id, $quantity) { + $this->db->query("UPDATE " . DB_PREFIX . "cart SET quantity = '" . (int)$quantity . "' WHERE cart_id = '" . (int)$cart_id . "' AND api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); + } + + public function remove($cart_id) { + $this->db->query("DELETE FROM " . DB_PREFIX . "cart WHERE cart_id = '" . (int)$cart_id . "' AND api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); + } + + public function clear() { + $this->db->query("DELETE FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'"); + } + + public function getRecurringProducts() { + $product_data = array(); + + foreach ($this->getProducts() as $value) { + if ($value['recurring']) { + $product_data[] = $value; + } + } + + return $product_data; + } + + public function getWeight() { + $weight = 0; + + foreach ($this->getProducts() as $product) { + if ($product['shipping']) { + $weight += $this->weight->convert($product['weight'], $product['weight_class_id'], $this->config->get('config_weight_class_id')); + } + } + + return $weight; + } + + public function getSubTotal() { + $total = 0; + + foreach ($this->getProducts() as $product) { + $total += $product['total']; + } + + return $total; + } + + public function getTaxes() { + $tax_data = array(); + + foreach ($this->getProducts() as $product) { + if ($product['tax_class_id']) { + $tax_rates = $this->tax->getRates($product['price'], $product['tax_class_id']); + + foreach ($tax_rates as $tax_rate) { + if (!isset($tax_data[$tax_rate['tax_rate_id']])) { + $tax_data[$tax_rate['tax_rate_id']] = ($tax_rate['amount'] * $product['quantity']); + } else { + $tax_data[$tax_rate['tax_rate_id']] += ($tax_rate['amount'] * $product['quantity']); + } + } + } + } + + return $tax_data; + } + + public function getTotal() { + $total = 0; + + foreach ($this->getProducts() as $product) { + $total += $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')) * $product['quantity']; + } + + return $total; + } + + public function countProducts() { + $product_total = 0; + + $products = $this->getProducts(); + + foreach ($products as $product) { + $product_total += $product['quantity']; + } + + return $product_total; + } + + public function hasProducts() { + return count($this->getProducts()); + } + + public function hasRecurringProducts() { + return count($this->getRecurringProducts()); + } + + public function hasStock() { + foreach ($this->getProducts() as $product) { + if (!$product['stock']) { + return false; + } + } + + return true; + } + + public function hasShipping() { + foreach ($this->getProducts() as $product) { + if ($product['shipping']) { + return true; + } + } + + return false; + } + + public function hasDownload() { + foreach ($this->getProducts() as $product) { + if ($product['download']) { + return true; + } + } + + return false; + } +} diff --git a/public/system/library/cart/currency.php b/public/system/library/cart/currency.php new file mode 100644 index 0000000..02026bb --- /dev/null +++ b/public/system/library/cart/currency.php @@ -0,0 +1,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]); + } +} diff --git a/public/system/library/cart/customer.php b/public/system/library/cart/customer.php new file mode 100644 index 0000000..c3de558 --- /dev/null +++ b/public/system/library/cart/customer.php @@ -0,0 +1,132 @@ +<?php +namespace Cart; +class Customer { + private $customer_id; + private $firstname; + private $lastname; + private $customer_group_id; + private $email; + private $telephone; + private $newsletter; + private $address_id; + + public function __construct($registry) { + $this->config = $registry->get('config'); + $this->db = $registry->get('db'); + $this->request = $registry->get('request'); + $this->session = $registry->get('session'); + + if (isset($this->session->data['customer_id'])) { + $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$this->session->data['customer_id'] . "' AND status = '1'"); + + if ($customer_query->num_rows) { + $this->customer_id = $customer_query->row['customer_id']; + $this->firstname = $customer_query->row['firstname']; + $this->lastname = $customer_query->row['lastname']; + $this->customer_group_id = $customer_query->row['customer_group_id']; + $this->email = $customer_query->row['email']; + $this->telephone = $customer_query->row['telephone']; + $this->newsletter = $customer_query->row['newsletter']; + $this->address_id = $customer_query->row['address_id']; + + $this->db->query("UPDATE " . DB_PREFIX . "customer SET language_id = '" . (int)$this->config->get('config_language_id') . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$this->customer_id . "'"); + + $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_ip WHERE customer_id = '" . (int)$this->session->data['customer_id'] . "' AND ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "'"); + + if (!$query->num_rows) { + $this->db->query("INSERT INTO " . DB_PREFIX . "customer_ip SET customer_id = '" . (int)$this->session->data['customer_id'] . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', date_added = NOW()"); + } + } else { + $this->logout(); + } + } + } + + public function login($email, $password, $override = false) { + if ($override) { + $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "' AND status = '1'"); + } else { + $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "' AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1'"); + } + + if ($customer_query->num_rows) { + $this->session->data['customer_id'] = $customer_query->row['customer_id']; + + $this->customer_id = $customer_query->row['customer_id']; + $this->firstname = $customer_query->row['firstname']; + $this->lastname = $customer_query->row['lastname']; + $this->customer_group_id = $customer_query->row['customer_group_id']; + $this->email = $customer_query->row['email']; + $this->telephone = $customer_query->row['telephone']; + $this->newsletter = $customer_query->row['newsletter']; + $this->address_id = $customer_query->row['address_id']; + + $this->db->query("UPDATE " . DB_PREFIX . "customer SET language_id = '" . (int)$this->config->get('config_language_id') . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$this->customer_id . "'"); + + return true; + } else { + return false; + } + } + + public function logout() { + unset($this->session->data['customer_id']); + + $this->customer_id = ''; + $this->firstname = ''; + $this->lastname = ''; + $this->customer_group_id = ''; + $this->email = ''; + $this->telephone = ''; + $this->newsletter = ''; + $this->address_id = ''; + } + + public function isLogged() { + return $this->customer_id; + } + + public function getId() { + return $this->customer_id; + } + + public function getFirstName() { + return $this->firstname; + } + + public function getLastName() { + return $this->lastname; + } + + public function getGroupId() { + return $this->customer_group_id; + } + + public function getEmail() { + return $this->email; + } + + public function getTelephone() { + return $this->telephone; + } + + public function getNewsletter() { + return $this->newsletter; + } + + public function getAddressId() { + return $this->address_id; + } + + public function getBalance() { + $query = $this->db->query("SELECT SUM(amount) AS total FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$this->customer_id . "'"); + + return $query->row['total']; + } + + public function getRewardPoints() { + $query = $this->db->query("SELECT SUM(points) AS total FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$this->customer_id . "'"); + + return $query->row['total']; + } +} diff --git a/public/system/library/cart/length.php b/public/system/library/cart/length.php new file mode 100644 index 0000000..65af015 --- /dev/null +++ b/public/system/library/cart/length.php @@ -0,0 +1,57 @@ +<?php +namespace Cart; +class Length { + private $lengths = array(); + + public function __construct($registry) { + $this->db = $registry->get('db'); + $this->config = $registry->get('config'); + + $length_class_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "length_class mc LEFT JOIN " . DB_PREFIX . "length_class_description mcd ON (mc.length_class_id = mcd.length_class_id) WHERE mcd.language_id = '" . (int)$this->config->get('config_language_id') . "'"); + + foreach ($length_class_query->rows as $result) { + $this->lengths[$result['length_class_id']] = array( + 'length_class_id' => $result['length_class_id'], + 'title' => $result['title'], + 'unit' => $result['unit'], + 'value' => $result['value'] + ); + } + } + + public function convert($value, $from, $to) { + if ($from == $to) { + return $value; + } + + if (isset($this->lengths[$from])) { + $from = $this->lengths[$from]['value']; + } else { + $from = 1; + } + + if (isset($this->lengths[$to])) { + $to = $this->lengths[$to]['value']; + } else { + $to = 1; + } + + return $value * ($to / $from); + } + + public function format($value, $length_class_id, $decimal_point = '.', $thousand_point = ',') { + if (isset($this->lengths[$length_class_id])) { + return number_format($value, 2, $decimal_point, $thousand_point) . $this->lengths[$length_class_id]['unit']; + } else { + return number_format($value, 2, $decimal_point, $thousand_point); + } + } + + public function getUnit($length_class_id) { + if (isset($this->lengths[$length_class_id])) { + return $this->lengths[$length_class_id]['unit']; + } else { + return ''; + } + } +} diff --git a/public/system/library/cart/tax.php b/public/system/library/cart/tax.php new file mode 100644 index 0000000..d535c23 --- /dev/null +++ b/public/system/library/cart/tax.php @@ -0,0 +1,128 @@ +<?php +namespace Cart; +final class Tax { + private $tax_rates = array(); + + public function __construct($registry) { + $this->config = $registry->get('config'); + $this->db = $registry->get('db'); + } + + public function unsetRates() { + $this->tax_rates = array(); + } + + public function setShippingAddress($country_id, $zone_id) { + $tax_query = $this->db->query("SELECT tr1.tax_class_id, tr2.tax_rate_id, tr2.name, tr2.rate, tr2.type, tr1.priority FROM " . DB_PREFIX . "tax_rule tr1 LEFT JOIN " . DB_PREFIX . "tax_rate tr2 ON (tr1.tax_rate_id = tr2.tax_rate_id) INNER JOIN " . DB_PREFIX . "tax_rate_to_customer_group tr2cg ON (tr2.tax_rate_id = tr2cg.tax_rate_id) LEFT JOIN " . DB_PREFIX . "zone_to_geo_zone z2gz ON (tr2.geo_zone_id = z2gz.geo_zone_id) LEFT JOIN " . DB_PREFIX . "geo_zone gz ON (tr2.geo_zone_id = gz.geo_zone_id) WHERE tr1.based = 'shipping' AND tr2cg.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND z2gz.country_id = '" . (int)$country_id . "' AND (z2gz.zone_id = '0' OR z2gz.zone_id = '" . (int)$zone_id . "') ORDER BY tr1.priority ASC"); + + foreach ($tax_query->rows as $result) { + $this->tax_rates[$result['tax_class_id']][$result['tax_rate_id']] = array( + 'tax_rate_id' => $result['tax_rate_id'], + 'name' => $result['name'], + 'rate' => $result['rate'], + 'type' => $result['type'], + 'priority' => $result['priority'] + ); + } + } + + public function setPaymentAddress($country_id, $zone_id) { + $tax_query = $this->db->query("SELECT tr1.tax_class_id, tr2.tax_rate_id, tr2.name, tr2.rate, tr2.type, tr1.priority FROM " . DB_PREFIX . "tax_rule tr1 LEFT JOIN " . DB_PREFIX . "tax_rate tr2 ON (tr1.tax_rate_id = tr2.tax_rate_id) INNER JOIN " . DB_PREFIX . "tax_rate_to_customer_group tr2cg ON (tr2.tax_rate_id = tr2cg.tax_rate_id) LEFT JOIN " . DB_PREFIX . "zone_to_geo_zone z2gz ON (tr2.geo_zone_id = z2gz.geo_zone_id) LEFT JOIN " . DB_PREFIX . "geo_zone gz ON (tr2.geo_zone_id = gz.geo_zone_id) WHERE tr1.based = 'payment' AND tr2cg.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND z2gz.country_id = '" . (int)$country_id . "' AND (z2gz.zone_id = '0' OR z2gz.zone_id = '" . (int)$zone_id . "') ORDER BY tr1.priority ASC"); + + foreach ($tax_query->rows as $result) { + $this->tax_rates[$result['tax_class_id']][$result['tax_rate_id']] = array( + 'tax_rate_id' => $result['tax_rate_id'], + 'name' => $result['name'], + 'rate' => $result['rate'], + 'type' => $result['type'], + 'priority' => $result['priority'] + ); + } + } + + public function setStoreAddress($country_id, $zone_id) { + $tax_query = $this->db->query("SELECT tr1.tax_class_id, tr2.tax_rate_id, tr2.name, tr2.rate, tr2.type, tr1.priority FROM " . DB_PREFIX . "tax_rule tr1 LEFT JOIN " . DB_PREFIX . "tax_rate tr2 ON (tr1.tax_rate_id = tr2.tax_rate_id) INNER JOIN " . DB_PREFIX . "tax_rate_to_customer_group tr2cg ON (tr2.tax_rate_id = tr2cg.tax_rate_id) LEFT JOIN " . DB_PREFIX . "zone_to_geo_zone z2gz ON (tr2.geo_zone_id = z2gz.geo_zone_id) LEFT JOIN " . DB_PREFIX . "geo_zone gz ON (tr2.geo_zone_id = gz.geo_zone_id) WHERE tr1.based = 'store' AND tr2cg.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND z2gz.country_id = '" . (int)$country_id . "' AND (z2gz.zone_id = '0' OR z2gz.zone_id = '" . (int)$zone_id . "') ORDER BY tr1.priority ASC"); + + foreach ($tax_query->rows as $result) { + $this->tax_rates[$result['tax_class_id']][$result['tax_rate_id']] = array( + 'tax_rate_id' => $result['tax_rate_id'], + 'name' => $result['name'], + 'rate' => $result['rate'], + 'type' => $result['type'], + 'priority' => $result['priority'] + ); + } + } + + public function calculate($value, $tax_class_id, $calculate = true) { + if ($tax_class_id && $calculate) { + $amount = 0; + + $tax_rates = $this->getRates($value, $tax_class_id); + + foreach ($tax_rates as $tax_rate) { + if ($calculate != 'P' && $calculate != 'F') { + $amount += $tax_rate['amount']; + } elseif ($tax_rate['type'] == $calculate) { + $amount += $tax_rate['amount']; + } + } + + return $value + $amount; + } else { + return $value; + } + } + + public function getTax($value, $tax_class_id) { + $amount = 0; + + $tax_rates = $this->getRates($value, $tax_class_id); + + foreach ($tax_rates as $tax_rate) { + $amount += $tax_rate['amount']; + } + + return $amount; + } + + public function getRateName($tax_rate_id) { + $tax_query = $this->db->query("SELECT name FROM " . DB_PREFIX . "tax_rate WHERE tax_rate_id = '" . (int)$tax_rate_id . "'"); + + if ($tax_query->num_rows) { + return $tax_query->row['name']; + } else { + return false; + } + } + + public function getRates($value, $tax_class_id) { + $tax_rate_data = array(); + + if (isset($this->tax_rates[$tax_class_id])) { + foreach ($this->tax_rates[$tax_class_id] as $tax_rate) { + if (isset($tax_rate_data[$tax_rate['tax_rate_id']])) { + $amount = $tax_rate_data[$tax_rate['tax_rate_id']]['amount']; + } else { + $amount = 0; + } + + if ($tax_rate['type'] == 'F') { + $amount += $tax_rate['rate']; + } elseif ($tax_rate['type'] == 'P') { + $amount += ($value / 100 * $tax_rate['rate']); + } + + $tax_rate_data[$tax_rate['tax_rate_id']] = array( + 'tax_rate_id' => $tax_rate['tax_rate_id'], + 'name' => $tax_rate['name'], + 'rate' => $tax_rate['rate'], + 'type' => $tax_rate['type'], + 'amount' => $amount + ); + } + } + + return $tax_rate_data; + } +} diff --git a/public/system/library/cart/user.php b/public/system/library/cart/user.php new file mode 100644 index 0000000..ca1d09e --- /dev/null +++ b/public/system/library/cart/user.php @@ -0,0 +1,95 @@ +<?php +namespace Cart; +class User { + private $user_id; + private $user_group_id; + private $username; + private $permission = array(); + + public function __construct($registry) { + $this->db = $registry->get('db'); + $this->request = $registry->get('request'); + $this->session = $registry->get('session'); + + if (isset($this->session->data['user_id'])) { + $user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE user_id = '" . (int)$this->session->data['user_id'] . "' AND status = '1'"); + + if ($user_query->num_rows) { + $this->user_id = $user_query->row['user_id']; + $this->username = $user_query->row['username']; + $this->user_group_id = $user_query->row['user_group_id']; + + $this->db->query("UPDATE " . DB_PREFIX . "user SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE user_id = '" . (int)$this->session->data['user_id'] . "'"); + + $user_group_query = $this->db->query("SELECT permission FROM " . DB_PREFIX . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'"); + + $permissions = json_decode($user_group_query->row['permission'], true); + + if (is_array($permissions)) { + foreach ($permissions as $key => $value) { + $this->permission[$key] = $value; + } + } + } else { + $this->logout(); + } + } + } + + public function login($username, $password) { + $user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE username = '" . $this->db->escape($username) . "' AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1'"); + + if ($user_query->num_rows) { + $this->session->data['user_id'] = $user_query->row['user_id']; + + $this->user_id = $user_query->row['user_id']; + $this->username = $user_query->row['username']; + $this->user_group_id = $user_query->row['user_group_id']; + + $user_group_query = $this->db->query("SELECT permission FROM " . DB_PREFIX . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'"); + + $permissions = json_decode($user_group_query->row['permission'], true); + + if (is_array($permissions)) { + foreach ($permissions as $key => $value) { + $this->permission[$key] = $value; + } + } + + return true; + } else { + return false; + } + } + + public function logout() { + unset($this->session->data['user_id']); + + $this->user_id = ''; + $this->username = ''; + } + + public function hasPermission($key, $value) { + if (isset($this->permission[$key])) { + return in_array($value, $this->permission[$key]); + } else { + return false; + } + } + + public function isLogged() { + return $this->user_id; + } + + public function getId() { + return $this->user_id; + } + + public function getUserName() { + return $this->username; + } + + public function getGroupId() { + return $this->user_group_id; + } +}
\ No newline at end of file diff --git a/public/system/library/cart/weight.php b/public/system/library/cart/weight.php new file mode 100644 index 0000000..a0df809 --- /dev/null +++ b/public/system/library/cart/weight.php @@ -0,0 +1,57 @@ +<?php +namespace Cart; +class Weight { + private $weights = array(); + + public function __construct($registry) { + $this->db = $registry->get('db'); + $this->config = $registry->get('config'); + + $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') . "'"); + + foreach ($weight_class_query->rows as $result) { + $this->weights[$result['weight_class_id']] = array( + 'weight_class_id' => $result['weight_class_id'], + 'title' => $result['title'], + 'unit' => $result['unit'], + 'value' => $result['value'] + ); + } + } + + public function convert($value, $from, $to) { + if ($from == $to) { + return $value; + } + + if (isset($this->weights[$from])) { + $from = $this->weights[$from]['value']; + } else { + $from = 1; + } + + if (isset($this->weights[$to])) { + $to = $this->weights[$to]['value']; + } else { + $to = 1; + } + + return $value * ($to / $from); + } + + public function format($value, $weight_class_id, $decimal_point = '.', $thousand_point = ',') { + if (isset($this->weights[$weight_class_id])) { + return number_format($value, 2, $decimal_point, $thousand_point) . $this->weights[$weight_class_id]['unit']; + } else { + return number_format($value, 2, $decimal_point, $thousand_point); + } + } + + public function getUnit($weight_class_id) { + if (isset($this->weights[$weight_class_id])) { + return $this->weights[$weight_class_id]['unit']; + } else { + return ''; + } + } +}
\ No newline at end of file |