Created
October 7, 2021 00:12
-
-
Save rbarros/94f47c92507f200ebfb0122ac85f9703 to your computer and use it in GitHub Desktop.
Adianti Framework - Busca Cep
This file contains hidden or 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 | |
public function onCep($param) | |
{ | |
$obj = new Cliente; // create an empty object | |
$obj->fromArray( (array) $param); // load the object with data | |
if (!empty($obj->cep)) { | |
$cep = preg_replace('/\D/', '', $obj->cep); | |
$rest = @file_get_contents( | |
"https://viacep.com.br/ws/{$cep}/json/" | |
); | |
$json = json_decode($rest); | |
if (!empty($json)) { | |
$obj->endereco = $json->logradouro; | |
$obj->bairro = $json->bairro; | |
TTransaction::open('base_dados'); // open a transaction | |
$estado = Estado::where('uf', '=', $json->uf) | |
->first(); | |
$cidade = Cidade::where('estado_id', '=', $estado->id) | |
->where('nome', 'like', "%{$json->localidade}%") | |
->first(); | |
$obj->estado_id = $estado->id; | |
$obj->cidade_id = $cidade->id; | |
$criteria = TCriteria::create( ['estado_id' => $estado->id ] ); | |
TDBCombo::reloadFromModel( | |
'form_Cliente', | |
'cidade_id', | |
'base_dados', | |
'Cidade', | |
'id', | |
'{nome}', | |
'nome', | |
$criteria, | |
true | |
); | |
TTransaction::close(); // close the transaction | |
} | |
} else if (!empty($obj->cidade_id) && !empty($obj->endereco)) { | |
TTransaction::open('base_dados'); // open a transaction | |
$cidade = Cidade::find($obj->cidade_id); | |
$estado = Estado::find($cidade->estado_id); | |
$criteria = TCriteria::create( ['estado_id' => $estado->id ] ); | |
TDBCombo::reloadFromModel( | |
'form_Cliente', | |
'cidade_id', | |
'base_dados', | |
'Cidade', | |
'id', | |
'{nome}', | |
'nome', | |
$criteria, | |
true | |
); | |
TTransaction::close(); // close the transaction | |
$win = TWindow::create('Buscar Endereço', 0.5, 0.5); | |
$datagrid = new BootstrapDatagridWrapper(new TDataGrid); | |
$vcep = new TDataGridColumn('cep', 'CEP', 'center', '15%'); | |
$vlogradouro = new TDataGridColumn('logradouro', 'Logradouro', 'center', '15%'); | |
$vbairro = new TDataGridColumn('bairro', 'Bairro', 'center', '15%'); | |
$vlocalidade = new TDataGridColumn('localidade', 'Localidade', 'center', '15%'); | |
$vuf = new TDataGridColumn('uf', 'UF', 'center', '15%'); | |
$datagrid->addColumn($vcep); | |
$datagrid->addColumn($vlogradouro); | |
$datagrid->addColumn($vbairro); | |
$datagrid->addColumn($vlocalidade); | |
$datagrid->addColumn($vuf); | |
$action1 = new TDataGridAction( | |
array($this, 'onSelect'), | |
array_merge( | |
[ | |
'id_modal' => $win->get(0)->getId(), | |
'vcep' => '{cep}', | |
'vlogradouro' => '{logradouro}', | |
'vbairro' => '{bairro}', | |
'vlocalidade' => '{localidade}', | |
'vuf' => '{uf}' | |
], | |
[ | |
'id' => $obj->id, | |
'nome_fantasia' => $obj->nome_fantasia, | |
'cnpj' => $obj->cnpj, | |
'razao_social' => $obj->razao_social, | |
'ie' => $obj->ie, | |
'endereco' => $obj->endereco, | |
'numero' => $obj->numero, | |
'bairro' => $obj->bairro, | |
'cep' => $obj->cep, | |
'estado_id' => $estado->id, | |
'cidade_id' => $obj->cidade_id | |
] | |
) | |
); | |
$action1->setDisplayCondition( | |
array($this, 'displayColumn') | |
); | |
$datagrid->addAction($action1, 'Selecionar', 'far:hand-pointer green'); | |
$datagrid->createModel(); | |
$rest = @file_get_contents( | |
"https://viacep.com.br/ws/{$estado->uf}/{$cidade->nome}/{$obj->endereco}/json/" | |
); | |
$ceps = json_decode($rest); | |
if (!empty($ceps)) { | |
foreach ($ceps as $cep) { | |
$datagrid->addItem($cep); | |
} | |
} | |
$win->add($datagrid); | |
$win->show(); | |
} else { | |
new TMessage('error', 'Você deve informar o endereço, estado e cidade para buscar o cep.'); | |
} | |
$this->form->setData($obj); | |
TScript::create("$('select[name=\"cidade_id\"]').val('{$obj->cidade_id}');"); | |
TScript::create("$('input[name=\"numero\"]').focus();"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment