Advertisement
Gepoko

Daganzo

Apr 21st, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.18 KB | None | 0 0
  1. //Copy this code tomorrow
  2.  
  3. //Daganzo controller
  4.  
  5. public function actionIndex()
  6.     {
  7.        
  8.         $searchModel = new DaganzoBusqueda();
  9.         $searchModelo = $searchModel->buscarAnio(Yii::$app->request->queryParams);
  10.         $dataProvider = $searchModel->busca(Yii::$app->request->queryParams);
  11.         $miObjeto = $searchModel->buscarTodoAgrupado($dataProvider);
  12.         return $this->render('index', [
  13.                 'searchModel' => $searchModel,
  14.                 'searchModelo' => $searchModelo,
  15.                 'dataProvider' => $miObjeto,
  16.         ]);
  17.     }
  18.  
  19. //Daganzo Busqueda
  20.  
  21. public function busca($params)
  22.     {
  23.         $query = Daganzo::findBySql("select * from daganzo");
  24.    
  25.         $dataProvider = new ActiveDataProvider([
  26.                 'query' => $query,
  27.         ]);
  28.    
  29.         $this->load($params);
  30.    
  31.         if (!$this->validate()) {
  32.             // uncomment the following line if you do not want to any records when validation fails
  33.             // $query->where('0=1');
  34.             return $dataProvider;
  35.         }
  36.    
  37.         $query->andFilterWhere([
  38.                 //'id_cuenta' => $this->id_cuenta,
  39.                 'ano' => $this->ano,
  40.                 'mes' => $this->mes,
  41.                 'id_funcional' => $this->id_funcional,
  42.                 'id_economica' => $this->id_economica,
  43.                 'pre_inicial' => $this->pre_inicial,
  44.                 'pre_modificacion' => $this->pre_modificacion,
  45.                 'pre_final' => $this->pre_final,
  46.                 'gas_comprometidos' => $this->gas_comprometidos,
  47.                 'gas_reconocidos' => $this->gas_reconocidos,
  48.                 'pag_realizados' => $this->pag_realizados,
  49.                 'pag_pendientes' => $this->pag_pendientes,
  50.                 'sal_remanente' => $this->sal_remanente,
  51.         ]);
  52.    
  53.         $query->andFilterWhere(['like', 'trimestre', $this->trimestre])
  54.         ->andFilterWhere(['like', 'des_funcional', $this->des_funcional])
  55.         ->andFilterWhere(['like', 'des_economica', $this->des_economica]);
  56.    
  57.         return $dataProvider;
  58.     }
  59.    
  60.     public function buscarAnio($params)
  61.     {
  62.         $query = Daganzo::findBySql('select distinct ano
  63.                 from daganzo group by ano');
  64.    
  65.         $dataProvider = new ActiveDataProvider([
  66.                 'query' => $query,
  67.         ]);
  68.    
  69.         $this->load($params);
  70.    
  71.         if (!$this->validate()) {
  72.             // uncomment the following line if you do not want to any records when validation fails
  73.             // $query->where('0=1');
  74.             return $dataProvider;
  75.         }
  76.    
  77.         $query->andFilterWhere([
  78.                 //'id_cuenta' => $this->id_cuenta,
  79.                 'ano' => $this->ano,
  80.                 'mes' => $this->mes,
  81.                 'id_funcional' => $this->id_funcional,
  82.                 'id_economica' => $this->id_economica,
  83.                 'pre_inicial' => $this->pre_inicial,
  84.                 'pre_modificacion' => $this->pre_modificacion,
  85.                 'pre_final' => $this->pre_final,
  86.                 'gas_comprometidos' => $this->gas_comprometidos,
  87.                 'gas_reconocidos' => $this->gas_reconocidos,
  88.                 'pag_realizados' => $this->pag_realizados,
  89.                 'pag_pendientes' => $this->pag_pendientes,
  90.                 'sal_remanente' => $this->sal_remanente,
  91.         ]);
  92.    
  93.         $query->andFilterWhere(['like', 'trimestre', $this->trimestre])
  94.         ->andFilterWhere(['like', 'des_funcional', $this->des_funcional])
  95.         ->andFilterWhere(['like', 'des_economica', $this->des_economica]);
  96.    
  97.         return $dataProvider;
  98.     }
  99.    
  100.     /**
  101.      * Crea un dataProvider con los distintos registros del a;o 2013 agrupados por id_funcional
  102.      * @param array $params
  103.      * @return ActiveDataProvider
  104.      */
  105.     public function buscarTodoAgrupado($dataProvider)
  106.     {
  107.         $datos=$dataProvider->getModels();
  108.  
  109.         foreach($datos as $row){
  110.            
  111.         $id_funcional = $row['id_funcional'];
  112.         $ano = $row['ano'];
  113.         $id = $row['id_cuenta'];
  114.        
  115.         }
  116.        
  117.         $query = Daganzo::findBySql("select id_funcional, ano, id_cuenta, des_funcional, trimestre, sum(pre_final) as pre_final,
  118.                 sum(gas_comprometidos) as gas_comprometidos, sum(pag_realizados) as pag_realizados
  119.                 from daganzo where ano = $ano group by id_funcional");
  120.        
  121.  
  122.         $dataProvider = new ActiveDataProvider([
  123.             'query' => $query,
  124.                 'pagination' => array('pageSize' => 20),
  125.                
  126.         ]);
  127.  
  128.         $this->load($id);
  129.  
  130.         if (!$this->validate()) {
  131.             // uncomment the following line if you do not want to any records when validation fails
  132.             // $query->where('0=1');
  133.             return $dataProvider;
  134.         }
  135.  
  136.         $query->andFilterWhere([
  137.             //'id_cuenta' => $this->id_cuenta,
  138.             'ano' => $this->ano,
  139.             'mes' => $this->mes,
  140.             'id_funcional' => $this->id_funcional,
  141.             'id_economica' => $this->id_economica,
  142.             'pre_inicial' => $this->pre_inicial,
  143.             'pre_modificacion' => $this->pre_modificacion,
  144.             'pre_final' => $this->pre_final,
  145.             'gas_comprometidos' => $this->gas_comprometidos,
  146.             'gas_reconocidos' => $this->gas_reconocidos,
  147.             'pag_realizados' => $this->pag_realizados,
  148.             'pag_pendientes' => $this->pag_pendientes,
  149.             'sal_remanente' => $this->sal_remanente,
  150.         ]);
  151.  
  152.         $query->andFilterWhere(['like', 'trimestre', $this->trimestre])
  153.             ->andFilterWhere(['like', 'des_funcional', $this->des_funcional])
  154.             ->andFilterWhere(['like', 'des_economica', $this->des_economica]);
  155.  
  156.         return $dataProvider;
  157.     }
  158.    
  159.     /**
  160.      * Metodo que recibe un id_cuenta y nos devuelve un dataProvider con el id_funcional que queremos encontrar
  161.      * @param string $id
  162.      * @return ActiveDataProvider
  163.      */
  164.     public function buscarIdFuncional($id)
  165.     {
  166.         $idCuenta = intval($id);
  167.         $query = Daganzo::findBySql("select id_funcional, ano from daganzo where id_cuenta = $idCuenta");
  168.         $dataProvider = new ActiveDataProvider([
  169.                 'query' => $query,
  170.            
  171.         ]);
  172.        
  173.         $this->load($id);
  174.    
  175.         if (!$this->validate()) {
  176.             // uncomment the following line if you do not want to any records when validation fails
  177.             // $query->where('0=1');
  178.             return $dataProvider;
  179.         }
  180.    
  181.         $query->andFilterWhere([
  182.                 //'id_cuenta' => $this->id_cuenta,
  183.                 'ano' => $this->ano,
  184.                 'mes' => $this->mes,
  185.                 'id_funcional' => $this->id_funcional,
  186.                 'id_economica' => $this->id_economica,
  187.                 'pre_inicial' => $this->pre_inicial,
  188.                 'pre_modificacion' => $this->pre_modificacion,
  189.                 'pre_final' => $this->pre_final,
  190.                 'gas_comprometidos' => $this->gas_comprometidos,
  191.                 'gas_reconocidos' => $this->gas_reconocidos,
  192.                 'pag_realizados' => $this->pag_realizados,
  193.                 'pag_pendientes' => $this->pag_pendientes,
  194.                 'sal_remanente' => $this->sal_remanente,
  195.         ]);
  196.    
  197.         $query->andFilterWhere(['like', 'trimestre', $this->trimestre])
  198.         ->andFilterWhere(['like', 'des_funcional', $this->des_funcional])
  199.         ->andFilterWhere(['like', 'des_economica', $this->des_economica]);
  200.    
  201.         return $dataProvider;
  202.     }
  203.    
  204.     /**
  205.      * Metodo que recibe un ActiveDataProvider y un string que devuelve los registros cuyo id_funcional
  206.      * es el que hemos seleccionado en el metodo buscarIdFuncional
  207.      * @param ActiveDataProvider $dataProvider
  208.      * @param string $id
  209.      * @return ActiveDataProvider
  210.      */
  211.     public function devuelveFilasIdFuncional($dataProvider, $id){
  212.        
  213.         $datos=$dataProvider->getModels();
  214.        
  215.         foreach($datos as $row){
  216.            
  217.         $id_funcional = $row['id_funcional'];
  218.         $ano = $row['ano'];
  219.        
  220.         }
  221.        
  222.         $query = Daganzo::findBySql("select id_funcional, id_cuenta, des_funcional, trimestre, pre_final,
  223.             gas_comprometidos, pag_realizados, ano from daganzo where ano = $ano AND id_funcional = $id_funcional");
  224.        
  225.         $dataProvider = new ActiveDataProvider([
  226.                 'query' => $query,
  227.                 //Eliminamos la paginacion existente para mejorar la salida de los datos
  228.                 'pagination' => array('pageSize' => 0),
  229.        
  230.         ]);
  231.        
  232.         $this->load($id);
  233.        
  234.         if (!$this->validate()) {
  235.             // uncomment the following line if you do not want to any records when validation fails
  236.             // $query->where('0=1');
  237.             return $dataProvider;
  238.         }
  239.        
  240.         $query->andFilterWhere([
  241.                 //'id_cuenta' => $this->id_cuenta,
  242.                 'ano' => $this->ano,
  243.                 'mes' => $this->mes,
  244.                 'id_funcional' => $this->id_funcional,
  245.                 'id_economica' => $this->id_economica,
  246.                 'pre_inicial' => $this->pre_inicial,
  247.                 'pre_modificacion' => $this->pre_modificacion,
  248.                 'pre_final' => $this->pre_final,
  249.                 'gas_comprometidos' => $this->gas_comprometidos,
  250.                 'gas_reconocidos' => $this->gas_reconocidos,
  251.                 'pag_realizados' => $this->pag_realizados,
  252.                 'pag_pendientes' => $this->pag_pendientes,
  253.                 'sal_remanente' => $this->sal_remanente,
  254.         ]);
  255.        
  256.         $query->andFilterWhere(['like', 'trimestre', $this->trimestre])
  257.         ->andFilterWhere(['like', 'des_funcional', $this->des_funcional])
  258.         ->andFilterWhere(['like', 'des_economica', $this->des_economica]);
  259.        
  260.  
  261.         return $dataProvider;
  262.          
  263.     }
  264.  
  265. //Index of Daganzo
  266.  
  267. <?php
  268. //namespace kartik\grid;
  269. //use yii\grid\GridView;
  270. use yii\helpers\Html;
  271. use app\models\Daganzo;
  272. use app\models\DaganzoBusqueda;
  273. use kartik\grid\GridView;
  274. use kartik\grid\DataColumn;
  275. use yii\bootstrap\Collapse;
  276. use kartik\widgets\Select2;
  277. use yii\helpers\ArrayHelper;
  278. use yii\data\ActiveDataProvider;
  279. /* @var $this yii\web\View */
  280. /* @var $searchModel app\models\DaganzoBusqueda */
  281. /* @var $dataProvider yii\data\ActiveDataProvider */
  282.  
  283. $this->title = 'Daganzos Index';
  284. $this->params['breadcrumbs'][] = $this->title;
  285. ?>
  286. <div class="daganzo-index">
  287.  
  288.     <h1><?= Html::encode($this->title) ?></h1>
  289.     <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
  290.  
  291.  
  292.    
  293.     <?php
  294.    
  295.     $data = ArrayHelper::map(Daganzo::find()->groupBy('ano')->asArray()->all(),'ano','ano');
  296.    
  297.  
  298.     $miObjeto = $searchModel->buscarTodoAgrupado($dataProvider);
  299.    
  300.     $datos=$miObjeto->getModels();
  301.    
  302.     foreach($datos as $row){
  303.    
  304.         $id_funcional = $row['id_funcional'];
  305.         $ano = $row['ano'];
  306.         $id = $row['id_cuenta'];
  307.          
  308.     }
  309.     //var_dump($ano);
  310.  
  311.      
  312.     if(isset($_GET['ano'])){
  313.     $a =htmlspecialchars($_GET["ano"]);
  314.         if($a == '2012'){
  315.    
  316.             $ano = $a;
  317.         }
  318.         else{
  319.             if($a == '2013'){
  320.                  
  321.                 $ano ='2013';
  322.             }
  323.             else{if($a=='2011'){
  324.    
  325.                 $ano = '2011';
  326.             }}
  327.              
  328.         }
  329.    
  330.     }
  331.    
  332.    
  333.     $query = Daganzo::findBySql("select id_funcional, ano, id_cuenta, des_funcional, trimestre, sum(pre_final) as pre_final,
  334.                 sum(gas_comprometidos) as gas_comprometidos, sum(pag_realizados) as pag_realizados
  335.                 from daganzo where ano = $ano group by id_funcional");
  336.    
  337.     $dataProvider = new ActiveDataProvider([
  338.             'query' => $query,
  339.     ]);
  340.    
  341.     $select =Select2::widget([
  342.             'name' => 'ano',
  343.             'model' => $searchModelo,
  344.             'attribute' => 'ano',
  345.             'data' =>  $data,
  346.             'options' => ['placeholder' => 'Selleccione año ...', 'id' =>'id_ano'],
  347.             'pluginOptions' => [
  348.                     'allowClear' => true
  349.             ],
  350.     ]);
  351.    
  352.      echo GridView::widget([
  353.             'dataProvider'=> $dataProvider,
  354.             'filterModel' => $searchModelo,
  355.            
  356.             'columns' => [
  357.             ['class' => 'yii\grid\SerialColumn'],
  358.                 [
  359.                      'class' => 'kartik\grid\ExpandRowColumn',
  360.                      'value' => function($model, $key, $index, $column){
  361.                             return GridView::ROW_COLLAPSED;
  362.                                 },
  363.                      'detail' => function($model, $key, $index, $column){
  364.                                    
  365.                         /**
  366.                          * Pasos para avanzar en una sql hasta poder llegar a pasarle el dataProvider
  367.                          * y el id_funcional necesarios para que la consulta devuelva:
  368.                          * Los campos por id funcional y que sean del año 2013
  369.                          */
  370.                         $searchModel = new DaganzoBusqueda();
  371.                         //$dataProvider = $searchModel->buscarTodoAgrupado(Yii::$app->request->queryParams);
  372.                         $dataProvider = $searchModel->buscarIdFuncional($key);
  373.                         $dataProvider = $searchModel->devuelveFilasIdFuncional($dataProvider, $key);
  374.                         #Accedemos al metodo devuelve totales
  375.                         #$miObjeto = $searchModel->devuelveTotales($dataProvider, $key);
  376.                         /**
  377.                          * return que devuelve una vista que no es necesario que este definida en DaganzoController
  378.                          * le pasamos el dataProvider y el searchModel
  379.                          */
  380.                         return Yii::$app->controller->renderPartial('vista',   
  381.                                 ['searchModel' => $searchModel,
  382.                                  'dataProvider' => $dataProvider,
  383.                                 ]);
  384.                                 }
  385.                 ],
  386.             //'id_funcional',
  387.             'des_funcional',
  388.             // 'id_economica',
  389.             // 'des_economica',
  390.             [
  391.                 'attribute' => 'ano',
  392.                 'value' => 'ano',
  393.                 'format' => 'raw',
  394.                 'filter' => $select
  395.                    
  396.             ],
  397.             #descomentamos las filas a mostrar
  398.            //'pre_inicial',
  399.             //'pre_modificacion',
  400.             'pre_final',
  401.             'gas_comprometidos',
  402.             //'gas_reconocidos',
  403.             //'pag_realizados',
  404.             //'pag_pendientes',
  405.             //'sal_remanente',
  406.             ],
  407.             //Atributos del GridView
  408.             'bordered' => true,
  409.             'striped' => true,
  410.             'condensed' => true,
  411.             'responsive' => true,
  412.             'hover' => true,
  413.             'showPageSummary' => false,
  414.             //Panel que hace referencia al "titulo" del gridview y a su footer
  415.             'panel' => [
  416.                      'heading'=>'<h1 class="panel-title"><i class="glyphicon glyphicon-globe"></i> Daganzo Index</h1>',
  417.                         'type'=>'info',
  418.                         'before'=>Html::a('<i class="glyphicon glyphicon-plus"></i> Crear Nuevo registro',
  419.                                  ['create'], ['class' => 'btn btn-success']),
  420.                         'after'=>Html::a('<i class="glyphicon glyphicon-repeat"></i> Reiniciar Tabla',
  421.                                 ['index'], ['class' => 'btn btn-info']),
  422.                         'footer'=>false
  423.             ],
  424.             'persistResize' => false,
  425.             'exportConfig' => true,
  426.     ]);
  427.      ?>
  428. </div>
  429.  
  430. <?php
  431. $script = <<< JS
  432.        
  433. $('#id_ano').change(function(){
  434.  
  435.         var idano = $(this).val();
  436.        
  437.  
  438.        
  439. });
  440. JS;
  441. $this->registerJs($script);
  442. ?>
  443.  
  444. //All here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement