Exemplo n.º 1
0
 /**
  * Create conversion map for destination entity type
  * @static
  * @param int $entityTypeID Destination Entity Type ID
  * @return EntityConversionMap
  */
 public static function createMap($entityTypeID)
 {
     if (!is_int($entityTypeID)) {
         $entityTypeID = (int) $entityTypeID;
     }
     if (!\CCrmOwnerType::IsDefined($entityTypeID)) {
         throw new Main\ArgumentOutOfRangeException('dstEntityTypeID', \CCrmOwnerType::FirstOwnerType, \CCrmOwnerType::LastOwnerType);
     }
     if ($entityTypeID !== \CCrmOwnerType::Deal && $entityTypeID !== \CCrmOwnerType::Invoice) {
         $dstEntityTypeName = \CCrmOwnerType::ResolveName($entityTypeID);
         throw new Main\NotSupportedException("Entity type: '{$dstEntityTypeName}' is not supported in current context");
     }
     $map = new EntityConversionMap(\CCrmOwnerType::Quote, $entityTypeID);
     if ($entityTypeID === \CCrmOwnerType::Deal) {
         //region Deal Map Static Field Bindings
         $map->createItem('TITLE');
         $map->createItem('COMMENTS');
         $map->createItem('ASSIGNED_BY_ID');
         $map->createItem('OPENED');
         $map->createItem('OPPORTUNITY');
         $map->createItem('CURRENCY_ID');
         $map->createItem('TAX_VALUE');
         $map->createItem('EXCH_RATE');
         $map->createItem('LOCATION_ID');
         $map->createItem('LEAD_ID');
         $map->createItem('COMPANY_ID');
         $map->createItem('CONTACT_ID');
         $map->createItem('PRODUCT_ROWS');
         //endregion
         //region Invoice Map User Field Bindings
         $intersections = UserFieldSynchronizer::getIntersection(\CCrmOwnerType::Quote, \CCrmOwnerType::Deal);
         foreach ($intersections as $intersection) {
             $map->createItem($intersection['SRC_FIELD_NAME'], $intersection['DST_FIELD_NAME']);
         }
         //endregion
     }
     if ($entityTypeID === \CCrmOwnerType::Invoice) {
         //region Invoice Map Static Field Bindings
         $map->createItem('TITLE', 'ORDER_TOPIC');
         $map->createItem('COMPANY_ID', 'UF_COMPANY_ID');
         $map->createItem('CONTACT_ID', 'UF_CONTACT_ID');
         $map->createItem('DEAL_ID', 'UF_DEAL_ID');
         $map->createItem('ASSIGNED_BY_ID', 'RESPONSIBLE_ID');
         $map->createItem('COMMENTS');
         $map->createItem('PRODUCT_ROWS');
         //endregion
         //region Invoice Map User Field Bindings
         $intersections = UserFieldSynchronizer::getIntersection(\CCrmOwnerType::Quote, \CCrmOwnerType::Invoice);
         foreach ($intersections as $intersection) {
             $map->createItem($intersection['SRC_FIELD_NAME'], $intersection['DST_FIELD_NAME']);
         }
         //endregion
     }
     return $map;
 }
 public static function updateMap(EntityConversionMap $map)
 {
     //Synchonize dynamic bindings only
     $srcEntitTypeID = $map->getSourceEntityTypeID();
     $dstEntitTypeID = $map->getDestinationEntityTypeID();
     $outdatedItems = array();
     foreach ($map->getItems() as $item) {
         $srcFieldID = $item->getSourceField();
         $dstFieldID = $item->getDestinationField();
         if ($dstFieldID === '') {
             $dstFieldID = $srcFieldID;
         }
         $isDynamicSrc = EntityConversionMapItem::isDynamicField($srcFieldID);
         $isDynamicDst = EntityConversionMapItem::isDynamicField($dstFieldID);
         $srcField = $isDynamicSrc ? self::getUserField($srcEntitTypeID, $srcFieldID) : null;
         $dstField = $isDynamicDst ? self::getUserField($dstEntitTypeID, $dstFieldID) : null;
         if ($isDynamicSrc && $srcField === null || $isDynamicDst && $dstField === null) {
             $outdatedItems[] = $item;
             continue;
         } elseif ($isDynamicSrc && $srcField !== null && $isDynamicDst && $dstField !== null && !$item->isLocked()) {
             $srcCode = UserFieldSynchronizer::getFieldComplianceCode($srcField);
             $dstCode = UserFieldSynchronizer::getFieldComplianceCode($dstField);
             if ($srcCode !== $dstCode) {
                 $outdatedItems[] = $item;
                 continue;
             }
         }
     }
     if (!empty($outdatedItems)) {
         foreach ($outdatedItems as $item) {
             $map->removeItem($item);
         }
     }
     $intersections = UserFieldSynchronizer::getIntersection($srcEntitTypeID, $dstEntitTypeID);
     foreach ($intersections as $intersection) {
         $srcFieldID = $intersection['SRC_FIELD_NAME'];
         $dstFieldID = $intersection['DST_FIELD_NAME'];
         if ($map->findItemBySourceID($srcFieldID) === null) {
             $map->createItem($srcFieldID, $dstFieldID);
         }
     }
 }
