PageRenderTime 330ms CodeModel.GetById 80ms app.highlight 129ms RepoModel.GetById 113ms app.codeStats 0ms

/catalog/model/checkout/order.php

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