{parts/gtag_head.html} {parts/gtag_body.html}
Mês do Programador

Vamos comemorar juntos o seu mês.
Aproveite condições especiais.

Cursos, combos e assinaturas com desconto exclusivo para comemorar o Dia do Programador. Tempo limitado.

Promoção válida de 14/09 à 25/09

De 30% Até50% OFF
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 ...
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:


 
  1. <?php
  2. /**
  3. * SC2010Form Master/Detail
  4. * @author <your name here>
  5. */
  6. class SC2010Form extends TPage
  7. {
  8. protected $form; // form
  9. protected $detail_list;
  10. protected $sumario;
  11. /**
  12. * Page constructor
  13. */
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. // creates the form
  18. $this->form = new BootstrapFormBuilder('form_SC2010');
  19. $this->form->setFormTitle('SC2010');
  20. $criteria = new TCriteria;
  21. $subquery = "(SELECT C2_NUM
  22. FROM SC2010 SC
  23. JOIN SB1000 SB ON SC.C2_PRODUTO = SB.B1_COD
  24. JOIN SBM000 SM ON SB.B1_GRUPO = SM.BM_GRUPO AND SM.BM_TIPGRU = 'P'
  25. WHERE SC.C2_OPBROK = '' AND SC.D_E_L_E_T_ = '')";
  26. $criteria->add(new TFilter('C2_NUM', 'IN', $subquery));
  27. $C2_NUM = new ">TDBSeekButton('C2_NUM', 'banco', 'form_SC2010', 'SC2010', 'C2_PRODUTO', 'C2_NUM', 'PRODUTO', $criteria);
  28. $C2_QUANT = new TEntry('C2_QUANT');
  29. $C2_QUANT->setEditable(FALSE);
  30. $PRODUTO = new TEntry('PRODUTO');
  31. $PRODUTO->setEditable(FALSE);
  32. $REMESSA = new TDropDown ('REMESSA');
  33. $RECEITA = new TEntry ('RECEITA');
  34. $RECEITA->setEditable(FALSE);
  35. $INSUMO1 = new TEntry ('INSUMO1');
  36. $INSUMO1->setEditable(FALSE);
  37. $INSUMO2 = new TEntry ('INSUMO2');
  38. $INSUMO2->setEditable(FALSE);
  39. $INSUMO3 = new TEntry ('INSUMO3');
  40. $INSUMO3->setEditable(FALSE);
  41. $INSUMO4 = new TEntry ('INSUMO4');
  42. $INSUMO4->setEditable(FALSE);
  43. $INSUMO5 = new TEntry ('INSUMO5');
  44. $INSUMO5->setEditable(FALSE);
  45. $PORCEN1 = new TEntry ('PORCEN1');
  46. $PORCEN1->setEditable(FALSE);
  47. $PORCEN2 = new TEntry ('PORCEN2');
  48. $PORCEN2->setEditable(FALSE);
  49. $PORCEN3 = new TEntry ('PORCEN3');
  50. $PORCEN3->setEditable(FALSE);
  51. $PORCEN4 = new TEntry ('PORCEN4');
  52. $PORCEN4->setEditable(FALSE);
  53. $PORCEN5 = new TEntry ('PORCEN5');
  54. $PORCEN5->setEditable(FALSE);
  55. $PRODUTO->setExitAction(new TAction(array($this, 'onExitProduct')));
  56. $C2_NUM->setExitAction(new TAction(array($this, 'onExitOP')));
  57. $RECEITA->setExitAction(new TAction(array($this, 'onExitInsumos')));
  58. // detail fields
  59. $detail_uniqid = new THidden('detail_uniqid');
  60. $detail_R_E_C_N_O_ = new THidden('detail_R_E_C_N_O_');
  61. $detail_Z32_BARRA = new TEntry('detail_Z32_BARRA');
  62. $detail_Z32_PESOLI = new TEntry('detail_Z32_PESOLI');
  63. // master fields
  64. $row = $this->form->addFields( [new TLabel('Ordem Produção')], [$C2_NUM],[new TLabel('PA')], [$PRODUTO]);
  65. $row->layout = ['col-sm-2 control-label', 'col-sm-2','col-sm-0.5 control-label', 'col-sm-2'];
  66. $row2 = $this->form->addFields( [new TLabel('KG')], [$C2_QUANT],[new TLabel('Codigo Receita')], [$RECEITA] );
  67. $row2->layout = ['col-sm-2 control-label', 'col-sm-2','col-sm-0.1 control-label', 'col-sm-1'];
  68. $this->form->addFields( [new TLabel('Remessa')], [$REMESSA]);
  69. // detail fields
  70. $this->form->addContent( ['<h4>Details</h4><hr>'] );
  71. $this->form->addFields( [$detail_uniqid] );
  72. $this->form->addFields( [$detail_R_E_C_N_O_] );
  73. $this->form->addFields( [new TLabel('Z32 Barra')], [$detail_Z32_BARRA] );
  74. $this->form->addFields( [new TLabel('Z32 Pesoli')], [$detail_Z32_PESOLI] );
  75. $add = TButton::create('add', [$this, 'onReload'], 'Register', 'fa:plus-circle green');
  76. //$add->getAction()->setParameter('static','1');
  77. $this->form->addFields( [], [$add] );
  78. $this->detail_list = new BootstrapDatagridWrapper(new TDataGrid);
  79. $this->detail_list->setId('Z32010_list');
  80. $this->detail_list->generateHiddenFields();
  81. $this->detail_list->style = "min-width: 700px; width:100%;margin-bottom: 10px";
  82. // items
  83. $this->detail_list->addColumn( new TDataGridColumn('uniqid', 'Uniqid', 'center') )->setVisibility(false);
  84. $this->detail_list->addColumn( new TDataGridColumn('R_E_C_N_O_', 'Id', 'center') )->setVisibility(false);
  85. $this->detail_list->addColumn( new TDataGridColumn('Z32_BARRA', 'Z32 Barra', 'left', 100) );
  86. $this->detail_list->addColumn( new TDataGridColumn('Z32_PESOLI', 'Z32 Pesoli', 'left', 100) );
  87. // detail actions
  88. $action1 = new TDataGridAction([$this, 'onDetailEdit'] );
  89. $action1->setFields( ['uniqid', '*'] );
  90. $action2 = new TDataGridAction([$this, 'onDetailDelete']);
  91. $action2->setField('uniqid');
  92. // add the actions to the datagrid
  93. $this->detail_list->addAction($action1, _t('Edit'), 'fa:edit blue');
  94. $this->detail_list->addAction($action2, _t('Delete'), 'far:trash-alt red');
  95. $this->detail_list->createModel();
  96. $panel = new TPanelGroup;
  97. $panel->add($this->detail_list);
  98. $panel->getBody()->style = 'overflow-x:auto';
  99. $this->form->addContent( [$panel] );
  100. //$rowfooter = $this->form->addFields( [new TLabel('Insumos')], [$INSUMO1],[$INSUMO2], [$INSUMO3], [$INSUMO4], [$INSUMO5]);
  101. //$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'];
  102. //$rowfooter2 = $this->form->addFields( [new TLabel('Kilos por Insumo')], [$PORCEN1],[$PORCEN2], [$PORCEN3], [$PORCEN4], [$PORCEN5]);
  103. //$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'];
  104. $this->form->addAction( 'Save', new TAction([$this, 'onSave'], ['static'=>'1']), 'fa:save green');
  105. $this->form->addAction( 'Clear', new TAction([$this, 'onClear']), 'fa:eraser red');
  106. $this->sumario = new BootstrapDatagridWrapper(new TDataGrid);
  107. $this->sumario->style = 'width: 100%';
  108. $prodmp = new TDataGridColumn('prodmp', 'PRODUTO', 'center', 100);
  109. $peso = new TDataGridColumn('peso', 'PESO', 'center', 100);
  110. $this->sumario->addColumn($prodmp);
  111. $this->sumario->addColumn($peso);
  112. $this->sumario->createModel();
  113. $panel2 = new TPanelGroup;
  114. $panel2->add($this->sumario);
  115. $panel2->getBody()->style = 'overflow-x:auto';
  116. $this->form->addContent( [$panel2] );
  117. // create the page container
  118. $container = new TVBox;
  119. $container->style = 'width: 100%';
  120. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  121. $container->add($this->form);
  122. parent::add($container);
  123. }
  124. /**
  125. * Clear form
  126. * @param $param URL parameters
  127. */
  128. public function onClear($param)
  129. {
  130. $this->form->clear(TRUE);
  131. }
  132. /**
  133. * Add detail item
  134. * @param $param URL parameters
  135. */
  136. public function onDetailAdd( $param )
  137. {
  138. try
  139. {
  140. $this->form->validate();
  141. $data = $this->form->getData();
  142. $uniqid = !empty($data->detail_uniqid) ? $data->detail_uniqid : uniqid();
  143. $grid_data = [];
  144. $grid_data['uniqid'] = $uniqid;
  145. $grid_data['R_E_C_N_O_'] = $data->detail_R_E_C_N_O_;
  146. $grid_data['Z32_BARRA'] = $data->detail_Z32_BARRA;
  147. $grid_data['Z32_PESOLI'] = $data->detail_Z32_PESOLI;
  148. // insert row dynamically
  149. $row = $this->detail_list->addItem( (object) $grid_data );
  150. $row->id = $uniqid;
  151. TDataGrid::replaceRowById('Z32010_list', $uniqid, $row);
  152. // clear detail form fields
  153. $data->detail_uniqid = '';
  154. $data->detail_R_E_C_N_O_ = '';
  155. $data->detail_Z32_BARRA = '';
  156. $data->detail_Z32_PESOLI = '';
  157. // send data, do not fire change/exit events
  158. TForm::sendData( 'form_SC2010', $data, false, false );
  159. }
  160. catch (Exception $e)
  161. {
  162. $this->form->setData( $this->form->getData());
  163. new TMessage('error', $e->getMessage());
  164. }
  165. }
  166. /**
  167. * Edit detail item
  168. * @param $param URL parameters
  169. */
  170. public static function onDetailEdit( $param )
  171. {
  172. $data = new stdClass;
  173. $data->detail_uniqid = $param['uniqid'];
  174. $data->detail_R_E_C_N_O_ = $param['R_E_C_N_O_'];
  175. $data->detail_Z32_BARRA = $param['Z32_BARRA'];
  176. $data->detail_Z32_PESOLI = $param['Z32_PESOLI'];
  177. // send data, do not fire change/exit events
  178. TForm::sendData( 'form_SC2010', $data, false, false );
  179. }
  180. /**
  181. * Delete detail item
  182. * @param $param URL parameters
  183. */
  184. public static function onDetailDelete( $param )
  185. {
  186. // clear detail form fields
  187. $data = new stdClass;
  188. $data->detail_uniqid = '';
  189. $data->detail_R_E_C_N_O_ = '';
  190. $data->detail_Z32_BARRA = '';
  191. $data->detail_Z32_PESOLI = '';
  192. // send data, do not fire change/exit events
  193. TForm::sendData( 'form_SC2010', $data, false, false );
  194. // remove row
  195. TDataGrid::removeRowById('Z32010_list', $param['uniqid']);
  196. }
  197. /**
  198. * Load Master/Detail data from database to form
  199. */
  200. public function onEdit($param)
  201. {
  202. try
  203. {
  204. TTransaction::open('banco');
  205. if (isset($param['key']))
  206. {
  207. $key = $param['key'];
  208. $object = new SC2010($key);
  209. $items = Z32010::where('Z32_PRODUT', '=', $key)->load();
  210. foreach( $items as $item )
  211. {
  212. $item->uniqid = uniqid();
  213. $row = $this->detail_list->addItem( $item );
  214. $row->id = $item->uniqid;
  215. }
  216. $this->form->setData($object);
  217. TTransaction::close();
  218. }
  219. else
  220. {
  221. $this->form->clear(TRUE);
  222. }
  223. }
  224. catch (Exception $e) // in case of exception
  225. {
  226. new TMessage('error', $e->getMessage());
  227. TTransaction::rollback();
  228. }
  229. }
  230. /**
  231. * Save the Master/Detail data from form to database
  232. */
  233. public function onSave($param)
  234. {
  235. try
  236. {
  237. // open a transaction with database
  238. TTransaction::open('banco');
  239. $data = $this->form->getData();
  240. $this->form->validate();
  241. $master = new SC2010;
  242. $master->fromArray( (array) $data);
  243. $master->store();
  244. Z32010::where('Z32_PRODUT', '=', $master->C2_NUM)->delete();
  245. if( $param['Z32010_list_Z32_BARRA'] )
  246. {
  247. foreach( $param['Z32010_list_Z32_BARRA'] as $key => $item_id )
  248. {
  249. $detail = new Z32010;
  250. $detail->Z32_BARRA = $param['Z32010_list_Z32_BARRA'][$key];
  251. $detail->Z32_PESOLI = $param['Z32010_list_Z32_PESOLI'][$key];
  252. $detail->Z32_PRODUT = $master->C2_NUM;
  253. $detail->store();
  254. }
  255. }
  256. TTransaction::close(); // close the transaction
  257. TForm::sendData('form_SC2010', (object) ['id' => $master->C2_NUM]);
  258. new TMessage('info', AdiantiCoreTranslator::translate('Record saved'));
  259. }
  260. catch (Exception $e) // in case of exception
  261. {
  262. new TMessage('error', $e->getMessage());
  263. $this->form->setData( $this->form->getData() ); // keep form data
  264. TTransaction::rollback();
  265. }
  266. }
  267. public static function onExitOP($param)
  268. {
  269. $product_code = $param['C2_NUM'];
  270. try
  271. {
  272. TTransaction::open('banco');
  273. $product = new SC2010($product_code);
  274. if ($product->C2_NUM !== null) {
  275. $obj = new StdClass;
  276. $obj->REMESSA = $product->C2_REMESSA;
  277. if ($product->C2_REMESSA != 0) {
  278. $calculatedQuant = $product->C2_QUANT / $product->C2_REMESSA;
  279. } else {
  280. $calculatedQuant = 0;
  281. }
  282. $obj->C2_QUANT = $calculatedQuant;
  283. TTransaction::close();
  284. TForm::sendData('form_SC2010', $obj);
  285. } else {
  286. TTransaction::close();
  287. }
  288. }
  289. catch (Exception $e)
  290. {
  291. // Handle exceptions
  292. }
  293. }
  294. public static function onExitProduct($param)
  295. {
  296. $product_code = $param['PRODUTO'];
  297. try
  298. {
  299. TTransaction::open('banco');
  300. $criteria = new TCriteria;
  301. $criteria->add(new TFilter('Z46_PRODPA', '=', $product_code));
  302. //$criteria->setProperty('limit', 1);
  303. $repository = new TRepository('Z46010');
  304. $productList = $repository->load($criteria);
  305. if (!empty($productList)) {
  306. $product = $productList[0];
  307. $obj = new StdClass;
  308. $obj->RECEITA = $product->Z46_CODREC;
  309. TTransaction::close();
  310. TForm::sendData('form_SC2010', $obj);
  311. } else {
  312. TTransaction::close();
  313. new TMessage('info','Ordem de Produção sem Receita');
  314. }
  315. }
  316. catch (Exception $e)
  317. {
  318. // Handle exceptions
  319. }
  320. }
  321. public static function onExitInsumos($param)
  322. {
  323. $product_code = $param['RECEITA'];
  324. $quant = $param ['C2_QUANT'];
  325. try
  326. {
  327. TTransaction::open('banco');
  328. $criteria = new TCriteria;
  329. $criteria->add(new TFilter('Z46_CODREC', '=', $product_code));
  330. $criteria->setProperty('limit', 5);
  331. $repository = new TRepository('Z46010');
  332. $productList = $repository->load($criteria);
  333. TTransaction::close();
  334. if (!empty($productList)) {
  335. $obj = new StdClass;
  336. foreach ($productList as $index => $product) {
  337. $fieldName = 'INSUMO' . ($index + 1);
  338. $obj->$fieldName = trim($product->Z46_PRODMP) . '-' . $product->Z46_PERCEN.'%';
  339. $fieldNamePorcen = 'PORCEN' . ($index + 1);
  340. if ($quant != 0) {
  341. $calculatedValue = ($product->Z46_PERCEN / 100) * $quant;
  342. $obj->$fieldNamePorcen = $calculatedValue .' KG';
  343. } else {
  344. $obj->$fieldNamePorcen = 0;
  345. }
  346. }
  347. TSession::setValue('form_SC2010', $obj);
  348. $instance = new self ();
  349. $instance->onReload();
  350. } else {
  351. new TMessage('info', 'Ordem de Produção sem Receita');
  352. }
  353. }
  354. catch (Exception $e)
  355. {
  356. // Handle exceptions
  357. }
  358. }
  359. function onReload()
  360. {
  361. $this->sumario->clear();
  362. $dataFromSession = TSession::getValue('form_SC2010', []);
  363. if (!empty($dataFromSession)) {
  364. $insumos = [];
  365. $porcentagens = [];
  366. foreach ($dataFromSession as $propertyName => $propertyValue) {
  367. if (strpos($propertyName, 'INSUMO') === 0) {
  368. $insumos[$propertyName] = $propertyValue;
  369. } elseif (strpos($propertyName, 'PORCEN') === 0) {
  370. $porcentagens[$propertyName] = $propertyValue;
  371. }
  372. }
  373. foreach ($insumos as $insumoName => $insumoValue) {
  374. $rowData = new stdClass();
  375. $rowData->prodmp = $insumoValue;
  376. $rowData->peso = $porcentagens[str_replace('INSUMO', 'PORCEN', $insumoName)];
  377. $this->sumario->addItem($rowData);
  378. }
  379. } else {
  380. new TMessage('Sumário não disponível');
  381. }
  382. }
  383. }
  384. ?>

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!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (1)


FC

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');