Example #1
0
 /**
  * @param array            $userfield
  * @param Type\Date|string $value
  *
  * @return Type\Date
  */
 public function onBeforeSave($userfield, $value)
 {
     if (strlen($value) && !$value instanceof Type\Date) {
         // try both site's format - short and full
         try {
             $value = new Type\Date($value);
         } catch (Main\ObjectException $e) {
             $value = new Type\Date($value, Type\DateTime::getFormat());
         }
     }
     return $value;
 }
 /**
  * @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;
 }