FH
Preencher Datagrid com método static
Olá, estou usando o setExitAction para preencher alguns campos automaticamente, como o função precisa ser static não consigo mandar diretamente do meu onExitInsumos, então oque fiz foi enviar do metodo static os dados por TSession, só que quando tento chamar a instancia do meu metodo onReload que iria preencher a datagrid, ele simplesmente não popula, ja dei alguns var_dump e sim, o metodo é executado, os dados estão sendo passados, só que no additem ele não adiciona na datagrid, só que se eu colocar ele em um botão e chamar o onReload funciona...
aqui esta meu código:
aqui esta meu código:
- <?php
- /**
- * SC2010Form Master/Detail
- * @author <your name here>
- */
- class SC2010Form extends TPage
- {
- protected $form; // form
- protected $detail_list;
- protected $sumario;
- /**
- * Page constructor
- */
- public function __construct()
- {
- parent::__construct();
- // creates the form
- $this->form = new BootstrapFormBuilder('form_SC2010');
- $this->form->setFormTitle('SC2010');
- $criteria = new TCriteria;
- $subquery = "(SELECT C2_NUM
- FROM SC2010 SC
- JOIN SB1000 SB ON SC.C2_PRODUTO = SB.B1_COD
- JOIN SBM000 SM ON SB.B1_GRUPO = SM.BM_GRUPO AND SM.BM_TIPGRU = 'P'
- WHERE SC.C2_OPBROK = '' AND SC.D_E_L_E_T_ = '')";
- $criteria->add(new TFilter('C2_NUM', 'IN', $subquery));
- $C2_NUM = new ">TDBSeekButton('C2_NUM', 'banco', 'form_SC2010', 'SC2010', 'C2_PRODUTO', 'C2_NUM', 'PRODUTO', $criteria);
- $C2_QUANT = new TEntry('C2_QUANT');
- $C2_QUANT->setEditable(FALSE);
- $PRODUTO = new TEntry('PRODUTO');
- $PRODUTO->setEditable(FALSE);
- $REMESSA = new TDropDown ('REMESSA');
- $RECEITA = new TEntry ('RECEITA');
- $RECEITA->setEditable(FALSE);
- $INSUMO1 = new TEntry ('INSUMO1');
- $INSUMO1->setEditable(FALSE);
- $INSUMO2 = new TEntry ('INSUMO2');
- $INSUMO2->setEditable(FALSE);
- $INSUMO3 = new TEntry ('INSUMO3');
- $INSUMO3->setEditable(FALSE);
- $INSUMO4 = new TEntry ('INSUMO4');
- $INSUMO4->setEditable(FALSE);
- $INSUMO5 = new TEntry ('INSUMO5');
- $INSUMO5->setEditable(FALSE);
- $PORCEN1 = new TEntry ('PORCEN1');
- $PORCEN1->setEditable(FALSE);
- $PORCEN2 = new TEntry ('PORCEN2');
- $PORCEN2->setEditable(FALSE);
- $PORCEN3 = new TEntry ('PORCEN3');
- $PORCEN3->setEditable(FALSE);
- $PORCEN4 = new TEntry ('PORCEN4');
- $PORCEN4->setEditable(FALSE);
- $PORCEN5 = new TEntry ('PORCEN5');
- $PORCEN5->setEditable(FALSE);
- $PRODUTO->setExitAction(new TAction(array($this, 'onExitProduct')));
- $C2_NUM->setExitAction(new TAction(array($this, 'onExitOP')));
- $RECEITA->setExitAction(new TAction(array($this, 'onExitInsumos')));
- // detail fields
- $detail_uniqid = new THidden('detail_uniqid');
- $detail_R_E_C_N_O_ = new THidden('detail_R_E_C_N_O_');
- $detail_Z32_BARRA = new TEntry('detail_Z32_BARRA');
- $detail_Z32_PESOLI = new TEntry('detail_Z32_PESOLI');
- // master fields
- $row = $this->form->addFields( [new TLabel('Ordem Produção')], [$C2_NUM],[new TLabel('PA')], [$PRODUTO]);
- $row->layout = ['col-sm-2 control-label', 'col-sm-2','col-sm-0.5 control-label', 'col-sm-2'];
- $row2 = $this->form->addFields( [new TLabel('KG')], [$C2_QUANT],[new TLabel('Codigo Receita')], [$RECEITA] );
- $row2->layout = ['col-sm-2 control-label', 'col-sm-2','col-sm-0.1 control-label', 'col-sm-1'];
- $this->form->addFields( [new TLabel('Remessa')], [$REMESSA]);
- // detail fields
- $this->form->addContent( ['<h4>Details</h4><hr>'] );
- $this->form->addFields( [$detail_uniqid] );
- $this->form->addFields( [$detail_R_E_C_N_O_] );
- $this->form->addFields( [new TLabel('Z32 Barra')], [$detail_Z32_BARRA] );
- $this->form->addFields( [new TLabel('Z32 Pesoli')], [$detail_Z32_PESOLI] );
- $add = TButton::create('add', [$this, 'onReload'], 'Register', 'fa:plus-circle green');
- //$add->getAction()->setParameter('static','1');
- $this->form->addFields( [], [$add] );
- $this->detail_list = new BootstrapDatagridWrapper(new TDataGrid);
- $this->detail_list->setId('Z32010_list');
- $this->detail_list->generateHiddenFields();
- $this->detail_list->style = "min-width: 700px; width:100%;margin-bottom: 10px";
- // items
- $this->detail_list->addColumn( new TDataGridColumn('uniqid', 'Uniqid', 'center') )->setVisibility(false);
- $this->detail_list->addColumn( new TDataGridColumn('R_E_C_N_O_', 'Id', 'center') )->setVisibility(false);
- $this->detail_list->addColumn( new TDataGridColumn('Z32_BARRA', 'Z32 Barra', 'left', 100) );
- $this->detail_list->addColumn( new TDataGridColumn('Z32_PESOLI', 'Z32 Pesoli', 'left', 100) );
- // detail actions
- $action1 = new TDataGridAction([$this, 'onDetailEdit'] );
- $action1->setFields( ['uniqid', '*'] );
- $action2 = new TDataGridAction([$this, 'onDetailDelete']);
- $action2->setField('uniqid');
- // add the actions to the datagrid
- $this->detail_list->addAction($action1, _t('Edit'), 'fa:edit blue');
- $this->detail_list->addAction($action2, _t('Delete'), 'far:trash-alt red');
- $this->detail_list->createModel();
- $panel = new TPanelGroup;
- $panel->add($this->detail_list);
- $panel->getBody()->style = 'overflow-x:auto';
- $this->form->addContent( [$panel] );
- //$rowfooter = $this->form->addFields( [new TLabel('Insumos')], [$INSUMO1],[$INSUMO2], [$INSUMO3], [$INSUMO4], [$INSUMO5]);
- //$rowfooter->layout = ['col-sm-2 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1'];
- //$rowfooter2 = $this->form->addFields( [new TLabel('Kilos por Insumo')], [$PORCEN1],[$PORCEN2], [$PORCEN3], [$PORCEN4], [$PORCEN5]);
- //$rowfooter2->layout = ['col-sm-2 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1'];
- $this->form->addAction( 'Save', new TAction([$this, 'onSave'], ['static'=>'1']), 'fa:save green');
- $this->form->addAction( 'Clear', new TAction([$this, 'onClear']), 'fa:eraser red');
- $this->sumario = new BootstrapDatagridWrapper(new TDataGrid);
- $this->sumario->style = 'width: 100%';
- $prodmp = new TDataGridColumn('prodmp', 'PRODUTO', 'center', 100);
- $peso = new TDataGridColumn('peso', 'PESO', 'center', 100);
- $this->sumario->addColumn($prodmp);
- $this->sumario->addColumn($peso);
- $this->sumario->createModel();
- $panel2 = new TPanelGroup;
- $panel2->add($this->sumario);
- $panel2->getBody()->style = 'overflow-x:auto';
- $this->form->addContent( [$panel2] );
- // 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);
- }
- /**
- * Clear form
- * @param $param URL parameters
- */
- public function onClear($param)
- {
- $this->form->clear(TRUE);
- }
- /**
- * Add detail item
- * @param $param URL parameters
- */
- public function onDetailAdd( $param )
- {
- try
- {
- $this->form->validate();
- $data = $this->form->getData();
- $uniqid = !empty($data->detail_uniqid) ? $data->detail_uniqid : uniqid();
- $grid_data = [];
- $grid_data['uniqid'] = $uniqid;
- $grid_data['R_E_C_N_O_'] = $data->detail_R_E_C_N_O_;
- $grid_data['Z32_BARRA'] = $data->detail_Z32_BARRA;
- $grid_data['Z32_PESOLI'] = $data->detail_Z32_PESOLI;
- // insert row dynamically
- $row = $this->detail_list->addItem( (object) $grid_data );
- $row->id = $uniqid;
- TDataGrid::replaceRowById('Z32010_list', $uniqid, $row);
- // clear detail form fields
- $data->detail_uniqid = '';
- $data->detail_R_E_C_N_O_ = '';
- $data->detail_Z32_BARRA = '';
- $data->detail_Z32_PESOLI = '';
- // send data, do not fire change/exit events
- TForm::sendData( 'form_SC2010', $data, false, false );
- }
- catch (Exception $e)
- {
- $this->form->setData( $this->form->getData());
- new TMessage('error', $e->getMessage());
- }
- }
- /**
- * Edit detail item
- * @param $param URL parameters
- */
- public static function onDetailEdit( $param )
- {
- $data = new stdClass;
- $data->detail_uniqid = $param['uniqid'];
- $data->detail_R_E_C_N_O_ = $param['R_E_C_N_O_'];
- $data->detail_Z32_BARRA = $param['Z32_BARRA'];
- $data->detail_Z32_PESOLI = $param['Z32_PESOLI'];
- // send data, do not fire change/exit events
- TForm::sendData( 'form_SC2010', $data, false, false );
- }
- /**
- * Delete detail item
- * @param $param URL parameters
- */
- public static function onDetailDelete( $param )
- {
- // clear detail form fields
- $data = new stdClass;
- $data->detail_uniqid = '';
- $data->detail_R_E_C_N_O_ = '';
- $data->detail_Z32_BARRA = '';
- $data->detail_Z32_PESOLI = '';
- // send data, do not fire change/exit events
- TForm::sendData( 'form_SC2010', $data, false, false );
- // remove row
- TDataGrid::removeRowById('Z32010_list', $param['uniqid']);
- }
- /**
- * Load Master/Detail data from database to form
- */
- public function onEdit($param)
- {
- try
- {
- TTransaction::open('banco');
- if (isset($param['key']))
- {
- $key = $param['key'];
- $object = new SC2010($key);
- $items = Z32010::where('Z32_PRODUT', '=', $key)->load();
- foreach( $items as $item )
- {
- $item->uniqid = uniqid();
- $row = $this->detail_list->addItem( $item );
- $row->id = $item->uniqid;
- }
- $this->form->setData($object);
- TTransaction::close();
- }
- else
- {
- $this->form->clear(TRUE);
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- /**
- * Save the Master/Detail data from form to database
- */
- public function onSave($param)
- {
- try
- {
- // open a transaction with database
- TTransaction::open('banco');
- $data = $this->form->getData();
- $this->form->validate();
- $master = new SC2010;
- $master->fromArray( (array) $data);
- $master->store();
- Z32010::where('Z32_PRODUT', '=', $master->C2_NUM)->delete();
- if( $param['Z32010_list_Z32_BARRA'] )
- {
- foreach( $param['Z32010_list_Z32_BARRA'] as $key => $item_id )
- {
- $detail = new Z32010;
- $detail->Z32_BARRA = $param['Z32010_list_Z32_BARRA'][$key];
- $detail->Z32_PESOLI = $param['Z32010_list_Z32_PESOLI'][$key];
- $detail->Z32_PRODUT = $master->C2_NUM;
- $detail->store();
- }
- }
- TTransaction::close(); // close the transaction
- TForm::sendData('form_SC2010', (object) ['id' => $master->C2_NUM]);
- new TMessage('info', AdiantiCoreTranslator::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();
- }
- }
- public static function onExitOP($param)
- {
- $product_code = $param['C2_NUM'];
- try
- {
- TTransaction::open('banco');
- $product = new SC2010($product_code);
- if ($product->C2_NUM !== null) {
- $obj = new StdClass;
- $obj->REMESSA = $product->C2_REMESSA;
- if ($product->C2_REMESSA != 0) {
- $calculatedQuant = $product->C2_QUANT / $product->C2_REMESSA;
- } else {
- $calculatedQuant = 0;
- }
- $obj->C2_QUANT = $calculatedQuant;
- TTransaction::close();
- TForm::sendData('form_SC2010', $obj);
- } else {
- TTransaction::close();
- }
- }
- catch (Exception $e)
- {
- // Handle exceptions
- }
- }
- public static function onExitProduct($param)
- {
- $product_code = $param['PRODUTO'];
- try
- {
- TTransaction::open('banco');
- $criteria = new TCriteria;
- $criteria->add(new TFilter('Z46_PRODPA', '=', $product_code));
- //$criteria->setProperty('limit', 1);
- $repository = new TRepository('Z46010');
- $productList = $repository->load($criteria);
- if (!empty($productList)) {
- $product = $productList[0];
- $obj = new StdClass;
- $obj->RECEITA = $product->Z46_CODREC;
- TTransaction::close();
- TForm::sendData('form_SC2010', $obj);
- } else {
- TTransaction::close();
- new TMessage('info','Ordem de Produção sem Receita');
- }
- }
- catch (Exception $e)
- {
- // Handle exceptions
- }
- }
- public static function onExitInsumos($param)
- {
- $product_code = $param['RECEITA'];
- $quant = $param ['C2_QUANT'];
- try
- {
- TTransaction::open('banco');
- $criteria = new TCriteria;
- $criteria->add(new TFilter('Z46_CODREC', '=', $product_code));
- $criteria->setProperty('limit', 5);
- $repository = new TRepository('Z46010');
- $productList = $repository->load($criteria);
- TTransaction::close();
- if (!empty($productList)) {
- $obj = new StdClass;
- foreach ($productList as $index => $product) {
- $fieldName = 'INSUMO' . ($index + 1);
- $obj->$fieldName = trim($product->Z46_PRODMP) . '-' . $product->Z46_PERCEN.'%';
- $fieldNamePorcen = 'PORCEN' . ($index + 1);
- if ($quant != 0) {
- $calculatedValue = ($product->Z46_PERCEN / 100) * $quant;
- $obj->$fieldNamePorcen = $calculatedValue .' KG';
- } else {
- $obj->$fieldNamePorcen = 0;
- }
- }
- TSession::setValue('form_SC2010', $obj);
- $instance = new self ();
- $instance->onReload();
- } else {
- new TMessage('info', 'Ordem de Produção sem Receita');
- }
- }
- catch (Exception $e)
- {
- // Handle exceptions
- }
- }
- function onReload()
- {
- $this->sumario->clear();
- $dataFromSession = TSession::getValue('form_SC2010', []);
- if (!empty($dataFromSession)) {
- $insumos = [];
- $porcentagens = [];
- foreach ($dataFromSession as $propertyName => $propertyValue) {
- if (strpos($propertyName, 'INSUMO') === 0) {
- $insumos[$propertyName] = $propertyValue;
- } elseif (strpos($propertyName, 'PORCEN') === 0) {
- $porcentagens[$propertyName] = $propertyValue;
- }
- }
- foreach ($insumos as $insumoName => $insumoValue) {
- $rowData = new stdClass();
- $rowData->prodmp = $insumoValue;
- $rowData->peso = $porcentagens[str_replace('INSUMO', 'PORCEN', $insumoName)];
- $this->sumario->addItem($rowData);
- }
- } else {
- new TMessage('Sumário não disponível');
- }
- }
- }
- ?>
Nem tudo da para fazer de forma estática, mas pode forçar o reload com a função postData
TApplication::postData('seu_form', 'FormNovo', 'onReload');