403Webshell
Server IP : 80.87.202.40  /  Your IP : 216.73.216.169
Web Server : Apache
System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64
User : bitrix ( 600)
PHP Version : 8.2.27
Disable Function : NONE
MySQL : OFF |  cURL : ON |  WGET : ON |  Perl : ON |  Python : OFF |  Sudo : ON |  Pkexec : ON
Directory :  /home/bitrix/ext_www/rospirotorg.ru/bitrix/php_interface/wbs24.lib/lib/Stocks/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/rospirotorg.ru/bitrix/php_interface/wbs24.lib/lib/Stocks/Wholesale.php
<?php
namespace Wbs24\Lib\Stocks;

class Wholesale
{
    public const ALLOWED_GROUP_ID = 9;
    public const WAREHOUSE_ID = 1;

    protected $isAllowedGroup;
    protected $wrappers;

    public function __construct(array $objects = [])
    {
        $this->wrappers = new \stdClass();
        $this->wrappers->UserGroupTable = $objects['UserGroupTable'] ?? new Wrappers\UserGroupTable();
        $this->wrappers->User = $objects['User'] ?? $GLOBALS["USER"];
        $this->wrappers->StoreProductTable = $objects['StoreProductTable'] ?? new Wrappers\StoreProductTable();
        $this->wrappers->CCatalogProduct = $objects['CCatalogProduct'] ?? new Wrappers\CCatalogProduct();
    }

    // для CatalogProvider
    public function getCatalogProducts(array $resultList): array
    {
        $productIdsToStocks = [];
        foreach ($resultList as $productId => $product) {
            $productIdsToStocks[$productId] = $product['QUANTITY'];
        }

        $productIdsToStocks = $this->updateProductIdsToStocks($productIdsToStocks);

        foreach ($productIdsToStocks as $productId => $stock) {
            if (isset($resultList[$productId])) {
                $resultList[$productId]['QUANTITY'] = $stock;
            }
        }

        return $resultList;
    }

    // для CatalogProvider
    public function updateStocks(int $productId, int $newFullQuantity): bool
    {
        $availableQuantity = $this->getAvailableQuantity($productId);
        $warehouseQuantity = $this->getWarehouseQuantity($productId);
        $fullQuantity = $availableQuantity + $warehouseQuantity;
        $buyQuantity = $fullQuantity - $newFullQuantity;
        $newAvailableQuantity = $availableQuantity - $buyQuantity;
        $needStillQuantity = 0;
        if ($newAvailableQuantity < 0) {
            $needStillQuantity = abs($newAvailableQuantity);
            $newAvailableQuantity = 0;
        }

        $isUpdated = $this->updateAvailableQuantity($productId, $newAvailableQuantity);

        if ($isUpdated && $needStillQuantity) {
            $newWarehouseQuantity = $warehouseQuantity - $needStillQuantity;
            $isUpdated = $this->updateWarehouseQuantity($productId, $newWarehouseQuantity);
        }

        return $isUpdated;
    }

    protected function getAvailableQuantity(int $productId): int
    {
        $quantity = 0;
        $result = $this->wrappers->CCatalogProduct->GetList([], ['ID' => $productId], false, false, ['QUANTITY']);
        if ($product = $result->Fetch()) {
            $quantity = $product['QUANTITY'];
        }

        return $quantity;
    }

    protected function updateAvailableQuantity(int $productId, int $quantity): bool
    {
        return $this->wrappers->CCatalogProduct->Update($productId, ['QUANTITY' => $quantity]);
    }

    protected function getWarehouseQuantity(int $productId): int
    {
        $productIdsToStocks = $this->getProductIdsToStocks([$productId]);

        return $productIdsToStocks[$productId] ?? 0;
    }

    protected function updateWarehouseQuantity(int $productId, int $quantity): bool
    {
        $isUpdated = false;
        $result = $this->wrappers->StoreProductTable->getList([
            'filter' => [
                '=PRODUCT_ID' => $productId,
                'STORE_ID' => $this->getWarehouseId(),
            ],
        ]);
        if ($storeEntry = $result->fetch()) {
            $isUpdated = $this->wrappers->StoreProductTable->update(
                $storeEntry['ID'],
                [
                    'PRODUCT_ID' => $productId,
                    'STORE_ID' => $this->getWarehouseId(),
                    'AMOUNT' => $quantity,
                ]
            );
        }

        return $isUpdated ? true : false;
    }

