Skip to content

Commit

Permalink
https://github.com/opencart/opencart/issues/12661
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkerr committed Feb 28, 2024
1 parent aafe6a6 commit 1feddd5
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 116 deletions.
19 changes: 19 additions & 0 deletions upload/admin/model/sale/voucher.php
Expand Up @@ -143,10 +143,29 @@ public function getTotalVouchersByVoucherThemeId(int $voucher_theme_id): int {
return (int)$query->row['total'];
}

/*
* Delete History
*
* @param int $voucher_id
*
* @return void
*/
public function deleteHistory(int $voucher_id): void {
$this->db->query("DELETE FROM `" . DB_PREFIX . "voucher_history` WHERE `voucher_id` = '" . (int)$voucher_id . "'");
}

/**
* Delete Voucher By Order ID
*
* @param int $order_id
*
* @return void
*/

public function deleteHistoryByOrderId(int $order_id): void {
$this->db->query("DELETE FROM `" . DB_PREFIX . "voucher_history` WHERE `order_id` = '" . (int)$order_id . "'");
}

/**
* Get Histories
*
Expand Down
3 changes: 2 additions & 1 deletion upload/admin/view/template/sale/order_info.twig
Expand Up @@ -79,7 +79,8 @@
{% for option in order_product.option %}
<br/>
{% if option.type != 'file' %}
<small> - {{ option.name }}: {{ option.value }}</small> {% else %}
<small> - {{ option.name }}: {{ option.value }}</small>
{% else %}
<small> - {{ option.name }}: <a href="{{ option.href }}">{{ option.value }}</a></small> {% endif %}
{% endfor %}
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions upload/catalog/controller/account/reward.php
Expand Up @@ -56,7 +56,7 @@ public function index(): void {

$this->load->model('account/reward');

$results = $this->model_account_reward->getRewards($filter_data);
$results = $this->model_account_reward->getRewards($this->customer->getId(), $filter_data);

foreach ($results as $result) {
$data['rewards'][] = [
Expand All @@ -68,7 +68,7 @@ public function index(): void {
];
}

$reward_total = $this->model_account_reward->getTotalRewards();
$reward_total = $this->model_account_reward->getTotalRewards($this->customer->getId());

$data['pagination'] = $this->load->controller('common/pagination', [
'total' => $reward_total,
Expand Down
4 changes: 2 additions & 2 deletions upload/catalog/controller/account/transaction.php
Expand Up @@ -58,7 +58,7 @@ public function index(): void {

$this->load->model('account/transaction');

$results = $this->model_account_transaction->getTransactions($filter_data);
$results = $this->model_account_transaction->getTransactions($this->customer->getId(), $filter_data);

foreach ($results as $result) {
$data['transactions'][] = [
Expand All @@ -68,7 +68,7 @@ public function index(): void {
];
}

$transaction_total = $this->model_account_transaction->getTotalTransactions();
$transaction_total = $this->model_account_transaction->getTotalTransactions($this->customer->getId());

$data['pagination'] = $this->load->controller('common/pagination', [
'total' => $transaction_total,
Expand Down
20 changes: 13 additions & 7 deletions upload/catalog/model/account/reward.php
Expand Up @@ -27,8 +27,14 @@ public function addReward(int $customer_id, int $order_id, string $description,
*
* @return void
*/
public function deleteReward(int $customer_id): void {
$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$customer_id . "'");
public function deleteReward(int $customer_id, int $order_id = 0): void {
$sql = "DELETE FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$customer_id . "'";

if ($order_id) {
$sql .= " AND `order_id` = '" . (int)$order_id . "'";
}

$this->db->query($sql);
}

/**
Expand All @@ -39,7 +45,7 @@ public function deleteReward(int $customer_id): void {
* @return void
*/
public function deleteRewardByOrderId(int $order_id): void {
$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_reward` WHERE `order_id` = '" . (int)$order_id . "'");
$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_reward` WHERE `order_id` = '" . (int)$order_id . "' AND `points` < 0");
}

