PageRenderTime 56ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/model/checkout/order.php

https://bitbucket.org/monobasic/shop.volero.ch
PHP | 565 lines | 453 code | 104 blank | 8 comment | 52 complexity | a6e05e4cb997f096c4cd697ed43cff93 MD5 | raw file
  1. <?php
  2. class ModelCheckoutOrder extends Model {
  3. public function create($data) {
  4. $this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', store_id = '" . (int)$data['store_id'] . "', store_name = '" . $this->db->escape($data['store_name']) . "', store_url = '" . $this->db->escape($data['store_url']) . "', customer_id = '" . (int)$data['customer_id'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', 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_method = '" . $this->db->escape($data['shipping_method']) . "', 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_method = '" . $this->db->escape($data['payment_method']) . "', comment = '" . $this->db->escape($data['comment']) . "', total = '" . (float)$data['total'] . "', reward = '" . (float)$data['reward'] . "', affiliate_id = '" . (int)$data['affiliate_id'] . "', commission = '" . (float)$data['commission'] . "', 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']) . "', date_added = NOW(), date_modified = NOW()");
  5. $order_id = $this->db->getLastId();
  6. foreach ($data['products'] as $product) {
  7. $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'] . "'");
  8. $order_product_id = $this->db->getLastId();
  9. foreach ($product['option'] as $option) {
  10. $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']) . "'");
  11. }
  12. foreach ($product['download'] as $download) {
  13. $this->db->query("INSERT INTO " . DB_PREFIX . "order_download SET order_id = '" . (int)$order_id . "', order_product_id = '" . (int)$order_product_id . "', name = '" . $this->db->escape($download['name']) . "', filename = '" . $this->db->escape($download['filename']) . "', mask = '" . $this->db->escape($download['mask']) . "', remaining = '" . (int)($download['remaining'] * $product['quantity']) . "'");
  14. }
  15. }
  16. foreach ($data['totals'] as $total) {
  17. $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']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
  18. }
  19. return $order_id;
  20. }
  21. public function getOrder($order_id) {
  22. $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 . "'");
  23. if ($order_query->num_rows) {
  24. $country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['shipping_country_id'] . "'");
  25. if ($country_query->num_rows) {
  26. $shipping_iso_code_2 = $country_query->row['iso_code_2'];
  27. $shipping_iso_code_3 = $country_query->row['iso_code_3'];
  28. } else {
  29. $shipping_iso_code_2 = '';
  30. $shipping_iso_code_3 = '';
  31. }
  32. $zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['shipping_zone_id'] . "'");
  33. if ($zone_query->num_rows) {
  34. $shipping_zone_code = $zone_query->row['code'];
  35. } else {
  36. $shipping_zone_code = '';
  37. }
  38. $country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['payment_country_id'] . "'");
  39. if ($country_query->num_rows) {
  40. $payment_iso_code_2 = $country_query->row['iso_code_2'];
  41. $payment_iso_code_3 = $country_query->row['iso_code_3'];
  42. } else {
  43. $payment_iso_code_2 = '';
  44. $payment_iso_code_3 = '';
  45. }
  46. $zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['payment_zone_id'] . "'");
  47. if ($zone_query->num_rows) {
  48. $payment_zone_code = $zone_query->row['code'];
  49. } else {
  50. $payment_zone_code = '';
  51. }
  52. $this->load->model('localisation/language');
  53. $language_info = $this->model_localisation_language->getLanguage($order_query->row['language_id']);
  54. if ($language_info) {
  55. $language_code = $language_info['code'];
  56. $language_filename = $language_info['filename'];
  57. $language_directory = $language_info['directory'];
  58. } else {
  59. $language_code = '';
  60. $language_filename = '';
  61. $language_directory = '';
  62. }
  63. return array(
  64. 'order_id' => $order_query->row['order_id'],
  65. 'invoice_no' => $order_query->row['invoice_no'],
  66. 'invoice_prefix' => $order_query->row['invoice_prefix'],
  67. 'store_id' => $order_query->row['store_id'],
  68. 'store_name' => $order_query->row['store_name'],
  69. 'store_url' => $order_query->row['store_url'],
  70. 'customer_id' => $order_query->row['customer_id'],
  71. 'firstname' => $order_query->row['firstname'],
  72. 'lastname' => $order_query->row['lastname'],
  73. 'telephone' => $order_query->row['telephone'],
  74. 'fax' => $order_query->row['fax'],
  75. 'email' => $order_query->row['email'],
  76. 'shipping_firstname' => $order_query->row['shipping_firstname'],
  77. 'shipping_lastname' => $order_query->row['shipping_lastname'],
  78. 'shipping_company' => $order_query->row['shipping_company'],
  79. 'shipping_address_1' => $order_query->row['shipping_address_1'],
  80. 'shipping_address_2' => $order_query->row['shipping_address_2'],
  81. 'shipping_postcode' => $order_query->row['shipping_postcode'],
  82. 'shipping_city' => $order_query->row['shipping_city'],
  83. 'shipping_zone_id' => $order_query->row['shipping_zone_id'],
  84. 'shipping_zone' => $order_query->row['shipping_zone'],
  85. 'shipping_zone_code' => $shipping_zone_code,
  86. 'shipping_country_id' => $order_query->row['shipping_country_id'],
  87. 'shipping_country' => $order_query->row['shipping_country'],
  88. 'shipping_iso_code_2' => $shipping_iso_code_2,
  89. 'shipping_iso_code_3' => $shipping_iso_code_3,
  90. 'shipping_address_format' => $order_query->row['shipping_address_format'],
  91. 'shipping_method' => $order_query->row['shipping_method'],
  92. 'payment_firstname' => $order_query->row['payment_firstname'],
  93. 'payment_lastname' => $order_query->row['payment_lastname'],
  94. 'payment_company' => $order_query->row['payment_company'],
  95. 'payment_address_1' => $order_query->row['payment_address_1'],
  96. 'payment_address_2' => $order_query->row['payment_address_2'],
  97. 'payment_postcode' => $order_query->row['payment_postcode'],
  98. 'payment_city' => $order_query->row['payment_city'],
  99. 'payment_zone_id' => $order_query->row['payment_zone_id'],
  100. 'payment_zone' => $order_query->row['payment_zone'],
  101. 'payment_zone_code' => $payment_zone_code,
  102. 'payment_country_id' => $order_query->row['payment_country_id'],
  103. 'payment_country' => $order_query->row['payment_country'],
  104. 'payment_iso_code_2' => $payment_iso_code_2,
  105. 'payment_iso_code_3' => $payment_iso_code_3,
  106. 'payment_address_format' => $order_query->row['payment_address_format'],
  107. 'payment_method' => $order_query->row['payment_method'],
  108. 'comment' => $order_query->row['comment'],
  109. 'total' => $order_query->row['total'],
  110. 'order_status_id' => $order_query->row['order_status_id'],
  111. 'order_status' => $order_query->row['order_status'],
  112. 'language_id' => $order_query->row['language_id'],
  113. 'language_code' => $language_code,
  114. 'language_filename' => $language_filename,
  115. 'language_directory' => $language_directory,
  116. 'currency_id' => $order_query->row['currency_id'],
  117. 'currency_code' => $order_query->row['currency_code'],
  118. 'currency_value' => $order_query->row['currency_value'],
  119. 'date_modified' => $order_query->row['date_modified'],
  120. 'date_added' => $order_query->row['date_added'],
  121. 'ip' => $order_query->row['ip']
  122. );
  123. } else {
  124. return false;
  125. }
  126. }
  127. public function confirm($order_id, $order_status_id, $comment = '', $notify = false) {
  128. $order_info = $this->getOrder($order_id);
  129. if ($order_info && !$order_info['order_status_id']) {
  130. $this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = '" . (int)$order_status_id . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'");
  131. $this->db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '1', comment = '" . $this->db->escape(($comment && $notify) ? $comment : '') . "', date_added = NOW()");
  132. $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  133. foreach ($order_product_query->rows as $order_product) {
  134. $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'");
  135. $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'] . "'");
  136. foreach ($order_option_query->rows as $option) {
  137. $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'");
  138. }
  139. }
  140. $this->cache->delete('product');
  141. $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "'");
  142. foreach ($order_total_query->rows as $order_total) {
  143. $this->load->model('total/' . $order_total['code']);
  144. if (method_exists($this->{'model_total_' . $order_total['code']}, 'confirm')) {
  145. $this->{'model_total_' . $order_total['code']}->confirm($order_info, $order_total);
  146. }
  147. }
  148. // Send out any gift voucher mails
  149. if ($this->config->get('config_complete_status_id') == $order_status_id) {
  150. $this->load->model('checkout/voucher');
  151. $this->model_checkout_voucher->confirm($order_id);
  152. }
  153. // Send out order confirmation mail
  154. $language = new Language($order_info['language_directory']);
  155. $language->load($order_info['language_filename']);
  156. $language->load('mail/order');
  157. $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'] . "'");
  158. if ($order_status_query->num_rows) {
  159. $order_status = $order_status_query->row['name'];
  160. } else {
  161. $order_status = '';
  162. }
  163. $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
  164. $order_total_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
  165. $order_download_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_download WHERE order_id = '" . (int)$order_id . "'");
  166. $subject = sprintf($language->get('text_new_subject'), $order_info['store_name'], $order_id);
  167. // HTML Mail
  168. $template = new Template();
  169. $template->data['title'] = sprintf($language->get('text_new_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
  170. $template->data['text_greeting'] = sprintf($language->get('text_new_greeting'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
  171. $template->data['text_link'] = $language->get('text_new_link');
  172. $template->data['text_download'] = $language->get('text_new_download');
  173. $template->data['text_order_detail'] = $language->get('text_new_order_detail');
  174. $template->data['text_instruction'] = $language->get('text_new_instruction');
  175. $template->data['text_order_id'] = $language->get('text_new_order_id');
  176. $template->data['text_date_added'] = $language->get('text_new_date_added');
  177. $template->data['text_payment_method'] = $language->get('text_new_payment_method');
  178. $template->data['text_shipping_method'] = $language->get('text_new_shipping_method');
  179. $template->data['text_email'] = $language->get('text_new_email');
  180. $template->data['text_telephone'] = $language->get('text_new_telephone');
  181. $template->data['text_ip'] = $language->get('text_new_ip');
  182. $template->data['text_payment_address'] = $language->get('text_new_payment_address');
  183. $template->data['text_shipping_address'] = $language->get('text_new_shipping_address');
  184. $template->data['text_product'] = $language->get('text_new_product');
  185. $template->data['text_model'] = $language->get('text_new_model');
  186. $template->data['text_quantity'] = $language->get('text_new_quantity');
  187. $template->data['text_price'] = $language->get('text_new_price');
  188. $template->data['text_total'] = $language->get('text_new_total');
  189. $template->data['text_footer'] = $language->get('text_new_footer');
  190. $template->data['text_powered'] = $language->get('text_new_powered');
  191. $template->data['logo'] = 'cid:' . md5(basename($this->config->get('config_logo')));
  192. $template->data['store_name'] = $order_info['store_name'];
  193. $template->data['store_url'] = $order_info['store_url'];
  194. $template->data['customer_id'] = $order_info['customer_id'];
  195. $template->data['link'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id;
  196. if ($order_download_query->num_rows) {
  197. $template->data['download'] = $order_info['store_url'] . 'index.php?route=account/download';
  198. } else {
  199. $template->data['download'] = '';
  200. }
  201. $template->data['order_id'] = $order_id;
  202. $template->data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added']));
  203. $template->data['payment_method'] = $order_info['payment_method'];
  204. $template->data['shipping_method'] = $order_info['shipping_method'];
  205. $template->data['email'] = $order_info['email'];
  206. $template->data['telephone'] = $order_info['telephone'];
  207. $template->data['ip'] = $order_info['ip'];
  208. if ($comment && $notify) {
  209. $template->data['comment'] = nl2br($comment);
  210. } else {
  211. $template->data['comment'] = '';
  212. }
  213. if ($order_info['shipping_address_format']) {
  214. $format = $order_info['shipping_address_format'];
  215. } else {
  216. $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
  217. }
  218. $find = array(
  219. '{firstname}',
  220. '{lastname}',
  221. '{company}',
  222. '{address_1}',
  223. '{address_2}',
  224. '{city}',
  225. '{postcode}',
  226. '{zone}',
  227. '{zone_code}',
  228. '{country}'
  229. );
  230. $replace = array(
  231. 'firstname' => $order_info['shipping_firstname'],
  232. 'lastname' => $order_info['shipping_lastname'],
  233. 'company' => $order_info['shipping_company'],
  234. 'address_1' => $order_info['shipping_address_1'],
  235. 'address_2' => $order_info['shipping_address_2'],
  236. 'city' => $order_info['shipping_city'],
  237. 'postcode' => $order_info['shipping_postcode'],
  238. 'zone' => $order_info['shipping_zone'],
  239. 'zone_code' => $order_info['shipping_zone_code'],
  240. 'country' => $order_info['shipping_country']
  241. );
  242. $template->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))));
  243. if ($order_info['payment_address_format']) {
  244. $format = $order_info['payment_address_format'];
  245. } else {
  246. $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
  247. }
  248. $find = array(
  249. '{firstname}',
  250. '{lastname}',
  251. '{company}',
  252. '{address_1}',
  253. '{address_2}',
  254. '{city}',
  255. '{postcode}',
  256. '{zone}',
  257. '{zone_code}',
  258. '{country}'
  259. );
  260. $replace = array(
  261. 'firstname' => $order_info['payment_firstname'],
  262. 'lastname' => $order_info['payment_lastname'],
  263. 'company' => $order_info['payment_company'],
  264. 'address_1' => $order_info['payment_address_1'],
  265. 'address_2' => $order_info['payment_address_2'],
  266. 'city' => $order_info['payment_city'],
  267. 'postcode' => $order_info['payment_postcode'],
  268. 'zone' => $order_info['payment_zone'],
  269. 'zone_code' => $order_info['payment_zone_code'],
  270. 'country' => $order_info['payment_country']
  271. );
  272. $template->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))));
  273. $template->data['products'] = array();
  274. foreach ($order_product_query->rows as $product) {
  275. $option_data = array();
  276. $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'] . "'");
  277. foreach ($order_option_query->rows as $option) {
  278. if ($option['type'] != 'file') {
  279. $option_data[] = array(
  280. 'name' => $option['name'],
  281. 'value' => (strlen($option['value']) > 20 ? substr($option['value'], 0, 20) . '..' : $option['value'])
  282. );
  283. } else {
  284. $filename = substr($option['value'], 0, strrpos($option['value'], '.'));
  285. $option_data[] = array(
  286. 'name' => $option['name'],
  287. 'value' => (strlen($filename) > 20 ? substr($filename, 0, 20) . '..' : $filename)
  288. );
  289. }
  290. }
  291. $template->data['products'][] = array(
  292. 'name' => $product['name'],
  293. 'model' => $product['model'],
  294. 'option' => $option_data,
  295. 'quantity' => $product['quantity'],
  296. 'price' => $this->currency->format($product['price'], $order_info['currency_code'], $order_info['currency_value']),
  297. 'total' => $this->currency->format($product['total'], $order_info['currency_code'], $order_info['currency_value'])
  298. );
  299. }
  300. $template->data['totals'] = $order_total_query->rows;
  301. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order.tpl')) {
  302. $html = $template->fetch($this->config->get('config_template') . '/template/mail/order.tpl');
  303. } else {
  304. $html = $template->fetch('default/template/mail/order.tpl');
  305. }
  306. // Text Mail
  307. $text = sprintf($language->get('text_new_greeting'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')) . "\n\n";
  308. $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
  309. $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
  310. $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";
  311. if ($comment && $notify) {
  312. $text .= $language->get('text_new_instruction') . "\n\n";
  313. $text .= $comment . "\n\n";
  314. }
  315. $text .= $language->get('text_new_products') . "\n";
  316. foreach ($order_product_query->rows as $result) {
  317. $text .= $result['quantity'] . 'x ' . $result['name'] . ' (' . $result['model'] . ') ' . html_entity_decode($this->currency->format($result['total'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
  318. $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . $result['order_product_id'] . "'");
  319. foreach ($order_option_query->rows as $option) {
  320. $text .= chr(9) . '-' . $option['name'] . ' ' . (strlen($option['value']) > 20 ? substr($option['value'], 0, 20) . '..' : $option['value']) . "\n";
  321. }
  322. }
  323. $text .= "\n";
  324. $text .= $language->get('text_new_order_total') . "\n";
  325. foreach ($order_total_query->rows as $result) {
  326. $text .= $result['title'] . ' ' . html_entity_decode($result['text'], ENT_NOQUOTES, 'UTF-8') . "\n";
  327. }
  328. $text .= "\n";
  329. if ($order_info['customer_id']) {
  330. $text .= $language->get('text_new_link') . "\n";
  331. $text .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
  332. }
  333. if ($order_download_query->num_rows) {
  334. $text .= $language->get('text_new_download') . "\n";
  335. $text .= $order_info['store_url'] . 'index.php?route=account/download' . "\n\n";
  336. }
  337. if ($order_info['comment']) {
  338. $text .= $language->get('text_new_comment') . "\n\n";
  339. $text .= $order_info['comment'] . "\n\n";
  340. }
  341. $text .= $language->get('text_new_footer') . "\n\n";
  342. $mail = new Mail();
  343. $mail->protocol = $this->config->get('config_mail_protocol');
  344. $mail->parameter = $this->config->get('config_mail_parameter');
  345. $mail->hostname = $this->config->get('config_smtp_host');
  346. $mail->username = $this->config->get('config_smtp_username');
  347. $mail->password = $this->config->get('config_smtp_password');
  348. $mail->port = $this->config->get('config_smtp_port');
  349. $mail->timeout = $this->config->get('config_smtp_timeout');
  350. $mail->setTo($order_info['email']);
  351. $mail->setFrom($this->config->get('config_email'));
  352. $mail->setSender($order_info['store_name']);
  353. $mail->setSubject($subject);
  354. $mail->setHtml($html);
  355. $mail->setText(html_entity_decode($text, ENT_QUOTES, 'UTF-8'));
  356. $mail->addAttachment(DIR_IMAGE . $this->config->get('config_logo'), md5(basename($this->config->get('config_logo'))));
  357. $mail->send();
  358. // Admin Alert Mail
  359. if ($this->config->get('config_alert_mail')) {
  360. $subject = sprintf($language->get('text_new_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'), $order_id);
  361. // Text
  362. $text = $language->get('text_new_received') . "\n\n";
  363. $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
  364. $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
  365. $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";
  366. $text .= $language->get('text_new_products') . "\n";
  367. foreach ($order_product_query->rows as $result) {
  368. $text .= $result['quantity'] . 'x ' . $result['name'] . ' (' . $result['model'] . ') ' . html_entity_decode($this->currency->format($result['total'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
  369. $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . $result['order_product_id'] . "'");
  370. foreach ($order_option_query->rows as $option) {
  371. $text .= chr(9) . '-' . $option['name'] . ' ' . (strlen($option['value']) > 20 ? substr($option['value'], 0, 20) . '..' : $option['value']) . "\n";
  372. }
  373. }
  374. $text .= "\n";
  375. $text .= $language->get('text_new_order_total') . "\n";
  376. foreach ($order_total_query->rows as $result) {
  377. $text .= $result['title'] . ' ' . html_entity_decode($result['text'], ENT_NOQUOTES, 'UTF-8') . "\n";
  378. }
  379. $text .= "\n";
  380. if ($order_info['comment'] != '') {
  381. $comment = ($order_info['comment'] . "\n\n" . $comment);
  382. }
  383. if ($comment) {
  384. $text .= $language->get('text_new_comment') . "\n\n";
  385. $text .= $comment . "\n\n";
  386. }
  387. $mail = new Mail();
  388. $mail->protocol = $this->config->get('config_mail_protocol');
  389. $mail->parameter = $this->config->get('config_mail_parameter');
  390. $mail->hostname = $this->config->get('config_smtp_host');
  391. $mail->username = $this->config->get('config_smtp_username');
  392. $mail->password = $this->config->get('config_smtp_password');
  393. $mail->port = $this->config->get('config_smtp_port');
  394. $mail->timeout = $this->config->get('config_smtp_timeout');
  395. $mail->setTo($this->config->get('config_email'));
  396. $mail->setFrom($this->config->get('config_email'));
  397. $mail->setSender($order_info['store_name']);
  398. $mail->setSubject($subject);
  399. $mail->setText($text);
  400. $mail->send();
  401. // Send to additional alert emails
  402. $emails = explode(',', $this->config->get('config_alert_emails'));
  403. foreach ($emails as $email) {
  404. if ($email && preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $email)) {
  405. $mail->setTo($email);
  406. $mail->send();
  407. }
  408. }
  409. }
  410. }
  411. }
  412. public function update($order_id, $order_status_id, $comment = '', $notify = false) {
  413. $order_info = $this->getOrder($order_id);
  414. if ($order_info && $order_info['order_status_id']) {
  415. $this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = '" . (int)$order_status_id . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'");
  416. $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()");
  417. // Send out any gift voucher mails
  418. if ($this->config->get('config_complete_status_id') == $order_status_id) {
  419. $this->load->model('checkout/voucher');
  420. $this->model_checkout_voucher->confirm($order_id);
  421. }
  422. if ($notify) {
  423. $language = new Language($order_info['language_directory']);
  424. $language->load($order_info['language_filename']);
  425. $language->load('mail/order');
  426. $subject = sprintf($language->get('text_update_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
  427. $message = $language->get('text_update_order') . ' ' . $order_id . "\n";
  428. $message .= $language->get('text_update_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n\n";
  429. $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'] . "'");
  430. if ($order_status_query->num_rows) {
  431. $message .= $language->get('text_update_order_status') . "\n\n";
  432. $message .= $order_status_query->row['name'] . "\n\n";
  433. }
  434. if ($order_info['customer_id']) {
  435. $message .= $language->get('text_update_link') . "\n";
  436. $message .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
  437. }
  438. if ($comment) {
  439. $message .= $language->get('text_update_comment') . "\n\n";
  440. $message .= $comment . "\n\n";
  441. }
  442. $message .= $language->get('text_update_footer');
  443. $mail = new Mail();
  444. $mail->protocol = $this->config->get('config_mail_protocol');
  445. $mail->parameter = $this->config->get('config_mail_parameter');
  446. $mail->hostname = $this->config->get('config_smtp_host');
  447. $mail->username = $this->config->get('config_smtp_username');
  448. $mail->password = $this->config->get('config_smtp_password');
  449. $mail->port = $this->config->get('config_smtp_port');
  450. $mail->timeout = $this->config->get('config_smtp_timeout');
  451. $mail->setTo($order_info['email']);
  452. $mail->setFrom($this->config->get('config_email'));
  453. $mail->setSender($order_info['store_name']);
  454. $mail->setSubject($subject);
  455. $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
  456. $mail->send();
  457. }
  458. }
  459. }
  460. }
  461. ?>