GG
Formulário bootstrap fica com espaços entre os campos
- <?php
- /**
- * Pedido_compraForm Master/Detail
- * @author <your name here>
- */
- class Pedido_compraForm extends TPage
- {
- protected $form; // form
- protected $formFields;
- protected $detail_list;
-
- /**
- * Page constructor
- */
- public function __construct()
- {
- parent::__construct();
-
- // creates the form
- $this->form = new TForm('form_Pedido_compra');
- $this->form->class = 'tform'; // CSS class
- $this->form->style = 'max-width:2500px'; // style
- $vbox = new TVBox;
- $vbox->style = 'width: 100%';
- parent::include_css('app/resources/custom-frame.css');
-
- $table_master = new TTable;
- $table_master->width = '100%';
-
- $table_master->addRowSet( new TLabel('Pedido de compra'), '', '')->class = 'tformtitle';
-
- // add a table inside form
- $table_general = new TTable;
- $table_detail = new TTable;
- $table_general-> width = '100%';
- $table_detail-> width = '100%';
-
- $frame_general = new TFrame;
- $frame_general->setLegend('Capa');
- $frame_general->style = 'background:whiteSmoke';
- $frame_general->add($table_general);
- $frame_general-> width = '100%';
-
- $table_master->addRow()->addCell( $frame_general )->colspan=2;
- $row = $table_master->addRow();
- $row->addCell( $table_detail );
-
- $this->form->add($table_master);
-
- // master fields
- $id = new TEntry('id');
- $fornecedor_id = new ">TDBSeekButton('fornecedor_id', 'erpweb',$this->form->getName(), 'Fornecedor','pessoa->nome', 'fornecedor_id', 'fornecedor_nome');
- $condicao_pagamento_id = new TDBCombo('condicao_pagamento_id', 'erpweb', 'Condicao_pagamento', 'id', 'descricao');
- $fornecedor_nome = new TEntry('fornecedor_nome');
- $data_compra = new TDate('data_compra');
- $observacao = new TText('observacao');
- $estoque_interno = new TRadioGroup('estoque_interno');
- $situacao = new TRadioGroup('situacao');
- $valor_total = new TEntry('valor_total');
- $valor_desconto = new TEntry('valor_desconto');
- $valor_total_liquido = new TEntry('valor_total_liquido');
-
- $valor_desconto->setExitAction(new TAction(array($this,'onValorDescontoChange')));
-
- $items = array();
- $items['1'] ='Sim';
- $items['0'] ='Nao';
- $estoque_interno->addItems($items);
-
- $situacao->setLayout('horizontal');
-
- $items = array();
- $items['0'] ='Em aberto';
- $items['1'] ='Em andamento';
- $items['2'] ='Atendido';
- $items['3'] ='Cancelado';
- $situacao->addItems($items);
-
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
-
- $id->setSize('5%');
- $fornecedor_id->setSize('30%');
- $fornecedor_nome->setSize('100%');
- $fornecedor_nome->setEditable(false);
- $data_compra->setValue(date("Y-m-d"));
- $valor_total->setEditable(FALSE);
- $valor_total->setSize('30%');
- $valor_total_liquido->setEditable(FALSE);
- $valor_total_liquido->setSize('30%');
- $situacao->setSize('70%');
-
- $valor_total->setNumericMask(2, ',','.',true);
- $valor_total_liquido->setNumericMask(2, ',','.',true);
- $valor_desconto->setNumericMask(2, ',','.',true);
-
- // detail fields
- $detail_id = new THidden('detail_id');
- $detail_qtde = new TEntry('detail_qtde');
- $detail_qtde_bonificada = new TEntry('detail_qtde_bonificada');
- $detail_valor_item = new TEntry('detail_valor_item');
- $detail_valor_total_item = new TEntry('detail_valor_total_item');
- $detail_produto_id = new ">TDBSeekButton('detail_produto_id', 'erpweb',$this->form->getName(), 'Produto','descricao', 'detail_produto_id', 'detail_produto_nome');
- $detail_produto_nome = new TEntry('detail_produto_nome');
-
- $detail_qtde->setNumericMask(3, ',','.',true);
- $detail_qtde_bonificada->setNumericMask(3, ',','.',true);
- $detail_valor_item->setNumericMask(2, ',','.',true);
- $detail_valor_total_item->setNumericMask(2, ',','.',true);
-
- $detail_produto_id->setSize('30%');
- $detail_valor_total_item->setEditable(FALSE);
- $detail_produto_id->setExitAction(new TAction(array($this,'onProductChange')));
- $detail_qtde_bonificada->setExitAction(new TAction(array($this,'onQtdebonificadaChange')));
- $detail_produto_nome->setEditable(false);
- $detail_produto_nome->setSize('70%');
- /** samples
- $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( 100, 40 ); // set size
- **/
-
- // master
- $table_general->addRowSet( new TLabel('Id'), $id );
- $table_general->addRowSet( new TLabel('Data de Compra'), $data_compra );
- $table_general->addRowSet( $label_fornecedor = new TLabel('Fornecedor (*)'), array( $fornecedor_id, $fornecedor_nome ) );
- $table_general->addRowSet( new TLabel('Condicao de Pagamento (*)'), $condicao_pagamento_id );
- $table_general->addRowSet( new TLabel('Estoque Interno'), $estoque_interno );
- $table_general->addRowSet( new TLabel('Situação'), $situacao );
- $table_general->addRowSet( new TLabel('Valor Total'), $valor_total );
- $table_general->addRowSet( new TLabel('Valor Desconto'), $valor_desconto );
- $table_general->addRowSet( new TLabel('Valor Total Liquido'), $valor_total_liquido );
- $table_general->addRowSet( new TLabel('Observação'), $observacao );
-
- // detail
- $frame_details = new TFrame();
- $frame_details->setLegend('Ítens');
- $row = $table_detail->addRow();
- $row->addCell($frame_details);
- $frame_details-> width = '30%';
-
- $vbox->add( $frame_general );
- $vbox->add( $frame_details );
- $vbox-> width = '30%';
-
- $btn_save_detail = new TButton('btn_save_detail');
- $btn_save_detail->setAction(new TAction(array($this, 'onSaveDetail')), 'Adicionar');
- $btn_save_detail->setImage('fa:save');
-
- $table_details = new TTable;
- $frame_details->add($table_details);
- $table_details-> width = '30%';
-
- $table_details->addRowSet( '', $detail_id );
- $table_details->addRowSet( new TLabel('Produto (*)'), array( $detail_produto_id, $detail_produto_nome ) );
- $table_details->addRowSet( new TLabel('Qtde'), $detail_qtde );
- $table_details->addRowSet( new TLabel('Qtde Bonificada'), $detail_qtde_bonificada );
- $table_details->addRowSet( new TLabel('Valor Item'), $detail_valor_item );
- $table_details->addRowSet( new TLabel('Valor Total Item'), $detail_valor_total_item );
-
- $table_details->addRowSet( $btn_save_detail );
-
- $this->detail_list = new TQuickGrid;
- $this->detail_list->setHeight( 200 );
- $this->detail_list->makeScrollable();
- $this->detail_list->disableDefaultClick();
- $this->detail_list->addQuickColumn('', 'edit', 'left', 60);
- $this->detail_list->addQuickColumn('', 'delete', 'left', 60);
-
- // items
- $this->detail_list->style = "margin-bottom: 10px";
- $this->detail_list->addQuickColumn('Id', 'produto_id', 'center', 50);
- $this->detail_list->addQuickColumn('Produto', 'produto_nome', 'left', 200);
- $qt = $this->detail_list->addQuickColumn('Qtde', 'qtde', 'left', 200);
- $qb = $this->detail_list->addQuickColumn('Qtde Bonificada', 'qtde_bonificada', 'left', 200);
- $pr = $this->detail_list->addQuickColumn('Valor Item', 'valor_item', 'left', 200);
- $st = $this->detail_list->addQuickColumn('Valor Total Item', 'valor_total_item', 'left', 200);
- $this->detail_list->createModel();
-
- $vbox->add( $this->detail_list );
-
- $format_value = function($value) {
- if (is_numeric($value)) {
- return 'R$ '.number_format($value, 2, ',', '.');
- }
- return $value;
- };
-
- $format_qtde = function($value) {
- if (is_numeric($value)) {
- return number_format($value, 3, ',', '.');
- }
- return $value;
- };
-
- $pr->setTransformer( $format_value );
- $st->setTransformer( $format_value );
-
- $qt->setTransformer( $format_qtde );
- $qb->setTransformer( $format_qtde );
-
- $st->setTotalFunction( function($values) {
- return array_sum((array) $values);
- });
-
-
-
- $row = $table_detail->addRow();
- $row->addCell($this->detail_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($id,$data_compra,$valor_total,$valor_desconto,$valor_total_liquido,$observacao,$estoque_interno,$situacao,$fornecedor_id,$fornecedor_nome,$condicao_pagamento_id,$detail_qtde,$detail_qtde_bonificada,$detail_valor_item,$detail_valor_total_item,$detail_produto_id,$detail_produto_nome);
- $this->formFields[] = $btn_save_detail;
- $this->formFields[] = $save_button;
- $this->formFields[] = $new_button;
- $this->formFields[] = $detail_id;
- $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->style = 'width: 100%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
-
-
- public function onLoad($param)
- {
- $data = new stdClass;
- $data->fornecedor_id = $param['fornecedor_id'];
- $data->fornecedor_nome = $param['fornecedor_nome'];
- $this->form->setData($data);
- }
-
- static function onValorDescontoChange( $params )
- {
- if( isset($params['valor_total']) && $params['valor_total'] )
- {
- try
- {
- TTransaction::open('erpweb');
-
- $fill_data = new StdClass;
- $fill_data->valor_total_liquido = number_format((double) str_replace(',', '.',$params['valor_total']) - (double) str_replace(',', '.',$params['valor_desconto']), 2, ',', '.' );
- TForm::sendData('form_Pedido_compra', $fill_data);
-
- TTransaction::close();
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- TTransaction::rollback();
- }
- }
- else
- {
- $fill_data = new StdClass;
- TForm::sendData('form_Pedido_compra', $fill_data);
- TTransaction::close();
-
- }
- }
-
- static function onProductChange( $params )
- {
- if( isset($params['detail_produto_id']) && $params['detail_produto_id'] )
- {
- try
- {
- TTransaction::open('erpweb');
-
- $produto = new Produto($params['detail_produto_id']);
- $fill_data = new StdClass;
- $fill_data->detail_valor_item = number_format($produto->valor_unitario, 2, ',','.');
- $fill_data->detail_qtde_bonificada = '';
- $fill_data->detail_qtde = '';
- $fill_data->detail_valor_total_item = '';
- TForm::sendData('form_Pedido_compra', $fill_data);
- TTransaction::close();
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- TTransaction::rollback();
- }
- }
- else
- {
- $fill_data = new StdClass;
- $fill_data->detail_valor_item = null;
- $fill_data->detail_produto_nome = null;
- TForm::sendData('form_Pedido_compra', $fill_data);
- TTransaction::close();
-
- }
- }
-
- static function onQtdebonificadaChange( $params )
- {
- if( isset($params['detail_produto_id']) && $params['detail_produto_id'] )
- {
- try
- {
- TTransaction::open('erpweb');
-
- $produto = new Produto($params['detail_produto_id']);
- $fill_data = new StdClass;
- $fill_data->detail_valor_total_item = number_format((((double) str_replace(',', '.',$params['detail_qtde']) - (double) str_replace(',', '.',$params['detail_qtde_bonificada']) ) * (double) str_replace(',', '.',$params['detail_valor_item'])), 2, ',', '.');
- TForm::sendData('form_Pedido_compra', $fill_data);
- TTransaction::close();
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- TTransaction::rollback();
- }
- }
- else
- {
- $fill_data = new StdClass;
- $fill_data->detail_valor_item = null;
- $fill_data->detail_produto_nome = null;
- TForm::sendData('form_Pedido_compra', $fill_data);
- TTransaction::close();
-
- }
- }
-
- /**
- * Clear form
- * @param $param URL parameters
- */
- public function onClear($param)
- {
- $this->form->clear(TRUE);
- TSession::setValue(__CLASS__.'_items', array());
- $this->onReload( $param );
- }
-
- /**
- * Save an item from form to session list
- * @param $param URL parameters
- */
- public function onSaveDetail( $param )
- {
- try
- {
- TTransaction::open('erpweb');
- $data = $this->form->getData();
- /** validation sample
- if (! $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 ]['qtde'] = $data->detail_qtde;
- $items[ $key ]['qtde_bonificada'] = $data->detail_qtde_bonificada;
- $items[ $key ]['valor_item'] = $data->detail_valor_item;
- $items[ $key ]['valor_total_item'] = $data->detail_valor_total_item;
- $items[ $key ]['produto_id'] = $data->detail_produto_id;
- $items[ $key ]['produto_nome'] = $data->detail_produto_nome;
-
-
- TSession::setValue(__CLASS__.'_items', $items);
-
- // clear detail form fields
- $data->detail_id = '';
- $data->detail_qtde = '';
- $data->detail_qtde_bonificada = '';
- $data->detail_valor_item = '';
- $data->detail_valor_total_item = '';
- $data->detail_produto_id = '';
- $data->detail_produto_nome = '';
- $this->form->setData($data);
-
- TTransaction::close();
- $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 function onEditDetail( $param )
- {
- $data = $this->form->getData();
-
- // read session items
- $items = TSession::getValue(__CLASS__.'_items');
-
- // get the session item
- $item = $items[ $param['item_key'] ];
-
- $data->detail_id = $item['id'];
- $data->detail_qtde = $item['qtde'];
- $data->detail_qtde_bonificada = $item['qtde_bonificada'];
- $data->detail_valor_item = $item['valor_item'];
- $data->detail_valor_total_item = $item['valor_total_item'];
- $data->detail_produto_id = $item['produto_id'];
- $data->detail_produto_nome = $item['produto_nome'];
-
- // fill detail fields
- $this->form->setData( $data );
-
- $this->onReload( $param );
- }
-
- /**
- * Delete an item from session list
- * @param $param URL parameters
- */
- public function onDeleteDetail( $param )
- {
- $data = $this->form->getData();
-
- // reset items
- $data->detail_qtde = '';
- $data->detail_qtde_bonificada = '';
- $data->detail_valor_item = '';
- $data->detail_valor_total_item = '';
- $data->detail_produto_id = '';
- $data->detail_produto_nome = '';
- $data->valor_total = '';
-
- // clear form data
- $this->form->setData( $data );
-
- // read session items
- $items = TSession::getValue(__CLASS__.'_items');
-
- // delete the item from session
- unset($items[ $param['item_key'] ] );
- TSession::setValue(__CLASS__.'_items', $items);
-
- // reload items
- $this->onReload( $param );
- }
-
- /**
- * 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
- $data = $this->form->getData();
- $total = 0.00;
-
- if ($items)
- {
- $cont = 1;
- foreach ($items as $list_item_key => $list_item)
- {
- $item_name = 'prod_' . $cont++;
- $item = new StdClass;
- // create action buttons
- $action_del = new TAction(array($this, 'onDeleteDetail'));
- $action_del->setParameter('item_key', $list_item_key);
-
- $action_edi = new TAction(array($this, 'onEditDetail'));
- $action_edi->setParameter('item_key', $list_item_key);
-
- $button_del = new TButton('delete_detail'.$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_detail'.$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;
-
- // items
- $item->id = $list_item['id'];
- $item->qtde = $list_item['qtde'];
- $item->qtde_bonificada = $list_item['qtde_bonificada'];
- $item->valor_item = $list_item['valor_item'];
- $item->valor_total_item = $list_item['valor_total_item'];
- $item->produto_id = $list_item['produto_id'];
- $item->produto_nome = $list_item['produto_nome'];
- $total += $item->valor_total_item;
- $row = $this->detail_list->addItem( $item );
- $row->onmouseover='';
- $row->onmouseout='';
- }
- $data = $this->form->getData();
- $valor_desconto = number_format(0.00, 2, ',','.');
- if ($data->valor_desconto) {
- $valor_desconto = number_format($data->valor_desconto, 2, ',','.');
- }
- $data = new Stdclass();
- $data->valor_total = number_format($total, 2, ',','.');
- $data->valor_desconto = $valor_desconto;
- TForm::sendData('form_Pedido_compra', $data);
-
- $this->form->setFields( $this->formFields );
- }
- else {
- $data = new Stdclass();
- $data->valor_total = number_format(0.00, 2, ',','.');
- $data->valor_desconto = number_format(0.00, 2, ',','.');
- TForm::sendData('form_Pedido_compra', $data);
- }
-
-
- $this->loaded = TRUE;
- }
-
- /**
- * Load Master/Detail data from database to form/session
- */
- public function onEdit($param)
- {
- try
- {
- TTransaction::open('erpweb');
-
- if (isset($param['key']))
- {
- $key = $param['key'];
-
- $object = new Pedido_compra($key);
- $items = Item_pedido_compra::where('pedido_compra_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]['qtde'] = $item->qtde;
- $session_items[$item_key]['qtde_bonificada'] = $item->qtde_bonificada;
- $session_items[$item_key]['valor_item'] = $item->valor_item;
- $session_items[$item_key]['valor_total_item'] = $item->valor_total_item;
- $session_items[$item_key]['produto_id'] = $item->produto_id;
- $session_items[$item_key]['produto_nome'] = $item->produto->descricao;
- }
- 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('erpweb');
-
- $data = $this->form->getData();
- $master = new Pedido_compra;
- $master->fromArray( (array) $data);
- $this->form->validate(); // form validation
-
- $master->store(); // save master object
- // delete details
- $old_items = Item_pedido_compra::where('pedido_compra_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 Item_pedido_compra;
- }
- else
- {
- $detail = Item_pedido_compra::find($item['id']);
- }
- $detail->qtde = $item['qtde'];
- $detail->qtde_bonificada = $item['qtde_bonificada'];
- $detail->valor_item = $item['valor_item'];
- $detail->valor_total_item = $item['valor_total_item'];
- $detail->produto_id = $item['produto_id'];
- $detail->pedido_compra_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();
- }
- }
Ao adicionar um array como parâmetro da addRowSet, internamente os componentes do array são adicionados a uma div, e o percentual passado como tamanho corresponde ao tamanho relativo a essa div. O jeito mais simples seria passar um tamanho fixo para o campo:
Outra maneira seria atribuir % ao campo e navega pelos containers até o desejado e modificar o seu tamanho:
Obrigado Nataniel já melhorei bastante o alinhamento com a sua idéia