Exemplo n.º 1
0
 public static function register($ownerID, array $entityFields = null, array $options = null)
 {
     if (!is_int($ownerID)) {
         $ownerID = (int) $ownerID;
     }
     if ($ownerID <= 0) {
         throw new Main\ArgumentException('Owner ID must be greater than zero.', 'ownerID');
     }
     if (!is_array($options)) {
         $options = array();
     }
     if (!is_array($entityFields)) {
         $dbResult = \CCrmDeal::GetListEx(array(), array('=ID' => $ownerID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID', 'STAGE_ID', 'ASSIGNED_BY_ID', 'BEGINDATE', 'CLOSEDATE'));
         $entityFields = is_object($dbResult) ? $dbResult->Fetch() : null;
         if (!is_array($entityFields)) {
             return false;
         }
     }
     $stageID = isset($entityFields['STAGE_ID']) ? $entityFields['STAGE_ID'] : '';
     if ($stageID === '') {
         return false;
     }
     $responsibleID = isset($entityFields['ASSIGNED_BY_ID']) ? (int) $entityFields['ASSIGNED_BY_ID'] : 0;
     $startDate = self::parseDateString(isset($entityFields['BEGINDATE']) ? $entityFields['BEGINDATE'] : '');
     if ($startDate === null) {
         $startDate = new Date();
     }
     $endDate = self::parseDateString(isset($entityFields['CLOSEDATE']) ? $entityFields['CLOSEDATE'] : '');
     if ($endDate === null) {
         $endDate = new Date('9999-12-31', 'Y-m-d');
     }
     $time = isset($options['TIME']) ? $options['TIME'] : null;
     if ($time === null) {
         $time = new DateTime();
     }
     $month = (int) $time->format('m');
     $quarter = $month <= 3 ? 1 : ($month <= 6 ? 2 : ($month <= 9 ? 3 : 4));
     $year = (int) $time->format('Y');
     $startMonth = (int) $startDate->format('m');
     $startQuarter = $startMonth <= 3 ? 1 : ($startMonth <= 6 ? 2 : ($startMonth <= 9 ? 3 : 4));
     $startYear = (int) $startDate->format('Y');
     $endMonth = (int) $endDate->format('m');
     $endQuarter = $endMonth <= 3 ? 1 : ($endMonth <= 6 ? 2 : ($endMonth <= 9 ? 3 : 4));
     $endYear = (int) $endDate->format('Y');
     $semanticID = \CCrmDeal::GetSemanticID($stageID);
     $isNew = isset($options['IS_NEW']) ? (bool) $options['IS_NEW'] : false;
     $typeID = PhaseSemantics::isFinal($semanticID) ? HistoryEntryType::FINALIZATION : ($isNew ? HistoryEntryType::CREATION : HistoryEntryType::MODIFICATION);
     $date = Date::createFromTimestamp($time->getTimestamp());
     $isLost = PhaseSemantics::isLost($semanticID);
     $latest = self::getLatest($ownerID);
     if ($latest['STAGE_ID'] === $stageID) {
         return false;
     }
     $result = DealStageHistoryTable::add(array('TYPE_ID' => $typeID, 'OWNER_ID' => $ownerID, 'CREATED_TIME' => $time, 'CREATED_DATE' => $date, 'START_DATE' => $startDate, 'END_DATE' => $endDate, 'PERIOD_YEAR' => $year, 'PERIOD_QUARTER' => $quarter, 'PERIOD_MONTH' => $month, 'START_PERIOD_YEAR' => $startYear, 'START_PERIOD_QUARTER' => $startQuarter, 'START_PERIOD_MONTH' => $startMonth, 'END_PERIOD_YEAR' => $endYear, 'END_PERIOD_QUARTER' => $endQuarter, 'END_PERIOD_MONTH' => $endMonth, 'RESPONSIBLE_ID' => $responsibleID, 'STAGE_ID' => $stageID, 'STAGE_SEMANTIC_ID' => $semanticID, 'IS_LOST' => $isLost ? 'Y' : 'N'));
     if ($result->isSuccess() && $result->getId() > 0 && is_array($latest) && (int) $latest['TYPE_ID'] === HistoryEntryType::FINALIZATION) {
         DealStageHistoryTable::delete($latest['ID']);
     }
     return true;
 }