Created
October 26, 2018 21:02
-
-
Save FredAzevedo/e7d3df9c8047ba889e3db671cadd3b93 to your computer and use it in GitHub Desktop.
Cadastro de clientes
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 | |
/** | |
* ClienteForm Form | |
* @author Fred Azv. | |
*/ | |
class ClienteForm extends TPage | |
{ | |
protected $form; // form | |
/** | |
* Form constructor | |
* @param $param Request | |
*/ | |
public function __construct( $param ) | |
{ | |
parent::__construct(); | |
$script = new TElement('script'); | |
$script->type = 'text/javascript'; | |
$javascript = " | |
// personaliza os campos de acordo com o tipo de pessoa | |
$('select[name=\"tipo\"]').change(function(event){ | |
var tipoPessoa; | |
$('select[name=\"tipo\"] > option:selected').each(function(){ | |
tipoPessoa = $(this).text(); | |
}); | |
//alert(tipoPessoa.toLowerCase()); | |
if (tipoPessoa.toLowerCase() == 'pessoa física') { | |
//$('label:contains(CNPJ/CPF)').text('CPF'); | |
//$('label:contains(CNPJ)').text('CPF'); | |
$('input[name=\"cpf_cnpj\"]').attr({onkeypress:'return tentry_mask(this,event,\"999.999.999-99\")'}).val(''); | |
} | |
if (tipoPessoa.toLowerCase() == 'pessoa jurídica') { | |
//$('label:contains(CNPJ/CPF)').text('CNPJ'); | |
//$('label:contains(CPF)').text('CNPJ'); | |
$('input[name=\"cpf_cnpj\"]').attr({onkeypress:'return tentry_mask(this,event,\"99.999.999/9999-99\")'}).val(''); | |
} | |
}); | |
"; | |
$script->add($javascript); | |
parent::add($script); | |
// creates the form | |
$this->form = new BootstrapFormBuilder('form_Cliente'); | |
$this->form->setFormTitle('Cliente'); | |
$this->form->setFieldSizes('100%'); | |
$this->form->appendPage('Dados Principais'); | |
// create the form fields | |
$id = new TEntry('id'); | |
$id_unit_session = new TCriteria(); | |
$id_unit_session->add(new TFilter('id','=',TSession::getValue('userunitid'))); | |
$unit_id = new TDBCombo('unit_id','sample','SystemUnit','id','unidade','unidade',$id_unit_session); | |
$unit_id->setValue(TSession::getValue('userunitid')); | |
$unit_id->setEditable(FALSE); | |
/*Dados Principais*/ | |
$this->form->addContent( ['<h4><b>Dados Principais</b></h4><hr>'] ); | |
$tipo = new TCombo('tipo'); | |
$tipo->setChangeAction(new TAction(array($this, 'onChangeSexo'))); | |
$combo_tipos = array(); | |
$combo_tipos['F'] = 'Pessoa Física'; | |
$combo_tipos['J'] = 'Pessoa Jurídica'; | |
$tipo->addItems($combo_tipos); | |
//$tipo->setValue('J'); | |
$cpf_cnpj = new TEntry('cpf_cnpj'); | |
$buscaCnpj = new TAction(array($this, 'onCNPJ')); | |
$cpf_cnpj->setExitAction($buscaCnpj); | |
TTransaction::open('sample'); | |
$pegarID = new Cliente($_GET['id']); | |
if (!empty($pegarID->id)) { | |
if($pegarID->tipo == 'J'){ | |
$cpf_cnpj->setMask('99.999.999/9999-99'); | |
}else{ | |
$cpf_cnpj->setMask('999.999.999-99'); | |
} | |
} | |
TTransaction::close(); | |
$rg_ie = new TEntry('rg_ie'); | |
$im = new TEntry('im'); | |
$row = $this->form->addFields( [ new TLabel('ID'), $id ], | |
[ new TLabel('Unidade'), $unit_id ], | |
[ new TLabel('Tipo'), $tipo ], | |
[ new TLabel('CPF/CNPJ'), $cpf_cnpj ], | |
[ new TLabel('RG/IE'), $rg_ie ], | |
[ new TLabel('IM'), $im ]); | |
$row->layout = ['col-sm-2', 'col-sm-2', 'col-sm-2', 'col-sm-2', 'col-sm-2', 'col-sm-2']; | |
$razao_social = new TEntry('razao_social'); | |
$nome_fantasia = new TEntry('nome_fantasia'); | |
$nascimento = new TDate('nascimento'); | |
$nascimento->setDatabaseMask('yyyy-mm-dd'); | |
$nascimento->setMask('dd/mm/yyyy'); | |
$row = $this->form->addFields( [ new TLabel('Razão Social'), $razao_social ], | |
[ new TLabel('Nome Fantasia'), $nome_fantasia ], | |
[ new TLabel('Nascimento/Fundação'), $nascimento ]); | |
$row->layout = ['col-sm-5', 'col-sm-5', 'col-sm-2']; | |
$indicador_ie = new TCombo('indicador_ie'); | |
$combo_indicador_ie['1'] = 'Contribuinte ICMS (informar a IE)'; | |
$combo_indicador_ie['2'] = 'Contribuinte insento de inscrição'; | |
$combo_indicador_ie['9'] = 'Contribuinte ICMS mas é insento de inscrição'; | |
$indicador_ie->addItems($combo_indicador_ie); | |
$site = new TEntry('site'); | |
$user_id = new TDBCombo('user_id','sample','SystemUser','id','name','name',$id_unit_session); | |
$user_id->setValue(TSession::getValue('userunitid')); | |
$user_id->setEditable(FALSE); | |
$sexo = new TCombo('sexo'); | |
$combo_sexo['M'] = 'Masculino'; | |
$combo_sexo['F'] = 'Feminino'; | |
$sexo->addItems($combo_sexo); | |
$row = $this->form->addFields( [ new TLabel('Tipo de Contribuinte'), $indicador_ie ], | |
[ new TLabel('Site'), $site ], | |
[ new TLabel('Usuário'), $user_id ]); | |
$row->layout = ['col-sm-4', 'col-sm-4', 'col-sm-4']; | |
$row = $this->form->addFields( [ new TLabel('Sexo'), $sexo ]); | |
$row->layout = ['col-sm-2']; | |
/*Endereço*/ | |
$this->form->addContent( ['<h4><b>Endereço</b></h4><hr>'] ); | |
$cep = new TEntry('cep'); | |
$buscaCep = new TAction(array($this, 'onCep')); | |
$cep->setExitAction($buscaCep); | |
$cep->setMask('99.999-999'); | |
$logradouro = new TEntry('logradouro'); | |
$numero = new TEntry('numero'); | |
$bairro = new TEntry('bairro'); | |
$complemento = new TEntry('complemento'); | |
$cidade = new TEntry('cidade'); | |
$uf = new TEntry('uf'); | |
$codMuni = new TEntry('codMuni'); | |
/*Endereço*/ | |
$row = $this->form->addFields( [ new TLabel('CEP'), $cep ], | |
[ new TLabel('Logradouro'), $logradouro ], | |
[ new TLabel('Número'), $numero ], | |
[ new TLabel('Bairro'), $bairro ]); | |
$row->layout = ['col-sm-2', 'col-sm-5', 'col-sm-1', 'col-sm-4']; | |
$row = $this->form->addFields( [ new TLabel('Complemento'), $complemento ], | |
[ new TLabel('Cidade'), $cidade ], | |
[ new TLabel('UF'), $uf ], | |
[ new TLabel('Código do IBGE'), $codMuni ]); | |
$row->layout = ['col-sm-5','col-sm-4', 'col-sm-1', 'col-sm-2']; | |
$this->form->appendPage('Contato'); | |
$this->form->addContent( ['<h4><b>Contatos</b></h4><hr>'] ); | |
/* Telefones */ | |
$tel_responsavel = new TEntry('tel_responsavel[]'); | |
$tel_telefone = new TEntry('tel_telefone[]'); | |
$tel_telefone->setMask('(84)99999-9999'); | |
//Detalhe dos telefones | |
$telefone_array = $this->fieldlist = new TFieldList; | |
$this->fieldlist->addField( '<b>Responsável</b>', $tel_responsavel); | |
$this->fieldlist->addField( '<b>Telefone</b>', $tel_telefone); | |
$this->fieldlist->enableSorting(); | |
/* Telefones */ | |
/* Emails */ | |
$email_responsavel = new TEntry('email_responsavel[]'); | |
$email_endereco = new TEntry('email_endereco[]'); | |
$tel_telefone->setMask('(84)99999-9999'); | |
//Detalhe dos emails | |
$email_array = $this->lista_email = new TFieldList; | |
$this->lista_email->addField( '<b>Responsável</b>', $email_responsavel); | |
$this->lista_email->addField( '<b>E-mail</b>', $email_endereco); | |
$this->lista_email->enableSorting(); | |
/* Emails */ | |
$row = $this->form->addFields( [ new TLabel('Telefone'), $telefone_array ], | |
[ new TLabel('Email'), $email_array ]); | |
$row->layout = ['col-sm-5', 'col-sm-5']; | |
$this->form->appendPage('Dados Secundários'); | |
$this->form->addContent( ['<h4><b>Dados Secundários</b></h4><hr>'] ); | |
$fornecedor_id = new TDBCombo('fornecedor_id', 'permission', 'Fornecedor','id', 'nome_fantasia'); | |
$fornecedor_id->enableSearch(); | |
$fornecedor_id->setSize('100%'); | |
$comissao_parceiro = new TDBCombo('comissao_parceiro','permission','ComissaoTabela','id','descricao','descricao'); | |
$comissao_vendedor = new TDBCombo('comissao_vendedor','permission','ComissaoTabela','id','descricao','descricao'); | |
$comissao_vendedor_externo = new TDBCombo('comissao_vendedor_externo','permission','ComissaoTabela','id','descricao','descricao'); | |
$tabelas_preco_cab_id = new TDBCombo('tabelas_preco_cab_id','permission','TabelaPrecosCab','id','nome','nome'); | |
$row = $this->form->addFields( [ new TLabel('Quem Indicou'), $fornecedor_id ], | |
[ new TLabel('Comissão do Indicador'), $comissao_parceiro ], | |
[ new TLabel('Comissão do Vendedor'), $comissao_vendedor ]); | |
$row->layout = ['col-sm-6', 'col-sm-3', 'col-sm-3']; | |
$row = $this->form->addFields( [ new TLabel('Comissão do Vendedor Externo'),$comissao_vendedor_externo ], | |
[ new TLabel('Tabela de Preço'),$tabelas_preco_cab_id ]); | |
$row->layout = ['col-sm-3', 'col-sm-3']; | |
if (!empty($id)) | |
{ | |
$id->setEditable(FALSE); | |
} | |
/** samples | |
$fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation | |
$fieldX->setSize( '100%' ); // set size | |
**/ | |
// create the form actions | |
$btn = $this->form->addAction(_t('Save'), new TAction([$this, 'onSave']), 'fa:floppy-o'); | |
$btn->class = 'btn btn-sm btn-primary'; | |
$this->form->addAction(_t('New'), new TAction([$this, 'onEdit']), 'fa:eraser red'); | |
$this->form->addAction('Voltar', new TAction(['ClienteList','onReload']), 'fa:angle-double-left'); | |
// vertical box container | |
$container = new TVBox; | |
$container->style = 'width: 100%'; | |
$container->add(new TXMLBreadCrumb('menu.xml','ClienteList')); | |
$container->add($this->form); | |
parent::add($container); | |
} | |
public static function onChangeSexo($param) | |
{ | |
if ($param['tipo'] == 'F') | |
{ | |
TQuickForm::showField('form_Cliente', 'sexo'); | |
} | |
else | |
{ | |
TQuickForm::hideField('form_Cliente', 'sexo'); | |
} | |
} | |
public static function onCep($param) | |
{ | |
try { | |
$retorno = Utilidades::onCep($param['cep']); | |
$objeto = json_decode($retorno); | |
if (isset($objeto->logradouro)){ | |
$obj = new stdClass(); | |
$obj->logradouro = $objeto->logradouro; | |
$obj->bairro = $objeto->bairro; | |
$obj->cidade = $objeto->localidade; | |
$obj->uf = $objeto->uf; | |
$obj->codMuni = $objeto->ibge; | |
TForm::sendData('form_Cliente',$obj); | |
unset($obj); | |
}else{ | |
new TMessage('info', 'Erro ao buscar endereço por este CEP.'); | |
} | |
}catch (Exception $e){ | |
new TMessage('error', '<b>Error:</b> ' . $e->getMessage()); | |
} | |
} | |
public static function onCNPJ($param) | |
{ | |
try { | |
if(strlen(trim($param['cpf_cnpj'])) == 18){ | |
$retorno = Utilidades::onCNPJ($param['cpf_cnpj']); | |
$objeto = json_decode($retorno); | |
//var_dump($objeto); | |
if (isset($objeto->nome)){ | |
$obj = new stdClass(); | |
$obj->razao_social = $objeto->nome; | |
$obj->nome_fantasia = $objeto->fantasia; | |
TForm::sendData('form_Cliente',$obj); | |
unset($obj); | |
}else{ | |
new TMessage('info', 'Erro ao buscar endereço por este CNPJ.'); | |
} | |
} | |
}catch (Exception $e){ | |
new TMessage('error', '<b>Error:</b> ' . $e->getMessage()); | |
} | |
} | |
public function onSave( $param ) | |
{ | |
try | |
{ | |
TTransaction::open('sample'); | |
//TTransaction::setLogger(new TLoggerSTD); // debugar sql | |
$this->form->validate(); | |
$data = $this->form->getData('Cliente'); | |
$data->store(); | |
//Recebendo valor da unidade cadastrada para inserir na tabela TelefonesUnidade | |
$id_cliente = $data->id; | |
$data->fromArray( $param ); | |
if( !empty($param['tel_responsavel']) AND is_array($param['tel_responsavel']) ) | |
{ | |
foreach( $param['tel_responsavel'] as $row => $tel_responsavel) | |
{ | |
if ($tel_responsavel) | |
{ | |
$tel = new TelefonesCliente; | |
$tel->cliente_id = $id_cliente; | |
$tel->responsavel = $tel_responsavel; | |
$tel->telefone = $param['tel_telefone'][$row]; | |
$tel->store(); | |
} | |
} | |
} | |
if( !empty($param['email_responsavel']) AND is_array($param['email_responsavel']) ) | |
{ | |
foreach( $param['email_responsavel'] as $row => $email_responsavel) | |
{ | |
if ($email_responsavel) | |
{ | |
$email = new EmailCliente; | |
$email->cliente_id = $id_cliente; | |
$email->responsavel = $email_responsavel; | |
$email->email = $param['email_endereco'][$row]; | |
$email->store(); | |
} | |
} | |
} | |
$this->form->setData($data); | |
TTransaction::close(); | |
new TMessage('info', TAdiantiCoreTranslator::translate('Record saved')); | |
AdiantiCoreApplication::loadPage(__CLASS__, 'onEdit', $param); | |
} | |
catch (Exception $e) // in case of exception | |
{ | |
new TMessage('error', $e->getMessage()); // shows the exception error message | |
$this->form->setData( $this->form->getData() ); // keep form data | |
TTransaction::rollback(); // undo all pending operations | |
} | |
} | |
public function onAdd(){ | |
$this->fieldlist->addHeader(); | |
$this->fieldlist->addDetail( new stdClass ); | |
$this->fieldlist->addCloneAction(); | |
$this->lista_email->addHeader(); | |
$this->lista_email->addDetail( new stdClass ); | |
$this->lista_email->addCloneAction(); | |
} | |
public function onEdit( $param ) | |
{ | |
try | |
{ | |
if(isset($param['id'])) | |
{ | |
TTransaction::open('sample'); | |
$cliente = new Cliente( $param['id'] ); | |
$this->form->setData($cliente); | |
$this->onChangeSexo( ['tipo' => $cliente->tipo] ); | |
$telefone = $cliente->getTelefonesClientes(); | |
$email = $cliente->getEmailClientes(); | |
if ($telefone) | |
{ | |
$this->fieldlist->addHeader(); | |
foreach ($telefone as $tel) | |
{ | |
$tel_detail = new stdClass; | |
$tel_detail->tel_responsavel = $tel->responsavel; | |
$tel_detail->tel_telefone = $tel->telefone; | |
$this->fieldlist->addDetail($tel_detail); | |
} | |
$this->fieldlist->addCloneAction(); | |
} | |
else | |
{ | |
// $this->onClear($param); | |
$this->fieldlist->addHeader(); | |
$this->fieldlist->addDetail( new stdClass ); | |
$this->fieldlist->addCloneAction(); | |
} | |
if ($email) | |
{ | |
$this->lista_email->addHeader(); | |
foreach ($email as $e) | |
{ | |
$email_detail = new stdClass; | |
$email_detail->email_responsavel = $e->responsavel; | |
$email_detail->email_endereco = $e->email; | |
$this->lista_email->addDetail($email_detail); | |
} | |
$this->lista_email->addCloneAction(); | |
} | |
else | |
{ | |
$this->lista_email->addHeader(); | |
$this->lista_email->addDetail( new stdClass ); | |
$this->lista_email->addCloneAction(); | |
} | |
TTransaction::close(); | |
} | |
else | |
{ | |
$this->form->clear(TRUE); | |
} | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error', $e->getMessage()); | |
TTransaction::rollback(); | |
} | |
} | |
public function onClear( $param ) | |
{ | |
$this->form->clear(TRUE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment