shop.balmet.com

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

order.php (28542B)


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