Dashboard duplicando o valores na inserção de dados Estou com seguinte erro RANKING TOP 5 CLIENTES. Quando faço uma inserção de outros dados ou seja de contratos, exibe o mesmo valor do dado anterior para o próximo dado -- view create view view_contratos as select c.id as contrato_id, c.cliente_id, c.tipo_contrato_id, c.dt_inicio, c.dt_fim, tc.no_nome as tipo_contrato , p.nome as nome_cliente, ...
HL
Dashboard duplicando o valores na inserção de dados  
Estou com seguinte erro RANKING TOP 5 CLIENTES. Quando faço uma inserção de outros dados ou seja de contratos, exibe o mesmo valor do dado anterior para o próximo dado


-- view
create view view_contratos as
select c.id as contrato_id,
c.cliente_id,
c.tipo_contrato_id,
c.dt_inicio,
c.dt_fim,
tc.no_nome as tipo_contrato ,
p.nome as nome_cliente,
m.no_nome as cidade,
e.sg_uf as uf,
e.no_nome as nome_estado,
g.no_nome as nome_grupo,
st_ativo as ativo,
(select sum(nu_total) as total from rl_contrato_servico cs join tb_contrato c
on c.id =cs.contrato_id ) as "total"
from tb_contrato c join tb_tipo_contrato tc
on tc.id = c.tipo_contrato_id
join tb_pessoa p
on p.id = c.cliente_id
join tb_municipio m
on m.id = p.municipio_id
join tb_estado e
on e.id = m.estado_id
join tb_grupo g
on g.id = p.grupo_id;


 
  1. <?php
  2. /**
  3. * ContratoDashboard
  4. *
  5. * @version 1.0
  6. * @subpackage control
  7. * @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  8. * @license http://www.adianti.com.br/framework-license
  9. */
  10. class ContratoDashboard extends TPage
  11. {
  12. /**
  13. * Class constructor
  14. * Creates the page
  15. */
  16. function __construct()
  17. {
  18. parent::__construct();
  19. $vbox = new TVBox;
  20. $vbox->style = 'width: 100%';
  21. $div = new TElement('div');
  22. $div->class = "row";
  23. try
  24. {
  25. TTransaction::open('sys_db');
  26. $total_ativos = Contrato::where('st_ativo','=','Y')->count();
  27. $total_renovar = Contrato::where('st_ativo','=','Y')->where('dt_fim', '<=', date('Y-m-d'))->count();
  28. $contratos_grupo = ViewContratos::where('ativo','=','Y')->groupBy('nome_grupo')->sumBy('total');
  29. $contratos_tipo = ViewContratos::where('ativo','=','Y')->groupBy('tipo_contrato')->sumBy('total');
  30. $top_clientes = ViewContratos::where('ativo','=','Y')->groupBy('nome_cliente')->take(5)->orderBy('total', 'desc')->sumBy('total');
  31. $old_clientes = ViewContratos::where('ativo','=','Y')->take(5)->orderBy('dt_inicio')->load();
  32. var_dump($top_clientes);
  33. TTransaction::close();
  34. $indicator1 = new THtmlRenderer('app/resources/info-box.html');
  35. $indicator1->enableSection('main', ['title' => 'Contratos ativos', 'icon' => 'check-double', 'background' => 'green', 'value' => $total_ativos ] );
  36. $indicator2 = new THtmlRenderer('app/resources/info-box.html');
  37. $indicator2->enableSection('main', ['title' => 'Renovações pendentes', 'icon' => 'hourglass-start', 'background' => 'orange', 'value' => $total_renovar ] );
  38. $data = [];
  39. $data[] = [ 'Estado', 'Contratos' ];
  40. if ($contratos_grupo)
  41. {
  42. foreach($contratos_grupo as $contrato_grupo)
  43. {
  44. $data[] = [ $contrato_grupo->nome_grupo, (float) $contrato_grupo->total ];
  45. }
  46. }
  47. $grafico1 = new THtmlRenderer('app/resources/google_pie_chart.html');
  48. $grafico1->enableSection('main', ['data' => json_encode($data), 'width' => '100%', 'height' => '400px',
  49. 'title' => 'Contratos por grupo', 'ytitle' => 'Estado', 'xtitle' => 'Contratos', 'uniqid' => uniqid()]);
  50. $data = [];
  51. $data[] = [ 'Estado', 'Contratos' ];
  52. if ($contratos_tipo)
  53. {
  54. foreach($contratos_tipo as $contrato_tipo)
  55. {
  56. $data[] = [ $contrato_tipo->tipo_contrato, (float) $contrato_tipo->total ];
  57. }
  58. }
  59. $grafico2 = new THtmlRenderer('app/resources/google_pie_chart.html');
  60. $grafico2->enableSection('main', ['data' => json_encode($data), 'width' => '100%', 'height' => '400px',
  61. 'title' => 'Contratos por tipo', 'ytitle' => 'Tipo', 'xtitle' => 'Contratos', 'uniqid' => uniqid()]);
  62. $table1 = TTable::create( [ 'class' => 'table table-striped table-hover', 'style' => 'border-collapse:collapse' ] );
  63. $table1->addSection('thead');
  64. $table1->addRowSet('Cliente', 'Quantidade');
  65. if ($top_clientes)
  66. {
  67. $table1->addSection('tbody');
  68. foreach ($top_clientes as $top_cliente)
  69. {
  70. $row = $table1->addRow();
  71. $row->addCell($top_cliente->nome_cliente);
  72. $row->addCell('R$&nbsp;' . number_format($top_cliente->total,2,',','.'))->style = 'text-align:right';
  73. }
  74. }
  75. $table2 = TTable::create( [ 'class' => 'table table-striped table-hover', 'style' => 'border-collapse:collapse' ] );
  76. $table2->addSection('thead');
  77. $table2->addRowSet('Cliente', 'Data');
  78. if ($old_clientes)
  79. {
  80. $table2->addSection('tbody');
  81. foreach ($old_clientes as $old_cliente)
  82. {
  83. $row = $table2->addRow();
  84. $row->addCell($old_cliente->nome_cliente);
  85. $row->addCell(TDate::convertToMask($old_cliente->dt_inicio, 'yyyy-mm-dd', 'dd/mm/yyyy'));
  86. }
  87. }
  88. $div->add( TElement::tag('div', $indicator1, ['class' => 'col-sm-6']) );
  89. $div->add( TElement::tag('div', $indicator2, ['class' => 'col-sm-6']) );
  90. $div->add( TElement::tag('div', $grafico1, ['class' => 'col-sm-6']) );
  91. $div->add( TElement::tag('div', $grafico2, ['class' => 'col-sm-6']) );
  92. $div->add( TElement::tag('div', TPanelGroup::pack('Ranking TOP 5 Clientes', $table1), ['class' => 'col-sm-6']) );
  93. $div->add( TElement::tag('div', TPanelGroup::pack('Raking 5 Clientes mais antigos', $table2), ['class' => 'col-sm-6']) );
  94. //$vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  95. $vbox->add($div);
  96. }
  97. catch (Exception $e)
  98. {
  99. new TMessage('error', $e->getMessage());
  100. }
  101. parent::add($vbox);
  102. }
  103. }
 
  1. <?php
  2. /**
  3. * ContratoForm
  4. *
  5. * @version 1.0
  6. * @subpackage control
  7. * @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  8. * @license http://www.adianti.com.br/framework-license
  9. */
  10. class ContratoForm extends TWindow
  11. {
  12. protected $form; // form
  13. protected $fieldlist;
  14. /**
  15. * Class constructor
  16. * Creates the page and the registration form
  17. */
  18. function __construct($param)
  19. {
  20. parent::__construct($param);
  21. parent::setSize(0.8, null);
  22. parent::removePadding();
  23. parent::removeTitleBar();
  24. //parent::disableEscape();
  25. // creates the form
  26. $this->form = new BootstrapFormBuilder('form_Contrato');
  27. $this->form->setFormTitle('Contrato');
  28. $this->form->setClientValidation(true);
  29. // master fields
  30. $id = new TEntry('id');
  31. $cliente_id = new TDBUniqueSearch('cliente_id', 'sys_db', 'Pessoa', 'id', 'nome_fantasia');
  32. $tipo_contrato_id = new TDBUniqueSearch('tipo_contrato_id', 'sys_db', 'TipoContrato', 'id', 'no_nome');
  33. $ativo = new TRadioGroup('st_ativo');
  34. $dt_inicio = new TDate('dt_inicio');
  35. $dt_fim = new TDate('dt_fim');
  36. $obs = new TText('ds_obs');
  37. $cliente_id->setMinLength(0);
  38. $tipo_contrato_id->setMinLength(0);
  39. $ativo->addItems( ['Y' => 'Sim', 'N' => 'Não'] );
  40. $ativo->setLayout('horizontal');
  41. $dt_inicio->setMask('dd/mm/yyyy');
  42. $dt_fim->setMask('dd/mm/yyyy');
  43. $dt_inicio->setDatabaseMask('yyyy-mm-dd');
  44. $dt_fim->setDatabaseMask('yyyy-mm-dd');
  45. $dt_inicio->setSize('100%');
  46. $dt_fim->setSize('100%');
  47. $obs->setSize('100%', 60);
  48. $ativo->setValue('Y');
  49. $cliente_id->addValidation('Cliente', new TRequiredValidator);
  50. $tipo_contrato_id->addValidation('Tipo contrato', new TRequiredValidator);
  51. $ativo->addValidation('Ativo', new TRequiredValidator);
  52. $dt_inicio->addValidation('Data início', new TRequiredValidator);
  53. $dt_fim->addValidation('Data fim', new TRequiredValidator);
  54. // sizes
  55. $id->setSize('100%');
  56. $cliente_id->setSize('100%');
  57. $tipo_contrato_id->setSize('100%');
  58. $ativo->setSize('100%');
  59. $dt_inicio->setSize('100%');
  60. $dt_fim->setSize('100%');
  61. $id->setEditable(FALSE);
  62. // add form fields to the form
  63. $this->form->addFields( [new TLabel('Id')], [$id] );
  64. $this->form->addFields( [new TLabel('Cliente')], [$cliente_id] );
  65. $this->form->addFields( [new TLabel('Tipo Contrato')], [$tipo_contrato_id] );
  66. $this->form->addFields( [new TLabel('Ativo')], [$ativo] );
  67. $this->form->addFields( [new TLabel('Dt Inicio')], [$dt_inicio], [new TLabel('Dt Fim')], [$dt_fim] );
  68. $this->form->addFields( [new TLabel('Obs')], [$obs] );
  69. // detail fields
  70. $this->fieldlist = new TFieldList;
  71. $this->fieldlist-> width = '100%';
  72. $this->fieldlist->enableSorting();
  73. $servico_id = new TDBUniqueSearch('list_servico_id[]', 'sys_db', 'Servico', 'id', 'no_nome', null, TCriteria::create( ['st_ativo' => 'Y'] ));
  74. $valor = new TNumeric('list_valor[]', 2, ',', '.');
  75. $quantidade = new TNumeric('list_quantidade[]', 2, ',', '.');
  76. $servico_id->setChangeAction(new TAction(array($this, 'onChangeServico')));
  77. $servico_id->setSize('100%');
  78. $servico_id->setMinLength(0);
  79. $valor->setSize('100%');
  80. $quantidade->setSize('100%');
  81. $this->fieldlist->addField( '<b>Serviço</b>', $servico_id, ['width' => '40%']);
  82. $this->fieldlist->addField( '<b>Valor</b>', $valor, ['width' => '30%']);
  83. $this->fieldlist->addField( '<b>Quantidade</b>', $quantidade, ['width' => '30%']);
  84. $this->form->addField($servico_id);
  85. $this->form->addField($valor);
  86. $this->form->addField($quantidade);
  87. $detail_wrapper = new TElement('div');
  88. $detail_wrapper->add($this->fieldlist);
  89. $detail_wrapper->style = 'overflow-x:auto';
  90. $this->form->addContent( [ TElement::tag('h5', 'Itens do contrato', [ 'style'=>'background: whitesmoke; padding: 5px; border-radius: 5px; margin-top: 5px'] ) ] );
  91. $this->form->addContent( [ $detail_wrapper ] );
  92. // create actions
  93. //$this->form->addAction( _t('Save'), new TAction( [$this, 'onSave'] ), 'fa:save green' );
  94. $btn = $this->form->addAction( _t('Save'), new TAction( [$this, 'onSave'] ), 'fa:save default' );
  95. $btn->class = 'btn btn-sm btn-success';
  96. $this->form->addAction( _t('Clear'), new TAction( [$this, 'onClear'] ), 'fa:eraser red' );
  97. $this->form->addHeaderActionLink( _t('Close'), new TAction([$this, 'onClose']), 'fa:times red');
  98. // create the page container
  99. $container = new TVBox;
  100. $container->style = 'width: 100%';
  101. //$container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  102. $container->add($this->form);
  103. parent::add($container);
  104. }
  105. /**
  106. * Executed whenever the user clicks at the edit button da datagrid
  107. */
  108. function onEdit($param)
  109. {
  110. try
  111. {
  112. TTransaction::open('sys_db');
  113. if (isset($param['key']))
  114. {
  115. $key = $param['key'];
  116. $object = new Contrato($key);
  117. $this->form->setData($object);
  118. $items = ContratoItem::where('contrato_id', '=', $key)->load();
  119. if ($items)
  120. {
  121. $this->fieldlist->addHeader();
  122. foreach($items as $item )
  123. {
  124. $detail = new stdClass;
  125. $detail->list_servico_id = $item->servico_id;
  126. $detail->list_valor = $item->nu_valor;
  127. $detail->list_quantidade = $item->qt_qtde;
  128. $this->fieldlist->addDetail($detail);
  129. }
  130. $this->fieldlist->addCloneAction();
  131. }
  132. else
  133. {
  134. $this->onClear($param);
  135. }
  136. TTransaction::close(); // close transaction
  137. }
  138. else
  139. {
  140. $this->onClear($param);
  141. }
  142. }
  143. catch (Exception $e) // in case of exception
  144. {
  145. new TMessage('error', $e->getMessage());
  146. TTransaction::rollback();
  147. }
  148. }
  149. /**
  150. * Clear form
  151. */
  152. public function onClear($param)
  153. {
  154. $this->fieldlist->addHeader();
  155. $this->fieldlist->addDetail( new stdClass );
  156. $this->fieldlist->addCloneAction();
  157. }
  158. /**
  159. * Save the Contrato and the ContratoItem's
  160. */
  161. public static function onSave($param)
  162. {
  163. try
  164. {
  165. TTransaction::open('sys_db');
  166. $id = (int) $param['id'];
  167. $master = new Contrato;
  168. $master->fromArray( $param);
  169. $master->dt_inicio = TDateTime::convertToMask($param['dt_inicio'], 'dd/mm/yyyy', 'yyyy-mm-dd');
  170. $master->dt_fim = TDateTime::convertToMask($param['dt_fim'], 'dd/mm/yyyy', 'yyyy-mm-dd');
  171. $master->store(); // save master object
  172. // delete details
  173. ContratoItem::where('contrato_id', '=', $master->id)->delete();
  174. if( !empty($param['list_servico_id']) AND is_array($param['list_servico_id']) )
  175. {
  176. foreach( $param['list_servico_id'] as $row => $servico_id)
  177. {
  178. if (!empty($servico_id))
  179. {
  180. $detail = new ContratoItem;
  181. $detail->contrato_id = $master->id;
  182. $detail->servico_id = $param['list_servico_id'][$row];
  183. $detail->valor = (float) str_replace(['.',','], ['','.'], $param['list_valor'][$row]);
  184. $detail->quantidade = (float) str_replace(['.',','], ['','.'], $param['list_quantidade'][$row]);
  185. $detail->total = round($detail->valor * $detail->quantidade,2);
  186. $detail->store();
  187. }
  188. }
  189. }
  190. $data = new stdClass;
  191. $data->id = $master->id;
  192. TForm::sendData('form_Contrato', $data);
  193. TTransaction::close(); // close the transaction
  194. new TMessage('info', AdiantiCoreTranslator::translate('Record saved'));
  195. }
  196. catch (Exception $e) // in case of exception
  197. {
  198. new TMessage('error', $e->getMessage());
  199. TTransaction::rollback();
  200. }
  201. }
  202. /**
  203. * Change servico
  204. */
  205. public static function onChangeServico($param)
  206. {
  207. $input_id = $param['_field_id'];
  208. $servico_id = $param['_field_value'];
  209. $input_pieces = explode('_', $input_id);
  210. $unique_id = end($input_pieces);
  211. if ($servico_id)
  212. {
  213. $response = new stdClass;
  214. try
  215. {
  216. TTransaction::open('sys_db');
  217. $servico = Servico::find($servico_id);
  218. $response->{'list_quantidade_'.$unique_id} = '1,00';
  219. $response->{'list_valor_'.$unique_id} = number_format($servico->nu_valor,2,',', '.');
  220. TForm::sendData('form_Contrato', $response);
  221. TTransaction::close();
  222. }
  223. catch (Exception $e)
  224. {
  225. TTransaction::rollback();
  226. }
  227. }
  228. }
  229. /**
  230. * Close
  231. */
  232. public static function onClose($param)
  233. {
  234. parent::closeWindow();
  235. }
  236. }

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