shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

vq2-catalog_model_checkout_order.php (28614B)


      1 <?php
      2 class ModelCheckoutOrder extends Model {
      3 	public function addOrder($data) {
      4 		$this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', store_id = '" . (int)$data['store_id'] . "', store_name = '" . $this->db->escape($data['store_name']) . "', store_url = '" . $this->db->escape($data['store_url']) . "', customer_id = '" . (int)$data['customer_id'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "', payment_firstname = '" . $this->db->escape($data['payment_firstname']) . "', payment_lastname = '" . $this->db->escape($data['payment_lastname']) . "', payment_company = '" . $this->db->escape($data['payment_company']) . "', payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', payment_city = '" . $this->db->escape($data['payment_city']) . "', payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', payment_country = '" . $this->db->escape($data['payment_country']) . "', payment_country_id = '" . (int)$data['payment_country_id'] . "', payment_zone = '" . $this->db->escape($data['payment_zone']) . "', payment_zone_id = '" . (int)$data['payment_zone_id'] . "', payment_address_format = '" . $this->db->escape($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']) . "', payment_code = '" . $this->db->escape($data['payment_code']) . "', shipping_firstname = '" . $this->db->escape($data['shipping_firstname']) . "', shipping_lastname = '" . $this->db->escape($data['shipping_lastname']) . "', shipping_company = '" . $this->db->escape($data['shipping_company']) . "', shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', shipping_city = '" . $this->db->escape($data['shipping_city']) . "', shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', shipping_country = '" . $this->db->escape($data['shipping_country']) . "', shipping_country_id = '" . (int)$data['shipping_country_id'] . "', shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', shipping_address_format = '" . $this->db->escape($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']) . "', shipping_code = '" . $this->db->escape($data['shipping_code']) . "', comment = '" . $this->db->escape($data['comment']) . "', total = '" . (float)$data['total'] . "', affiliate_id = '" . (int)$data['affiliate_id'] . "', commission = '" . (float)$data['commission'] . "', marketing_id = '" . (int)$data['marketing_id'] . "', tracking = '" . $this->db->escape($data['tracking']) . "', language_id = '" . (int)$data['language_id'] . "', currency_id = '" . (int)$data['currency_id'] . "', currency_code = '" . $this->db->escape($data['currency_code']) . "', currency_value = '" . (float)$data['currency_value'] . "', ip = '" . $this->db->escape($data['ip']) . "', forwarded_ip = '" .  $this->db->escape($data['forwarded_ip']) . "', user_agent = '" . $this->db->escape($data['user_agent']) . "', accept_language = '" . $this->db->escape($data['accept_language']) . "', date_added = NOW(), date_modified = NOW()");
      5 
      6 		$order_id = $this->db->getLastId();
      7 
      8 		// Products
      9 		if (isset($data['products'])) {
     10 			foreach ($data['products'] as $product) {
     11 				
     12    $this->db->query("INSERT INTO " . DB_PREFIX . "order_product SET order_id = '" . (int)$order_id . "', product_id = '" . (int)$product['product_id'] . "', name = '" . $this->db->escape($product['name']) . "', model = '" . $this->db->escape($product['model']) . "', quantity = '" . (float)$product['quantity'] . "', price = '" . (float)$product['price'] . "', total = '" . (float)$product['total'] . "', tax = '" . (float)$product['tax'] . "', reward = '" . (int)$product['reward'] . "'");
     13            
     14 
     15 				$order_product_id = $this->db->getLastId();
     16 
     17 				foreach ($product['option'] as $option) {
     18 					$this->db->query("INSERT INTO " . DB_PREFIX . "order_option SET order_id = '" . (int)$order_id . "', order_product_id = '" . (int)$order_product_id . "', product_option_id = '" . (int)$option['product_option_id'] . "', product_option_value_id = '" . (int)$option['product_option_value_id'] . "', name = '" . $this->db->escape($option['name']) . "', `value` = '" . $this->db->escape($option['value']) . "', `type` = '" . $this->db->escape($option['type']) . "'");
     19 				}
     20 			}
     21 		}
     22 
     23 		// Gift Voucher
     24 		$this->load->model('extension/total/voucher');
     25 
     26 		// Vouchers
     27 		if (isset($data['vouchers'])) {
     28 			foreach ($data['vouchers'] as $voucher) {
     29 				$this->db->query("INSERT INTO " . DB_PREFIX . "order_voucher SET order_id = '" . (int)$order_id . "', description = '" . $this->db->escape($voucher['description']) . "', code = '" . $this->db->escape($voucher['code']) . "', from_name = '" . $this->db->escape($voucher['from_name']) . "', from_email = '" . $this->db->escape($voucher['from_email']) . "', to_name = '" . $this->db->escape($voucher['to_name']) . "', to_email = '" . $this->db->escape($voucher['to_email']) . "', voucher_theme_id = '" . (int)$voucher['voucher_theme_id'] . "', message = '" . $this->db->escape($voucher['message']) . "', amount = '" . (float)$voucher['amount'] . "'");
     30 
     31 				$order_voucher_id = $this->db->getLastId();
     32 
     33 				$voucher_id = $this->model_extension_total_voucher->addVoucher($order_id, $voucher);
     34 
     35 				$this->db->query("UPDATE " . DB_PREFIX . "order_voucher SET voucher_id = '" . (int)$voucher_id . "' WHERE order_voucher_id = '" . (int)$order_voucher_id . "'");
     36 			}
     37 		}
     38 
     39 		// Totals
     40 		if (isset($data['totals'])) {
     41 			foreach ($data['totals'] as $total) {
     42 				$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
     43 			}
     44 		}
     45 
     46 		return $order_id;
     47 	}
     48 
     49 	public function editOrder($order_id, $data) {
     50 		// Void the order first
     51 		$this->addOrderHistory($order_id, 0);
     52 
     53 		$this->db->query("UPDATE `" . DB_PREFIX . "order` SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', store_id = '" . (int)$data['store_id'] . "', store_name = '" . $this->db->escape($data['store_name']) . "', store_url = '" . $this->db->escape($data['store_url']) . "', customer_id = '" . (int)$data['customer_id'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', custom_field = '" . $this->db->escape(json_encode($data['custom_field'])) . "', payment_firstname = '" . $this->db->escape($data['payment_firstname']) . "', payment_lastname = '" . $this->db->escape($data['payment_lastname']) . "', payment_company = '" . $this->db->escape($data['payment_company']) . "', payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', payment_city = '" . $this->db->escape($data['payment_city']) . "', payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', payment_country = '" . $this->db->escape($data['payment_country']) . "', payment_country_id = '" . (int)$data['payment_country_id'] . "', payment_zone = '" . $this->db->escape($data['payment_zone']) . "', payment_zone_id = '" . (int)$data['payment_zone_id'] . "', payment_address_format = '" . $this->db->escape($data['payment_address_format']) . "', payment_custom_field = '" . $this->db->escape(json_encode($data['payment_custom_field'])) . "', payment_method = '" . $this->db->escape($data['payment_method']) . "', payment_code = '" . $this->db->escape($data['payment_code']) . "', shipping_firstname = '" . $this->db->escape($data['shipping_firstname']) . "', shipping_lastname = '" . $this->db->escape($data['shipping_lastname']) . "', shipping_company = '" . $this->db->escape($data['shipping_company']) . "', shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', shipping_city = '" . $this->db->escape($data['shipping_city']) . "', shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', shipping_country = '" . $this->db->escape($data['shipping_country']) . "', shipping_country_id = '" . (int)$data['shipping_country_id'] . "', shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', shipping_address_format = '" . $this->db->escape($data['shipping_address_format']) . "', shipping_custom_field = '" . $this->db->escape(json_encode($data['shipping_custom_field'])) . "', shipping_method = '" . $this->db->escape($data['shipping_method']) . "', shipping_code = '" . $this->db->escape($data['shipping_code']) . "', comment = '" . $this->db->escape($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 . "'");
     54 
     55 		$this->db->query("DELETE FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
     56 		$this->db->query("DELETE FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "'");
     57 
     58 		// Products
     59 		if (isset($data['products'])) {
     60 			foreach ($data['products'] as $product) {
     61 				
     62    $this->db->query("INSERT INTO " . DB_PREFIX . "order_product SET order_id = '" . (int)$order_id . "', product_id = '" . (int)$product['product_id'] . "', name = '" . $this->db->escape($product['name']) . "', model = '" . $this->db->escape($product['model']) . "', quantity = '" . (float)$product['quantity'] . "', price = '" . (float)$product['price'] . "', total = '" . (float)$product['total'] . "', tax = '" . (float)$product['tax'] . "', reward = '" . (int)$product['reward'] . "'");
     63            
     64 
     65 				$order_product_id = $this->db->getLastId();
     66 
     67 				foreach ($product['option'] as $option) {
     68 					$this->db->query("INSERT INTO " . DB_PREFIX . "order_option SET order_id = '" . (int)$order_id . "', order_product_id = '" . (int)$order_product_id . "', product_option_id = '" . (int)$option['product_option_id'] . "', product_option_value_id = '" . (int)$option['product_option_value_id'] . "', name = '" . $this->db->escape($option['name']) . "', `value` = '" . $this->db->escape($option['value']) . "', `type` = '" . $this->db->escape($option['type']) . "'");
     69 				}
     70 			}
     71 		}
     72 
     73 		// Gift Voucher
     74 		$this->load->model('extension/total/voucher');
     75 
     76 		$this->model_extension_total_voucher->disableVoucher($order_id);
     77 
     78 		// Vouchers
     79 		$this->db->query("DELETE FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");
     80 
     81 		if (isset($data['vouchers'])) {
     82 			foreach ($data['vouchers'] as $voucher) {
     83 				$this->db->query("INSERT INTO " . DB_PREFIX . "order_voucher SET order_id = '" . (int)$order_id . "', description = '" . $this->db->escape($voucher['description']) . "', code = '" . $this->db->escape($voucher['code']) . "', from_name = '" . $this->db->escape($voucher['from_name']) . "', from_email = '" . $this->db->escape($voucher['from_email']) . "', to_name = '" . $this->db->escape($voucher['to_name']) . "', to_email = '" . $this->db->escape($voucher['to_email']) . "', voucher_theme_id = '" . (int)$voucher['voucher_theme_id'] . "', message = '" . $this->db->escape($voucher['message']) . "', amount = '" . (float)$voucher['amount'] . "'");
     84 
     85 				$order_voucher_id = $this->db->getLastId();
     86 
     87 				$voucher_id = $this->model_extension_total_voucher->addVoucher($order_id, $voucher);
     88 
     89 				$this->db->query("UPDATE " . DB_PREFIX . "order_voucher SET voucher_id = '" . (int)$voucher_id . "' WHERE order_voucher_id = '" . (int)$order_voucher_id . "'");
     90 			}
     91 		}
     92 
     93 		// Totals
     94 		$this->db->query("DELETE FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "'");
     95 
     96 		if (isset($data['totals'])) {
     97 			foreach ($data['totals'] as $total) {
     98 				$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
     99 			}
    100 		}
    101 	}
    102 
    103 	public function deleteOrder($order_id) {
    104 		// Void the order first
    105 		$this->addOrderHistory($order_id, 0);
    106 
    107 		$this->db->query("DELETE FROM `" . DB_PREFIX . "order` WHERE order_id = '" . (int)$order_id . "'");
    108 		$this->db->query("DELETE FROM `" . DB_PREFIX . "order_product` WHERE order_id = '" . (int)$order_id . "'");
    109 		$this->db->query("DELETE FROM `" . DB_PREFIX . "order_option` WHERE order_id = '" . (int)$order_id . "'");
    110 		$this->db->query("DELETE FROM `" . DB_PREFIX . "order_voucher` WHERE order_id = '" . (int)$order_id . "'");
    111 		$this->db->query("DELETE FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "'");
    112 		$this->db->query("DELETE FROM `" . DB_PREFIX . "order_history` WHERE order_id = '" . (int)$order_id . "'");
    113 		$this->db->query("DELETE `or`, ort FROM `" . DB_PREFIX . "order_recurring` `or`, `" . DB_PREFIX . "order_recurring_transaction` `ort` WHERE order_id = '" . (int)$order_id . "' AND ort.order_recurring_id = `or`.order_recurring_id");
    114 		$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_transaction` WHERE order_id = '" . (int)$order_id . "'");
    115 
    116 		// Gift Voucher
    117 		$this->load->model('extension/total/voucher');
    118 
    119 		$this->model_extension_total_voucher->disableVoucher($order_id);
    120 	}
    121 
    122 	public function getOrder($order_id) {
    123 		$order_query = $this->db->query("SELECT *, (SELECT os.name FROM `" . DB_PREFIX . "order_status` os WHERE os.order_status_id = o.order_status_id AND os.language_id = o.language_id) AS order_status FROM `" . DB_PREFIX . "order` o WHERE o.order_id = '" . (int)$order_id . "'");
    124 
    125 		if ($order_query->num_rows) {
    126 			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['payment_country_id'] . "'");
    127 
    128 			if ($country_query->num_rows) {
    129 				$payment_iso_code_2 = $country_query->row['iso_code_2'];
    130 				$payment_iso_code_3 = $country_query->row['iso_code_3'];
    131 			} else {
    132 				$payment_iso_code_2 = '';
    133 				$payment_iso_code_3 = '';
    134 			}
    135 
    136 			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['payment_zone_id'] . "'");
    137 
    138 			if ($zone_query->num_rows) {
    139 				$payment_zone_code = $zone_query->row['code'];
    140 			} else {
    141 				$payment_zone_code = '';
    142 			}
    143 
    144 			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['shipping_country_id'] . "'");
    145 
    146 			if ($country_query->num_rows) {
    147 				$shipping_iso_code_2 = $country_query->row['iso_code_2'];
    148 				$shipping_iso_code_3 = $country_query->row['iso_code_3'];
    149 			} else {
    150 				$shipping_iso_code_2 = '';
    151 				$shipping_iso_code_3 = '';
    152 			}
    153 
    154 			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['shipping_zone_id'] . "'");
    155 
    156 			if ($zone_query->num_rows) {
    157 				$shipping_zone_code = $zone_query->row['code'];
    158 			} else {
    159 				$shipping_zone_code = '';
    160 			}
    161 
    162 			$this->load->model('localisation/language');
    163 
    164 			$language_info = $this->model_localisation_language->getLanguage($order_query->row['language_id']);
    165 
    166 			if ($language_info) {
    167 				$language_code = $language_info['code'];
    168 			} else {
    169 				$language_code = $this->config->get('config_language');
    170 			}
    171 
    172 			return array(
    173 				'order_id'                => $order_query->row['order_id'],
    174 				'invoice_no'              => $order_query->row['invoice_no'],
    175 				'invoice_prefix'          => $order_query->row['invoice_prefix'],
    176 				'store_id'                => $order_query->row['store_id'],
    177 				'store_name'              => $order_query->row['store_name'],
    178 				'store_url'               => $order_query->row['store_url'],
    179 				'customer_id'             => $order_query->row['customer_id'],
    180 				'firstname'               => $order_query->row['firstname'],
    181 				'lastname'                => $order_query->row['lastname'],
    182 				'email'                   => $order_query->row['email'],
    183 				'telephone'               => $order_query->row['telephone'],
    184 				'custom_field'            => json_decode($order_query->row['custom_field'], true),
    185 				'payment_firstname'       => $order_query->row['payment_firstname'],
    186 				'payment_lastname'        => $order_query->row['payment_lastname'],
    187 				'payment_company'         => $order_query->row['payment_company'],
    188 				'payment_address_1'       => $order_query->row['payment_address_1'],
    189 				'payment_address_2'       => $order_query->row['payment_address_2'],
    190 				'payment_postcode'        => $order_query->row['payment_postcode'],
    191 				'payment_city'            => $order_query->row['payment_city'],
    192 				'payment_zone_id'         => $order_query->row['payment_zone_id'],
    193 				'payment_zone'            => $order_query->row['payment_zone'],
    194 				'payment_zone_code'       => $payment_zone_code,
    195 				'payment_country_id'      => $order_query->row['payment_country_id'],
    196 				'payment_country'         => $order_query->row['payment_country'],
    197 				'payment_iso_code_2'      => $payment_iso_code_2,
    198 				'payment_iso_code_3'      => $payment_iso_code_3,
    199 				'payment_address_format'  => $order_query->row['payment_address_format'],
    200 				'payment_custom_field'    => json_decode($order_query->row['payment_custom_field'], true),
    201 				'payment_method'          => $order_query->row['payment_method'],
    202 				'payment_code'            => $order_query->row['payment_code'],
    203 				'shipping_firstname'      => $order_query->row['shipping_firstname'],
    204 				'shipping_lastname'       => $order_query->row['shipping_lastname'],
    205 				'shipping_company'        => $order_query->row['shipping_company'],
    206 				'shipping_address_1'      => $order_query->row['shipping_address_1'],
    207 				'shipping_address_2'      => $order_query->row['shipping_address_2'],
    208 				'shipping_postcode'       => $order_query->row['shipping_postcode'],
    209 				'shipping_city'           => $order_query->row['shipping_city'],
    210 				'shipping_zone_id'        => $order_query->row['shipping_zone_id'],
    211 				'shipping_zone'           => $order_query->row['shipping_zone'],
    212 				'shipping_zone_code'      => $shipping_zone_code,
    213 				'shipping_country_id'     => $order_query->row['shipping_country_id'],
    214 				'shipping_country'        => $order_query->row['shipping_country'],
    215 				'shipping_iso_code_2'     => $shipping_iso_code_2,
    216 				'shipping_iso_code_3'     => $shipping_iso_code_3,
    217 				'shipping_address_format' => $order_query->row['shipping_address_format'],
    218 				'shipping_custom_field'   => json_decode($order_query->row['shipping_custom_field'], true),
    219 				'shipping_method'         => $order_query->row['shipping_method'],
    220 				'shipping_code'           => $order_query->row['shipping_code'],
    221 				'comment'                 => $order_query->row['comment'],
    222 				'total'                   => $order_query->row['total'],
    223 				'order_status_id'         => $order_query->row['order_status_id'],
    224 				'order_status'            => $order_query->row['order_status'],
    225 				'affiliate_id'            => $order_query->row['affiliate_id'],
    226 				'commission'              => $order_query->row['commission'],
    227 				'language_id'             => $order_query->row['language_id'],
    228 				'language_code'           => $language_code,
    229 				'currency_id'             => $order_query->row['currency_id'],
    230 				'currency_code'           => $order_query->row['currency_code'],
    231 				'currency_value'          => $order_query->row['currency_value'],
    232 				'ip'                      => $order_query->row['ip'],
    233 				'forwarded_ip'            => $order_query->row['forwarded_ip'],
    234 				'user_agent'              => $order_query->row['user_agent'],
    235 				'accept_language'         => $order_query->row['accept_language'],
    236 				'date_added'              => $order_query->row['date_added'],
    237 				'date_modified'           => $order_query->row['date_modified']
    238 			);
    239 		} else {
    240 			return false;
    241 		}
    242 	}
    243 	
    244 	public function getOrderProducts($order_id) {
    245 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
    246 		
    247 		return $query->rows;
    248 	}
    249 	
    250 	public function getOrderOptions($order_id, $order_product_id) {
    251 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product_id . "'");
    252 		
    253 		return $query->rows;
    254 	}
    255 	
    256 	public function getOrderVouchers($order_id) {
    257 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");
    258 	
    259 		return $query->rows;
    260 	}
    261 	
    262 	public function getOrderTotals($order_id) {
    263 		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
    264 		
    265 		return $query->rows;
    266 	}	
    267 			
    268 	public function addOrderHistory($order_id, $order_status_id, $comment = '', $notify = false, $override = false) {
    269 		$order_info = $this->getOrder($order_id);
    270 		
    271 		if ($order_info) {
    272 			// Fraud Detection
    273 			$this->load->model('account/customer');
    274 
    275 			$customer_info = $this->model_account_customer->getCustomer($order_info['customer_id']);
    276 
    277 			if ($customer_info && $customer_info['safe']) {
    278 				$safe = true;
    279 			} else {
    280 				$safe = false;
    281 			}
    282 
    283 			// Only do the fraud check if the customer is not on the safe list and the order status is changing into the complete or process order status
    284 			if (!$safe && !$override && in_array($order_status_id, array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status')))) {
    285 				// Anti-Fraud
    286 				$this->load->model('setting/extension');
    287 
    288 				$extensions = $this->model_setting_extension->getExtensions('fraud');
    289 
    290 				foreach ($extensions as $extension) {
    291 					if ($this->config->get('fraud_' . $extension['code'] . '_status')) {
    292 						$this->load->model('extension/fraud/' . $extension['code']);
    293 
    294 						if (property_exists($this->{'model_extension_fraud_' . $extension['code']}, 'check')) {
    295 							$fraud_status_id = $this->{'model_extension_fraud_' . $extension['code']}->check($order_info);
    296 	
    297 							if ($fraud_status_id) {
    298 								$order_status_id = $fraud_status_id;
    299 							}
    300 						}
    301 					}
    302 				}
    303 			}
    304 
    305 			// If current order status is not processing or complete but new status is processing or complete then commence completing the order
    306 			if (!in_array($order_info['order_status_id'], array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status'))) && in_array($order_status_id, array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status')))) {
    307 				// Redeem coupon, vouchers and reward points
    308 				$order_totals = $this->getOrderTotals($order_id);
    309 
    310 				foreach ($order_totals as $order_total) {
    311 					$this->load->model('extension/total/' . $order_total['code']);
    312 
    313 					if (property_exists($this->{'model_extension_total_' . $order_total['code']}, 'confirm')) {
    314 						// Confirm coupon, vouchers and reward points
    315 						$fraud_status_id = $this->{'model_extension_total_' . $order_total['code']}->confirm($order_info, $order_total);
    316 						
    317 						// If the balance on the coupon, vouchers and reward points is not enough to cover the transaction or has already been used then the fraud order status is returned.
    318 						if ($fraud_status_id) {
    319 							$order_status_id = $fraud_status_id;
    320 						}
    321 					}
    322 				}
    323 
    324 				// Stock subtraction
    325 				$order_products = $this->getOrderProducts($order_id);
    326 
    327 				foreach ($order_products as $order_product) {
    328 					$this->db->query("UPDATE " . DB_PREFIX . "product SET quantity = (quantity - " . (int)$order_product['quantity'] . ") WHERE product_id = '" . (int)$order_product['product_id'] . "' AND subtract = '1'");
    329 
    330 					$order_options = $this->getOrderOptions($order_id, $order_product['order_product_id']);
    331 
    332 					foreach ($order_options as $order_option) {
    333 						$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'");
    334 					}
    335 				}
    336 				
    337 				// Add commission if sale is linked to affiliate referral.
    338 				if ($order_info['affiliate_id'] && $this->config->get('config_affiliate_auto')) {
    339 					$this->load->model('account/customer');
    340 
    341 					if (!$this->model_account_customer->getTotalTransactionsByOrderId($order_id)) {
    342 						$this->model_account_customer->addTransaction($order_info['affiliate_id'], $this->language->get('text_order_id') . ' #' . $order_id, $order_info['commission'], $order_id);
    343 					}
    344 				}
    345 			}
    346 
    347 			// Update the DB with the new statuses
    348 			$this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = '" . (int)$order_status_id . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'");
    349 
    350 			$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()");
    351 
    352 			// If old order status is the processing or complete status but new status is not then commence restock, and remove coupon, voucher and reward history
    353 			if (in_array($order_info['order_status_id'], array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status'))) && !in_array($order_status_id, array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status')))) {
    354 				// Restock
    355 				$order_products = $this->getOrderProducts($order_id);
    356 
    357 				foreach($order_products as $order_product) {
    358 					
    359    $this->db->query("UPDATE `" . DB_PREFIX . "product` SET quantity = (quantity + " . (float)$order_product['quantity'] . ") WHERE product_id = '" . (int)$order_product['product_id'] . "' AND subtract = '1'");
    360            
    361 
    362 					$order_options = $this->getOrderOptions($order_id, $order_product['order_product_id']);
    363 
    364 					foreach ($order_options as $order_option) {
    365 						
    366    $this->db->query("UPDATE " . DB_PREFIX . "product_option_value SET quantity = (quantity + " . (float)$order_product['quantity'] . ") WHERE product_option_value_id = '" . (int)$order_option['product_option_value_id'] . "' AND subtract = '1'");
    367            
    368 					}
    369 				}
    370 
    371 				// Remove coupon, vouchers and reward points history
    372 				$order_totals = $this->getOrderTotals($order_id);
    373 				
    374 				foreach ($order_totals as $order_total) {
    375 					$this->load->model('extension/total/' . $order_total['code']);
    376 
    377 					if (property_exists($this->{'model_extension_total_' . $order_total['code']}, 'unconfirm')) {
    378 						$this->{'model_extension_total_' . $order_total['code']}->unconfirm($order_id);
    379 					}
    380 				}
    381 
    382 				// Remove commission if sale is linked to affiliate referral.
    383 				if ($order_info['affiliate_id']) {
    384 					$this->load->model('account/customer');
    385 					
    386 					$this->model_account_customer->deleteTransactionByOrderId($order_id);
    387 				}
    388 			}
    389 
    390 			$this->cache->delete('product');
    391 		}
    392 	}
    393 }