Skip to content

Commit

Permalink
Apenas o arquivo md relativo a operação em contingencia SVC
Browse files Browse the repository at this point in the history
  • Loading branch information
robmachado committed Jun 11, 2024
1 parent f10aea5 commit 8837f0a
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions src/Factories/Contingency.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ class Contingency
{
public const SVCAN = 'SVCAN';
public const SVCRS = 'SVCRS';
public const OFFLINE = 'OFFLINE';
public const EPEC = 'EPEC';
public const FSDA = 'FS-DA';
public const TPEMIS_FSDA = 5;
public const TPEMIS_OFFLINE = 9;

/**
* @var \stdClass
Expand Down Expand Up @@ -73,13 +68,15 @@ public function load(string $contingency): void

/**
* Create a object with contingency data
* @param string $acronym Sigla do estado
* @param string $type Opcional parameter only used if FS-DA, EPEC or OFFLINE
* @param string $acronym sigla dos estados
* @param string $motive motivo de entrada em contingência
* @param string $type tipo de contingência SVCAN ou SVCRS
* @return string
* @throws \Exception
*/
public function activate(string $acronym, string $motive, string $type = ''): string
{
$dt = new \DateTime('now');
$list = array(
$list = [
'AC' => 'SVCAN',
'AL' => 'SVCAN',
'AM' => 'SVCRS',
Expand Down Expand Up @@ -107,13 +104,31 @@ public function activate(string $acronym, string $motive, string $type = ''): st
'SE' => 'SVCAN',
'SP' => 'SVCAN',
'TO' => 'SVCAN'
);
$type = strtoupper(str_replace('-', '', $type));
];
if (!empty($type)) {
$type = strtoupper(str_replace('-', '', $type));
if (!in_array($type, ['SVCAN', 'SVCRS'])) {
throw new \RuntimeException(
"O tipo indicado de contingência não é aceito nesta operação. Usar apenas SVCAN ou SVCRS"
);
}
$this->type = $type;

}
//gerar o timestamp para Greenwich (GMT).
$dt = new \DateTime(gmdate('Y-m-d H:i:s')); //data hora GMT
$this->motive = trim($motive);
$len = mb_strlen($this->motive);
if ($len < 15 || $len > 255) {
throw new \RuntimeException(
"A justificativa para entrada em contingência deve ter entre 15 e 256 caracteres UTF-8."
);
}
$this->timestamp = $dt->getTimestamp();
if (empty($type)) {
$type = $list[$acronym];
$this->type = $list[$acronym];
}
$this->config = $this->configBuild($dt->getTimestamp(), $motive, $type);
$this->config = $this->configBuild();
return $this->__toString();
}

Expand All @@ -122,11 +137,11 @@ public function activate(string $acronym, string $motive, string $type = ''): st
*/
public function deactivate(): string
{
$this->config = $this->configBuild(0, '', '');
$this->timestamp = 0;
$this->motive = '';
$this->type = '';
$this->tpEmis = 1;
$this->config = $this->configBuild();
return $this->__toString();
}

Expand All @@ -141,16 +156,10 @@ public function __toString(): string
/**
* Build parameter config as stdClass
*/
private function configBuild(int $timestamp, string $motive, string $type): \stdClass
private function configBuild(): \stdClass
{
switch ($type) {
case 'EPEC':
$tpEmis = 4;
break;
case 'FS-DA':
case 'FSDA':
$tpEmis = 5;
break;
$tpEmis = 1;
switch ($this->type) {
case 'SVC-AN':
case 'SVCAN':
$tpEmis = 6;
Expand All @@ -159,25 +168,19 @@ private function configBuild(int $timestamp, string $motive, string $type): \std
case 'SVCRS':
$tpEmis = 7;
break;
case 'OFFLINE': //this is only to model 65 NFCe
$tpEmis = 9;
break;
default:
if ($type == '') {
if ($this->type === '') {
$tpEmis = 1;
$timestamp = 0;
$motive = '';
$this->timestamp = 0;
$this->motive = '';
break;
}
throw new \InvalidArgumentException(
"Tipo de contingência "
. "[$type] não está disponível;"
);
}
$this->tpEmis = $tpEmis;
$config = new \stdClass();
$config->motive = Strings::replaceUnacceptableCharacters(substr(trim($motive), 0, 256));
$config->timestamp = $timestamp;
$config->type = $type;
$config->motive = Strings::replaceUnacceptableCharacters(substr(trim($this->motive), 0, 256));
$config->timestamp = $this->timestamp;
$config->type = $this->type;
$config->tpEmis = $tpEmis;
$this->load(json_encode($config));
return $config;
Expand Down

0 comments on commit 8837f0a

Please sign in to comment.