File: //proc/self/cwd/nueva/modules/roja45quotationsprofree/controllers/front/QuotationsProFront.php
<?php
/**
* roja45quotationsprofreeQuotationsProFrontModuleFrontController.
*
* @author Roja45
* @copyright 2016 Roja45
* @license license.txt
* @category roja45quotationsprofreeQuotationsProFrontModuleFrontController
*
*
* 2016 ROJA45 - All rights reserved.
*
* DISCLAIMER
* Changing this file will render any support provided by us null and void.
*/
/**
* roja45quotationsprofreeQuotationsProFrontModuleFrontController.
*
*
* @author Roja45
* @copyright 2016 Roja45
* @license license.txt
* @category Class
*
*
* 2016 ROJA45 - All rights reserved.
*
* DISCLAIMER
* Changing this file will render any support provided by us null and void.
*/
class roja45quotationsprofreeQuotationsProFrontModuleFrontController extends ModuleFrontController
{
public function __construct()
{
parent::__construct();
$this->context = Context::getContext();
}
public function setMedia()
{
parent::setMedia();
RojaFortyFiveQuotationsProCore::addJs(
$this,
_PS_MODULE_DIR_ . $this->module->name . '/views/js/roja45quotationspro_summary'
);
RojaFortyFiveQuotationsProCore::addJs(
$this, _PS_MODULE_DIR_ . $this->module->name . '/views/js/validate.js'
);
}
public function postProcess()
{
$action = Tools::toCamelCase(Tools::getValue('action'), true);
if (!$this->ajax && !empty($action) && method_exists($this, 'process'.$action)) {
$this->{'process'.$action}();
}
parent::postProcess();
}
public function processAddToQuote()
{
$validationErrors = array();
$id_product = (int) Tools::getValue('id_product');
if (!isset($id_product) && !$id_product || ($id_product=="undefined")) {
$validationErrors[] = $this->module->l('Product Id Required');
}
$id_product_attribute = trim(Tools::getValue('id_product_attribute'));
if (empty($id_product_attribute) || !$id_product_attribute) {
$id_product_attribute = 0;
}
$quantity = (int) trim(Tools::getValue('quantity'));
if (!$quantity) {
$quantity = (int) trim(Tools::getValue('minimal_quantity'));
}
if (!count($validationErrors)) {
$form_config = $this->module->getForm();
$form = $this->module->buildFormComponents($form_config);
$this->context->smarty->assign(array(
'product' => $this->getRequestedProduct($id_product, $id_product_attribute),
'nbTotalProducts' => 1,
'request_qty' => $quantity,
'id_language' => $this->context->language->id,
'form' => $form,
'columns' => $form_config['cols'],
'col_width' => 12 / $form_config['cols'],
'token_cart' => Tools::getToken(false),
'isLogged' => $this->context->customer->isLogged(),
'numberProducts' => 1,
'smallSize' => Image::getSize(ImageType::getFormatedName('small')),
'errors' => array()
));
RojaFortyFiveQuotationsProCore::setFrontControllerTemplate($this, 'request-summary.tpl');
}
}
public function processGetCustomerQuoteHistory()
{
if (!Context::getContext()->customer->isLogged()) {
Tools::redirect('index.php?controller=authentication&redirect=module&module=mailalerts&action=account');
}
if (Context::getContext()->customer->id) {
$ids = RojaQuotation::getQuotationsForCustomer($this->context->customer->id);
$historicalquotes = array();
foreach ($ids as $id) {
$quotation = new RojaQuotation($id['id_roja45_quotation']);
if ($quotation->id_order > 0) {
$quotation->total = $quotation->getQuotationTotal((int)$quotation->calculate_taxes);
$quotation->total_formatted = Tools::displayPrice(Tools::convertPrice($quotation->total));
$historicalquotes[] = $quotation;
}
}
$addresses = $this->context->customer->getAddresses($this->context->language->id);
$this->context->smarty->assign(array(
'num_address' => count($addresses),
'quotes' => $historicalquotes,
));
Media::addJsDef(array(
'roja45_quotationspro_controller' => $this->context->link->getModuleLink(
'roja45quotationsprofree',
'QuotationsProFront',
array()
),
));
RojaFortyFiveQuotationsProCore::setFrontControllerTemplate($this, 'customer-quote-history.tpl');
}
}
public function processQuoteSummary()
{
$validationErrors = array();
if (!count($validationErrors)) {
$summary = QuotationRequest::getInstance()->getSummaryDetails();
$form_config = $this->module->getForm();
$form = $this->module->buildFormComponents($form_config);
$this->id_country = (int)Tools::getCountry();
$this->context->smarty->assign($summary);
$lastProductAdded = null;
$this->context->smarty->assign(array(
'id_language' => $this->context->language->id,
'home_url' => $this->context->link->getPageLink('index', true, null),
'form' => $form,
'columns' => $form_config['cols'],
'roja45quotationspro_enable_captcha' => Configuration::get('ROJA45_QUOTATIONSPRO_ENABLE_CAPTCHA'),
'roja45quotationspro_enable_invisiblecaptcha' => Configuration::get(
'ROJA45_QUOTATIONSPRO_ENABLE_INVISIBLECAPTCHA'
),
'in_cart' => true,
'lastProductAdded' => $lastProductAdded,
'col_width' => 12 / $form_config['cols'],
'sl_country' => (int)$this->id_country,
'roja45quotationspro_recaptcha_site_key' => Configuration::get(
'ROJA45_QUOTATIONSPRO_RECAPTCHA_SITE'
),
'token_cart' => Tools::getToken(false),
'isLogged' => $this->context->customer->isLogged(),
'numberProducts' => count($summary['products']),
'smallSize' => Image::getSize(ImageType::getFormatedName('small')),
'cannotModify' => 0,
'displayQuantity' => 1,
'empty' => (count($summary['products'])==0),
'errors' => array()
));
RojaFortyFiveQuotationsProCore::setFrontControllerTemplate($this, 'request-received.tpl');
} else {
}
}
public function processSubmitRequest()
{
$validationErrors = array();
try {
if (!Tools::strlen($email = trim(Tools::getValue('ROJA45QUOTATIONSPRO_EMAIL'))) > 0) {
$validationErrors[] = $this->module->l('Email Address Required');
}
if (!Tools::strlen($firstname = trim(Tools::getValue('ROJA45QUOTATIONSPRO_FIRSTNAME'))) > 0) {
$validationErrors[] = $this->module->l('First Name Required');
}
if (!Tools::strlen($lastname = trim(Tools::getValue('ROJA45QUOTATIONSPRO_LASTNAME'))) > 0) {
$validationErrors[] = $this->module->l('Last Name Required');
}
if (!Tools::strlen($id_product = trim(Tools::getValue('id_product'))) > 0) {
$validationErrors[] = $this->module->l('Product id Required');
}
if (!Tools::strlen($id_product_attribute= trim(Tools::getValue('id_product_attribute'))) > 0) {
$validationErrors[] = $this->module->l('Product id Required');
}
if (!Tools::strlen($id_product_attribute= trim(Tools::getValue('id_product_attribute'))) > 0) {
$validationErrors[] = $this->module->l('Product id Required');
}
if (!count($validationErrors)) {
$request = Tools::getValue('ROJA45QUOTATIONSPRO_FORMDATA');
//$quotation_request = QuotationRequest::getInstance();
//$quote_products = $quotation_request->getProducts();
$products = array();
$product = array();
$combination = null;
if (isset($id_product_attribute) && $id_product_attribute) {
$combination = new Combination($id_product_attribute);
if (!Validate::isLoadedObject($combination)) {
die(Tools::jsonEncode(array(
'result' => false,
'error' => Tools::displayError('The combination object cannot be loaded.'),
)));
}
}
$product['product'] = new Product(
$id_product,
false,
$this->context->language->id
);
$product['qty'] = Tools::getValue('quote_quantity');
$product['combination'] = $combination;
$products[] = $product;
if ($this->createQuotation($email, $firstname, $lastname, $request, $products)) {
$this->context->smarty->assign(array(
'home_url' => $this->context->link->getPageLink('index', true, null),
));
RojaFortyFiveQuotationsProCore::setFrontControllerTemplate(
$this,
'request-received.tpl'
);
return;
}
$validationErrors[] = $this->module->l('Unable to create quotation.');
}
$this->errors = $validationErrors;
} catch (Exception $e) {
$validationErrors = array();
$validationErrors[] = $e->getMessage();
$this->errors = $validationErrors;
}
$this->processQuoteSummary();
}
private function createQuotation($email, $firstname, $lastname, $request, $products)
{
$product = $products[0];
$mysql_date_now = date('Y-m-d H:i:s');
if (!$id_country = Tools::getValue('country')) {
$id_country = (int) $this->context->country->id;
}
$quotation = new RojaQuotation();
$quotation->id_lang = (int) $this->context->language->id;
$quotation->id_shop = (int) $this->context->shop->id;
$quotation->id_currency = (int) $this->context->currency->id;
$quotation->id_country = $id_country;
$quotation->received = $mysql_date_now;
$quotation->last_update = $mysql_date_now;
$quotation->email = $email;
$quotation->valid_days = (int) Configuration::get('ROJA45_QUOTATIONSPRO_QUOTE_VALID_DAYS');
$quotation->firstname = $firstname;
$quotation->lastname = $lastname;
$quotation->form_data = $request;
$quotation->reference = Tools::strtoupper(Tools::passwdGen(9, 'NO_NUMERIC'));
if (Group::getDefaultPriceDisplayMethod()==0) {
$quotation->calculate_taxes = 1;
} else {
$quotation->calculate_taxes = 0;
}
if (!$this->context->customer->id) {
$customer = new Customer();
$customer = $customer->getByEmail($email);
if (Validate::isLoadedObject($customer)) {
$quotation->id_customer = $customer->id;
}
} else {
$quotation->id_customer = $this->context->customer->id;
}
if (!$quotation->save()) {
return false;
}
$result = true;
if (!$quotation->addProduct($product['product'], $product['combination'], $product['qty'])) {
$result = false;
};
if (!$result) {
$quotation->delete();
return false;
}
$contact = new Contact(
Configuration::get('ROJA45_QUOTATIONSPRO_CS_ACCOUNT'),
$this->context->language->id
);
if (Validate::isLoadedObject($contact)) {
$contact_name = $contact->name;
$contact_email = $contact->email;
} else {
$contact_name = Configuration::get('ROJA45_QUOTATIONSPRO_CONTACT_NAME');
$contact_email = Configuration::get('ROJA45_QUOTATIONSPRO_EMAIL');
}
$quotation->setStatus(QuotationStatus::$RCVD);
$customer_copy = Tools::getValue('ROJA45QUOTATIONSPRO_CUSTOMER_COPY');
$requestJSON = Tools::jsonDecode($request);
$to_list = array(Tools::getValue('ROJA45QUOTATIONSPRO_EMAIL'));
$to_names = array($firstname . ' ' . $lastname);
if (!$bcc = Configuration::get('ROJA45_QUOTATIONSPRO_CONTACT_BCC')) {
$bcc = null;
}
$contact = new Contact(
Configuration::get('ROJA45_QUOTATIONSPRO_CS_ACCOUNT'),
$this->context->language->id
);
$products = $quotation->getProducts();
$product = reset($products);
$tpl = $this->context->smarty->createTemplate(
_PS_MODULE_DIR_.'roja45quotationsprofree/views/templates/front/email-request-content.tpl'
);
$tpl->assign(
array(
'columns' => $requestJSON->columns,
'product' => $product,
'request_qty' => $product['request_qty'],
)
);
$content = $tpl->fetch();
$content_text = new Html2Text($content);
$content_text = $content_text->getText();
$params = array(
'{id_product}' => Tools::getValue('ID_PRODUCT'),
'{product_name}' => $product['name'],
'{customer_firstname}' => $firstname,
'{customer_lastname}' => $lastname,
'{content}' => ($customer_copy) ? $content : '',
'{content_text}' => ($customer_copy) ? $content_text : '',
);
$sent = Mail::Send(
(int) $this->context->language->id,
'roja45quotationrequest',
sprintf(
Mail::l('Quotation Request [%s]', (int) $this->context->language->id),
Tools::strtoupper($product['name'])
),
$params,
$to_list,
$to_names,
$contact_email,
$contact_name,
null,
null,
_PS_MODULE_DIR_.'roja45quotationsprofree/mails/',
false,
null,
$bcc,
$contact->email
);
if (!$sent) {
throw new Exception($this->module->l(
'Quotation request sent, but there was a problem sending a copy to your email address. We will be in contact soon.'
));
}
$params = array(
'{customer_firstname}' => $firstname,
'{customer_lastname}' => $lastname,
'{content}' => $content,
'{content_text}' => $content_text,
);
$sent = Mail::Send(
(int) $this->context->language->id,
'roja45quotationrequestadmin',
Mail::l('Quotation Request Received', (int) $this->context->language->id),
$params,
$contact_email,
$contact_name,
$contact_email,
$contact_name,
null,
null,
_PS_MODULE_DIR_.'roja45quotationsprofree/mails/',
false,
null,
$bcc,
Tools::getValue('ROJA45QUOTATIONSPRO_EMAIL')
);
if (!$sent) {
throw new Exception('Unable to send admin email.');
}
return $result;
}
private function getRequestedProduct($id_product, $id_product_attribute)
{
$sql = new DbQuery();
$sql->select(
'p.`id_product`, pl.`name`, p.`is_virtual`, pl.`description_short`, pl.`available_now`,
pl.`available_later`, product_shop.`id_category_default`, p.`id_supplier`, p.`id_manufacturer`,
product_shop.`id_shop`, product_shop.`on_sale`, product_shop.`ecotax`,
product_shop.`additional_shipping_cost`,product_shop.`available_for_order`, product_shop.`price`,
product_shop.`active`, product_shop.`unity`, product_shop.`unit_price_ratio`, p.`width`, p.`height`,
p.`depth`, p.`weight`, p.`date_add`, p.`date_upd`, pl.`link_rewrite`,cl.`link_rewrite` AS category'
);
$sql->from('product', 'p');
$sql->innerJoin(
'product_shop',
'product_shop',
'(product_shop.`id_product` = p.`id_product`)'
);
$sql->leftJoin(
'product_lang',
'pl',
'p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang(
'pl',
'product_shop.id_shop'
)
);
$sql->leftJoin(
'category_lang',
'cl',
'product_shop.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang(
'cl',
'product_shop.id_shop'
)
);
if (Combination::isFeatureActive() && $id_product_attribute) {
$sql->select(
'pa.`id_product_attribute`,product_attribute_shop.`price` AS price_attribute,
product_attribute_shop.`ecotax` AS ecotax_attr,
IF (IFNULL(pa.`reference`, \'\') = \'\', p.`reference`, pa.`reference`) AS reference,
(p.`weight`+ pa.`weight`) weight_attribute,
IF (IFNULL(pa.`ean13`, \'\') = \'\', p.`ean13`, pa.`ean13`) AS ean13,
IF (IFNULL(pa.`upc`, \'\') = \'\', p.`upc`, pa.`upc`) AS upc,
IFNULL(product_attribute_shop.`minimal_quantity`, product_shop.`minimal_quantity`) as minimal_quantity,
IF(product_attribute_shop.wholesale_price > 0, product_attribute_shop.wholesale_price,
product_shop.`wholesale_price`) wholesale_price'
);
$sql->leftJoin('product_attribute', 'pa', 'pa.`id_product` = p.id_product');
$sql->leftJoin(
'product_attribute_shop',
'product_attribute_shop',
'(product_attribute_shop.`id_shop` = product_shop.`id_shop` AND
product_attribute_shop.`id_product_attribute` = pa.`id_product_attribute`)'
);
$sql->where('pa.`id_product_attribute` = '.(int)$id_product_attribute);
} else {
$sql->select(
'p.`reference` AS reference, p.`ean13`,
p.`upc` AS upc, product_shop.`minimal_quantity` AS minimal_quantity,
product_shop.`wholesale_price` wholesale_price'
);
}
$sql->where('p.`id_product` = '.(int)$id_product);
$sql->orderBy('p.`id_product` ASC');
$sql->select('image_shop.`id_image` id_image, il.`legend`');
$sql->leftJoin(
'image_shop',
'image_shop',
'image_shop.`id_product` = p.`id_product`
AND image_shop.cover=1
AND image_shop.id_shop='.(int)$this->context->shop->id);
$sql->leftJoin(
'image_lang',
'il',
'il.`id_image` = image_shop.`id_image` AND il.`id_lang` = '.(int)$this->context->language->id);
$result = Db::getInstance()->executeS($sql);
if (count($result)) {
$result[0]['link'] = Context::getContext()->link->getProductLink(
$result[0]['id_product'],
$result[0]['link_rewrite'],
$result[0]['category']
);
if (!isset($result[0]['id_product_attribute'])) {
$result[0]['id_product_attribute'] = 0;
}
$result[0]['image_url'] = $this->context->link->getImageLink(
$result[0]['link_rewrite'],
$result[0]['id_image'],
'small_default'
);
return $result[0];
} else {
return false;
}
}
}