Exemplo n.º 1
0
 public function confirm($order_id, $order_status_id, $comment = '')
 {
     $order_query = $this->db->query("SELECT *, l.filename AS filename, l.directory AS directory FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "language l ON (o.language_id = l.language_id) WHERE o.order_id = '" . (int) $order_id . "' AND o.order_status_id = '0'");
     if ($order_query->num_rows) {
         $this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = '" . (int) $order_status_id . "' WHERE order_id = '" . (int) $order_id . "'");
         $this->db->query("INSERT INTO " . DB_PREFIX . "order_history (order_id, order_status_id, notify, comment, date_added) VALUES ('" . (int) $order_id . "', '" . (int) $order_status_id . "', '1', '" . $this->db->escape($comment) . "', NOW())");
         $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int) $order_id . "'");
         foreach ($order_product_query->rows as $product) {
             $this->db->query("UPDATE " . DB_PREFIX . "product SET quantity = (quantity - " . (int) $product['quantity'] . ") WHERE product_id = '" . (int) $product['product_id'] . "' AND subtract = '1'");
             $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'] . "'");
             foreach ($order_option_query->rows as $option) {
                 $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'");
             }
             $this->cache->delete('product');
         }
         $language = new Language($order_query->row['directory']);
         $language->load($order_query->row['filename']);
         $language->load('mail/order_confirm');
         $this->load->model('localisation/currency');
         $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_query->row['language_id'] . "'");
         $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int) $order_id . "'");
         $order_total_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int) $order_id . "' ORDER BY sort_order ASC");
         $order_download_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_download WHERE order_id = '" . (int) $order_id . "'");
         $subject = sprintf($language->get('text_subject'), $order_query->row['store_name'], $order_id);
         // HTML Mail
         $template = new Template();
         $template->data['title'] = sprintf($language->get('text_subject'), html_entity_decode($order_query->row['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
         $template->data['text_greeting'] = sprintf($language->get('text_greeting'), html_entity_decode($order_query->row['store_name'], ENT_QUOTES, 'UTF-8'));
         $template->data['text_order_detail'] = $language->get('text_order_detail');
         $template->data['text_order_id'] = $language->get('text_order_id');
         $template->data['text_invoice'] = $language->get('text_invoice');
         $template->data['text_date_added'] = $language->get('text_date_added');
         $template->data['text_telephone'] = $language->get('text_telephone');
         $template->data['text_email'] = $language->get('text_email');
         $template->data['text_ip'] = $language->get('text_ip');
         $template->data['text_fax'] = $language->get('text_fax');
         $template->data['text_shipping_address'] = $language->get('text_shipping_address');
         $template->data['text_payment_address'] = $language->get('text_payment_address');
         $template->data['text_shipping_method'] = $language->get('text_shipping_method');
         $template->data['text_payment_method'] = $language->get('text_payment_method');
         $template->data['text_comment'] = $language->get('text_comment');
         $template->data['text_powered_by'] = $language->get('text_powered_by');
         $template->data['column_product'] = $language->get('column_product');
         $template->data['column_model'] = $language->get('column_model');
         $template->data['column_quantity'] = $language->get('column_quantity');
         $template->data['column_price'] = $language->get('column_price');
         $template->data['column_total'] = $language->get('column_total');
         $template->data['order_id'] = $order_id;
         $template->data['customer_id'] = $order_query->row['customer_id'];
         $template->data['date_added'] = date($language->get('date_format_short'), strtotime($order_query->row['date_added']));
         $template->data['logo'] = 'cid:' . basename($this->config->get('config_logo'));
         $template->data['store_name'] = $order_query->row['store_name'];
         $template->data['address'] = nl2br($this->config->get('config_address'));
         $template->data['telephone'] = $this->config->get('config_telephone');
         $template->data['fax'] = $this->config->get('config_fax');
         $template->data['email'] = $this->config->get('config_email');
         $template->data['store_url'] = $order_query->row['store_url'];
         $template->data['invoice'] = $order_query->row['store_url'] . 'index.php?route=account/invoice&order_id=' . $order_id;
         $template->data['firstname'] = $order_query->row['firstname'];
         $template->data['lastname'] = $order_query->row['lastname'];
         $template->data['shipping_method'] = $order_query->row['shipping_method'];
         $template->data['payment_method'] = $order_query->row['payment_method'];
         $template->data['customer_email'] = $order_query->row['email'];
         $template->data['customer_telephone'] = $order_query->row['telephone'];
         $template->data['customer_ip'] = $order_query->row['ip'];
         $template->data['comment'] = nl2br($order_query->row['comment']);
         if ($comment) {
             $template->data['comment'] .= '<br /><br />' . nl2br($comment);
         }
         $zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int) $order_query->row['shipping_zone_id'] . "'");
         if ($zone_query->num_rows) {
             $zone_code = $zone_query->row['code'];
         } else {
             $zone_code = '';
         }
         if ($order_query->row['shipping_address_format']) {
             $format = $order_query->row['shipping_address_format'];
         } else {
             $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
         }
         $find = array('{firstname}', '{lastname}', '{company}', '{address_1}', '{address_2}', '{city}', '{postcode}', '{zone}', '{zone_code}', '{country}');
         $replace = array('firstname' => $order_query->row['shipping_firstname'], 'lastname' => $order_query->row['shipping_lastname'], 'company' => $order_query->row['shipping_company'], 'address_1' => $order_query->row['shipping_address_1'], 'address_2' => $order_query->row['shipping_address_2'], 'city' => $order_query->row['shipping_city'], 'postcode' => $order_query->row['shipping_postcode'], 'zone' => $order_query->row['shipping_zone'], 'zone_code' => $zone_code, 'country' => $order_query->row['shipping_country']);
         $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))));
         $zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int) $order_query->row['payment_zone_id'] . "'");
         if ($zone_query->num_rows) {
             $zone_code = $zone_query->row['code'];
         } else {
             $zone_code = '';
         }
         if ($order_query->row['payment_address_format']) {
             $format = $order_query->row['payment_address_format'];
         } else {
             $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
         }
         $find = array('{firstname}', '{lastname}', '{company}', '{address_1}', '{address_2}', '{city}', '{postcode}', '{zone}', '{zone_code}', '{country}');
         $replace = array('firstname' => $order_query->row['payment_firstname'], 'lastname' => $order_query->row['payment_lastname'], 'company' => $order_query->row['payment_company'], 'address_1' => $order_query->row['payment_address_1'], 'address_2' => $order_query->row['payment_address_2'], 'city' => $order_query->row['payment_city'], 'postcode' => $order_query->row['payment_postcode'], 'zone' => $order_query->row['payment_zone'], 'zone_code' => $zone_code, 'country' => $order_query->row['payment_country']);
         $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))));
         $template->data['products'] = array();
         foreach ($order_product_query->rows as $product) {
             $option_data = array();
             $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'] . "'");
             foreach ($order_option_query->rows as $option) {
                 $option_data[] = array('name' => $option['name'], 'value' => $option['value']);
             }
             $template->data['products'][] = array('name' => $product['name'], 'model' => $product['model'], 'option' => $option_data, 'quantity' => $product['quantity'], 'price' => $this->currency->format($product['price'], $order_query->row['currency'], $order_query->row['value']), 'total' => $this->currency->format($product['total'], $order_query->row['currency'], $order_query->row['value']));
         }
         $template->data['totals'] = $order_total_query->rows;
         if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order_confirm.tpl')) {
             $html = $template->fetch($this->config->get('config_template') . '/template/mail/order_confirm.tpl');
         } else {
             $html = $template->fetch('default/template/mail/order_confirm.tpl');
         }
         // Text Mail
         $text = sprintf($language->get('text_greeting'), html_entity_decode($order_query->row['store_name'], ENT_QUOTES, 'UTF-8')) . "\n\n";
         $text .= $language->get('text_order_id') . ' ' . $order_id . "\n";
         $text .= $language->get('text_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_query->row['date_added'])) . "\n";
         $text .= $language->get('text_order_status') . ' ' . $order_status_query->row['name'] . "\n\n";
         $text .= $language->get('text_product') . "\n";
         foreach ($order_product_query->rows as $result) {
             $text .= $result['quantity'] . 'x ' . $result['name'] . ' (' . $result['model'] . ') ' . html_entity_decode($this->currency->format($result['total'], $order_query->row['currency'], $order_query->row['value']), ENT_NOQUOTES, 'UTF-8') . "\n";
             $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'] . "'");
             foreach ($order_option_query->rows as $option) {
                 $text .= chr(9) . '-' . $option['name'] . ' ' . $option['value'] . "\n";
             }
         }
         $text .= "\n";
         $text .= $language->get('text_total') . "\n";
         foreach ($order_total_query->rows as $result) {
             $text .= $result['title'] . ' ' . html_entity_decode($result['text'], ENT_NOQUOTES, 'UTF-8') . "\n";
         }
         $order_total = $result['text'];
         $text .= "\n";
         if ($order_query->row['customer_id']) {
             $text .= $language->get('text_invoice') . "\n";
             $text .= $order_query->row['store_url'] . 'index.php?route=account/invoice&order_id=' . $order_id . "\n\n";
         }
         if ($order_download_query->num_rows) {
             $text .= $language->get('text_download') . "\n";
             $text .= $order_query->row['store_url'] . 'index.php?route=account/download' . "\n\n";
         }
         if ($order_query->row['comment'] != '') {
             $comment = $order_query->row['comment'] . "\n\n" . $comment;
         }
         if ($comment) {
             $text .= $language->get('text_comment') . "\n\n";
             $text .= $comment . "\n\n";
         }
         $text .= $language->get('text_footer');
         $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($order_query->row['email']);
         $mail->setFrom($this->config->get('config_email'));
         $mail->setSender($order_query->row['store_name']);
         $mail->setSubject($subject);
         $mail->setHtml($html);
         $mail->setText(html_entity_decode($text, ENT_QUOTES, 'UTF-8'));
         $mail->addAttachment(DIR_IMAGE . $this->config->get('config_logo'));
         $mail->send();
         if ($this->config->get('config_alert_mail')) {
             // HTML
             $template->data['text_greeting'] = $language->get('text_received') . "\n\n";
             $template->data['invoice'] = '';
             $template->data['text_invoice'] = '';
             if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order_confirm.tpl')) {
                 $html = $template->fetch($this->config->get('config_template') . '/template/mail/order_confirm.tpl');
             } else {
                 $html = $template->fetch('default/template/mail/order_confirm.tpl');
             }
             $subject = sprintf($language->get('text_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'), $order_id . ' (' . $order_total . ')');
             $mail->setSubject($subject);
             $mail->setTo($this->config->get('config_email'));
             $mail->setHtml($html);
             $mail->send();
             // Send to additional alert emails
             $pattern = '/^[A-Z0-9._%-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\\.[A-Z]{2,6}$/i';
             $emails = explode(',', $this->config->get('config_alert_emails'));
             foreach ($emails as $email) {
                 if (strlen($email) > 0 && preg_match($pattern, $email)) {
                     $mail->setTo($email);
                     $mail->send();
                 }
             }
             /*
             // Text
             $text  = $language->get('text_received') . "\n\n";
             $text .= $language->get('text_order_id') . ' ' . $order_id . "\n";
             $text .= $language->get('text_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_query->row['date_added'])) . "\n";
             $text .= $language->get('text_order_status') . ' ' . $order_status_query->row['name'] . "\n\n";
             $text .= $language->get('text_product') . "\n";
             
             foreach ($order_product_query->rows as $result) {
             	$text .= $result['quantity'] . 'x ' . $result['name'] . ' (' . $result['model'] . ') ' . html_entity_decode($this->currency->format($result['total'], $order_query->row['currency'], $order_query->row['value']), ENT_NOQUOTES, 'UTF-8') . "\n";
             	$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'] . "'");
             	foreach ($order_option_query->rows as $option) {
             		$text .= chr(9) . '-' . $option['name'] . ' ' . $option['value'] . "\n";
             	}
             }
             
             $text .= "\n";
             
             $text.= $language->get('text_total') . "\n";
             
             foreach ($order_total_query->rows as $result) {
             	$text .= $result['title'] . ' ' . html_entity_decode($result['text'], ENT_NOQUOTES, 'UTF-8') . "\n";
             }
             
             $text .= "\n";
             
             if ($order_query->row['comment'] != '') {
             	$comment = ($order_query->row['comment'] .  "\n\n" . $comment);
             }
             
             if ($comment) {
             	$text .= $language->get('text_comment') . "\n\n";
             	$text .= $comment . "\n\n";
             }
             
             $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($order_query->row['store_name']);
             $mail->setSubject($subject);
             $mail->setText($text);
             $mail->send();
             */
         }
         if ($this->config->get('config_alert_sms')) {
             $message = str_replace(array('{ID}', '{DATE}', '{TIME}', '{SUM}'), array($order_id, date('d.m.Y'), date('H:i'), floatval($order_query->row['total'])), $this->config->get('config_sms_message'));
             $sms = new Sms($this->config->get('config_sms_gatename'));
             $sms->setTo($this->config->get('config_sms_admin_phone'));
             $sms->setText($message);
             $sms->setUsername($this->config->get('config_sms_gate_username'));
             $sms->setPassword($this->config->get('config_sms_gate_password'));
             $sms->setFrom($this->config->get('config_sms_from'));
             $sms->send();
         }
     }
 }