Advertisement
Guest User

adianti-master/detail

a guest
Feb 18th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.63 KB | None | 0 0
  1. <?php
  2. /**
  3.  * MovimentoForm Master/Detail
  4.  * @author  <your name here>
  5.  */
  6. class MovimentoForm extends TPage
  7. {
  8.     protected $form; // form
  9.     protected $formFields;
  10.     protected $detail_list;
  11.    
  12.     /**
  13.      * Page constructor
  14.      */
  15.     public function __construct()
  16.     {
  17.         parent::__construct();
  18.        
  19.         // creates the form
  20.         $this->form = new TForm('form_Movimento');
  21.         $this->form->class = 'tform'; // CSS class
  22.         $this->form->style = 'max-width:700px'; // style
  23.         parent::include_css('app/resources/custom-frame.css');
  24.        
  25.         $table_master = new TTable;
  26.         $table_master->width = '100%';
  27.        
  28.         $table_master->addRowSet( new TLabel('Movimento'), '', '')->class = 'tformtitle';
  29.        
  30.         // add a table inside form
  31.         $table_general = new TTable;
  32.         $table_detail  = new TTable;
  33.         $table_general-> width = '100%';
  34.         $table_detail-> width  = '100%';
  35.        
  36.         $frame_general = new TFrame;
  37.         $frame_general->setLegend('Movimento');
  38.         $frame_general->style = 'background:whiteSmoke';
  39.         $frame_general->add($table_general);
  40.        
  41.         $table_master->addRow()->addCell( $frame_general )->colspan=2;
  42.         $row = $table_master->addRow();
  43.         $row->addCell( $table_detail );
  44.        
  45.         $this->form->add($table_master);
  46.        
  47.         // master fields
  48.         $id = new TEntry('id');
  49.         $dt_movimento = new TDate('dt_movimento');
  50.         $dt_processamento = new TDate('dt_processamento');
  51.         $dt_saida = new TDate('dt_saida');
  52.         $nro_pedido = new TEntry('nro_pedido');
  53.         $nro_nota = new TEntry('nro_nota');
  54.         $val_itens = new TEntry('val_itens');
  55.         $val_desc_cortesia = new TEntry('val_desc_cortesia');
  56.         $val_movimento = new TEntry('val_movimento');
  57.         $pessoa_id = new TEntry('pessoa_id');
  58.         $producao_id = new TEntry('producao_id');
  59.        
  60.         if (!empty($id))
  61.         {
  62.             $id->setEditable(FALSE);
  63.         }
  64.        
  65.         // detail fields
  66.         $detail_id = new THidden('detail_id');
  67.         $detail_produto_id = new TEntry('detail_produto_id');
  68.         $detail_unidade = new TEntry('detail_unidade');
  69.         $detail_val_unitario = new TEntry('detail_val_unitario');
  70.         $detail_quantidade = new TEntry('detail_quantidade');
  71.         $detail_val_total = new TEntry('detail_val_total');
  72.  
  73.         /** samples
  74.          $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  75.          $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  76.          $fieldX->setSize( 100, 40 ); // set size
  77.          **/
  78.        
  79.         // master
  80.         $table_general->addRowSet( new TLabel('Id'), $id );
  81.         $table_general->addRowSet( new TLabel('Dt Movimento'), $dt_movimento );
  82.         $table_general->addRowSet( new TLabel('Dt Processamento'), $dt_processamento );
  83.         $table_general->addRowSet( new TLabel('Dt Saida'), $dt_saida );
  84.         $table_general->addRowSet( new TLabel('Nro Pedido'), $nro_pedido );
  85.         $table_general->addRowSet( new TLabel('Nro Nota'), $nro_nota );
  86.         $table_general->addRowSet( new TLabel('Val Itens'), $val_itens );
  87.         $table_general->addRowSet( new TLabel('Val Desc Cortesia'), $val_desc_cortesia );
  88.         $table_general->addRowSet( new TLabel('Val Movimento'), $val_movimento );
  89.         $table_general->addRowSet( new TLabel('Pessoa Id'), $pessoa_id );
  90.         $table_general->addRowSet( new TLabel('Producao Id'), $producao_id );
  91.        
  92.          // detail
  93.         $frame_details = new TFrame();
  94.         $frame_details->setLegend('Item');
  95.         $row = $table_detail->addRow();
  96.         $row->addCell($frame_details);
  97.        
  98.         $btn_save_detail = new TButton('btn_save_detail');
  99.         $btn_save_detail->setAction(new TAction(array($this, 'onSaveDetail')), 'Register');
  100.         $btn_save_detail->setImage('fa:save');
  101.        
  102.         $table_details = new TTable;
  103.         $frame_details->add($table_details);
  104.        
  105.         $table_details->addRowSet( '', $detail_id );
  106.         $table_details->addRowSet( new TLabel('Produto Id'), $detail_produto_id );
  107.         $table_details->addRowSet( new TLabel('Unidade'), $detail_unidade );
  108.         $table_details->addRowSet( new TLabel('Val Unitario'), $detail_val_unitario );
  109.         $table_details->addRowSet( new TLabel('Quantidade'), $detail_quantidade );
  110.         $table_details->addRowSet( new TLabel('Val Total'), $detail_val_total );
  111.        
  112.         $table_details->addRowSet( $btn_save_detail );
  113.        
  114.         $this->detail_list = new TQuickGrid;
  115.         $this->detail_list->setHeight( 175 );
  116.         $this->detail_list->makeScrollable();
  117.         $this->detail_list->disableDefaultClick();
  118.         $this->detail_list->addQuickColumn('', 'edit', 'left', 50);
  119.         $this->detail_list->addQuickColumn('', 'delete', 'left', 50);
  120.        
  121.         // items
  122.         $this->detail_list->addQuickColumn('Produto Id', 'produto_id', 'left', 100);
  123.         $this->detail_list->addQuickColumn('Unidade', 'unidade', 'left', 200);
  124.         $this->detail_list->addQuickColumn('Val Unitario', 'val_unitario', 'left', 200);
  125.         $this->detail_list->addQuickColumn('Quantidade', 'quantidade', 'left', 200);
  126.         $this->detail_list->addQuickColumn('Val Total', 'val_total', 'left', 200);
  127.         $this->detail_list->createModel();
  128.        
  129.         $row = $table_detail->addRow();
  130.         $row->addCell($this->detail_list);
  131.        
  132.         // create an action button (save)
  133.         $save_button=new TButton('save');
  134.         $save_button->setAction(new TAction(array($this, 'onSave')), _t('Save'));
  135.         $save_button->setImage('ico_save.png');
  136.  
  137.         // create an new button (edit with no parameters)
  138.         $new_button=new TButton('new');
  139.         $new_button->setAction(new TAction(array($this, 'onClear')), _t('New'));
  140.         $new_button->setImage('ico_new.png');
  141.        
  142.         // define form fields
  143.         $this->formFields   = array($id,$dt_movimento,$dt_processamento,$dt_saida,$nro_pedido,$nro_nota,$val_itens,$val_desc_cortesia,$val_movimento,$pessoa_id,$producao_id,$detail_produto_id,$detail_unidade,$detail_val_unitario,$detail_quantidade,$detail_val_total);
  144.         $this->formFields[] = $btn_save_detail;
  145.         $this->formFields[] = $save_button;
  146.         $this->formFields[] = $new_button;
  147.         $this->formFields[] = $detail_id;
  148.         $this->form->setFields( $this->formFields );
  149.        
  150.         $table_master->addRowSet( array($save_button, $new_button), '', '')->class = 'tformaction'; // CSS class
  151.        
  152.         // create the page container
  153.         $container = new TVBox;
  154.         $container->style = 'width: 90%';
  155.         // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  156.         $container->add($this->form);
  157.         parent::add($container);
  158.     }
  159.    
  160.    
  161.     /**
  162.      * Clear form
  163.      * @param $param URL parameters
  164.      */
  165.     public function onClear($param)
  166.     {
  167.         $this->form->clear();
  168.         TSession::setValue(__CLASS__.'_items', array());
  169.         $this->onReload( $param );
  170.     }
  171.    
  172.     /**
  173.      * Save an item from form to session list
  174.      * @param $param URL parameters
  175.      */
  176.     public function onSaveDetail( $param )
  177.     {
  178.         try
  179.         {
  180.             TTransaction::open('dash');
  181.             $data = $this->form->getData();
  182.            
  183.             /** validation sample
  184.             if (! $data->fieldX)
  185.                 throw new Exception('The field fieldX is required');
  186.             **/
  187.            
  188.             $items = TSession::getValue(__CLASS__.'_items');
  189.             $key = empty($data->detail_id) ? 'X'.mt_rand(1000000000, 1999999999) : $data->detail_id;
  190.            
  191.             $items[ $key ] = array();
  192.             $items[ $key ]['id'] = $key;
  193.             $items[ $key ]['produto_id'] = $data->detail_produto_id;
  194.             $items[ $key ]['unidade'] = $data->detail_unidade;
  195.             $items[ $key ]['val_unitario'] = $data->detail_val_unitario;
  196.             $items[ $key ]['quantidade'] = $data->detail_quantidade;
  197.             $items[ $key ]['val_total'] = $data->detail_val_total;
  198.            
  199.             TSession::setValue(__CLASS__.'_items', $items);
  200.            
  201.             // clear detail form fields
  202.             $data->detail_id = '';
  203.             $data->detail_produto_id = '';
  204.             $data->detail_unidade = '';
  205.             $data->detail_val_unitario = '';
  206.             $data->detail_quantidade = '';
  207.             $data->detail_val_total = '';
  208.            
  209.             TTransaction::close();
  210.             $this->form->setData($data);
  211.            
  212.             $this->onReload( $param ); // reload the items
  213.         }
  214.         catch (Exception $e)
  215.         {
  216.             $this->form->setData( $this->form->getData());
  217.             new TMessage('error', $e->getMessage());
  218.         }
  219.     }
  220.    
  221.     /**
  222.      * Load an item from session list to detail form
  223.      * @param $param URL parameters
  224.      */
  225.     public function onEditDetail( $param )
  226.     {
  227.         $data = $this->form->getData();
  228.        
  229.         // read session items
  230.         $items = TSession::getValue(__CLASS__.'_items');
  231.        
  232.         // get the session item
  233.         $item = $items[ $param['item_key'] ];
  234.        
  235.         $data->detail_id = $item['id'];
  236.         $data->detail_produto_id = $item['produto_id'];
  237.         $data->detail_unidade = $item['unidade'];
  238.         $data->detail_val_unitario = $item['val_unitario'];
  239.         $data->detail_quantidade = $item['quantidade'];
  240.         $data->detail_val_total = $item['val_total'];
  241.        
  242.         // fill detail fields
  243.         $this->form->setData( $data );
  244.    
  245.         $this->onReload( $param );
  246.     }
  247.    
  248.     /**
  249.      * Delete an item from session list
  250.      * @param $param URL parameters
  251.      */
  252.     public function onDeleteDetail( $param )
  253.     {
  254.         $data = $this->form->getData();
  255.        
  256.         // reset items
  257.             $data->detail_produto_id = '';
  258.             $data->detail_unidade = '';
  259.             $data->detail_val_unitario = '';
  260.             $data->detail_quantidade = '';
  261.             $data->detail_val_total = '';
  262.        
  263.         // clear form data
  264.         $this->form->setData( $data );
  265.        
  266.         // read session items
  267.         $items = TSession::getValue(__CLASS__.'_items');
  268.        
  269.         // delete the item from session
  270.         unset($items[ $param['item_key'] ] );
  271.         TSession::setValue(__CLASS__.'_items', $items);
  272.        
  273.         // reload items
  274.         $this->onReload( $param );
  275.     }
  276.    
  277.     /**
  278.      * Load the items list from session
  279.      * @param $param URL parameters
  280.      */
  281.     public function onReload($param)
  282.     {
  283.         // read session items
  284.         $items = TSession::getValue(__CLASS__.'_items');
  285.        
  286.         $this->detail_list->clear(); // clear detail list
  287.         $data = $this->form->getData();
  288.        
  289.         if ($items)
  290.         {
  291.             $cont = 1;
  292.             foreach ($items as $list_item_key => $list_item)
  293.             {
  294.                 $item_name = 'prod_' . $cont++;
  295.                 $item = new StdClass;
  296.                
  297.                 // create action buttons
  298.                 $action_del = new TAction(array($this, 'onDeleteDetail'));
  299.                 $action_del->setParameter('item_key', $list_item_key);
  300.                
  301.                 $action_edi = new TAction(array($this, 'onEditDetail'));
  302.                 $action_edi->setParameter('item_key', $list_item_key);
  303.                
  304.                 $button_del = new TButton('delete_detail'.$cont);
  305.                 $button_del->class = 'btn btn-default btn-sm';
  306.                 $button_del->setAction( $action_del, '' );
  307.                 $button_del->setImage('fa:trash-o red fa-lg');
  308.                
  309.                 $button_edi = new TButton('edit_detail'.$cont);
  310.                 $button_edi->class = 'btn btn-default btn-sm';
  311.                 $button_edi->setAction( $action_edi, '' );
  312.                 $button_edi->setImage('fa:edit blue fa-lg');
  313.                
  314.                 $item->edit   = $button_edi;
  315.                 $item->delete = $button_del;
  316.                
  317.                 $this->formFields[ $item_name.'_edit' ] = $item->edit;
  318.                 $this->formFields[ $item_name.'_delete' ] = $item->delete;
  319.                
  320.                 // items
  321.                 $item->id = $list_item['id'];
  322.                 $item->produto_id = $list_item['produto_id'];
  323.                 $item->unidade = $list_item['unidade'];
  324.                 $item->val_unitario = $list_item['val_unitario'];
  325.                 $item->quantidade = $list_item['quantidade'];
  326.                 $item->val_total = $list_item['val_total'];
  327.                
  328.                 $row = $this->detail_list->addItem( $item );
  329.                 $row->onmouseover='';
  330.                 $row->onmouseout='';
  331.             }
  332.  
  333.             $this->form->setFields( $this->formFields );
  334.         }
  335.        
  336.         $this->loaded = TRUE;
  337.     }
  338.    
  339.     /**
  340.      * Load Master/Detail data from database to form/session
  341.      */
  342.     public function onEdit($param)
  343.     {
  344.         try
  345.         {
  346.             TTransaction::open('dash');
  347.            
  348.             if (isset($param['key']))
  349.             {
  350.                 $key = $param['key'];
  351.                
  352.                 $object = new Movimento($key);
  353.                 $items  = Item::where('movimento_id', '=', $key)->load();
  354.                
  355.                 $session_items = array();
  356.                 foreach( $items as $item )
  357.                 {
  358.                     $item_key = $item->id;
  359.                     $session_items[$item_key] = $item->toArray();
  360.                     $session_items[$item_key]['id'] = $item->id;
  361.                     $session_items[$item_key]['produto_id'] = $item->produto_id;
  362.                     $session_items[$item_key]['unidade'] = $item->unidade;
  363.                     $session_items[$item_key]['val_unitario'] = $item->val_unitario;
  364.                     $session_items[$item_key]['quantidade'] = $item->quantidade;
  365.                     $session_items[$item_key]['val_total'] = $item->val_total;
  366.                 }
  367.                 TSession::setValue(__CLASS__.'_items', $session_items);
  368.                
  369.                 $this->form->setData($object); // fill the form with the active record data
  370.                 $this->onReload( $param ); // reload items list
  371.                 TTransaction::close(); // close transaction
  372.             }
  373.             else
  374.             {
  375.                 $this->form->clear();
  376.                 TSession::setValue(__CLASS__.'_items', null);
  377.                 $this->onReload( $param );
  378.             }
  379.         }
  380.         catch (Exception $e) // in case of exception
  381.         {
  382.             new TMessage('error', $e->getMessage());
  383.             TTransaction::rollback();
  384.         }
  385.     }
  386.    
  387.     /**
  388.      * Save the Master/Detail data from form/session to database
  389.      */
  390.     public function onSave()
  391.     {
  392.         try
  393.         {
  394.             // open a transaction with database
  395.             TTransaction::open('dash');
  396.            
  397.             $data = $this->form->getData();
  398.             $master = new Movimento;
  399.             $master->fromArray( (array) $data);
  400.             $this->form->validate(); // form validation
  401.            
  402.             $master->store(); // save master object
  403.             // delete details
  404.             $old_items = Item::where('movimento_id', '=', $master->id)->load();
  405.            
  406.             $keep_items = array();
  407.            
  408.             // get session items
  409.             $items = TSession::getValue(__CLASS__.'_items');
  410.            
  411.             if( $items )
  412.             {
  413.                 foreach( $items as $item )
  414.                 {
  415.                     if (substr($item['id'],0,1) == 'X' ) // new record
  416.                     {
  417.                         $detail = new Item;
  418.                     }
  419.                     else
  420.                     {
  421.                         $detail = Item::find($item['id']);
  422.                     }
  423.                     $detail->produto_id  = $item['produto_id'];
  424.                     $detail->unidade  = $item['unidade'];
  425.                     $detail->val_unitario  = $item['val_unitario'];
  426.                     $detail->quantidade  = $item['quantidade'];
  427.                     $detail->val_total  = $item['val_total'];
  428.                     $detail->movimento_id = $master->id;
  429.                     $detail->store();
  430.                    
  431.                     $keep_items[] = $detail->id;
  432.                 }
  433.             }
  434.            
  435.             if ($old_items)
  436.             {
  437.                 foreach ($old_items as $old_item)
  438.                 {
  439.                     if (!in_array( $old_item->id, $keep_items))
  440.                     {
  441.                         $old_item->delete();
  442.                     }
  443.                 }
  444.             }
  445.             TTransaction::close(); // close the transaction
  446.            
  447.             // reload form and session items
  448.             $this->onEdit(array('key'=>$master->id));
  449.            
  450.             new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  451.         }
  452.         catch (Exception $e) // in case of exception
  453.         {
  454.             new TMessage('error', $e->getMessage());
  455.             $this->form->setData( $this->form->getData() ); // keep form data
  456.             TTransaction::rollback();
  457.         }
  458.     }
  459.    
  460.     /**
  461.      * Show the page
  462.      */
  463.     public function show()
  464.     {
  465.         // check if the datagrid is already loaded
  466.         if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  467.         {
  468.             $this->onReload( func_get_arg(0) );
  469.         }
  470.         parent::show();
  471.     }
  472. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement