/** * @return array */ public function prepareData() { $totals = array(); $stages = \CCrmStatus::GetStatusList('DEAL_STAGE'); foreach ($stages as $k => $v) { $semanticID = \CCrmDeal::GetSemanticID($k); if ($semanticID === PhaseSemantics::FAILURE) { continue; } $totals[$k] = array('ID' => $k, 'NAME' => $v, 'TOTAL' => 0); } $source = new DealStageHistory(array(), $this->userID, $this->enablePermissionCheck); //CREATION $this->filter->setExtras(array('typeID' => HistoryEntryType::CREATION)); $values = $source->getList(array('filter' => $this->filter)); $this->prepareTotals($values, $totals); //MODIFICATION $this->filter->setExtras(array('typeID' => HistoryEntryType::MODIFICATION)); $values = $source->getList(array('filter' => $this->filter)); $this->prepareTotals($values, $totals); //FINALIZATION $this->filter->setExtras(array('typeID' => HistoryEntryType::FINALIZATION)); $values = $source->getList(array('filter' => $this->filter)); $this->prepareTotals($values, $totals); $items = array(); foreach ($totals as $total) { if ($total['TOTAL'] > 0) { $items[] = $total; } } return array('items' => $items, 'valueField' => 'TOTAL', 'titleField' => 'NAME'); }
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; }
/** * @return boolean */ public static function synchronize($ownerID, array $entityFields = 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($entityFields)) { $dbResult = \CCrmDeal::GetListEx(array(), array('=ID' => $ownerID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('STAGE_ID', 'ASSIGNED_BY_ID', 'BEGINDATE', 'CLOSEDATE', 'OPPORTUNITY', 'CURRENCY_ID')); $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; /** @var Date $startDate */ $startDate = self::parseDateString(isset($entityFields['BEGINDATE']) ? $entityFields['BEGINDATE'] : ''); if ($startDate === null) { $startDate = new Date(); } /** @var Date $endDate */ $endDate = self::parseDateString(isset($entityFields['CLOSEDATE']) ? $entityFields['CLOSEDATE'] : ''); if ($endDate === null) { $endDate = new Date('9999-12-31', 'Y-m-d'); } $sum = isset($entityFields['OPPORTUNITY']) ? (double) $entityFields['OPPORTUNITY'] : 0.0; $currencyID = isset($entityFields['CURRENCY_ID']) ? $entityFields['CURRENCY_ID'] : ''; if ($currencyID === '') { $currencyID = \CCrmCurrency::GetBaseCurrencyID(); } $accountCurrencyID = \CCrmCurrency::GetAccountCurrencyID(); $sumData = \CCrmAccountingHelper::PrepareAccountingData(array('CURRENCY_ID' => $currencyID, 'SUM' => $sum)); if (is_array($sumData)) { $sum = (double) $sumData['ACCOUNT_SUM']; } $latest = self::getLatest($ownerID); if (!is_array($latest)) { if ($semanticID === PhaseSemantics::SUCCESS) { //Creation of stub for successfully completed entity without invoices self::innerRegister(array('OWNER_ID' => $ownerID, 'CREATED_DATE' => new Date(), 'START_DATE' => $startDate, 'END_DATE' => $endDate, 'RESPONSIBLE_ID' => $responsibleID, 'STAGE_SEMANTIC_ID' => PhaseSemantics::SUCCESS, 'STAGE_ID' => $stageID, 'IS_LOST' => 'N', 'CURRENCY_ID' => $accountCurrencyID, 'INVOICE_SUM' => 0.0, 'INVOICE_QTY' => 0, 'TOTAL_INVOICE_SUM' => 0.0, 'TOTAL_INVOICE_QTY' => 0, 'TOTAL_SUM' => $sum)); } return true; } if ($startDate->getTimestamp() === $latest['START_DATE']->getTimestamp() && $endDate->getTimestamp() === $latest['END_DATE']->getTimestamp() && $responsibleID === (int) $latest['RESPONSIBLE_ID'] && $stageID === $latest['STAGE_ID'] && $semanticID === $latest['STAGE_SEMANTIC_ID'] && $sum === (double) $latest['TOTAL_SUM']) { return false; } if ($semanticID !== $latest['STAGE_SEMANTIC_ID'] && $latest['STAGE_SEMANTIC_ID'] === PhaseSemantics::SUCCESS && (int) $latest['INVOICE_QTY'] === 0) { //Clean up stub for successfully completed entity without invoices DealInvoiceStatisticsTable::delete(array('OWNER_ID' => $ownerID, 'CREATED_DATE' => $latest['CREATED_DATE'])); } else { DealInvoiceStatisticsTable::synchronize($ownerID, array('START_DATE' => $startDate, 'END_DATE' => $endDate, 'RESPONSIBLE_ID' => $responsibleID, 'STAGE_SEMANTIC_ID' => $semanticID, 'STAGE_ID' => $stageID, 'IS_LOST' => $isLost ? 'Y' : 'N', 'TOTAL_SUM' => $sum)); } return true; }
/** * @return void */ public static function register($ownerID, array $entityFields = 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($entityFields)) { $dbResult = \CCrmDeal::GetListEx(array(), array('=ID' => $ownerID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('STAGE_ID', 'ASSIGNED_BY_ID', 'BEGINDATE', 'CLOSEDATE', 'CURRENCY_ID', 'OPPORTUNITY', 'UF_*')); $entityFields = is_object($dbResult) ? $dbResult->Fetch() : null; if (!is_array($entityFields)) { return; } } $bindingMap = self::getSlotBindingMap(); $stageID = isset($entityFields['STAGE_ID']) ? $entityFields['STAGE_ID'] : ''; $semanticID = \CCrmDeal::GetSemanticID($stageID); $isLost = PhaseSemantics::isLost($semanticID); $isFinalized = PhaseSemantics::isFinal($semanticID); $responsibleID = isset($entityFields['ASSIGNED_BY_ID']) ? (int) $entityFields['ASSIGNED_BY_ID'] : 0; /** @var Date $startDate */ $startDate = self::parseDateString(isset($entityFields['BEGINDATE']) ? $entityFields['BEGINDATE'] : ''); if ($startDate === null || $startDate->getTimestamp() === false) { $startDate = isset($entityFields['DATE_CREATE']) ? self::parseDateString($entityFields['DATE_CREATE']) : null; if ($startDate === null || $startDate->getTimestamp() === false) { $startDate = new Date(); } } /** @var Date $endDate */ $endDate = self::parseDateString(isset($entityFields['CLOSEDATE']) ? $entityFields['CLOSEDATE'] : ''); if ($endDate === null || $endDate->getTimestamp() === false) { $endDate = new Date('9999-12-31', 'Y-m-d'); } $date = $isFinalized ? $endDate : $startDate; $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'); $currencyID = isset($entityFields['CURRENCY_ID']) ? $entityFields['CURRENCY_ID'] : ''; $accountingCurrencyID = \CCrmCurrency::GetAccountCurrencyID(); $sum = isset($entityFields['OPPORTUNITY']) ? (double) $entityFields['OPPORTUNITY'] : 0.0; $binding = $bindingMap->get('SUM_TOTAL'); if ($binding === null) { $total = isset($entityFields['OPPORTUNITY']) ? (double) $entityFields['OPPORTUNITY'] : 0.0; } else { $bindingFieldName = $binding->getFieldName(); if ($bindingFieldName === '') { $bindingFieldName = 'OPPORTUNITY'; } $total = isset($entityFields[$bindingFieldName]) ? (double) $entityFields[$bindingFieldName] : 0.0; if ($bindingFieldName !== 'OPPORTUNITY' && $binding->getOption('ADD_PRODUCT_ROW_SUM') === 'Y') { $total += $sum; } } if ($currencyID !== $accountingCurrencyID) { $accData = \CCrmAccountingHelper::PrepareAccountingData(array('CURRENCY_ID' => $currencyID, 'SUM' => $total, 'EXCH_RATE' => isset($entityFields['EXCH_RATE']) ? $entityFields['EXCH_RATE'] : null)); if (is_array($accData)) { $total = (double) $accData['ACCOUNT_SUM']; } } $sumSlots = array(); $sumSlotFields = DealSumStatisticsTable::getSumSlotFieldNames(); foreach ($sumSlotFields as $fieldName) { $binding = $bindingMap->get($fieldName); if ($binding === null) { $slotSum = 0.0; } else { $bindingFieldName = $binding->getFieldName(); $slotSum = $bindingFieldName !== '' && isset($entityFields[$bindingFieldName]) ? (double) $entityFields[$bindingFieldName] : 0.0; if ($binding->getOption('ADD_PRODUCT_ROW_SUM') === 'Y') { $slotSum += $sum; } } if ($currencyID !== $accountingCurrencyID) { $accData = \CCrmAccountingHelper::PrepareAccountingData(array('CURRENCY_ID' => $currencyID, 'SUM' => $slotSum, 'EXCH_RATE' => isset($entityFields['EXCH_RATE']) ? $entityFields['EXCH_RATE'] : null)); if (is_array($accData)) { $slotSum = (double) $accData['ACCOUNT_SUM']; } } $sumSlots[$fieldName] = $slotSum; } $entities = self::getAll($ownerID); $final = null; $process = null; foreach ($entities as $entity) { if (PhaseSemantics::isFinal($entity['STAGE_SEMANTIC_ID'])) { $final = $entity; } else { $process = $entity; } if ($final && $process) { break; } } $latest = self::getLatest($ownerID); if (is_array($latest) && isset($latest['START_DATE']) && isset($latest['END_DATE'])) { if ($startDate->getTimestamp() === $latest['START_DATE']->getTimestamp() && $endDate->getTimestamp() === $latest['END_DATE']->getTimestamp() && $responsibleID === (int) $latest['RESPONSIBLE_ID'] && $stageID === $latest['STAGE_ID'] && $currencyID === $latest['CURRENCY_ID'] && $total === (double) $latest['SUM_TOTAL'] && $sumSlots['UF_SUM_1'] === (double) $latest['UF_SUM_1'] && $sumSlots['UF_SUM_2'] === (double) $latest['UF_SUM_2'] && $sumSlots['UF_SUM_3'] === (double) $latest['UF_SUM_3'] && $sumSlots['UF_SUM_4'] === (double) $latest['UF_SUM_4'] && $sumSlots['UF_SUM_5'] === (double) $latest['UF_SUM_5']) { return; } if ($startDate->getTimestamp() !== $latest['START_DATE']->getTimestamp() || $endDate->getTimestamp() !== $latest['END_DATE']->getTimestamp() || $responsibleID !== (int) $latest['RESPONSIBLE_ID']) { if (!$isFinalized) { DealSumStatisticsTable::deleteByFilter(array('OWNER_ID' => $ownerID, 'SEMANTIC_ID' => PhaseSemantics::getFinalSemantis())); DealSumStatisticsTable::synchronize($ownerID, array('CREATED_DATE' => $startDate, 'START_DATE' => $startDate, 'END_DATE' => $endDate, 'RESPONSIBLE_ID' => $responsibleID), PhaseSemantics::getProcessSemantis()); } else { if ($startDate->getTimestamp() === $endDate->getTimestamp()) { DealSumStatisticsTable::deleteByFilter(array('OWNER_ID' => $ownerID, 'SEMANTIC_ID' => PhaseSemantics::getProcessSemantis())); DealSumStatisticsTable::synchronize($ownerID, array('CREATED_DATE' => $endDate, 'START_DATE' => $startDate, 'END_DATE' => $endDate, 'RESPONSIBLE_ID' => $responsibleID), PhaseSemantics::getFinalSemantis()); } else { //Diphasic update of "final" semantics (first update with forged date: "1970-01-01") for avoid possible primary key conflict with "process" semantics DealSumStatisticsTable::synchronize($ownerID, array('CREATED_DATE' => new Date('1970-01-01', 'Y-m-d'), 'START_DATE' => $startDate, 'END_DATE' => $endDate, 'RESPONSIBLE_ID' => $responsibleID), PhaseSemantics::getFinalSemantis()); DealSumStatisticsTable::synchronize($ownerID, array('CREATED_DATE' => $startDate, 'START_DATE' => $startDate, 'END_DATE' => $endDate, 'RESPONSIBLE_ID' => $responsibleID), PhaseSemantics::getProcessSemantis()); DealSumStatisticsTable::synchronize($ownerID, array('CREATED_DATE' => $endDate, 'START_DATE' => $startDate, 'END_DATE' => $endDate, 'RESPONSIBLE_ID' => $responsibleID), PhaseSemantics::getFinalSemantis()); } } } } $data = array_merge(array('OWNER_ID' => $ownerID, 'CREATED_DATE' => $date, 'START_DATE' => $startDate, 'END_DATE' => $endDate, 'PERIOD_YEAR' => $year, 'PERIOD_QUARTER' => $quarter, 'PERIOD_MONTH' => $month, 'PERIOD_DAY' => $day, 'RESPONSIBLE_ID' => $responsibleID, 'STAGE_SEMANTIC_ID' => $semanticID, 'STAGE_ID' => $stageID, 'IS_LOST' => $isLost ? 'Y' : 'N', 'CURRENCY_ID' => $accountingCurrencyID, 'SUM_TOTAL' => $total), $sumSlots); DealSumStatisticsTable::upsert($data); }