PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/extensions/eexcelview/ReporteEgresoView.php

https://bitbucket.org/raboit/sisfac
PHP | 286 lines | 190 code | 54 blank | 42 comment | 29 complexity | e316af082fff096d05c0519927dfa9f6 MD5 | raw file
  1. <?php
  2. Yii::import('ext.eexcelview.*');
  3. /**
  4. * @author Nikola Kostadinov
  5. * @license GPL
  6. * @version 0.2
  7. */
  8. class ReporteEgresoView extends EExcelView
  9. {
  10. public $bandera = null;
  11. public $subtotal_proyecto = null;
  12. public $total_proyecto = null;
  13. public function renderHeader()
  14. {
  15. $i=0;
  16. $default_border = array(
  17. 'style' => PHPExcel_Style_Border::BORDER_THIN,
  18. 'color' => array('rgb'=>'000000')
  19. );
  20. $style_header = array(
  21. 'borders' => array(
  22. 'bottom' => $default_border,
  23. 'left' => $default_border,
  24. 'top' => $default_border,
  25. 'right' => $default_border,
  26. ),
  27. 'fill' => array(
  28. 'type' => PHPExcel_Style_Fill::FILL_SOLID,
  29. 'color' => array('rgb'=>'819FF7'),
  30. ),
  31. 'font' => array(
  32. 'bold' => true,
  33. )
  34. );
  35. foreach($this->columns as $n=>$column)
  36. {
  37. ++$i;
  38. if($column->name!==null && $column->header===null)
  39. {
  40. if($column->grid->dataProvider instanceof CActiveDataProvider)
  41. $head = $column->grid->dataProvider->model->getAttributeLabel($column->name);
  42. else
  43. $head = $column->name;
  44. } else
  45. $head =trim($column->header)!=='' ? $column->header : $column->grid->blankDisplay;
  46. self::$activeSheet->setCellValue($this->columnName($i)."1" ,$head);
  47. self::$activeSheet->getStyle($this->columnName($i)."1" )->applyFromArray( $style_header );
  48. }
  49. }
  50. public function renderFooter()//footer created by francis
  51. {
  52. $i=0;
  53. $data=$this->dataProvider->getData();
  54. $row=count($data);
  55. foreach($this->columns as $n=>$column)
  56. {
  57. $i=$i+1;
  58. $footer =trim($column->footer)!=='' ? $column->footer : "";
  59. self::$activeSheet->setCellValue($this->columnName($i).($row+2),$footer);
  60. }
  61. }
  62. // Main consuming function, apply every optimization you could think of
  63. public function renderBody()
  64. {
  65. if($this->disablePaging) //if needed disable paging to export all data
  66. $this->enablePagination = false;
  67. self::$data = $this->dataProvider->getData();
  68. // $solicitudes = $this->dataProvider->getData();
  69. // foreach ($solicitudes as $solicitud) {
  70. //
  71. // print_r($solicitud);
  72. // echo "nombre de la wea: ".$solicitud->proyecto->nombre;
  73. //
  74. // }
  75. $n=count(self::$data);
  76. if($n>0)
  77. for($row=0; $row < $n; ++$row)
  78. $this->renderRow($row);
  79. }
  80. public function renderRow($row)
  81. {
  82. $i=0;
  83. foreach($this->columns as $n=>$column):
  84. if($column->value!==null)
  85. $value=$this->evaluateExpression($column->value ,array('row'=>$row,'data'=>self::$data));
  86. else if($column->name!==null)
  87. {
  88. //edited by francis to support relational dB tables
  89. $condition= explode(";", $column->name);
  90. $value=$column->name;
  91. // I don't understand this piece of code (the conditions and all that stuff), when these conditions will meet?
  92. // Francis, if you see this code ever again, please comment
  93. $countCondition = count($condition);
  94. if($countCondition==6 || $countCondition==5):
  95. switch($countCondition):
  96. case 6:
  97. $cond1=$this->dataProcess($condition[0],$row);
  98. if($condition[3]=='true')
  99. $cond2=$condition[2];
  100. else
  101. $cond2=$this->dataProcess($condition[2],$row);
  102. $cond3=$this->dataProcess($condition[4],$row);
  103. $cond4=$this->dataProcess($condition[5],$row);
  104. break;
  105. case 5:
  106. $cond1=$this->dataProcess($condition[0],$row);
  107. $cond2=$this->dataProcess($condition[2],$row);
  108. $cond3=$this->dataProcess($condition[3],$row);
  109. $cond4=$this->dataProcess($condition[4],$row);
  110. break;
  111. default:
  112. break;
  113. endswitch;
  114. switch($condition[1]):
  115. case '==':
  116. $value = ($cond1==$cond2)? $cond3 : $cond4;
  117. break;
  118. case '!=':
  119. $value = ($cond1!=$cond2)? $cond3 : $cond4;
  120. break;
  121. case '<=':
  122. $value = ($cond1<=$cond2)? $cond3 : $cond4;
  123. case '>=':
  124. $value = ($cond1>=$cond2)? $cond3 : $cond4;
  125. break;
  126. case '<':
  127. $value = ($cond1<$cond2)? $cond3 : $cond4;
  128. case '>':
  129. $value = ($cond1>$cond2)? $cond3 : $cond4;
  130. default:
  131. break;
  132. endswitch;
  133. elseif($countCondition!=1):
  134. $value='';
  135. else:
  136. $value=$this->dataProcess($column->name,$row);
  137. endif;
  138. }
  139. //date edited francis
  140. $dateF= explode("-", $value);
  141. $c1=count($dateF);
  142. if($c1==3 && $dateF[0]<9000 && $dateF[1]<13 && $dateF[2]<32)//{}
  143. $value=$dateF[2].'/'.$dateF[1].'/'.$dateF[0];
  144. //end of date
  145. $value=$value===null ? "" : $column->grid->getFormatter()->format($value,$column->type);
  146. // Write to the cell (and advance to the next)
  147. // Editado por: Sergio Cárdenas Espinoza
  148. // Lógica:
  149. // CUANDO LA INFORMACION ESTA LISTA PARA SER IMPRESA
  150. // SE APLICA LA SIGUIENTE LOGICA PARA IMPRIMIR INFORMES
  151. // QUE NO SE IMPRIMA DE FORMA REPETIDA LOS PROYECTOS
  152. // Y SE HAGA UNA SUMA POR PROYECTO Y UNA TOTAL.
  153. if($column->name=='total'):
  154. $this->total_proyecto = $value;
  155. endif;
  156. if($column->name=='centroCosto'){
  157. if($this->bandera!=$value){
  158. $this->bandera = $value;
  159. self::$activeSheet->setCellValue( $this->columnName(++$i).($row+2) , $value);
  160. }else{
  161. self::$activeSheet->setCellValue( $this->columnName(++$i).($row+2) , '');
  162. }
  163. }else{
  164. self::$activeSheet->setCellValue( $this->columnName(++$i).($row+2) , $value);
  165. }
  166. // else{
  167. // self::$activeSheet->setCellValue( $this->columnName(++$i).($row+2) , $value);
  168. // }
  169. //self::$activeSheet->setCellValue( $this->columnName(++$i).($row+2) , $value);
  170. endforeach;
  171. // As we are done with this row we DONT need this specific record
  172. unset(self::$data[$row]);
  173. }
  174. public function dataProcess($name,$row)
  175. {
  176. // Separate name (eg person.code into array('person', 'code'))
  177. $separated_name = explode(".", $name);
  178. // Count
  179. $n=count($separated_name);
  180. // Create a copy of the data row. Now we can "dive" trough the array until we reach the desired value
  181. // (because is nested)
  182. $aux = self::$data[$row]; //if n is greater than zero, we will loop, if not, $aux actually holds the desired value
  183. for($i=0; $i < $n; ++$i)
  184. $aux = $aux[$separated_name[$i]]; // We keep a deeper reference each time
  185. return $aux;
  186. }
  187. public function run()
  188. {
  189. $this->renderHeader();
  190. $this->renderBody();
  191. $this->renderFooter();
  192. //set auto width
  193. if($this->autoWidth)
  194. foreach($this->columns as $n=>$column)
  195. self::$objPHPExcel->getActiveSheet()->getColumnDimension($this->columnName($n+1))->setAutoSize(true);
  196. //create writer for saving
  197. $objWriter = PHPExcel_IOFactory::createWriter(self::$objPHPExcel, $this->exportType);
  198. if(!$this->stream)
  199. $objWriter->save($this->filename);
  200. else //output to browser
  201. {
  202. if(!$this->filename)
  203. $this->filename = $this->title;
  204. if (ob_get_length() > 0) {
  205. ob_end_clean();
  206. }
  207. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  208. header('Pragma: public');
  209. header('Content-type: '.$this->mimeTypes[$this->exportType]['Content-type']);
  210. header('Content-Disposition: attachment; filename="'.$this->filename.'.'.$this->mimeTypes[$this->exportType]['extension'].'"');
  211. header('Cache-Control: max-age=0');
  212. $objWriter->save('php://output');
  213. Yii::app()->end();
  214. }
  215. }
  216. /**
  217. * Returns the coresponding excel column.(Abdul Rehman from yii forum)
  218. *
  219. * @param int $index
  220. * @return string
  221. */
  222. public function columnName($index)
  223. {
  224. --$index;
  225. if($index >= 0 && $index < 26)
  226. return chr(ord('A') + $index);
  227. else if ($index > 25)
  228. return ($this->columnName($index / 26)).($this->columnName($index%26 + 1));
  229. else
  230. throw new Exception("Invalid Column # ".($index + 1));
  231. }
  232. }