    // для CatalogComponentTrait, а также для универсальных вызовов
    public function verifyAndUpdateProductIdsToStocks(array $productIdsToStocks): array
    {
        return $this->isAllowedGroup() ? $this->updateProductIdsToStocks($productIdsToStocks) : $productIdsToStocks;
    }

    // для ajax/item.php (Aspro)
    public function verifyAndAddToBasketFromAjax(array $product): bool
    {
        if (!$this->isAllowedGroup() || !isset($product['PRODUCT_ID'])) return false;

        $success = false;
        $basket = $this->getBasket();
        if ($item = $this->getBasketItem($basket, $product['PRODUCT_ID'])) {
            $this->updateProductInBasket($basket, $item, $product);
        } else {
            $this->addProductToBasket($basket, $product);
        }
        $basket->save();

        return true;
    }

    protected function getBasket()
    {
        return \Bitrix\Sale\Basket::LoadItemsForFUser(
            \Bitrix\Sale\Fuser::getId(),
            SITE_ID
        );
    }

    protected function getBasketItem(&$basket, $productId)
    {
        $foundBasketItem = false;
        foreach ($basket as $basketItem) {
            if ($basketItem->getProductId() == $productId) {
                $foundBasketItem = $basketItem;
            }
        }

        return $foundBasketItem;
    }

    protected function addProductToBasket(&$basket, $product)
    {
        $item = $basket->createItem('catalog', $product['PRODUCT_ID']);
        $this->updateProductInBasket($basket, $item, $product);
    }

    protected function updateProductInBasket(&$basket, &$item, $product)
    {
        $newQuantity = $item->getQuantity() + $product['QUANTITY'];

        $item->setFields([
            'QUANTITY' => $newQuantity,
            'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
            'LID' => SITE_ID,
            'PRODUCT_PROVIDER_CLASS' => '\Wbs24\Lib\Stocks\CatalogProvider',
        ]);
    }

    public function updateProductIdsToStocks(array $productIdsToStocks): array
    {

        $productIds = array_keys($productIdsToStocks);
        $addProductIdsToStocks = $this->getProductIdsToStocks($productIds);

        foreach ($productIdsToStocks as $productId => $stock) {
            $addStock = $addProductIdsToStocks[$productId] ?? 0;
            $productIdsToStocks[$productId] = $stock + $addStock;
        }

        return $productIdsToStocks;
    }

    protected function getProductIdsToStocks($productIds)
    {
        $productIdsToStocks = [];
        $result = $this->wrappers->StoreProductTable->getList([
            'filter' => [
                '=PRODUCT_ID' => $productIds,
                'STORE_ID' => $this->getWarehouseId(),
            ],
        ]);
        while ($product = $result->fetch()) {
            $productIdsToStocks[$product['PRODUCT_ID']] = $product['AMOUNT'];
        }

        return $productIdsToStocks;
    }

    // используется в том числе в CatalogComponentTrait для фильтра и сортировки
    public function isAllowedGroup(): bool
    {
        if ($this->isAllowedGroup !== null) return $this->isAllowedGroup;

        $result = $this->wrappers->UserGroupTable->getList([
            'filter' => [
                'USER_ID' => $this->wrappers->User ? $this->wrappers->User->GetID() : null,
                'GROUP.ACTIVE' => 'Y',
                'GROUP_ID' => $this->getAllowedGroupId(),
            ],
            'select' => [
                'GROUP_ID',
            ],
        ]);
        $this->isAllowedGroup = $result->fetch() ? true : false;

        return $this->isAllowedGroup;
    }

    // используется в том числе в CatalogComponentTrait для фильтра и сортировки
    public function getWarehouseId(): int
    {
        return self::WAREHOUSE_ID;
    }

    protected function getAllowedGroupId(): int
    {
        return self::ALLOWED_GROUP_ID;
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit