Guest User

OpenCart Order Model

a guest
Jun 24th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 48.41 KB | None | 0 0
  1. <?php
  2. class ModelCheckoutOrder extends Model {
  3.     public function addOrder($data) {
  4.         $this->event->trigger('pre.order.add', $data);
  5.  
  6.         $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']) . "', fax = '" . $this->db->escape($data['fax']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? serialize($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']) ? serialize($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']) ? serialize($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()");
  7.  
  8.         $order_id = $this->db->getLastId();
  9.  
  10.         // Products
  11.         if (isset($data['products'])) {
  12.             foreach ($data['products'] as $product) {
  13.                 $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'] . "'");
  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('checkout/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_checkout_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.         $this->event->trigger('post.order.add', $order_id);
  47.  
  48.         return $order_id;
  49.     }
  50.  
  51.     public function editOrder($order_id, $data) {
  52.         $this->event->trigger('pre.order.edit', $data);
  53.  
  54.         // Void the order first
  55.         $this->addOrderHistory($order_id, 0);
  56.  
  57.         $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']) . "', fax = '" . $this->db->escape($data['fax']) . "', custom_field = '" . $this->db->escape(serialize($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(serialize($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(serialize($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 . "'");
  58.  
  59.         $this->db->query("DELETE FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  60.         $this->db->query("DELETE FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "'");
  61.  
  62.         // Products
  63.         if (isset($data['products'])) {
  64.             foreach ($data['products'] as $product) {
  65.                 $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'] . "'");
  66.  
  67.                 $order_product_id = $this->db->getLastId();
  68.  
  69.                 foreach ($product['option'] as $option) {
  70.                     $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']) . "'");
  71.                 }
  72.             }
  73.         }
  74.  
  75.         // Gift Voucher
  76.         $this->load->model('checkout/voucher');
  77.  
  78.         $this->model_checkout_voucher->disableVoucher($order_id);
  79.  
  80.         // Vouchers
  81.         $this->db->query("DELETE FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");
  82.  
  83.         if (isset($data['vouchers'])) {
  84.             foreach ($data['vouchers'] as $voucher) {
  85.                 $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'] . "'");
  86.  
  87.                 $order_voucher_id = $this->db->getLastId();
  88.  
  89.                 $voucher_id = $this->model_checkout_voucher->addVoucher($order_id, $voucher);
  90.  
  91.                 $this->db->query("UPDATE " . DB_PREFIX . "order_voucher SET voucher_id = '" . (int)$voucher_id . "' WHERE order_voucher_id = '" . (int)$order_voucher_id . "'");
  92.             }
  93.         }
  94.  
  95.         // Totals
  96.         $this->db->query("DELETE FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "'");
  97.  
  98.         if (isset($data['totals'])) {
  99.             foreach ($data['totals'] as $total) {
  100.                 $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'] . "'");
  101.             }
  102.         }
  103.  
  104.         $this->event->trigger('post.order.edit', $order_id);
  105.     }
  106.  
  107.     public function deleteOrder($order_id) {
  108.         $this->event->trigger('pre.order.delete', $order_id);
  109.  
  110.         // Void the order first
  111.         $this->addOrderHistory($order_id, 0);
  112.  
  113.         $this->db->query("DELETE FROM `" . DB_PREFIX . "order` WHERE order_id = '" . (int)$order_id . "'");
  114.         $this->db->query("DELETE FROM `" . DB_PREFIX . "order_product` WHERE order_id = '" . (int)$order_id . "'");
  115.         $this->db->query("DELETE FROM `" . DB_PREFIX . "order_option` WHERE order_id = '" . (int)$order_id . "'");
  116.         $this->db->query("DELETE FROM `" . DB_PREFIX . "order_voucher` WHERE order_id = '" . (int)$order_id . "'");
  117.         $this->db->query("DELETE FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "'");
  118.         $this->db->query("DELETE FROM `" . DB_PREFIX . "order_history` WHERE order_id = '" . (int)$order_id . "'");
  119.         $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");
  120.         $this->db->query("DELETE FROM `" . DB_PREFIX . "affiliate_transaction` WHERE order_id = '" . (int)$order_id . "'");
  121.  
  122.         // Gift Voucher
  123.         $this->load->model('checkout/voucher');
  124.  
  125.         $this->model_checkout_voucher->disableVoucher($order_id);
  126.  
  127.         $this->event->trigger('post.order.delete', $order_id);
  128.     }
  129.  
  130.     public function getOrder($order_id) {
  131.         $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 . "'");
  132.  
  133.         if ($order_query->num_rows) {
  134.             $country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['payment_country_id'] . "'");
  135.  
  136.             if ($country_query->num_rows) {
  137.                 $payment_iso_code_2 = $country_query->row['iso_code_2'];
  138.                 $payment_iso_code_3 = $country_query->row['iso_code_3'];
  139.             } else {
  140.                 $payment_iso_code_2 = '';
  141.                 $payment_iso_code_3 = '';
  142.             }
  143.  
  144.             $zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['payment_zone_id'] . "'");
  145.  
  146.             if ($zone_query->num_rows) {
  147.                 $payment_zone_code = $zone_query->row['code'];
  148.             } else {
  149.                 $payment_zone_code = '';
  150.             }
  151.  
  152.             $country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['shipping_country_id'] . "'");
  153.  
  154.             if ($country_query->num_rows) {
  155.                 $shipping_iso_code_2 = $country_query->row['iso_code_2'];
  156.                 $shipping_iso_code_3 = $country_query->row['iso_code_3'];
  157.             } else {
  158.                 $shipping_iso_code_2 = '';
  159.                 $shipping_iso_code_3 = '';
  160.             }
  161.  
  162.             $zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['shipping_zone_id'] . "'");
  163.  
  164.             if ($zone_query->num_rows) {
  165.                 $shipping_zone_code = $zone_query->row['code'];
  166.             } else {
  167.                 $shipping_zone_code = '';
  168.             }
  169.  
  170.             $this->load->model('localisation/language');
  171.  
  172.             $language_info = $this->model_localisation_language->getLanguage($order_query->row['language_id']);
  173.  
  174.             if ($language_info) {
  175.                 $language_code = $language_info['code'];
  176.                 $language_directory = $language_info['directory'];
  177.             } else {
  178.                 $language_code = '';
  179.                 $language_directory = '';
  180.             }
  181.  
  182.             return array(
  183.                 'order_id'                => $order_query->row['order_id'],
  184.                 'invoice_no'              => $order_query->row['invoice_no'],
  185.                 'invoice_prefix'          => $order_query->row['invoice_prefix'],
  186.                 'store_id'                => $order_query->row['store_id'],
  187.                 'store_name'              => $order_query->row['store_name'],
  188.                 'store_url'               => $order_query->row['store_url'],
  189.                 'customer_id'             => $order_query->row['customer_id'],
  190.                 'firstname'               => $order_query->row['firstname'],
  191.                 'lastname'                => $order_query->row['lastname'],
  192.                 'email'                   => $order_query->row['email'],
  193.                 'telephone'               => $order_query->row['telephone'],
  194.                 'fax'                     => $order_query->row['fax'],
  195.                 'custom_field'            => unserialize($order_query->row['custom_field']),
  196.                 'payment_firstname'       => $order_query->row['payment_firstname'],
  197.                 'payment_lastname'        => $order_query->row['payment_lastname'],
  198.                 'payment_company'         => $order_query->row['payment_company'],
  199.                 'payment_address_1'       => $order_query->row['payment_address_1'],
  200.                 'payment_address_2'       => $order_query->row['payment_address_2'],
  201.                 'payment_postcode'        => $order_query->row['payment_postcode'],
  202.                 'payment_city'            => $order_query->row['payment_city'],
  203.                 'payment_zone_id'         => $order_query->row['payment_zone_id'],
  204.                 'payment_zone'            => $order_query->row['payment_zone'],
  205.                 'payment_zone_code'       => $payment_zone_code,
  206.                 'payment_country_id'      => $order_query->row['payment_country_id'],
  207.                 'payment_country'         => $order_query->row['payment_country'],
  208.                 'payment_iso_code_2'      => $payment_iso_code_2,
  209.                 'payment_iso_code_3'      => $payment_iso_code_3,
  210.                 'payment_address_format'  => $order_query->row['payment_address_format'],
  211.                 'payment_custom_field'    => unserialize($order_query->row['payment_custom_field']),
  212.                 'payment_method'          => $order_query->row['payment_method'],
  213.                 'payment_code'            => $order_query->row['payment_code'],
  214.                 'shipping_firstname'      => $order_query->row['shipping_firstname'],
  215.                 'shipping_lastname'       => $order_query->row['shipping_lastname'],
  216.                 'shipping_company'        => $order_query->row['shipping_company'],
  217.                 'shipping_address_1'      => $order_query->row['shipping_address_1'],
  218.                 'shipping_address_2'      => $order_query->row['shipping_address_2'],
  219.                 'shipping_postcode'       => $order_query->row['shipping_postcode'],
  220.                 'shipping_city'           => $order_query->row['shipping_city'],
  221.                 'shipping_zone_id'        => $order_query->row['shipping_zone_id'],
  222.                 'shipping_zone'           => $order_query->row['shipping_zone'],
  223.                 'shipping_zone_code'      => $shipping_zone_code,
  224.                 'shipping_country_id'     => $order_query->row['shipping_country_id'],
  225.                 'shipping_country'        => $order_query->row['shipping_country'],
  226.                 'shipping_iso_code_2'     => $shipping_iso_code_2,
  227.                 'shipping_iso_code_3'     => $shipping_iso_code_3,
  228.                 'shipping_address_format' => $order_query->row['shipping_address_format'],
  229.                 'shipping_custom_field'   => unserialize($order_query->row['shipping_custom_field']),
  230.                 'shipping_method'         => $order_query->row['shipping_method'],
  231.                 'shipping_code'           => $order_query->row['shipping_code'],
  232.                 'comment'                 => $order_query->row['comment'],
  233.                 'total'                   => $order_query->row['total'],
  234.                 'order_status_id'         => $order_query->row['order_status_id'],
  235.                 'order_status'            => $order_query->row['order_status'],
  236.                 'affiliate_id'            => $order_query->row['affiliate_id'],
  237.                 'commission'              => $order_query->row['commission'],
  238.                 'language_id'             => $order_query->row['language_id'],
  239.                 'language_code'           => $language_code,
  240.                 'language_directory'      => $language_directory,
  241.                 'currency_id'             => $order_query->row['currency_id'],
  242.                 'currency_code'           => $order_query->row['currency_code'],
  243.                 'currency_value'          => $order_query->row['currency_value'],
  244.                 'ip'                      => $order_query->row['ip'],
  245.                 'forwarded_ip'            => $order_query->row['forwarded_ip'],
  246.                 'user_agent'              => $order_query->row['user_agent'],
  247.                 'accept_language'         => $order_query->row['accept_language'],
  248.                 'date_modified'           => $order_query->row['date_modified'],
  249.                 'date_added'              => $order_query->row['date_added']
  250.             );
  251.         } else {
  252.             return false;
  253.         }
  254.     }
  255.  
  256.     public function addOrderHistory($order_id, $order_status_id, $comment = '', $notify = false) {
  257.         $this->event->trigger('pre.order.history.add', $order_id);
  258.  
  259.         $order_info = $this->getOrder($order_id);
  260.  
  261.         if ($order_info) {
  262.             // Fraud Detection
  263.             $this->load->model('account/customer');
  264.  
  265.             $customer_info = $this->model_account_customer->getCustomer($order_info['customer_id']);
  266.  
  267.             if ($customer_info && $customer_info['safe']) {
  268.                 $safe = true;
  269.             } else {
  270.                 $safe = false;
  271.             }
  272.  
  273.             if (!$safe) {
  274.                 // Ban IP
  275.                 $status = false;
  276.  
  277.                 if ($order_info['customer_id']) {
  278.                     $results = $this->model_account_customer->getIps($order_info['customer_id']);
  279.  
  280.                     foreach ($results as $result) {
  281.                         if ($this->model_account_customer->isBanIp($result['ip'])) {
  282.                             $status = true;
  283.  
  284.                             break;
  285.                         }
  286.                     }
  287.                 } else {
  288.                     $status = $this->model_account_customer->isBanIp($order_info['ip']);
  289.                 }
  290.  
  291.                 if ($status) {
  292.                     $order_status_id = $this->config->get('config_order_status_id');
  293.                 }
  294.  
  295.                 // Anti-Fraud
  296.                 $this->load->model('extension/extension');
  297.  
  298.                 $extensions = $this->model_extension_extension->getExtensions('fraud');
  299.  
  300.                 foreach ($extensions as $extension) {
  301.                     if ($this->config->get($extension['code'] . '_status')) {
  302.                         $this->load->model('fraud/' . $extension['code']);
  303.  
  304.                         $fraud_status_id = $this->{'model_fraud_' . $extension['code']}->check($order_info);
  305.  
  306.                         if ($fraud_status_id) {
  307.                             $order_status_id = $fraud_status_id;
  308.                         }
  309.                     }
  310.                 }
  311.             }
  312.  
  313.             $this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = '" . (int)$order_status_id . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'");
  314.  
  315.             $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()");
  316.  
  317.             // If current order status is not processing or complete but new status is processing or complete then commence completing the order
  318.             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')))) {
  319.                 // Stock subtraction
  320.                 $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  321.  
  322.                 foreach ($order_product_query->rows as $order_product) {
  323.                     $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'");
  324.  
  325.                     $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product['order_product_id'] . "'");
  326.  
  327.                     foreach ($order_option_query->rows as $option) {
  328.                         $this->db->query("UPDATE " . DB_PREFIX . "product_option_value SET quantity = (quantity - " . (int)$order_product['quantity'] . ") WHERE product_option_value_id = '" . (int)$option['product_option_value_id'] . "' AND subtract = '1'");
  329.                     }
  330.                 }
  331.  
  332.                 // Redeem coupon, vouchers and reward points
  333.                 $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
  334.  
  335.                 foreach ($order_total_query->rows as $order_total) {
  336.                     $this->load->model('total/' . $order_total['code']);
  337.  
  338.                     if (method_exists($this->{'model_total_' . $order_total['code']}, 'confirm')) {
  339.                         $this->{'model_total_' . $order_total['code']}->confirm($order_info, $order_total);
  340.                     }
  341.                 }
  342.  
  343.                 // Add commission if sale is linked to affiliate referral.
  344.                 if ($order_info['affiliate_id'] && $this->config->get('config_affiliate_auto')) {
  345.                     $this->load->model('affiliate/affiliate');
  346.  
  347.                     $this->model_affiliate_affiliate->addTransaction($order_info['affiliate_id'], $order_info['commission'], $order_id);
  348.                 }
  349.             }
  350.  
  351.             // 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
  352.             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')))) {
  353.                 // Restock
  354.                 $product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  355.  
  356.                 foreach($product_query->rows as $product) {
  357.                     $this->db->query("UPDATE `" . DB_PREFIX . "product` SET quantity = (quantity + " . (int)$product['quantity'] . ") WHERE product_id = '" . (int)$product['product_id'] . "' AND subtract = '1'");
  358.  
  359.                     $option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$product['order_product_id'] . "'");
  360.  
  361.                     foreach ($option_query->rows as $option) {
  362.                         $this->db->query("UPDATE " . DB_PREFIX . "product_option_value SET quantity = (quantity + " . (int)$product['quantity'] . ") WHERE product_option_value_id = '" . (int)$option['product_option_value_id'] . "' AND subtract = '1'");
  363.                     }
  364.                 }
  365.  
  366.                 // Remove coupon, vouchers and reward points history
  367.                 $this->load->model('account/order');
  368.  
  369.                 $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
  370.  
  371.                 foreach ($order_total_query->rows as $order_total) {
  372.                     $this->load->model('total/' . $order_total['code']);
  373.  
  374.                     if (method_exists($this->{'model_total_' . $order_total['code']}, 'unconfirm')) {
  375.                         $this->{'model_total_' . $order_total['code']}->unconfirm($order_id);
  376.                     }
  377.                 }
  378.  
  379.                 // Remove commission if sale is linked to affiliate referral.
  380.                 if ($order_info['affiliate_id']) {
  381.                     $this->load->model('affiliate/affiliate');
  382.  
  383.                     $this->model_affiliate_affiliate->deleteTransaction($order_id);
  384.                 }
  385.             }
  386.  
  387.             $this->cache->delete('product');
  388.  
  389.             // If order status is 0 then becomes greater than 0 send main html email
  390.             if (!$order_info['order_status_id'] && $order_status_id) {
  391.                 // Check for any downloadable products
  392.                 $download_status = false;
  393.  
  394.                 $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  395.  
  396.                 foreach ($order_product_query->rows as $order_product) {
  397.                     // Check if there are any linked downloads
  398.                     $product_download_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_download` WHERE product_id = '" . (int)$order_product['product_id'] . "'");
  399.  
  400.                     if ($product_download_query->row['total']) {
  401.                         $download_status = true;
  402.                     }
  403.                 }
  404.  
  405.                 // Load the language for any mails that might be required to be sent out
  406.                 $language = new Language($order_info['language_directory']);
  407.                 $language->load($order_info['language_directory']);
  408.                 $language->load('mail/order');
  409.  
  410.                 $order_status_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$order_info['language_id'] . "'");
  411.  
  412.                 if ($order_status_query->num_rows) {
  413.                     $order_status = $order_status_query->row['name'];
  414.                 } else {
  415.                     $order_status = '';
  416.                 }
  417.  
  418.                 $subject = sprintf($language->get('text_new_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
  419.  
  420.                 // HTML Mail
  421.                 $data = array();
  422.  
  423.                 $data['title'] = sprintf($language->get('text_new_subject'), $order_info['store_name'], $order_id);
  424.  
  425.                 $data['text_greeting'] = sprintf($language->get('text_new_greeting'), $order_info['store_name']);
  426.                 $data['text_link'] = $language->get('text_new_link');
  427.                 $data['text_download'] = $language->get('text_new_download');
  428.                 $data['text_order_detail'] = $language->get('text_new_order_detail');
  429.                 $data['text_instruction'] = $language->get('text_new_instruction');
  430.                 $data['text_order_id'] = $language->get('text_new_order_id');
  431.                 $data['text_date_added'] = $language->get('text_new_date_added');
  432.                 $data['text_payment_method'] = $language->get('text_new_payment_method');
  433.                 $data['text_shipping_method'] = $language->get('text_new_shipping_method');
  434.                 $data['text_email'] = $language->get('text_new_email');
  435.                 $data['text_telephone'] = $language->get('text_new_telephone');
  436.                 $data['text_ip'] = $language->get('text_new_ip');
  437.                 $data['text_order_status'] = $language->get('text_new_order_status');
  438.                 $data['text_payment_address'] = $language->get('text_new_payment_address');
  439.                 $data['text_shipping_address'] = $language->get('text_new_shipping_address');
  440.                 $data['text_product'] = $language->get('text_new_product');
  441.                 $data['text_model'] = $language->get('text_new_model');
  442.                 $data['text_quantity'] = $language->get('text_new_quantity');
  443.                 $data['text_price'] = $language->get('text_new_price');
  444.                 $data['text_total'] = $language->get('text_new_total');
  445.                 $data['text_footer'] = $language->get('text_new_footer');
  446.  
  447.                 $data['logo'] = $this->config->get('config_url') . 'image/' . $this->config->get('config_logo');
  448.                 $data['store_name'] = $order_info['store_name'];
  449.                 $data['store_url'] = $order_info['store_url'];
  450.                 $data['customer_id'] = $order_info['customer_id'];
  451.                 $data['link'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id;
  452.  
  453.                 if ($download_status) {
  454.                     $data['download'] = $order_info['store_url'] . 'index.php?route=account/download';
  455.                 } else {
  456.                     $data['download'] = '';
  457.                 }
  458.  
  459.                 $data['order_id'] = $order_id;
  460.                 $data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added']));
  461.                 $data['payment_method'] = $order_info['payment_method'];
  462.                 $data['shipping_method'] = $order_info['shipping_method'];
  463.                 $data['email'] = $order_info['email'];
  464.                 $data['telephone'] = $order_info['telephone'];
  465.                 $data['ip'] = $order_info['ip'];
  466.                 $data['order_status'] = $order_status;
  467.  
  468.                 if ($comment && $notify) {
  469.                     $data['comment'] = nl2br($comment);
  470.                 } else {
  471.                     $data['comment'] = '';
  472.                 }
  473.  
  474.                 if ($order_info['payment_address_format']) {
  475.                     $format = $order_info['payment_address_format'];
  476.                 } else {
  477.                     $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
  478.                 }
  479.  
  480.                 $find = array(
  481.                     '{firstname}',
  482.                     '{lastname}',
  483.                     '{company}',
  484.                     '{address_1}',
  485.                     '{address_2}',
  486.                     '{city}',
  487.                     '{postcode}',
  488.                     '{zone}',
  489.                     '{zone_code}',
  490.                     '{country}'
  491.                 );
  492.  
  493.                 $replace = array(
  494.                     'firstname' => $order_info['payment_firstname'],
  495.                     'lastname'  => $order_info['payment_lastname'],
  496.                     'company'   => $order_info['payment_company'],
  497.                     'address_1' => $order_info['payment_address_1'],
  498.                     'address_2' => $order_info['payment_address_2'],
  499.                     'city'      => $order_info['payment_city'],
  500.                     'postcode'  => $order_info['payment_postcode'],
  501.                     'zone'      => $order_info['payment_zone'],
  502.                     'zone_code' => $order_info['payment_zone_code'],
  503.                     'country'   => $order_info['payment_country']
  504.                 );
  505.  
  506.                 $data['payment_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));
  507.  
  508.                 if ($order_info['shipping_address_format']) {
  509.                     $format = $order_info['shipping_address_format'];
  510.                 } else {
  511.                     $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
  512.                 }
  513.  
  514.                 $find = array(
  515.                     '{firstname}',
  516.                     '{lastname}',
  517.                     '{company}',
  518.                     '{address_1}',
  519.                     '{address_2}',
  520.                     '{city}',
  521.                     '{postcode}',
  522.                     '{zone}',
  523.                     '{zone_code}',
  524.                     '{country}'
  525.                 );
  526.  
  527.                 $replace = array(
  528.                     'firstname' => $order_info['shipping_firstname'],
  529.                     'lastname'  => $order_info['shipping_lastname'],
  530.                     'company'   => $order_info['shipping_company'],
  531.                     'address_1' => $order_info['shipping_address_1'],
  532.                     'address_2' => $order_info['shipping_address_2'],
  533.                     'city'      => $order_info['shipping_city'],
  534.                     'postcode'  => $order_info['shipping_postcode'],
  535.                     'zone'      => $order_info['shipping_zone'],
  536.                     'zone_code' => $order_info['shipping_zone_code'],
  537.                     'country'   => $order_info['shipping_country']
  538.                 );
  539.  
  540.                 $data['shipping_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));
  541.  
  542.                 $this->load->model('tool/upload');
  543.  
  544.                 // Products
  545.                 $data['products'] = array();
  546.  
  547.                 foreach ($order_product_query->rows as $product) {
  548.                     $option_data = array();
  549.  
  550.                     $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$product['order_product_id'] . "'");
  551.  
  552.                     foreach ($order_option_query->rows as $option) {
  553.                         if ($option['type'] != 'file') {
  554.                             $value = $option['value'];
  555.                         } else {
  556.                             $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
  557.  
  558.                             if ($upload_info) {
  559.                                 $value = $upload_info['name'];
  560.                             } else {
  561.                                 $value = '';
  562.                             }
  563.                         }
  564.  
  565.                         $option_data[] = array(
  566.                             'name'  => $option['name'],
  567.                             'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
  568.                         );
  569.                     }
  570.  
  571.                     $data['products'][] = array(
  572.                         'name'     => $product['name'],
  573.                         'model'    => $product['model'],
  574.                         'option'   => $option_data,
  575.                         'quantity' => $product['quantity'],
  576.                         'price'    => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
  577.                         'total'    => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value'])
  578.                     );
  579.                 }
  580.  
  581.                 // Vouchers
  582.                 $data['vouchers'] = array();
  583.  
  584.                 $order_voucher_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");
  585.  
  586.                 foreach ($order_voucher_query->rows as $voucher) {
  587.                     $data['vouchers'][] = array(
  588.                         'description' => $voucher['description'],
  589.                         'amount'      => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']),
  590.                     );
  591.                 }
  592.  
  593.                 // Order Totals
  594.                 $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
  595.  
  596.                 foreach ($order_total_query->rows as $total) {
  597.                     $data['totals'][] = array(
  598.                         'title' => $total['title'],
  599.                         'text'  => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
  600.                     );
  601.                 }
  602.  
  603.                 if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order.tpl')) {
  604.                     $html = $this->load->view($this->config->get('config_template') . '/template/mail/order.tpl', $data);
  605.                 } else {
  606.                     $html = $this->load->view('default/template/mail/order.tpl', $data);
  607.                 }
  608.  
  609.                 // Can not send confirmation emails for CBA orders as email is unknown
  610.                 $this->load->model('payment/amazon_checkout');
  611.  
  612.                 if (!$this->model_payment_amazon_checkout->isAmazonOrder($order_info['order_id'])) {
  613.                     // Text Mail
  614.                     $text  = sprintf($language->get('text_new_greeting'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')) . "\n\n";
  615.                     $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
  616.                     $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
  617.                     $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";
  618.  
  619.                     if ($comment && $notify) {
  620.                         $text .= $language->get('text_new_instruction') . "\n\n";
  621.                         $text .= $comment . "\n\n";
  622.                     }
  623.  
  624.                     // Products
  625.                     $text .= $language->get('text_new_products') . "\n";
  626.  
  627.                     foreach ($order_product_query->rows as $product) {
  628.                         $text .= $product['quantity'] . 'x ' . $product['name'] . ' (' . $product['model'] . ') ' . html_entity_decode($this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
  629.  
  630.                         $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . $product['order_product_id'] . "'");
  631.  
  632.                         foreach ($order_option_query->rows as $option) {
  633.                             if ($option['type'] != 'file') {
  634.                                 $value = $option['value'];
  635.                             } else {
  636.                                 $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
  637.  
  638.                                 if ($upload_info) {
  639.                                     $value = $upload_info['name'];
  640.                                 } else {
  641.                                     $value = '';
  642.                                 }
  643.                             }
  644.  
  645.                             $text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) . "\n";
  646.                         }
  647.                     }
  648.  
  649.                     foreach ($order_voucher_query->rows as $voucher) {
  650.                         $text .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
  651.                     }
  652.  
  653.                     $text .= "\n";
  654.  
  655.                     $text .= $language->get('text_new_order_total') . "\n";
  656.  
  657.                     foreach ($order_total_query->rows as $total) {
  658.                         $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
  659.                     }
  660.  
  661.                     $text .= "\n";
  662.  
  663.                     if ($order_info['customer_id']) {
  664.                         $text .= $language->get('text_new_link') . "\n";
  665.                         $text .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
  666.                     }
  667.  
  668.                     if ($download_status) {
  669.                         $text .= $language->get('text_new_download') . "\n";
  670.                         $text .= $order_info['store_url'] . 'index.php?route=account/download' . "\n\n";
  671.                     }
  672.  
  673.                     // Comment
  674.                     if ($order_info['comment']) {
  675.                         $text .= $language->get('text_new_comment') . "\n\n";
  676.                         $text .= $order_info['comment'] . "\n\n";
  677.                     }
  678.  
  679.                     $text .= $language->get('text_new_footer') . "\n\n";
  680.  
  681.                     $mail = new Mail();
  682.                     $mail->protocol = $this->config->get('config_mail_protocol');
  683.                     $mail->parameter = $this->config->get('config_mail_parameter');
  684.                     $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  685.                     $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  686.                     $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  687.                     $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  688.                     $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  689.  
  690.                     $mail->setTo($order_info['email']);
  691.                     $mail->setFrom($this->config->get('config_email'));
  692.                     $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
  693.                     $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
  694.                     $mail->setHtml($html);
  695.                     $mail->setText($text);
  696.                     $mail->send();
  697.                 }
  698.  
  699.                 // Admin Alert Mail
  700.                 if ($this->config->get('config_order_mail')) {
  701.                     $subject = sprintf($language->get('text_new_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'), $order_id);
  702.  
  703.                     // HTML Mail
  704.                     $data['text_greeting'] = $language->get('text_new_received');
  705.  
  706.                     if ($comment) {
  707.                         if ($order_info['comment']) {
  708.                             $data['comment'] = nl2br($comment) . '<br/><br/>' . $order_info['comment'];
  709.                         } else {
  710.                             $data['comment'] = nl2br($comment);
  711.                         }
  712.                     } else {
  713.                         if ($order_info['comment']) {
  714.                             $data['comment'] = $order_info['comment'];
  715.                         } else {
  716.                             $data['comment'] = '';
  717.                         }
  718.                     }
  719.  
  720.                     $data['text_download'] = '';
  721.  
  722.                     $data['text_footer'] = '';
  723.  
  724.                     $data['text_link'] = '';
  725.                     $data['link'] = '';
  726.                     $data['download'] = '';
  727.  
  728.                     if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order.tpl')) {
  729.                         $html = $this->load->view($this->config->get('config_template') . '/template/mail/order.tpl', $data);
  730.                     } else {
  731.                         $html = $this->load->view('default/template/mail/order.tpl', $data);
  732.                     }
  733.  
  734.                     // Text
  735.                     $text  = $language->get('text_new_received') . "\n\n";
  736.                     $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
  737.                     $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
  738.                     $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";
  739.                     $text .= $language->get('text_new_products') . "\n";
  740.  
  741.                     foreach ($order_product_query->rows as $product) {
  742.                         $text .= $product['quantity'] . 'x ' . $product['name'] . ' (' . $product['model'] . ') ' . html_entity_decode($this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
  743.  
  744.                         $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . $product['order_product_id'] . "'");
  745.  
  746.                         foreach ($order_option_query->rows as $option) {
  747.                             if ($option['type'] != 'file') {
  748.                                 $value = $option['value'];
  749.                             } else {
  750.                                 $value = utf8_substr($option['value'], 0, utf8_strrpos($option['value'], '.'));
  751.                             }
  752.  
  753.                             $text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) . "\n";
  754.                         }
  755.                     }
  756.  
  757.                     foreach ($order_voucher_query->rows as $voucher) {
  758.                         $text .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
  759.                     }
  760.  
  761.                     $text .= "\n";
  762.  
  763.                     $text .= $language->get('text_new_order_total') . "\n";
  764.  
  765.                     foreach ($order_total_query->rows as $total) {
  766.                         $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
  767.                     }
  768.  
  769.                     $text .= "\n";
  770.  
  771.                     if ($order_info['comment']) {
  772.                         $text .= $language->get('text_new_comment') . "\n\n";
  773.                         $text .= $order_info['comment'] . "\n\n";
  774.                     }
  775.  
  776.                     $mail = new Mail();
  777.                     $mail->protocol = $this->config->get('config_mail_protocol');
  778.                     $mail->parameter = $this->config->get('config_mail_parameter');
  779.                     $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  780.                     $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  781.                     $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  782.                     $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  783.                     $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  784.  
  785.                     $mail->setTo($this->config->get('config_email'));
  786.                     $mail->setFrom($this->config->get('config_email'));
  787.                     $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
  788.                     $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
  789.                     $mail->setHtml($html);
  790.                     $mail->setText($text);
  791.                     $mail->send();
  792.  
  793.                     // Send to additional alert emails
  794.                     $emails = explode(',', $this->config->get('config_mail_alert'));
  795.  
  796.                     foreach ($emails as $email) {
  797.                         if ($email && preg_match('/^[^\@]+@.*.[a-z]{2,15}$/i', $email)) {
  798.                             $mail->setTo($email);
  799.                             $mail->send();
  800.                         }
  801.                     }
  802.                 }
  803.             }
  804.  
  805.             // If order status is not 0 then send update text email
  806.             if ($order_info['order_status_id'] && $order_status_id && $notify) {
  807.                 $language = new Language($order_info['language_directory']);
  808.                 $language->load($order_info['language_directory']);
  809.                 $language->load('mail/order');
  810.  
  811.                 $subject = sprintf($language->get('text_update_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
  812.  
  813.                 $message  = $language->get('text_update_order') . ' ' . $order_id . "\n";
  814.                 $message .= $language->get('text_update_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n\n";
  815.  
  816.                 $order_status_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$order_info['language_id'] . "'");
  817.  
  818.                 if ($order_status_query->num_rows) {
  819.                     $message .= $language->get('text_update_order_status') . "\n\n";
  820.                     $message .= $order_status_query->row['name'] . "\n\n";
  821.                 }
  822.  
  823.                 if ($order_info['customer_id']) {
  824.                     $message .= $language->get('text_update_link') . "\n";
  825.                     $message .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
  826.                 }
  827.  
  828.                 if ($comment) {
  829.                     $message .= $language->get('text_update_comment') . "\n\n";
  830.                     $message .= strip_tags($comment) . "\n\n";
  831.                 }
  832.  
  833.                 $message .= $language->get('text_update_footer');
  834.  
  835.                 $mail = new Mail();
  836.                 $mail->protocol = $this->config->get('config_mail_protocol');
  837.                 $mail->parameter = $this->config->get('config_mail_parameter');
  838.                 $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  839.                 $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  840.                 $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  841.                 $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  842.                 $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  843.  
  844.                 $mail->setTo($order_info['email']);
  845.                 $mail->setFrom($this->config->get('config_email'));
  846.                 $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
  847.                 $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
  848.                 $mail->setText($message);
  849.                 $mail->send();
  850.             }
  851.  
  852.             // If order status in the complete range create any vouchers that where in the order need to be made available.
  853.             if (in_array($order_info['order_status_id'], $this->config->get('config_complete_status'))) {
  854.                 // Send out any gift voucher mails
  855.                 $this->load->model('checkout/voucher');
  856.  
  857.                 $this->model_checkout_voucher->confirm($order_id);
  858.             }
  859.         }
  860.  
  861.         $this->event->trigger('post.order.history.add', $order_id);
  862.     }
  863. }
Add Comment
Please, Sign In to add comment