/
home
/
sharedstore
/
.trash
/
productquotation
/
controllers
/
front
/
/home/sharedstore/.trash/productquotation/controllers/front
mkdir
upload
Name
Size
Mode
Actions
quote.php
26562
0644
edit
dl
rm
Edit:
/home/sharedstore/.trash/productquotation/controllers/front/quote.php
(26562B)
<?php /** * modules/productquotation/controllers/front/quote.php */ if (!defined('_PS_VERSION_')) { exit; } class ProductQuotationQuoteModuleFrontController extends ModuleFrontController { public $ssl = true; public function __construct() { parent::__construct(); $this->context = Context::getContext(); } public function _initContent() { parent::initContent(); $obj = new Quote(); $tax_status = (int) Configuration::get('PQUOTE_TAX'); $shop_ids = Shop::getContextListShopID(); $id_lang = (int) $this->context->language->id; $id_quote = $this->context->cookie->id_quote; $products = $obj->getQuoteProducts($id_quote, $tax_status); $products_total = $obj->getQuoteProductsTotal($id_quote, $tax_status); $form = $obj->getForm($id_lang, $shop_ids); $form_action = $this->context->link->getModuleLink('productquotation', 'quote'); $success = (int) Tools::getValue('success'); $id_module = $this->module->id; $force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE')); $email = Context::getContext()->cookie->email; $coo = new Cookie('psFront'); $id_employee = (int) $coo->__get('id_employee'); $token = Tools::getAdminToken('Ajax' . Tab::getIdFromClassName('Ajax') . $id_employee); $this->context->smarty->assign([ 'id_quote' => (int) $id_quote, 'products' => $products, 'total' => $products_total, 'path_uri' => $this->module->getPathUri(), 'forms' => $form, 'form_action' => $form_action, 'success' => $success, 'id_module' => $id_module, 'id_language' => $id_lang, 'base_dir' => _PS_BASE_URL_ . __PS_BASE_URI__, 'base_dir_ssl' => _PS_BASE_URL_SSL_ . __PS_BASE_URI__, 'force_ssl' => $force_ssl, 'token' => $token, 'email' => $email, ]); if (Tools::version_compare(_PS_VERSION_, '1.7.0.0', '>=') == true) { return $this->setTemplate('module:productquotation/views/templates/front/quote-17.tpl'); } else { $this->setTemplate('quote.tpl'); } } public function initContent() { parent::initContent(); $id_lang = $this->context->language->id; $objModel = new FieldsQuote(); $quote_fields = $objModel->getCustomFields($id_lang); $id_customer = (isset($this->context->customer->id)) ? $this->context->customer->id : $this->context->cookie->id_customer; $id_guest = (!$id_customer) ? $this->context->cookie->id_guest : 0; $field = new FieldsQuote(); $fields = []; if ($id_customer || $id_guest) { $fields = $field->getAllFields( $id_customer ? 'val.id_customer = ' . (int) $id_customer : 'val.id_guest = ' . (int) $id_guest, $id_lang, 'a.position' ); if (isset($fields) && $fields) { foreach ($fields as &$field) { if (in_array($field['field_type'], ['multiselect', 'radio', 'checkbox']) && $field['field_value_id']) { $field['field_value'] = explode(',', $field['field_value_id']); } elseif (in_array($field['field_type'], ['message', 'select']) && $field['field_value_id']) { $field['field_value'] = $field['field_value_id']; } } } } $quote_fields_options = []; foreach ($quote_fields as $sf) { $quote_fields_options[$sf['id_fmm_quote_fields']] = $objModel->getCustomFieldsValues($sf['id_fmm_quote_fields']); } if (isset($quote_fields) && !empty($quote_fields)) { foreach ($quote_fields as &$field) { if (isset($field['id_heading']) && (int) $field['id_heading'] > 0) { $field['sub_heading'] = $objModel->getSubHeading($field['id_heading'], $id_lang); } else { $field['sub_heading'] = ''; } } } $obj = new Quote(); $tax_status = (int) Configuration::get('PQUOTE_TAX'); $shop_ids = Shop::getContextListShopID(); $id_lang = (int) $this->context->language->id; $id_quote = $this->context->cookie->id_quote; $products = $obj->getQuoteProducts($id_quote, $tax_status); $products_total = $obj->getQuoteProductsTotal($id_quote, $tax_status); $form = $obj->getForm($id_lang, $shop_ids); $form_action = $this->context->link->getModuleLink('productquotation', 'quote'); $success = (int) Tools::getValue('success'); $id_module = $this->module->id; $force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE')); $email = Context::getContext()->cookie->email; $coo = new Cookie('psFront'); $id_employee = (int) $coo->__get('id_employee'); $token = Tools::getAdminToken('Ajax' . Tab::getIdFromClassName('Ajax') . $id_employee); $this->context->smarty->assign([ 'id_quote' => (int) $id_quote, 'products' => $products, 'total' => $products_total, 'path_uri' => $this->module->getPathUri(), 'forms' => $form, 'form_action' => $form_action, 'success' => $success, 'id_module' => $id_module, 'id_language' => $id_lang, 'base_dir' => _PS_BASE_URL_ . __PS_BASE_URI__, 'base_dir_ssl' => _PS_BASE_URL_SSL_ . __PS_BASE_URI__, 'force_ssl' => $force_ssl, 'token' => $token, 'email' => $email, ]); if ($quote_fields_options) { $this->context->smarty->assign('version', _PS_VERSION_); $this->context->smarty->assign('id_guest', $id_guest); $this->context->smarty->assign('id_customer', $id_customer); $this->context->smarty->assign('id_lang', $this->context->cookie->id_lang); $this->context->smarty->assign('summary_fields_values', $quote_fields_options); $this->context->smarty->assign('summary_fields', $quote_fields); $this->context->smarty->assign('value_reg_fields', $fields); $this->context->smarty->assign('is_psgdpr', Module::isInstalled('psgdpr') && Module::isEnabled('psgdpr')); $this->context->smarty->assign('quote_FIELDS_HEADING', Configuration::get('quote_FIELDS_HEADING', $this->context->language->id, $this->context->shop->id_shop_group, $this->context->shop->id)); $this->context->smarty->assign('actionLink', $this->context->link->getModuleLink('productquotation', 'ajax', ['action' => 'download', 'me' => base64_encode($id_customer)], true)); $this->context->smarty->assign('PQUOTE_CAPTCHA_ENABLE', Configuration::get('PQUOTE_CAPTCHA_ENABLE')); $this->context->smarty->assign('PQUOTE_CAPTCHA_V3', Configuration::get('PQUOTE_CAPTCHA_V3')); $this->context->smarty->assign('PQUOTE_GOOGLE_V3_CAPTCHA_SITE_KEY', Configuration::get('PQUOTE_GOOGLE_V3_CAPTCHA_SITE_KEY')); $this->context->smarty->assign('PQUOTE_GOOGLE_V3_CAPTCHA_SECRET_KEY', Configuration::get('PQUOTE_GOOGLE_V3_CAPTCHA_SECRET_KEY')); $ps_17 = (Tools::version_compare(_PS_VERSION_, '1.7.0.0', '>=') == true) ? 1 : 0; $this->context->smarty->assign('ps_17', (int) $ps_17); if ($ps_17 > 0) { $this->context->smarty->assign('base_dir', _PS_BASE_URL_ . __PS_BASE_URI__); } } if (Tools::version_compare(_PS_VERSION_, '1.7.0.0', '>=') == true) { return $this->setTemplate('module:productquotation/views/templates/front/newquote-17.tpl'); } else { $this->setTemplate('newquote-16.tpl'); } } public function postProcess() { $obj = new Quote(); $id_quote = $this->context->cookie->id_quote; $new_link = $this->context->link->getModuleLink('productquotation', 'quote') . '?success=1'; $as_quote = (int) Tools::getValue('asquote'); $id_lang = (int) $this->context->language->id; $id_currency = (int) $this->context->currency->id; $id_customer = $this->context->customer->id; $objModel = new FieldsQuote(); $id_guest = FieldsQuote::getGuestId($id_customer); $result = $objModel->saveFieldValues(Tools::getValue('fields'), $id_customer, $id_quote); if (Tools::isSubmit('submitQuote')) { if (Configuration::get('PQUOTE_CAPTCHA_ENABLE') == 1) { $response = $this->pQouteCaptchaValidation(); if (!empty($response)) { foreach ($response as $error) { $result['error'] = $error; } } } if (isset($result) && $result) { if ($result['result'] == false && isset($result['errors'])) { foreach ($result['errors'] as $error) { $this->context->controller->errors[] = $error; return true; } $this->context->controller->errors; } } $email = Tools::getValue('email'); $template = Tools::getValue('quote_template'); $message = Tools::getValue('pq_message'); $fullname = Tools::getValue('fullname'); $company = Tools::getValue('company'); $phone = Tools::getValue('phone'); $subject = Tools::getValue('subject'); $customer_id = (int) $this->context->customer->id; $key_code = md5($email); $obj->saveQuote( $id_quote, $email, $message, $fullname, $company, $phone, $subject, $template, $customer_id, $id_currency, $key_code ); foreach ($_POST as $key => $val) { if (!in_array($key, ['quote_template', 'submitQuote', 'email', 'fields', 'errors']) && $key != 'g-recaptcha-response') { $obj->saveQuoteData((int) $id_quote, $key, $val); } } $this->context->cookie->id_quote = 0; $send_mail_admin = (int) Configuration::get('PQUOTE_SENDMAIL_ADMIN'); $id_quotation = (int) $obj->getQoutationIdByQuote($id_quote); if ($send_mail_admin > 0) { $this->sendMailToAdmin($id_quote, $id_quotation, $email, $message, $fullname, $company, $phone, $subject); } $this->sendMailToUser($id_quotation, $email, $message, $fullname, $company, $phone, $subject, $key_code); Tools::redirect($new_link); } elseif ($as_quote > 0) { $quote_link = $this->context->link->getModuleLink('productquotation', 'quote'); $id_cart = (int) $this->context->cart->id; if ($id_cart <= 0) { $this->errors[] = $this->module->l('Cart is empty, no products to add.'); } else { $cart = new Cart($id_cart); $cart_products = $cart->getProducts(); if (empty($cart_products)) { $this->errors[] = $this->module->l('Cart is empty, no products to add.'); } else { $id_quote = (int) $this->context->cookie->id_quote; if (!$id_quote) { $id = $obj->insertNewQuote($id_lang); foreach ($cart_products as $row) { $price_tax_excl = $product['price']; $price_tax_incl = $product['price_wt']; $tax_price = $price_tax_incl - $price_tax_excl; $obj->saveNewQuoteData( $id, $row['id_product'], $row['id_product_attribute'], $row['cart_quantity'], $tax_price ); } $this->context->cookie->id_quote = (int) $id; Tools::redirect($quote_link); } elseif ($id_quote > 0) { $id = (int) $this->context->cookie->id_quote; foreach ($cart_products as $row) { $pre_exist = (int) $obj->checkPreExistance( $id, $row['id_product'], $row['id_product_attribute'] ); if ($pre_exist <= 0) { $price_tax_excl = $product['price']; $price_tax_incl = $product['price_wt']; $tax_price = $price_tax_incl - $price_tax_excl; $obj->saveNewQuoteData( $id, $row['id_product'], $row['id_product_attribute'], $row['cart_quantity'], $tax_price ); } } Tools::redirect($quote_link); } } } } elseif (Tools::isSubmit('action') == 'updatePrice&quantity') { $productId = (int) Tools::getValue('product_id'); $price = (float) Tools::getValue('price'); $quantity = (int) Tools::getValue('quantity'); $product_attribute_id = (int) Tools::getValue('product_attribute_id'); $this->updateQuoteProduct($productId, $product_attribute_id, $price, $quantity); } } private function sendMailToAdmin($id, $id_quotation, $email, $message, $fullname, $company, $phone, $subject) { $admin_emails = Configuration::get('PQUOTE_ADMIN_EMAIL'); if (!is_array($admin_emails)) { $admin_emails = explode(',', $admin_emails); } if (empty($admin_emails) || !$admin_emails[0]) { $admin_emails = [Configuration::get('PS_SHOP_EMAIL')]; } $id_lang = (int) $this->context->language->id; $vars = [ '{id}' => $id, '{email}' => $email, '{message}' => $message, '{fullname}' => $fullname, '{company}' => $company, '{phone}' => $phone, '{subject}' => $subject, ]; $file_attachement = []; $file_attachement['mime'] = 'application/pdf'; $file_attachement['name'] = sprintf('quote_%d.pdf', $id); $file_attachement['content'] = $this->module->generateQuotePdf($id, $id_quotation); foreach ($admin_emails as $admin_email) { if (!filter_var($admin_email, FILTER_VALIDATE_EMAIL)) { throw new PrestaShopException("Invalid admin email: {$admin_email}"); } $mail_sent = Mail::Send( (int) $id_lang, 'notifyadmin', Mail::l('Quote Received', (int) $id_lang), $vars, $admin_email, null, null, $this->context->shop->name, $file_attachement, null, _PS_MODULE_DIR_ . 'productquotation/mails/', false, $this->context->shop->id ); if (!$mail_sent) { return false; } } return true; } private function sendMailToUser($id_quotation, $email, $key, $message, $fullname, $company, $phone, $subject) { $id_lang = (int) $this->context->language->id; $template = 'notifyuser'; $message = Configuration::get('PQUOTE_MESSAGE', $id_lang); $heading = Mail::l('Your Quote Details', (int) $id_lang); $message_link = $this->context->link->getModuleLink( 'productquotation', 'messages' ) . '?id_quotation=' . $id_quotation . '&key=' . $key; $vars = [ '{link_message}' => $message_link, '{email}' => $email, '{message}' => $message, '{fullname}' => $fullname, '{company}' => $company, '{phone}' => $phone, '{subject}' => $subject, ]; return Mail::Send( (int) $id_lang, $template, $heading, $vars, $email, null, null, $this->context->shop->name, null, null, _PS_MODULE_DIR_ . 'productquotation/mails/', false, 1 ); } // captcha Validation private function pQouteCaptchaValidation() { $errors = []; if (($captcha_type = Configuration::get('PQUOTE_CAPTCHA_V3')) == 'google_v3') { if (Tools::getIsset('g-recaptcha-response') && ($reCaptcha = Tools::getValue('g-recaptcha-response')) && Validate::isCleanHtml($reCaptcha)) { $secret = Configuration::get('PQUOTE_GOOGLE' . ($captcha_type == 'google_v3' ? '_V3' : '') . '_CAPTCHA_SECRET_KEY'); $site_verify = 'https://www.google.com/recaptcha/api/siteverify'; $query_build = http_build_query([ 'secret' => $secret, 'response' => $reCaptcha, ]); $response = Tools::file_get_contents($site_verify . '?' . $query_build); $response = json_decode($response); if (!$response || (property_exists($response, 'success') && $response->success == false) || (property_exists($response, 'score') && $response->score < 0.5)) { $errors[] = $this->module->l('reCaptcha is invalid or Timeout.'); } } else { $errors[] = $this->module->l('reCaptcha error'); } if (!Tools::getIsset('g-recaptcha-response')) { exit(json_encode([ 'error' => true, 'message' => $this->module->l('404 not found!'), ])); } } return $errors; } // add to cart public function displayAjaxAddProductToCart() { if (true === Tools::version_compare(_PS_VERSION_, '1.7.0.0', '>=')) { $id_field_error = 'product-availability'; } else { $id_field_error = 'availability_value'; } $default_form_data = Tools::getValue('form_datas'); $productsData = json_decode($default_form_data, true); $cart = $this->context->cart; $successProducts = []; $errors = []; foreach ($productsData as $form_data) { $id_product = (int) $form_data['product_id']; $quantity = $form_data['qty']; $combination = $form_data['combination']; $id_customization = 0; $product = new Product($id_product); $id_product_attribute = (int) $form_data['id_product_attribute']; if (!$id_product || !Validate::isLoadedObject($pricingCalProduct = new Product($id_product, true, (int) Context::getContext()->language->id))) { $errors[] = [ 'id_field' => $id_field_error, 'msg' => $this->module->l('Product not found for ID: ' . $id_product), ]; continue; } elseif (!$pricingCalProduct->active) { $errors[] = [ 'id_field' => $id_field_error, 'msg' => $this->module->l('Inactive Product for ID: ' . $id_product), ]; continue; } elseif (!$quantity || !Validate::isUnsignedInt($quantity)) { $errors[] = [ 'id_field' => $id_field_error, 'msg' => $this->module->l('Quantity is not valid for ID: ' . $id_product), ]; continue; } elseif ($quantity < $pricingCalProduct->minimal_quantity) { $errors[] = [ 'id_field' => $id_field_error, 'msg' => sprintf($this->module->l('You must order minimum quantity %s for ID: %s.'), $pricingCalProduct->minimum_quantity, $id_product), ]; continue; } elseif ($quantity > StockAvailable::getQuantityAvailableByProduct($pricingCalProduct->id, $id_product_attribute, $this->context->shop->id)) { $errors[] = [ 'id_field' => $id_field_error, 'msg' => $this->module->l('Unavailable Quantity for ID: ' . $id_product), ]; continue; } elseif (!$pricingCalProduct->checkQty($quantity)) { $errors[] = [ 'id_field' => $id_field_error, 'msg' => $this->module->l('Unavailable Quantity for ID: ' . $id_product), ]; continue; } // Update cart if ($product->price) { $this->setContextCart(); $updated = $cart->updateQty( $quantity, $pricingCalProduct->id, $id_product_attribute, $id_customization, 'up', $cart->id_address_delivery ); if ($updated) { $successProducts[] = [ 'product_id' => $id_product, 'quantity' => $quantity, 'id_customization' => (int) $id_customization, ]; } else { $errors[] = [ 'id_field' => $id_field_error, 'msg' => $this->module->l('Product cannot be added to cart for ID: ' . $id_product), ]; } } } if (count($errors) > 0) { exit(json_encode([ 'hasError' => true, 'errors' => $errors, ])); } exit(json_encode([ 'success' => true, 'products' => $successProducts, 'msg' => $this->module->l('Products added to cart successfully.'), ])); } // resubmit Quote public function displayAjaxResubmitQuote() { $quoteId = (int) Tools::getValue('id_quote'); if (!$quoteId) { exit(json_encode(['success' => false, 'error' => 'Quote ID is missing'])); } $obj = new Quote(); $quotes = Quote::getProductQuotationById($quoteId); foreach ($quotes as $quote) { if (empty($quote)) { exit(json_encode(['success' => false, 'error' => 'Quote not found'])); } if (in_array($quote['status'], [3, 4])) { $key_code = md5($quote['email']); $obj->saveQuote($quote['id_quote'], $quote['email'], $quote['template'], $quote['id_customer'], $quote['id_currency'], $key_code); exit(json_encode(['success' => true, 'message' => 'Quote has been resubmitted.'])); } elseif ($quote['status'] == 7) { $expiration_date = $obj->getVoucherDetails($quote['id_productquotation']); $currentDate = new DateTime(); $expirationDate = new DateTime($expiration_date['date_to']); if ($expirationDate < $currentDate) { $key_code = md5($quote['email']); $obj->saveQuote($quote['id_quote'], $quote['email'], $quote['template'], $quote['id_customer'], $quote['id_currency'], $key_code); exit(json_encode(['success' => true, 'message' => 'Quote was expired and has been resubmitted.'])); } else { exit(json_encode(['success' => false, 'error' => 'Approved quote is still valid and not expired.'])); } } else { exit(json_encode(['success' => false, 'error' => 'Quote status does not allow resubmission.'])); } } } // update price and quantity public function updateQuoteProduct($productId, $product_attribute_id, $price, $quantity) { Db::getInstance()->update( 'quotes_products', [ 'quotes_price' => (float) $price, 'qty' => (int) $quantity, ], 'id_product = ' . (int) $productId . ' AND combination = ' . (int) $product_attribute_id ); } protected function setContextCart() { if (!$this->context->cookie->id_guest) { Guest::setNewGuest($this->context->cookie); } if (!$this->context->cart->id) { if ($this->context->cookie->id_guest) { $guest = new Guest(Context::getContext()->cookie->id_guest); $this->context->cart->mobile_theme = $guest->mobile_theme; } $this->context->cart->add(); if ($this->context->cart->id) { $this->context->cookie->id_cart = (int) $this->context->cart->id; $this->context->cookie->write(); } } $this->context->cart = $this->context->cart; } }
Save
cmd:
run