ObraEmpleadoController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Obra;
  12. use Yii;
  13. use yii\web\Controller;
  14. class ObraEmpleadoController extends Controller {
  15. public function actionExcelObraEmpleado($idObra) {
  16. $obra = Obra::findOne($idObra);
  17. $request = Yii::$app->request;
  18. $query= ObraEmpleado::find()
  19. ->joinWith('empleado')
  20. ->andWhere(['{{ObraEmpleado}}.[[eliminado]]'=>null])->andWhere(['{{ObraEmpleado}}.[[idObra]]'=>$idObra]);
  21. $query->orderBy(['creado' => SORT_DESC]);
  22. $spreadsheet = new Spreadsheet();
  23. $sheet = $spreadsheet->getActiveSheet();
  24. $sheet->setTitle('Empleados');
  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','Empleados relacionados 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:J8');
  72. $sheet->setCellValue('A8', 'Nombre');
  73. $sheet->getStyle('A8:J8')->applyFromArray( $headerStyle);
  74. $cellBorderStyle = [
  75. 'borders' => [
  76. 'allBorders' => [
  77. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  78. 'color' => ['argb' => 'FF000000'], // Color negro
  79. ],
  80. ],
  81. ];
  82. $row = 9;
  83. foreach ($query->each() as $obraEmpleado) {
  84. $empleado = $obraEmpleado->empleado;
  85. $sheet->mergeCells('A' . $row . ':J' . $row);
  86. $sheet->setCellValue('A' . $row, $empleado->nombre); // Nombre del empleado
  87. $sheet->getStyle('A' . $row . ':J' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  88. $sheet->getStyle('A' . $row . ':J' . $row)->applyFromArray($cellBorderStyle);
  89. $row++;
  90. }
  91. $sheet->getStyle('A5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  92. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  93. try {
  94. ob_start();
  95. $writer->save("php://output");
  96. $documento = ob_get_contents();
  97. ob_clean();
  98. Yii::$app->getResponse()->sendContentAsFile($documento, "Empleados.xlsx");
  99. } catch (\Exception $exception) {
  100. return null;
  101. }
  102. }
  103. }