Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Gerar relatorio PDF e imprimi-lo A principio eu criei um formulário mestre/detalhe para cadastrar Vendas (mestre) e ItensVenda (detalhes , até então tudo certo. Agora preciso de um página que liste todas as despesas com seus respectivos itens e com opção para gerar um relatório em PDF e impressão. ...
JP
Gerar relatorio PDF e imprimi-lo  
A principio eu criei um formulário mestre/detalhe para cadastrar Vendas (mestre) e ItensVenda (detalhes , até então tudo certo. Agora preciso de um página que liste todas as despesas com seus respectivos itens e com opção para gerar um relatório em PDF e impressão.

  1. <?php
  2. /**
  3.  * VendaForm Master/Detail
  4.  * @author  <your name here>
  5.  */
  6. class VendaForm extends TPage
  7. {
  8.     protected $form// form
  9.     protected $detail_list;
  10.     
  11.     /**
  12.      * Page constructor
  13.      */
  14.     public function __construct()
  15.     {
  16.         parent::__construct();
  17.         
  18.         // creates the form
  19.         $this->form = new BootstrapFormBuilder('form_Venda');
  20.         $this->form->setFormTitle('Venda');
  21.         
  22.         // master fields
  23.         $ven_cod = new TEntry('ven_cod');
  24.         $cliente = new TEntry('cliente');
  25.         $cli_cod_fk = new TDBUniqueSearch('cli_cod_fk''bd_venda''Cliente''cli_cod''cli_nome');
  26.         // detail fields
  27.         $detail_itv_cod = new THidden('detail_itv_cod');
  28.         $detail_cod_prod = new TDBUniqueSearch('detail_cod_prod''bd_venda''Produto''pro_cod''pro_nome');
  29.         $detail_preco_venda = new TEntry('detail_preco_venda');
  30.         $detail_quantidade = new TEntry('detail_quantidade');
  31.        
  32.        
  33.         $detail_cod_prod->setChangeAction( new TAction([$this'onProductChange']));
  34.         if (!empty($ven_cod))
  35.         {
  36.             $ven_cod->setEditable(FALSE);
  37.         }
  38.         
  39.         // master fields
  40.         $this->form->addFields( [new TLabel('Código')], [$ven_cod] );
  41.         $this->form->addFields( [new TLabel('Cliente')], [$cliente] );
  42.         $this->form->addFields( [new TLabel('Cliente Fidelizado')], [$cli_cod_fk] );
  43.         
  44.         // detail fields
  45.         $this->form->addContent( ['<h4>Detalhes da Venda</h4><hr>'] );
  46.         $this->form->addFields( [$detail_itv_cod] );
  47.         
  48.         $this->form->addFields( [new TLabel('Produto')], [$detail_cod_prod] );
  49.         $this->form->addFields( [new TLabel('Preço ')], [$detail_preco_venda] );
  50.         $this->form->addFields( [new TLabel('Quantidade')], [$detail_quantidade] );
  51.        
  52.         $add TButton::create('add', [$this'onSaveDetail'], 'Register''fa:save');
  53.         $this->form->addFields( [], [$add] )->style 'background: whitesmoke; padding: 5px; margin: 1px;';
  54.         
  55.         $this->detail_list = new BootstrapDatagridWrapper(new TQuickGrid);
  56.         $this->detail_list->style "min-width: 700px; width:100%;margin-bottom: 10px";
  57.         $this->detail_list->setId('Venda_list');
  58.         
  59.         // items
  60.       $prod=   $this->detail_list->addQuickColumn('Produto''cod_prod''left'50);
  61.       $preco=  $this->detail_list->addQuickColumn('Preço ''preco_venda''left'30);
  62.         $this->detail_list->addQuickColumn('Quantidade''quantidade''left'30);
  63.       
  64.         
  65.         
  66.          //Calcula o total da datagrid
  67.         $subtotal $this->detail_list->addQuickColumn('Subtotal''={quantidade} *  {preco_venda}''right'50);
  68.    
  69.          //Converte o valor numerico em milhares
  70.         $format_value = function($value){
  71.             if  (is_numeric($value))
  72.             {
  73.                 return  'Kz '  .  number_format ($value,   2,   ' ,  ' ,  '  .  ');
  74.             }
  75.             return $value;
  76.         };
  77.         
  78.         $subtotal->setTransformer($format_value);
  79.         $preco->setTransformer($format_value);
  80.  
  81.        $subtotal->setTotalFunction(function($values){
  82.            return array_sum((array) $values);
  83.        });
  84.        
  85.        $prod-> setTransformer( function ($value)   {
  86.              return Produto :: findInTransaction 'bd_venda' ,  $value  )->pro_nome;
  87.        });
  88.         // detail actions
  89.         $this->detail_list->addQuickAction'Edit',   new TDataGridAction([$this'onEditDetail']),   'itv_cod''fa:edit blue');
  90.         $this->detail_list->addQuickAction'Delete', new TDataGridAction([$this'onDeleteDetail']), 'itv_cod''fa:trash red');
  91.         $this->detail_list->createModel();
  92.         
  93.         $panel = new TPanelGroup;
  94.         $panel->add($this->detail_list);
  95.         $panel->getBody()->style 'overflow-x:auto';
  96.         $this->form->addContent( [$panel] );
  97.         $btn $this->form->addAction_t('Save'),  new TAction([$this'onSave']), 'fa:save');
  98.         $btn->class 'btn btn-sm btn-primary';
  99.         $this->form->addAction_t('Clear'), new TAction([$this'onClear']), 'fa:eraser red');
  100.         
  101.         // create the page container
  102.         $container = new TVBox;
  103.         $container->style 'width: 100%';
  104.         // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  105.         $container->add($this->form);
  106.         parent::add($container);
  107.     }
  108.     
  109.      //METODO QUE BUSCA O PREÇO DO ITEM 
  110.     public static function onProductChange$params )
  111.     {
  112.         if(!empty($params['detail_cod_prod']))
  113.         {
  114.             try
  115.             {
  116.                 TTransaction :: open('bd_venda');
  117.                 
  118.                 $produto Produto :: find($params['detail_cod_prod']);
  119.                 $fill_data = new StdClass;
  120.                 $fill_data->detail_preco_venda  =  $produto->pro_preco;
  121.                 
  122.                 TForm :: sendData ('form_Venda'$fill_data);
  123.                 TTransaction::close();   
  124.             }
  125.             catch(Exception $e)
  126.             {
  127.                 new TMessage('error'$e->getMessage());
  128.                 TTransaction::rollback();
  129.             }
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Clear form
  135.      * @param $param URL parameters
  136.      */
  137.     public function onClear($param)
  138.     {
  139.         $this->form->clear(TRUE);
  140.         TSession::setValue(__CLASS__.'_items', array());
  141.         $this->onReload$param );
  142.     }
  143.     
  144.     /**
  145.      * Save an item from form to session list
  146.      * @param $param URL parameters
  147.      */
  148.     public function onSaveDetail$param )
  149.     {
  150.         try
  151.         {
  152.             TTransaction::open('bd_venda');
  153.             $data $this->form->getData();
  154.             
  155.             /** validation sample
  156.             if (empty($data->fieldX))
  157.             {
  158.                 throw new Exception('The field fieldX is required');
  159.             }
  160.             **/
  161.             
  162.             $items TSession::getValue(__CLASS__.'_items');
  163.             $key = empty($data->detail_itv_cod) ? 'X'.mt_rand(10000000001999999999) : $data->detail_itv_cod;
  164.             
  165.             $items$key ] = array();
  166.             $items$key ]['itv_cod'] = $key;
  167.             $items$key ]['cod_prod'] = $data->detail_cod_prod;
  168.             $items$key ]['preco_venda'] = $data->detail_preco_venda;
  169.             $items$key ]['quantidade'] = $data->detail_quantidade;
  170.           
  171.             
  172.             TSession::setValue(__CLASS__.'_items'$items);
  173.             
  174.             // clear detail form fields
  175.             $data->detail_itv_cod '';
  176.             $data->detail_cod_prod '';
  177.             $data->detail_preco_venda '';
  178.             $data->detail_quantidade '';
  179.           
  180.             
  181.             TTransaction::close();
  182.             $this->form->setData($data);
  183.             
  184.             $this->onReload$param ); // reload the items
  185.         }
  186.         catch (Exception $e)
  187.         {
  188.             $this->form->setData$this->form->getData());
  189.             new TMessage('error'$e->getMessage());
  190.         }
  191.         $this->form->clear(TRUE);  //Para Limpar os campos do formulario Mestre
  192.     }
  193.     
  194.     /**
  195.      * Load an item from session list to detail form
  196.      * @param $param URL parameters
  197.      */
  198.     public static function onEditDetail$param )
  199.     {
  200.         // read session items
  201.         $items TSession::getValue(__CLASS__.'_items');
  202.         
  203.         // get the session item
  204.         $item $items$param['key'] ];
  205.         
  206.         $data = new stdClass;
  207.         $data->detail_itv_cod $item['itv_cod'];
  208.         $data->detail_cod_prod $item['cod_prod'];
  209.         $data->detail_preco_venda $item['preco_venda'];
  210.         $data->detail_quantidade $item['quantidade'];
  211.       
  212.         
  213.         // fill detail fields
  214.         TForm::sendData'form_Venda'$data );
  215.     }
  216.     
  217.     /**
  218.      * Delete an item from session list
  219.      * @param $param URL parameters
  220.      */
  221.     public static function onDeleteDetail$param )
  222.     {
  223.         // reset items
  224.         $data = new stdClass;
  225.             $data->detail_cod_prod '';
  226.             $data->detail_preco_venda '';
  227.             $data->detail_quantidade '';
  228.             
  229.         
  230.         // clear form data
  231.         TForm::sendData('form_Venda'$data );
  232.         
  233.         // read session items
  234.         $items TSession::getValue(__CLASS__.'_items');
  235.         
  236.         // get detail id
  237.         $detail_id $param['key'];
  238.         
  239.         // delete the item from session
  240.         unset($items$detail_id ] );
  241.         
  242.         // rewrite session items
  243.         TSession::setValue(__CLASS__.'_items'$items);
  244.         
  245.         // delete item from screen
  246.         TScript::create("ttable_remove_row_by_id('Venda_list', '{$detail_id}')");
  247.     }
  248.     
  249.     /**
  250.      * Load the items list from session
  251.      * @param $param URL parameters
  252.      */
  253.     public function onReload($param)
  254.     {
  255.         // read session items
  256.         $items TSession::getValue(__CLASS__.'_items');
  257.         
  258.         $this->detail_list->clear(); // clear detail list
  259.         
  260.         if ($items)
  261.         {
  262.             foreach ($items as $list_item)
  263.             {
  264.                 $item = (object) $list_item;
  265.                 
  266.                 $row $this->detail_list->addItem$item );
  267.                 $row->id $list_item['itv_cod'];
  268.             }
  269.         }
  270.         
  271.         $this->loaded TRUE;
  272.     }
  273.     
  274.     /**
  275.      * Load Master/Detail data from database to form/session
  276.      */
  277.     public function onEdit($param)
  278.     {
  279.         try
  280.         {
  281.             TTransaction::open('bd_venda');
  282.             
  283.             if (isset($param['key']))
  284.             {
  285.                 $key $param['key'];
  286.                 
  287.                 $object = new Venda($key);
  288.                 $items  Itensvenda::where('cod_venda''='$key)->load();
  289.                 
  290.                 $session_items = array();
  291.                 foreach( $items as $item )
  292.                 {
  293.                     $item_key $item->itv_cod;
  294.                     $session_items[$item_key] = $item->toArray();
  295.                     $session_items[$item_key]['itv_cod'] = $item->itv_cod;
  296.                     $session_items[$item_key]['cod_prod'] = $item->cod_prod;
  297.                     $session_items[$item_key]['preco_venda'] = $item->preco_venda;
  298.                     $session_items[$item_key]['quantidade'] = $item->quantidade;
  299.                    // $session_items[$item_key]['desconto'] = $item->desconto;
  300.                 }
  301.                 TSession::setValue(__CLASS__.'_items'$session_items);
  302.                 
  303.                 $this->form->setData($object); // fill the form with the active record data
  304.                 $this->onReload$param ); // reload items list
  305.                 TTransaction::close(); // close transaction
  306.             }
  307.             else
  308.             {
  309.                 $this->form->clear(TRUE);
  310.                 TSession::setValue(__CLASS__.'_items'null);
  311.                 $this->onReload$param );
  312.             }
  313.         }
  314.         catch (Exception $e// in case of exception
  315.         {
  316.             new TMessage('error'$e->getMessage());
  317.             TTransaction::rollback();
  318.         }
  319.     }
  320.     
  321.     /**
  322.      * Save the Master/Detail data from form/session to database
  323.      */
  324.     public function onSave()
  325.     {
  326.     
  327.         try
  328.         {
  329.             // open a transaction with database
  330.             TTransaction::open('bd_venda');
  331.             
  332.             $data $this->form->getData();
  333.             $master = new Venda
  334.             $master->fromArray( (array) $data);
  335.             $this->form->validate(); // Valida a data do formulario
  336.             
  337.             $master->store(); // save master object
  338.             // delete details
  339.             $old_items Itensvenda::where('cod_venda''='$master->ven_cod)->load();
  340.             
  341.             $keep_items = array();
  342.             
  343.             // get session items
  344.             $items TSession::getValue(__CLASS__.'_items');
  345.             
  346.             if( $items )
  347.             {
  348.                 foreach( $items as $item )
  349.                 {
  350.                     if (substr($item['itv_cod'],0,1) == 'X' // new record
  351.                     {
  352.                         $detail = new Itensvenda;
  353.                     }
  354.                     else
  355.                     {
  356.                         $detail Itensvenda::find($item['itv_cod']);
  357.                     }
  358.                     $detail->cod_prod  $item['cod_prod'];
  359.                     $detail->preco_venda  $item['preco_venda'];
  360.                     $detail->quantidade  $item['quantidade'];
  361.                     $detail->cod_venda $master->ven_cod;
  362.                     
  363.                     // Criamos um Campo  ven_total  para guardar o total da venda
  364.                      $detail->ven_total = ($detail->quantidade * ($detail->preco_venda));
  365.                    
  366.                     $detail->store(); // Guarda os itens dos detalhes
  367.       
  368.                     $this->form->setData($data); //  Preencha a Data no Formulário
  369.                 
  370.                     $master->ven_total += $detail->ven_total;
  371.                      
  372.                     $master->venda_data date('Y-m-d H:m:s');
  373.                     
  374.                     $keep_items[] = $detail->itv_cod;
  375.                 }
  376.                       $master->store();
  377.             }
  378.             
  379.             if ($old_items)
  380.             {
  381.                 foreach ($old_items as $old_item)
  382.                 {
  383.                     if (!in_array$old_item->itv_cod$keep_items))
  384.                     {
  385.                         $old_item->delete();
  386.                     }
  387.                 }
  388.             }
  389.             
  390.              
  391.             TTransaction::close(); // close the transaction
  392.             
  393.             // reload form and session items
  394.             $this->onEdit(array('key'=>$master->ven_cod));
  395.             
  396.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved'));
  397.         }
  398.         catch (Exception $e// in case of exception
  399.         {
  400.             new TMessage('error'$e->getMessage());
  401.             $this->form->setData$this->form->getData() ); // keep form data
  402.             TTransaction::rollback();
  403.         }
  404.         $this->form->clear(TRUE);  //Para Limpar os campos do formulario Mestre
  405.     }
  406.     
  407.     
  408.     
  409.     /**
  410.      * Show the page
  411.      */
  412.     public function show()
  413.     {
  414.         // check if the datagrid is already loaded
  415.         if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  416.         {
  417.             $this->onReloadfunc_get_arg(0) );
  418.         }
  419.         parent::show();
  420.     }
  421. }

Pacotão Dominando o Adianti Framework 7
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4


Dominando o Adianti 7 Quero me inscrever agora!

Comentários (1)