Files
kupshop/admin/pricelevels.php
2025-08-02 16:30:27 +02:00

238 lines
9.4 KiB
PHP

<?php
use KupShop\AdminBundle\Util\AdminSectionTree;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
class PriceLevels extends Window
{
private AdminSectionTree $adminSectionTree;
protected $tableName = 'price_levels';
protected $defaults = [
'combine' => 'Y',
];
public function __construct()
{
$this->adminSectionTree = ServiceContainer::getService(AdminSectionTree::class);
}
public function get_vars()
{
$vars = parent::get_vars();
$pageVars = getVal('body', $vars);
$pageVars['tree'] = $this->adminSectionTree->getCategories();
$ID = $this->getID();
$dbcfg = Settings::getDefault();
$pageVars['data']['priceWithVat'] = $dbcfg['prod_prefer_price_vat'] == 'Y' || $dbcfg['prod_prefer_price_vat'] == 'F';
$pageVars['price_levels_products'] = [];
if (!empty($ID)) {
$SQL = sqlQuery('SELECT id_section, discount, unit
FROM '.getTableName('price_levels_sections')."
WHERE id_price_level='".$ID."' ");
$pageVars['dbSec'] = sqlFetchAll($SQL, 'id_section');
$this->selected = $pageVars['dbSec'];
$this->adminSectionTree->getOpened($pageVars['tree']);
$pageVars['opened'] = $this->adminSectionTree->opened;
$SQL = sqlQuery('SELECT p.id, p.name, plp.discount, plp.unit
FROM price_levels_producers AS plp
JOIN producers AS p ON plp.id_producer=p.id AND plp.id_price_level=:id
ORDER BY p.name ASC', ['id' => $ID]);
$pageVars['price_levels_producers'] = sqlFetchAll($SQL);
$SQL = sqlQuery('SELECT p.id, p.code, p.in_store, p.title, plp.discount, plp.unit,
ph.id as id_photo, ph.source, ph.image_2, vats.vat AS vat, vats.vat AS vats_value
FROM price_levels_products AS plp, products AS p
LEFT JOIN vats ON vats.id = p.vat
LEFT JOIN photos_products_relation ppr ON ppr.id_product=p.id AND ppr.show_in_lead = "Y"
LEFT JOIN photos ph ON ph.id=ppr.id_photo
WHERE plp.id_price_level=:id AND plp.id_product=p.id
ORDER BY p.title ASC', ['id' => $ID]);
foreach ($SQL as $product) {
if (($product['unit'] == 'final_price' || $product['unit'] == 'price') && $pageVars['data']['priceWithVat'] && $product['vats_value']) {
$product['discount'] = calcPrice($product['discount'], $product['vats_value'])->printFloatValue(-2);
}
$product['image'] = getImage($product['id_photo'], $product['image_2'], $product['source'], 4);
$pageVars['price_levels_products'][] = $product;
}
$pageVars['ranges'] = sqlQueryBuilder()
->select('*')
->from('price_levels_ranges')
->orderBy('range_from')
->where(\Query\Operator::equals(['id_price_level' => $ID]))
->execute()
->fetchAll();
$last = 0;
foreach ($pageVars['ranges'] as &$margin) {
if ($margin['range_from'] < $last) {
$margin['bad_down_range'] = true;
}
$last = $margin['range_to'];
}
}
$this->unserializeCustomData($pageVars['data']);
$vars['body'] = $pageVars;
return $vars;
}
public function getData()
{
$data = parent::getData();
if (getVal('Submit')) {
$this->serializeCustomData($data);
}
return $data;
}
public function handleUpdate()
{
$OLD_ID = $this->getID();
$SQL = parent::handleUpdate();
if ($SQL) {
$ID = $this->getID();
$data = $this->getData();
if ($this->isDuplicate()) {
sqlQuery('INSERT INTO price_levels_producers (id_price_level, id_producer, discount, unit)
SELECT :new_price_level, id_producer, discount, unit
FROM price_levels_producers WHERE id_price_level=:old_price_level', ['new_price_level' => $ID, 'old_price_level' => $OLD_ID]);
sqlQuery('INSERT INTO price_levels_products (id_price_level, id_product, discount, unit)
SELECT :new_price_level, id_product, discount, unit
FROM price_levels_products WHERE id_price_level=:old_price_level', ['new_price_level' => $ID, 'old_price_level' => $OLD_ID]);
sqlQuery('INSERT INTO price_levels_sections (id_price_level, id_section, discount, unit)
SELECT :new_price_level, id_section, discount, unit
FROM price_levels_sections WHERE id_price_level=:old_price_level', ['new_price_level' => $ID, 'old_price_level' => $OLD_ID]);
}
// # ZARAZENI CENOVYCH HLADIN DO SEKCI
if (findModule('products_sections')) {
// plSecDiscount,plSecUnit
sqlQuery('DELETE FROM '.getTableName('price_levels_sections')."
WHERE id_price_level={$ID}");
foreach (getVal('plSecID', null, []) as $key => $value) {
$key = intval($key);
$id = intval($_POST['plSecID'][$key]);
$discount = trim($_POST['plSecDiscount'][$key]);
$unit = ($_POST['plSecUnit'][$key] == 'perc') ? 'perc' : 'price';
// jestli neni prazdny klidc, hodnota a jestli ma parametr nastaveno - pouzit: Y
if (empty($id) || $discount == '') {
continue;
}
$this->preparePrice($discount);
$this->insertSQL('price_levels_sections', [
'id_price_level' => $ID,
'id_section' => $id,
'discount' => $discount,
'unit' => $unit,
]);
}
}
// # ZARAZENI CENOVYCH HLADIN KE ZBOZI
if (findModule('products')) {
$products = getVal('products', $data, []);
foreach ($products as $id => $product) {
$product['id_product'] = intval($product['id_product']);
$product['id_price_level'] = $ID;
if (!empty($product['delete']) || !$id || $product['id_product'] <= 0) {
if ($id > 0) {
$this->deleteSQL('price_levels_products', $this->filterFields($product, ['id_product', 'id_price_level']));
}
continue;
}
$this->preparePrice($product['discount']);
if (($product['unit'] == 'final_price' || $product['unit'] == 'price') && $product['priceWithVat']) {
$product['discount'] = calcPrice($product['discount'], -$product['priceVat']);
}
if ($id < 0) {
$this->insertSQL('price_levels_products', $product, ['priceVat', 'priceWithVat']);
} else {
$this->updateSQL('price_levels_products', $product, $this->filterFields($product, ['id_product', 'id_price_level']), ['priceVat', 'priceWithVat']);
}
}
}
// # ZARAZENI CENOVYCH HLADIN K VYROBCI
if (findModule('producers')) {
$producers = getVal('producers', $data, []);
foreach ($producers as $id => $producer) {
$producer['id_producer'] = intval($producer['id_producer']);
$producer['id_price_level'] = $ID;
if (!empty($producer['delete']) || !$id || $producer['id_producer'] <= 0) {
if ($id > 0) {
$this->deleteSQL('price_levels_producers', $this->filterFields($producer, ['id_producer', 'id_price_level']));
}
continue;
}
$this->preparePrice($producer['discount']);
if ($id < 0) {
$this->insertSQL('price_levels_producers', $producer);
} else {
$this->updateSQL('price_levels_producers', $producer, $this->filterFields($producer, ['id_producer', 'id_price_level']));
}
}
}
/**
* ZARAZENI rozsahu cen v CENOVYCH HLADINach.
*/
$ranges = getVal('ranges', $data, []);
if (!empty($ranges)) {
$this->deleteSQL('price_levels_ranges', ['id_price_level' => $ID]);
foreach ($ranges as $key => $range) {
$key = intval($key);
// jestli neni prazdny klidc, hodnota a jestli ma parametr nastaveno - pouzit: Y
if (!empty($range['delete']) || empty($key) || $range['discount'] == '') {
continue;
}
$range['id_price_level'] = $ID;
$this->preparePrice($range['discount']);
$this->insertSQL('price_levels_ranges', $range);
}
}
}
return $SQL;
}
}
$pricelevels = new PriceLevels();
$pricelevels->run();