/**
Expand All @@ -49,8 +55,8 @@ public function deleteRewardByOrderId(int $order_id): void {
*
* @return array<int, array<string, mixed>>
*/
public function getRewards(array $data = []): array {
$sql = "SELECT * FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$this->customer->getId() . "'";
public function getRewards(int $customer_id, array $data = []): array {
$sql = "SELECT * FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$customer_id . "'";

$sort_data = [
'points',
Expand Down Expand Up @@ -92,8 +98,8 @@ public function getRewards(array $data = []): array {
*
* @return int
*/
public function getTotalRewards(): int {
$query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$this->customer->getId() . "'");
public function getTotalRewards(int $customer_id): int {
$query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "customer_reward` WHERE `customer_id` = '" . (int)$customer_id . "'");

return (int)$query->row['total'];
}
Expand Down
12 changes: 6 additions & 6 deletions upload/catalog/model/account/transaction.php
Expand Up @@ -16,7 +16,7 @@ class Transaction extends \Opencart\System\Engine\Model {
*
* @return void
*/
public function addTransaction(int $customer_id, string $description, float $amount = 0, int $order_id = 0): void {
public function addTransaction(int $customer_id, int $order_id, string $description, float $amount): void {
$this->db->query("INSERT INTO `" . DB_PREFIX . "customer_transaction` SET `customer_id` = '" . (int)$customer_id . "', `order_id` = '" . (int)$order_id . "', `description` = '" . $this->db->escape($description) . "', `amount` = '" . (float)$amount . "', `date_added` = NOW()");
}

Expand Down Expand Up @@ -45,7 +45,7 @@ public function deleteTransaction(int $customer_id, int $order_id = 0): void {
* @return void
*/
public function deleteTransactionByOrderId(int $order_id): void {
$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_transaction` WHERE `order_id` = '" . (int)$order_id . "'");
$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_transaction` WHERE `order_id` = '" . (int)$order_id . "' AND `amount` < 0");
}

/**
Expand All @@ -55,8 +55,8 @@ public function deleteTransactionByOrderId(int $order_id): void {
*
* @return array<int, array<string, mixed>>
*/
public function getTransactions(array $data = []): array {
$sql = "SELECT * FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$this->customer->getId() . "'";
public function getTransactions(int $customer_id, array $data = []): array {
$sql = "SELECT * FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$customer_id . "'";

$sort_data = [
'amount',
Expand Down Expand Up @@ -96,8 +96,8 @@ public function getTransactions(array $data = []): array {
/**
* @return int
*/
public function getTotalTransactions(): int {
$query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$this->customer->getId() . "'");
public function getTotalTransactions(int $customer_id): int {
$query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "customer_transaction` WHERE `customer_id` = '" . (int)$customer_id . "'");

return (int)$query->row['total'];
}
Expand Down
14 changes: 14 additions & 0 deletions upload/catalog/model/catalog/product.php
Expand Up @@ -229,6 +229,20 @@ public function getCategories(int $product_id): array {
return $query->rows;
}

/**
* Get Total Categories By Category ID
*
* @param int $product_id
* @param int $category_id
*
* @return array<string, mixed>
*/
public function getCategoriesByCategoryId(int $product_id, int $category_id): array {
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product_to_category` WHERE `product_id` = '" . (int)$product_id . "' AND `category_id` = '" . (int)$category_id . "'");

return $query->row;
}

/**
* Get Attributes
*
Expand Down
60 changes: 36 additions & 24 deletions upload/catalog/model/checkout/order.php
Expand Up @@ -21,21 +21,21 @@ public function addOrder(array $data): int {
// Products
if (isset($data['products'])) {
foreach ($data['products'] as $product) {
$this->addProduct($order_id, $product);
$this->model_checkout_order->addProduct($order_id, $product);
}
}

// Vouchers
if (isset($data['vouchers'])) {
foreach ($data['vouchers'] as $voucher) {
$this->addVoucher($order_id);
$this->model_checkout_order->addVoucher($order_id);
}
}

// Totals
if (isset($data['totals'])) {
foreach ($data['totals'] as $total) {
$this->addTotal($order_id, $total);
$this->model_checkout_order->addTotal($order_id, $total);
}
}

Expand Down Expand Up @@ -67,29 +67,29 @@ public function editOrder(int $order_id, array $data): void {
$this->db->query("UPDATE `" . DB_PREFIX . "order` SET `invoice_prefix` = '" . $this->db->escape((string)$data['invoice_prefix']) . "', `store_id` = '" . (int)$data['store_id'] . "', `store_name` = '" . $this->db->escape((string)$data['store_name']) . "', `store_url` = '" . $this->db->escape((string)$data['store_url']) . "', `customer_id` = '" . (int)$data['customer_id'] . "', `customer_group_id` = '" . (int)$data['customer_group_id'] . "', `firstname` = '" . $this->db->escape((string)$data['firstname']) . "', `lastname` = '" . $this->db->escape((string)$data['lastname']) . "', `email` = '" . $this->db->escape((string)$data['email']) . "', `telephone` = '" . $this->db->escape((string)$data['telephone']) . "', `custom_field` = '" . $this->db->escape(json_encode($data['custom_field'])) . "', `payment_address_id` = '" . (int)$data['payment_address_id'] . "', `payment_firstname` = '" . $this->db->escape((string)$data['payment_firstname']) . "', `payment_lastname` = '" . $this->db->escape((string)$data['payment_lastname']) . "', `payment_company` = '" . $this->db->escape((string)$data['payment_company']) . "', `payment_address_1` = '" . $this->db->escape((string)$data['payment_address_1']) . "', `payment_address_2` = '" . $this->db->escape((string)$data['payment_address_2']) . "', `payment_city` = '" . $this->db->escape((string)$data['payment_city']) . "', `payment_postcode` = '" . $this->db->escape((string)$data['payment_postcode']) . "', `payment_country` = '" . $this->db->escape((string)$data['payment_country']) . "', `payment_country_id` = '" . (int)$data['payment_country_id'] . "', `payment_zone` = '" . $this->db->escape((string)$data['payment_zone']) . "', `payment_zone_id` = '" . (int)$data['payment_zone_id'] . "', `payment_address_format` = '" . $this->db->escape((string)$data['payment_address_format']) . "', `payment_custom_field` = '" . $this->db->escape(isset($data['payment_custom_field']) ? json_encode($data['payment_custom_field']) : '') . "', `payment_method` = '" . $this->db->escape($data['payment_method'] ? json_encode($data['payment_method']) : '') . "', `shipping_address_id` = '" . (int)$data['shipping_address_id'] . "', `shipping_firstname` = '" . $this->db->escape((string)$data['shipping_firstname']) . "', `shipping_lastname` = '" . $this->db->escape((string)$data['shipping_lastname']) . "', `shipping_company` = '" . $this->db->escape((string)$data['shipping_company']) . "', `shipping_address_1` = '" . $this->db->escape((string)$data['shipping_address_1']) . "', `shipping_address_2` = '" . $this->db->escape((string)$data['shipping_address_2']) . "', `shipping_city` = '" . $this->db->escape((string)$data['shipping_city']) . "', `shipping_postcode` = '" . $this->db->escape((string)$data['shipping_postcode']) . "', `shipping_country` = '" . $this->db->escape((string)$data['shipping_country']) . "', `shipping_country_id` = '" . (int)$data['shipping_country_id'] . "', `shipping_zone` = '" . $this->db->escape((string)$data['shipping_zone']) . "', `shipping_zone_id` = '" . (int)$data['shipping_zone_id'] . "', `shipping_address_format` = '" . $this->db->escape((string)$data['shipping_address_format']) . "', `shipping_custom_field` = '" . $this->db->escape(isset($data['shipping_custom_field']) ? json_encode($data['shipping_custom_field']) : '') . "', `shipping_method` = '" . $this->db->escape($data['shipping_method'] ? json_encode($data['shipping_method']) : '') . "', `comment` = '" . $this->db->escape((string)$data['comment']) . "', `total` = '" . (float)$data['total'] . "', `affiliate_id` = '" . (int)$data['affiliate_id'] . "', `commission` = '" . (float)$data['commission'] . "', `date_modified` = NOW() WHERE `order_id` = '" . (int)$order_id . "'");

// Products
$this->deleteProduct($order_id);
$this->model_checkout_order->deleteProduct($order_id);

if (isset($data['products'])) {
foreach ($data['products'] as $product) {
$this->addProduct($order_id, $product);
$this->model_checkout_order->addProduct($order_id, $product);
}
}

// Vouchers
$this->deleteVoucher($order_id);
$this->model_checkout_order->deleteVoucher($order_id);

if (isset($data['vouchers'])) {
foreach ($data['vouchers'] as $voucher) {
$this->addVoucher($order_id);
$this->model_checkout_order->addVoucher($order_id);
}
}

// Totals
$this->deleteTotal($order_id);
$this->model_checkout_order->deleteTotal($order_id);

if (isset($data['totals'])) {
foreach ($data['totals'] as $total) {
$this->addTotal($order_id, $total);
$this->model_checkout_order->addTotal($order_id, $total);
}
}
}
Expand All @@ -107,6 +107,18 @@ public function editTransactionId(int $order_id, string $transaction_id): void {
$this->db->query("UPDATE `" . DB_PREFIX . "order` SET `transaction_id` = '" . $this->db->escape($transaction_id) . "' WHERE `order_id` = '" . (int)$order_id . "'");
}

/**
* Edit Order Status ID
*
* @param int $order_id
* @param int $order_status_id
*
* @return void
*/
public function editOrderStatusId(int $order_id, int $order_status_id): void {
$this->db->query("UPDATE `" . DB_PREFIX . "order` SET `order_status_id` = '" . (int)$order_status_id . "' WHERE `order_id` = '" . (int)$order_id . "'");
}

/**
* Edit Comment
*
Expand All @@ -128,14 +140,14 @@ public function editComment(int $order_id, string $comment): void {
*/
public function deleteOrder(int $order_id): void {
// Void the order first so it restocks products
$this->addHistory($order_id, 0);
$this->model_checkout_order->addHistory($order_id, 0);

$this->db->query("DELETE FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$order_id . "'");

$this->deleteProduct($order_id);
$this->deleteVoucher($order_id);
$this->deleteTotal($order_id);
$this->deleteHistory($order_id);
$this->model_checkout_order->deleteProduct($order_id);
$this->model_checkout_order->deleteVoucher($order_id);
$this->model_checkout_order->deleteTotal($order_id);
$this->model_checkout_order->deleteHistory($order_id);

// Gift Voucher
$this->load->model('checkout/voucher');
Expand Down Expand Up @@ -216,12 +228,12 @@ public function addProduct(int $order_id, array $data): int {
$order_product_id = $this->db->getLastId();

foreach ($data['option'] as $option) {
$this->addOption($order_id, $order_product_id, $option);
$this->model_checkout_order->addOption($order_id, $order_product_id, $option);
}

// If subscription add details
if ($data['subscription']) {
$this->addSubscription($order_id, $order_product_id, $data['subscription']);
$this->model_checkout_order->addSubscription($order_id, $order_product_id, $data['subscription']);
}

return $this->db->getLastId();
Expand Down Expand Up @@ -529,7 +541,7 @@ public function getTotals(int $order_id): array {
* @return void
*/
public function addHistory(int $order_id, int $order_status_id, string $comment = '', bool $notify = false, bool $override = false): void {
$order_info = $this->getOrder($order_id);
$order_info = $this->model_checkout_order->getOrder($order_id);

if ($order_info) {
// Load subscription model
Expand Down Expand Up @@ -569,10 +581,10 @@ public function addHistory(int $order_id, int $order_status_id, string $comment
}

// Products
$order_products = $this->getProducts($order_id);
$order_products = $this->model_checkout_order->getProducts($order_id);

// Totals
$order_totals = $this->getTotals($order_id);
$order_totals = $this->model_checkout_order->getTotals($order_id);

// If current order status is not processing or complete but new status is processing or complete then commence completing the order
if (!in_array($order_info['order_status_id'], (array)$this->config->get('config_processing_status') + (array)$this->config->get('config_complete_status')) && in_array($order_status_id, (array)$this->config->get('config_processing_status') + (array)$this->config->get('config_complete_status'))) {
Expand Down Expand Up @@ -627,7 +639,7 @@ public function addHistory(int $order_id, int $order_status_id, string $comment

foreach ($order_products as $order_product) {
// Subscription
$order_subscription_info = $this->getSubscription($order_id, $order_product['order_product_id']);
$order_subscription_info = $this->model_checkout_order->getSubscription($order_id, $order_product['order_product_id']);

if ($order_subscription_info) {
// Add options for subscription
Expand All @@ -651,7 +663,7 @@ public function addHistory(int $order_id, int $order_status_id, string $comment
if ($subscription_info) {
$subscription_id = $subscription_info['subscription_id'];
} else {
$subscription_id = $this->model_checkout_subscription->addSubscription(array_merge($order_subscription_info, $order_product, $order_info, ['option' => $option_data]));
$subscription_id = $this->model_checkout_subscription->addSubscription($order_subscription_info + $order_product + $order_info + ['option' => $option_data]);
}

// Add history and set active subscription
Expand All @@ -671,7 +683,7 @@ public function addHistory(int $order_id, int $order_status_id, string $comment
$this->db->query("UPDATE `" . DB_PREFIX . "product` SET `quantity` = (`quantity` + " . (int)$order_product['quantity'] . ") WHERE `product_id` = '" . (int)$order_product['master_id'] . "' AND `subtract` = '1'");
}

$order_options = $this->getOptions($order_id, $order_product['order_product_id']);
$order_options = $this->model_checkout_order->getOptions($order_id, $order_product['order_product_id']);

foreach ($order_options as $order_option) {
$this->db->query("UPDATE `" . DB_PREFIX . "product_option_value` SET `quantity` = (`quantity` + " . (int)$order_product['quantity'] . ") WHERE `product_option_value_id` = '" . (int)$order_option['product_option_value_id'] . "' AND `subtract` = '1'");
Expand All @@ -685,7 +697,7 @@ public function addHistory(int $order_id, int $order_status_id, string $comment
$model_extension_total = $this->{'model_extension_' . $order_total['extension'] . '_total_' . $order_total['code']} ?? null;

if ($model_extension_total && isset($model_extension_total->unconfirm)) {
$model_extension_total->unconfirm($order_id);
$model_extension_total->unconfirm($order_info);
}
}
}
Expand Down Expand Up @@ -714,7 +726,7 @@ public function addHistory(int $order_id, int $order_status_id, string $comment
}

// Update the DB with the new statuses
$this->db->query("UPDATE `" . DB_PREFIX . "order` SET `order_status_id` = '" . (int)$order_status_id . "', `date_modified` = NOW() WHERE `order_id` = '" . (int)$order_id . "'");
$this->model_checkout_order->editOrderStatusId($order_id, $order_status_id);

$this->db->query("INSERT INTO `" . DB_PREFIX . "order_history` SET `order_id` = '" . (int)$order_id . "', `order_status_id` = '" . (int)$order_status_id . "', `notify` = '" . (int)$notify . "', `comment` = '" . $this->db->escape($comment) . "', `date_added` = NOW()");

Expand Down

0 comments on commit 1feddd5

Please sign in to comment.