Files
wyczarujprezent.pl/modules/zakeke/controllers/front/price.php
2024-10-28 22:14:22 +01:00

199 lines
6.2 KiB
PHP

<?php
/**
* Copyright (C) 2020 Futurenext srl
*
* This file is part of Zakeke.
*
* Zakeke Interactive Product Designer can not be copied and/or distributed
* without the express permission of Futurenext srl
*
* @author Futurenext srl <help@zakeke.com>
* @copyright 2019 Futurenext srl
* @license https://www.zakeke.com/privacy/#general_conditions
*/
class ZakekePriceModuleFrontController extends ModuleFrontController
{
/** @var float */
protected $productPrice;
/** @var float */
protected $zakekePrice;
/** @var float */
protected $zakekeFinalPrice;
/** @var float */
protected $finalPrice;
/** @var bool */
protected $isInStock;
public function init()
{
parent::init();
$id_product = (int)Tools::getValue('id_product');
if (!$id_product) {
$this->errors[] = $this->module->l('id_product must be not empty.');
return;
}
$product = new Product($id_product, true, $this->context->language->id, $this->context->shop->id);
if (!Validate::isLoadedObject($product)) {
$this->errors[] = $this->module->l('Product not found.');
return;
} elseif (!$product->isAssociatedToShop() ||
!$product->active ||
!$product->checkAccess(
isset($this->context->customer->id)
&& $this->context->customer->id
? (int)$this->context->customer->id
: 0
)
) {
$this->errors[] = $this->module->l('Permission denied.');
return;
}
$qty = max((int)Tools::getValue('qty', 1), 1);
$totalQty = max((int)Tools::getValue('total_qty', $qty), $qty);
$idProductAttribute = 0;
if (_PS_VERSION_ < 1.7) {
$groups = array();
foreach (Tools::getAllValues() as $key => $value) {
if (Tools::substr($key, 0, 6) == 'group_') {
$groups[Tools::substr($key, 6)] = $value;
}
}
if (!empty($groups)) {
$idProductAttribute = (int)ZakekeCompatibility::getIdProductAttributeByIdAttributes(
$id_product,
$groups,
true
);
}
} else {
$groups = Tools::getValue('group');
if (!empty($groups)) {
$idProductAttribute = (int)Product::getIdProductAttributesByIdAttributes(
$id_product,
$groups,
true
);
}
}
$priceDisplay = Product::getTaxCalculationMethod((int)$this->context->cookie->id_customer);
$tax = (!$priceDisplay || $priceDisplay == 2) && !Tax::excludeTaxeOption();
$this->productPrice = $product->getPrice($tax, $idProductAttribute, 6, null, false, true, $totalQty) * $qty;
$this->zakekePrice = 0.0;
$zakekePercentPrice = (float)Tools::getValue('zakeke-percent-price', 0.0);
if ($zakekePercentPrice > 0.0) {
$productPriceWT = $product->getPrice(false, $idProductAttribute, 6, null, false, true, $totalQty) * $qty;
$this->zakekePrice += $productPriceWT * ($zakekePercentPrice / 100);
}
$this->zakekePrice += (float)Tools::getValue('zakeke-price', 0.0);
$this->zakekeFinalPrice = 0.0;
if ($this->zakekePrice >= 0.0) {
if ($tax) {
// retrieve address information
$address = Address::initialize(null, true);
if (!(!empty($address->vat_number)
&& $address->id_country != Configuration::get('VATNUMBER_COUNTRY')
&& Configuration::get('VATNUMBER_MANAGEMENT'))) {
// Add Tax
$taxManager = TaxManagerFactory::getManager(
$address,
Product::getIdTaxRulesGroupByIdProduct((int)$id_product, $this->context)
);
$taxCalculator = $taxManager->getTaxCalculator();
$this->zakekeFinalPrice = $taxCalculator->addTaxes($this->zakekePrice);
$this->zakekeFinalPrice = Tools::ps_round($this->zakekeFinalPrice, 2);
}
} else {
$this->zakekeFinalPrice = $this->zakekePrice;
}
}
if (_PS_VERSION_ >= 1.7) {
$context = Context::getContext();
$id_group = $context->customer->id_default_group;
$id_currency = (int) $context->currency->id;
$ids = Address::getCountryAndState((int) $context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$id_country = $ids['id_country'] ?
(int) $ids['id_country']
: (int) Configuration::get('PS_COUNTRY_DEFAULT');
$specific_price = SpecificPrice::getSpecificPrice(
(int) $id_product,
$context->shop->id,
$id_currency,
$id_country,
$id_group,
$totalQty,
null,
0,
0,
$totalQty
);
if ($specific_price && $specific_price['reduction_type'] != 'amount') {
$this->zakekeFinalPrice -= $this->zakekeFinalPrice * $specific_price['reduction'];
}
}
$this->finalPrice = Tools::ps_round($this->productPrice + $this->zakekeFinalPrice, 2);
$this->isInStock = $product->checkQty($qty);
}
public function display()
{
$this->displayAjax();
}
public function displayAjax()
{
ob_end_clean();
header('Content-Type: application/json');
if (!$this->errors) {
$this->ajaxDie(json_encode(array(
'success' => true,
'zakekePrice' => $this->zakekePrice,
'zakekeFinalPrice' => $this->zakekeFinalPrice,
'price' => $this->productPrice,
'finalPrice' => $this->finalPrice,
'isInStock' => $this->isInStock
)));
} else {
$this->ajaxDie(json_encode(array(
'hasError' => true,
'errors' => $this->errors
)));
}
}
}