public function Synchronize()
{
$currentDay = time() + CTimeZone::GetOffset();
$currentDayEnd = ConvertTimeStamp(mktime(23, 59, 59, date('n', $currentDay), date('j', $currentDay), date('Y', $currentDay)), 'FULL', SITE_ID);
$count = 0;
if (
!\Bitrix\Crm\Settings\CounterSettings::getInstance()->isEnabled()
|| !\Bitrix\Crm\Settings\CounterSettings::getInstance()->canBeCounted()
)
{
$count = 0; // counters feature is completely disabled
}
elseif($this->typeID === self::CurrentActivies)
{
//Count of open user activities (start time: before tomorrow)
//Activities are filtered by RESPONSIBLE - we can switch off permission checking
$filter = array(
'RESPONSIBLE_ID' => $this->userID,
'COMPLETED' => 'N',
'<=START_TIME' => $currentDayEnd,
'CHECK_PERMISSIONS' => 'N'
);
$count = CCrmActivity::GetCount($filter);
}
elseif($this->typeID === self::CurrentCompanyActivies)
{
$count = CCrmActivity::GetCurrentQuantity($this->userID, CCrmOwnerType::Company);
}
elseif($this->typeID === self::CurrentContactActivies)
{
$count = CCrmActivity::GetCurrentQuantity($this->userID, CCrmOwnerType::Contact);
}
elseif($this->typeID === self::CurrentLeadActivies)
{
$count = CCrmActivity::GetCurrentQuantity($this->userID, CCrmOwnerType::Lead);
if(CCrmUserCounterSettings::GetValue(CCrmUserCounterSettings::ReckonActivitylessItems, true))
{
$leadTable = CCrmLead::TABLE_NAME;
$activityTable = CCrmActivity::USER_ACTIVITY_TABLE_NAME;
$ownerTypeID = CCrmOwnerType::Lead;
global $DB;
$dbResult = $DB->Query(
//"SELECT COUNT(l.ID) AS CNT FROM {$leadTable} l WHERE l.ASSIGNED_BY_ID = {$this->userID} AND l.STATUS_SEMANTIC_ID = 'P' AND l.ID NOT IN(SELECT a.OWNER_ID FROM {$activityTable} a WHERE a.USER_ID = 0 AND a.OWNER_TYPE_ID = 1)",
"SELECT COUNT(l.ID) AS CNT
FROM {$leadTable} l
LEFT JOIN {$activityTable} a ON a.OWNER_ID = l.ID AND a.OWNER_TYPE_ID = {$ownerTypeID} AND a.USER_ID = 0
WHERE l.ASSIGNED_BY_ID = {$this->userID} AND l.STATUS_SEMANTIC_ID = 'P' AND a.OWNER_ID IS NULL",
false,
'File: '.__FILE__.'
Line: '.__LINE__
);
$result = $dbResult->Fetch();
$count += is_array($result) ? intval($result['CNT']) : 0;
}
}
elseif($this->typeID === self::CurrentDealActivies)
{
$count = CCrmActivity::GetCurrentQuantity($this->userID, CCrmOwnerType::Deal);
if(CCrmUserCounterSettings::GetValue(CCrmUserCounterSettings::ReckonActivitylessItems, true))
{
$dealTable = CCrmDeal::TABLE_NAME;
$activityTable = CCrmActivity::USER_ACTIVITY_TABLE_NAME;
$ownerTypeID = CCrmOwnerType::Deal;
global $DB;
$dbResult = $DB->Query(
//"SELECT COUNT(d.ID) AS CNT FROM {$dealTable} d WHERE d.ASSIGNED_BY_ID = {$this->userID} AND d.STAGE_SEMANTIC_ID = 'P' AND d.ID NOT IN(SELECT a.OWNER_ID FROM {$activityTable} a WHERE a.USER_ID = 0 AND a.OWNER_TYPE_ID = 2)",
"SELECT COUNT(d.ID) AS CNT
FROM {$dealTable} d
LEFT JOIN {$activityTable} a ON a.OWNER_ID = d.ID AND a.OWNER_TYPE_ID = {$ownerTypeID} AND a.USER_ID = 0
WHERE d.ASSIGNED_BY_ID = {$this->userID} AND d.STAGE_SEMANTIC_ID = 'P' AND a.OWNER_ID IS NULL",
false,
'File: '.__FILE__.'
Line: '.__LINE__
);
$result = $dbResult->Fetch();
$count += is_array($result) ? intval($result['CNT']) : 0;
}
}
elseif($this->typeID === self::CurrentDealCategoryActivities)
{
$categoryID = $this->GetParamIntegerValue('CATEGORY_ID');
$dealTable = CCrmDeal::TABLE_NAME;
$activityTable = CCrmActivity::USER_ACTIVITY_TABLE_NAME;
global $DB;
$currentDay = time() + CTimeZone::GetOffset();
$currentDayEnd = ConvertTimeStamp(mktime(23, 59, 59, date('n', $currentDay), date('j', $currentDay), date('Y', $currentDay)), 'FULL', SITE_ID);
$currentDayEnd = $DB->CharToDateFunction($DB->ForSql($currentDayEnd), 'FULL');
$ownerTypeID = CCrmOwnerType::Deal;
$sql = "SELECT COUNT(DISTINCT a.OWNER_ID) AS CNT FROM {$activityTable} a
INNER JOIN {$dealTable} d ON a.OWNER_TYPE_ID = {$ownerTypeID}
AND a.OWNER_ID = d.ID
AND d.CATEGORY_ID = {$categoryID}
AND a.USER_ID = {$this->userID}
AND a.ACTIVITY_TIME <= {$currentDayEnd}";
$dbResult = $DB->Query(
$sql,
false,
'File: '.__FILE__.'
Line: '.__LINE__
);
$result = $dbResult->Fetch();
$count = is_array($result) ? (int)$result['CNT'] : 0;
if(CCrmUserCounterSettings::GetValue(CCrmUserCounterSettings::ReckonActivitylessItems, true))
{
$dbResult = $DB->Query(
//"SELECT COUNT(d.ID) AS CNT FROM {$dealTable} d WHERE d.ASSIGNED_BY_ID = {$this->userID} AND d.STAGE_SEMANTIC_ID = 'P' AND d.CATEGORY_ID = {$categoryID} AND d.ID NOT IN(SELECT a.OWNER_ID FROM {$activityTable} a WHERE a.USER_ID = 0 AND a.OWNER_TYPE_ID = 2)",
"SELECT COUNT(d.ID) AS CNT
FROM {$dealTable} d
LEFT JOIN {$activityTable} a ON a.OWNER_ID = d.ID AND a.OWNER_TYPE_ID = {$ownerTypeID} AND a.USER_ID = 0
WHERE d.ASSIGNED_BY_ID = {$this->userID} AND d.STAGE_SEMANTIC_ID = 'P' AND d.CATEGORY_ID = {$categoryID} AND a.OWNER_ID IS NULL",
false,
'File: '.__FILE__.'
Line: '.__LINE__
);
$result = $dbResult->Fetch();
$count += is_array($result) ? intval($result['CNT']) : 0;
}
}
elseif($this->typeID === self::CurrentQuoteActivies)
{
$count = 0;
if(CCrmUserCounterSettings::GetValue(CCrmUserCounterSettings::ReckonActivitylessItems, true))
{
$quoteTable = CCrmQuote::TABLE_NAME;
$statusStr = "'APPROVED'";
$statusCount = 1;
$statuses = self::GetStatusList('QUOTE_STATUS');
$isFound = false;
foreach($statuses as &$status)
{
if(!$isFound)
{
$isFound = $status['STATUS_ID'] === 'APPROVED';
}
else
{
$statusStr .= ",'{$status['STATUS_ID']}'";
$statusCount++;
// Foolproof
if($statusCount === 10)
{
break;
}
}
}
unset($status);
global $DB;
$currentDay = time() + CTimeZone::GetOffset();
$currentDayEnd = ConvertTimeStamp(mktime(23, 59, 59, date('n', $currentDay), date('j', $currentDay), date('Y', $currentDay)), 'FULL', SITE_ID);
$currentDayEnd = $DB->CharToDateFunction($DB->ForSql($currentDayEnd), 'FULL');
$dbResult = $DB->Query(
"SELECT COUNT(q.ID) AS CNT FROM {$quoteTable} q WHERE q.ASSIGNED_BY_ID = {$this->userID} AND q.CLOSEDATE IS NOT NULL AND q.CLOSEDATE <= {$currentDayEnd} AND q.STATUS_ID NOT IN ({$statusStr})",
false,
'File: '.__FILE__.'
Line: '.__LINE__
);
$result = $dbResult->Fetch();
$count += is_array($result) ? intval($result['CNT']) : 0;
}
}
elseif($this->typeID === self::CurrentOrderActivies)
{
//todo: order
$count = 0;
}
if($this->GetValue() !== $count)
{
$this->curValue = $count;
if($this->code !== '')
{
CUserCounter::Set($this->userID, $this->code, $this->curValue, SITE_ID, '', false);
}
}
$this->RefreshLastCalculatedTime();
return $this->curValue;
}