Preencher um TCombo, com base no valor selecionado em um TDBUniq Bom dia... Amigos estou com um formulário onde digito o cliente, e quero que seja apresentado as lojas referentes a esse cliente selecionado. na combo. Anexo há tabela com o relacionamento. Abaixo segue o código completo, eu tentei escrever um código mas não retorna nada, fico agradecido desde já pela ajuda. ...
RF
Preencher um TCombo, com base no valor selecionado em um TDBUniq  
Bom dia...

Amigos estou com um formulário onde digito o cliente, e quero que seja apresentado as lojas referentes a esse cliente selecionado. na combo.
Anexo há tabela com o relacionamento.
Abaixo segue o código completo, eu tentei escrever um código mas não retorna nada, fico agradecido desde já pela ajuda.
 
  1. <?php
  2. /**
  3. * digitacao_solicitacao_de_carga Master/Detail
  4. * @author <your name here>
  5. */
  6. class digitacao_solicitacao_de_carga 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_solicitacao_de_carga');
  18. $this->form->setFormTitle('Solicitação de carga');
  19. $this->form->setFieldSizes('100%');
  20. // master fields
  21. $id = new TEntry('id');
  22. $status_carga = new TEntry('status_carga');
  23. $servico = new TEntry('servico');
  24. $data_cadastro = new TDate('data_cadastro');
  25. $unidade_devolucao = new TEntry('unidade_devolucao');
  26. $unidade_coleta = new TEntry('unidade_coleta');
  27. $uf_origem = new TEntry('uf_origem');
  28. $cidade_origem = new TEntry('cidade_origem');
  29. $uf_destino = new TEntry('uf_destino');
  30. $cidade_destino = new TEntry('cidade_destino');
  31. $prazo_entrega = new TEntry('prazo_entrega');
  32. $unidade_id = new TDBCombo ('unidade_id','banco','unidade','id','razao_social');
  33. $cliente_id = new TDBUniqueSearch('cliente_id','banco','cliente','id','razao_social');//TEntry('cliente_id');
  34. $loja_id = new TCombo('loja_id');//TDBCombo ('loja_id', 'banco','loja', 'id','nome_fantasia');//TEntry('loja_id');
  35. $remetente_cnpj = new TEntry('remetente_cnpj');
  36. $remetente_ie = new TEntry('remetente_ie');
  37. $remetente_rz_social = new TEntry('remetente_rz_social');
  38. $remetente_cep = new TEntry('remetente_cep');
  39. $remetente_uf = new TEntry('remetente_uf');
  40. $remetente_cidade = new TEntry('remetente_cidade');
  41. $remetente_endereco = new TEntry('remetente_endereco');
  42. $remetente_numero = new TEntry('remetente_numero');
  43. $remetente_bairro = new TEntry('remetente_bairro');
  44. $remetente_complemento = new TEntry('remetente_complemento');
  45. $remetente_telefone = new TEntry('remetente_telefone');
  46. $remetente_email = new TEntry('remetente_email');
  47. $destinatario_cnpj = new TEntry('destinatario_cnpj');
  48. $destinatario_ie = new TEntry('destinatario_ie');
  49. $destinatario_rz_social = new TEntry('destinatario_rz_social');
  50. $destinatario_cep = new TEntry('destinatario_cep');
  51. $destinatario_uf = new TEntry('destinatario_uf');
  52. $destinatario_id = new TDBCombo('destinatario_id','banco','destinatario','id','cnpj_cpf'); //TEntry('destinatario_id');
  53. $destinatario_cidade = new TEntry('destinatario_cidade');
  54. $destinatario_endereco = new TEntry('destinatario_endereco');
  55. $destinatario_numero = new TEntry('destinatario_numero');
  56. $destinatario_bairro = new TEntry('destinatario_bairro');
  57. $destinatario_completo = new TEntry('destinatario_completo');
  58. $destinatario_telefone = new TEntry('destinatario_telefone');
  59. $destinatario_email = new TEntry('destinatario_email');
  60. $expedior_cnpj = new TEntry('expedior_cnpj');
  61. $expedior_ie = new TEntry('expedior_ie');
  62. $expedior_rz_social = new TEntry('expedior_rz_social');
  63. $expedior_cep = new TEntry('expedior_cep');
  64. $expedior_uf = new TEntry('expedior_uf');
  65. $expedior_cidade = new TEntry('expedior_cidade');
  66. $expedior_endereco = new TEntry('expedior_endereco');
  67. $expedior_numero = new TEntry('expedior_numero');
  68. $expedior_bairro = new TEntry('expedior_bairro');
  69. $expedior_complemento = new TEntry('expedior_complemento');
  70. $expedior_telefone = new TEntry('expedior_telefone');
  71. $expedior_email = new TEntry('expedior_email');
  72. $tomador_cnpj = new TEntry('tomador_cnpj');
  73. $tomador_ie = new TEntry('tomador_ie');
  74. $tomador_rz_social = new TEntry('tomador_rz_social');
  75. $tomador_cep = new TEntry('tomador_cep');
  76. $tomador_uf = new TEntry('tomador_uf');
  77. $tomador_cidade = new TEntry('tomador_cidade');
  78. $tomador_endereco = new TEntry('tomador_endereco');
  79. $tomador_numero = new TEntry('tomador_numero');
  80. $tomador_bairro = new TEntry('tomador_bairro');
  81. $tomador_complemento = new TEntry('tomador_complemento');
  82. $tomador_telefone = new TEntry('tomador_telefone');
  83. $tomador_email = new TEntry('tomador_email');
  84. $tipo_de_documento = new TCombo('tipo_de_documento');//TEntry
  85. $valor_rs_total = new TEntry('valor_rs_total');
  86. $volume_total = new TEntry('volume_total');
  87. $peso_total = new TEntry('peso_total');
  88. $cubagem_total = new TEntry('cubagem_total');
  89. $peso_cubado_total = new TEntry('peso_cubado_total');
  90. $peso_taxado = new TEntry('peso_taxado');
  91. // detail fields
  92. $detail_id = new THidden('detail_id');
  93. $detail_nota_fiscal = new TEntry('detail_nota_fiscal');
  94. $detail_serie_nota_fiscal = new TEntry('detail_serie_nota_fiscal');
  95. $detail_danfe = new TEntry('detail_danfe');
  96. $detail_emissao = new TDate('detail_emissao');
  97. $detail_valor = new TEntry('detail_valor');
  98. $detail_pedido = new TEntry('detail_pedido');
  99. $detail_entrega = new TEntry('detail_entrega');
  100. $detail_carga = new TEntry('detail_carga');
  101. $detail_volumes = new TEntry('detail_volumes');
  102. $detail_peso_informado = new TEntry('detail_peso_informado');
  103. //$detail_peso_medido = new TEntry('detail_peso_medido');
  104. $detail_fator_cubagem = new TEntry('detail_fator_cubagem');
  105. $detail_comprimento = new TEntry('detail_comprimento');
  106. $detail_largura = new TEntry('detail_largura');
  107. $detail_altura = new TEntry('detail_altura');
  108. $detail_cubagem = new TEntry('detail_cubagem');
  109. $detail_peso_taxado = new TEntry('detail_peso_taxado');
  110. //$detail_itens_notas_fiscais_carga_id = new TEntry('detail_itens_notas_fiscais_carga_id');
  111. if (!empty($id))
  112. {
  113. $id->setEditable(FALSE);
  114. }
  115. //Mascara da data
  116. $data_cadastro ->setMask('dd/mm/yyyy');
  117. $detail_emissao ->setMask('dd/mm/yyyy');
  118. // Busca as informações e preenche os campos da loja
  119. $loja_id ->setChangeAction(new TAction([$this,'buscaDadosLoja']));
  120. $combo_tipodoc = array();
  121. $combo_tipodoc ['NF-e'] = 'NF-e';
  122. $combo_tipodoc ['Declaração'] = 'Declaração';
  123. $tipo_de_documento -> addItems($combo_tipodoc);
  124. $tipo_de_documento->setValue('NF-e');// default value
  125. //******************************
  126. $cliente_action = new TAction(array($this, 'onChangeAction'));
  127. $cliente_id->setChangeAction($cliente_action);
  128. // master fields
  129. // Primeira Aba
  130. $this->form->appendPage('Dados da nota');
  131. $row = $this->form->addFields( [ new TLabel('Número da solicitação'), $id ],
  132. [ new TLabel('Unidade'), $unidade_id ],
  133. [ new TLabel('Status'), $status_carga ],
  134. [ new TLabel('Data de digitação'), $data_cadastro ] );
  135. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-3','col-sm-3' ];
  136. // Campos das unidades
  137. $this->form->addContent( ['<h4>Dados das unidades</h4><hr>'] );
  138. $row = $this->form->addFields( [ new TLabel('Cliente'), $cliente_id ],
  139. [ new TLabel('Loja'), $loja_id ],
  140. [ new TLabel('Serviço'), $servico ]);
  141. $row->layout = ['col-sm-4', 'col-sm-4','col-sm-4' ];
  142. $row = $this->form->addFields( [ new TLabel('Unidade de devolução'), $unidade_devolucao ],
  143. [ new TLabel('Unidade de coleta'), $unidade_coleta ],
  144. [ new TLabel('Prazo de entrega'), $prazo_entrega ]
  145. );
  146. $row->layout = ['col-sm-4', 'col-sm-4','col-sm-4' ];
  147. // Campos de origem e destino
  148. $this->form->addContent( ['<h4>Origem e destino da carga</h4><hr>'] );
  149. $row = $this->form->addFields( [ new TLabel('UF origem'), $uf_origem],
  150. [ new TLabel('Cidade origem'), $cidade_origem ],
  151. [ new TLabel('UF destino'), $uf_destino],
  152. [ new TLabel('Cidade destino'), $cidade_destino] );
  153. $row->layout = ['col-sm-2', 'col-sm-4','col-sm-2', 'col-sm-4' ];
  154. // CAMPOS DAS NOTAS FISCAIS - APRESENTAÇÃO EM TELA
  155. $this->form->addContent( ['<h4>Dados da Nota Fiscal</h4><hr>'] );
  156. $this->form->addFields( [$detail_id] );
  157. $row = $this->form->addFields( [ new TLabel('Tipo de documento'), $tipo_de_documento ],
  158. [ new TLabel('Número do documento'),$detail_nota_fiscal ],
  159. [ new TLabel('Série'), $detail_serie_nota_fiscal ],
  160. [ new TLabel('Número da DANFE'), $detail_danfe ]
  161. );
  162. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-3','col-sm-3' ];
  163. // $this->form->addFields( [new TLabel('Tipo De Documento')], [$tipo_de_documento] );
  164. $row = $this->form->addFields( [ new TLabel('Emissão - DANFE'), $detail_emissao],
  165. [ new TLabel('Valor'), $detail_valor ],
  166. [ new TLabel('Volume'), $detail_volumes],
  167. [ new TLabel('Peso'), $detail_peso_informado] );
  168. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-3', 'col-sm-3' ];
  169. $row = $this->form->addFields( [ new TLabel('Comprimento'), $detail_comprimento ],
  170. [ new TLabel('Largura'), $detail_largura ],
  171. [ new TLabel('Altura'), $detail_altura ]
  172. );
  173. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
  174. $row = $this->form->addFields( [ new TLabel('Fator cubagem'), $detail_fator_cubagem ],
  175. [ new TLabel('Cubagem'), $detail_cubagem ],
  176. [ new TLabel('Peso Taxado'), $detail_peso_taxado ]
  177. );
  178. $row->layout = ['col-sm-4', 'col-sm-4','col-sm-4' ];
  179. $row = $this->form->addFields( [ new TLabel('Pedido'), $detail_pedido ],
  180. [ new TLabel('Entrega'), $detail_entrega ],
  181. [ new TLabel('Carga'), $detail_carga ]
  182. );
  183. $row->layout = ['col-sm-4', 'col-sm-4','col-sm-4' ];
  184. //$this->form->addFields( [new TLabel('Peso Medido')], [$detail_peso_medido] );
  185. //$this->form->addFields( [new TLabel('Itens Notas Fiscais Carga Id')], [$detail_itens_notas_fiscais_carga_id] );
  186. $add = TButton::create('add', [$this, 'onSaveDetail'], 'Register', 'fa:save');
  187. $this->form->addFields( [], [$add] )->style = 'background: whitesmoke; padding: 5px; margin: 1px;';
  188. $this->detail_list = new BootstrapDatagridWrapper(new TQuickGrid);
  189. $this->detail_list->style = "min-width: 700px; width:100%;margin-bottom: 10px";
  190. $this->detail_list->setId('solicitacao_de_carga_list');
  191. // items
  192. $this->detail_list->addQuickColumn('N.F', 'nota_fiscal', 'left', 100);
  193. $this->detail_list->addQuickColumn('Série', 'serie_nota_fiscal', 'left', 100);
  194. //$this->detail_list->addQuickColumn('Danfe', 'danfe', 'left', 100);
  195. //$this->detail_list->addQuickColumn('Emissao', 'emissao', 'left', 100);
  196. $this->detail_list->addQuickColumn('Valor', 'valor', 'left', 100);
  197. //$this->detail_list->addQuickColumn('Pedido', 'pedido', 'left', 100);
  198. //$this->detail_list->addQuickColumn('Entrega', 'entrega', 'left', 100);
  199. //$this->detail_list->addQuickColumn('Carga', 'carga', 'left', 100);
  200. $this->detail_list->addQuickColumn('Volumes', 'volumes', 'left', 100);
  201. $this->detail_list->addQuickColumn('Peso', 'peso_informado', 'left', 100);
  202. //$this->detail_list->addQuickColumn('Peso Medido', 'peso_medido', 'left', 100);
  203. $this->detail_list->addQuickColumn('Fator Cubagem', 'fator_cubagem', 'left', 100);
  204. //$this->detail_list->addQuickColumn('Comprimento', 'comprimento', 'left', 100);
  205. //$this->detail_list->addQuickColumn('Largura', 'largura', 'left', 100);
  206. //$this->detail_list->addQuickColumn('Altura', 'altura', 'left', 100);
  207. $this->detail_list->addQuickColumn('Cubagem', 'cubagem', 'left', 100);
  208. $this->detail_list->addQuickColumn('Peso Taxado', 'peso_taxado', 'left', 100);
  209. //$this->detail_list->addQuickColumn('Itens Notas Fiscais Carga Id', 'itens_notas_fiscais_carga_id', 'left', 100);
  210. // detail actions
  211. $this->detail_list->addQuickAction( 'Edit', new TDataGridAction([$this, 'onEditDetail']), 'id', 'fa:edit blue');
  212. $this->detail_list->addQuickAction( 'Delete', new TDataGridAction([$this, 'onDeleteDetail']), 'id', 'fa:trash red');
  213. $this->detail_list->createModel();
  214. $panel = new TPanelGroup;
  215. $panel->add($this->detail_list);
  216. $panel->getBody()->style = 'overflow-x:auto';
  217. $this->form->addContent( [$panel] );
  218. // Campos que apresentam o total da(s) nota(s)
  219. $this->form->addContent( ['<h4>Valor total das notas</h4><hr>'] );
  220. $row = $this->form->addFields( [ new TLabel('Total R$'), $valor_rs_total],
  221. [ new TLabel('Total de volume'), $volume_total ],
  222. [ new TLabel('Total de peso'), $peso_total],
  223. [ new TLabel('Cubagem'), $cubagem_total ],
  224. [ new TLabel('Peso cubado'), $peso_cubado_total],
  225. [ new TLabel('Peso taxado'), $peso_taxado] );
  226. $row->layout = ['col-sm-2', 'col-sm-2','col-sm-2', 'col-sm-2','col-sm-2', 'col-sm-2' ];
  227. // Segunda Aba
  228. $this->form->appendPage('Remetente');
  229. $row = $this->form->addFields( [ new TLabel('CNPJ'), $remetente_cnpj ],
  230. [ new TLabel('Inscrição Estadual'), $remetente_ie ],
  231. [ new TLabel('Razão Social'), $remetente_rz_social ]
  232. );
  233. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
  234. $row = $this->form->addFields( [ new TLabel('CEP'), $remetente_cep ],
  235. [ new TLabel('UF'), $remetente_uf ],
  236. [ new TLabel('Cidade'), $remetente_cidade ]
  237. );
  238. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
  239. $row = $this->form->addFields( [ new TLabel('Endereço'), $remetente_endereco ],
  240. [ new TLabel('Número'), $remetente_numero ]
  241. );
  242. $row->layout = ['col-sm-8', 'col-sm-4' ];
  243. $row = $this->form->addFields( [ new TLabel('Bairro'), $remetente_bairro],
  244. [ new TLabel('Complemento'), $remetente_complemento ]
  245. );
  246. $row->layout = ['col-sm-4', 'col-sm-8' ];
  247. $row = $this->form->addFields( [ new TLabel('Telefone'), $remetente_telefone],
  248. [ new TLabel('E-mail'), $remetente_email ]
  249. );
  250. $row->layout = ['col-sm-4', 'col-sm-8' ];
  251. // Terceira Aba
  252. $this->form->appendPage('Destinatário');
  253. $row = $this->form->addFields( [ new TLabel('CNPJ'), $destinatario_id], //$destinatario_cnpj ],
  254. [ new TLabel('Inscrição Estadual'), $destinatario_ie ],
  255. [ new TLabel('Razão Social'), $destinatario_rz_social ]
  256. );
  257. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
  258. $row = $this->form->addFields( [ new TLabel('CEP'), $destinatario_cep ],
  259. [ new TLabel('UF'), $destinatario_uf ],
  260. [ new TLabel('Cidade'), $destinatario_cidade ]
  261. );
  262. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
  263. $row = $this->form->addFields( [ new TLabel('Endereço'), $destinatario_endereco ],
  264. [ new TLabel('Número'), $destinatario_numero ]
  265. );
  266. $row->layout = ['col-sm-8', 'col-sm-4' ];
  267. $row = $this->form->addFields( [ new TLabel('Bairro'), $destinatario_bairro],
  268. [ new TLabel('Complemento'), $destinatario_completo ]
  269. );
  270. $row->layout = ['col-sm-4', 'col-sm-8' ];
  271. $row = $this->form->addFields( [ new TLabel('Telefone'), $destinatario_telefone],
  272. [ new TLabel('E-mail'), $destinatario_email ]
  273. );
  274. $row->layout = ['col-sm-4', 'col-sm-8' ];
  275. // Quarta Aba
  276. $this->form->appendPage('Expedidor');
  277. $row = $this->form->addFields( [ new TLabel('CNPJ'), $expedior_cnpj ],
  278. [ new TLabel('Inscrição Estadual.'), $expedior_ie ],
  279. [ new TLabel('Razão Social'), $expedior_rz_social ]
  280. );
  281. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
  282. $row = $this->form->addFields( [ new TLabel('CEP'), $expedior_cep ],
  283. [ new TLabel('UF'), $expedior_uf ],
  284. [ new TLabel('Cidade'), $expedior_cidade ]
  285. );
  286. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
  287. $row = $this->form->addFields( [ new TLabel('Endereço'), $expedior_endereco ],
  288. [ new TLabel('Número'), $expedior_numero ]
  289. );
  290. $row->layout = ['col-sm-8', 'col-sm-4' ];
  291. $row = $this->form->addFields( [ new TLabel('Bairro'), $expedior_bairro],
  292. [ new TLabel('Complemento'), $expedior_complemento]
  293. );
  294. $row->layout = ['col-sm-4', 'col-sm-8' ];
  295. $row = $this->form->addFields( [ new TLabel('Telefone'), $expedior_telefone],
  296. [ new TLabel('E-mail'), $expedior_email ]
  297. );
  298. $row->layout = ['col-sm-4', 'col-sm-8' ];
  299. // Quinta Aba
  300. $this->form->appendPage('Tomador');
  301. //$this->form->appendPage('Expedidor');
  302. $row = $this->form->addFields( [ new TLabel('CNPJ'), $tomador_cnpj ],
  303. [ new TLabel('Inscrição Estadual.'), $tomador_ie ],
  304. [ new TLabel('Razão Social'), $tomador_rz_social ]
  305. );
  306. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
  307. $row = $this->form->addFields( [ new TLabel('CEP'), $tomador_cep ],
  308. [ new TLabel('UF'), $tomador_uf ],
  309. [ new TLabel('Cidade'), $tomador_cidade ]
  310. );
  311. $row->layout = ['col-sm-3', 'col-sm-3','col-sm-6' ];
  312. $row = $this->form->addFields( [ new TLabel('Endereço'), $tomador_endereco ],
  313. [ new TLabel('Número'), $tomador_numero ]
  314. );
  315. $row->layout = ['col-sm-8', 'col-sm-4' ];
  316. $row = $this->form->addFields( [ new TLabel('Bairro'), $tomador_bairro],
  317. [ new TLabel('Complemento'), $tomador_complemento]
  318. );
  319. $row->layout = ['col-sm-4', 'col-sm-8' ];
  320. $row = $this->form->addFields( [ new TLabel('Telefone'), $tomador_telefone],
  321. [ new TLabel('E-mail'), $tomador_email ]
  322. );
  323. $row->layout = ['col-sm-4', 'col-sm-8' ];
  324. $btn = $this->form->addAction( _t('Save'), new TAction([$this, 'onSave']), 'fa:save');
  325. $btn->class = 'btn btn-sm btn-primary';
  326. $this->form->addAction( _t('Clear'), new TAction([$this, 'onClear']), 'fa:eraser red');
  327. // create the page container
  328. $container = new TVBox;
  329. $container->style = 'width: 90%';
  330. $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  331. $container->add($this->form);
  332. parent::add($container);
  333. }
  334. /**
  335. * Clear form
  336. * @param $param URL parameters
  337. */
  338. public function onClear($param)
  339. {
  340. $this->form->clear(TRUE);
  341. TSession::setValue(__CLASS__.'_items', array());
  342. $this->onReload( $param );
  343. }
  344. /**
  345. * Busca os dados da loja
  346. */
  347. static function buscaDadosLoja( $params )
  348. {
  349. if( !empty($params['loja_id']) )
  350. {
  351. try
  352. {
  353. TTransaction::open('banco');
  354. // MODEL
  355. $loja = new loja($params['loja_id']);
  356. $campos_model = new StdClass;
  357. $campos_model ->servico = $loja->razao_social;
  358. $campos_model ->uf_origem = $loja->uf;
  359. $campos_model ->cidade_origem = $loja->cidade;
  360. // Remetente, expedidor e toamador
  361. $campos_model ->remetente_rz_social = $loja->razao_social; // campo do form = model->campo na model
  362. $campos_model ->expedior_rz_social = $loja->razao_social;
  363. $campos_model ->tomador_rz_social = $loja->razao_social;
  364. $campos_model ->remetente_cnpj = $loja->cnpj;
  365. $campos_model ->expedior_cnpj = $loja->cnpj;
  366. $campos_model ->tomador_cnpj = $loja->cnpj;
  367. $campos_model ->remetente_ie = $loja->ie;
  368. $campos_model ->expedior_ie = $loja->ie;
  369. $campos_model ->tomador_ie = $loja->ie;
  370. $campos_model ->remetente_cep = $loja->cep;
  371. $campos_model ->expedior_cep = $loja->cep;
  372. $campos_model ->tomador_cep = $loja->cep;
  373. $campos_model ->remetente_uf = $loja->uf;
  374. $campos_model ->expedior_uf = $loja->uf;
  375. $campos_model ->tomador_uf = $loja->uf;
  376. $campos_model ->remetente_cidade = $loja->cidade;
  377. $campos_model ->expedior_cidade = $loja->cidade;
  378. $campos_model ->tomador_cidade = $loja->cidade;
  379. $campos_model ->remetente_endereco = $loja->endereco;
  380. $campos_model ->expedior_endereco = $loja->endereco;
  381. $campos_model ->tomador_endereco = $loja->endereco;
  382. $campos_model ->remetente_numero = $loja->numero;
  383. $campos_model ->expedior_numero = $loja->numero;
  384. $campos_model ->tomador_numero = $loja->numero;
  385. $campos_model ->remetente_bairro = $loja->bairro;
  386. $campos_model ->expedior_bairro = $loja->bairro;
  387. $campos_model ->tomador_bairro = $loja->bairro;
  388. $campos_model ->remetente_complemento = $loja->complemento;
  389. $campos_model ->expedior_complemento = $loja->complemento;
  390. $campos_model ->tomador_complemento = $loja->complemento;
  391. $campos_model ->remetente_telefone = $loja->telefone;
  392. $campos_model ->expedior_telefone = $loja->telefone;
  393. $campos_model ->tomador_telefone = $loja->telefone;
  394. $campos_model ->remetente_email = $loja->email;
  395. $campos_model ->expedior_email = $loja->email;
  396. $campos_model ->tomador_email = $loja->email;
  397. TForm::sendData('form_solicitacao_de_carga', $campos_model);
  398. TTransaction::close();
  399. }
  400. catch (Exception $e) // in case of exception
  401. {
  402. new TMessage('error', $e->getMessage());
  403. TTransaction::rollback();
  404. }
  405. }
  406. }
  407. public static function onChangeAction($param)
  408. {
  409. try
  410. {
  411. $object = new StdClass;
  412. $object->iduf = $param['cliente_id'];
  413. $clienteloja = new Cliente;
  414. $cs = $clienteloja->loja($object->id);
  415. $item = array();
  416. foreach($cs as $c)
  417. {
  418. $item[$clienteloja] = $c['loja_id'];
  419. }
  420. TCombo::reload('form_solicitacao_de_carga', 'loja_id', $item);
  421. }
  422. catch (Exception $e)
  423. {
  424. new TMessage('error',$e->getMessage());
  425. }
  426. }
  427. /**
  428. * Save an item from form to session list
  429. * @param $param URL parameters
  430. */
  431. public function onSaveDetail( $param )
  432. {
  433. try
  434. {
  435. TTransaction::open('banco');
  436. $data = $this->form->getData();
  437. /** validation sample
  438. if (empty($data->fieldX))
  439. {
  440. throw new Exception('The field fieldX is required');
  441. }
  442. **/
  443. $items = TSession::getValue(__CLASS__.'_items');
  444. $key = empty($data->detail_id) ? 'X'.mt_rand(1000000000, 1999999999) : $data->detail_id;
  445. $items[ $key ] = array();
  446. $items[ $key ]['id'] = $key;
  447. $items[ $key ]['nota_fiscal'] = $data->detail_nota_fiscal;
  448. $items[ $key ]['serie_nota_fiscal'] = $data->detail_serie_nota_fiscal;
  449. $items[ $key ]['danfe'] = $data->detail_danfe;
  450. $items[ $key ]['emissao'] = $data->detail_emissao;
  451. $items[ $key ]['valor'] = $data->detail_valor;
  452. $items[ $key ]['pedido'] = $data->detail_pedido;
  453. $items[ $key ]['entrega'] = $data->detail_entrega;
  454. $items[ $key ]['carga'] = $data->detail_carga;
  455. $items[ $key ]['volumes'] = $data->detail_volumes;
  456. $items[ $key ]['peso_informado'] = $data->detail_peso_informado;
  457. //$items[ $key ]['peso_medido'] = $data->detail_peso_medido;
  458. $items[ $key ]['fator_cubagem'] = $data->detail_fator_cubagem;
  459. $items[ $key ]['comprimento'] = $data->detail_comprimento;
  460. $items[ $key ]['largura'] = $data->detail_largura;
  461. $items[ $key ]['altura'] = $data->detail_altura;
  462. $items[ $key ]['cubagem'] = $data->detail_cubagem;
  463. $items[ $key ]['peso_taxado'] = $data->detail_peso_taxado;
  464. //$items[ $key ]['itens_notas_fiscais_carga_id'] = $data->detail_itens_notas_fiscais_carga_id;
  465. TSession::setValue(__CLASS__.'_items', $items);
  466. // clear detail form fields
  467. $data->detail_id = '';
  468. $data->detail_nota_fiscal = '';
  469. $data->detail_serie_nota_fiscal = '';
  470. $data->detail_danfe = '';
  471. $data->detail_emissao = '';
  472. $data->detail_valor = '';
  473. $data->detail_pedido = '';
  474. $data->detail_entrega = '';
  475. $data->detail_carga = '';
  476. $data->detail_volumes = '';
  477. $data->detail_peso_informado = '';
  478. //$data->detail_peso_medido = '';
  479. $data->detail_fator_cubagem = '';
  480. $data->detail_comprimento = '';
  481. $data->detail_largura = '';
  482. $data->detail_altura = '';
  483. $data->detail_cubagem = '';
  484. $data->detail_peso_taxado = '';
  485. //$data->detail_itens_notas_fiscais_carga_id = '';
  486. TTransaction::close();
  487. $this->form->setData($data);
  488. $this->onReload( $param ); // reload the items
  489. }
  490. catch (Exception $e)
  491. {
  492. $this->form->setData( $this->form->getData());
  493. new TMessage('error', $e->getMessage());
  494. }
  495. }
  496. /**
  497. * Load an item from session list to detail form
  498. * @param $param URL parameters
  499. */
  500. public static function onEditDetail( $param )
  501. {
  502. // read session items
  503. $items = TSession::getValue(__CLASS__.'_items');
  504. // get the session item
  505. $item = $items[ $param['key'] ];
  506. $data = new stdClass;
  507. $data->detail_id = $item['id'];
  508. $data->detail_nota_fiscal = $item['nota_fiscal'];
  509. $data->detail_serie_nota_fiscal = $item['serie_nota_fiscal'];
  510. $data->detail_danfe = $item['danfe'];
  511. $data->detail_emissao = $item['emissao'];
  512. $data->detail_valor = $item['valor'];
  513. $data->detail_pedido = $item['pedido'];
  514. $data->detail_entrega = $item['entrega'];
  515. $data->detail_carga = $item['carga'];
  516. $data->detail_volumes = $item['volumes'];
  517. $data->detail_peso_informado = $item['peso_informado'];
  518. //$data->detail_peso_medido = $item['peso_medido'];
  519. $data->detail_fator_cubagem = $item['fator_cubagem'];
  520. $data->detail_comprimento = $item['comprimento'];
  521. $data->detail_largura = $item['largura'];
  522. $data->detail_altura = $item['altura'];
  523. $data->detail_cubagem = $item['cubagem'];
  524. $data->detail_peso_taxado = $item['peso_taxado'];
  525. //$data->detail_itens_notas_fiscais_carga_id = $item['itens_notas_fiscais_carga_id'];
  526. // fill detail fields
  527. TForm::sendData( 'form_solicitacao_de_carga', $data );
  528. }
  529. /**
  530. * Delete an item from session list
  531. * @param $param URL parameters
  532. */
  533. public static function onDeleteDetail( $param )
  534. {
  535. // reset items
  536. $data = new stdClass;
  537. $data->detail_nota_fiscal = '';
  538. $data->detail_serie_nota_fiscal = '';
  539. $data->detail_danfe = '';
  540. $data->detail_emissao = '';
  541. $data->detail_valor = '';
  542. $data->detail_pedido = '';
  543. $data->detail_entrega = '';
  544. $data->detail_carga = '';
  545. $data->detail_volumes = '';
  546. $data->detail_peso_informado = '';
  547. // $data->detail_peso_medido = '';
  548. $data->detail_fator_cubagem = '';
  549. $data->detail_comprimento = '';
  550. $data->detail_largura = '';
  551. $data->detail_altura = '';
  552. $data->detail_cubagem = '';
  553. $data->detail_peso_taxado = '';
  554. //$data->detail_itens_notas_fiscais_carga_id = '';
  555. // clear form data
  556. TForm::sendData('form_solicitacao_de_carga', $data );
  557. // read session items
  558. $items = TSession::getValue(__CLASS__.'_items');
  559. // get detail id
  560. $detail_id = $param['key'];
  561. // delete the item from session
  562. unset($items[ $detail_id ] );
  563. // rewrite session items
  564. TSession::setValue(__CLASS__.'_items', $items);
  565. // delete item from screen
  566. TScript::create("ttable_remove_row_by_id('solicitacao_de_carga_list', '{$detail_id}')");
  567. }
  568. /**
  569. * Load the items list from session
  570. * @param $param URL parameters
  571. */
  572. public function onReload($param)
  573. {
  574. // read session items
  575. $items = TSession::getValue(__CLASS__.'_items');
  576. $this->detail_list->clear(); // clear detail list
  577. if ($items)
  578. {
  579. foreach ($items as $list_item)
  580. {
  581. $item = (object) $list_item;
  582. $row = $this->detail_list->addItem( $item );
  583. $row->id = $list_item['id'];
  584. }
  585. }
  586. $this->loaded = TRUE;
  587. }
  588. /**
  589. * Load Master/Detail data from database to form/session
  590. */
  591. public function onEdit($param)
  592. {
  593. try
  594. {
  595. TTransaction::open('banco');
  596. if (isset($param['key']))
  597. {
  598. $key = $param['key'];
  599. $object = new solicitacao_de_carga($key);
  600. $items = notas_fiscais_carga::where('solicitacao_de_carga_id', '=', $key)->load();
  601. $session_items = array();
  602. foreach( $items as $item )
  603. {
  604. $item_key = $item->id;
  605. $session_items[$item_key] = $item->toArray();
  606. $session_items[$item_key]['id'] = $item->id;
  607. $session_items[$item_key]['nota_fiscal'] = $item->nota_fiscal;
  608. $session_items[$item_key]['serie_nota_fiscal'] = $item->serie_nota_fiscal;
  609. $session_items[$item_key]['danfe'] = $item->danfe;
  610. $session_items[$item_key]['emissao'] = $item->emissao;
  611. $session_items[$item_key]['valor'] = $item->valor;
  612. $session_items[$item_key]['pedido'] = $item->pedido;
  613. $session_items[$item_key]['entrega'] = $item->entrega;
  614. $session_items[$item_key]['carga'] = $item->carga;
  615. $session_items[$item_key]['volumes'] = $item->volumes;
  616. $session_items[$item_key]['peso_informado'] = $item->peso_informado;
  617. //$session_items[$item_key]['peso_medido'] = $item->peso_medido;
  618. $session_items[$item_key]['fator_cubagem'] = $item->fator_cubagem;
  619. $session_items[$item_key]['comprimento'] = $item->comprimento;
  620. $session_items[$item_key]['largura'] = $item->largura;
  621. $session_items[$item_key]['altura'] = $item->altura;
  622. $session_items[$item_key]['cubagem'] = $item->cubagem;
  623. $session_items[$item_key]['peso_taxado'] = $item->peso_taxado;
  624. //$session_items[$item_key]['itens_notas_fiscais_carga_id'] = $item->itens_notas_fiscais_carga_id;
  625. }
  626. TSession::setValue(__CLASS__.'_items', $session_items);
  627. $this->form->setData($object); // fill the form with the active record data
  628. $this->onReload( $param ); // reload items list
  629. TTransaction::close(); // close transaction
  630. }
  631. else
  632. {
  633. $this->form->clear(TRUE);
  634. TSession::setValue(__CLASS__.'_items', null);
  635. $this->onReload( $param );
  636. }
  637. }
  638. catch (Exception $e) // in case of exception
  639. {
  640. new TMessage('error', $e->getMessage());
  641. TTransaction::rollback();
  642. }
  643. }
  644. /**
  645. * Save the Master/Detail data from form/session to database
  646. */
  647. public function onSave()
  648. {
  649. try
  650. {
  651. // open a transaction with database
  652. TTransaction::open('banco');
  653. $data = $this->form->getData();
  654. $master = new solicitacao_de_carga;
  655. $master->fromArray( (array) $data);
  656. $this->form->validate(); // form validation
  657. $master->store(); // save master object
  658. // delete details
  659. $old_items = notas_fiscais_carga::where('solicitacao_de_carga_id', '=', $master->id)->load();
  660. $keep_items = array();
  661. // get session items
  662. $items = TSession::getValue(__CLASS__.'_items');
  663. if( $items )
  664. {
  665. foreach( $items as $item )
  666. {
  667. if (substr($item['id'],0,1) == 'X' ) // new record
  668. {
  669. $detail = new notas_fiscais_carga;
  670. }
  671. else
  672. {
  673. $detail = notas_fiscais_carga::find($item['id']);
  674. }
  675. $detail->nota_fiscal = $item['nota_fiscal'];
  676. $detail->serie_nota_fiscal = $item['serie_nota_fiscal'];
  677. $detail->danfe = $item['danfe'];
  678. $detail->emissao = $item['emissao'];
  679. $detail->valor = $item['valor'];
  680. $detail->pedido = $item['pedido'];
  681. $detail->entrega = $item['entrega'];
  682. $detail->carga = $item['carga'];
  683. $detail->volumes = $item['volumes'];
  684. $detail->peso_informado = $item['peso_informado'];
  685. //$detail->peso_medido = $item['peso_medido'];
  686. $detail->fator_cubagem = $item['fator_cubagem'];
  687. $detail->comprimento = $item['comprimento'];
  688. $detail->largura = $item['largura'];
  689. $detail->altura = $item['altura'];
  690. $detail->cubagem = $item['cubagem'];
  691. $detail->peso_taxado = $item['peso_taxado'];
  692. //$detail->itens_notas_fiscais_carga_id = $item['itens_notas_fiscais_carga_id'];
  693. $detail->solicitacao_de_carga_id = $master->id;
  694. $detail->store();
  695. $keep_items[] = $detail->id;
  696. }
  697. }
  698. if ($old_items)
  699. {
  700. foreach ($old_items as $old_item)
  701. {
  702. if (!in_array( $old_item->id, $keep_items))
  703. {
  704. $old_item->delete();
  705. }
  706. }
  707. }
  708. TTransaction::close(); // close the transaction
  709. // reload form and session items
  710. $this->onEdit(array('key'=>$master->id));
  711. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  712. }
  713. catch (Exception $e) // in case of exception
  714. {
  715. new TMessage('error', $e->getMessage());
  716. $this->form->setData( $this->form->getData() ); // keep form data
  717. TTransaction::rollback();
  718. }
  719. }
  720. /**
  721. * Show the page
  722. */
  723. public function show()
  724. {
  725. // check if the datagrid is already loaded
  726. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  727. {
  728. $this->onReload( func_get_arg(0) );
  729. }
  730. parent::show();
  731. }
  732. }

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 (4)


