RF
Preencher um TCombo, com base no valor selecionado em um TDBUniq
Bom dia...
Amigos estou com um formulário onde digito o cliente, e quero que seja apresentado as lojas referentes a esse cliente selecionado. na combo.
Anexo há tabela com o relacionamento.
Abaixo segue o código completo, eu tentei escrever um código mas não retorna nada, fico agradecido desde já pela ajuda.
Amigos estou com um formulário onde digito o cliente, e quero que seja apresentado as lojas referentes a esse cliente selecionado. na combo.
Anexo há tabela com o relacionamento.
Abaixo segue o código completo, eu tentei escrever um código mas não retorna nada, fico agradecido desde já pela ajuda.
- <?php
- /**
- * digitacao_solicitacao_de_carga Master/Detail
- * @author <your name here>
- */
- class digitacao_solicitacao_de_carga extends TPage
- {
- protected $form; // form
- protected $detail_list;
- /**
- * Page constructor
- */
- public function __construct()
- {
- parent::__construct();
- // creates the form
- $this->form = new BootstrapFormBuilder('form_solicitacao_de_carga');
- $this->form->setFormTitle('Solicitação de carga');
- $this->form->setFieldSizes('100%');
- // master fields
- $id = new TEntry('id');
- $status_carga = new TEntry('status_carga');
- $servico = new TEntry('servico');
- $data_cadastro = new TDate('data_cadastro');
- $unidade_devolucao = new TEntry('unidade_devolucao');
- $unidade_coleta = new TEntry('unidade_coleta');
- $uf_origem = new TEntry('uf_origem');
- $cidade_origem = new TEntry('cidade_origem');
- $uf_destino = new TEntry('uf_destino');
- $cidade_destino = new TEntry('cidade_destino');
- $prazo_entrega = new TEntry('prazo_entrega');
- $unidade_id = new TDBCombo ('unidade_id','banco','unidade','id','razao_social');
- $cliente_id = new TDBUniqueSearch('cliente_id','banco','cliente','id','razao_social');//TEntry('cliente_id');
- $loja_id = new TCombo('loja_id');//TDBCombo ('loja_id', 'banco','loja', 'id','nome_fantasia');//TEntry('loja_id');
- $remetente_cnpj = new TEntry('remetente_cnpj');
- $remetente_ie = new TEntry('remetente_ie');
- $remetente_rz_social = new TEntry('remetente_rz_social');
- $remetente_cep = new TEntry('remetente_cep');
- $remetente_uf = new TEntry('remetente_uf');
- $remetente_cidade = new TEntry('remetente_cidade');
- $remetente_endereco = new TEntry('remetente_endereco');
- $remetente_numero = new TEntry('remetente_numero');
- $remetente_bairro = new TEntry('remetente_bairro');
- $remetente_complemento = new TEntry('remetente_complemento');
- $remetente_telefone = new TEntry('remetente_telefone');
- $remetente_email = new TEntry('remetente_email');
- $destinatario_cnpj = new TEntry('destinatario_cnpj');
- $destinatario_ie = new TEntry('destinatario_ie');
- $destinatario_rz_social = new TEntry('destinatario_rz_social');
- $destinatario_cep = new TEntry('destinatario_cep');
- $destinatario_uf = new TEntry('destinatario_uf');
- $destinatario_id = new TDBCombo('destinatario_id','banco','destinatario','id','cnpj_cpf'); //TEntry('destinatario_id');
- $destinatario_cidade = new TEntry('destinatario_cidade');
- $destinatario_endereco = new TEntry('destinatario_endereco');
- $destinatario_numero = new TEntry('destinatario_numero');
- $destinatario_bairro = new TEntry('destinatario_bairro');
- $destinatario_completo = new TEntry('destinatario_completo');
- $destinatario_telefone = new TEntry('destinatario_telefone');
- $destinatario_email = new TEntry('destinatario_email');
- $expedior_cnpj = new TEntry('expedior_cnpj');
- $expedior_ie = new TEntry('expedior_ie');
- $expedior_rz_social = new TEntry('expedior_rz_social');
- $expedior_cep = new TEntry('expedior_cep');
- $expedior_uf = new TEntry('expedior_uf');
- $expedior_cidade = new TEntry('expedior_cidade');
- $expedior_endereco = new TEntry('expedior_endereco');
- $expedior_numero = new TEntry('expedior_numero');
- $expedior_bairro = new TEntry('expedior_bairro');
- $expedior_complemento = new TEntry('expedior_complemento');
- $expedior_telefone = new TEntry('expedior_telefone');
- $expedior_email = new TEntry('expedior_email');
- $tomador_cnpj = new TEntry('tomador_cnpj');
- $tomador_ie = new TEntry('tomador_ie');
- $tomador_rz_social = new TEntry('tomador_rz_social');
- $tomador_cep = new TEntry('tomador_cep');
- $tomador_uf = new TEntry('tomador_uf');
- $tomador_cidade = new TEntry('tomador_cidade');
- $tomador_endereco = new TEntry('tomador_endereco');
- $tomador_numero = new TEntry('tomador_numero');
- $tomador_bairro = new TEntry('tomador_bairro');
- $tomador_complemento = new TEntry('tomador_complemento');
- $tomador_telefone = new TEntry('tomador_telefone');
- $tomador_email = new TEntry('tomador_email');
- $tipo_de_documento = new TCombo('tipo_de_documento');//TEntry
- $valor_rs_total = new TEntry('valor_rs_total');
- $volume_total = new TEntry('volume_total');
- $peso_total = new TEntry('peso_total');
- $cubagem_total = new TEntry('cubagem_total');
- $peso_cubado_total = new TEntry('peso_cubado_total');
- $peso_taxado = new TEntry('peso_taxado');
- // detail fields
- $detail_id = new THidden('detail_id');
- $detail_nota_fiscal = new TEntry('detail_nota_fiscal');
- $detail_serie_nota_fiscal = new TEntry('detail_serie_nota_fiscal');
- $detail_danfe = new TEntry('detail_danfe');
- $detail_emissao = new TDate('detail_emissao');
- $detail_valor = new TEntry('detail_valor');
- $detail_pedido = new TEntry('detail_pedido');
- $detail_entrega = new TEntry('detail_entrega');
- $detail_carga = new TEntry('detail_carga');
- $detail_volumes = new TEntry('detail_volumes');
- $detail_peso_informado = new TEntry('detail_peso_informado');
- //$detail_peso_medido = new TEntry('detail_peso_medido');
- $detail_fator_cubagem = new TEntry('detail_fator_cubagem');
- $detail_comprimento = new TEntry('detail_comprimento');
- $detail_largura = new TEntry('detail_largura');
- $detail_altura = new TEntry('detail_altura');
- $detail_cubagem = new TEntry('detail_cubagem');
- $detail_peso_taxado = new TEntry('detail_peso_taxado');
- //$detail_itens_notas_fiscais_carga_id = new TEntry('detail_itens_notas_fiscais_carga_id');
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
- //Mascara da data
- $data_cadastro ->setMask('dd/mm/yyyy');
- $detail_emissao ->setMask('dd/mm/yyyy');
- // Busca as informações e preenche os campos da loja
- $loja_id ->setChangeAction(new TAction([$this,'buscaDadosLoja']));
- $combo_tipodoc = array();
- $combo_tipodoc ['NF-e'] = 'NF-e';
- $combo_tipodoc ['Declaração'] = 'Declaração';
- $tipo_de_documento -> addItems($combo_tipodoc);
- $tipo_de_documento->setValue('NF-e');// default value
- //******************************
- $cliente_action = new TAction(array($this, 'onChangeAction'));
- $cliente_id->setChangeAction($cliente_action);
- // master fields
- // Primeira Aba
- $this->form->appendPage('Dados da nota');
- $row = $this->form->addFields( [ new TLabel('Número da solicitação'), $id ],
- [ new TLabel('Unidade'), $unidade_id ],
- [ new TLabel('Status'), $status_carga ],
- [ new TLabel('Data de digitação'), $data_cadastro ] );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-3','col-sm-3' ];
- // Campos das unidades
- $this->form->addContent( ['<h4>Dados das unidades</h4><hr>'] );
- $row = $this->form->addFields( [ new TLabel('Cliente'), $cliente_id ],
- [ new TLabel('Loja'), $loja_id ],
- [ new TLabel('Serviço'), $servico ]);
- $row->layout = ['col-sm-4', 'col-sm-4','col-sm-4' ];
- $row = $this->form->addFields( [ new TLabel('Unidade de devolução'), $unidade_devolucao ],
- [ new TLabel('Unidade de coleta'), $unidade_coleta ],
- [ new TLabel('Prazo de entrega'), $prazo_entrega ]
- );
- $row->layout = ['col-sm-4', 'col-sm-4','col-sm-4' ];
- // Campos de origem e destino
- $this->form->addContent( ['<h4>Origem e destino da carga</h4><hr>'] );
- $row = $this->form->addFields( [ new TLabel('UF origem'), $uf_origem],
- [ new TLabel('Cidade origem'), $cidade_origem ],
- [ new TLabel('UF destino'), $uf_destino],
- [ new TLabel('Cidade destino'), $cidade_destino] );
- $row->layout = ['col-sm-2', 'col-sm-4','col-sm-2', 'col-sm-4' ];
- // CAMPOS DAS NOTAS FISCAIS - APRESENTAÇÃO EM TELA
- $this->form->addContent( ['<h4>Dados da Nota Fiscal</h4><hr>'] );
- $this->form->addFields( [$detail_id] );
- $row = $this->form->addFields( [ new TLabel('Tipo de documento'), $tipo_de_documento ],
- [ new TLabel('Número do documento'),$detail_nota_fiscal ],
- [ new TLabel('Série'), $detail_serie_nota_fiscal ],
- [ new TLabel('Número da DANFE'), $detail_danfe ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-3','col-sm-3' ];
- // $this->form->addFields( [new TLabel('Tipo De Documento')], [$tipo_de_documento] );
- $row = $this->form->addFields( [ new TLabel('Emissão - DANFE'), $detail_emissao],
- [ new TLabel('Valor'), $detail_valor ],
- [ new TLabel('Volume'), $detail_volumes],
- [ new TLabel('Peso'), $detail_peso_informado] );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-3', 'col-sm-3' ];
- $row = $this->form->addFields( [ new TLabel('Comprimento'), $detail_comprimento ],
- [ new TLabel('Largura'), $detail_largura ],
- [ new TLabel('Altura'), $detail_altura ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
- $row = $this->form->addFields( [ new TLabel('Fator cubagem'), $detail_fator_cubagem ],
- [ new TLabel('Cubagem'), $detail_cubagem ],
- [ new TLabel('Peso Taxado'), $detail_peso_taxado ]
- );
- $row->layout = ['col-sm-4', 'col-sm-4','col-sm-4' ];
- $row = $this->form->addFields( [ new TLabel('Pedido'), $detail_pedido ],
- [ new TLabel('Entrega'), $detail_entrega ],
- [ new TLabel('Carga'), $detail_carga ]
- );
- $row->layout = ['col-sm-4', 'col-sm-4','col-sm-4' ];
- //$this->form->addFields( [new TLabel('Peso Medido')], [$detail_peso_medido] );
- //$this->form->addFields( [new TLabel('Itens Notas Fiscais Carga Id')], [$detail_itens_notas_fiscais_carga_id] );
- $add = TButton::create('add', [$this, 'onSaveDetail'], 'Register', 'fa:save');
- $this->form->addFields( [], [$add] )->style = 'background: whitesmoke; padding: 5px; margin: 1px;';
- $this->detail_list = new BootstrapDatagridWrapper(new TQuickGrid);
- $this->detail_list->style = "min-width: 700px; width:100%;margin-bottom: 10px";
- $this->detail_list->setId('solicitacao_de_carga_list');
- // items
- $this->detail_list->addQuickColumn('N.F', 'nota_fiscal', 'left', 100);
- $this->detail_list->addQuickColumn('Série', 'serie_nota_fiscal', 'left', 100);
- //$this->detail_list->addQuickColumn('Danfe', 'danfe', 'left', 100);
- //$this->detail_list->addQuickColumn('Emissao', 'emissao', 'left', 100);
- $this->detail_list->addQuickColumn('Valor', 'valor', 'left', 100);
- //$this->detail_list->addQuickColumn('Pedido', 'pedido', 'left', 100);
- //$this->detail_list->addQuickColumn('Entrega', 'entrega', 'left', 100);
- //$this->detail_list->addQuickColumn('Carga', 'carga', 'left', 100);
- $this->detail_list->addQuickColumn('Volumes', 'volumes', 'left', 100);
- $this->detail_list->addQuickColumn('Peso', 'peso_informado', 'left', 100);
- //$this->detail_list->addQuickColumn('Peso Medido', 'peso_medido', 'left', 100);
- $this->detail_list->addQuickColumn('Fator Cubagem', 'fator_cubagem', 'left', 100);
- //$this->detail_list->addQuickColumn('Comprimento', 'comprimento', 'left', 100);
- //$this->detail_list->addQuickColumn('Largura', 'largura', 'left', 100);
- //$this->detail_list->addQuickColumn('Altura', 'altura', 'left', 100);
- $this->detail_list->addQuickColumn('Cubagem', 'cubagem', 'left', 100);
- $this->detail_list->addQuickColumn('Peso Taxado', 'peso_taxado', 'left', 100);
- //$this->detail_list->addQuickColumn('Itens Notas Fiscais Carga Id', 'itens_notas_fiscais_carga_id', 'left', 100);
- // detail actions
- $this->detail_list->addQuickAction( 'Edit', new TDataGridAction([$this, 'onEditDetail']), 'id', 'fa:edit blue');
- $this->detail_list->addQuickAction( 'Delete', new TDataGridAction([$this, 'onDeleteDetail']), 'id', 'fa:trash red');
- $this->detail_list->createModel();
- $panel = new TPanelGroup;
- $panel->add($this->detail_list);
- $panel->getBody()->style = 'overflow-x:auto';
- $this->form->addContent( [$panel] );
- // Campos que apresentam o total da(s) nota(s)
- $this->form->addContent( ['<h4>Valor total das notas</h4><hr>'] );
- $row = $this->form->addFields( [ new TLabel('Total R$'), $valor_rs_total],
- [ new TLabel('Total de volume'), $volume_total ],
- [ new TLabel('Total de peso'), $peso_total],
- [ new TLabel('Cubagem'), $cubagem_total ],
- [ new TLabel('Peso cubado'), $peso_cubado_total],
- [ new TLabel('Peso taxado'), $peso_taxado] );
- $row->layout = ['col-sm-2', 'col-sm-2','col-sm-2', 'col-sm-2','col-sm-2', 'col-sm-2' ];
- // Segunda Aba
- $this->form->appendPage('Remetente');
- $row = $this->form->addFields( [ new TLabel('CNPJ'), $remetente_cnpj ],
- [ new TLabel('Inscrição Estadual'), $remetente_ie ],
- [ new TLabel('Razão Social'), $remetente_rz_social ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
- $row = $this->form->addFields( [ new TLabel('CEP'), $remetente_cep ],
- [ new TLabel('UF'), $remetente_uf ],
- [ new TLabel('Cidade'), $remetente_cidade ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
- $row = $this->form->addFields( [ new TLabel('Endereço'), $remetente_endereco ],
- [ new TLabel('Número'), $remetente_numero ]
- );
- $row->layout = ['col-sm-8', 'col-sm-4' ];
- $row = $this->form->addFields( [ new TLabel('Bairro'), $remetente_bairro],
- [ new TLabel('Complemento'), $remetente_complemento ]
- );
- $row->layout = ['col-sm-4', 'col-sm-8' ];
- $row = $this->form->addFields( [ new TLabel('Telefone'), $remetente_telefone],
- [ new TLabel('E-mail'), $remetente_email ]
- );
- $row->layout = ['col-sm-4', 'col-sm-8' ];
- // Terceira Aba
- $this->form->appendPage('Destinatário');
- $row = $this->form->addFields( [ new TLabel('CNPJ'), $destinatario_id], //$destinatario_cnpj ],
- [ new TLabel('Inscrição Estadual'), $destinatario_ie ],
- [ new TLabel('Razão Social'), $destinatario_rz_social ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
- $row = $this->form->addFields( [ new TLabel('CEP'), $destinatario_cep ],
- [ new TLabel('UF'), $destinatario_uf ],
- [ new TLabel('Cidade'), $destinatario_cidade ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
- $row = $this->form->addFields( [ new TLabel('Endereço'), $destinatario_endereco ],
- [ new TLabel('Número'), $destinatario_numero ]
- );
- $row->layout = ['col-sm-8', 'col-sm-4' ];
- $row = $this->form->addFields( [ new TLabel('Bairro'), $destinatario_bairro],
- [ new TLabel('Complemento'), $destinatario_completo ]
- );
- $row->layout = ['col-sm-4', 'col-sm-8' ];
- $row = $this->form->addFields( [ new TLabel('Telefone'), $destinatario_telefone],
- [ new TLabel('E-mail'), $destinatario_email ]
- );
- $row->layout = ['col-sm-4', 'col-sm-8' ];
- // Quarta Aba
- $this->form->appendPage('Expedidor');
- $row = $this->form->addFields( [ new TLabel('CNPJ'), $expedior_cnpj ],
- [ new TLabel('Inscrição Estadual.'), $expedior_ie ],
- [ new TLabel('Razão Social'), $expedior_rz_social ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
- $row = $this->form->addFields( [ new TLabel('CEP'), $expedior_cep ],
- [ new TLabel('UF'), $expedior_uf ],
- [ new TLabel('Cidade'), $expedior_cidade ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
- $row = $this->form->addFields( [ new TLabel('Endereço'), $expedior_endereco ],
- [ new TLabel('Número'), $expedior_numero ]
- );
- $row->layout = ['col-sm-8', 'col-sm-4' ];
- $row = $this->form->addFields( [ new TLabel('Bairro'), $expedior_bairro],
- [ new TLabel('Complemento'), $expedior_complemento]
- );
- $row->layout = ['col-sm-4', 'col-sm-8' ];
- $row = $this->form->addFields( [ new TLabel('Telefone'), $expedior_telefone],
- [ new TLabel('E-mail'), $expedior_email ]
- );
- $row->layout = ['col-sm-4', 'col-sm-8' ];
- // Quinta Aba
- $this->form->appendPage('Tomador');
- //$this->form->appendPage('Expedidor');
- $row = $this->form->addFields( [ new TLabel('CNPJ'), $tomador_cnpj ],
- [ new TLabel('Inscrição Estadual.'), $tomador_ie ],
- [ new TLabel('Razão Social'), $tomador_rz_social ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
- $row = $this->form->addFields( [ new TLabel('CEP'), $tomador_cep ],
- [ new TLabel('UF'), $tomador_uf ],
- [ new TLabel('Cidade'), $tomador_cidade ]
- );
- $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
- $row = $this->form->addFields( [ new TLabel('Endereço'), $tomador_endereco ],
- [ new TLabel('Número'), $tomador_numero ]
- );
- $row->layout = ['col-sm-8', 'col-sm-4' ];
- $row = $this->form->addFields( [ new TLabel('Bairro'), $tomador_bairro],
- [ new TLabel('Complemento'), $tomador_complemento]
- );
- $row->layout = ['col-sm-4', 'col-sm-8' ];
- $row = $this->form->addFields( [ new TLabel('Telefone'), $tomador_telefone],
- [ new TLabel('E-mail'), $tomador_email ]
- );
- $row->layout = ['col-sm-4', 'col-sm-8' ];
- $btn = $this->form->addAction( _t('Save'), new TAction([$this, 'onSave']), 'fa:save');
- $btn->class = 'btn btn-sm btn-primary';
- $this->form->addAction( _t('Clear'), new TAction([$this, 'onClear']), 'fa:eraser red');
- // create the page container
- $container = new TVBox;
- $container->style = 'width: 90%';
- $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
- /**
- * Clear form
- * @param $param URL parameters
- */
- public function onClear($param)
- {
- $this->form->clear(TRUE);
- TSession::setValue(__CLASS__.'_items', array());
- $this->onReload( $param );
- }
- /**
- * Busca os dados da loja
- */
- static function buscaDadosLoja( $params )
- {
- if( !empty($params['loja_id']) )
- {
- try
- {
- TTransaction::open('banco');
- // MODEL
- $loja = new loja($params['loja_id']);
- $campos_model = new StdClass;
- $campos_model ->servico = $loja->razao_social;
- $campos_model ->uf_origem = $loja->uf;
- $campos_model ->cidade_origem = $loja->cidade;
- // Remetente, expedidor e toamador
- $campos_model ->remetente_rz_social = $loja->razao_social; // campo do form = model->campo na model
- $campos_model ->expedior_rz_social = $loja->razao_social;
- $campos_model ->tomador_rz_social = $loja->razao_social;
- $campos_model ->remetente_cnpj = $loja->cnpj;
- $campos_model ->expedior_cnpj = $loja->cnpj;
- $campos_model ->tomador_cnpj = $loja->cnpj;
- $campos_model ->remetente_ie = $loja->ie;
- $campos_model ->expedior_ie = $loja->ie;
- $campos_model ->tomador_ie = $loja->ie;
- $campos_model ->remetente_cep = $loja->cep;
- $campos_model ->expedior_cep = $loja->cep;
- $campos_model ->tomador_cep = $loja->cep;
- $campos_model ->remetente_uf = $loja->uf;
- $campos_model ->expedior_uf = $loja->uf;
- $campos_model ->tomador_uf = $loja->uf;
- $campos_model ->remetente_cidade = $loja->cidade;
- $campos_model ->expedior_cidade = $loja->cidade;
- $campos_model ->tomador_cidade = $loja->cidade;
- $campos_model ->remetente_endereco = $loja->endereco;
- $campos_model ->expedior_endereco = $loja->endereco;
- $campos_model ->tomador_endereco = $loja->endereco;
- $campos_model ->remetente_numero = $loja->numero;
- $campos_model ->expedior_numero = $loja->numero;
- $campos_model ->tomador_numero = $loja->numero;
- $campos_model ->remetente_bairro = $loja->bairro;
- $campos_model ->expedior_bairro = $loja->bairro;
- $campos_model ->tomador_bairro = $loja->bairro;
- $campos_model ->remetente_complemento = $loja->complemento;
- $campos_model ->expedior_complemento = $loja->complemento;
- $campos_model ->tomador_complemento = $loja->complemento;
- $campos_model ->remetente_telefone = $loja->telefone;
- $campos_model ->expedior_telefone = $loja->telefone;
- $campos_model ->tomador_telefone = $loja->telefone;
- $campos_model ->remetente_email = $loja->email;
- $campos_model ->expedior_email = $loja->email;
- $campos_model ->tomador_email = $loja->email;
- TForm::sendData('form_solicitacao_de_carga', $campos_model);
- TTransaction::close();
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- }
- public static function onChangeAction($param)
- {
- try
- {
- $object = new StdClass;
- $object->iduf = $param['cliente_id'];
- $clienteloja = new Cliente;
- $cs = $clienteloja->loja($object->id);
- $item = array();
- foreach($cs as $c)
- {
- $item[$clienteloja] = $c['loja_id'];
- }
- TCombo::reload('form_solicitacao_de_carga', 'loja_id', $item);
- }
- catch (Exception $e)
- {
- new TMessage('error',$e->getMessage());
- }
- }
- /**
- * Save an item from form to session list
- * @param $param URL parameters
- */
- public function onSaveDetail( $param )
- {
- try
- {
- TTransaction::open('banco');
- $data = $this->form->getData();
- /** validation sample
- if (empty($data->fieldX))
- {
- throw new Exception('The field fieldX is required');
- }
- **/
- $items = TSession::getValue(__CLASS__.'_items');
- $key = empty($data->detail_id) ? 'X'.mt_rand(1000000000, 1999999999) : $data->detail_id;
- $items[ $key ] = array();
- $items[ $key ]['id'] = $key;
- $items[ $key ]['nota_fiscal'] = $data->detail_nota_fiscal;
- $items[ $key ]['serie_nota_fiscal'] = $data->detail_serie_nota_fiscal;
- $items[ $key ]['danfe'] = $data->detail_danfe;
- $items[ $key ]['emissao'] = $data->detail_emissao;
- $items[ $key ]['valor'] = $data->detail_valor;
- $items[ $key ]['pedido'] = $data->detail_pedido;
- $items[ $key ]['entrega'] = $data->detail_entrega;
- $items[ $key ]['carga'] = $data->detail_carga;
- $items[ $key ]['volumes'] = $data->detail_volumes;
- $items[ $key ]['peso_informado'] = $data->detail_peso_informado;
- //$items[ $key ]['peso_medido'] = $data->detail_peso_medido;
- $items[ $key ]['fator_cubagem'] = $data->detail_fator_cubagem;
- $items[ $key ]['comprimento'] = $data->detail_comprimento;
- $items[ $key ]['largura'] = $data->detail_largura;
- $items[ $key ]['altura'] = $data->detail_altura;
- $items[ $key ]['cubagem'] = $data->detail_cubagem;
- $items[ $key ]['peso_taxado'] = $data->detail_peso_taxado;
- //$items[ $key ]['itens_notas_fiscais_carga_id'] = $data->detail_itens_notas_fiscais_carga_id;
- TSession::setValue(__CLASS__.'_items', $items);
- // clear detail form fields
- $data->detail_id = '';
- $data->detail_nota_fiscal = '';
- $data->detail_serie_nota_fiscal = '';
- $data->detail_danfe = '';
- $data->detail_emissao = '';
- $data->detail_valor = '';
- $data->detail_pedido = '';
- $data->detail_entrega = '';
- $data->detail_carga = '';
- $data->detail_volumes = '';
- $data->detail_peso_informado = '';
- //$data->detail_peso_medido = '';
- $data->detail_fator_cubagem = '';
- $data->detail_comprimento = '';
- $data->detail_largura = '';
- $data->detail_altura = '';
- $data->detail_cubagem = '';
- $data->detail_peso_taxado = '';
- //$data->detail_itens_notas_fiscais_carga_id = '';
- TTransaction::close();
- $this->form->setData($data);
- $this->onReload( $param ); // reload the items
- }
- catch (Exception $e)
- {
- $this->form->setData( $this->form->getData());
- new TMessage('error', $e->getMessage());
- }
- }
- /**
- * Load an item from session list to detail form
- * @param $param URL parameters
- */
- public static function onEditDetail( $param )
- {
- // read session items
- $items = TSession::getValue(__CLASS__.'_items');
- // get the session item
- $item = $items[ $param['key'] ];
- $data = new stdClass;
- $data->detail_id = $item['id'];
- $data->detail_nota_fiscal = $item['nota_fiscal'];
- $data->detail_serie_nota_fiscal = $item['serie_nota_fiscal'];
- $data->detail_danfe = $item['danfe'];
- $data->detail_emissao = $item['emissao'];
- $data->detail_valor = $item['valor'];
- $data->detail_pedido = $item['pedido'];
- $data->detail_entrega = $item['entrega'];
- $data->detail_carga = $item['carga'];
- $data->detail_volumes = $item['volumes'];
- $data->detail_peso_informado = $item['peso_informado'];
- //$data->detail_peso_medido = $item['peso_medido'];
- $data->detail_fator_cubagem = $item['fator_cubagem'];
- $data->detail_comprimento = $item['comprimento'];
- $data->detail_largura = $item['largura'];
- $data->detail_altura = $item['altura'];
- $data->detail_cubagem = $item['cubagem'];
- $data->detail_peso_taxado = $item['peso_taxado'];
- //$data->detail_itens_notas_fiscais_carga_id = $item['itens_notas_fiscais_carga_id'];
- // fill detail fields
- TForm::sendData( 'form_solicitacao_de_carga', $data );
- }
- /**
- * Delete an item from session list
- * @param $param URL parameters
- */
- public static function onDeleteDetail( $param )
- {
- // reset items
- $data = new stdClass;
- $data->detail_nota_fiscal = '';
- $data->detail_serie_nota_fiscal = '';
- $data->detail_danfe = '';
- $data->detail_emissao = '';
- $data->detail_valor = '';
- $data->detail_pedido = '';
- $data->detail_entrega = '';
- $data->detail_carga = '';
- $data->detail_volumes = '';
- $data->detail_peso_informado = '';
- // $data->detail_peso_medido = '';
- $data->detail_fator_cubagem = '';
- $data->detail_comprimento = '';
- $data->detail_largura = '';
- $data->detail_altura = '';
- $data->detail_cubagem = '';
- $data->detail_peso_taxado = '';
- //$data->detail_itens_notas_fiscais_carga_id = '';
- // clear form data
- TForm::sendData('form_solicitacao_de_carga', $data );
- // read session items
- $items = TSession::getValue(__CLASS__.'_items');
- // get detail id
- $detail_id = $param['key'];
- // delete the item from session
- unset($items[ $detail_id ] );
- // rewrite session items
- TSession::setValue(__CLASS__.'_items', $items);
- // delete item from screen
- TScript::create("ttable_remove_row_by_id('solicitacao_de_carga_list', '{$detail_id}')");
- }
- /**
- * Load the items list from session
- * @param $param URL parameters
- */
- public function onReload($param)
- {
- // read session items
- $items = TSession::getValue(__CLASS__.'_items');
- $this->detail_list->clear(); // clear detail list
- if ($items)
- {
- foreach ($items as $list_item)
- {
- $item = (object) $list_item;
- $row = $this->detail_list->addItem( $item );
- $row->id = $list_item['id'];
- }
- }
- $this->loaded = TRUE;
- }
- /**
- * Load Master/Detail data from database to form/session
- */
- public function onEdit($param)
- {
- try
- {
- TTransaction::open('banco');
- if (isset($param['key']))
- {
- $key = $param['key'];
- $object = new solicitacao_de_carga($key);
- $items = notas_fiscais_carga::where('solicitacao_de_carga_id', '=', $key)->load();
- $session_items = array();
- foreach( $items as $item )
- {
- $item_key = $item->id;
- $session_items[$item_key] = $item->toArray();
- $session_items[$item_key]['id'] = $item->id;
- $session_items[$item_key]['nota_fiscal'] = $item->nota_fiscal;
- $session_items[$item_key]['serie_nota_fiscal'] = $item->serie_nota_fiscal;
- $session_items[$item_key]['danfe'] = $item->danfe;
- $session_items[$item_key]['emissao'] = $item->emissao;
- $session_items[$item_key]['valor'] = $item->valor;
- $session_items[$item_key]['pedido'] = $item->pedido;
- $session_items[$item_key]['entrega'] = $item->entrega;
- $session_items[$item_key]['carga'] = $item->carga;
- $session_items[$item_key]['volumes'] = $item->volumes;
- $session_items[$item_key]['peso_informado'] = $item->peso_informado;
- //$session_items[$item_key]['peso_medido'] = $item->peso_medido;
- $session_items[$item_key]['fator_cubagem'] = $item->fator_cubagem;
- $session_items[$item_key]['comprimento'] = $item->comprimento;
- $session_items[$item_key]['largura'] = $item->largura;
- $session_items[$item_key]['altura'] = $item->altura;
- $session_items[$item_key]['cubagem'] = $item->cubagem;
- $session_items[$item_key]['peso_taxado'] = $item->peso_taxado;
- //$session_items[$item_key]['itens_notas_fiscais_carga_id'] = $item->itens_notas_fiscais_carga_id;
- }
- TSession::setValue(__CLASS__.'_items', $session_items);
- $this->form->setData($object); // fill the form with the active record data
- $this->onReload( $param ); // reload items list
- TTransaction::close(); // close transaction
- }
- else
- {
- $this->form->clear(TRUE);
- TSession::setValue(__CLASS__.'_items', null);
- $this->onReload( $param );
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- /**
- * Save the Master/Detail data from form/session to database
- */
- public function onSave()
- {
- try
- {
- // open a transaction with database
- TTransaction::open('banco');
- $data = $this->form->getData();
- $master = new solicitacao_de_carga;
- $master->fromArray( (array) $data);
- $this->form->validate(); // form validation
- $master->store(); // save master object
- // delete details
- $old_items = notas_fiscais_carga::where('solicitacao_de_carga_id', '=', $master->id)->load();
- $keep_items = array();
- // get session items
- $items = TSession::getValue(__CLASS__.'_items');
- if( $items )
- {
- foreach( $items as $item )
- {
- if (substr($item['id'],0,1) == 'X' ) // new record
- {
- $detail = new notas_fiscais_carga;
- }
- else
- {
- $detail = notas_fiscais_carga::find($item['id']);
- }
- $detail->nota_fiscal = $item['nota_fiscal'];
- $detail->serie_nota_fiscal = $item['serie_nota_fiscal'];
- $detail->danfe = $item['danfe'];
- $detail->emissao = $item['emissao'];
- $detail->valor = $item['valor'];
- $detail->pedido = $item['pedido'];
- $detail->entrega = $item['entrega'];
- $detail->carga = $item['carga'];
- $detail->volumes = $item['volumes'];
- $detail->peso_informado = $item['peso_informado'];
- //$detail->peso_medido = $item['peso_medido'];
- $detail->fator_cubagem = $item['fator_cubagem'];
- $detail->comprimento = $item['comprimento'];
- $detail->largura = $item['largura'];
- $detail->altura = $item['altura'];
- $detail->cubagem = $item['cubagem'];
- $detail->peso_taxado = $item['peso_taxado'];
- //$detail->itens_notas_fiscais_carga_id = $item['itens_notas_fiscais_carga_id'];
- $detail->solicitacao_de_carga_id = $master->id;
- $detail->store();
- $keep_items[] = $detail->id;
- }
- }
- if ($old_items)
- {
- foreach ($old_items as $old_item)
- {
- if (!in_array( $old_item->id, $keep_items))
- {
- $old_item->delete();
- }
- }
- }
- TTransaction::close(); // close the transaction
- // reload form and session items
- $this->onEdit(array('key'=>$master->id));
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback();
- }
- }
- /**
- * Show the page
- */
- public function show()
- {
- // check if the datagrid is already loaded
- if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
- {
- $this->onReload( func_get_arg(0) );
- }
- parent::show();
- }
- }
Boa noite, testei aqui mas deu um erro, não entendi o que quer dizer.
Sem transação ativa com a base de dados:AdiantiDatabaseTRecord::load cliente
Antes de qualquer operação com consulta a banco de dados é preciso abrir uma transação:
Bom dia, Nataniel.
Muito obrigado, funcionou, estou pulando de alegria, rssss.
As coisas estão caminhando bem no meu projeto, tenho ainda algumas dúvidas sobre outros pontos nesse mesmo formulário, mas serão tópicos para outros posts no forum. por enquanto é isso. Valeuuuuuu.
public static function onChangeAction($params)
{
try
{
TTransaction::open('banco');
$cliente_loja = new cliente_loja($params['cliente_id']);
//primeiro passo é instanciar o cliente através do seu id($param['id'])
$lojas = $cliente_loja->getLojas();
// se vc criou a composicao entre cliente e loja, deve existir a função getLojas no model Cliente
$item = array();
foreach($lojas as $loja)
{
$item[$loja->id] = $loja->razao_social;
}
TCombo::reload('form_solicitacao_de_carga', 'loja_id', $item);
TTransaction::close();
}
catch (Exception $e)
{
new TMessage('error',$e->getMessage());
}
}