Created
February 28, 2019 03:39
-
-
Save instplanet/a8fba1dd420163c73db139a3a6765aed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use function GuzzleHttp\Psr7\str; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\DB; | |
class cobrancaController extends Controller | |
{ | |
private function getDD($codcli) | |
{ | |
$dd = DB::connection('mysql2')->select("select ddd from clientes where codcli = $codcli")[0]->ddd; | |
return $dd; | |
} | |
private function addNove($numero) | |
{ | |
return substr_replace($numero, '9', '2', 0); | |
} | |
private function addDD($dd, $numero) | |
{ | |
return $dd . $numero; | |
} | |
private function ddENove($dd, $numero) | |
{ | |
$numero = $dd . $numero; | |
return substr_replace($numero, '9', '2', 0); | |
} | |
private function atualizaNumero($coluna, $valor, $where) | |
{ | |
DB::update("update cobranca set $coluna = '$valor' where codcli = $where"); | |
} | |
private function verificaNumeroComercial($numero) | |
{ | |
if($numero[0] == '3' or $numero[0] == '4') { | |
return true; | |
} | |
return false; | |
} | |
private function trataNumero($dd, $numero) { | |
$length = strlen($numero); | |
$response = new \stdClass(); | |
switch ($length) { | |
case 8: | |
$response->alterado = true; | |
$response->original = $numero; | |
$response->novo = $this->ddENove($dd, $numero); | |
return $response; | |
break; | |
case 9: | |
$response->alterado = true; | |
$response->original = $numero; | |
$response->novo = $this->addDD($dd, $numero); | |
return $response; | |
break; | |
case 10: | |
$response->alterado = true; | |
$response->original = $numero; | |
$response->novo = $this->addNove($numero); | |
return $response; | |
break; | |
case $length < 8: | |
$response->alterado = true; | |
$response->original = $numero; | |
$response->novo = ''; | |
return $response; | |
break; | |
default: | |
$response->alterado = false; | |
$response->original = $numero; | |
$response->novo = $numero; | |
return $response; | |
} | |
} | |
public function start() | |
{ | |
$results = DB::select("select * from cobranca where fone != '' or celular != '' and codcli != ''"); | |
// dd($results); | |
foreach ($results as $result) { | |
$celular = $result->celular; | |
$telefone = $result->fone; | |
$codcli = $result->codcli; | |
$dd = $this->getDD($codcli); | |
$result = $this->trataNumero($dd, $celular); | |
if ($result->alterado) { | |
$this->atualizaNumero('celular', $result->novo, $codcli); | |
echo "Celular alterado de $result->original para $result->novo </br>"; | |
} else { | |
echo "Celular não foi alterado, $result->original </br>"; | |
} | |
$result = $this->trataNumero($dd, $telefone); | |
if ($result->alterado) { | |
$this->atualizaNumero('fone', $result->novo, $codcli); | |
echo "Fone alterado de $result->original para $result->novo </br>"; | |
} else { | |
echo "Fone não foi alterado, $result->original </br>"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment