first commit
This commit is contained in:
600
system/library/cart/cart.php
Normal file
600
system/library/cart/cart.php
Normal file
@ -0,0 +1,600 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cart;
|
||||
/**
|
||||
* Class Cart
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Cart {
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $db;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $config;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $customer;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $session;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $tax;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $weight;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $data = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $registry) {
|
||||
$this->db = $registry->get('db');
|
||||
$this->config = $registry->get('config');
|
||||
$this->customer = $registry->get('customer');
|
||||
$this->session = $registry->get('session');
|
||||
$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->isLogged()) {
|
||||
// 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 increase the quantity if necessary.
|
||||
$this->add($cart['product_id'], $cart['quantity'], json_decode($cart['option'], true), $cart['subscription_plan_id'], $cart['override'], $cart['price']);
|
||||
}
|
||||
}
|
||||
|
||||
// Populate the cart data
|
||||
$this->data = $this->getProducts();
|
||||
}
|
||||
|
||||
/**
|
||||
* getProducts
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getProducts(): array {
|
||||
if (!$this->data) {
|
||||
$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 = [];
|
||||
|
||||
$product_options = (array)json_decode($cart['option'], true);
|
||||
|
||||
// Merge variant code with options
|
||||
$variant = json_decode($product_query->row['variant'], true);
|
||||
|
||||
if ($variant) {
|
||||
foreach ($variant as $key => $value) {
|
||||
$product_options[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($product_options as $product_option_id => $value) {
|
||||
if (!$product_query->row['master_id']) {
|
||||
$product_id = $cart['product_id'];
|
||||
} else {
|
||||
$product_id = $product_query->row['master_id'];
|
||||
}
|
||||
|
||||
$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)$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[] = [
|
||||
'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[] = ['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[] = ['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'];
|
||||
}
|
||||
|
||||
$product_total = 0;
|
||||
|
||||
foreach ($cart_query->rows as $cart_2) {
|
||||
if ($cart_2['product_id'] == $cart['product_id']) {
|
||||
$product_total += $cart_2['quantity'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($product_query->row['minimum'] > $product_total) {
|
||||
$minimum = false;
|
||||
} else {
|
||||
$minimum = true;
|
||||
}
|
||||
|
||||
// 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 = [];
|
||||
|
||||
$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[] = [
|
||||
'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;
|
||||
}
|
||||
|
||||
$subscription_data = [];
|
||||
|
||||
$subscription_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product_subscription` ps LEFT JOIN `" . DB_PREFIX . "subscription_plan` sp ON (ps.`subscription_plan_id` = sp.`subscription_plan_id`) LEFT JOIN `" . DB_PREFIX . "subscription_plan_description` spd ON (sp.`subscription_plan_id` = spd.`subscription_plan_id`) WHERE ps.`product_id` = '" . (int)$cart['product_id'] . "' AND ps.`subscription_plan_id` = '" . (int)$cart['subscription_plan_id'] . "' AND ps.`customer_group_id` = '" . (int)$this->config->get('config_customer_group_id') . "' AND spd.`language_id` = '" . (int)$this->config->get('config_language_id') . "' AND sp.`status` = '1'");
|
||||
|
||||
if ($subscription_query->num_rows) {
|
||||
$price = $subscription_query->row['price'];
|
||||
|
||||
if ($subscription_query->row['trial_status']) {
|
||||
$price = $subscription_query->row['trial_price'];
|
||||
}
|
||||
|
||||
$subscription_data = [
|
||||
'subscription_plan_id' => $subscription_query->row['subscription_plan_id'],
|
||||
'name' => $subscription_query->row['name'],
|
||||
'trial_price' => $subscription_query->row['trial_price'],
|
||||
'trial_frequency' => $subscription_query->row['trial_frequency'],
|
||||
'trial_cycle' => $subscription_query->row['trial_cycle'],
|
||||
'trial_duration' => $subscription_query->row['trial_duration'],
|
||||
'trial_remaining' => $subscription_query->row['trial_duration'],
|
||||
'trial_status' => $subscription_query->row['trial_status'],
|
||||
'price' => $subscription_query->row['price'],
|
||||
'frequency' => $subscription_query->row['frequency'],
|
||||
'cycle' => $subscription_query->row['cycle'],
|
||||
'duration' => $subscription_query->row['duration'],
|
||||
'remaining' => $subscription_query->row['duration']
|
||||
];
|
||||
}
|
||||
|
||||
if ($cart['override']) {
|
||||
$price = $cart['price'];
|
||||
}
|
||||
|
||||
$this->data[$cart['cart_id']] = [
|
||||
'cart_id' => $cart['cart_id'],
|
||||
'product_id' => $product_query->row['product_id'],
|
||||
'master_id' => $product_query->row['master_id'],
|
||||
'name' => $product_query->row['name'],
|
||||
'model' => $product_query->row['model'],
|
||||
'shipping' => $product_query->row['shipping'],
|
||||
'image' => $product_query->row['image'],
|
||||
'option' => $option_data,
|
||||
'subscription' => $subscription_data,
|
||||
'download' => $download_data,
|
||||
'quantity' => $cart['quantity'],
|
||||
'minimum' => $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']
|
||||
];
|
||||
} else {
|
||||
$this->remove($cart['cart_id']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add
|
||||
*
|
||||
* @param int $product_id
|
||||
* @param int $quantity
|
||||
* @param array $option
|
||||
* @param int $subscription_plan_id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add(int $product_id, int $quantity = 1, array $option = [], int $subscription_plan_id = 0, bool $override = false, float $price = 0): void {
|
||||
$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 `subscription_plan_id` = '" . (int)$subscription_plan_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
|
||||
|
||||
if (!$query->row['total']) {
|
||||
$this->db->query("INSERT INTO `" . 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 . "', `subscription_plan_id` = '" . (int)$subscription_plan_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', `quantity` = '" . (int)$quantity . "', `override` = '" . (bool)$override . "', `price` = '" . (float)($override ? $price : 0) . "', `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 `subscription_plan_id` = '" . (int)$subscription_plan_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
|
||||
}
|
||||
|
||||
// Populate the cart data
|
||||
$this->data = $this->getProducts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update
|
||||
*
|
||||
* @param int $cart_id
|
||||
* @param int $quantity
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function update(int $cart_id, int $quantity): void {
|
||||
$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()) . "'");
|
||||
|
||||
// Populate the cart data
|
||||
$this->data = $this->getProducts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Has
|
||||
*
|
||||
* @param int $cart_id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(int $cart_id): bool {
|
||||
return isset($this->data[$cart_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove
|
||||
*
|
||||
* @param int $cart_id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function remove(int $cart_id): void {
|
||||
$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()) . "'");
|
||||
|
||||
unset($this->data[$cart_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear(): void {
|
||||
$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()) . "'");
|
||||
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* getSubscription
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSubscriptions(): array {
|
||||
$product_data = [];
|
||||
|
||||
foreach ($this->getProducts() as $value) {
|
||||
if ($value['subscription']) {
|
||||
$product_data[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $product_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* getWeight
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getWeight(): float {
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* getSubTotal
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getSubTotal(): float {
|
||||
$total = 0;
|
||||
|
||||
foreach ($this->getProducts() as $product) {
|
||||
$total += $product['total'];
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* getTaxes
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTaxes(): array {
|
||||
$tax_data = [];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* getTotal
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getTotal(): float {
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* countProducts
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countProducts(): int {
|
||||
$product_total = 0;
|
||||
|
||||
$products = $this->getProducts();
|
||||
|
||||
foreach ($products as $product) {
|
||||
$product_total += $product['quantity'];
|
||||
}
|
||||
|
||||
return $product_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* hadProducts
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasProducts(): bool {
|
||||
return (bool)count($this->getProducts());
|
||||
}
|
||||
|
||||
/**
|
||||
* hasSubscription
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSubscription(): bool {
|
||||
return (bool)count($this->getSubscriptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* hasStock
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasStock(): bool {
|
||||
foreach ($this->getProducts() as $product) {
|
||||
if (!$product['stock']) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* hasShipping
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasShipping(): bool {
|
||||
foreach ($this->getProducts() as $product) {
|
||||
if ($product['shipping']) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* hasDownload
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasDownload(): bool {
|
||||
foreach ($this->getProducts() as $product) {
|
||||
if ($product['download']) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
195
system/library/cart/currency.php
Normal file
195
system/library/cart/currency.php
Normal file
@ -0,0 +1,195 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cart;
|
||||
/**
|
||||
* Class Currency
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Currency {
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $db;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $language;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $currencies = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $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']] = ['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']];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format
|
||||
*
|
||||
* @param float $number
|
||||
* @param string $currency
|
||||
* @param float $value
|
||||
* @param bool $format
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function format(float $number, string $currency, float $value = 0, bool $format = true): string {
|
||||
if (!isset($this->currencies[$currency])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$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, $decimal_place);
|
||||
|
||||
if (!$format) {
|
||||
return $amount;
|
||||
}
|
||||
|
||||
$string = '';
|
||||
|
||||
if ($symbol_left) {
|
||||
$string .= $symbol_left;
|
||||
}
|
||||
|
||||
$string .= number_format($amount, $decimal_place, $this->language->get('decimal_point'), $this->language->get('thousand_point'));
|
||||
|
||||
if ($symbol_right) {
|
||||
$string .= $symbol_right;
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert
|
||||
*
|
||||
* @param float $value
|
||||
* @param string $from
|
||||
* @param string $to
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function convert(float $value, string $from, string $to): float {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* getId
|
||||
*
|
||||
* @param string $currency
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId(string $currency): int {
|
||||
if (isset($this->currencies[$currency])) {
|
||||
return $this->currencies[$currency]['currency_id'];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getSymbolLeft
|
||||
*
|
||||
* @param string $currency
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSymbolLeft(string $currency): string {
|
||||
if (isset($this->currencies[$currency])) {
|
||||
return $this->currencies[$currency]['symbol_left'];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getSymbolRight
|
||||
*
|
||||
* @param string $currency
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSymbolRight(string $currency): string {
|
||||
if (isset($this->currencies[$currency])) {
|
||||
return $this->currencies[$currency]['symbol_right'];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getDecimalPlace
|
||||
*
|
||||
* @param string $currency
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDecimalPlace(string $currency): string {
|
||||
if (isset($this->currencies[$currency])) {
|
||||
return $this->currencies[$currency]['decimal_place'];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getValue
|
||||
*
|
||||
* @param string $currency
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
|
||||
public function getValue(string $currency): float {
|
||||
if (isset($this->currencies[$currency])) {
|
||||
return $this->currencies[$currency]['value'];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Has
|
||||
*
|
||||
* @param string $currency
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $currency): bool {
|
||||
return isset($this->currencies[$currency]);
|
||||
}
|
||||
}
|
256
system/library/cart/customer.php
Normal file
256
system/library/cart/customer.php
Normal file
@ -0,0 +1,256 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cart;
|
||||
/**
|
||||
* Class Customer
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Customer {
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $db;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $config;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $request;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $session;
|
||||
/**
|
||||
* @var int|mixed
|
||||
*/
|
||||
private int $customer_id = 0;
|
||||
/**
|
||||
* @var string|mixed
|
||||
*/
|
||||
private string $firstname = '';
|
||||
/**
|
||||
* @var string|mixed
|
||||
*/
|
||||
private string $lastname = '';
|
||||
/**
|
||||
* @var int|mixed
|
||||
*/
|
||||
private int $customer_group_id = 0;
|
||||
/**
|
||||
* @var string|mixed
|
||||
*/
|
||||
private string $email = '';
|
||||
/**
|
||||
* @var string|mixed
|
||||
*/
|
||||
private string $telephone = '';
|
||||
/**
|
||||
* @var bool|mixed
|
||||
*/
|
||||
private bool $newsletter = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $registry) {
|
||||
$this->db = $registry->get('db');
|
||||
$this->config = $registry->get('config');
|
||||
$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->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 . "'");
|
||||
} else {
|
||||
$this->logout();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Login
|
||||
*
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @param bool $override
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function login(string $email, string $password, bool $override = false): bool {
|
||||
$customer_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer` WHERE LCASE(`email`) = '" . $this->db->escape(oc_strtolower($email)) . "' AND `status` = '1'");
|
||||
|
||||
if ($customer_query->row) {
|
||||
if (!$override) {
|
||||
if (password_verify($password, $customer_query->row['password'])) {
|
||||
$rehash = password_needs_rehash($customer_query->row['password'], PASSWORD_DEFAULT);
|
||||
} elseif (isset($customer_query->row['salt']) && $customer_query->row['password'] == sha1($customer_query->row['salt'] . sha1($customer_query->row['salt'] . sha1($password)))) {
|
||||
$rehash = true;
|
||||
} elseif ($customer_query->row['password'] == md5($password)) {
|
||||
$rehash = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($rehash) {
|
||||
$this->db->query("UPDATE `" . DB_PREFIX . "customer` SET `password` = '" . $this->db->escape(password_hash($password, PASSWORD_DEFAULT)) . "' WHERE `customer_id` = '" . (int)$customer_query->row['customer_id'] . "'");
|
||||
}
|
||||
}
|
||||
|
||||
$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->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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function logout(): void {
|
||||
unset($this->session->data['customer_id']);
|
||||
|
||||
$this->customer_id = 0;
|
||||
$this->firstname = '';
|
||||
$this->lastname = '';
|
||||
$this->customer_group_id = 0;
|
||||
$this->email = '';
|
||||
$this->telephone = '';
|
||||
$this->newsletter = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* isLogged
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLogged(): bool {
|
||||
return $this->customer_id ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* getId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int {
|
||||
return $this->customer_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* getFirstName
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstName(): string {
|
||||
return $this->firstname;
|
||||
}
|
||||
|
||||
/**
|
||||
* getLastName
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastName(): string {
|
||||
return $this->lastname;
|
||||
}
|
||||
|
||||
/**
|
||||
* getGroupId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getGroupId(): int {
|
||||
return $this->customer_group_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* getEmail
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail(): string {
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* getTelephone
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTelephone(): string {
|
||||
return $this->telephone;
|
||||
}
|
||||
|
||||
/**
|
||||
* getNewsletter
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getNewsletter(): bool {
|
||||
return $this->newsletter;
|
||||
}
|
||||
|
||||
/**
|
||||
* getAddressId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getAddressId(): int {
|
||||
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "address` WHERE `customer_id` = '" . (int)$this->customer_id . "' AND `default` = '1'");
|
||||
|
||||
if ($query->num_rows) {
|
||||
return (int)$query->row['address_id'];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getBalance
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getBalance(): float {
|
||||
$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$this->customer_id . "'");
|
||||
|
||||
return (float)$query->row['total'];
|
||||
}
|
||||
|
||||
/**
|
||||
* getRewardPoints
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getRewardPoints(): float {
|
||||
$query = $this->db->query("SELECT SUM(`points`) AS `total` FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$this->customer_id . "'");
|
||||
|
||||
return (float)$query->row['total'];
|
||||
}
|
||||
}
|
104
system/library/cart/length.php
Normal file
104
system/library/cart/length.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cart;
|
||||
/**
|
||||
* Class Length
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Length {
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $db;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $config;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $lengths = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $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']] = [
|
||||
'length_class_id' => $result['length_class_id'],
|
||||
'title' => $result['title'],
|
||||
'unit' => $result['unit'],
|
||||
'value' => $result['value']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert
|
||||
*
|
||||
* @param float $value
|
||||
* @param string $from
|
||||
* @param string $to
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function convert(float $value, string $from, string $to): float {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format
|
||||
*
|
||||
* @param float $value
|
||||
* @param int $length_class_id
|
||||
* @param string $decimal_point
|
||||
* @param string $thousand_point
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function format(float $value, int $length_class_id, string $decimal_point = '.', string $thousand_point = ','): string {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getUnit
|
||||
*
|
||||
* @param int $length_class_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUnit(int $length_class_id): string {
|
||||
if (isset($this->lengths[$length_class_id])) {
|
||||
return $this->lengths[$length_class_id]['unit'];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
210
system/library/cart/tax.php
Normal file
210
system/library/cart/tax.php
Normal file
@ -0,0 +1,210 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cart;
|
||||
/**
|
||||
* Class Tax
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Tax {
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $db;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $config;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $tax_rates = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $registry) {
|
||||
$this->db = $registry->get('db');
|
||||
$this->config = $registry->get('config');
|
||||
}
|
||||
|
||||
/**
|
||||
* setShippingAddress
|
||||
*
|
||||
* @param int $country_id
|
||||
* @param int $zone_id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setShippingAddress(int $country_id, int $zone_id): void {
|
||||
$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']] = [
|
||||
'tax_rate_id' => $result['tax_rate_id'],
|
||||
'name' => $result['name'],
|
||||
'rate' => $result['rate'],
|
||||
'type' => $result['type'],
|
||||
'priority' => $result['priority']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* setPaymentAddress
|
||||
*
|
||||
* @param int $country_id
|
||||
* @param int $zone_id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setPaymentAddress(int $country_id, int $zone_id): void {
|
||||
$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']] = [
|
||||
'tax_rate_id' => $result['tax_rate_id'],
|
||||
'name' => $result['name'],
|
||||
'rate' => $result['rate'],
|
||||
'type' => $result['type'],
|
||||
'priority' => $result['priority']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* setStoreAddress
|
||||
*
|
||||
* @param int $country_id
|
||||
* @param int $zone_id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setStoreAddress(int $country_id, int $zone_id): void {
|
||||
$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']] = [
|
||||
'tax_rate_id' => $result['tax_rate_id'],
|
||||
'name' => $result['name'],
|
||||
'rate' => $result['rate'],
|
||||
'type' => $result['type'],
|
||||
'priority' => $result['priority']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate
|
||||
*
|
||||
* @param float $value
|
||||
* @param int $tax_class_id
|
||||
* @param bool $calculate
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function calculate(float $value, int $tax_class_id, bool $calculate = true): float {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getTax
|
||||
*
|
||||
* @param float $value
|
||||
* @param int $tax_class_id
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getTax(float $value, int $tax_class_id): float {
|
||||
$amount = 0;
|
||||
|
||||
$tax_rates = $this->getRates($value, $tax_class_id);
|
||||
|
||||
foreach ($tax_rates as $tax_rate) {
|
||||
$amount += $tax_rate['amount'];
|
||||
}
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* getRateName
|
||||
*
|
||||
* @param int $tax_rate_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRateName(int $tax_rate_id): string {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getRates
|
||||
*
|
||||
* @param float $value
|
||||
* @param int $tax_class_id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRates(float $value, int $tax_class_id): array {
|
||||
$tax_rate_data = [];
|
||||
|
||||
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']] = [
|
||||
'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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear(): void {
|
||||
$this->tax_rates = [];
|
||||
}
|
||||
}
|
201
system/library/cart/user.php
Normal file
201
system/library/cart/user.php
Normal file
@ -0,0 +1,201 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cart;
|
||||
/**
|
||||
* Class User
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class User {
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $db;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $request;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $session;
|
||||
/**
|
||||
* @var int|mixed
|
||||
*/
|
||||
private int $user_id = 0;
|
||||
/**
|
||||
* @var string|mixed
|
||||
*/
|
||||
private string $username = '';
|
||||
/**
|
||||
* @var int|mixed
|
||||
*/
|
||||
private int $user_group_id = 0;
|
||||
/**
|
||||
* @var string|mixed
|
||||
*/
|
||||
private string $email = '';
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $permission = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $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->email = $user_query->row['email'];
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Login
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function login(string $username, string $password): bool {
|
||||
$user_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "user` WHERE `username` = '" . $this->db->escape($username) . "' AND `status` = '1'");
|
||||
|
||||
if ($user_query->num_rows) {
|
||||
if (password_verify($password, $user_query->row['password'])) {
|
||||
$rehash = password_needs_rehash($user_query->row['password'], PASSWORD_DEFAULT);
|
||||
} elseif (isset($user_query->row['salt']) && $user_query->row['password'] == sha1($user_query->row['salt'] . sha1($user_query->row['salt'] . sha1($password)))) {
|
||||
$rehash = true;
|
||||
} elseif ($user_query->row['password'] == md5($password)) {
|
||||
$rehash = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($rehash) {
|
||||
$this->db->query("UPDATE `" . DB_PREFIX . "user` SET `password` = '" . $this->db->escape(password_hash($password, PASSWORD_DEFAULT)) . "' WHERE `user_id` = '" . (int)$user_query->row['user_id'] . "'");
|
||||
}
|
||||
|
||||
$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'];
|
||||
$this->email = $user_query->row['email'];
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function logout(): void {
|
||||
unset($this->session->data['user_id']);
|
||||
|
||||
$this->user_id = 0;
|
||||
$this->username = '';
|
||||
$this->user_group_id = 0;
|
||||
$this->email = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* hasPermission
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPermission(string $key, mixed $value): bool {
|
||||
if (isset($this->permission[$key])) {
|
||||
return in_array($value, $this->permission[$key]);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* isLogged
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLogged(): bool {
|
||||
return $this->user_id ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* getId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int {
|
||||
return $this->user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* getUserName
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUserName(): string {
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* getGroupId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getGroupId(): int {
|
||||
return $this->user_group_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* getEmail
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail(): string {
|
||||
return $this->email;
|
||||
}
|
||||
}
|
104
system/library/cart/weight.php
Normal file
104
system/library/cart/weight.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cart;
|
||||
/**
|
||||
* Class Weight
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Weight {
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $db;
|
||||
/**
|
||||
* @var object|mixed|null
|
||||
*/
|
||||
private object $config;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $weights = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $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']] = [
|
||||
'weight_class_id' => $result['weight_class_id'],
|
||||
'title' => $result['title'],
|
||||
'unit' => $result['unit'],
|
||||
'value' => $result['value']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert
|
||||
*
|
||||
* @param float $value
|
||||
* @param string $from
|
||||
* @param string $to
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function convert(float $value, string $from, string $to): float {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format
|
||||
*
|
||||
* @param float $value
|
||||
* @param string $weight_class_id
|
||||
* @param string $decimal_point
|
||||
* @param string $thousand_point
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function format(float $value, string $weight_class_id, string $decimal_point = '.', string $thousand_point = ','): string {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getUnit
|
||||
*
|
||||
* @param int $weight_class_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUnit(int $weight_class_id): string {
|
||||
if (isset($this->weights[$weight_class_id])) {
|
||||
return $this->weights[$weight_class_id]['unit'];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user