Advertisement
Guest User

oc_model_order_php

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