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/cvetdv.ru/bitrix/modules/wbs24.ozonapinew/lib/Prices/ |
Upload File : |
<?php namespace Wbs24\Ozonapinew\Prices; use Bitrix\Main\{ Loader, SystemException }; use Wbs24\Ozonapinew\{ Main, Exception, Wrappers, Product, Formula }; class Helper { use Exception; protected $main; protected $moduleId; protected $wrappers; public function __construct(array $objects = []) { try { if (!Loader::IncludeModule('catalog')) { throw new SystemException("Catalog module isn`t installed"); } if (!Loader::IncludeModule('iblock')) { throw new SystemException("Iblock module isn`t installed"); } $this->main = $objects['Main'] ?? new Main(); $this->moduleId = $this->main->getModuleId(); $this->wrappers = new Wrappers($objects); $this->Product = $objects['Product'] ?? new Product(); $this->Formula = $objects['Formula'] ?? new Formula(); // price $this->ozonProductPriceFormula = $this->wrappers->Option->get( $this->moduleId, 'ozonPriceProduct' ); $this->ozonOfferPriceFormula = $this->wrappers->Option->get( $this->moduleId, 'ozonPriceOffer' ); // old_price $this->ozonProductOldPriceFormula = $this->wrappers->Option->get( $this->moduleId, 'ozonOldPriceProduct' ); $this->ozonOfferOldPriceFormula = $this->wrappers->Option->get( $this->moduleId, 'ozonOldPriceOffer' ); // min_price $this->ozonProductMinPriceFormula = $this->wrappers->Option->get( $this->moduleId, 'ozonMinPriceProduct' ); $this->ozonOfferMinPriceFormula = $this->wrappers->Option->get( $this->moduleId, 'ozonMinOfferProduct' ); } catch (SystemException $exception) { $this->exceptionHandler($exception); } } public function getFormulaTemplates() { return [ 'ozonPrice' => [ 'PRODUCT' => $this->ozonProductPriceFormula, 'OFFER' => $this->ozonOfferPriceFormula, ], 'ozonOldPrice' => [ 'PRODUCT' => $this->ozonProductOldPriceFormula, 'OFFER' => $this->ozonOfferOldPriceFormula, ], 'ozonMinPrice' => [ 'PRODUCT' => $this->ozonProductMinPriceFormula, 'OFFER' => $this->ozonOfferMinPriceFormula, ] ]; } public function getPricesByProductId(int $productId, $productInfo): array { $formulaTemplates = $this->getFormulaTemplates(); $calculatePrices = []; switch ((int) $productInfo['type']) { case 1: case 2: case 3: foreach ($formulaTemplates as $ozonPriceType => $formulaTemplates) { $calculatePrices[$ozonPriceType] = $this->calculateProductPrice( [ 'productId' => $productId, 'iblockId' => $productInfo['iblock_id'], 'formula' => $formulaTemplates['PRODUCT'] ] ); } break; case 4: foreach ($formulaTemplates as $ozonPriceType => $formulaTemplates) { $calculatePrices[$ozonPriceType] = $this->calculateOfferPrice( [ 'productId' => $productId, 'parentProductId' => $productInfo['parent_product_id'], 'iblockId' => $productInfo['iblock_id'], 'parentIblockId' => $productInfo['parent_iblock_id'], 'formula' => $formulaTemplates['OFFER'] ] ); } break; } // Валидация old_price $validateOldPrice = $this->validateOldPrice( $calculatePrices['ozonPrice'], $calculatePrices['ozonOldPrice'] ); // Валидация min_price $validateMinPrice = $this->validateMinPrice( $calculatePrices['ozonPrice'], $calculatePrices['ozonMinPrice'] ); return [ 'price' => $calculatePrices['ozonPrice'], 'oldPrice' => $validateOldPrice ? $calculatePrices['ozonOldPrice'] : 0, 'minPrice' => $validateMinPrice ? $calculatePrices['ozonMinPrice'] : 0, ]; } protected function validateOldPrice($price, $oldPrice) { $validate = true; // меньше 400 руб if ($price < 400) { ($price <= $oldPrice - 20) ? $validate = true : $validate = false; } // от 400 руб до 10 000 if ( $price > 400 && $price < 10000 ) { ($price <= $oldPrice * 0.95) ? $validate = true : $validate = false; } // свыше 10 000 if ( $price > 10000 ) { ($price <= $oldPrice - 500) ? $validate = true : $validate = false; } return $validate; } protected function validateMinPrice($price, $minPrice) { return $minPrice > round(($price / 2)); } protected function calculateProductPrice($needInfo) { $productId = $needInfo['productId']; $iblockId = $needInfo['iblockId']; $formula = $needInfo['formula']; if ( !$productId || !$iblockId || !$formula ) { return 0; } // Получить айди свойств из меток $propertyIds = $this->getPropertyOrPriceIdsFromTemplate( $formula, 'PROPERTY_' ); // Получить типы цен из меток $priceTypeIds = $this->getPropertyOrPriceIdsFromTemplate( $formula, 'PRICE_' ); // Получить свойства простого товара $propertyIdsToValues = []; if ($propertyIds) { $propertyIdsToValues = $this->getPropertyIdsToValues( $productId, $iblockId, $propertyIds ); } // Получить типы цен $priceTypeIdsToValues = []; if ($priceTypeIds) { $priceTypeIdsToValues = $this->getPriceTypeIdsToValues( $productId, $priceTypeIds ); } // Сопоставляем метки к полученным значениям $marksToValues = $this->compareMarksToValues( $priceTypeIdsToValues, $propertyIdsToValues, ); // если меток вообще нет возвращаем 0 if (empty($marksToValues)) return 0; // проверить, что у всех меток есть заполненные значения $checkMarksHaveValues = $this->checkMarksHaveValues($marksToValues); if (!$checkMarksHaveValues) return 0; // Отправялем на калькуляцию return $this->calcByFormula($formula, $marksToValues); } protected function calculateOfferPrice($needInfo) { $productId = $needInfo['productId']; $parentProductId = $needInfo['parentProductId']; $iblockId = $needInfo['iblockId']; $parentIblockId = $needInfo['parentIblockId']; $formula = $needInfo['formula']; if ( !$productId || !$parentProductId || !$iblockId || !$parentIblockId || !$formula ) { return 0; } // Получить айди свойств из меток $offerPropertyIds = $this->getPropertyOrPriceIdsFromTemplate( $formula, 'OFFER_PROPERTY_' ); $propertyIds = $this->getPropertyOrPriceIdsFromTemplate( $formula, 'PROPERTY_' ); $priceTypeIds = $this->getPropertyOrPriceIdsFromTemplate( $formula, 'PRICE_' ); // Получить свойства торгового предложения $offerPropertyIdsToValues = []; if ($offerPropertyIds) { $offerPropertyIdsToValues = $this->getPropertyIdsToValues( $productId, $iblockId, $offerPropertyIds ); } // Получить свойства родительского товара $propertyIdsToValues = []; if ($propertyIds) { $propertyIdsToValues = $this->getPropertyIdsToValues( $parentProductId, $parentIblockId, $propertyIds ); } // Получить типы цен $priceTypeIdsToValues = []; if ($priceTypeIds) { $priceTypeIdsToValues = $this->getPriceTypeIdsToValues( $productId, $priceTypeIds ); } // Сопоставляем метки к полученным значениям $marksToValues = $this->compareMarksToValues( $priceTypeIdsToValues, $propertyIdsToValues, $offerPropertyIdsToValues, ); // если меток вообще нет возвращаем 0 if (empty($marksToValues)) return 0; // проверить, что у всех меток есть заполненные значения $checkMarksHaveValues = $this->checkMarksHaveValues($marksToValues); if (!$checkMarksHaveValues) return 0; // Отправялем на калькуляцию return $this->calcByFormula($formula, $marksToValues); } protected function compareMarksToValues( $priceTypeIdsToValues, $propertyIdsToValues, $offerPropertyIdsToValues = [], ) { $marksToValues = []; foreach ($priceTypeIdsToValues as $priceTypeId => $priceValue) { $marksToValues['PRICE_'.$priceTypeId] = $priceValue; } foreach ($propertyIdsToValues as $propertyId => $propertyValue) { $marksToValues['PROPERTY_'.$propertyId] = $propertyValue; } if ($offerPropertyIdsToValues) { foreach ($offerPropertyIdsToValues as $propertyId => $propertyValue) { $marksToValues['OFFER_PROPERTY_'.$propertyId] = $propertyValue; } } return $marksToValues; } protected function checkMarksHaveValues($marksToValues) { $result = true; foreach ($marksToValues as $mark => $value) { if (!$value) { $result = false; break; } } return $result; } protected function getPropertyIdsToValues($productId, $iblockId, $propertyIds) { $propertyIdsToValues = []; $findProperties = []; $iterator = \CIBlockElement::GetPropertyValues( $iblockId, ['ID' => $productId], false, ['ID' => $propertyIds] ); if ($row = $iterator->Fetch()) { $findProperties = $row; } foreach ($propertyIds as $propertyId) { $propertyIdsToValues[$propertyId] = (int) $findProperties[$propertyId] ?? 0; } return $propertyIdsToValues; } protected function getPriceTypeIdsToValues($productId, $priceTypeIds) { $priceTypeIdsToValues = []; $findPrices = []; $preparedPriceTypeIds = []; foreach ($priceTypeIds as $priceTypeId) { $preparedPriceTypeIds[] = 'PRICE_'.$priceTypeId; } $select = ['ID', 'IBLOCK_ID']; $select = array_merge($select, $preparedPriceTypeIds); $filter = ['ID' => $productId]; $source = 'prices'; $resultQuery = $this->Product->get($select, $filter, $source); if ($priceInfo = $resultQuery->fetch()) { $findPrices = $priceInfo; } foreach ($priceTypeIds as $priceTypeId) { $priceTypeIdsToValues[$priceTypeId] = (int) $findPrices['PRICE_'.$priceTypeId] ?? 0; } return $priceTypeIdsToValues; } protected function getPropertyOrPriceIdsFromTemplate($template, $prefix) { preg_match_all('/\{'.$prefix.'(\d+)\}/', $template, $matches); $findIds = $matches[1] ?? []; return $findIds; } protected function calcByFormula($formula, $marks) { $allowedMarks = array_keys($marks); $this->Formula->setMarks($allowedMarks); $this->Formula->setFormula($formula); $price = $this->Formula->calc($marks); return $price; } }