Problema de reset de TEntry quando fazo um submit no detalhe. 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. ...
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.
 
  1. <?php
  2. /**
  3. * Formulario pra orcamentos
  4. * @author Jose Maldonado
  5. */
  6. class OrcamentoForm extends TPage
  7. {
  8. protected $form; // form
  9. protected $formFields;
  10. protected $dt_venda;
  11. function __construct()
  12. {
  13. parent::__construct();
  14. //parent::include_js('app/lib/include/application.js');
  15. // creates the form
  16. $this->form = new TForm('OrcamentoForm');
  17. $this->form->class = 'tform'; // CSS class
  18. parent::include_css('app/resources/custom-frame.css');
  19. // Cria nova tabela
  20. $table_master = new TTable;
  21. // Tamanho da tabela
  22. $table_master->width = '100%';
  23. // Adiciona linha na tabela
  24. $table_master->addRowSet( new TLabel('Orçamentos'), '', '')->class = 'tformtitle';
  25. // Cria nova tabela
  26. $table_general = new TTable;
  27. // Tamanho da tabela
  28. $table_general-> width = '100%';
  29. // Cria nova tabela
  30. $tableProduct = new TTable;
  31. // Tamanho da tabela
  32. $tableProduct-> width = '100%';
  33. // Cria nova frame
  34. $frame_general = new TFrame;
  35. // Título da frame
  36. $frame_general->setLegend('Destinatário');
  37. // Cor de fundo da frame
  38. //$frame_general->style = 'background:whiteSmoke';
  39. $frame_general->add($table_general);
  40. // Adiciona a frame tabela e define com tamanho de duas celulas
  41. $table_master->addRow()->addCell( $frame_general )->colspan=2;
  42. $row = $table_master->addRow();
  43. $row->addCell( $tableProduct );
  44. $this->form->add($table_master);
  45. // Titulo campos
  46. $lab_codi_orc = new TLabel('Orçamento:');
  47. $lab_data_orc = new TLabel('Emissão:');
  48. $lab_id_vendedor = new TLabel('Vendedor:');
  49. $lab_codi_cli = new TLabel('Cliente:');
  50. $lab_obsv_cli = new TLabel('Observação:');
  51. $lab_nome_comp = new TLabel('Comprador:');
  52. $lab_mail_comp = new TLabel('E-mail:');
  53. $lab_nped_cli = new TLabel('Pedido Cliente:');
  54. $lab_codi_vct = new TLabel('Vencimento:');
  55. $lab_tipo_frt = new TLabel('Frete:');
  56. // Mestre
  57. 2155 = new TEntry('id');
  58. $data = new TDate('data');
  59. 2155_vendedor = new TEntry('id_vendedor');
  60. $vendedor = new TEntry('vendedor');
  61. 2155_cliente = new ">TDBSeekButton('id_cliente', 'proterra', $this->form->getName(), 'Cliente', 'nome', 'id_cliente', 'nome_clie');
  62. $nome_clie = new TEntry('nome_clie');
  63. $obsv = new TEntry('obsv_orc');
  64. $nome_comp = new TEntry('nome_comp');
  65. $mail_comp = new TEntry('mail_comp');
  66. $nped_cli = new TEntry('nped_cli');
  67. $codi_vct = new ">TDBSeekButton('codi_vct', 'proterra', $this->form->getName(), 'Vencimento', 'nome', 'codi_vct', 'nome_vct');
  68. $nome_vct = new TEntry('nome_vct');
  69. $tipo_frt = new TCombo('tipo_frete');
  70. //Carga tipo de frete
  71. $frete_items = array();
  72. $frete_items['CIB'] ='CIB';
  73. $frete_items['FOB'] ='FOB';
  74. $tipo_frt->addItems($frete_items);
  75. //Cria os id
  76. 2155->setId('id');
  77. $data->setId('date');
  78. 2155_vendedor->setId('id_vendedor');
  79. $vendedor->setId('vendedor');
  80. 2155_cliente->setId('id_cliente');
  81. $nome_clie->setId('nome_clie');
  82. $obsv->setId('obsv');
  83. $nome_comp->setId('nome_comp');
  84. $mail_comp->setId('mail_comp');
  85. $nped_cli->setId('nped_cli');
  86. $codi_vct->setId('codi_vct');
  87. $nome_vct->setId('nome_vct');
  88. $tipo_frt->setId('tipo_frt');
  89. //Cria os eventos
  90. 2155_cliente->setExitAction(new TAction(array($this,'onClienteChange')));
  91. TScript::create('$("#id").blur(function(){$("#id_cliente").focus();});');
  92. TScript::create('$("#id_cliente").blur(function(){$("#obsv").focus();});');
  93. TScript::create('$("#nome_clie").prop("tabIndex", -1);');
  94. //Seta os valores de inicio
  95. $data->setValue(date("d/m/Y"));
  96. 2155_vendedor->setValue(TSession::getValue('login_id', $user->id));
  97. $vendedor->setValue(TSession::getValue('username', $user->name));
  98. // Detalhe
  99. 2155_produto = new ">TDBSeekButton('id_produto', 'proterra', $this->form->getName(), 'Produto', 'nome', 'id_produto', 'nome_prd');
  100. $nome_prd = new TEntry('nome_prd');
  101. $vunt_prd = new TEntry('vunt_prd');
  102. $qtde_prd = new TEntry('qtde_prd');
  103. $vdsc_prd = new TEntry('vdsc_prd');
  104. $vtot_prd = new TEntry('vtot_prd');
  105. $data_prd = new TEntry('data_prd');
  106. $total = new TEntry('total');
  107. 2155_produto->setExitAction(new TAction(array($this,'onProdutoChange')));
  108. $qtde_prd->setExitAction(new TAction(array($this,'onQtdChange')));
  109. $vdsc_prd->setExitAction(new TAction(array($this,'onQtdChange')));
  110. //Editable
  111. 2155->setEditable(false);
  112. $data->setEditable(false);
  113. 2155_vendedor->setEditable(false);
  114. $vendedor->setEditable(false);
  115. $nome_prd->setEditable(false);
  116. $nome_clie->setEditable(false);
  117. $nome_vct->setEditable(false);
  118. $vunt_prd->setEditable(false);
  119. $vtot_prd->setEditable(false);
  120. //Case
  121. $obsv->setUpperCase();
  122. $nome_comp->setUpperCase();
  123. $mail_comp->setUpperCase();
  124. $nped_cli->setUpperCase();
  125. // Formatacao
  126. 2155->setMask('999999');
  127. 2155_produto->setMask('999999');
  128. 2155_cliente->setMask('999999');
  129. $vunt_prd->setNumericMask(2, '.', ',', TRUE);
  130. $vdsc_prd->setNumericMask(2, '.', ',', TRUE);
  131. $vtot_prd->setNumericMask(2, '.', ',', TRUE);
  132. $total->setNumericMask(2, '.', ',', TRUE);
  133. //Comprimento maximo
  134. $qtde_prd->setMaxLength(6);
  135. $vunt_prd->setMaxLength(10);
  136. $vdsc_prd->setMaxLength(10);
  137. // Validacao
  138. $data->addValidation('Date', new TRequiredValidator);
  139. 2155_cliente->addValidation('Clientes', new TRequiredValidator);
  140. //adiciona jQuery para máscara e focus
  141. 2155_cliente->id = 'id_cliente';
  142. $script = new TElement('script');
  143. $script->type = 'text/javascript';
  144. $script->add('$("#id_cliente").focus();');
  145. $table_general->addRow()->addCell($script);
  146. // pedido
  147. $table_general->addRowSet( $lab_codi_orc, 2155 );
  148. $table_general->addRowSet( $lab_data_orc, $data );
  149. $table_general->addRowSet( $lab_id_vendedor, array( 2155_vendedor, $vendedor ) );
  150. $table_general->addRowSet( $lab_codi_cli, array( 2155_cliente, $nome_clie ) );
  151. $table_general->addRowSet( $lab_nome_comp, $nome_comp );
  152. $table_general->addRowSet( $lab_mail_comp, $mail_comp );
  153. $table_general->addRowSet( $lab_obsv_cli, $obsv );
  154. $table_general->addRowSet( $lab_codi_vct, array( $codi_vct, $nome_vct ) );
  155. $table_general->addRowSet( $lab_nped_cli, $nped_cli );
  156. $table_general->addRowSet( $lab_tipo_frt, $tipo_frt);
  157. //$label_date->setFontColor('#FF0000');
  158. // Produtos
  159. $frame_product = new TFrame();
  160. $frame_product->setLegend('Produtos');
  161. $row = $tableProduct->addRow();
  162. $row->addCell($frame_product);
  163. $add_product = new TButton('add_product');
  164. $action_product = new TAction(array($this, 'onProductAdd'));
  165. $add_product->setAction($action_product, 'Gravar Ítem');
  166. $add_product->setImage('fa:save');
  167. $subtable_product = new TTable;
  168. $frame_product->add($subtable_product);
  169. $subtable_product->addRowSet( $lab_id_produto = new TLabel('Produto:'), array(2155_produto,$nome_prd) );
  170. $subtable_product->addRowSet( $lab_vunt_prd = new TLabel('Preço de Venda:'), $vunt_prd );
  171. $subtable_product->addRowSet( $lab_qtde_prd = new TLabel('Quantidade:'), $qtde_prd );
  172. $subtable_product->addRowSet( $lab_vdsc_prd = new TLabel('Desconto:'), $vdsc_prd );
  173. $subtable_product->addRowSet( $lab_vtot_prd = new TLabel('Valor Total:'), $vtot_prd );
  174. $subtable_product->addRowSet( $lab_data_prd = new TLabel('Prazo Entrega:'), $data_prd );
  175. $subtable_product->addRowSet( $lab_vtot_orc = new TLabel('Total Geral:'), $vtot_prd );
  176. $subtable_product->addRowSet( $add_product );
  177. $this->product_list = new TQuickGrid;
  178. $this->product_list->makeScrollable();
  179. $this->product_list->disableDefaultClick();
  180. $this->product_list->addQuickColumn('', 'edit', 'left', 30);
  181. $this->product_list->addQuickColumn('', 'delete', 'left', 30);
  182. $this->product_list->addQuickColumn('Produto', 'id_produto', 'right', 40);
  183. $this->product_list->addQuickColumn('Descrição', 'nome_prd', 'left', 300);
  184. $this->product_list->addQuickColumn('Quantidade', 'qtde_prd', 'right', 60);
  185. $this->product_list->addQuickColumn('Vlr. Unitário', 'vunt_prd', 'right', 80);
  186. $this->product_list->addQuickColumn('Vlr. Total', 'vtot_prd', 'right', 80);
  187. $this->product_list->createModel();
  188. $row = $tableProduct->addRow();
  189. $row->addCell($this->product_list);
  190. // create an action button (save)
  191. $save_button=new TButton('save');
  192. $save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
  193. $save_button->setImage('ico_save.png');
  194. // create an new button (edit with no parameters)
  195. $new_button=new TButton('new');
  196. $new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
  197. $new_button->setImage('ico_new.png');
  198. // define form fields
  199. $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);
  200. $this->form->setFields( $this->formFields );
  201. $table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
  202. // create the page container
  203. $container = new TVBox;
  204. $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  205. $container->add($this->form);
  206. parent::add($container);
  207. }
  208. function onClear($param)
  209. {
  210. $this->form->clear();
  211. TSession::setValue('sale_items', array());
  212. $this->onReload( $param );
  213. }
  214. public function onProductAdd( $param )
  215. {
  216. try
  217. {
  218. TTransaction::open('proterra');
  219. $data = $this->form->getData();
  220. if( (! $data->id_produto) || (! $data->qtde_prd) || (! $data->vunt_prd) )
  221. throw new Exception('Os campos produto, quantidade e preço de venda são obrigatórios !');
  222. $product = new Produto($data->id_produto);
  223. $sale_items = TSession::getValue('sale_items');
  224. $key = (int) $data->id_produto;
  225. $sale_items[ $key ] = array('id_produto' => $data->id_produto,
  226. 'nome_prd' => $data->nome_prd,
  227. 'qtde_prd' => $data->qtde_prd,
  228. 'vunt_prd' => $data->vunt_prd,
  229. 'vtot_prd' => $data->vtot_prd
  230. );
  231. TSession::setValue('sale_items', $sale_items);
  232. // clear product form fields after add
  233. $data->id_produto = '';
  234. $data->nome_prd = '';
  235. $data->qtde_prd = '';
  236. $data->vunt_prd = '';
  237. $data->vtot_prd = '';
  238. TTransaction::close();
  239. $this->form->setData($data);
  240. $this->onReload( $param ); // reload the sale items
  241. }
  242. catch (Exception $e)
  243. {
  244. $this->form->setData( $this->form->getData());
  245. new TMessage('error', $e->getMessage());
  246. }
  247. }
  248. public function onEditItemProduto( $param )
  249. {
  250. $data = $this->form->getData();
  251. // read session items
  252. $sale_items = TSession::getValue('sale_items');
  253. // get the session item
  254. $sale_item = $sale_items[ (int) $param['list_product_id'] ];
  255. $data->product_id = $param['list_product_id'];
  256. $data->nome_prd = $sale_item['nome_prd'];
  257. $data->qtde_prd = $sale_item['qtde_prd'];
  258. $data->vunt_prd = $sale_item['vunt_prd'];
  259. $data->vtot_prd = $sale_item['vtot_prd'];
  260. // fill product fields
  261. $this->form->setData( $data );
  262. $this->onReload( $param );
  263. }
  264. public function onDeleteItem( $param )
  265. {
  266. $data = $this->form->getData();
  267. $data->id_produto = '';
  268. $data->nome_prd = '';
  269. $data->qtde_prd = '';
  270. $data->vunt_prd = '';
  271. $data->vtot_prd = '';
  272. // clear form data
  273. $this->form->setData( $data );
  274. // read session items
  275. $sale_items = TSession::getValue('sale_items');
  276. // delete the item from session
  277. unset($sale_items[ (int) $param['list_product_id'] ] );
  278. TSession::setValue('sale_items', $sale_items);
  279. // reload sale items
  280. $this->onReload( $param );
  281. }
  282. public function onReload($param)
  283. {
  284. // read session items
  285. $sale_items = TSession::getValue('sale_items');
  286. $this->product_list->clear(); // clear product list
  287. $data = $this->form->getData();
  288. if ($sale_items)
  289. {
  290. $cont = 1;
  291. foreach ($sale_items as $list_product_id => $list_product)
  292. {
  293. $item_name = 'prod_' . $cont++;
  294. $item = new StdClass;
  295. // create action buttons
  296. $action_del = new TAction(array($this, 'onDeleteItem'));
  297. $action_del->setParameter('list_product_id', $list_product_id);
  298. $action_edi = new TAction(array($this, 'onEditItemProduto'));
  299. $action_edi->setParameter('list_product_id', $list_product_id);
  300. $button_del = new TButton('delete_product'.$cont);
  301. $button_del->class = 'btn btn-default btn-sm';
  302. $button_del->setAction( $action_del, '' );
  303. $button_del->setImage('fa:trash-o red fa-lg');
  304. $button_edi = new TButton('edit_product'.$cont);
  305. $button_edi->class = 'btn btn-default btn-sm';
  306. $button_edi->setAction( $action_edi, '' );
  307. $button_edi->setImage('fa:edit blue fa-lg');
  308. $item->edit = $button_edi;
  309. $item->delete = $button_del;
  310. $this->formFields[ $item_name.'_edit' ] = $item->edit;
  311. $this->formFields[ $item_name.'_delete' ] = $item->delete;
  312. $item->id_produto = $list_product['id_produto'];
  313. $item->nome_prd = $list_product['nome_prd'];
  314. $item->qtde_prd = $list_product['qtde_prd'];
  315. $item->vunt_prd = $list_product['vunt_prd'];
  316. $item->vtot_prd = $list_product['vtot_prd'];
  317. $row = $this->product_list->addItem( $item );
  318. $row->onmouseover='';
  319. $row->onmouseout='';
  320. }
  321. $this->form->setFields( $this->formFields );
  322. }
  323. $this->loaded = TRUE;
  324. }
  325. function onEdit($param)
  326. {
  327. try
  328. {
  329. TTransaction::open('proterra');
  330. if (isset($param['key']))
  331. {
  332. $key = $param['key'];
  333. $object = new Sale($key);
  334. $sale_items = $object->getSaleItems();
  335. $session_items = array();
  336. foreach( $sale_items as $item )
  337. {
  338. $session_items[$item->product_id] = $item->toArray();
  339. $session_items[$item->product_id]['id_produto'] = $item->product_id;
  340. $session_items[$item->product_id]['nome_prd'] = $item->product->nome_prd;
  341. $session_items[$item->product_id]['qtde_prd'] = $item->qtde_prd;
  342. $session_items[$item->product_id]['prc_venda'] = $item->vunt_prd;
  343. $session_items[$item->product_id]['vtot_prd'] = $item->vtot_prd;
  344. }
  345. TSession::setValue('sale_items', $session_items);
  346. $this->form->setData($object); // fill the form with the active record data
  347. $this->onReload( $param ); // reload sale items list
  348. TTransaction::close(); // close transaction
  349. }
  350. else
  351. {
  352. $this->form->clear();
  353. TSession::setValue('sale_items', null);
  354. $this->onReload( $param );
  355. }
  356. }
  357. catch (Exception $e) // in case of exception
  358. {
  359. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  360. TTransaction::rollback();
  361. }
  362. }
  363. function onSave()
  364. {
  365. try
  366. {
  367. // open a transaction with database 'proterra'
  368. TTransaction::open('proterra');
  369. $sale = $this->form->getData('Sale');
  370. $this->form->validate(); // form validation
  371. // get session items
  372. $sale_items = TSession::getValue('sale_items');
  373. if( $sale_items )
  374. {
  375. $total = 0;
  376. foreach( $sale_items as $sale_item )
  377. {
  378. $item = new SaleItem;
  379. $item->product_id = $sale_item['id_produto'];
  380. $item->sale_price = $sale_item['prc_venda'];
  381. $item->amount = $sale_item['qtde_prd'];
  382. $item->discount = $sale_item['product_discount'];
  383. $item->total = ($sale_item['prc_venda'] * $sale_item['qtde_prd']) - $sale_item['product_discount'];
  384. $sale->addSaleItem($item);
  385. $total += ($item->sale_price * $item->amount) - $item->discount;
  386. }
  387. }
  388. $sale->date = TDate::date2us($sale->date);
  389. $sale->total = $total;
  390. $sale->store(); // stores the object
  391. $this->form->setData($sale); // keep form data
  392. TTransaction::close(); // close the transaction
  393. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  394. }
  395. catch (Exception $e) // in case of exception
  396. {
  397. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  398. $this->form->setData( $this->form->getData() ); // keep form data
  399. TTransaction::rollback();
  400. }
  401. }
  402. static function onClienteChange( $params )
  403. {
  404. if( isset($params['id_cliente']) && $params['id_cliente'] )
  405. {
  406. try
  407. {
  408. TTransaction::open('proterra');
  409. $cliente = new Cliente ($params['id_cliente']);
  410. $fill_data = new StdClass;
  411. $fill_data->nome_comp = $cliente->apelido;
  412. $fill_data->mail_comp = $cliente->email;
  413. TForm::sendData('OrcamentoForm', $fill_data);
  414. TTransaction::close();
  415. }
  416. catch (Exception $e) // in case of exception
  417. {
  418. new TMessage('error', '<b>Error- Sem Preco de venda</b> ' . $e->getMessage());
  419. TTransaction::rollback();
  420. }
  421. }
  422. }
  423. static function onProdutoChange( $params )
  424. {
  425. if( isset($params['id_produto']) && $params['id_produto'] )
  426. {
  427. try
  428. {
  429. TTransaction::open('proterra');
  430. $producto = new Produto ($params['id_produto']);
  431. $fill_data = new StdClass;
  432. $fill_data->vunt_prd = number_format($producto->prc_venda,2);
  433. TForm::sendData('OrcamentoForm', $fill_data);
  434. TTransaction::close();
  435. }
  436. catch (Exception $e) // in case of exception
  437. {
  438. new TMessage('error', '<b>Error- Sem Preco de venda</b> ' . $e->getMessage());
  439. TTransaction::rollback();
  440. }
  441. }
  442. }
  443. public static function onQtdChange( $params )
  444. {
  445. $dto = (double) str_replace(',', '', $params['vdsc_prd']);
  446. $preco_unitario = (double) str_replace(',', '', $params['vunt_prd']);
  447. $qte = (double) str_replace(',', '', $params['qtde_prd']);
  448. $obj = new StdClass;
  449. $obj->vtot_prd = number_format( ($preco_unitario * $qte) - $dto, 2, '.', ',');
  450. TForm::sendData('OrcamentoForm', $obj);
  451. }
  452. public function show()
  453. {
  454. parent::show();
  455. // check if the datagrid is already loaded
  456. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  457. {
  458. $this->onReload( func_get_arg(0) );
  459. }
  460. }
  461. }
  462. ?>



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)


PD

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,