示例#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;
 }
示例#2
0
 if ($arResult['INVOICE_MODE']) {
     $row['ID'] = isset($row['ID']) ? intval($row['ID']) : 0;
     $row['OWNER_ID'] = $row['ORDER_ID'];
     $row['OWNER_TYPE'] = $ownerType;
     $row['PRODUCT_ID'] = isset($row['PRODUCT_ID']) ? intval($row['PRODUCT_ID']) : 0;
     $row['PRODUCT_NAME'] = isset($row['PRODUCT_NAME']) ? strval($row['PRODUCT_NAME']) : '';
     $row['ORIGINAL_PRODUCT_NAME'] = $row['PRODUCT_NAME'];
     $row['PRICE'] = round(doubleval($row['PRICE']), 2);
     $row['QUANTITY'] = round(doubleval($row['QUANTITY']), 2);
     $row['CUSTOMIZED'] = $row['CUSTOM_PRICE'] === 'Y' ? 'Y' : 'N';
     $row['TAX_RATE'] = round(doubleval($row['VAT_RATE']), 2) * 100;
     $row['TAX_INCLUDED'] = 'Y';
     $row['DISCOUNT_TYPE_ID'] = \Bitrix\Crm\Discount::MONETARY;
     $exclusivePrice = CCrmProductRow::CalculateExclusivePrice($row['PRICE'], $row['TAX_RATE']);
     $row['DISCOUNT_SUM'] = round(doubleval($row['DISCOUNT_PRICE']), 2);
     $row['DISCOUNT_RATE'] = \Bitrix\Crm\Discount::calculateDiscountRate($exclusivePrice + $row['DISCOUNT_SUM'], $exclusivePrice);
     $ary['MEASURE_CODE'] = isset($row['MEASURE_CODE']) ? intval($row['MEASURE_CODE']) : 0;
     $ary['MEASURE_NAME'] = isset($row['MEASURE_NAME']) ? $row['MEASURE_NAME'] : '';
     unset($row['ORDER_ID'], $row['NAME'], $row['DISCOUNT_PRICE'], $row['VAT_RATE'], $row['CUSTOM_PRICE']);
 }
 if ($arResult['ALLOW_LD_TAX']) {
     $row['PRICE'] = CCrmProductRow::CalculateExclusivePrice($row['PRICE'], $row['TAX_RATE']);
     $row['TAX_RATE'] = 0.0;
     $row['TAX_INCLUDED'] = 'N';
 }
 $exclusivePrice = CCrmProductRow::CalculateExclusivePrice($row['PRICE'], $row['TAX_RATE']);
 $discountTypeID = intval($row['DISCOUNT_TYPE_ID']);
 if ($discountTypeID === \Bitrix\Crm\Discount::MONETARY) {
     $row['PRICE_NETTO'] = $exclusivePrice + $row['DISCOUNT_SUM'];
 } else {
     $discoutRate = doubleval($row['DISCOUNT_RATE']);
示例#3
0
 protected function innerGetList($order, $filter, $select, $navigation, &$errors)
 {
     $ownerID = isset($filter['OWNER_ID']) ? intval($filter['OWNER_ID']) : 0;
     $ownerType = isset($filter['OWNER_TYPE']) ? $filter['OWNER_TYPE'] : '';
     if ($ownerID <= 0 || $ownerType === '') {
         if ($ownerID <= 0) {
             $errors[] = 'The field OWNER_ID is required in filer.';
         }
         if ($ownerType === '') {
             $errors[] = 'The field OWNER_TYPE is required in filer.';
         }
         return false;
     }
     if ($ownerType === 'I') {
         //Crutch for Invoices
         if (!CCrmInvoice::CheckReadPermission($ownerID)) {
             $errors[] = 'Access denied.';
             return false;
         }
         $result = array();
         $productRows = CCrmInvoice::GetProductRows($ownerID);
         foreach ($productRows as $productRow) {
             $price = isset($productRow['PRICE']) ? $productRow['PRICE'] : 0.0;
             $discountSum = isset($productRow['DISCOUNT_PRICE']) ? $productRow['DISCOUNT_PRICE'] : 0.0;
             $taxRate = isset($productRow['VAT_RATE']) ? $productRow['VAT_RATE'] * 100 : 0.0;
             $exclusivePrice = CCrmProductRow::CalculateExclusivePrice($price, $taxRate);
             $discountRate = \Bitrix\Crm\Discount::calculateDiscountRate($exclusivePrice + $discountSum, $exclusivePrice);
             $result[] = array('ID' => $productRow['ID'], 'OWNER_ID' => $ownerID, 'OWNER_TYPE' => 'I', 'PRODUCT_ID' => isset($productRow['PRODUCT_ID']) ? $productRow['PRODUCT_ID'] : 0, 'PRODUCT_NAME' => isset($productRow['PRODUCT_NAME']) ? $productRow['PRODUCT_NAME'] : '', 'PRICE' => $price, 'QUANTITY' => isset($productRow['QUANTITY']) ? $productRow['QUANTITY'] : 0, 'DISCOUNT_TYPE_ID' => \Bitrix\Crm\Discount::MONETARY, 'DISCOUNT_RATE' => $discountRate, 'DISCOUNT_SUM' => $discountSum, 'TAX_RATE' => $taxRate, 'TAX_INCLUDED' => 'Y', 'MEASURE_CODE' => isset($productRow['MEASURE_CODE']) ? $productRow['MEASURE_CODE'] : '', 'MEASURE_NAME' => isset($productRow['MEASURE_NAME']) ? $productRow['MEASURE_NAME'] : '', 'CUSTOMIZED' => isset($productRow['CUSTOM_PRICE']) ? $productRow['CUSTOM_PRICE'] : 'N');
         }
         return $result;
     }
     if (!CCrmAuthorizationHelper::CheckReadPermission(CCrmProductRow::ResolveOwnerTypeName($ownerType), $ownerID)) {
         $errors[] = 'Access denied.';
         return false;
     }
     return CCrmProductRow::GetList($order, $filter, false, $navigation, $select, array('IS_EXTERNAL_CONTEXT' => true));
 }