Exemple #1
0
 private function _parsePercentage(&$config, $where)
 {
     if ($config['percentage-total']) {
         $cmdSelect = 'SELECT SUM(' . $config['expression'] . ') total ';
         $cmdFrom = ' FROM ' . $this->_getSqlBase();
         /**
          * Trata a entrada do Where para ser um Grupo de Where 
          */
         if ($where instanceof ZendT_Db_Where_Group) {
             $whereGroup = $where;
         } else {
             if ($where instanceof ZendT_Db_Where) {
                 $whereGroup = new ZendT_Db_Where_Group();
                 $whereGroup->addWhere($where);
             } else {
                 $whereGroup = new ZendT_Db_Where_Group();
             }
         }
         /**
          * Avalia se existe algum Where específico do MapperView
          * colocando o mesmo dentro do objeto que agrupa os wheres
          */
         $_whereMapperView = $this->_getWhere($postData, $where);
         if ($_whereMapperView) {
             $whereGroup->addWhere($_whereMapperView);
         }
         /**
          * Monta o comando Where
          */
         $binds = $whereGroup->getBinds();
         $cmdWhere = "  WHERE " . $whereGroup->getSqlWhere();
         $sql = $cmdSelect . $cmdFrom . $cmdWhere;
         $this->_prepareSql($sql, $binds, 'full');
         $stmt = $this->getModel()->getAdapter()->query($sql, $binds);
         $mappers = array();
         $mappers['total']['mapper'] = new ZendT_Type_Number(null, array('numDecimal' => 0));
         $mappers['total']['column'] = 'total';
         $mappers['total']['operation'] = '=';
         $mappers['total']['expression'] = '';
         $data = new ZendT_Grid_Data($stmt, $mappers, true);
         $row = $data->getRow();
         $total = $row['total']->toPhp();
         $columnFat = $config['columns'][0][0];
         $config['column-fat'] = str_replace(array('{', '}'), '', $config['columns'][0][0]);
         if ($columnFat == '') {
             throw new ZendT_Exception_Alert('Formula de porcentagem não contém a coluna fator para divisão!');
         }
         $config['expression_original'] = 'CASE WHEN ' . $columnFat . ' IS NULL OR ' . $columnFat . ' = 0 THEN 0 ELSE (' . $columnFat . '/' . $total . ')*100 END';
         $config['expression'] = $config['expression_original'];
         return true;
     } else {
         if ($config['percentage-columns']) {
             $columnFat = $config['columns'][0][0];
             $columnDiv = $config['columns'][0][1];
             $config['column-fat'] = str_replace(array('{', '}'), '', $config['columns'][0][0]);
             $config['column-div'] = str_replace(array('{', '}'), '', $config['columns'][0][1]);
             if ($columnFat == '') {
                 throw new ZendT_Exception_Alert('Formula de porcentagem não contém a coluna fator para divisão!');
             }
             if ($columnDiv == '') {
                 throw new ZendT_Exception_Alert('Formula de porcentagem não contém a coluna para divisão!');
             }
             $config['expression_original'] = 'CASE WHEN ' . $columnFat . ' IS NULL OR ' . $columnFat . ' = 0 THEN 0 ELSE (' . $columnFat . '/' . $columnDiv . ')*100) END';
             $config['expression'] = $config['expression_original'];
             return true;
         }
     }
 }