ObraGastoController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\modules\excel\controllers;
  3. use v1\models\ObraEmpleado;
  4. use v1\models\Usuario;
  5. use DateTime;
  6. use DateTimeZone;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  9. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  10. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  11. use v1\models\Gasto;
  12. use v1\models\Obra;
  13. use v1\models\ObraHerramienta;
  14. use Yii;
  15. use yii\web\Controller;
  16. class ObraGastoController extends Controller {
  17. public function actionExcelObraGasto($idObra) {
  18. $obra = Obra::findOne($idObra);
  19. $request = Yii::$app->request;
  20. $query= Gasto::find()
  21. ->joinWith('conceptosObra')
  22. ->andWhere(['{{Gasto}}.[[eliminado]]'=>null])->andWhere(['{{Gasto}}.[[idObra]]'=>$idObra]);
  23. $query->orderBy(['creado' => SORT_DESC]);
  24. $spreadsheet = new Spreadsheet();
  25. $sheet = $spreadsheet->getActiveSheet();
  26. $sheet->setTitle('Gastos de la obra');
  27. $BASEPATH = \Yii::getAlias('@app') . "/web";
  28. $logo = $BASEPATH . '/img/logos/edesarrollos-unicolor-azul.png';
  29. $fechaInicio = (new DateTime($obra->fechaInicio))->format('d-m-Y');
  30. $fechaFin = (new DateTime($obra->fechaFinal))->format('d-m-Y');
  31. $drawing = new Drawing();
  32. $drawing->setName('Logotipo');
  33. $drawing->setDescription('Logotipo de la empresa');
  34. $drawing->setPath($logo);
  35. $drawing->setHeight(70);
  36. $drawing->setCoordinates('A1');
  37. $drawing->setOffsetX(10);
  38. $drawing->setOffsetY(10);
  39. $drawing->setWorksheet($sheet);
  40. $sheet->mergeCells('A5:J5');
  41. $sheet->setCellValue('A5','Gastos relacionado a la obra: ' . $obra->nombre);
  42. $sheet->mergeCells('A6:E6');
  43. $sheet->setCellValue('A6','Fecha Inicial: ' .$fechaInicio);
  44. $sheet->mergeCells('F6:J6');
  45. $sheet->setCellValue('F6','Fecha Final: ' .$fechaFin);
  46. $sheet->getStyle('A5')->getFont()->setBold(true);
  47. $sheet->getStyle('A5')->getFont()->setSize(16);
  48. $headerStyle = [
  49. 'font' => [
  50. 'bold' => true,
  51. 'color' => ['argb' => 'FFFFFFFF'] // Letras en blanco
  52. ],
  53. 'alignment' => [
  54. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  55. 'vertical' => Alignment::VERTICAL_CENTER,
  56. 'wrapText' => true
  57. ],
  58. 'fill' => [
  59. 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
  60. 'startColor' => ['argb' => 'FF625FF5'] // Fondo azul (con opacidad FF)
  61. ],
  62. 'borders' => [
  63. 'allBorders' => [
  64. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  65. 'color' => ['argb' => 'FF020073'], // Bordes azul oscuro (con opacidad FF)
  66. ],
  67. ],
  68. ];
  69. // Aplicar el borde a las celdas combinadas
  70. $sheet->getStyle('A5:J5')->applyFromArray( $headerStyle);
  71. $sheet->getStyle('A6:E6')->applyFromArray( $headerStyle);
  72. $sheet->getStyle('F6:J6')->applyFromArray( $headerStyle);
  73. $sheet->mergeCells('A8:C8');
  74. $sheet->setCellValue('A8', 'Concepto Obra');
  75. $sheet->getStyle('A8:C8')->applyFromArray( $headerStyle);
  76. $sheet->mergeCells('D8:E8');
  77. $sheet->setCellValue('D8', 'Cantidad');
  78. $sheet->getStyle('D8:E8')->applyFromArray( $headerStyle);
  79. $sheet->mergeCells('F8:G8');
  80. $sheet->setCellValue('F8', 'Fecha Compra');
  81. $sheet->getStyle('F8:G8')->applyFromArray( $headerStyle);
  82. $sheet->mergeCells('H8:J8');
  83. $sheet->setCellValue('H8', 'Descripción');
  84. $sheet->getStyle('H8:J8')->applyFromArray( $headerStyle);
  85. $cellBorderStyle = [
  86. 'borders' => [
  87. 'allBorders' => [
  88. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  89. 'color' => ['argb' => 'FF000000'], // Color negro
  90. ],
  91. ],
  92. ];
  93. $row = 9;
  94. foreach ($query->each() as $obraGasto) {
  95. $fecha = (new DateTime($obraGasto->fechaCompra))->format('d-m-Y');
  96. $gasto = $obraGasto->conceptosObra;
  97. $sheet->mergeCells('A' . $row . ':C' . $row);
  98. $sheet->setCellValue('A' . $row, $gasto->concepto);
  99. $sheet->getStyle('A' . $row . ':C' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  100. $sheet->getStyle('A' . $row . ':C' . $row)->applyFromArray($cellBorderStyle);
  101. $sheet->mergeCells('D' . $row . ':E' . $row);
  102. $sheet->setCellValue('D' . $row, $obraGasto->cantidad);
  103. $sheet->getStyle('D' . $row . ':E' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  104. $sheet->getStyle('D' . $row . ':E' . $row)->applyFromArray($cellBorderStyle);
  105. $sheet->getStyle('D' . $row)
  106. ->getNumberFormat()
  107. ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  108. $sheet->mergeCells('F' . $row . ':G' . $row);
  109. $sheet->setCellValue('F' . $row, $fecha);
  110. $sheet->getStyle('F' . $row . ':G' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  111. $sheet->getStyle('F' . $row . ':G' . $row)->applyFromArray($cellBorderStyle);
  112. $sheet->mergeCells('H' . $row . ':J' . $row);
  113. $sheet->setCellValue('H' . $row, $obraGasto->descripcion);
  114. $sheet->getStyle('H' . $row . ':J' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  115. $sheet->getStyle('H' . $row . ':J' . $row)->applyFromArray($cellBorderStyle);
  116. $row++;
  117. }
  118. $sheet->getStyle('A5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  119. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  120. try {
  121. ob_start();
  122. $writer->save("php://output");
  123. $documento = ob_get_contents();
  124. ob_clean();
  125. Yii::$app->getResponse()->sendContentAsFile($documento, "Gasto.xlsx");
  126. } catch (\Exception $exception) {
  127. return null;
  128. }
  129. }
  130. }