JM
Problema de reset de TEntry quando fazo um submit no detalhe.
Fechado
Meu codigo e quanto esse aqui, quando faço o submit no detalhe pra gravar produto ele reseta os campos no form mestre, deixando so aqueles que ficano no Tseekbutton.
- <?php
- /**
- * Formulario pra orcamentos
- * @author Jose Maldonado
- */
- class OrcamentoForm extends TPage
- {
- protected $form; // form
- protected $formFields;
- protected $dt_venda;
-
- function __construct()
- {
- parent::__construct();
-
- //parent::include_js('app/lib/include/application.js');
-
- // creates the form
- $this->form = new TForm('OrcamentoForm');
- $this->form->class = 'tform'; // CSS class
- parent::include_css('app/resources/custom-frame.css');
-
- // Cria nova tabela
- $table_master = new TTable;
- // Tamanho da tabela
- $table_master->width = '100%';
- // Adiciona linha na tabela
- $table_master->addRowSet( new TLabel('Orçamentos'), '', '')->class = 'tformtitle';
-
- // Cria nova tabela
- $table_general = new TTable;
- // Tamanho da tabela
- $table_general-> width = '100%';
- // Cria nova tabela
- $tableProduct = new TTable;
- // Tamanho da tabela
- $tableProduct-> width = '100%';
-
- // Cria nova frame
- $frame_general = new TFrame;
- // Título da frame
- $frame_general->setLegend('Destinatário');
- // Cor de fundo da frame
- //$frame_general->style = 'background:whiteSmoke';
- $frame_general->add($table_general);
- // Adiciona a frame tabela e define com tamanho de duas celulas
- $table_master->addRow()->addCell( $frame_general )->colspan=2;
- $row = $table_master->addRow();
- $row->addCell( $tableProduct );
- $this->form->add($table_master);
-
- // Titulo campos
- $lab_codi_orc = new TLabel('Orçamento:');
- $lab_data_orc = new TLabel('Emissão:');
- $lab_id_vendedor = new TLabel('Vendedor:');
- $lab_codi_cli = new TLabel('Cliente:');
- $lab_obsv_cli = new TLabel('Observação:');
- $lab_nome_comp = new TLabel('Comprador:');
- $lab_mail_comp = new TLabel('E-mail:');
- $lab_nped_cli = new TLabel('Pedido Cliente:');
- $lab_codi_vct = new TLabel('Vencimento:');
- $lab_tipo_frt = new TLabel('Frete:');
-
- // Mestre
- 2155 = new TEntry('id');
- $data = new TDate('data');
- 2155_vendedor = new TEntry('id_vendedor');
- $vendedor = new TEntry('vendedor');
- 2155_cliente = new ">TDBSeekButton('id_cliente', 'proterra', $this->form->getName(), 'Cliente', 'nome', 'id_cliente', 'nome_clie');
- $nome_clie = new TEntry('nome_clie');
- $obsv = new TEntry('obsv_orc');
- $nome_comp = new TEntry('nome_comp');
- $mail_comp = new TEntry('mail_comp');
- $nped_cli = new TEntry('nped_cli');
- $codi_vct = new ">TDBSeekButton('codi_vct', 'proterra', $this->form->getName(), 'Vencimento', 'nome', 'codi_vct', 'nome_vct');
- $nome_vct = new TEntry('nome_vct');
- $tipo_frt = new TCombo('tipo_frete');
- //Carga tipo de frete
- $frete_items = array();
- $frete_items['CIB'] ='CIB';
- $frete_items['FOB'] ='FOB';
- $tipo_frt->addItems($frete_items);
-
- //Cria os id
- 2155->setId('id');
- $data->setId('date');
- 2155_vendedor->setId('id_vendedor');
- $vendedor->setId('vendedor');
- 2155_cliente->setId('id_cliente');
- $nome_clie->setId('nome_clie');
- $obsv->setId('obsv');
- $nome_comp->setId('nome_comp');
- $mail_comp->setId('mail_comp');
- $nped_cli->setId('nped_cli');
- $codi_vct->setId('codi_vct');
- $nome_vct->setId('nome_vct');
- $tipo_frt->setId('tipo_frt');
-
- //Cria os eventos
-
- 2155_cliente->setExitAction(new TAction(array($this,'onClienteChange')));
-
-
- TScript::create('$("#id").blur(function(){$("#id_cliente").focus();});');
- TScript::create('$("#id_cliente").blur(function(){$("#obsv").focus();});');
- TScript::create('$("#nome_clie").prop("tabIndex", -1);');
-
- //Seta os valores de inicio
- $data->setValue(date("d/m/Y"));
- 2155_vendedor->setValue(TSession::getValue('login_id', $user->id));
- $vendedor->setValue(TSession::getValue('username', $user->name));
-
-
- // Detalhe
- 2155_produto = new ">TDBSeekButton('id_produto', 'proterra', $this->form->getName(), 'Produto', 'nome', 'id_produto', 'nome_prd');
- $nome_prd = new TEntry('nome_prd');
- $vunt_prd = new TEntry('vunt_prd');
- $qtde_prd = new TEntry('qtde_prd');
- $vdsc_prd = new TEntry('vdsc_prd');
- $vtot_prd = new TEntry('vtot_prd');
- $data_prd = new TEntry('data_prd');
- $total = new TEntry('total');
-
- 2155_produto->setExitAction(new TAction(array($this,'onProdutoChange')));
- $qtde_prd->setExitAction(new TAction(array($this,'onQtdChange')));
- $vdsc_prd->setExitAction(new TAction(array($this,'onQtdChange')));
-
- //Editable
- 2155->setEditable(false);
- $data->setEditable(false);
- 2155_vendedor->setEditable(false);
- $vendedor->setEditable(false);
- $nome_prd->setEditable(false);
- $nome_clie->setEditable(false);
- $nome_vct->setEditable(false);
- $vunt_prd->setEditable(false);
- $vtot_prd->setEditable(false);
-
- //Case
- $obsv->setUpperCase();
- $nome_comp->setUpperCase();
- $mail_comp->setUpperCase();
- $nped_cli->setUpperCase();
-
- // Formatacao
- 2155->setMask('999999');
- 2155_produto->setMask('999999');
- 2155_cliente->setMask('999999');
- $vunt_prd->setNumericMask(2, '.', ',', TRUE);
- $vdsc_prd->setNumericMask(2, '.', ',', TRUE);
- $vtot_prd->setNumericMask(2, '.', ',', TRUE);
- $total->setNumericMask(2, '.', ',', TRUE);
- //Comprimento maximo
- $qtde_prd->setMaxLength(6);
- $vunt_prd->setMaxLength(10);
- $vdsc_prd->setMaxLength(10);
- // Validacao
- $data->addValidation('Date', new TRequiredValidator);
- 2155_cliente->addValidation('Clientes', new TRequiredValidator);
- //adiciona jQuery para máscara e focus
- 2155_cliente->id = 'id_cliente';
- $script = new TElement('script');
- $script->type = 'text/javascript';
- $script->add('$("#id_cliente").focus();');
- $table_general->addRow()->addCell($script);
- // pedido
- $table_general->addRowSet( $lab_codi_orc, 2155 );
- $table_general->addRowSet( $lab_data_orc, $data );
- $table_general->addRowSet( $lab_id_vendedor, array( 2155_vendedor, $vendedor ) );
- $table_general->addRowSet( $lab_codi_cli, array( 2155_cliente, $nome_clie ) );
- $table_general->addRowSet( $lab_nome_comp, $nome_comp );
- $table_general->addRowSet( $lab_mail_comp, $mail_comp );
- $table_general->addRowSet( $lab_obsv_cli, $obsv );
- $table_general->addRowSet( $lab_codi_vct, array( $codi_vct, $nome_vct ) );
- $table_general->addRowSet( $lab_nped_cli, $nped_cli );
- $table_general->addRowSet( $lab_tipo_frt, $tipo_frt);
- //$label_date->setFontColor('#FF0000');
-
- // Produtos
- $frame_product = new TFrame();
- $frame_product->setLegend('Produtos');
- $row = $tableProduct->addRow();
- $row->addCell($frame_product);
-
- $add_product = new TButton('add_product');
- $action_product = new TAction(array($this, 'onProductAdd'));
- $add_product->setAction($action_product, 'Gravar Ítem');
- $add_product->setImage('fa:save');
-
- $subtable_product = new TTable;
- $frame_product->add($subtable_product);
- $subtable_product->addRowSet( $lab_id_produto = new TLabel('Produto:'), array(2155_produto,$nome_prd) );
- $subtable_product->addRowSet( $lab_vunt_prd = new TLabel('Preço de Venda:'), $vunt_prd );
- $subtable_product->addRowSet( $lab_qtde_prd = new TLabel('Quantidade:'), $qtde_prd );
- $subtable_product->addRowSet( $lab_vdsc_prd = new TLabel('Desconto:'), $vdsc_prd );
- $subtable_product->addRowSet( $lab_vtot_prd = new TLabel('Valor Total:'), $vtot_prd );
- $subtable_product->addRowSet( $lab_data_prd = new TLabel('Prazo Entrega:'), $data_prd );
- $subtable_product->addRowSet( $lab_vtot_orc = new TLabel('Total Geral:'), $vtot_prd );
- $subtable_product->addRowSet( $add_product );
-
-
- $this->product_list = new TQuickGrid;
- $this->product_list->makeScrollable();
- $this->product_list->disableDefaultClick();
- $this->product_list->addQuickColumn('', 'edit', 'left', 30);
- $this->product_list->addQuickColumn('', 'delete', 'left', 30);
- $this->product_list->addQuickColumn('Produto', 'id_produto', 'right', 40);
- $this->product_list->addQuickColumn('Descrição', 'nome_prd', 'left', 300);
- $this->product_list->addQuickColumn('Quantidade', 'qtde_prd', 'right', 60);
- $this->product_list->addQuickColumn('Vlr. Unitário', 'vunt_prd', 'right', 80);
- $this->product_list->addQuickColumn('Vlr. Total', 'vtot_prd', 'right', 80);
- $this->product_list->createModel();
-
- $row = $tableProduct->addRow();
- $row->addCell($this->product_list);
-
- // create an action button (save)
- $save_button=new TButton('save');
- $save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
- $save_button->setImage('ico_save.png');
- // create an new button (edit with no parameters)
- $new_button=new TButton('new');
- $new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
- $new_button->setImage('ico_new.png');
-
- // define form fields
- $this->formFields = array(2155, $data, 2155_cliente, $nome_clie, $obsv, $codi_vct, $nome_vct, 2155_produto, $nome_prd, $vunt_prd, $qtde_prd, $vtot_prd, $vdsc_prd, $total, $add_product, $save_button, $new_button);
-
- $this->form->setFields( $this->formFields );
-
- $table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
-
- // create the page container
- $container = new TVBox;
- $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
-
- function onClear($param)
- {
- $this->form->clear();
- TSession::setValue('sale_items', array());
- $this->onReload( $param );
- }
-
- public function onProductAdd( $param )
- {
- try
- {
- TTransaction::open('proterra');
- $data = $this->form->getData();
-
- if( (! $data->id_produto) || (! $data->qtde_prd) || (! $data->vunt_prd) )
- throw new Exception('Os campos produto, quantidade e preço de venda são obrigatórios !');
-
- $product = new Produto($data->id_produto);
-
- $sale_items = TSession::getValue('sale_items');
- $key = (int) $data->id_produto;
- $sale_items[ $key ] = array('id_produto' => $data->id_produto,
- 'nome_prd' => $data->nome_prd,
- 'qtde_prd' => $data->qtde_prd,
- 'vunt_prd' => $data->vunt_prd,
- 'vtot_prd' => $data->vtot_prd
- );
-
- TSession::setValue('sale_items', $sale_items);
-
- // clear product form fields after add
- $data->id_produto = '';
- $data->nome_prd = '';
- $data->qtde_prd = '';
- $data->vunt_prd = '';
- $data->vtot_prd = '';
- TTransaction::close();
- $this->form->setData($data);
- $this->onReload( $param ); // reload the sale items
- }
- catch (Exception $e)
- {
- $this->form->setData( $this->form->getData());
- new TMessage('error', $e->getMessage());
- }
- }
-
- public function onEditItemProduto( $param )
- {
- $data = $this->form->getData();
-
- // read session items
- $sale_items = TSession::getValue('sale_items');
-
- // get the session item
- $sale_item = $sale_items[ (int) $param['list_product_id'] ];
-
- $data->product_id = $param['list_product_id'];
- $data->nome_prd = $sale_item['nome_prd'];
- $data->qtde_prd = $sale_item['qtde_prd'];
- $data->vunt_prd = $sale_item['vunt_prd'];
- $data->vtot_prd = $sale_item['vtot_prd'];
-
- // fill product fields
- $this->form->setData( $data );
-
- $this->onReload( $param );
- }
-
- public function onDeleteItem( $param )
- {
- $data = $this->form->getData();
-
- $data->id_produto = '';
- $data->nome_prd = '';
- $data->qtde_prd = '';
- $data->vunt_prd = '';
- $data->vtot_prd = '';
-
- // clear form data
- $this->form->setData( $data );
-
- // read session items
- $sale_items = TSession::getValue('sale_items');
-
- // delete the item from session
- unset($sale_items[ (int) $param['list_product_id'] ] );
- TSession::setValue('sale_items', $sale_items);
-
- // reload sale items
- $this->onReload( $param );
- }
-
- public function onReload($param)
- {
- // read session items
- $sale_items = TSession::getValue('sale_items');
- $this->product_list->clear(); // clear product list
- $data = $this->form->getData();
-
- if ($sale_items)
- {
- $cont = 1;
- foreach ($sale_items as $list_product_id => $list_product)
- {
- $item_name = 'prod_' . $cont++;
- $item = new StdClass;
-
- // create action buttons
- $action_del = new TAction(array($this, 'onDeleteItem'));
- $action_del->setParameter('list_product_id', $list_product_id);
-
- $action_edi = new TAction(array($this, 'onEditItemProduto'));
- $action_edi->setParameter('list_product_id', $list_product_id);
-
- $button_del = new TButton('delete_product'.$cont);
- $button_del->class = 'btn btn-default btn-sm';
- $button_del->setAction( $action_del, '' );
- $button_del->setImage('fa:trash-o red fa-lg');
-
- $button_edi = new TButton('edit_product'.$cont);
- $button_edi->class = 'btn btn-default btn-sm';
- $button_edi->setAction( $action_edi, '' );
- $button_edi->setImage('fa:edit blue fa-lg');
-
- $item->edit = $button_edi;
- $item->delete = $button_del;
-
- $this->formFields[ $item_name.'_edit' ] = $item->edit;
- $this->formFields[ $item_name.'_delete' ] = $item->delete;
-
- $item->id_produto = $list_product['id_produto'];
- $item->nome_prd = $list_product['nome_prd'];
- $item->qtde_prd = $list_product['qtde_prd'];
- $item->vunt_prd = $list_product['vunt_prd'];
- $item->vtot_prd = $list_product['vtot_prd'];
-
- $row = $this->product_list->addItem( $item );
- $row->onmouseover='';
- $row->onmouseout='';
- }
- $this->form->setFields( $this->formFields );
- }
-
- $this->loaded = TRUE;
- }
-
- function onEdit($param)
- {
- try
- {
- TTransaction::open('proterra');
-
- if (isset($param['key']))
- {
- $key = $param['key'];
-
- $object = new Sale($key);
- $sale_items = $object->getSaleItems();
-
- $session_items = array();
- foreach( $sale_items as $item )
- {
- $session_items[$item->product_id] = $item->toArray();
- $session_items[$item->product_id]['id_produto'] = $item->product_id;
- $session_items[$item->product_id]['nome_prd'] = $item->product->nome_prd;
- $session_items[$item->product_id]['qtde_prd'] = $item->qtde_prd;
- $session_items[$item->product_id]['prc_venda'] = $item->vunt_prd;
- $session_items[$item->product_id]['vtot_prd'] = $item->vtot_prd;
- }
- TSession::setValue('sale_items', $session_items);
-
- $this->form->setData($object); // fill the form with the active record data
- $this->onReload( $param ); // reload sale items list
- TTransaction::close(); // close transaction
- }
- else
- {
- $this->form->clear();
- TSession::setValue('sale_items', null);
- $this->onReload( $param );
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- TTransaction::rollback();
- }
- }
-
- function onSave()
- {
- try
- {
- // open a transaction with database 'proterra'
- TTransaction::open('proterra');
-
- $sale = $this->form->getData('Sale');
- $this->form->validate(); // form validation
-
- // get session items
- $sale_items = TSession::getValue('sale_items');
-
- if( $sale_items )
- {
- $total = 0;
- foreach( $sale_items as $sale_item )
- {
- $item = new SaleItem;
- $item->product_id = $sale_item['id_produto'];
- $item->sale_price = $sale_item['prc_venda'];
- $item->amount = $sale_item['qtde_prd'];
- $item->discount = $sale_item['product_discount'];
- $item->total = ($sale_item['prc_venda'] * $sale_item['qtde_prd']) - $sale_item['product_discount'];
-
- $sale->addSaleItem($item);
- $total += ($item->sale_price * $item->amount) - $item->discount;
- }
- }
-
- $sale->date = TDate::date2us($sale->date);
- $sale->total = $total;
- $sale->store(); // stores the object
- $this->form->setData($sale); // keep form data
- TTransaction::close(); // close the transaction
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback();
- }
- }
-
- static function onClienteChange( $params )
- {
- if( isset($params['id_cliente']) && $params['id_cliente'] )
- {
- try
- {
- TTransaction::open('proterra');
-
- $cliente = new Cliente ($params['id_cliente']);
- $fill_data = new StdClass;
- $fill_data->nome_comp = $cliente->apelido;
- $fill_data->mail_comp = $cliente->email;
-
- TForm::sendData('OrcamentoForm', $fill_data);
- TTransaction::close();
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error- Sem Preco de venda</b> ' . $e->getMessage());
- TTransaction::rollback();
- }
- }
- }
-
- static function onProdutoChange( $params )
- {
- if( isset($params['id_produto']) && $params['id_produto'] )
- {
- try
- {
- TTransaction::open('proterra');
-
- $producto = new Produto ($params['id_produto']);
- $fill_data = new StdClass;
- $fill_data->vunt_prd = number_format($producto->prc_venda,2);
- TForm::sendData('OrcamentoForm', $fill_data);
- TTransaction::close();
-
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error- Sem Preco de venda</b> ' . $e->getMessage());
- TTransaction::rollback();
- }
- }
- }
-
- public static function onQtdChange( $params )
- {
- $dto = (double) str_replace(',', '', $params['vdsc_prd']);
- $preco_unitario = (double) str_replace(',', '', $params['vunt_prd']);
- $qte = (double) str_replace(',', '', $params['qtde_prd']);
-
- $obj = new StdClass;
- $obj->vtot_prd = number_format( ($preco_unitario * $qte) - $dto, 2, '.', ',');
- TForm::sendData('OrcamentoForm', $obj);
-
- }
-
- public function show()
- {
- parent::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) );
- }
-
- }
- }
- ?>
Curso Dominando o Adianti Framework
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!
Dominando o Adianti Framework Quero me inscrever agora!
Aqui tem um exemplo de form master/detail.
No Studio PRO tem um gerador deste tipo de tela,
onde basta selecionar as tabelas master/detail.
Att,