ObraHerramientaController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 v1\models\ObraHerramienta;
  13. use Yii;
  14. use yii\web\Controller;
  15. class ObraHerramientaController extends Controller {
  16. public function actionExcelObraHerramienta($idObra) {
  17. $obra = Obra::findOne($idObra);
  18. $request = Yii::$app->request;
  19. $query= ObraHerramienta::find()
  20. ->joinWith('herramienta')
  21. ->andWhere(['{{ObraHerramienta}}.[[eliminado]]'=>null])->andWhere(['{{ObraHerramienta}}.[[idObra]]'=>$idObra]);
  22. $query->orderBy(['creado' => SORT_DESC]);
  23. $spreadsheet = new Spreadsheet();
  24. $sheet = $spreadsheet->getActiveSheet();
  25. $sheet->setTitle('Herramientas de la obra');
  26. $BASEPATH = \Yii::getAlias('@app') . "/web";
  27. $logo = $BASEPATH . '/img/logos/edesarrollos-unicolor-azul.png';
  28. $fechaInicio = (new DateTime($obra->fechaInicio))->format('d-m-Y');
  29. $fechaFin = (new DateTime($obra->fechaFinal))->format('d-m-Y');
  30. $drawing = new Drawing();
  31. $drawing->setName('Logotipo');
  32. $drawing->setDescription('Logotipo de la empresa');
  33. $drawing->setPath($logo);
  34. $drawing->setHeight(70);
  35. $drawing->setCoordinates('A1');
  36. $drawing->setOffsetX(10);
  37. $drawing->setOffsetY(10);
  38. $drawing->setWorksheet($sheet);
  39. $sheet->mergeCells('A5:J5');
  40. $sheet->setCellValue('A5','Herramientas relacionadas a la obra: ' . $obra->nombre);
  41. $sheet->mergeCells('A6:E6');
  42. $sheet->setCellValue('A6','Fecha Inicial: ' .$fechaInicio);
  43. $sheet->mergeCells('F6:J6');
  44. $sheet->setCellValue('F6','Fecha Final: ' .$fechaFin);
  45. $sheet->getStyle('A5')->getFont()->setBold(true);
  46. $sheet->getStyle('A5')->getFont()->setSize(16);
  47. $headerStyle = [
  48. 'font' => [
  49. 'bold' => true,
  50. 'color' => ['argb' => 'FFFFFFFF'] // Letras en blanco
  51. ],
  52. 'alignment' => [
  53. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  54. 'vertical' => Alignment::VERTICAL_CENTER,
  55. 'wrapText' => true
  56. ],
  57. 'fill' => [
  58. 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
  59. 'startColor' => ['argb' => 'FF625FF5'] // Fondo azul (con opacidad FF)
  60. ],
  61. 'borders' => [
  62. 'allBorders' => [
  63. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  64. 'color' => ['argb' => 'FF020073'], // Bordes azul oscuro (con opacidad FF)
  65. ],
  66. ],
  67. ];
  68. // Aplicar el borde a las celdas combinadas
  69. $sheet->getStyle('A5:J5')->applyFromArray( $headerStyle);
  70. $sheet->getStyle('A6:E6')->applyFromArray( $headerStyle);
  71. $sheet->getStyle('F6:J6')->applyFromArray( $headerStyle);
  72. $sheet->mergeCells('A8:D8');
  73. $sheet->setCellValue('A8', 'Herramienta');
  74. $sheet->getStyle('A8:D8')->applyFromArray( $headerStyle);
  75. $sheet->mergeCells('E8:J8');
  76. $sheet->setCellValue('E8', 'Cantidad');
  77. $sheet->getStyle('E8:J8')->applyFromArray( $headerStyle);
  78. $cellBorderStyle = [
  79. 'borders' => [
  80. 'allBorders' => [
  81. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  82. 'color' => ['argb' => 'FF000000'], // Color negro
  83. ],
  84. ],
  85. ];
  86. $row = 9;
  87. foreach ($query->each() as $obraHerramienta) {
  88. $herramienta = $obraHerramienta->herramienta;
  89. $sheet->mergeCells('A' . $row . ':D' . $row);
  90. $sheet->setCellValue('A' . $row, $herramienta->nombre); // Nombre del empleado
  91. $sheet->getStyle('A' . $row . ':D' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  92. $sheet->getStyle('A' . $row . ':D' . $row)->applyFromArray($cellBorderStyle);
  93. $sheet->mergeCells('E' . $row . ':J' . $row);
  94. $sheet->setCellValue('E' . $row, $obraHerramienta->cantidad); // Nombre del empleado
  95. $sheet->getStyle('E' . $row . ':J' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  96. $sheet->getStyle('E' . $row . ':J' . $row)->applyFromArray($cellBorderStyle);
  97. $row++;
  98. }
  99. $sheet->getStyle('A5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  100. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  101. try {
  102. ob_start();
  103. $writer->save("php://output");
  104. $documento = ob_get_contents();
  105. ob_clean();
  106. Yii::$app->getResponse()->sendContentAsFile($documento, "Herramientas.xlsx");
  107. } catch (\Exception $exception) {
  108. return null;
  109. }
  110. }
  111. }