public function assureValueObject($value) { if ($value instanceof Type\DateTime) { // oracle sql helper returns datetime instead of date - it doesn't see the difference $value = new Type\Date($value->format(Main\UserFieldTable::MULTIPLE_DATE_FORMAT), Main\UserFieldTable::MULTIPLE_DATE_FORMAT); } return $value; }
public static function generateInitialData(Date $from) { if (($to = Option::get('conversion', 'START_DATE_TIME', 'undefined')) != 'undefined' && DateTime::isCorrect($to, 'Y-m-d H:i:s') && ($to = new DateTime($to, 'Y-m-d H:i:s')) && $to->format('Y-m-d H:i:s') > $from->format('Y-m-d H:i:s') && Option::get('conversion', 'GENERATE_INITIAL_DATA', 'undefined') == 'undefined') { Option::set('conversion', 'GENERATE_INITIAL_DATA', 'generated'); $context = new self(); // generate data $data = array(); foreach (EventManager::getInstance()->findEventHandlers('conversion', 'OnGenerateInitialData') as $handler) { $result = ExecuteModuleEventEx($handler, array($from, $to)); // TODO validate foreach ($result as $row) { $context->id = null; $context->attributes = array(); $context->setAttributes($row['ATTRIBUTES']); $context->save(); if ($dayCounters =& $data[$context->id]) { self::appendDayCounters($dayCounters, $row['DAY_COUNTERS']); } else { $dayCounters = $row['DAY_COUNTERS']; } } } unset($dayCounters); // save data to database $numerators = CounterManager::getTypes(array('GROUP' => 'day')); unset($numerators['conversion_visit_day']); foreach ($data as $id => $dayCounters) { $context->id = $id; foreach ($dayCounters as $day => $counters) { $day = new Date($day, 'Y-m-d'); $visitSum = 0; $visitQuantity = 0; unset($counters['conversion_visit_day']); foreach ($counters as $name => $value) { $context->addCounter($day, $name, $value); if ($numerators[$name]) { $visitSum += $value; $visitQuantity += 1; } } $context->addCounter($day, 'conversion_visit_day', $visitQuantity ? round($visitSum / $visitQuantity * 100) + 1 : 1); } } } }
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; }
/** * @return boolean */ 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(); } $date = isset($options['DATE']) ? $options['DATE'] : null; if ($date === null) { $date = new Date(); } $day = (int) $date->format('d'); $month = (int) $date->format('m'); $quarter = $month <= 3 ? 1 : ($month <= 6 ? 2 : ($month <= 9 ? 3 : 4)); $year = (int) $date->format('Y'); if (!is_array($entityFields)) { $dbResult = \CCrmDeal::GetListEx(array(), array('=ID' => $ownerID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('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'] : ''; $semanticID = \CCrmDeal::GetSemanticID($stageID); $isLost = PhaseSemantics::isLost($semanticID); $responsibleID = isset($entityFields['ASSIGNED_BY_ID']) ? (int) $entityFields['ASSIGNED_BY_ID'] : 0; $callQty = 0; $meetingQty = 0; $emailQty = 0; $startTime = new DateTime($date->format(DateTime::getFormat())); $endTime = new DateTime($date->format(DateTime::getFormat())); $endTime->setTime(23, 59, 59); $query = new Query(Crm\ActivityTable::getEntity()); $query->addSelect('TYPE_ID'); $query->registerRuntimeField('', new ExpressionField('QTY', 'COUNT(*)')); $query->addSelect('QTY'); $query->addFilter('=COMPLETED', 'Y'); $query->addFilter('>=DEADLINE', $startTime); $query->addFilter('<=DEADLINE', $endTime); $query->addGroup('TYPE_ID'); $subQuery = new Query(Crm\ActivityBindingTable::getEntity()); $subQuery->addSelect('ACTIVITY_ID'); $subQuery->addFilter('=OWNER_TYPE_ID', \CCrmOwnerType::Deal); $subQuery->addFilter('=OWNER_ID', $ownerID); $query->registerRuntimeField('', new ReferenceField('B', Base::getInstanceByQuery($subQuery), array('=this.ID' => 'ref.ACTIVITY_ID'), array('join_type' => 'INNER'))); $dbResult = $query->exec(); while ($stats = $dbResult->fetch()) { $typeID = isset($stats['TYPE_ID']) ? (int) $stats['TYPE_ID'] : 0; $qty = isset($stats['QTY']) ? (int) $stats['QTY'] : 0; if ($typeID === \CCrmActivityType::Call) { $callQty = $qty; } elseif ($typeID === \CCrmActivityType::Meeting) { $meetingQty = $qty; } elseif ($typeID === \CCrmActivityType::Email) { $emailQty = $qty; } } if ($callQty === 0 && $meetingQty === 0 && $emailQty === 0) { DealActivityStatisticsTable::delete(array('OWNER_ID' => $ownerID, 'DEADLINE_DATE' => $date)); return true; } $present = self::get($ownerID, $date); if (is_array($present)) { if ($responsibleID === (int) $present['RESPONSIBLE_ID'] && $stageID === $present['STAGE_ID'] && $callQty === (int) $present['CALL_QTY'] && $meetingQty === (int) $present['MEETING_QTY'] && $emailQty === (int) $present['EMAIL_QTY']) { return false; } if ($responsibleID !== (int) $present['RESPONSIBLE_ID']) { DealActivityStatisticsTable::synchronize($ownerID, array('RESPONSIBLE_ID' => $responsibleID)); } } $data = array('OWNER_ID' => $ownerID, 'DEADLINE_DATE' => $date, 'DEADLINE_YEAR' => $year, 'DEADLINE_QUARTER' => $quarter, 'DEADLINE_MONTH' => $month, 'DEADLINE_DAY' => $day, 'RESPONSIBLE_ID' => $responsibleID, 'STAGE_SEMANTIC_ID' => $semanticID, 'STAGE_ID' => $stageID, 'IS_LOST' => $isLost ? 'Y' : 'N', 'CALL_QTY' => $callQty, 'MEETING_QTY' => $meetingQty, 'EMAIL_QTY' => $emailQty); DealActivityStatisticsTable::upsert($data); return true; }
protected function loadStat(YandexDirectLive $liveEngine, $campaignXmlId, $dateStart, $dateFinish, $skipCurrency = false) { $dateStart = new Date($dateStart); $dateFinish = new Date($dateFinish); $queryData = array("CampaignID" => $campaignXmlId, "StartDate" => $dateStart->format("Y-m-d"), 'EndDate' => $dateFinish->format("Y-m-d"), 'GroupByColumns' => array('clDate', 'clBanner')); $currency = ''; if (!$skipCurrency && Loader::includeModule('currency')) { $baseCurrency = \CCurrency::GetBaseCurrency(); if ($baseCurrency == 'RUR') { $baseCurrency = 'RUB'; } if (in_array($baseCurrency, $liveEngine->allowedCurrency)) { $currency = $baseCurrency; } } if ($currency != '') { $queryData['Currency'] = $currency; } try { $result = $liveEngine->getBannerStats($queryData); $result['Currency'] = $currency; } catch (YandexDirectException $e) { if ($currency != '' && $e->getCode() == YandexDirectLive::ERROR_WRONG_CURRENCY) { $result = static::loadStat($liveEngine, $campaignXmlId, $dateStart, $dateFinish, true); } else { throw $e; } } return $result; }