Advertisement
darek2019

order

Feb 21st, 2019
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 72.55 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 weight = '" . (isset($data['weight']) ? floatval($data['weight']) : 0) . "', 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. 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('total/voucher');
  25.  
  26. // Vouchers
  27. if (isset($data['vouchers'])) {
  28. foreach ($data['vouchers'] as $voucher) {
  29. $this->db->query("INSERT INTO " . DB_PREFIX . "order_voucher SET order_id = '" . (int)$order_id . "', description = '" . $this->db->escape($voucher['description']) . "', code = '" . $this->db->escape($voucher['code']) . "', from_name = '" . $this->db->escape($voucher['from_name']) . "', from_email = '" . $this->db->escape($voucher['from_email']) . "', to_name = '" . $this->db->escape($voucher['to_name']) . "', to_email = '" . $this->db->escape($voucher['to_email']) . "', voucher_theme_id = '" . (int)$voucher['voucher_theme_id'] . "', message = '" . $this->db->escape($voucher['message']) . "', amount = '" . (float)$voucher['amount'] . "'");
  30.  
  31. $order_voucher_id = $this->db->getLastId();
  32.  
  33. $voucher_id = $this->model_total_voucher->addVoucher($order_id, $voucher);
  34.  
  35. $this->db->query("UPDATE " . DB_PREFIX . "order_voucher SET voucher_id = '" . (int)$voucher_id . "' WHERE order_voucher_id = '" . (int)$order_voucher_id . "'");
  36. }
  37. }
  38.  
  39. // Totals
  40. if (isset($data['totals'])) {
  41. foreach ($data['totals'] as $total) {
  42. $this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
  43. }
  44. }
  45.  
  46. $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(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 . "'");
  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('total/voucher');
  77.  
  78. $this->model_total_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_total_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('total/voucher');
  124.  
  125. $this->model_total_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. 'customer_group_id' => (isset($order_query->row['customer_group_id'])) ? $order_query->row['customer_group_id'] : '',
  191. 'affiliate_id' => (isset($order_query->row['affiliate_id'])) ? $order_query->row['affiliate_id'] : '',
  192. 'weight' => (isset($order_query->row['weight'])) ? $order_query->row['weight'] : 0,
  193. 'firstname' => $order_query->row['firstname'],
  194. 'lastname' => $order_query->row['lastname'],
  195. 'email' => $order_query->row['email'],
  196. 'telephone' => $order_query->row['telephone'],
  197. 'fax' => $order_query->row['fax'],
  198. 'custom_field' => json_decode($order_query->row['custom_field'], true),
  199. 'payment_firstname' => $order_query->row['payment_firstname'],
  200. 'payment_lastname' => $order_query->row['payment_lastname'],
  201. 'payment_company' => $order_query->row['payment_company'],
  202. 'payment_address_1' => $order_query->row['payment_address_1'],
  203. 'payment_address_2' => $order_query->row['payment_address_2'],
  204. 'payment_postcode' => $order_query->row['payment_postcode'],
  205. 'payment_city' => $order_query->row['payment_city'],
  206. 'payment_zone_id' => $order_query->row['payment_zone_id'],
  207. 'payment_zone' => $order_query->row['payment_zone'],
  208. 'payment_zone_code' => $payment_zone_code,
  209. 'payment_country_id' => $order_query->row['payment_country_id'],
  210. 'payment_country' => $order_query->row['payment_country'],
  211. 'payment_iso_code_2' => $payment_iso_code_2,
  212. 'payment_iso_code_3' => $payment_iso_code_3,
  213. 'payment_address_format' => $order_query->row['payment_address_format'],
  214. 'payment_custom_field' => json_decode($order_query->row['payment_custom_field'], true),
  215. 'payment_method' => $order_query->row['payment_method'],
  216. 'payment_code' => $order_query->row['payment_code'],
  217. 'shipping_firstname' => $order_query->row['shipping_firstname'],
  218. 'shipping_lastname' => $order_query->row['shipping_lastname'],
  219. 'shipping_company' => $order_query->row['shipping_company'],
  220. 'shipping_address_1' => $order_query->row['shipping_address_1'],
  221. 'shipping_address_2' => $order_query->row['shipping_address_2'],
  222. 'shipping_postcode' => $order_query->row['shipping_postcode'],
  223. 'shipping_city' => $order_query->row['shipping_city'],
  224. 'shipping_zone_id' => $order_query->row['shipping_zone_id'],
  225. 'shipping_zone' => $order_query->row['shipping_zone'],
  226. 'shipping_zone_code' => $shipping_zone_code,
  227. 'shipping_country_id' => $order_query->row['shipping_country_id'],
  228. 'shipping_country' => $order_query->row['shipping_country'],
  229. 'shipping_iso_code_2' => $shipping_iso_code_2,
  230. 'shipping_iso_code_3' => $shipping_iso_code_3,
  231. 'shipping_address_format' => $order_query->row['shipping_address_format'],
  232. 'shipping_custom_field' => json_decode($order_query->row['shipping_custom_field'], true),
  233. 'shipping_method' => $order_query->row['shipping_method'],
  234. 'shipping_code' => $order_query->row['shipping_code'],
  235. 'comment' => $order_query->row['comment'],
  236. 'total' => $order_query->row['total'],
  237. 'order_status_id' => $order_query->row['order_status_id'],
  238. 'order_status' => $order_query->row['order_status'],
  239. 'affiliate_id' => $order_query->row['affiliate_id'],
  240. 'commission' => $order_query->row['commission'],
  241. 'language_id' => $order_query->row['language_id'],
  242. 'language_code' => $language_code,
  243. 'language_directory' => $language_directory,
  244. 'currency_id' => $order_query->row['currency_id'],
  245. 'currency_code' => $order_query->row['currency_code'],
  246. 'currency_value' => $order_query->row['currency_value'],
  247. 'ip' => $order_query->row['ip'],
  248. 'forwarded_ip' => $order_query->row['forwarded_ip'],
  249. 'user_agent' => $order_query->row['user_agent'],
  250. 'accept_language' => $order_query->row['accept_language'],
  251. 'customer_group_id' => $order_query->row['customer_group_id'],
  252. 'date_modified' => $order_query->row['date_modified'],
  253. 'date_added' => $order_query->row['date_added']
  254. );
  255. } else {
  256. return false;
  257. }
  258. }
  259.  
  260. public function addOrderHistory($order_id, $order_status_id, $comment = '', $notify = false, $override = false) {
  261. // Decode twice from javascript encodeURIComponent and curl API http_build_query
  262. $comment = html_entity_decode(html_entity_decode($comment, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
  263.  
  264. $event_data = array(
  265. 'order_id' => $order_id,
  266. 'order_status_id' => $order_status_id,
  267. 'comment' => $comment,
  268. 'notify' => $notify
  269. );
  270.  
  271. $this->event->trigger('pre.order.history.add', $event_data);
  272.  
  273. $order_info = $this->getOrder($order_id);
  274.  
  275. if( is_null( $this->adk_cache ) ) {
  276. $this->adk_cache = array( 'old_order' => $order_info );
  277. } else {
  278. $adk_cache = $this->adk_cache;
  279. $adk_cache['old_order'] = $order_info;
  280. $this->adk_cache = $adk_cache;
  281. }
  282.  
  283.  
  284. if ($order_info) {
  285. // Fraud Detection
  286. $this->load->model('account/customer');
  287.  
  288. $customer_info = $this->model_account_customer->getCustomer($order_info['customer_id']);
  289.  
  290. if ($customer_info && $customer_info['safe']) {
  291. $safe = true;
  292. } else {
  293. $safe = false;
  294. }
  295.  
  296. // 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
  297. if (!$safe && !$override && in_array($order_status_id, array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status')))) {
  298. // Anti-Fraud
  299. $this->load->model('extension/extension');
  300.  
  301. $extensions = $this->model_extension_extension->getExtensions('fraud');
  302.  
  303. foreach ($extensions as $extension) {
  304. if ($this->config->get($extension['code'] . '_status')) {
  305. $this->load->model('fraud/' . $extension['code']);
  306.  
  307. $fraud_status_id = $this->{'model_fraud_' . $extension['code']}->check($order_info);
  308.  
  309. if ($fraud_status_id) {
  310. $order_status_id = $fraud_status_id;
  311. }
  312. }
  313. }
  314. }
  315.  
  316. // If current order status is not processing or complete but new status is processing or complete then commence completing the order
  317. 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')))) {
  318. // Redeem coupon, vouchers and reward points
  319. $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
  320.  
  321. foreach ($order_total_query->rows as $order_total) {
  322.  
  323. if(strpos($order_total['code'],'xfee')!==false) continue;
  324.  
  325. $this->load->model('total/' . $order_total['code']);
  326.  
  327. if (method_exists($this->{'model_total_' . $order_total['code']}, 'confirm')) {
  328. // Confirm coupon, vouchers and reward points
  329. $fraud_status_id = $this->{'model_total_' . $order_total['code']}->confirm($order_info, $order_total);
  330.  
  331. // 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.
  332. if ($fraud_status_id) {
  333. $order_status_id = $fraud_status_id;
  334. }
  335. }
  336. }
  337.  
  338. // Add commission if sale is linked to affiliate referral.
  339. if ($order_info['affiliate_id'] && $this->config->get('config_affiliate_auto')) {
  340. $this->load->model('affiliate/affiliate');
  341.  
  342. $this->model_affiliate_affiliate->addTransaction($order_info['affiliate_id'], $order_info['commission'], $order_id);
  343. }
  344.  
  345. // Stock subtraction
  346. $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  347.  
  348. foreach ($order_product_query->rows as $order_product) {
  349. $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'");
  350.  
  351. $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'] . "'");
  352.  
  353. foreach ($order_option_query->rows as $option) {
  354. $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'");
  355. }
  356. }
  357. }
  358.  
  359. // Update the DB with the new statuses
  360. $this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = '" . (int)$order_status_id . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'");
  361.  
  362. $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()");
  363.  
  364. $this->load->config('isenselabs/orderfollowup');
  365. $this->load->model($this->config->get('orderfollowup_path'));
  366. $this->{$this->config->get('orderfollowup_model')}->tryToSendFollowUp($order_id, $order_info['store_id']);
  367.  
  368.  
  369. $order_history_id = $this->db->getLastId();
  370.  
  371. // 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
  372. 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')))) {
  373. // Restock
  374. $product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  375.  
  376. foreach($product_query->rows as $product) {
  377. $this->db->query("UPDATE `" . DB_PREFIX . "product` SET quantity = (quantity + " . (int)$product['quantity'] . ") WHERE product_id = '" . (int)$product['product_id'] . "' AND subtract = '1'");
  378.  
  379. $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'] . "'");
  380.  
  381. foreach ($option_query->rows as $option) {
  382. $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'");
  383. }
  384. }
  385.  
  386. // Remove coupon, vouchers and reward points history
  387. $this->load->model('account/order');
  388.  
  389. $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
  390.  
  391. foreach ($order_total_query->rows as $order_total) {
  392.  
  393. if(strpos($order_total['code'],'xfee')!==false) continue;
  394.  
  395. $this->load->model('total/' . $order_total['code']);
  396.  
  397. if (method_exists($this->{'model_total_' . $order_total['code']}, 'unconfirm')) {
  398. $this->{'model_total_' . $order_total['code']}->unconfirm($order_id);
  399. }
  400. }
  401.  
  402. // Remove commission if sale is linked to affiliate referral.
  403. if ($order_info['affiliate_id']) {
  404. $this->load->model('affiliate/affiliate');
  405.  
  406. $this->model_affiliate_affiliate->deleteTransaction($order_id);
  407. }
  408. }
  409.  
  410. $this->cache->delete('product');
  411.  
  412. // If order status is 0 then becomes greater than 0 send main html email
  413. if (!$order_info['order_status_id'] && $order_status_id) {
  414. // Check for any downloadable products
  415. $download_status = false;
  416.  
  417. $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  418.  
  419. foreach ($order_product_query->rows as $order_product) {
  420. // Check if there are any linked downloads
  421. $product_download_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_download` WHERE product_id = '" . (int)$order_product['product_id'] . "'");
  422.  
  423. if ($product_download_query->row['total']) {
  424. $download_status = true;
  425. }
  426. }
  427.  
  428. // Load the language for any mails that might be required to be sent out
  429. $language = new Language($order_info['language_directory']);
  430. $language->load($order_info['language_directory']);
  431. $language->load('mail/order');
  432.  
  433. $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'] . "'");
  434.  
  435. if ($order_status_query->num_rows) {
  436. $order_status = $order_status_query->row['name'];
  437. } else {
  438. $order_status = '';
  439. }
  440.  
  441. $subject = sprintf($language->get('text_new_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
  442.  
  443. $this->load->model('tool/image');
  444. $this->load->model('catalog/product');
  445.  
  446. $file = DIR_SYSTEM . 'library/emailtemplate/email_template.php';
  447.  
  448. if (file_exists($file)) {
  449. include_once($file);
  450. } else {
  451. trigger_error('Error: Could not load library ' . $file . '!');
  452. exit();
  453. }
  454.  
  455. $template = new EmailTemplate($this->request, $this->registry);
  456.  
  457. if (!empty($order_info['customer_id'])) {
  458. $this->load->model('account/customer');
  459. $template->data['customer'] = $this->model_account_customer->getCustomer($order_info['customer_id']);
  460. }
  461.  
  462. if (!empty($order_info['customer_group_id'])) {
  463. $this->load->model('account/customer_group');
  464. $template->data['customer_group'] = $this->model_account_customer_group->getCustomerGroup($order_info['customer_group_id']);
  465. }
  466.  
  467. if (!empty($order_info['affiliate_id'])) {
  468. $this->load->model('affiliate/affiliate');
  469. $template->data['affiliate'] = $this->model_affiliate_affiliate->getAffiliate($order_info['affiliate_id']);
  470. }
  471.  
  472. // Custom fields
  473. if (!empty($order_info['custom_field']) || !empty($order_info['payment_custom_field'])) {
  474. $this->load->model('account/custom_field');
  475.  
  476. if (!empty($order_info['customer_group_id'])) {
  477. $customer_group_id = $order_info['customer_group_id'];
  478. } else {
  479. $customer_group_id = $this->config->get('config_customer_group_id');
  480. }
  481.  
  482. $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
  483.  
  484. foreach($custom_fields as $custom_field){
  485. if (isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
  486. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_name'] = $custom_field['name'];
  487. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_value'] = $order_info['payment_custom_field'][$custom_field['custom_field_id']];
  488. } elseif (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
  489. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_name'] = $custom_field['name'];
  490. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_value'] = $order_info['custom_field'][$custom_field['custom_field_id']];
  491. }
  492. }
  493. }
  494.  
  495. $template->addData($order_info);
  496.  
  497. $template->data['text_affiliate'] = $language->get('text_affiliate');
  498. $template->data['text_customer_group'] = $language->get('text_customer_group');
  499. $template->data['text_id'] = $language->get('text_id');
  500. $template->data['text_order_link'] = $language->get('text_order_link');
  501. $template->data['text_sku'] = $language->get('text_sku');
  502. $template->data['text_stock_quantity'] = $language->get('text_stock_quantity');
  503. $template->data['text_backorder_quantity'] = $language->get('text_backorder_quantity');
  504. $template->data['text_invoice_no'] = $language->get('text_invoice_no');
  505. $template->data['text_fax'] = $language->get('text_fax');
  506.  
  507. // Custom fields
  508. if (!empty($order_info['custom_field']) || !empty($order_info['payment_custom_field'])) {
  509. $this->load->model('account/custom_field');
  510.  
  511. if (!empty($order_info['customer_group_id'])) {
  512. $customer_group_id = $order_info['customer_group_id'];
  513. } else {
  514. $customer_group_id = $this->config->get('config_customer_group_id');
  515. }
  516.  
  517. $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
  518.  
  519. foreach($custom_fields as $custom_field){
  520. if (isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
  521. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_name'] = $custom_field['name'];
  522. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_value'] = $order_info['payment_custom_field'][$custom_field['custom_field_id']];
  523. } elseif (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
  524. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_name'] = $custom_field['name'];
  525. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_value'] = $order_info['custom_field'][$custom_field['custom_field_id']];
  526. }
  527. }
  528. }
  529.  
  530. $template->data['order_subject_products'] = '';
  531.  
  532. foreach ($order_product_query->rows as $order_product) {
  533. $template->data['order_subject_products'] .= ($template->data['order_subject_products'] ? ', ' : '') . $order_product['name'];
  534. }
  535.  
  536. if (strlen($template->data['order_subject_products']) > 32) {
  537. $template->data['order_subject_products'] = trim(substr($template->data['order_subject_products'], 0, strrpos(substr($template->data['order_subject_products'], 0, 32), ' ')), "-_ \t\n\r\0\x0B") . '...';
  538. }
  539.  
  540. $template->data['new_order_status'] = $order_status;
  541.  
  542. // HTML Mail
  543. $data = array();
  544.  
  545. $data['title'] = sprintf($language->get('text_new_subject'), $order_info['store_name'], $order_id);
  546.  
  547. $data['text_greeting'] = sprintf($language->get('text_new_greeting'), $order_info['store_name']);
  548. $data['text_link'] = $language->get('text_new_link');
  549. $data['text_download'] = $language->get('text_new_download');
  550. $data['text_order_detail'] = $language->get('text_new_order_detail');
  551. $data['text_instruction'] = $language->get('text_new_instruction');
  552. $data['text_order_id'] = $language->get('text_new_order_id');
  553. $data['text_date_added'] = $language->get('text_new_date_added');
  554. $data['text_payment_method'] = $language->get('text_new_payment_method');
  555. $data['text_shipping_method'] = $language->get('text_new_shipping_method');
  556. $data['text_email'] = $language->get('text_new_email');
  557. $data['text_telephone'] = $language->get('text_new_telephone');
  558. $data['text_ip'] = $language->get('text_new_ip');
  559. $data['text_order_status'] = $language->get('text_new_order_status');
  560. $data['text_payment_address'] = $language->get('text_new_payment_address');
  561. $data['text_shipping_address'] = $language->get('text_new_shipping_address');
  562. $data['text_product'] = $language->get('text_new_product');
  563. $data['text_model'] = $language->get('text_new_model');
  564. $data['text_quantity'] = $language->get('text_new_quantity');
  565. $data['text_price'] = $language->get('text_new_price');
  566. $data['text_total'] = $language->get('text_new_total');
  567. $data['text_footer'] = $language->get('text_new_footer');
  568.  
  569. $data['logo'] = $this->config->get('config_url') . 'image/' . $this->config->get('config_logo');
  570. $data['store_name'] = $order_info['store_name'];
  571. $data['store_url'] = $order_info['store_url'];
  572. $data['customer_id'] = $order_info['customer_id'];
  573. $data['link'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id;
  574.  
  575. if ($download_status) {
  576. $data['download'] = $order_info['store_url'] . 'index.php?route=account/download';
  577. } else {
  578. $data['download'] = '';
  579. }
  580.  
  581. $data['order_id'] = $order_id;
  582. $data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added']));
  583. $data['payment_method'] = $order_info['payment_method'];
  584. $data['shipping_method'] = $order_info['shipping_method'];
  585. $data['email'] = $order_info['email'];
  586. $data['telephone'] = $order_info['telephone'];
  587. $data['ip'] = $order_info['ip'];
  588. $data['order_status'] = $order_status;
  589.  
  590. if ($comment && $notify) {
  591. $data['comment'] = nl2br($comment);
  592. } else {
  593. $data['comment'] = '';
  594. }
  595.  
  596. if ($order_info['payment_address_format']) {
  597. $format = $order_info['payment_address_format'];
  598. } else {
  599. $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
  600. }
  601.  
  602. $find = array(
  603. '{firstname}',
  604. '{lastname}',
  605. '{company}',
  606. '{address_1}',
  607. '{address_2}',
  608. '{city}',
  609. '{postcode}',
  610. '{zone}',
  611. '{zone_code}',
  612. '{country}'
  613. );
  614.  
  615. $replace = array(
  616. 'firstname' => $order_info['payment_firstname'],
  617. 'lastname' => $order_info['payment_lastname'],
  618. 'company' => $order_info['payment_company'],
  619. 'address_1' => $order_info['payment_address_1'],
  620. 'address_2' => $order_info['payment_address_2'],
  621. 'city' => $order_info['payment_city'],
  622. 'postcode' => $order_info['payment_postcode'],
  623. 'zone' => $order_info['payment_zone'],
  624. 'zone_code' => $order_info['payment_zone_code'],
  625. 'country' => $order_info['payment_country']
  626. );
  627.  
  628. $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))));
  629.  
  630. if ($order_info['shipping_address_format']) {
  631. $format = $order_info['shipping_address_format'];
  632. } else {
  633. $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
  634. }
  635.  
  636. $find = array(
  637. '{firstname}',
  638. '{lastname}',
  639. '{company}',
  640. '{address_1}',
  641. '{address_2}',
  642. '{city}',
  643. '{postcode}',
  644. '{zone}',
  645. '{zone_code}',
  646. '{country}'
  647. );
  648.  
  649. $replace = array(
  650. 'firstname' => $order_info['shipping_firstname'],
  651. 'lastname' => $order_info['shipping_lastname'],
  652. 'company' => $order_info['shipping_company'],
  653. 'address_1' => $order_info['shipping_address_1'],
  654. 'address_2' => $order_info['shipping_address_2'],
  655. 'city' => $order_info['shipping_city'],
  656. 'postcode' => $order_info['shipping_postcode'],
  657. 'zone' => $order_info['shipping_zone'],
  658. 'zone_code' => $order_info['shipping_zone_code'],
  659. 'country' => $order_info['shipping_country']
  660. );
  661.  
  662. $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))));
  663.  
  664. $this->load->model('tool/upload');
  665.  
  666. // Products
  667. $data['products'] = array();
  668.  
  669. foreach ($order_product_query->rows as $product) {
  670. $option_data = array();
  671.  
  672. $product_data = $this->model_catalog_product->getProduct($product['product_id']);
  673.  
  674. $order_option_query = $this->db->query("SELECT oo.*, pov.*, ov.image FROM " . DB_PREFIX . "order_option oo LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_option_value_id = oo.product_option_value_id) LEFT JOIN " . DB_PREFIX . "option_value ov ON (ov.option_value_id = pov.option_value_id) WHERE oo.order_id = '" . (int)$order_id . "' AND oo.order_product_id = '" . (int)$product['order_product_id'] . "'");
  675.  
  676. foreach ($order_option_query->rows as $option) {
  677. if ($option['type'] != 'file') {
  678. $value = $option['value'];
  679. } else {
  680. $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
  681.  
  682. if ($upload_info) {
  683. $value = $upload_info['name'];
  684. } else {
  685. $value = '';
  686. }
  687. }
  688.  
  689. $option_data[] = array(
  690. 'name' => $option['name'],
  691. 'price' => ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option['price']) ? $this->currency->format($this->tax->calculate($option['price'], $product_data['tax_class_id'], $this->config->get('config_tax') ? 'P' : false)) : 0,
  692. 'price_prefix' => isset($option['price_prefix']) ? $option['price_prefix'] : '',
  693. 'stock_quantity' => isset($option['quantity']) ? $option['quantity'] : '',
  694. 'stock_subtract' => isset($option['subtract']) ? $option['subtract'] : '',
  695. 'value' => (utf8_strlen($value) > 120 ? utf8_substr($value, 0, 120) . '..' : $value)
  696. );
  697. }
  698.  
  699. $image = $product_data['image'];
  700.  
  701. // Product Option Image
  702. /*foreach ($order_option_query->rows as $option) {
  703. if ($option['image']) {
  704. $image = $option['image'];
  705. }
  706. }*/
  707.  
  708. if ($image) {
  709. $this->model_tool_image->setUrl($order_info['store_url']);
  710.  
  711. $image = $this->model_tool_image->resize($image, 50, 50);
  712.  
  713. if (substr($image, 0, 4) != 'http') {
  714. $image = $order_info['store_url'] . ltrim($image, '/');
  715. }
  716. }
  717.  
  718. $url = $this->url->link('product/product', 'product_id='.$product['product_id'], 'SSL');
  719.  
  720. if ($this->config->get('config_stock_checkout') && $product_data['subtract']) {
  721. $stock_quantity = $product_data['quantity'] + $product['quantity']; // quantity before order
  722. } else {
  723. $stock_quantity = false;
  724. }
  725.  
  726. if ($stock_quantity && $product['quantity'] > $stock_quantity) {
  727. $stock_backorder = $product['quantity'] - $stock_quantity;
  728. } else {
  729. $stock_backorder = false;
  730. }
  731.  
  732. $data['products'][] = array(
  733. 'product_id' => $product_data['product_id'],
  734. 'url' => $url,
  735. 'url_tracking' => $template->getTracking($url),
  736. 'image' => $image,
  737. 'weight' => ($product_data['weight'] > 0) ? $this->weight->format($product_data['weight'], $product_data['weight_class_id']) : 0,
  738. 'description' => utf8_substr(strip_tags(html_entity_decode($product_data['description'], ENT_QUOTES, 'UTF-8')), 0, 200) . '..',
  739. 'manufacturer' => $product_data['manufacturer'],
  740. 'sku' => $product_data['sku'],
  741. 'stock_status' => $product_data['stock_status'],
  742. 'stock_subtract' => $product_data['subtract'],
  743. 'stock_quantity' => $stock_quantity,
  744. 'stock_backorder' => $stock_backorder,
  745. 'name' => $product['name'],
  746. 'model' => $product['model'],
  747. 'option' => $option_data,
  748. 'quantity' => $product['quantity'],
  749. 'price' => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
  750. 'total' => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value'])
  751. );
  752. }
  753.  
  754. // Vouchers
  755. $data['vouchers'] = array();
  756.  
  757. $order_voucher_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");
  758.  
  759. foreach ($order_voucher_query->rows as $voucher) {
  760. $data['vouchers'][] = array(
  761. 'description' => $voucher['description'],
  762. 'amount' => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']),
  763. );
  764. }
  765.  
  766. // Order Totals
  767. $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
  768.  
  769. foreach ($order_total_query->rows as $total) {
  770. $data['totals'][] = array(
  771. 'title' => $total['title'],
  772. 'text' => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
  773. );
  774. }
  775.  
  776. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order.tpl')) {
  777.  
  778. } else {
  779.  
  780. }
  781.  
  782.  
  783. $adk_cache = $this->adk_cache;
  784. $adk_cache['old_order_data'] = $data;
  785. $this->adk_cache = $adk_cache;
  786.  
  787. // Text Mail
  788. $text = sprintf($language->get('text_new_greeting'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')) . "\n\n";
  789. $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
  790. $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
  791. $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";
  792.  
  793. if ($comment && $notify) {
  794. $text .= $language->get('text_new_instruction') . "\n\n";
  795. $text .= strip_tags($comment) . "\n\n";
  796. }
  797.  
  798. // Products
  799. $text .= $language->get('text_new_products') . "\n";
  800.  
  801. foreach ($order_product_query->rows as $product) {
  802. $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";
  803.  
  804. $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'] . "'");
  805.  
  806. foreach ($order_option_query->rows as $option) {
  807. if ($option['type'] != 'file') {
  808. $value = $option['value'];
  809. } else {
  810. $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
  811.  
  812. if ($upload_info) {
  813. $value = $upload_info['name'];
  814. } else {
  815. $value = '';
  816. }
  817. }
  818.  
  819. $text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) . "\n";
  820. }
  821. }
  822.  
  823. foreach ($order_voucher_query->rows as $voucher) {
  824. $text .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
  825. }
  826.  
  827. $text .= "\n";
  828.  
  829. $text .= $language->get('text_new_order_total') . "\n";
  830.  
  831. foreach ($order_total_query->rows as $total) {
  832. $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
  833. }
  834.  
  835. $text .= "\n";
  836.  
  837. if ($order_info['customer_id']) {
  838. $text .= $language->get('text_new_link') . "\n";
  839. $text .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
  840. }
  841.  
  842. if ($download_status) {
  843. $text .= $language->get('text_new_download') . "\n";
  844. $text .= $order_info['store_url'] . 'index.php?route=account/download' . "\n\n";
  845. }
  846.  
  847. // Comment
  848. if ($order_info['comment']) {
  849. $text .= $language->get('text_new_comment') . "\n\n";
  850. $text .= $order_info['comment'] . "\n\n";
  851. }
  852.  
  853. $text .= $language->get('text_new_footer') . "\n\n";
  854.  
  855. $mail = new Mail();
  856. $mail->protocol = $this->config->get('config_mail_protocol');
  857. $mail->parameter = $this->config->get('config_mail_parameter');
  858. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  859. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  860. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  861. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  862. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  863.  
  864. $mail->setTo($order_info['email']);
  865. $mail->setFrom($this->config->get('config_email'));
  866. $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
  867. $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
  868.  
  869. $mail->setText($text);
  870.  
  871. $template->addData($data);
  872.  
  873. if (!empty($data['link'])) {
  874. $template->data['link_tracking'] = $template->getTracking($data['link']);
  875. }
  876.  
  877. if (!empty($data['download'])) {
  878. $template->data['download_tracking'] = $template->getTracking($data['download']);
  879. }
  880.  
  881. $template->data['order_comment'] = nl2br($order_info['comment']);
  882.  
  883. $template->data['comment'] = $comment;
  884.  
  885. if ($comment && $notify && $order_info['comment'] != $comment) {
  886. $template->data['instruction'] = nl2br($comment);
  887. }
  888.  
  889. $template->data['order_total'] = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value']);
  890.  
  891. $template_load = array(
  892. 'key' => 'order.customer'
  893. );
  894.  
  895. if (!empty($order_status_id)) {
  896. $template_load['order_status_id'] = $order_status_id;
  897. }
  898.  
  899. if (!empty($order_info['language_id'])) {
  900. $template_load['language_id'] = $order_info['language_id'];
  901. }
  902.  
  903. $template->load($template_load);
  904.  
  905. if ($template->data['emailtemplate']['attach_invoice']) {
  906. if (!$this->config->get('pdf_invoice')) {
  907. trigger_error('Error: could not attach invoice! Install module \'opencart pdf order invoice\'');
  908. } else {
  909. $this->load->model('module/pdf_invoice');
  910.  
  911. if ($this->model_module_pdf_invoice) {
  912. $pdf_invoice = $this->model_module_pdf_invoice->getInvoice($order_info, true);
  913.  
  914. if ($pdf_invoice && file_exists($pdf_invoice)) {
  915. $template->data['emailtemplate_invoice_pdf'] = $pdf_invoice;
  916.  
  917. $mail->addAttachment($pdf_invoice);
  918. }
  919. }
  920. }
  921. }
  922.  
  923. $mail = $template->hook($mail);
  924.  
  925. $mail->send();
  926.  
  927. $template->sent();
  928.  
  929. // Admin Alert Mail
  930. if ($this->config->get('config_order_mail')) {
  931.  
  932. $template->data['order_link'] = (defined('HTTP_ADMIN') ? HTTP_ADMIN : HTTPS_SERVER.'admin/') . 'index.php?route=sale/order/info&order_id=' . $order_id;
  933.  
  934. // Total cart weight
  935. if (!empty($order_info['weight'])) {
  936. $template->data['order_weight'] = $this->weight->format($order_info['weight'], $this->config->get('config_weight_class_id'), $this->language->get('decimal_point'), $this->language->get('thousand_point'));
  937. }
  938.  
  939. // Total Customer Orders
  940. $sql = "SELECT COUNT(DISTINCT o.customer_id) AS total FROM `" . DB_PREFIX . "order` o WHERE o.customer_id > '0' AND o.order_status_id > '0'";
  941.  
  942. $result = $this->db->query($sql);
  943.  
  944. if ($result->row) {
  945. $template->data['total_orders'] = $result->row['total'];
  946. } else {
  947. $template->data['total_orders'] = 0;
  948. }
  949. $subject = sprintf($language->get('text_new_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'), $order_id);
  950.  
  951. // HTML Mail
  952. $data['text_greeting'] = $language->get('text_new_received');
  953.  
  954. if ($comment) {
  955. if ($order_info['comment']) {
  956. $data['comment'] = nl2br($comment) . '<br/><br/>' . $order_info['comment'];
  957. } else {
  958. $data['comment'] = nl2br($comment);
  959. }
  960. } else {
  961. if ($order_info['comment']) {
  962. $data['comment'] = $order_info['comment'];
  963. } else {
  964. $data['comment'] = '';
  965. }
  966. }
  967.  
  968. $data['text_download'] = '';
  969.  
  970. $data['text_footer'] = '';
  971.  
  972. $data['text_link'] = '';
  973. $data['link'] = '';
  974. $data['download'] = '';
  975.  
  976. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order.tpl')) {
  977.  
  978. } else {
  979.  
  980. }
  981.  
  982. // Text
  983. $text = $language->get('text_new_received') . "\n\n";
  984. $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
  985. $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
  986. $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";
  987. $text .= $language->get('text_new_products') . "\n";
  988.  
  989. foreach ($order_product_query->rows as $product) {
  990. $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";
  991.  
  992. $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'] . "'");
  993.  
  994. foreach ($order_option_query->rows as $option) {
  995. if ($option['type'] != 'file') {
  996. $value = $option['value'];
  997. } else {
  998. $value = utf8_substr($option['value'], 0, utf8_strrpos($option['value'], '.'));
  999. }
  1000.  
  1001. $text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) . "\n";
  1002. }
  1003. }
  1004.  
  1005. foreach ($order_voucher_query->rows as $voucher) {
  1006. $text .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
  1007. }
  1008.  
  1009. $text .= "\n";
  1010.  
  1011. $text .= $language->get('text_new_order_total') . "\n";
  1012.  
  1013. foreach ($order_total_query->rows as $total) {
  1014. $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
  1015. }
  1016.  
  1017. $text .= "\n";
  1018.  
  1019. if ($order_info['comment']) {
  1020. $text .= $language->get('text_new_comment') . "\n\n";
  1021. $text .= $order_info['comment'] . "\n\n";
  1022. }
  1023.  
  1024. $mail = new Mail();
  1025. $mail->protocol = $this->config->get('config_mail_protocol');
  1026. $mail->parameter = $this->config->get('config_mail_parameter');
  1027. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  1028. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  1029. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  1030. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  1031. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  1032.  
  1033. $mail->setTo($this->config->get('config_email'));
  1034. $mail->setFrom($this->config->get('config_email'));
  1035. $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
  1036. $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
  1037.  
  1038. $mail->setText($text);
  1039. $template->load('order.admin');
  1040. $template->build();
  1041.  
  1042. $mail = $template->hook($mail);
  1043.  
  1044. if ($template->data['emailtemplate']['attach_invoice']) {
  1045. if (!$this->config->get('pdf_invoice')) {
  1046. trigger_error('Error: could not attach invoice! Install module \'opencart pdf order invoice\'');
  1047. } else {
  1048. $this->load->model('module/pdf_invoice');
  1049.  
  1050. $pdf_invoice = $this->model_module_pdf_invoice->getInvoice($order_info, true);
  1051.  
  1052. if ($pdf_invoice && file_exists($pdf_invoice)) {
  1053. $template->data['emailtemplate_invoice_pdf'] = $pdf_invoice;
  1054.  
  1055. $mail->addAttachment($pdf_invoice);
  1056. }
  1057. }
  1058. }
  1059.  
  1060. $mail->send();
  1061.  
  1062. $template->sent();
  1063.  
  1064. // Send to additional alert emails
  1065. $emails = explode(',', $this->config->get('config_mail_alert'));
  1066.  
  1067. foreach ($emails as $email) {
  1068. if ($email && preg_match('/^[^\@]+@.*.[a-z]{2,15}$/i', $email)) {
  1069. $mail->setTo($email);
  1070. $mail->send();
  1071. }
  1072. }
  1073. }
  1074. }
  1075.  
  1076. // If order status is not 0 then send update text email
  1077. if ($order_info['order_status_id'] && $order_status_id && $notify) {
  1078. $language = new Language($order_info['language_directory']);
  1079. $language->load($order_info['language_directory']);
  1080. $language->load('mail/order');
  1081.  
  1082. $subject = sprintf($language->get('text_update_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
  1083.  
  1084. $message = $language->get('text_update_order') . ' ' . $order_id . "\n";
  1085. $message .= $language->get('text_update_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n\n";
  1086.  
  1087. $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'] . "'");
  1088.  
  1089. if ($order_status_query->num_rows) {
  1090. $message .= $language->get('text_update_order_status') . "\n\n";
  1091. $message .= $order_status_query->row['name'] . "\n\n";
  1092. }
  1093.  
  1094. if ($order_info['customer_id']) {
  1095. $message .= $language->get('text_update_link') . "\n";
  1096. $message .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
  1097. }
  1098.  
  1099. if ($comment) {
  1100. $message .= $language->get('text_update_comment') . "\n\n";
  1101. $message .= strip_tags($comment) . "\n\n";
  1102. }
  1103.  
  1104. $message .= $language->get('text_update_footer');
  1105.  
  1106. $file = DIR_SYSTEM . 'library/emailtemplate/email_template.php';
  1107.  
  1108. if (file_exists($file)) {
  1109. include_once($file);
  1110. } else {
  1111. trigger_error('Error: Could not load library ' . $file . '!');
  1112. exit();
  1113. }
  1114.  
  1115. $template = new EmailTemplate($this->request, $this->registry);
  1116.  
  1117. if (!empty($order_info['customer_group_id'])) {
  1118. $this->load->model('account/customer_group');
  1119. $template->data['customer_group'] = $this->model_account_customer_group->getCustomerGroup($order_info['customer_group_id']);
  1120. }
  1121.  
  1122. if (!empty($order_info['affiliate_id'])) {
  1123. $this->load->model('affiliate/affiliate');
  1124. $template->data['affiliate'] = $this->model_affiliate_affiliate->getAffiliate($order_info['affiliate_id']);
  1125. }
  1126.  
  1127. $template->addData($order_info);
  1128.  
  1129. $template->data['order_status_id'] = $order_status_id;
  1130.  
  1131. if ($order_status_query->num_rows) {
  1132. $template->data['order_status'] = $order_status_query->row['name'];
  1133. }
  1134.  
  1135. $template->data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added']));
  1136.  
  1137. if ($order_info['order_status_id'] != $order_status_id){
  1138. $template->data['prev_order_status_id'] = $order_info['order_status_id'];
  1139. }
  1140.  
  1141. $template->data['text_update_heading'] = sprintf($language->get('text_update_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
  1142.  
  1143. $template->data['comment'] = $comment;
  1144.  
  1145. if ($order_info['comment']) {
  1146. $template->data['order_comment'] = str_replace(array("\r\n", "\r", "\n"), "<br />", $order_info['comment']);
  1147. }
  1148.  
  1149. if ($comment && $order_info['comment'] != $comment) {
  1150. $template->data['instruction'] = str_replace(array("\r\n", "\r", "\n"), "<br />", $comment);
  1151. } else {
  1152. $template->data['instruction'] = '';
  1153. }
  1154.  
  1155. if ($order_info['customer_id']) {
  1156. $template->data['order_url'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id;
  1157. $template->data['order_url_tracking'] = $template->getTracking($template->data['order_url']);
  1158. }
  1159.  
  1160. // Custom fields
  1161. if (!empty($order_info['custom_field']) || !empty($order_info['payment_custom_field'])) {
  1162. $this->load->model('account/custom_field');
  1163.  
  1164. if (!empty($order_info['customer_group_id'])) {
  1165. $customer_group_id = $order_info['customer_group_id'];
  1166. } else {
  1167. $customer_group_id = $this->config->get('config_customer_group_id');
  1168. }
  1169.  
  1170. $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
  1171.  
  1172. foreach($custom_fields as $custom_field){
  1173. if (isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
  1174. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_name'] = $custom_field['name'];
  1175. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_value'] = $order_info['payment_custom_field'][$custom_field['custom_field_id']];
  1176. } elseif (isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
  1177. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_name'] = $custom_field['name'];
  1178. $template->data['custom_field_' . $custom_field['custom_field_id'] . '_value'] = $order_info['custom_field'][$custom_field['custom_field_id']];
  1179. }
  1180. }
  1181. }
  1182.  
  1183. $template->data['order_subject_products'] = '';
  1184.  
  1185. $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  1186.  
  1187. foreach ($order_product_query->rows as $order_product) {
  1188. $template->data['order_subject_products'] .= ($template->data['order_subject_products'] ? ', ' : '') . $order_product['name'];
  1189. }
  1190.  
  1191. if (strlen($template->data['order_subject_products']) > 32) {
  1192. $template->data['order_subject_products'] = trim(substr($template->data['order_subject_products'], 0, strrpos(substr($template->data['order_subject_products'], 0, 32), ' ')), "-_ \t\n\r\0\x0B") . '...';
  1193. }
  1194.  
  1195. if (isset($this->request->post['order_summary'])) {
  1196. $this->load->model('tool/image');
  1197. $this->load->model('tool/upload');
  1198. $this->load->model('catalog/product');
  1199.  
  1200. $order_voucher_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");
  1201.  
  1202. $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
  1203.  
  1204. $template->data['text_product'] = $language->get('text_new_product');
  1205. $template->data['text_model'] = $language->get('text_new_model');
  1206. $template->data['text_quantity'] = $language->get('text_new_quantity');
  1207. $template->data['text_price'] = $language->get('text_new_price');
  1208. $template->data['text_total'] = $language->get('text_new_total');
  1209.  
  1210. // Plain text
  1211. $message .= $language->get('text_new_products') . "\n";
  1212.  
  1213. foreach ($order_product_query->rows as $product) {
  1214. $message .= $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";
  1215.  
  1216. $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'] . "'");
  1217.  
  1218. foreach ($order_option_query->rows as $option) {
  1219. if ($option['type'] != 'file') {
  1220. $value = $option['value'];
  1221. } else {
  1222. $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
  1223.  
  1224. if ($upload_info) {
  1225. $value = $upload_info['name'];
  1226. } else {
  1227. $value = '';
  1228. }
  1229. }
  1230.  
  1231. $message .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) . "\n";
  1232. }
  1233. }
  1234.  
  1235. foreach ($order_voucher_query->rows as $voucher) {
  1236. $message .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
  1237. }
  1238.  
  1239. $message .= "\n";
  1240.  
  1241. $message .= $language->get('text_new_order_total') . "\n";
  1242.  
  1243. foreach ($order_total_query->rows as $total) {
  1244. $message .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
  1245. }
  1246.  
  1247. // HTML
  1248. $template->data['products'] = array();
  1249.  
  1250. foreach ($order_product_query->rows as $product) {
  1251. $product_data = $this->model_catalog_product->getProduct($product['product_id']);
  1252.  
  1253. $option_data = array();
  1254.  
  1255. $order_option_query = $this->db->query("SELECT oo.*, pov.* FROM " . DB_PREFIX . "order_option oo LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_option_value_id = oo.product_option_value_id) WHERE oo.order_id = '" . (int)$order_id . "' AND oo.order_product_id = '" . (int)$product['order_product_id'] . "'");
  1256.  
  1257. foreach ($order_option_query->rows as $option) {
  1258. if ($option['type'] != 'file') {
  1259. $value = $option['value'];
  1260. } else {
  1261. $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
  1262.  
  1263. if ($upload_info) {
  1264. $value = $upload_info['name'];
  1265. } else {
  1266. $value = '';
  1267. }
  1268. }
  1269.  
  1270. if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option['price']) {
  1271. $price = $this->currency->format($this->tax->calculate($option['price'], $product_data['tax_class_id'], $this->config->get('config_tax') ? 'P' : false));
  1272. } else {
  1273. $price = false;
  1274. }
  1275.  
  1276. $option_data[] = array(
  1277. 'name' => $option['name'],
  1278. 'price' => $price,
  1279. 'price_prefix' => isset($option['price_prefix']) ? $option['price_prefix'] : '',
  1280. 'stock_quantity' => isset($option['quantity']) ? $option['quantity'] : '',
  1281. 'stock_subtract' => isset($option['subtract']) ? $option['subtract'] : '',
  1282. 'value' => (utf8_strlen($value) > 120 ? utf8_substr($value, 0, 120) . '..' : $value)
  1283. );
  1284. }
  1285.  
  1286. if (isset($product_data['image'])) {
  1287. $this->model_tool_image->setUrl($order_info['store_url']);
  1288.  
  1289. $image = $this->model_tool_image->resize($product_data['image'], 50, 50);
  1290.  
  1291. if (substr($image, 0, 4) != 'http') {
  1292. $image = $order_info['store_url'] . ltrim($image, '/');
  1293. }
  1294. } else {
  1295. $image = '';
  1296. }
  1297.  
  1298. $url = $this->url->link('product/product', 'product_id='.$product['product_id'], 'SSL');
  1299.  
  1300. if ($this->config->get('config_stock_checkout') && $product_data['subtract']) {
  1301. $stock_quantity = $product_data['quantity'] + $product['quantity']; // quantity before order
  1302. } else {
  1303. $stock_quantity = false;
  1304. }
  1305.  
  1306. if ($stock_quantity && $product['quantity'] > $stock_quantity) {
  1307. $stock_backorder = $product['quantity'] - $stock_quantity;
  1308. } else {
  1309. $stock_backorder = false;
  1310. }
  1311.  
  1312. $template->data['products'][] = array(
  1313. 'product_id' => $product_data['product_id'],
  1314. 'url' => $url,
  1315. 'url_tracking' => $template->getTracking($url),
  1316. 'image' => $image,
  1317. 'weight' => ($product_data['weight'] > 0) ? $this->weight->format($product_data['weight'], $product_data['weight_class_id']) : 0,
  1318. 'description' => utf8_substr(strip_tags(html_entity_decode($product_data['description'], ENT_QUOTES, 'UTF-8')), 0, 200) . '..',
  1319. 'manufacturer' => $product_data['manufacturer'],
  1320. 'sku' => $product_data['sku'],
  1321. 'stock_status' => $product_data['stock_status'],
  1322. 'stock_subtract' => $product_data['subtract'],
  1323. 'stock_quantity' => $stock_quantity,
  1324. 'stock_backorder' => $stock_backorder,
  1325. 'name' => $product['name'],
  1326. 'model' => $product['model'],
  1327. 'option' => $option_data,
  1328. 'quantity' => $product['quantity'],
  1329. 'price' => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
  1330. 'total' => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value'])
  1331. );
  1332. }
  1333.  
  1334. $template->data['vouchers'] = array();
  1335.  
  1336. foreach ($order_voucher_query->rows as $voucher) {
  1337. $template->data['vouchers'][] = array(
  1338. 'description' => $voucher['description'],
  1339. 'amount' => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']),
  1340. );
  1341. }
  1342.  
  1343. foreach ($order_total_query->rows as $total) {
  1344. $template->data['totals'][] = array(
  1345. 'title' => $total['title'],
  1346. 'text' => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
  1347. );
  1348. }
  1349. }
  1350.  
  1351.  
  1352. $mail = new Mail();
  1353. $mail->protocol = $this->config->get('config_mail_protocol');
  1354. $mail->parameter = $this->config->get('config_mail_parameter');
  1355. $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
  1356. $mail->smtp_username = $this->config->get('config_mail_smtp_username');
  1357. $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
  1358. $mail->smtp_port = $this->config->get('config_mail_smtp_port');
  1359. $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
  1360.  
  1361. $mail->setTo($order_info['email']);
  1362. $mail->setFrom($this->config->get('config_email'));
  1363. $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
  1364. $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
  1365. $mail->setText($message);
  1366.  
  1367. if (!empty($this->request->post['emailtemplate_id'])) {
  1368. $template->load($this->request->post['emailtemplate_id']);
  1369. } else {
  1370. $template_load = array(
  1371. 'key' => 'order.update'
  1372. );
  1373.  
  1374. if (!empty($order_status_id)) {
  1375. $template_load['order_status_id'] = $order_status_id;
  1376. }
  1377.  
  1378. if (!empty($order_info['language_id'])) {
  1379. $template_load['language_id'] = $order_info['language_id'];
  1380. }
  1381.  
  1382. $template->load($template_load);
  1383. }
  1384.  
  1385. $mail = $template->hook($mail);
  1386.  
  1387. if ($template->data['emailtemplate']['attach_invoice']) {
  1388. if (!$this->config->get('pdf_invoice')) {
  1389. trigger_error('Error: could not attach invoice! Install module \'opencart pdf order invoice\'');
  1390. } else {
  1391. $this->load->model('module/pdf_invoice');
  1392.  
  1393. $pdf_invoice = $this->model_module_pdf_invoice->getInvoice($order_info, true);
  1394.  
  1395. if ($pdf_invoice && file_exists($pdf_invoice)) {
  1396. $template->data['emailtemplate_invoice_pdf'] = $pdf_invoice;
  1397.  
  1398. $mail->addAttachment($pdf_invoice);
  1399. }
  1400. }
  1401. }
  1402.  
  1403. $mail->send();
  1404.  
  1405. $template->sent();
  1406.  
  1407. }
  1408. }
  1409.  
  1410. $this->event->trigger('post.order.history.add', $order_id);
  1411. }
  1412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement