Incluir nome no grid Pessoal tenho uma entrada de dados mestre/detalhe, no detalhe entro com informações só que queria que mostrasse o nome do destino, quando edito um registro ele mostra, mas quando incluo o movimento ele só mostra o ID, código abaixo: ...
MO
Incluir nome no grid  
Pessoal tenho uma entrada de dados mestre/detalhe, no detalhe entro com informações só que queria que mostrasse o nome do destino, quando edito um registro ele mostra, mas quando incluo o movimento ele só mostra o ID, código abaixo:

 
  1. <?php
  2. /**
  3. * BensForm Master/Detail
  4. * @author <your name here>
  5. */
  6. class BensForm extends TPage
  7. {
  8. protected $form; // form
  9. protected $detail_list;
  10. /**
  11. * Page constructor
  12. */
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. // creates the form
  17. $this->form = new BootstrapFormBuilder('form_Bens');
  18. $this->form->setFormTitle('<h4><b>Bens Patrimoniais</b></h4>');
  19. //$this->form->style = "ont-weight:bold; font-style: ilatic;color: red;";
  20. $this->form->setFieldSizes('100%');
  21. $this->form->generateAria(); // automatic aria-label
  22. // master fields
  23. $id = new TEntry('id');
  24. $num_tombamento = new TEntry('num_tombamento');
  25. $descricao = new TEntry('descricao');
  26. $empresa_id = new TDBUniqueSearch('empresa_id', 'patrimonio', 'Empresa', 'id', 'nome');
  27. $data_receb = new TDate('data_receb');
  28. $tipoaquisicao_id = new TDBCombo('tipoaquisicao_id', 'patrimonio', 'Tipoaquisicao', 'id', 'descaquisicao');
  29. $estadoprod_id = new TDBCombo('estadoprod_id', 'patrimonio', 'Estadoprod', 'id', 'estadoprod');
  30. $numdoc = new TEntry('numdoc');
  31. $num_notafiscal = new TEntry('num_notafiscal');
  32. $valordoben = new TEntry('valordoben');
  33. //$ativo = new TEntry('ativo');
  34. $tipotomb_id = new TDBCombo('tipotomb_id', 'patrimonio', 'Tipotomb', 'id', 'descricao');
  35. $tipoproduto_id = new TDBCombo('tipoproduto_id', 'patrimonio', 'Tipoproduto', 'id', 'descricao');
  36. $empresa_id->setMinLength(1);
  37. // detail fields
  38. $detail_uniqid = new THidden('detail_uniqid');
  39. $detail_id = new THidden('detail_id');
  40. $detail_numtermomov = new TEntry('detail_numtermomov');
  41. $detail_datamov = new TDate('detail_datamov');
  42. $detail_destino_id = new TDBCombo('detail_destino_id', 'patrimonio', 'Destino', 'id', 'nome');
  43. $detail_motivo_id = new TDBCombo('detail_motivo_id', 'patrimonio', 'Motivo', 'id', 'motivo');
  44. $detail_ativo = new TCombo('detail_ativo');
  45. if (!empty($id))
  46. {
  47. $id->setEditable(FALSE);
  48. }
  49. $detail_ativo->setSize('100%');
  50. $detail_ativo->addItems( ['S' => 'S', 'N' => 'N'] );
  51. //definindo valor inicial
  52. $detail_ativo->setValue('S');
  53. //
  54. //$detail_destino_id->setMinLength(1);
  55. //$detail_motivo_id->setMinLength(1);
  56. $row = $this->form->addFields( [ new TLabel('<b>Codigo</b>') , $id ],
  57. [ new TLabel('<b>Num.Tombamento</b>'), $num_tombamento ],
  58. [ new TLabel('<b>Descrição</b>') , $descricao ] );
  59. $row->layout = ['col-sm-2', 'col-sm-2', 'col-sm-8' ];
  60. $row = $this->form->addFields( [ new TLabel('<b>Empresa</b>'), $empresa_id ] );
  61. $row->layout = ['col-sm-12'];
  62. $row = $this->form->addFields( [ new TLabel('<b>Data do Receb.</b>') , $data_receb ],
  63. [ new TLabel('<b>Tipo Aquisição</b>') , $tipoaquisicao_id ],
  64. [ new TLabel('<b>Estado do Produto</b>') , $estadoprod_id ],
  65. [ new TLabel('<b>Numero Doc.</b>') , $numdoc ] );
  66. $row->layout = ['col-sm-3', 'col-sm-3', 'col-sm-3','col-sm-2'];
  67. $row = $this->form->addFields( [ new TLabel('<b>N. Fiscal</b>') , $num_notafiscal ],
  68. [ new TLabel('<b>Valor do Produto</b>'), $valordoben ],
  69. [ new TLabel('<b>Tp. Patrim.</b>') , $tipotomb_id ],
  70. [ new TLabel('<b>Tp. do Produto</b>') , $tipoproduto_id ]);
  71. $row->layout = [ 'col-sm-2', 'col-sm-2','col-sm-3', 'col-sm-2' ];
  72. // detail fields
  73. //$this->form->addContent( ['<br> </br>'] );
  74. $this->form->addContent( ['<h4><b>Movimentação</b></h4>'] );
  75. $this->form->addFields( [$detail_uniqid] );
  76. $this->form->addFields( [$detail_id] );
  77. $add = TButton::create('add', [$this, 'onDetailAdd'], 'Incluir', 'fa:plus-circle green');
  78. $add->getAction()->setParameter('static','1');
  79. $row = $this->form->addFields( [ new TLabel('<b>N.Termo</b>') , $detail_numtermomov ],
  80. [ new TLabel('<b>Data do Mov.</b>'), $detail_datamov ],
  81. [ new TLabel('<b>Destino</b>') , $detail_destino_id ],
  82. [ new TLabel('<b>Motivo</b>') , $detail_motivo_id ],
  83. [ new TLabel('<b>Ativo</b>') , $detail_ativo ],
  84. [ new TLabel('') , $add]);
  85. $row->layout = [ 'col-sm-2', 'col-sm-2','col-sm-3', 'col-sm-3','col-sm-1','col-sm-1','col-sm-1'];
  86. /*
  87. $add = TButton::create('add', [$this, 'onDetailAdd'], 'Incluir Mov.', 'fa:plus-circle green');
  88. $add->getAction()->setParameter('static','1');
  89. $this->form->addFields( [], [$add] );
  90. */
  91. $this->detail_list = new BootstrapDatagridWrapper(new TDataGrid);
  92. $this->detail_list->setId('Movimentacao_list');
  93. $this->detail_list->generateHiddenFields();
  94. $this->detail_list->style = "min-width: 700px; width:100%;margin-bottom: 10px";
  95. // items
  96. $this->detail_list->addColumn( new TDataGridColumn('uniqid', 'Uniqid', 'center') )->setVisibility(false);
  97. $this->detail_list->addColumn( new TDataGridColumn('id', 'Id', 'center') )->setVisibility(false);
  98. $this->detail_list->addColumn( new TDataGridColumn('numtermomov', 'N.Termo', 'left', 10) );
  99. $this->detail_list->addColumn( new TDataGridColumn('datamov', 'Data.Mov', 'left', 50) );
  100. $this->detail_list->addColumn( new TDataGridColumn('destino_id', 'ID', 'left', 10) );
  101. $this->detail_list->addColumn( new TDataGridColumn('destino->nome', 'Destino', 'left', 100) );
  102. $this->detail_list->addColumn( new TDataGridColumn('motivo_id', 'M.ID', 'left', 10) );
  103. $this->detail_list->addColumn( new TDataGridColumn('motivo->motivo', 'Motivo', 'left', 100) );
  104. $this->detail_list->addColumn( new TDataGridColumn('ativo', 'Ativo', 'left', 10) );
  105. // detail actions
  106. $action1 = new TDataGridAction([$this, 'onDetailEdit'] );
  107. $action1->setFields( ['uniqid', '*'] );
  108. $action2 = new TDataGridAction([$this, 'onDetailDelete']);
  109. $action2->setField('uniqid');
  110. // add the actions to the datagrid
  111. $this->detail_list->addAction($action1, _t('Edit'), 'fa:edit blue');
  112. $this->detail_list->addAction($action2, _t('Delete'), 'far:trash-alt red');
  113. $this->detail_list->createModel();
  114. $panel = new TPanelGroup;
  115. $panel->add($this->detail_list);
  116. $panel->getBody()->style = 'overflow-x:auto';
  117. $this->form->addContent( [$panel] );
  118. $this->form->addAction( 'Save', new TAction([$this, 'onSave'], ['static'=>'1']), 'fa:save green');
  119. $this->form->addAction( 'Clear', new TAction([$this, 'onClear']), 'fa:eraser red');
  120. $this->form->addActionLink('Voltar', new TAction(array('BensList','onReload')),'fa:table blue');
  121. // create the page container
  122. $container = new TVBox;
  123. $container->style = 'width: 100%';
  124. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  125. $container->add($this->form);
  126. parent::add($container);
  127. }
  128. /**
  129. * Clear form
  130. * @param $param URL parameters
  131. */
  132. public function onClear($param)
  133. {
  134. $this->form->clear(TRUE);
  135. }
  136. /**
  137. * Add detail item
  138. * @param $param URL parameters
  139. */
  140. public function onDetailAdd( $param )
  141. {
  142. try
  143. {
  144. $this->form->validate();
  145. $data = $this->form->getData();
  146. /** validation sample
  147. if (empty($data->fieldX))
  148. {
  149. throw new Exception('The field fieldX is required');
  150. }
  151. **/
  152. $uniqid = !empty($data->detail_uniqid) ? $data->detail_uniqid : uniqid();
  153. echo '<pre>';
  154. print_r($data);
  155. echo '</pre>';
  156. $grid_data = [];
  157. $grid_data['uniqid'] = $uniqid;
  158. $grid_data['id'] = $data->detail_id;
  159. $grid_data['numtermomov'] = $data->detail_numtermomov;
  160. $grid_data['datamov'] = $data->detail_datamov;
  161. $grid_data['destino_id'] = $data->detail_destino_id; //.' - '.$data->destino->nome;
  162. $grid_data['motivo_id'] = $data->detail_motivo_id;
  163. $grid_data['ativo'] = $data->detail_ativo;
  164. // insert row dynamically
  165. $row = $this->detail_list->addItem( (object) $grid_data );
  166. $row->id = $uniqid;
  167. TDataGrid::replaceRowById('Movimentacao_list', $uniqid, $row);
  168. // clear detail form fields
  169. $data->detail_uniqid = '';
  170. $data->detail_id = '';
  171. $data->detail_numtermomov = '';
  172. $data->detail_datamov = '';
  173. $data->detail_destino_id = '';
  174. $data->detail_motivo_id = '';
  175. $data->detail_ativo = '';
  176. // send data, do not fire change/exit events
  177. TForm::sendData( 'form_Bens', $data, false, false );
  178. }
  179. catch (Exception $e)
  180. {
  181. $this->form->setData( $this->form->getData());
  182. new TMessage('error', $e->getMessage());
  183. }
  184. }
  185. /**
  186. * Edit detail item
  187. * @param $param URL parameters
  188. */
  189. public static function onDetailEdit( $param )
  190. {
  191. //echo'<pre>';
  192. //print_r($param);
  193. //echo'<pre>';
  194. $data = new stdClass;
  195. $data->detail_uniqid = $param['uniqid'];
  196. $data->detail_id = $param['id'];
  197. $data->detail_numtermomov = $param['numtermomov'];
  198. $data->detail_datamov = $param['datamov'];
  199. $data->detail_destino_id = $param['destino_id'];
  200. $data->detail_motivo_id = $param['motivo_id'];
  201. $data->detail_ativo = $param['ativo'];
  202. // send data, do not fire change/exit events
  203. TForm::sendData( 'form_Bens', $data, false, false );
  204. }
  205. /**
  206. * Delete detail item
  207. * @param $param URL parameters
  208. */
  209. public static function onDetailDelete( $param )
  210. {
  211. // clear detail form fields
  212. $data = new stdClass;
  213. $data->detail_uniqid = '';
  214. $data->detail_id = '';
  215. $data->detail_numtermomov = '';
  216. $data->detail_datamov = '';
  217. $data->detail_destino_id = '';
  218. $data->detail_motivo_id = '';
  219. $data->detail_ativo = '';
  220. // send data, do not fire change/exit events
  221. TForm::sendData( 'form_Bens', $data, false, false );
  222. // remove row
  223. TDataGrid::removeRowById('Movimentacao_list', $param['uniqid']);
  224. }
  225. /**
  226. * Load Master/Detail data from database to form
  227. */
  228. public function onEdit($param)
  229. {
  230. try
  231. {
  232. TTransaction::open('patrimonio');
  233. if (isset($param['key']))
  234. {
  235. $key = $param['key'];
  236. $object = new Bens($key);
  237. $items = Movimentacao::where('bens_id', '=', $key)->load();
  238. foreach( $items as $item )
  239. {
  240. $item->uniqid = uniqid();
  241. $row = $this->detail_list->addItem( $item );
  242. $row->id = $item->uniqid;
  243. }
  244. $this->form->setData($object);
  245. TTransaction::close();
  246. }
  247. else
  248. {
  249. $this->form->clear(TRUE);
  250. }
  251. }
  252. catch (Exception $e) // in case of exception
  253. {
  254. new TMessage('error', $e->getMessage());
  255. TTransaction::rollback();
  256. }
  257. }
  258. /**
  259. * Save the Master/Detail data from form to database
  260. */
  261. public function onSave($param)
  262. {
  263. try
  264. {
  265. // open a transaction with database
  266. TTransaction::open('patrimonio');
  267. $data = $this->form->getData();
  268. $this->form->validate();
  269. $master = new Bens;
  270. $master->fromArray( (array) $data);
  271. $master->store();
  272. Movimentacao::where('bens_id', '=', $master->id)->delete();
  273. if( $param['Movimentacao_list_numtermomov'] ?? false )
  274. {
  275. foreach( $param['Movimentacao_list_numtermomov'] as $key => $item_id )
  276. {
  277. $detail = new Movimentacao;
  278. $detail->numtermomov = $param['Movimentacao_list_numtermomov'][$key];
  279. $detail->datamov = $param['Movimentacao_list_datamov'][$key];
  280. $detail->destino_id = $param['Movimentacao_list_destino_id'][$key];
  281. $detail->motivo_id = $param['Movimentacao_list_motivo_id'][$key];
  282. $detail->ativo = $param['Movimentacao_list_ativo'][$key];
  283. $detail->bens_id = $master->id;
  284. $detail->store();
  285. }
  286. }
  287. TTransaction::close(); // close the transaction
  288. TForm::sendData('form_Bens', (object) ['id' => $master->id]);
  289. new TMessage('info', AdiantiCoreTranslator::translate('Record saved'));
  290. }
  291. catch (Exception $e) // in case of exception
  292. {
  293. new TMessage('error', $e->getMessage());
  294. $this->form->setData( $this->form->getData() ); // keep form data
  295. TTransaction::rollback();
  296. }
  297. }
  298. }
  299. stdClass Object
  300. (
  301. [id] => 9
  302. [num_tombamento] => 126621
  303. [descricao] => Birô com 02 gavetas
  304. [empresa_id] => 1
  305. [data_receb] => 2021-08-02
  306. [tipoaquisicao_id] => 2
  307. [estadoprod_id] => 1
  308. [numdoc] => S/N
  309. [num_notafiscal] => S/N
  310. [valordoben] => 0
  311. [tipotomb_id] => 2
  312. [tipoproduto_id] => 107
  313. [detail_uniqid] => 611eef3f542f0
  314. [detail_id] => 6
  315. [detail_numtermomov] => 3
  316. [detail_datamov] => 2021-08-02
  317. [detail_destino_id] => 1
  318. [detail_motivo_id] => 1
  319. [detail_ativo] => S
  320. )

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)


NR

Você pode usar uns transformer como no exemplo do tutor:
https://adianti.com.br/framework_files/tutor/index.php?class=SaleForm