/**
  * Validate element value
  *
  * If a translation adapter is registered, any error messages will be
  * translated according to the current locale, using the given error code;
  * if no matching translation is found, the original message will be
  * utilized.
  *
  * Note: The *filtered* value is validated.
  *
  * @param  array   $data
  * @param  mixed   $context
  * @return boolean
  */
 public function isValid($data, $context = null, $removeNotPresentFields = false)
 {
     if (!($data instanceof TariffPlanServicesModel || $this->_acceptArrayAsModel && is_array($data))) {
         $this->_messages = array();
         $this->_messages[self::NOT_TARIFF_PLAN_SERVICES] = $this->_messageTemplatesUser[self::NOT_TARIFF_PLAN_SERVICES];
         return false;
     }
     if (!parent::isValid($data, $context, $removeNotPresentFields)) {
         return false;
     }
     if (!$data instanceof TariffPlanServicesModel) {
         $data = new TariffPlanServicesModel($data);
     }
     /*
      * GLOBALPORTAL-97959
      */
     $ignoreDeleted = $data->id && $data->published == ServicePackModel::STATUS_PUBLISHED;
     $tariffsData = $data->getData();
     $tariffsData[] = $data->getDefaultData();
     $validatorData = new TariffPlan\Services\Complex\DataTariffValidate(array('ignoreDeleted' => $ignoreDeleted));
     $tariffsVoice = $data->getOrigVoice();
     $tariffsVoice[] = $data->getDefaultOrigVoice();
     $validatorVoice = new TariffPlan\Services\Complex\VoiceTariffValidate(array('ignoreDeleted' => $ignoreDeleted));
     $tariffsTerm = $data->getTermVoice();
     $tariffsTerm[] = $data->getDefaultTermVoice();
     $validatorTerm = new TariffPlan\Services\Complex\VoiceTariffValidate(array('ignoreDeleted' => $ignoreDeleted));
     $tariffsSms = $data->getSms();
     $tariffsSms[] = $data->getDefaultSms();
     $validatorSms = new TariffPlan\Services\Complex\SmsTariffValidate(array('ignoreDeleted' => $ignoreDeleted));
     if (!$this->_correctNumberOfComplexTariffs($tariffsData, $validatorData, 'data') || !$this->_correctNumberOfComplexTariffs($tariffsVoice, $validatorVoice, 'originated voice') || !$this->_correctNumberOfComplexTariffs($tariffsTerm, $validatorTerm, 'terminated voice') || !$this->_correctNumberOfComplexTariffs($tariffsSms, $validatorSms, 'sms')) {
         return false;
     }
     return true;
 }
 /**
  *
  * @param  \Application\Model\TemplateModel           $template
  * @param  \Application\Model\ZonePlanModel           $zonePlan
  * @param  array                                      $relation
  * @throws App_MigrationTool_TemplatesTo20_Exception
  * @return \Application\Model\TariffPlanServicesModel
  */
 public static function upgradeTariffPlan($template, $zonePlan, $relation)
 {
     $oldData = $template->exportData();
     $tariffPlan = new TariffPlanServicesModel();
     $tariffPlan->importData($oldData);
     if ($tariffPlan->zonePlan) {
         throw new App_MigrationTool_TemplatesTo20_Exception("Tariff plan template " . $template->id . " already upgraded.");
     }
     $tariffPlan->setZonePlan($zonePlan);
     //Single tariff fields
     $tariffTypes = array('defaultData', 'defaultOrigVoice', 'defaultTermVoice', 'defaultSms');
     foreach ($tariffTypes as $type) {
         if (!isset($oldData[$type]['zoneId']) || $oldData[$type]['zoneId'] == -1) {
             continue;
         }
         if (!isset($relation[$oldData[$type]['zoneId']])) {
             throw new App_MigrationTool_TemplatesTo20_Exception("Unknown zone " . $oldData[$type]['zoneId'] . ". Impossible to upgrade.");
         }
         $tariff = new ServiceTariffModel($oldData[$type]);
         $tariff->zoneGroupId = $relation[$oldData[$type]['zoneId']];
         $tariffPlan->{$type} = $tariff;
     }
     //Array of tariff fields
     $tariffTypes = array('data', 'origVoice', 'termVoice', 'sms');
     foreach ($tariffTypes as $type) {
         $tariffs = array();
         foreach ($oldData[$type] as $oldTariff) {
             if (!isset($oldTariff['zoneId']) || $oldTariff['zoneId'] == -1) {
                 $tariff = new ServiceTariffModel($oldTariff);
                 $tariff->zoneGroupId = -1;
                 $tariffs[] = $tariff;
                 continue;
             }
             if (!isset($relation[$oldTariff['zoneId']])) {
                 throw new App_MigrationTool_TemplatesTo20_Exception("Unknown zone " . $oldTariff['zoneId'] . ". Impossible to upgrade.");
             }
             $tariff = new ServiceTariffModel($oldTariff);
             $tariff->zoneGroupId = $relation[$oldTariff['zoneId']];
             $tariffs[] = $tariff;
         }
         $tariffPlan->{$type} = $tariffs;
     }
     return $tariffPlan;
 }
 protected function _insertEricsson(TariffPlanServicesModel $tariff)
 {
     $methodName = self::METHOD_NAME_CREATE;
     try {
         $proto = $this->_createProto($methodName);
         $createData = new TariffPlanDefinition();
         $proto->setCreateData($createData);
         $mappedData = $this->_mapModelToEricssonModel($tariff->exportData());
         $parser = new \DrSlump\Protobuf\Codec\PhpArray(array('strict' => true));
         $createData->parse($mappedData, $parser);
         $response = $this->_sendRequest($methodName, array('protoMessage' => $proto));
     } catch (Exception $e) {
         \App::log()->CRIT($e);
     }
     $this->_checkPostResponse($response);
     $tariff->setId($response->getId());
     return $tariff->getId();
 }
 /**
  * Validate element value
  *
  * If a translation adapter is registered, any error messages will be
  * translated according to the current locale, using the given error code;
  * if no matching translation is found, the original message will be
  * utilized.
  *
  * Note: The *filtered* value is validated.
  *
  * @param  array   $data
  * @param  mixed   $context
  * @return boolean
  */
 public function isValid($data, $context = null, $removeNotPresentFields = false)
 {
     if (!($data instanceof TariffPlanServicesModel || $this->_acceptArrayAsModel && is_array($data))) {
         $this->_messages = array();
         $this->_messages[self::NOT_TARIFF_PLAN_SERVICES] = $this->_messageTemplatesUser[self::NOT_TARIFF_PLAN_SERVICES];
         return false;
     }
     if (!parent::isValid($data, $context, $removeNotPresentFields)) {
         return false;
     }
     if (!$data instanceof TariffPlanServicesModel) {
         $data = new TariffPlanServicesModel($data);
     }
     $tariffs = $data->getData();
     $tariffs[] = $data->getDefaultData();
     $count = 0;
     $limit = 3;
     $validator = new TariffPlan\Services\Complex\DataTariffValidate();
     foreach ($tariffs as $tariff) {
         if (!$validator->isValid($tariff)) {
             $count++;
             if ($count > $limit) {
                 $this->_messages = array();
                 $this->_messages[self::TOO_MANY_COMPLEX_TARIFFS] = str_replace('%type%', 'data', $this->_messageTemplatesUser[self::TOO_MANY_COMPLEX_TARIFFS]);
                 return false;
             }
         }
     }
     $tariffs = $data->getOrigVoice();
     $tariffs[] = $data->getDefaultOrigVoice();
     $count = 0;
     $limit = 3;
     $validator = new TariffPlan\Services\Complex\VoiceTariffValidate();
     foreach ($tariffs as $tariff) {
         if (!$validator->isValid($tariff)) {
             $count++;
             if ($count > $limit) {
                 $this->_messages = array();
                 $this->_messages[self::TOO_MANY_COMPLEX_TARIFFS] = str_replace('%type%', 'origined voice', $this->_messageTemplatesUser[self::TOO_MANY_COMPLEX_TARIFFS]);
                 return false;
             }
         }
     }
     $tariffs = $data->getTermVoice();
     $tariffs[] = $data->getDefaultTermVoice();
     $count = 0;
     $limit = 3;
     $validator = new TariffPlan\Services\Complex\VoiceTariffValidate();
     foreach ($tariffs as $tariff) {
         if (!$validator->isValid($tariff)) {
             $count++;
             if ($count > $limit) {
                 $this->_messages = array();
                 $this->_messages[self::TOO_MANY_COMPLEX_TARIFFS] = str_replace('%type%', 'terminated voice', $this->_messageTemplatesUser[self::TOO_MANY_COMPLEX_TARIFFS]);
                 return false;
             }
         }
     }
     $tariffs = $data->getSms();
     $tariffs[] = $data->getDefaultSms();
     $count = 0;
     $limit = 3;
     $validator = new TariffPlan\Services\Complex\SmsTariffValidate();
     foreach ($tariffs as $tariff) {
         if (!$validator->isValid($tariff)) {
             $count++;
             if ($count > $limit) {
                 $this->_messages = array();
                 $this->_messages[self::TOO_MANY_COMPLEX_TARIFFS] = str_replace('%type%', 'sms', $this->_messageTemplatesUser[self::TOO_MANY_COMPLEX_TARIFFS]);
                 return false;
             }
         }
     }
     return true;
 }