Exemplo n.º 1
0
 public static function SaveRows($ownerType, $ownerID, $arRows, $accountContext = null, $checkPerms = true, $regEvent = true, $syncOwner = true, $totalInfo = array())
 {
     $ownerType = strval($ownerType);
     $ownerID = intval($ownerID);
     if (!isset($ownerType[0]) || $ownerID <= 0 || !is_array($arRows)) {
         self::RegisterError('Invalid arguments are supplied.');
         return false;
     }
     if (!is_array($totalInfo)) {
         $totalInfo = array();
     }
     $owner = null;
     if (!is_array($accountContext)) {
         if ($ownerType === 'D') {
             $owner = CCrmDeal::GetByID($ownerID, $checkPerms);
         } elseif ($ownerType === CCrmQuote::OWNER_TYPE) {
             $owner = CCrmQuote::GetByID($ownerID);
         } elseif ($ownerType === 'L') {
             $owner = CCrmLead::GetByID($ownerID);
         }
     }
     // Preparing accounting context -->
     if (!is_array($accountContext)) {
         $accountContext = array();
         if (is_array($owner)) {
             if (isset($owner['CURRENCY_ID'])) {
                 $accountContext['CURRENCY_ID'] = $owner['CURRENCY_ID'];
             }
             if (isset($owner['EXCH_RATE'])) {
                 $accountContext['EXCH_RATE'] = $owner['EXCH_RATE'];
             }
         }
     }
     $currencyID = isset($accountContext['CURRENCY_ID']) ? $accountContext['CURRENCY_ID'] : CCrmCurrency::GetBaseCurrencyID();
     $exchRate = isset($accountContext['EXCH_RATE']) ? $accountContext['EXCH_RATE'] : null;
     // <-- Preparing accounting context
     $productIDs = array();
     $products = array();
     foreach ($arRows as &$arRow) {
         $productID = isset($arRow['PRODUCT_ID']) ? intval($arRow['PRODUCT_ID']) : 0;
         if ($productID > 0 && !in_array($productID, $productIDs, true)) {
             $productIDs[] = $productID;
         }
     }
     unset($arRow);
     if (!empty($productIDs)) {
         $dbProduct = CCrmProduct::GetList(array(), array('ID' => $productIDs), array('ID', 'NAME'));
         if (is_object($dbProduct)) {
             while ($product = $dbProduct->Fetch()) {
                 $products[intval($product['ID'])] = $product;
             }
         }
     }
     $measurelessProductIDs = array();
     $arSafeRows = array();
     foreach ($arRows as &$arRow) {
         //$rowID = isset($arRow['ID']) ? intval($arRow['ID']) : 0;
         $productID = $arRow['PRODUCT_ID'] = isset($arRow['PRODUCT_ID']) ? intval($arRow['PRODUCT_ID']) : 0;
         $productName = $arRow['PRODUCT_NAME'] = isset($arRow['PRODUCT_NAME']) ? $arRow['PRODUCT_NAME'] : '';
         $arRow['PRICE'] = isset($arRow['PRICE']) ? round(doubleval($arRow['PRICE']), 2) : 0.0;
         $arRow['QUANTITY'] = isset($arRow['QUANTITY']) ? round(doubleval($arRow['QUANTITY']), 4) : 1;
         $arRow['TAX_RATE'] = isset($arRow['TAX_RATE']) ? round(doubleval($arRow['TAX_RATE']), 2) : 0.0;
         $arRow['TAX_INCLUDED'] = isset($arRow['TAX_INCLUDED']) ? $arRow['TAX_INCLUDED'] === 'Y' ? 'Y' : 'N' : 'N';
         $arRow['MEASURE_CODE'] = isset($arRow['MEASURE_CODE']) ? intval($arRow['MEASURE_CODE']) : 0;
         $arRow['MEASURE_NAME'] = isset($arRow['MEASURE_NAME']) ? $arRow['MEASURE_NAME'] : '';
         $arRow['CUSTOMIZED'] = isset($arRow['CUSTOMIZED']) && strtoupper($arRow['CUSTOMIZED']) === 'Y' ? 'Y' : 'N';
         $arRow['SORT'] = isset($arRow['SORT']) ? intval($arRow['SORT']) : 0;
         if ($productID > 0 && $productName !== '' && isset($products[$productID])) {
             //Skip save product name if it equal to original
             $product = $products[$productID];
             $originalProductName = isset($product['NAME']) ? trim($product['NAME']) : '';
             if ($productName === $originalProductName) {
                 $productName = '';
             }
         }
         $price = $arRow['PRICE'];
         $arRow['DISCOUNT_TYPE_ID'] = isset($arRow['DISCOUNT_TYPE_ID']) ? intval($arRow['DISCOUNT_TYPE_ID']) : 0;
         if (!\Bitrix\Crm\Discount::isDefined($arRow['DISCOUNT_TYPE_ID'])) {
             $arRow['DISCOUNT_TYPE_ID'] = \Bitrix\Crm\Discount::PERCENTAGE;
             $arRow['DISCOUNT_RATE'] = 0.0;
         }
         $discountTypeID = $arRow['DISCOUNT_TYPE_ID'];
         if ($discountTypeID === \Bitrix\Crm\Discount::PERCENTAGE) {
             if (!isset($arRow['DISCOUNT_RATE'])) {
                 self::RegisterError("Discount Rate (DISCOUNT_RATE) is required if Percentage Discount Type (DISCOUNT_TYPE_ID) is defined.");
                 return false;
             }
             $discountRate = round(doubleval($arRow['DISCOUNT_RATE']), 2);
             if (isset($arRow['DISCOUNT_SUM'])) {
                 $discountSum = round(doubleval($arRow['DISCOUNT_SUM']), 2);
             } else {
                 $exclusivePrice = self::CalculateExclusivePrice($price, $arRow['TAX_RATE']);
                 $discountSum = round(\Bitrix\Crm\Discount::calculateDiscountSum($exclusivePrice, $discountRate), 2);
             }
         } else {
             if (!isset($arRow['DISCOUNT_SUM'])) {
                 self::RegisterError("Discount Sum (DISCOUNT_SUM) is required if Monetary Discount Type (DISCOUNT_TYPE_ID) is defined.");
                 return false;
             }
             $discountSum = round(doubleval($arRow['DISCOUNT_SUM']), 2);
             if (isset($arRow['DISCOUNT_RATE'])) {
                 $discountRate = round(doubleval($arRow['DISCOUNT_RATE']), 2);
             } else {
                 $exclusivePrice = self::CalculateExclusivePrice($price, $arRow['TAX_RATE']);
                 $discountRate = \Bitrix\Crm\Discount::calculateDiscountRate($exclusivePrice + $discountSum, $exclusivePrice);
             }
         }
         $measureCode = $arRow['MEASURE_CODE'];
         if ($productID > 0 && $measureCode <= 0) {
             if (!in_array($productID, $measurelessProductIDs, true)) {
                 $measurelessProductIDs[] = $productID;
             }
         }
         $safeRow = array('OWNER_TYPE' => $ownerType, 'OWNER_ID' => $ownerID, 'PRODUCT_ID' => $productID, 'PRODUCT_NAME' => $productName, 'PRICE' => $price, 'QUANTITY' => $arRow['QUANTITY'], 'DISCOUNT_TYPE_ID' => $discountTypeID, 'DISCOUNT_SUM' => $discountSum, 'DISCOUNT_RATE' => $discountRate, 'TAX_RATE' => $arRow['TAX_RATE'], 'TAX_INCLUDED' => isset($arRow['TAX_INCLUDED']) ? $arRow['TAX_INCLUDED'] === 'Y' ? 'Y' : 'N' : 'N', 'MEASURE_CODE' => $measureCode, 'MEASURE_NAME' => $arRow['MEASURE_NAME'], 'CUSTOMIZED' => 'Y', 'SORT' => $arRow['SORT']);
         $accData = CCrmAccountingHelper::PrepareAccountingData(array('CURRENCY_ID' => $currencyID, 'SUM' => $safeRow['PRICE'], 'EXCH_RATE' => $exchRate));
         if (is_array($accData)) {
             $safeRow['PRICE_ACCOUNT'] = $accData['ACCOUNT_SUM'];
         }
         $arSafeRows[] =& $safeRow;
         unset($safeRow);
     }
     unset($arRow);
     if (!empty($measurelessProductIDs)) {
         $defaultMeasureInfo = \Bitrix\Crm\Measure::getDefaultMeasure();
         $measureInfos = \Bitrix\Crm\Measure::getProductMeasures($measurelessProductIDs);
         foreach ($arSafeRows as &$safeRow) {
             if ($safeRow['MEASURE_CODE'] > 0) {
                 continue;
             }
             $productID = $safeRow['PRODUCT_ID'];
             if (isset($measureInfos[$productID]) && !empty($measureInfos[$productID])) {
                 $measureInfo = $measureInfos[$productID][0];
                 $safeRow['MEASURE_CODE'] = $measureInfo['CODE'];
                 $safeRow['MEASURE_NAME'] = $measureInfo['SYMBOL'];
             } elseif ($defaultMeasureInfo !== null) {
                 $safeRow['MEASURE_CODE'] = $defaultMeasureInfo['CODE'];
                 $safeRow['MEASURE_NAME'] = $defaultMeasureInfo['SYMBOL'];
             }
         }
         unset($safeRow);
     }
     $arPresentRows = self::LoadRows($ownerType, $ownerID, true);
     // Registering events -->
     if ($regEvent) {
         $arRowIDs = array();
         foreach ($arRows as &$arRow) {
             if (isset($arRow['ID'])) {
                 $arRowIDs[] = intval($arRow['ID']);
             }
             $rowID = isset($arRow['ID']) ? intval($arRow['ID']) : 0;
             if ($rowID <= 0) {
                 // Row was added
                 self::RegisterAddEvent($ownerType, $ownerID, $arRow, $checkPerms);
                 continue;
             }
             $arPresentRow = isset($arPresentRows[$rowID]) ? $arPresentRows[$rowID] : null;
             if ($arPresentRow) {
                 // Row was modified
                 self::RegisterUpdateEvent($ownerType, $ownerID, $arRow, $arPresentRow, $checkPerms);
             }
         }
         unset($arRow);
         foreach ($arPresentRows as $rowID => &$arPresentRow) {
             if (!in_array($rowID, $arRowIDs, true)) {
                 // Product  was removed
                 self::RegisterRemoveEvent($ownerType, $ownerID, $arPresentRow, $checkPerms);
             }
         }
     }
     // <-- Registering events
     $result = CCrmProductRow::DoSaveRows($ownerType, $ownerID, $arSafeRows);
     // Update list of taxes
     if (!isset($totalInfo['CURRENCY_ID'])) {
         $totalInfo['CURRENCY_ID'] = $currencyID;
     }
     self::UpdateTotalInfo($ownerType, $ownerID, $totalInfo);
     // Disable sum synchronization if product rows are empty
     if ($result && $syncOwner && (count($arPresentRows) > 0 || count($arSafeRows) > 0)) {
         self::SynchronizeOwner($ownerType, $ownerID, $checkPerms, $totalInfo);
     }
     return $result;
 }