ObraPagoController.php 6.2 KB

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