Exemplo n.º 3
0
 public function executePhase()
 {
     if ($this->currentPhase === LeadConversionPhase::COMPANY_CREATION || $this->currentPhase === LeadConversionPhase::CONTACT_CREATION || $this->currentPhase === LeadConversionPhase::DEAL_CREATION) {
         if ($this->currentPhase === LeadConversionPhase::COMPANY_CREATION) {
             $entityTypeID = \CCrmOwnerType::Company;
         } elseif ($this->currentPhase === LeadConversionPhase::CONTACT_CREATION) {
             $entityTypeID = \CCrmOwnerType::Contact;
         } else {
             $entityTypeID = \CCrmOwnerType::Deal;
         }
         $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::Company) {
                 if (!\CCrmCompany::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Company, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 $entity = new \CCrmCompany(false);
             } elseif ($entityTypeID === \CCrmOwnerType::Contact) {
                 if (!\CCrmContact::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Contact, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 $entity = new \CCrmContact(false);
             } else {
                 if (!\CCrmDeal::Exists($entityID)) {
                     throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Deal, EntityConversionException::TARG_DST, EntityConversionException::NOT_FOUND);
                 }
                 $entity = new \CCrmDeal(false);
             }
             if (!\CCrmAuthorizationHelper::CheckUpdatePermission($entityTypeName, $entityID, $permissions)) {
                 throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::UPDATE_DENIED);
             }
             $fields = array('LEAD_ID' => $this->entityID);
             $entity->Update($entityID, $fields);
             $this->resultData[$entityTypeName] = $entityID;
             return true;
         }
         if (!\CCrmAuthorizationHelper::CheckCreatePermission($entityTypeName, $permissions)) {
             throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::CREATE_DENIED);
         }
         if (UserFieldSynchronizer::needForSynchronization(\CCrmOwnerType::Lead, $entityTypeID)) {
             throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::NOT_SYNCHRONIZED);
         }
         if (!ConversionSettings::getCurrent()->isAutocreationEnabled()) {
             throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::AUTOCREATION_DISABLED);
         }
         if (\CCrmBizProcHelper::HasAutoWorkflows($entityTypeID, \CCrmBizProcEventType::Create)) {
             throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::HAS_WORKFLOWS);
         }
         /** @var LeadConversionMapper $mapper */
         $mapper = $this->getMapper();
         $map = self::prepareMap($entityTypeID);
         $fields = $mapper->map($map);
         if (empty($fields)) {
             throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::EMPTY_FIELDS);
         }
         if ($entityTypeID === \CCrmOwnerType::Company) {
             $entity = new \CCrmCompany(false);
             $entityID = $entity->Add($fields);
             if ($entityID <= 0) {
                 throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Company, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR);
             }
             //region BizProcess
             $arErrors = array();
             \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Company, $entityID, \CCrmBizProcEventType::Create, $arErrors);
             //endregion
             $this->resultData[\CCrmOwnerType::CompanyName] = $entityID;
         } elseif ($entityTypeID === \CCrmOwnerType::Contact) {
             if (isset($this->resultData[\CCrmOwnerType::CompanyName])) {
                 $fields['COMPANY_ID'] = $this->resultData[\CCrmOwnerType::CompanyName];
             }
             $entity = new \CCrmContact(false);
             if (!$entity->CheckFields($fields)) {
                 throw new EntityConversionException(\CCrmOwnerType::Lead, $entityTypeID, EntityConversionException::TARG_DST, EntityConversionException::INVALID_FIELDS, $entity->LAST_ERROR);
             }
             $entityID = $entity->Add($fields);
             if ($entityID <= 0) {
                 throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Contact, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR);
             }
             //region BizProcess
             $arErrors = array();
             \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Contact, $entityID, \CCrmBizProcEventType::Create, $arErrors);
             //endregion
             $this->resultData[\CCrmOwnerType::ContactName] = $entityID;
         } else {
             if (isset($this->resultData[\CCrmOwnerType::ContactName])) {
                 $fields['CONTACT_ID'] = $this->resultData[\CCrmOwnerType::ContactName];
             }
             if (isset($this->resultData[\CCrmOwnerType::CompanyName])) {
                 $fields['COMPANY_ID'] = $this->resultData[\CCrmOwnerType::CompanyName];
             }
             $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;
             }
             $entity = new \CCrmDeal(false);
             $entityID = $entity->Add($fields);
             if ($entityID <= 0) {
                 throw new EntityConversionException(\CCrmOwnerType::Lead, \CCrmOwnerType::Deal, EntityConversionException::TARG_DST, EntityConversionException::CREATE_FAILED, $entity->LAST_ERROR);
             }
             if (!empty($productRows)) {
                 \CCrmDeal::SaveProductRows($entityID, $productRows, false, false, false);
             }
             //region BizProcess
             $arErrors = array();
             \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Deal, $entityID, \CCrmBizProcEventType::Create, $arErrors);
             //endregion
             $this->resultData[\CCrmOwnerType::DealName] = $entityID;
         }
         return true;
     } elseif ($this->currentPhase === LeadConversionPhase::FINALIZATION) {
         $result = \CCrmLead::GetListEx(array(), array('=ID' => $this->entityID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('STATUS_ID'));
         $presentFields = is_object($result) ? $result->Fetch() : null;
         if (is_array($presentFields)) {
             $fields = array();
             $statusID = isset($presentFields['STATUS_ID']) ? $presentFields['STATUS_ID'] : '';
             if ($statusID !== 'CONVERTED') {
                 $fields['STATUS_ID'] = 'CONVERTED';
             }
             if (isset($this->resultData[\CCrmOwnerType::CompanyName])) {
                 $fields['COMPANY_ID'] = $this->resultData[\CCrmOwnerType::CompanyName];
             }
             if (isset($this->resultData[\CCrmOwnerType::ContactName])) {
                 $fields['CONTACT_ID'] = $this->resultData[\CCrmOwnerType::ContactName];
             }
             if (!empty($fields)) {
                 $entity = new \CCrmLead(false);
                 if ($entity->Update($this->entityID, $fields)) {
                     //region BizProcess
                     $arErrors = array();
                     \CCrmBizProcHelper::AutoStartWorkflows(\CCrmOwnerType::Lead, $this->entityID, \CCrmBizProcEventType::Edit, $arErrors);
                     //endregion
                 }
             }
         }
         return true;
     }
     return false;
 }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
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;
 }
