/**
  * Formata o valor
  * @param mixed $value
  * @param string $type
  * @param boolean $formatar
  * @return mixed
  */
 function formatValue($value, $type, $formatar)
 {
     switch (strtolower($type)) {
         case 'data':
         case 'dt':
         case 'date':
             $value = Date::data((string) $value);
             return $formatar ? Date::formatData($value) : $value;
             break;
         case 'timestamp':
         case 'datatime':
             $value = Date::timestamp((string) $value);
             return $formatar ? Date::formatDataTime($value) : $value;
             break;
         case 'real':
             return $formatar ? Number::real($value) : Number::float($value, 2);
         case 'hide':
             return $formatar ? null : $value;
             break;
         case 'array':
             return $formatar ? stringToArray($value) : arrayToString($value);
             break;
         default:
             return $value;
     }
 }
Пример #2
0
 public static function realExtenso($valor = 0, $maiusculas = false)
 {
     $valor = Number::float($valor, 2);
     # verifica se tem virgula decimal
     if (strpos($valor, ",") > 0) {
         # retira o ponto de milhar, se tiver
         $valor = str_replace(".", "", $valor);
         # troca a virgula decimal por ponto decimal
         $valor = str_replace(",", ".", $valor);
     }
     $singular = array("centavo", "real", "mil", "milhão", "bilhão", "trilhão", "quatrilhão");
     $plural = array("centavos", "reais", "mil", "milhões", "bilhões", "trilhões", "quatrilhões");
     $c = array("", "cem", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos");
     $d = array("", "dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta", "setenta", "oitenta", "noventa");
     $d10 = array("dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezesete", "dezoito", "dezenove");
     $u = array("", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove");
     $z = 0;
     $valor = number_format($valor, 2, ".", ".");
     $inteiro = explode(".", $valor);
     $cont = count($inteiro);
     for ($i = 0; $i < $cont; $i++) {
         for ($ii = strlen($inteiro[$i]); $ii < 3; $ii++) {
             $inteiro[$i] = "0" . $inteiro[$i];
         }
     }
     $fim = $cont - ($inteiro[$cont - 1] > 0 ? 1 : 2);
     for ($i = 0; $i < $cont; $i++) {
         $valor = $inteiro[$i];
         $rc = $valor > 100 && $valor < 200 ? "cento" : $c[$valor[0]];
         $rd = $valor[1] < 2 ? "" : $d[$valor[1]];
         $ru = $valor > 0 ? $valor[1] == 1 ? $d10[$valor[2]] : $u[$valor[2]] : "";
         $r = $rc . ($rc && ($rd || $ru) ? " e " : "") . $rd . ($rd && $ru ? " e " : "") . $ru;
         $t = $cont - 1 - $i;
         $r .= $r ? " " . ($valor > 1 ? $plural[$t] : $singular[$t]) : "";
         if ($valor == "000") {
             $z++;
         } elseif ($z > 0) {
             $z--;
         }
         if ($t == 1 && $z > 0 && $inteiro[0] > 0) {
             $r .= ($z > 1 ? " de " : "") . $plural[$t];
         }
         if ($r) {
             $rt = @$rt . ($i > 0 && $i <= $fim && $inteiro[0] > 0 && $z < 1 ? $i < $fim ? ", " : " e " : " ") . $r;
         }
     }
     if (!$maiusculas) {
         return trim($rt ? $rt : "zero");
     } elseif ($maiusculas == "2") {
         return trim(strtoupper($rt) ? strtoupper($rt) : "Zero");
     } else {
         return trim(ucwords($rt) ? ucwords($rt) : "Zero");
     }
 }
 function calcFrete($cepRemetente, $cepDestinatario)
 {
     if (empty($this->Produtos)) {
         throw new CorreiosHellperException('Nenhum produto foi adicionado para o cálculo.');
     }
     $calc = max(1, $this->getPeso() / 30);
     $dados = ['StrRetorno' => 'xml', 'nCdEmpresa' => '', 'sDsSenha' => '', 'nVlDiametro' => 0, 'nCdServico' => '41106,40010', 'sCepOrigem' => preg_replace('/[^0-9]/', NULL, $cepRemetente), 'sCepDestino' => preg_replace('/[^0-9]/', NULL, $cepDestinatario), 'nVlComprimento' => 16, 'nVlLargura' => 11, 'nVlAltura' => 2, 'nVlPeso' => max(1, min(30, $this->getPeso())), 'nCdFormato' => 1, 'sCdMaoPropria' => 'N', 'nVlValorDeclarado' => 0, 'sCdAvisoRecebimento' => 'N'];
     $url = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?' . http_build_query($dados);
     if ($xml = @simplexml_load_file($url)) {
         $frete = ['pac' => ['codigo' => 41106, 'cepR' => Mask::cep($cepRemetente), 'cepD' => Mask::cep($cepDestinatario), 'valor' => Number::float(Number::float($xml->cServico[0]->Valor, 2) * $calc, 2), 'prazo' => (int) $xml->cServico[0]->PrazoEntrega, 'errorCode' => (string) $xml->cServico[0]->Erro, 'error' => $this->errorMessage($xml->cServico[0]->Erro), 'errorMessage' => str_replace(' Busca CEP', ' <a href="http://www.buscacep.correios.com.br/" target="_blank" ><b>Busca CEP</b></a>', (string) $xml->cServico[1]->MsgErro)], 'sedex' => ['codigo' => 40010, 'cepR' => Mask::cep($cepRemetente), 'cepD' => Mask::cep($cepDestinatario), 'valor' => Number::float(Number::float($xml->cServico[1]->Valor, 2) * $calc, 2), 'prazo' => (int) $xml->cServico[1]->PrazoEntrega, 'errorCode' => (string) $xml->cServico[1]->Erro, 'error' => $this->errorMessage($xml->cServico[1]->Erro), 'errorMessage' => str_replace(' Busca CEP', ' <a href="http://www.buscacep.correios.com.br/" target="_blank" ><b>Busca CEP</b></a>', (string) $xml->cServico[1]->MsgErro)]];
         if ($frete['pac']['error']) {
             throw new CorreiosHellperException($frete['pac']['errorMessage'], (int) $frete['pac']['errorCode']);
         }
         return $frete;
     } else {
         throw new CorreiosHellperException('Não foi possível calcular o frete.', (int) '-999');
     }
 }
Пример #4
0
 /**
  * 
  * @param string $Table
  * @param array $values
  * @return array
  */
 public static function format_values($Table, array $values = null)
 {
     if (is_null($values)) {
         return $values;
     } else {
         foreach ($values as $key => $value) {
             if (is_null($value)) {
                 $values[$key] = '';
             }
         }
     }
     $infoTable = self::getTableInfo($Table);
     $result = [];
     foreach ($infoTable as $info) {
         $field = $info['Field'];
         # Verificando campo
         if (isset($values[$field])) {
             # Valor passado
             $value = $values[$field];
             # Tipo de dado
             $type = strtolower(current(explode('(', $info['Type'])));
             # Formatando tipos de valores
             if (trim($value) !== '') {
                 switch ($type) {
                     case 'date':
                         $value = Date::data($value);
                         break;
                     case 'tinyint':
                     case 'smallint':
                     case 'mediumint':
                     case 'bigint':
                     case 'int':
                         $value = Number::int($value);
                         break;
                     case 'decimal':
                     case 'float':
                     case 'double':
                     case 'real':
                         $value = Number::float($value);
                         break;
                     case 'datetime':
                     case 'timestamp':
                         $value = Date::timestamp($value);
                         break;
                     case 'time':
                         $value = Date::time($value);
                         break;
                 }
             } else {
                 if ($info['Null'] == 'NO') {
                     $value = '';
                 } else {
                     if ($info['Null'] == 'YES') {
                         $value = null;
                     }
                 }
             }
             # Setando valor na array
             $result[$field] = $value;
         }
     }
     return $result;
 }