diff --git a/web/override/classes/order/OrderHistory.php b/web/override/classes/order/OrderHistory.php index e3f88980c..2838a88ea 100644 --- a/web/override/classes/order/OrderHistory.php +++ b/web/override/classes/order/OrderHistory.php @@ -1,5 +1,101 @@ array()), $line_sep, ' ', $fields_style); + } + + public function sendEmail($order, $template_vars = false) + { + $carrier = new Carrier((int) $order->id_carrier, $order->id_lang); + $delivery = new Address((int)$order->id_address_delivery); + + $result = Db::getInstance()->getRow(' + SELECT osl.`template`, c.`lastname`, c.`firstname`, osl.`name` AS osname, c.`email`, os.`module_name`, os.`id_order_state`, os.`pdf_invoice`, os.`pdf_delivery` + FROM `' . _DB_PREFIX_ . 'order_history` oh + LEFT JOIN `' . _DB_PREFIX_ . 'orders` o ON oh.`id_order` = o.`id_order` + LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON o.`id_customer` = c.`id_customer` + LEFT JOIN `' . _DB_PREFIX_ . 'order_state` os ON oh.`id_order_state` = os.`id_order_state` + LEFT JOIN `' . _DB_PREFIX_ . 'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = o.`id_lang`) + WHERE oh.`id_order_history` = ' . (int) $this->id . ' AND os.`send_email` = 1'); + if (isset($result['template']) && Validate::isEmail($result['email'])) { + ShopUrl::cacheMainDomainForShop($order->id_shop); + + $topic = $result['osname']; + $data = array( + '{lastname}' => $result['lastname'], + '{firstname}' => $result['firstname'], + '{id_order}' => (int) $this->id_order, + '{order_name}' => $order->getUniqReference(), + '{followup}' => str_replace('@', $this->tracking_number, $carrier->url), + '{delivery_block_html}' => $this->_getFormatedAddress($delivery, '
', array( + 'firstname' => '%s', + 'lastname' => '%s' + )), + ); + + if ($result['module_name']) { + $module = Module::getInstanceByName($result['module_name']); + if (Validate::isLoadedObject($module) && isset($module->extra_mail_vars) && is_array($module->extra_mail_vars)) { + $data = array_merge($data, $module->extra_mail_vars); + } + } + + if (is_array($template_vars)) { + $data = array_merge($data, $template_vars); + } + + $data['{total_paid}'] = Tools::displayPrice((float) $order->total_paid, new Currency((int) $order->id_currency), false); + + if (Validate::isLoadedObject($order)) { + // Attach invoice and / or delivery-slip if they exists and status is set to attach them + if (($result['pdf_invoice'] || $result['pdf_delivery'])) { + $context = Context::getContext(); + $invoice = $order->getInvoicesCollection(); + $file_attachement = array(); + + if ($result['pdf_invoice'] && (int) Configuration::get('PS_INVOICE') && $order->invoice_number) { + Hook::exec('actionPDFInvoiceRender', array('order_invoice_list' => $invoice)); + $pdf = new PDF($invoice, PDF::TEMPLATE_INVOICE, $context->smarty); + $file_attachement['invoice']['content'] = $pdf->render(false); + $file_attachement['invoice']['name'] = Configuration::get('PS_INVOICE_PREFIX', (int) $order->id_lang, null, $order->id_shop) . sprintf('%06d', $order->invoice_number) . '.pdf'; + $file_attachement['invoice']['mime'] = 'application/pdf'; + } + if ($result['pdf_delivery'] && $order->delivery_number) { + $pdf = new PDF($invoice, PDF::TEMPLATE_DELIVERY_SLIP, $context->smarty); + $file_attachement['delivery']['content'] = $pdf->render(false); + $file_attachement['delivery']['name'] = Configuration::get('PS_DELIVERY_PREFIX', Context::getContext()->language->id, null, $order->id_shop) . sprintf('%06d', $order->delivery_number) . '.pdf'; + $file_attachement['delivery']['mime'] = 'application/pdf'; + } + } else { + $file_attachement = null; + } + + if (!Mail::Send( + (int) $order->id_lang, + $result['template'], + $topic, + $data, + $result['email'], + $result['firstname'] . ' ' . $result['lastname'], + null, + null, + $file_attachement, + null, + _PS_MAIL_DIR_, + false, + (int) $order->id_shop + )) { + return false; + } + } + + ShopUrl::resetMainDomainCache(); + } + + return true; + } }