Exemplo n.º 6
0
 /**
  * Create conversion map for destination entity type
  * @static
  * @param int $entityTypeID Destination Entity Type ID
  * @return EntityConversionMap
  */
 public static function createMap($entityTypeID)
 {
     if (!is_int($entityTypeID)) {
         $entityTypeID = (int) $entityTypeID;
     }
     if (!\CCrmOwnerType::IsDefined($entityTypeID)) {
         throw new Main\ArgumentOutOfRangeException('dstEntityTypeID', \CCrmOwnerType::FirstOwnerType, \CCrmOwnerType::LastOwnerType);
     }
     if ($entityTypeID !== \CCrmOwnerType::Contact && $entityTypeID !== \CCrmOwnerType::Company && $entityTypeID !== \CCrmOwnerType::Deal) {
         $dstEntityTypeName = \CCrmOwnerType::ResolveName($entityTypeID);
         throw new Main\NotSupportedException("Entity type: '{$dstEntityTypeName}' is not supported in current context");
     }
     $map = new EntityConversionMap(\CCrmOwnerType::Lead, $entityTypeID);
     if ($entityTypeID === \CCrmOwnerType::Contact) {
         //region Contact Map Static Field Bindings
         $map->createItem('HONORIFIC');
         $map->createItem('NAME');
         $map->createItem('SECOND_NAME');
         $map->createItem('LAST_NAME');
         $map->createItem('BIRTHDATE');
         $map->createItem('POST');
         $map->createItem('COMMENTS');
         $map->createItem('OPENED');
         $map->createItem('SOURCE_ID');
         $map->createItem('SOURCE_DESCRIPTION');
         $map->createItem('ADDRESS');
         $map->createItem('ADDRESS_2');
         $map->createItem('ADDRESS_CITY');
         $map->createItem('ADDRESS_POSTAL_CODE');
         $map->createItem('ADDRESS_REGION');
         $map->createItem('ADDRESS_PROVINCE');
         $map->createItem('ADDRESS_COUNTRY');
         $map->createItem('ADDRESS_COUNTRY_CODE');
         $map->createItem('PHONE');
         $map->createItem('EMAIL');
         $map->createItem('WEB');
         $map->createItem('IM');
         $map->createItem('ASSIGNED_BY_ID');
         $map->createItem('ORIGINATOR_ID');
         $map->createItem('ORIGIN_ID');
         //endregion
         //region Contact Map User Field Bindings
         $intersections = UserFieldSynchronizer::getIntersection(\CCrmOwnerType::Lead, \CCrmOwnerType::Contact);
         foreach ($intersections as $intersection) {
             $map->createItem($intersection['SRC_FIELD_NAME'], $intersection['DST_FIELD_NAME']);
         }
         //endregion
     } elseif ($entityTypeID === \CCrmOwnerType::Company) {
         //region Company Map Static Field Bindings
         //$map->createItem('COMPANY_TITLE', 'TITLE', array('ALT_SRC_FIELD_IDS' => array('TITLE')));
         $map->createItem('TITLE');
         $map->createItem('COMMENTS');
         $map->createItem('OPENED');
         $map->createItem('ADDRESS');
         $map->createItem('ADDRESS_2');
         $map->createItem('ADDRESS_CITY');
         $map->createItem('ADDRESS_POSTAL_CODE');
         $map->createItem('ADDRESS_REGION');
         $map->createItem('ADDRESS_PROVINCE');
         $map->createItem('ADDRESS_COUNTRY');
         $map->createItem('ADDRESS_COUNTRY_CODE');
         $map->createItem('PHONE');
         $map->createItem('EMAIL');
         $map->createItem('WEB');
         $map->createItem('IM');
         $map->createItem('ASSIGNED_BY_ID');
         $map->createItem('ORIGINATOR_ID');
         $map->createItem('ORIGIN_ID');
         //endregion
         //region Company Map User Field Bindings
         $intersections = UserFieldSynchronizer::getIntersection(\CCrmOwnerType::Lead, \CCrmOwnerType::Company);
         foreach ($intersections as $intersection) {
             $map->createItem($intersection['SRC_FIELD_NAME'], $intersection['DST_FIELD_NAME']);
         }
         //endregion
     } elseif ($entityTypeID === \CCrmOwnerType::Deal) {
         //region Deal Map Static Field Bindings
         $map->createItem('TITLE');
         $map->createItem('OPPORTUNITY');
         $map->createItem('CURRENCY_ID');
         $map->createItem('COMMENTS');
         $map->createItem('OPENED');
         $map->createItem('ADDRESS');
         $map->createItem('ADDRESS_2');
         $map->createItem('ADDRESS_CITY');
         $map->createItem('ADDRESS_POSTAL_CODE');
         $map->createItem('ADDRESS_REGION');
         $map->createItem('ADDRESS_PROVINCE');
         $map->createItem('ADDRESS_COUNTRY');
         $map->createItem('ADDRESS_COUNTRY_CODE');
         $map->createItem('PRODUCT_ROWS');
         $map->createItem('ASSIGNED_BY_ID');
         $map->createItem('ORIGINATOR_ID');
         $map->createItem('ORIGIN_ID');
         //endregion
         //region Deal Map User Field Bindings
         $intersections = UserFieldSynchronizer::getIntersection(\CCrmOwnerType::Lead, \CCrmOwnerType::Deal);
         foreach ($intersections as $intersection) {
             $map->createItem($intersection['SRC_FIELD_NAME'], $intersection['DST_FIELD_NAME']);
         }
         //endregion
     }
     return $map;
 }