Пример #1
0
 public function executePhase()
 {
     if ($this->currentPhase === QuoteConversionPhase::DEAL_CREATION || $this->currentPhase === QuoteConversionPhase::INVOICE_CREATION) {
         if ($this->currentPhase === QuoteConversionPhase::DEAL_CREATION) {
             $entityTypeID = \CCrmOwnerType::Deal;
         } else {
             $entityTypeID = \CCrmOwnerType::Invoice;
         }
         $entityTypeName = \CCrmOwnerType::ResolveName($entityTypeID);
         $config = $this->config->getItem($entityTypeID);
         if (!$config->isActive()) {
             return false;
         }
         /** @var \CCrmPerms $permissions */
         $permissions = $this->getUserPermissions();
         $entityID = isset($this->contextData[$entityTypeName]) ? $this->contextData[$entityTypeName] : 0;
         if ($entityID > 0) {
             if ($entityTypeID === \CCrmOwnerType::Deal) {
                 if (!\CCrmDeal::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Quote, \CCrmOwnerType::Deal, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 $this->resultData[$entityTypeName] = $entityID;
             } else {
                 if (!\CCrmInvoice::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Quote, \CCrmOwnerType::Invoice, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 if (!\CCrmInvoice::CheckUpdatePermission($entityID, $permissions)) {
                     throw new EntityConversionException(\CCrmOwnerType::Quote, \CCrmOwnerType::Invoice, EntityConversionException::TARG_DST, EntityConversionException::UPDATE_DENIED);
                 }
                 $entity = new \CCrmInvoice(false);
                 $fields = array('UF_QUOTE_ID' => $this->entityID);
                 try {
                     $entity->Update($entityID, $fields);
                 } catch (Main\DB\SqlQueryException $e) {
                 }
                 $this->resultData[$entityTypeName] = $entityID;
             }
             return true;
         }
         if (!\CCrmAuthorizationHelper::CheckCreatePermission($entityTypeName, $permissions)) {
             throw new EntityConversionException(\CCrmOwnerType::Quote, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::CREATE_DENIED);
         }
         if (UserFieldSynchronizer::needForSynchronization(\CCrmOwnerType::Quote, $entityTypeID)) {
             throw new EntityConversionException(\CCrmOwnerType::Quote, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::NOT_SYNCHRONIZED);
         }
         if (!ConversionSettings::getCurrent()->isAutocreationEnabled()) {
             throw new EntityConversionException(\CCrmOwnerType::Quote, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::AUTOCREATION_DISABLED);
         }
         if ($entityTypeID === \CCrmOwnerType::Deal && \CCrmBizProcHelper::HasAutoWorkflows($entityTypeID, \CCrmBizProcEventType::Create)) {
             throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::HAS_WORKFLOWS);
         }
         /** @var QuoteConversionMapper $mapper */
         $mapper = $this->getMapper();
         $map = self::prepareMap($entityTypeID);
         $fields = $mapper->map($map);
         if (empty($fields)) {
             throw new EntityConversionException(\CCrmOwnerType::Quote, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::EMPTY_FIELDS);
         }
         if ($entityTypeID === \CCrmOwnerType::Deal) {
             $entity = new \CCrmDeal(false);
             if (!$entity->CheckFields($fields)) {
                 throw new EntityConversionException(\CCrmOwnerType::Quote, \CCrmOwnerType::Deal, EntityConversionException::TARG_DST, EntityConversionException::INVALID_FIELDS, $entity->LAST_ERROR);
             }
             $productRows = isset($fields['PRODUCT_ROWS']) && is_array($fields['PRODUCT_ROWS']) ? $fields['PRODUCT_ROWS'] : array();
             if (!empty($productRows)) {
                 $result = \CCrmProductRow::CalculateTotalInfo('D', 0, false, $fields, $productRows);
                 $fields['OPPORTUNITY'] = isset($result['OPPORTUNITY']) ? $result['OPPORTUNITY'] : 1.0;
                 $fields['TAX_VALUE'] = isset($result['TAX_VALUE']) ? $result['TAX_VALUE'] : 0.0;
             }
             $entityID = $entity->Add($fields);
             if ($entityID <= 0) {
                 throw new EntityConversionException(\CCrmOwnerType::Quote, \CCrmOwnerType::Deal, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR);
             }
             if (!empty($productRows)) {
                 \CCrmDeal::SaveProductRows($entityID, $productRows, false, false, false);
             }
             $this->resultData[\CCrmOwnerType::DealName] = $entityID;
         } else {
             $entity = new \CCrmInvoice(false);
             if (!$entity->CheckFields($fields)) {
                 throw new EntityConversionException(\CCrmOwnerType::Quote, \CCrmOwnerType::Invoice, EntityConversionException::TARG_DST, EntityConversionException::INVALID_FIELDS, $entity->LAST_ERROR);
             }
             $entityID = $entity->Add($fields);
             if ($entityID <= 0) {
                 throw new EntityConversionException(\CCrmOwnerType::Quote, \CCrmOwnerType::Invoice, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR);
             }
             $this->resultData[\CCrmOwnerType::InvoiceName] = $entityID;
         }
         return true;
     } elseif ($this->currentPhase === DealConversionPhase::FINALIZATION) {
         $fields = array();
         if (isset($this->resultData[\CCrmOwnerType::DealName])) {
             $fields['DEAL_ID'] = $this->resultData[\CCrmOwnerType::DealName];
         }
         if (!empty($fields)) {
             $entity = new \CCrmQuote(false);
             $entity->Update($this->entityID, $fields);
         }
         return true;
     }
     return false;
 }
Пример #2
0
 public function executePhase()
 {
     if ($this->currentPhase === DealConversionPhase::INVOICE_CREATION || $this->currentPhase === DealConversionPhase::QUOTE_CREATION) {
         if ($this->currentPhase === DealConversionPhase::INVOICE_CREATION) {
             $entityTypeID = \CCrmOwnerType::Invoice;
         } else {
             $entityTypeID = \CCrmOwnerType::Quote;
         }
         $entityTypeName = \CCrmOwnerType::ResolveName($entityTypeID);
         $config = $this->config->getItem($entityTypeID);
         if (!$config->isActive()) {
             return false;
         }
         /** @var \CCrmPerms $permissions */
         $permissions = $this->getUserPermissions();
         $entityID = isset($this->contextData[$entityTypeName]) ? $this->contextData[$entityTypeName] : 0;
         if ($entityID > 0) {
             if ($entityTypeID === \CCrmOwnerType::Invoice) {
                 if (!\CCrmInvoice::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Deal, \CCrmOwnerType::Invoice, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 if (!\CCrmInvoice::CheckUpdatePermission($entityID, $permissions)) {
                     throw new EntityConversionException(\CCrmOwnerType::Deal, \CCrmOwnerType::Invoice, EntityConversionException::TARG_DST, EntityConversionException::UPDATE_DENIED);
                 }
                 $entity = new \CCrmInvoice(false);
                 $fields = array('UF_DEAL_ID' => $this->entityID);
                 try {
                     $entity->Update($entityID, $fields);
                 } catch (Main\DB\SqlQueryException $e) {
                 }
                 $this->resultData[$entityTypeName] = $entityID;
             } else {
                 if (!\CCrmQuote::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Deal, \CCrmOwnerType::Quote, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 if (!\CCrmQuote::CheckUpdatePermission($entityID, $permissions)) {
                     throw new EntityConversionException(\CCrmOwnerType::Deal, \CCrmOwnerType::Quote, EntityConversionException::TARG_DST, EntityConversionException::UPDATE_DENIED);
                 }
                 $entity = new \CCrmQuote(false);
                 $fields = array('DEAL_ID' => $this->entityID);
                 $entity->Update($entityID, $fields);
                 $this->resultData[$entityTypeName] = $entityID;
             }
             return true;
         }
         if (!\CCrmAuthorizationHelper::CheckCreatePermission($entityTypeName, $permissions)) {
             throw new EntityConversionException(\CCrmOwnerType::Deal, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::CREATE_DENIED);
         }
         if (UserFieldSynchronizer::needForSynchronization(\CCrmOwnerType::Deal, $entityTypeID)) {
             throw new EntityConversionException(\CCrmOwnerType::Deal, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::NOT_SYNCHRONIZED);
         }
         if (!ConversionSettings::getCurrent()->isAutocreationEnabled()) {
             throw new EntityConversionException(\CCrmOwnerType::Deal, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::AUTOCREATION_DISABLED);
         }
         /** @var DealConversionMapper $mapper */
         $mapper = $this->getMapper();
         $map = self::prepareMap($entityTypeID);
         $fields = $mapper->map($map);
         if (empty($fields)) {
             throw new EntityConversionException(\CCrmOwnerType::Deal, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::EMPTY_FIELDS);
         }
         if ($entityTypeID === \CCrmOwnerType::Invoice) {
             $entity = new \CCrmInvoice(false);
             if (!$entity->CheckFields($fields)) {
                 throw new EntityConversionException(\CCrmOwnerType::Deal, \CCrmOwnerType::Invoice, EntityConversionException::TARG_DST, EntityConversionException::INVALID_FIELDS, $entity->LAST_ERROR);
             }
             $entityID = $entity->Add($fields);
             if ($entityID <= 0) {
                 throw new EntityConversionException(\CCrmOwnerType::Deal, \CCrmOwnerType::Invoice, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR);
             }
             $this->resultData[\CCrmOwnerType::InvoiceName] = $entityID;
         } else {
             $entity = new \CCrmQuote(false);
             if (!$entity->CheckFields($fields)) {
                 throw new EntityConversionException(\CCrmOwnerType::Deal, \CCrmOwnerType::Quote, EntityConversionException::TARG_DST, EntityConversionException::INVALID_FIELDS, $entity->LAST_ERROR);
             }
             $productRows = isset($fields['PRODUCT_ROWS']) && is_array($fields['PRODUCT_ROWS']) ? $fields['PRODUCT_ROWS'] : array();
             if (!empty($productRows)) {
                 $currencyID = isset($fields['CURRENCY_ID']) ? $fields['CURRENCY_ID'] : '';
                 $personTypes = \CCrmPaySystem::getPersonTypeIDs();
                 $personTypeID = 0;
                 if (isset($personTypes['COMPANY']) && isset($personTypes['CONTACT'])) {
                     if (isset($fields['COMPANY_ID']) && $fields['COMPANY_ID'] > 0) {
                         $personTypeID = $personTypes['COMPANY'];
                     } elseif (isset($fields['CONTACT_ID']) && $fields['CONTACT_ID'] > 0) {
                         $personTypeID = $personTypes['CONTACT'];
                     }
                 }
                 if ($currencyID !== '' && $personTypeID > 0) {
                     $calculationOptions = array();
                     if (\CCrmTax::isTaxMode() && isset($fields['LOCATION_ID'])) {
                         $calculationOptions['LOCATION_ID'] = $fields['LOCATION_ID'];
                     }
                     $result = \CCrmSaleHelper::Calculate($productRows, $currencyID, $personTypeID, false, SITE_ID, $calculationOptions);
                     $arFields['OPPORTUNITY'] = isset($result['PRICE']) ? round(doubleval($result['PRICE']), 2) : 1.0;
                     $arFields['TAX_VALUE'] = isset($result['TAX_VALUE']) ? round(doubleval($result['TAX_VALUE']), 2) : 0.0;
                 }
             }
             $entityID = $entity->Add($fields);
             if ($entityID <= 0) {
                 throw new EntityConversionException(\CCrmOwnerType::Deal, \CCrmOwnerType::Quote, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR);
             }
             $this->resultData[\CCrmOwnerType::QuoteName] = $entityID;
         }
         return true;
     } elseif ($this->currentPhase === DealConversionPhase::FINALIZATION) {
         return true;
     }
     return false;
 }
Пример #3
0
             foreach ($actionData['FIELDS'] as $ID => $arSrcData) {
                 $arEntityAttr = $CCrmPerms->GetEntityAttr('INVOICE', array($ID));
                 if (!$CCrmPerms->CheckEnityAccess('INVOICE', 'WRITE', $arEntityAttr[$ID])) {
                     continue;
                 }
                 $arUpdateData = array();
                 reset($arResult['HEADERS']);
                 foreach ($arResult['HEADERS'] as $arHead) {
                     if (isset($arHead['editable']) && $arHead['editable'] == true && isset($arSrcData[$arHead['id']])) {
                         $arUpdateData[$arHead['id']] = $arSrcData[$arHead['id']];
                     }
                 }
                 if (!empty($arUpdateData)) {
                     if ($CCrmInvoice->CheckFieldsUpdate($arUpdateData)) {
                         $DB->StartTransaction();
                         if ($CCrmInvoice->Update($ID, $arUpdateData, array('REGISTER_SONET_EVENT' => true, 'UPDATE_SEARCH' => true))) {
                             $DB->Commit();
                         } else {
                             $DB->Rollback();
                         }
                     }
                 }
             }
         }
     }
     if (!$actionData['AJAX_CALL']) {
         LocalRedirect($arParams['PATH_TO_INVOICE_LIST']);
     }
 } else {
     if ($actionData['NAME'] == 'delete' && isset($actionData['ID'])) {
         $ID = intval($actionData['ID']);
Пример #4
0
            $fieldNames[] = $_POST['FIELD_NAME'];
        }
    }
    if (count($fieldNames) == 0) {
        __CrmInvoiceShowEndJsonResonse(array('ERROR' => 'FIELD_NAME IS NOT DEFINED!'));
    }
    $fieldValues = array();
    if (isset($_POST['FIELD_VALUE'])) {
        if (is_array($_POST['FIELD_VALUE'])) {
            $fieldValues = $_POST['FIELD_VALUE'];
        } else {
            $fieldValues[] = $_POST['FIELD_VALUE'];
        }
    }
    $arFields = CCrmInvoice::GetByID($ID);
    if (is_array($arFields)) {
        CCrmInstantEditorHelper::PrepareUpdate(CCrmOwnerType::Invoice, $arFields, $fieldNames, $fieldValues);
        // check fields
        if (!$CCrmInvoice->CheckFieldsUpdate($arFields, $ID)) {
            $errMsg = '';
            if (!empty($CCrmInvoice->LAST_ERROR)) {
                $errMsg .= $CCrmInvoice->LAST_ERROR;
            } else {
                $errMsg .= GetMessage('UNKNOWN_ERROR');
            }
            __CrmInvoiceShowEndJsonResonse(array('ERROR' => $errMsg));
        }
        $CCrmInvoice->Update($ID, $arFields, array('REGISTER_SONET_EVENT' => true, 'UPDATE_SEARCH' => true));
    }
}
die;
Пример #5
0
     		}*/
 }
 //<-- INVOICE_PROPERTIES
 $entity = new CCrmInvoice(false);
 if (!$entity->CheckFields($arFields, !$isNew ? $ID : false, $isSuccessfull, $isFailed)) {
     if ($entity->LAST_ERROR !== '') {
         $errorText = preg_replace('/<br\\s*\\/>/', "\n", $entity->LAST_ERROR);
         __CrmMobileInvoiceEditEndResonse(array('ERROR' => $errorText));
     } else {
         __CrmMobileInvoiceEditEndResonse(array('ERROR' => GetMessage('CRM_INVOICE_FIELD_CHECK_GENERAL_ERROR')));
     }
 }
 //$DB->StartTransaction();
 $successed = false;
 if (!$isNew) {
     $successed = $entity->Update($ID, $arFields, array('UPDATE_SEARCH' => true));
 } else {
     $recalculate = false;
     $ID = $entity->Add($arFields, $recalculate, SITE_ID, array('UPDATE_SEARCH' => true));
     $successed = is_int($ID) && $ID > 0;
 }
 if ($successed) {
     //$DB->Commit();
     $dbRes = CCrmInvoice::GetList(array(), array('ID' => $ID, 'CHECK_PERMISSIONS' => 'N'));
     $currentItem = $dbRes->GetNext();
     $formatParams = isset($_REQUEST['FORMAT_PARAMS']) ? $_REQUEST['FORMAT_PARAMS'] : array();
     CCrmMobileHelper::PrepareInvoiceItem($currentItem, $formatParams, array('PAY_SYSTEMS' => $paySystems), array('ENABLE_MULTI_FIELDS' => true, 'ENABLE_PAYER_INFO' => true, 'ENABLE_LOCATION' => true));
     __CrmMobileInvoiceEditEndResonse(array('SAVED_ENTITY_ID' => $ID, 'SAVED_ENTITY_DATA' => CCrmMobileHelper::PrepareInvoiceData($currentItem)));
 } else {
     //$DB->Rollback();
     $errorText = '';
Пример #6
0
                echo CUtil::PhpToJSObject($arResponse);
                ?>
;
					top.<?php 
                echo CUtil::JSEscape($arResult['FORM_ID'] . '_ajax_response');
                ?>
 = response;
				</script>
				<?php 
                exit;
            }
            if (empty($arResult['ERROR_MESSAGE'])) {
                $DB->StartTransaction();
                $bSuccess = false;
                if ($bEdit) {
                    $bSuccess = $CCrmInvoice->Update($arResult['ELEMENT']['ID'], $arFields, array('REGISTER_SONET_EVENT' => true, 'UPDATE_SEARCH' => true));
                } else {
                    $recalculate = false;
                    $ID = $CCrmInvoice->Add($arFields, $recalculate, SITE_ID, array('REGISTER_SONET_EVENT' => true, 'UPDATE_SEARCH' => true));
                    $bSuccess = intval($ID) > 0 ? true : false;
                    if ($bSuccess) {
                        $arResult['ELEMENT']['ID'] = $ID;
                    }
                }
                if ($bSuccess) {
                    // Save settings
                    if (is_array($productRowSettings) && count($productRowSettings) > 0) {
                        $arSettings = CCrmProductRow::LoadSettings('I', $arResult['ELEMENT']['ID']);
                        foreach ($productRowSettings as $k => $v) {
                            $arSettings[$k] = $v;
                        }
Пример #7
0
 public static function update($params)
 {
     global $DB;
     $ID = CCrmInvoiceRestUtil::getParamScalar($params, 'id', 0);
     if ($ID <= 0) {
         throw new RestException('Invalid identifier.');
     }
     $invoice = new CCrmInvoice();
     if (!CCrmInvoice::CheckUpdatePermission($ID)) {
         throw new RestException('Access denied.');
     }
     $fields = CCrmInvoiceRestUtil::getParamArray($params, 'fields');
     $fields = self::filterFields($fields, 'update');
     // sanitize
     $updateComments = isset($fields['COMMENTS']);
     $updateUserDescription = isset($fields['USER_DESCRIPTION']);
     $comments = $updateComments ? trim($fields['COMMENTS']) : '';
     $userDescription = $updateUserDescription ? trim($fields['USER_DESCRIPTION']) : '';
     $bSanitizeComments = $comments !== '' && strpos($comments, '<');
     $bSanitizeUserDescription = $userDescription !== '' && strpos($userDescription, '<');
     if ($bSanitizeComments || $bSanitizeUserDescription) {
         $sanitizer = new CBXSanitizer();
         $sanitizer->ApplyDoubleEncode(false);
         $sanitizer->SetLevel(CBXSanitizer::SECURE_LEVEL_MIDDLE);
         //Crutch for for Chrome line break behaviour in HTML editor.
         $sanitizer->AddTags(array('div' => array()));
         if ($bSanitizeComments) {
             $fields['COMMENTS'] = $sanitizer->SanitizeHtml($fields['COMMENTS']);
         }
         if ($bSanitizeUserDescription) {
             $fields['USER_DESCRIPTION'] = $sanitizer->SanitizeHtml($fields['USER_DESCRIPTION']);
         }
         unset($sanitizer);
     }
     unset($bSanitizeComments, $bSanitizeUserDescription);
     if ($updateComments) {
         $fields['COMMENTS'] = $comments;
     }
     if ($updateUserDescription) {
         $fields['USER_DESCRIPTION'] = $userDescription;
     }
     unset($updateComments, $updateUserDescription, $comments, $userDescription);
     if (!is_array($fields) || count($fields) === 0) {
         throw new RestException('Invalid parameters.');
     }
     $origFields = self::getInvoiceDataByID($ID);
     $origFields = self::filterFields($origFields, 'update');
     foreach ($origFields as $fName => $fValue) {
         if (!array_key_exists($fName, $fields)) {
             $fields[$fName] = $fValue;
         }
     }
     $bStatusSuccess = CCrmStatusInvoice::isStatusSuccess($fields['STATUS_ID']);
     if ($bStatusSuccess) {
         $bStatusFailed = false;
     } else {
         $bStatusFailed = CCrmStatusInvoice::isStatusFailed($fields['STATUS_ID']);
     }
     if (!$invoice->CheckFields($fields, false, $bStatusSuccess, $bStatusFailed)) {
         if (!empty($invoice->LAST_ERROR)) {
             throw new RestException($invoice->LAST_ERROR);
         } else {
             throw new RestException('Error on check fields.');
         }
     }
     $propsInfo = CCrmInvoice::GetPropertiesInfo($fields['PERSON_TYPE_ID']);
     $propsInfo = is_array($propsInfo[$fields['PERSON_TYPE_ID']]) ? $propsInfo[$fields['PERSON_TYPE_ID']] : array();
     $invoiceProperties = array();
     foreach ($propsInfo as $propCode => $arProp) {
         if (array_key_exists($propCode, $fields['INVOICE_PROPERTIES'])) {
             $invoiceProperties[$arProp['ID']] = $fields['INVOICE_PROPERTIES'][$propCode];
         } else {
             if ($propCode === 'COMPANY_NAME' && array_key_exists('COMPANY', $fields['INVOICE_PROPERTIES'])) {
                 $invoiceProperties[$arProp['ID']] = $fields['INVOICE_PROPERTIES']['COMPANY'];
             } else {
                 if (is_array($origFields['INVOICE_PROPERTIES'])) {
                     if (array_key_exists($propCode, $origFields['INVOICE_PROPERTIES'])) {
                         $invoiceProperties[$arProp['ID']] = $origFields['INVOICE_PROPERTIES'][$propCode];
                     } else {
                         if ($propCode === 'COMPANY_NAME' && array_key_exists('COMPANY', $fields['INVOICE_PROPERTIES'])) {
                             $invoiceProperties[$arProp['ID']] = $origFields['INVOICE_PROPERTIES']['COMPANY'];
                         }
                     }
                 }
             }
         }
     }
     $fields['INVOICE_PROPERTIES'] = $invoiceProperties;
     unset($propsInfo, $invoiceProperties, $propCode, $arProp);
     $DB->StartTransaction();
     $ID = $invoice->Update($ID, $fields, array('UPDATE_SEARCH' => true));
     if (!is_int($ID) || $ID <= 0) {
         $DB->Rollback();
         if (!empty($invoice->LAST_ERROR)) {
             throw new RestException($invoice->LAST_ERROR);
         } else {
             throw new RestException('Error on updating invoice.');
         }
     } else {
         $DB->Commit();
     }
     return $ID;
 }