NR

 
  1. <?php
  2. public static function onChangeAction($param)
  3. {
  4. try
  5. {
  6. $cliente = new Cliente($param['cliente_id']); //primeiro passo é instanciar o cliente através do seu id($param['id'])
  7. $lojas = $cliente->getLojas(); // se vc criou a composicao entre cliente e loja, deve existir a função getLojas no model Cliente
  8. $item = array();
  9. foreach($lojas as $loja)
  10. {
  11. $item[$loja->id] = $loja->razao_social;
  12. }
  13. TCombo::reload('form_solicitacao_de_carga', 'loja_id', $item);
  14. }
  15. catch (Exception $e)
  16. {
  17. new TMessage('error',$e->getMessage());
  18. }
  19. }
  20. ?>
RF

Boa noite, testei aqui mas deu um erro, não entendi o que quer dizer.

Sem transação ativa com a base de dados:AdiantiDatabaseTRecord::load cliente
NR

Antes de qualquer operação com consulta a banco de dados é preciso abrir uma transação:
 
  1. <?php
  2. TTransaction::open('nome_conexao');
  3. ...
  4. $cliente = new Cliente($param['cliente_id']);
  5. ...
  6. TTransaction::close();
  7. ?>
RF

Bom dia, Nataniel.
Muito obrigado, funcionou, estou pulando de alegria, rssss.

As coisas estão caminhando bem no meu projeto, tenho ainda algumas dúvidas sobre outros pontos nesse mesmo formulário, mas serão tópicos para outros posts no forum. por enquanto é isso. Valeuuuuuu.

public static function onChangeAction($params)
{
try
{
TTransaction::open('banco');

$cliente_loja = new cliente_loja($params['cliente_id']);
//primeiro passo é instanciar o cliente através do seu id($param['id'])
$lojas = $cliente_loja->getLojas();
// se vc criou a composicao entre cliente e loja, deve existir a função getLojas no model Cliente

$item = array();
foreach($lojas as $loja)
{
$item[$loja->id] = $loja->razao_social;
}

TCombo::reload('form_solicitacao_de_carga', 'loja_id', $item);

TTransaction::close();
}
catch (Exception $e)
{
new TMessage('error',$e->getMessage());
}

}