234 lines
6.7 KiB
PHP
234 lines
6.7 KiB
PHP
<?php
|
|
|
|
use KupShop\KupShopBundle\Config;
|
|
use Query\Operator;
|
|
|
|
class AdminAjax
|
|
{
|
|
/** @var \Query\QueryBuilder */
|
|
public $qb;
|
|
|
|
private $search;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->qb = sqlQueryBuilder();
|
|
}
|
|
|
|
public function run()
|
|
{
|
|
header('Content-type: application/json');
|
|
$type = getVal('type');
|
|
|
|
$limit = getVal('limit', null, 100);
|
|
if ($limit < 0) {
|
|
$limit = 999999;
|
|
}
|
|
|
|
$this->qb->setMaxResults($limit);
|
|
|
|
return $this->handle($type);
|
|
}
|
|
|
|
public function handle($type)
|
|
{
|
|
$method = 'handle'.ucfirst($type);
|
|
if (method_exists($this, $method)) {
|
|
return $this->$method();
|
|
}
|
|
|
|
return $this->fallback($type);
|
|
}
|
|
|
|
public function handleOrder()
|
|
{
|
|
$orderId = getVal('id_order');
|
|
$result = false;
|
|
if (!empty($orderId)) {
|
|
$result = sqlQueryBuilder()->select('o.*')
|
|
->from('orders', 'o')
|
|
->where(Operator::equals(['o.id' => $orderId]))
|
|
->execute()->fetch();
|
|
}
|
|
|
|
if (!$result) {
|
|
$result = [];
|
|
}
|
|
|
|
return json_encode($result);
|
|
}
|
|
|
|
public function handlePositionsOverSupply()
|
|
{
|
|
if (findModule(Modules::WAREHOUSE)) {
|
|
sqlQueryBuilder()->update('warehouse_products')
|
|
->directValues(['over_supply' => getVal('value')])
|
|
->andWhere(Operator::equalsNullable([
|
|
'id_position' => getVal('id_position'),
|
|
'id_product' => getVal('id_product'),
|
|
'id_variation' => getVal('id_variation') ?: null,
|
|
]))->execute();
|
|
|
|
return getVal('value') == 'Y';
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function handleProduct_info()
|
|
{
|
|
$cfg = Config::get();
|
|
$qb = $this->qb
|
|
->select('v.vat as vat, COALESCE(pv.in_store, p.in_store) as quantity, COALESCE(pv.ean, p.ean) as ean, COALESCE(pv.price, p.price) as price, p.discount as discount');
|
|
|
|
if (!empty($cfg['Modules']['products_variations']['variationCode'])) {
|
|
$qb->addSelect(' COALESCE(pv.code, p.code) as code');
|
|
} else {
|
|
$qb->addSelect(' p.code as code');
|
|
}
|
|
|
|
if (findModule('products', 'weight')) {
|
|
if (findModule('products_variations')) {
|
|
$qb->addSelect(' COALESCE(pv.weight, p.weight) as weight');
|
|
} else {
|
|
$qb->addSelect(' p.weight as weight');
|
|
}
|
|
}
|
|
|
|
if (findModule(Modules::PRODUCTS, Modules::SUB_PRICE_BUY)) {
|
|
if (findModule(Modules::PRODUCTS_VARIATIONS)) {
|
|
$qb->addSelect('COALESCE(pv.price_buy, p.price_buy) as price_buy');
|
|
} else {
|
|
$qb->addSelect('p.price_buy as price_buy');
|
|
}
|
|
}
|
|
|
|
$qb->from('products', 'p')
|
|
->leftJoin('p', 'products_variations', 'pv', 'p.id=pv.id_product')
|
|
->leftJoin('p', 'vats', 'v', 'p.vat=v.id');
|
|
|
|
if (!getVal('id_product')) {
|
|
exit;
|
|
}
|
|
|
|
$id_product = intval(getVal('id_product'));
|
|
$id_variation = intval(getVal('id_variation'));
|
|
|
|
$qb->where(Operator::equals(['p.id' => $id_product]));
|
|
|
|
if ($id_variation > 0) {
|
|
$qb->andWhere(Operator::equals(['pv.id' => $id_variation]));
|
|
}
|
|
|
|
$products = $qb->execute()->fetchAll();
|
|
$user_id = getVal('user_id');
|
|
|
|
if (findModule(Modules::PRICE_LEVELS) && $user_id) {
|
|
$user = User::createFromId($user_id);
|
|
if ($price_level_id = $user->getUserPricelevelId()) {
|
|
$plService = KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService(\KupShop\KupShopBundle\Context\PriceLevelContext::class);
|
|
$plService->activate($price_level_id);
|
|
}
|
|
}
|
|
|
|
if (getVal('currency')) {
|
|
$currencyContext = KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService(\KupShop\KupShopBundle\Context\CurrencyContext::class);
|
|
$currencyContext->activate(getVal('currency'));
|
|
}
|
|
|
|
$result = [];
|
|
|
|
foreach ($products as $product) {
|
|
$PRICE_ARRAY = formatCustomerPrice($product['price'], $product['discount'], $product['vat'], $_GET['id_product']);
|
|
|
|
$price_with_vat = $PRICE_ARRAY['value_without_vat_no_rounding']->addVat($product['vat']);
|
|
|
|
$price_with_vat = roundPrice($price_with_vat);
|
|
|
|
$product['price'] = $price_with_vat->removeVat($product['vat'])->asFloat();
|
|
|
|
if (findModule(Modules::PRODUCTS, Modules::SUB_PRICE_BUY)) {
|
|
$product['price_buy_with_vat'] = toDecimal($product['price_buy'])->addVat($product['vat'])->value(4);
|
|
}
|
|
|
|
$product['price_with_vat'] = $price_with_vat->asFloat();
|
|
|
|
$result[] = $product;
|
|
}
|
|
|
|
return json_encode($result);
|
|
}
|
|
|
|
public function prepareSearchFields($fields)
|
|
{
|
|
return get_search_query($this->search, $fields);
|
|
}
|
|
|
|
public function prepareSearch($search)
|
|
{
|
|
return sqlFormatInput(trim($search));
|
|
}
|
|
|
|
public function getResult()
|
|
{
|
|
return json_encode($this->qb->execute()->fetchAll());
|
|
}
|
|
|
|
public function fallback($type)
|
|
{
|
|
$limit = getVal('limit', null, 100);
|
|
$group = null;
|
|
$where = ' 1 ';
|
|
|
|
$result = [];
|
|
|
|
$data = [];
|
|
|
|
if ($limit < 0) {
|
|
$limit = 999999;
|
|
}
|
|
|
|
switch ($type) {
|
|
case 'product_of_suppliers_info':
|
|
$fields = 'pos.code code, pos.id_supplier id_supplier';
|
|
$from = getTableName('products_of_suppliers').' pos';
|
|
if (empty($_GET['id_product'])) {
|
|
exit;
|
|
}
|
|
$id_product = intval($_GET['id_product']);
|
|
$id_variation = intval($_GET['id_variation']);
|
|
|
|
$where = "pos.id_product={$id_product}";
|
|
|
|
if (!empty($_GET['id_supplier'])) {
|
|
$id_supplier = intval($_GET['id_supplier']);
|
|
$where .= " AND pos.id_supplier={$id_supplier}";
|
|
}
|
|
|
|
if (!empty($id_variation)) {
|
|
$where .= " AND pos.id_variation={$id_variation}";
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if ($where) {
|
|
$where = " WHERE {$where} ";
|
|
}
|
|
|
|
if ($group) {
|
|
$where .= " GROUP BY {$group} ";
|
|
}
|
|
|
|
$SQL = sqlQuery("SELECT {$fields}
|
|
FROM {$from}
|
|
{$where} LIMIT {$limit}", $data);
|
|
|
|
$result = array_merge($result, $SQL->fetchAll());
|
|
|
|
return json_encode($result);
|
|
}
|
|
}
|
|
|
|
echo (new AdminAjax())->run();
|