ObraRubroController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace app\modules\excel\controllers;
  3. use DateTime;
  4. use DateTimeZone;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  7. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  8. use v1\models\Nomina;
  9. use v1\models\Obra;
  10. use v1\models\ObraRubro;
  11. use v1\models\Pago;
  12. use Yii;
  13. use yii\web\Controller;
  14. class ObraRubroController extends Controller {
  15. public function actionExcelObraRubro($idObra) {
  16. $obra = Obra::findOne($idObra);
  17. $request = Yii::$app->request;
  18. $query= ObraRubro::find()
  19. ->joinWith('conceptoObra')
  20. ->andWhere(['{{ObraRubro}}.[[eliminado]]'=>null])->andWhere(['{{ObraRubro}}.[[idObra]]'=>$idObra]);
  21. $query->orderBy(['creado' => SORT_DESC]);
  22. $spreadsheet = new Spreadsheet();
  23. $sheet = $spreadsheet->getActiveSheet();
  24. $sheet->setTitle('Rubro de la obra');
  25. $BASEPATH = \Yii::getAlias('@app') . "/web";
  26. $logo = $BASEPATH . '/img/logos/edesarrollos-unicolor-azul.png';
  27. $fechaInicio = (new DateTime($obra->fechaInicio))->format('d-m-Y');
  28. $fechaFin = (new DateTime($obra->fechaFinal))->format('d-m-Y');
  29. $drawing = new Drawing();
  30. $drawing->setName('Logotipo');
  31. $drawing->setDescription('Logotipo de la empresa');
  32. $drawing->setPath($logo);
  33. $drawing->setHeight(70);
  34. $drawing->setCoordinates('A1');
  35. $drawing->setOffsetX(10);
  36. $drawing->setOffsetY(10);
  37. $drawing->setWorksheet($sheet);
  38. $sheet->mergeCells('A5:J5');
  39. $sheet->setCellValue('A5','Rubro relacionado a la obra: ' . $obra->nombre);
  40. $sheet->mergeCells('A6:E6');
  41. $sheet->setCellValue('A6','Fecha Inicial: ' .$fechaInicio);
  42. $sheet->mergeCells('F6:J6');
  43. $sheet->setCellValue('F6','Fecha Final: ' .$fechaFin);
  44. $sheet->getStyle('A5')->getFont()->setBold(true);
  45. $sheet->getStyle('A5')->getFont()->setSize(16);
  46. $headerStyle = [
  47. 'font' => [
  48. 'bold' => true,
  49. 'color' => ['argb' => 'FFFFFFFF'] // Letras en blanco
  50. ],
  51. 'alignment' => [
  52. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  53. 'vertical' => Alignment::VERTICAL_CENTER,
  54. 'wrapText' => true
  55. ],
  56. 'fill' => [
  57. 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
  58. 'startColor' => ['argb' => 'FF625FF5'] // Fondo azul (con opacidad FF)
  59. ],
  60. 'borders' => [
  61. 'allBorders' => [
  62. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  63. 'color' => ['argb' => 'FF020073'], // Bordes azul oscuro (con opacidad FF)
  64. ],
  65. ],
  66. ];
  67. // Aplicar el borde a las celdas combinadas
  68. $sheet->getStyle('A5:J5')->applyFromArray( $headerStyle);
  69. $sheet->getStyle('A6:E6')->applyFromArray( $headerStyle);
  70. $sheet->getStyle('F6:J6')->applyFromArray( $headerStyle);
  71. $sheet->mergeCells('A8:C8');
  72. $sheet->setCellValue('A8', 'Concepto Obra');
  73. $sheet->getStyle('A8:C8')->applyFromArray( $headerStyle);
  74. $sheet->mergeCells('D8:E8');
  75. $sheet->setCellValue('D8', 'Cantidad');
  76. $sheet->getStyle('D8:E8')->applyFromArray( $headerStyle);
  77. $sheet->mergeCells('F8:G8');
  78. $sheet->setCellValue('F8', 'Fecha Compra');
  79. $sheet->getStyle('F8:G8')->applyFromArray( $headerStyle);
  80. $sheet->mergeCells('H8:J8');
  81. $sheet->setCellValue('H8', 'Descripción');
  82. $sheet->getStyle('H8:J8')->applyFromArray( $headerStyle);
  83. $cellBorderStyle = [
  84. 'borders' => [
  85. 'allBorders' => [
  86. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  87. 'color' => ['argb' => 'FF000000'], // Color negro
  88. ],
  89. ],
  90. ];
  91. $row = 9;
  92. foreach ($query->each() as $obraRubro) {
  93. $fecha = (new DateTime($obraRubro->fechaCompra))->format('d-m-Y');
  94. $gasto = $obraRubro->conceptoObra;
  95. $sheet->mergeCells('A' . $row . ':C' . $row);
  96. $sheet->setCellValue('A' . $row, $gasto->concepto);
  97. $sheet->getStyle('A' . $row . ':C' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  98. $sheet->getStyle('A' . $row . ':C' . $row)->applyFromArray($cellBorderStyle);
  99. $sheet->mergeCells('D' . $row . ':E' . $row);
  100. $sheet->setCellValue('D' . $row, $obraRubro->cantidad);
  101. $sheet->getStyle('D' . $row . ':E' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  102. $sheet->getStyle('D' . $row . ':E' . $row)->applyFromArray($cellBorderStyle);
  103. $sheet->getStyle('D' . $row)
  104. ->getNumberFormat()
  105. ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  106. $sheet->mergeCells('F' . $row . ':G' . $row);
  107. $sheet->setCellValue('F' . $row, $fecha);
  108. $sheet->getStyle('F' . $row . ':G' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  109. $sheet->getStyle('F' . $row . ':G' . $row)->applyFromArray($cellBorderStyle);
  110. $sheet->mergeCells('H' . $row . ':J' . $row);
  111. $sheet->setCellValue('H' . $row, $obraRubro->descripcion);
  112. $sheet->getStyle('H' . $row . ':J' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  113. $sheet->getStyle('H' . $row . ':J' . $row)->applyFromArray($cellBorderStyle);
  114. $row++;
  115. }
  116. $sheet->getStyle('A5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  117. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  118. try {
  119. ob_start();
  120. $writer->save("php://output");
  121. $documento = ob_get_contents();
  122. ob_clean();
  123. Yii::$app->getResponse()->sendContentAsFile($documento, "Rubro.xlsx");
  124. } catch (\Exception $exception) {
  125. return null;
  126. }
  127. }
  128. public function actionExcelRubro($idObra) {
  129. $obra = Obra::findOne($idObra);
  130. $request = Yii::$app->request;
  131. $query= ObraRubro::find()
  132. ->joinWith('conceptoObra')
  133. ->andWhere(['{{ObraRubro}}.[[eliminado]]'=>null])->andWhere(['{{ObraRubro}}.[[idObra]]'=>$idObra]);
  134. $query->orderBy(['creado' => SORT_DESC]);
  135. $spreadsheet = new Spreadsheet();
  136. $sheet = $spreadsheet->getActiveSheet();
  137. $sheet->setTitle('Rubro de la obra');
  138. $BASEPATH = \Yii::getAlias('@app') . "/web";
  139. $logo = $BASEPATH . '/img/logos/edesarrollos-unicolor-azul.png';
  140. $fechaInicio = (new DateTime($obra->fechaInicio))->format('d-m-Y');
  141. $fechaFin = (new DateTime($obra->fechaFinal))->format('d-m-Y');
  142. $drawing = new Drawing();
  143. $drawing->setName('Logotipo');
  144. $drawing->setDescription('Logotipo de la empresa');
  145. $drawing->setPath($logo);
  146. $drawing->setHeight(70);
  147. $drawing->setCoordinates('A1');
  148. $drawing->setOffsetX(10);
  149. $drawing->setOffsetY(10);
  150. $drawing->setWorksheet($sheet);
  151. $sheet->mergeCells('A5:J5');
  152. $sheet->setCellValue('A5','Rubro relacionado a la obra: ' . $obra->nombre);
  153. $sheet->mergeCells('A6:E6');
  154. $sheet->setCellValue('A6','Fecha Inicial: ' .$fechaInicio);
  155. $sheet->mergeCells('F6:J6');
  156. $sheet->setCellValue('F6','Fecha Final: ' .$fechaFin);
  157. $sheet->getStyle('A5')->getFont()->setBold(true);
  158. $sheet->getStyle('A5')->getFont()->setSize(16);
  159. $headerStyle = [
  160. 'font' => [
  161. 'bold' => true,
  162. 'color' => ['argb' => 'FFFFFFFF'] // Letras en blanco
  163. ],
  164. 'alignment' => [
  165. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  166. 'vertical' => Alignment::VERTICAL_CENTER,
  167. 'wrapText' => true
  168. ],
  169. 'fill' => [
  170. 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
  171. 'startColor' => ['argb' => 'FF625FF5'] // Fondo azul (con opacidad FF)
  172. ],
  173. 'borders' => [
  174. 'allBorders' => [
  175. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  176. 'color' => ['argb' => 'FF020073'], // Bordes azul oscuro (con opacidad FF)
  177. ],
  178. ],
  179. ];
  180. // Aplicar el borde a las celdas combinadas
  181. $sheet->getStyle('A5:J5')->applyFromArray( $headerStyle);
  182. $sheet->getStyle('A6:E6')->applyFromArray( $headerStyle);
  183. $sheet->getStyle('F6:J6')->applyFromArray( $headerStyle);
  184. $sheet->mergeCells('A8:C8');
  185. $sheet->setCellValue('A8', 'Concepto Obra');
  186. $sheet->getStyle('A8:C8')->applyFromArray( $headerStyle);
  187. $sheet->mergeCells('D8:E8');
  188. $sheet->setCellValue('D8', 'Cantidad');
  189. $sheet->getStyle('D8:E8')->applyFromArray( $headerStyle);
  190. $sheet->mergeCells('F8:G8');
  191. $sheet->setCellValue('F8', 'Fecha Compra');
  192. $sheet->getStyle('F8:G8')->applyFromArray( $headerStyle);
  193. $sheet->mergeCells('H8:J8');
  194. $sheet->setCellValue('H8', 'Descripción');
  195. $sheet->getStyle('H8:J8')->applyFromArray( $headerStyle);
  196. $cellBorderStyle = [
  197. 'borders' => [
  198. 'allBorders' => [
  199. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  200. 'color' => ['argb' => 'FF000000'], // Color negro
  201. ],
  202. ],
  203. ];
  204. $row = 9;
  205. foreach ($query->each() as $obraRubro) {
  206. $fecha = (new DateTime($obraRubro->fechaCompra))->format('d-m-Y');
  207. $gasto = $obraRubro->conceptoObra;
  208. $sheet->mergeCells('A' . $row . ':C' . $row);
  209. $sheet->setCellValue('A' . $row, $gasto->concepto);
  210. $sheet->getStyle('A' . $row . ':C' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  211. $sheet->getStyle('A' . $row . ':C' . $row)->applyFromArray($cellBorderStyle);
  212. $sheet->mergeCells('D' . $row . ':E' . $row);
  213. $sheet->setCellValue('D' . $row, $obraRubro->cantidad);
  214. $sheet->getStyle('D' . $row . ':E' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  215. $sheet->getStyle('D' . $row . ':E' . $row)->applyFromArray($cellBorderStyle);
  216. $sheet->getStyle('D' . $row)
  217. ->getNumberFormat()
  218. ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  219. $sheet->mergeCells('F' . $row . ':G' . $row);
  220. $sheet->setCellValue('F' . $row, $fecha);
  221. $sheet->getStyle('F' . $row . ':G' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  222. $sheet->getStyle('F' . $row . ':G' . $row)->applyFromArray($cellBorderStyle);
  223. $sheet->mergeCells('H' . $row . ':J' . $row);
  224. $sheet->setCellValue('H' . $row, $obraRubro->descripcion);
  225. $sheet->getStyle('H' . $row . ':J' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  226. $sheet->getStyle('H' . $row . ':J' . $row)->applyFromArray($cellBorderStyle);
  227. $row++;
  228. }
  229. $sheet->getStyle('A5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  230. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  231. try {
  232. return $sheet;
  233. } catch (\Exception $exception) {
  234. return null;
  235. }
  236. }
  237. }