<modification>
	<id>Low Stock Alert  - Coded by waabay.com</id>
	<version>1.0.0</version>
	<vqmver>2.3.2</vqmver>
	<author>waabay</author>
	<email>waabay@gmail.com</email>
	<website>http://www.waabay.com</website>

	<file name="catalog/model/checkout/order.php">
		<operation error="skip">
			<search position="before"><![CDATA[
			foreach ($order_product_query->rows as $order_product) {
			]]></search>
			<add><![CDATA[
			/*code start*/
			if ($this->config->get('stock_alert_status')) {		
				$store_products = array();
				$store_low_stock_products[] = array();
				$store_option_low_stocks = array();			
				$sbps[] = array();
				$ols[] = array();
			}
			/*code end*/
			]]></add>
		</operation>

		<operation error="skip">
			<search position="after"><![CDATA[
			$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'");
			]]></search>
			<add><![CDATA[
			/*code start*/
				if ($this->config->get('stock_alert_status')) {					
					$pq = $this->db->query("SELECT p.product_id as product_id, pd.name AS name, p.quantity AS quantity, p.model AS model FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$order_product['product_id'] . "'");
							$store_products[] = array (
								'product_id'	=> $pq->row['product_id'],
								'name'     		=> $pq->row['name'],
								'model'    		=> $pq->row['model'],
								'quantity' 		=> $pq->row['quantity']
							);
						if ($pq->row['quantity'] < $this->config->get('stock_alert_quantity')) {
							$sbps[] = array ('product_id' => $pq->row['product_id']);
						}
				}
			/*code end*/
		]]></add>
		</operation>
		
		<operation error="skip">
			<search position="after"><![CDATA[
			$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'");
			]]></search>
			<add><![CDATA[
			/*code start*/
				if ($this->config->get('stock_alert_status')) {
					$qovd = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_value_description ovd LEFT JOIN " . DB_PREFIX . "product_option_value pov on (ovd.option_value_id = pov.option_value_id) WHERE pov.product_option_value_id = '" . (int)$option['product_option_value_id'] . "'");
						if ($qovd->row['quantity'] < $this->config->get('stock_alert_option_quantity')) {
							$store_option_low_stocks[] = array (
								'product_id'		=> $qovd->row['product_id'],
								'option_name'     	=> $qovd->row['name'],
								'option_quantity' 	=> $qovd->row['quantity']
							);
							$ols[] = array ('product_id' => $qovd->row['product_id']);
						}						
				}	
			/*code end*/
		]]></add>
		</operation>
		
		<operation error="skip">
			<search position="before"><![CDATA[
			$this->cache->delete('product');
			]]></search>
			<add><![CDATA[
			/*code start*/
			if ($this->config->get('stock_alert_status')) {
				$merge_array = array_merge($sbps,$ols);
				$unique_products = array_map("unserialize", array_unique(array_map("serialize", $merge_array)));
				
				foreach ($unique_products as $unique_product) {
					if (isset($unique_product['product_id'])) {
						foreach ($store_products as $store_product) {					
							if ($unique_product['product_id'] == $store_product['product_id']) {
								$store_low_stock_products[] = array (
									'product_id'	=> $store_product['product_id'],
									'name'     		=> $store_product['name'],
									'model'    		=> $store_product['model'],
									'quantity' 		=> $store_product['quantity']
								);				
							}
						}
					}
				}
				
				$pes = false;
				foreach ($unique_products as $up) {
					if (isset($up['product_id'])) {
						$pes = true;
					}
				}
			}
			/*code end*/
			]]></add>
		</operation>
		
		<operation error="skip">
			<search position="after"><![CDATA[
			$this->cache->delete('product');
			]]></search>
			<add><![CDATA[
			/*code start*/
			if ($this->config->get('stock_alert_status')) {
				if ($pes){				
					$language = new Language($order_info['language_directory']);
					$language->load('mail/stock_alert');
					$template = new Template();
					
					$mail = new Mail(); 
					$mail->protocol = $this->config->get('config_mail_protocol');
					$mail->parameter = $this->config->get('config_mail_parameter');
					$mail->hostname = $this->config->get('config_smtp_host');
					$mail->username = $this->config->get('config_smtp_username');
					$mail->password = $this->config->get('config_smtp_password');
					$mail->port = $this->config->get('config_smtp_port');
					$mail->timeout = $this->config->get('config_smtp_timeout');
					$mail->setTo($this->config->get('config_email'));
					$mail->setFrom($this->config->get('config_email'));
					$mail->setSender($this->config->get('config_name'));
					$mail->setSubject($language->get('text_email_subject'));
					
					$template->data['logo'] = HTTP_SERVER . 'image/' . $this->config->get('config_logo');
					$template->data['store_name'] = $order_info['store_name'];
					$template->data['store_url'] = $order_info['store_url'];
					
					$template->data['stock_alert_name'] = 'Dear ' . $this->config->get('config_name') . ',' . "\n\n";
					$template->data['stock_alert_message'] = $this->config->get('stock_alert_message') . "\n\n";
					
					$template->data['product'] = $language->get('column_product');
					$template->data['model'] = $language->get('column_model');
					$template->data['quantity'] = $language->get('column_quantity');
					$template->data['auto_msg'] = $language->get('text_auto_msg');
					$template->data['low_stock_alert'] = sprintf($language->get('text_low_stock_alert'),$order_info['store_name']);
					$template->data['signature'] = $language->get('text_signature') . ',<br>' . $order_info['store_name'] . $language->get('text_signature_name');
										
					if ($store_low_stock_products) {
					foreach ($store_low_stock_products as $store_low_stock_product) {
						if ($store_low_stock_product) {
							$template->data['low_stocks'][] = array(
								'product_id'	=> $store_low_stock_product['product_id'],
								'name'     		=> $store_low_stock_product['name'],
								'model'    		=> $store_low_stock_product['model'],
								'quantity' 		=> $store_low_stock_product['quantity']
							);
						}
					}
					} else {
						$template->data['low_stocks'] = false;
					}
					
					if ($store_option_low_stocks) {
						foreach ($store_option_low_stocks as $store_option_low_stock) {
							$template->data['option_low_stocks'][] = array(
								'product_id'      => $store_option_low_stock['product_id'],
								'option_name'     => $store_option_low_stock['option_name'],
								'option_quantity' => $store_option_low_stock['option_quantity']
							);
						}
					} else {
						$template->data['option_low_stocks'] = false;
					}
						
					if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/stock_alert.tpl')) {
						$html = $template->fetch($this->config->get('config_template') . '/template/mail/stock_alert.tpl');
					} else {
						$html = $template->fetch('default/template/mail/stock_alert.tpl');
					}
				
					$mail->setHtml($html);
					$mail->send();
					
					if ($this->config->get('stock_alert_email')) {
						$emails = explode(',', $this->config->get('stock_alert_email'));
					
						foreach ($emails as $email) {
							if ($email && preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $email)) {
								$mail->setTo($email);
								$mail->send();
							}
						}
					}					
				}
			}
			/*code end*/
			]]></add>
		</operation>
	</file>
</modification>