ObraEmpleadoController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\Fill;
  10. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  11. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  12. use v1\models\Obra;
  13. use Yii;
  14. use excel\web\Controller;
  15. use yii\web\Response;
  16. class ObraEmpleadoController extends Controller
  17. {
  18. public static $estiloCeldaIzquierda12 = [
  19. 'font' => [
  20. 'bold' => true,
  21. 'size' => 12,
  22. 'color' => ['argb' => 'FF000000'],
  23. ],
  24. 'alignment' => [
  25. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  26. 'vertical' => Alignment::VERTICAL_CENTER,
  27. ],
  28. ];
  29. public static $celdaVertical = [
  30. 'font' => [
  31. 'bold' => true,
  32. 'size' => 14,
  33. 'color' => ['argb' => 'FFFFFFFF'],
  34. ],
  35. 'alignment' => [
  36. // 'textRotation' => 90,
  37. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  38. 'vertical' => Alignment::VERTICAL_CENTER
  39. ],
  40. 'fill' => [
  41. 'fillType' => Fill::FILL_SOLID,
  42. 'startColor' => ['argb' => "FF625FF5"]
  43. ],
  44. ];
  45. public static $celdaHorizontal = [
  46. 'font' => [
  47. 'bold' => true,
  48. 'size' => 14,
  49. 'color' => ['argb' => 'FF000000'],
  50. ],
  51. 'alignment' => [
  52. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  53. 'vertical' => Alignment::VERTICAL_CENTER
  54. ],
  55. 'fill' => [
  56. 'fillType' => Fill::FILL_SOLID,
  57. 'startColor' => ['argb' => "FFE7E7E7"]
  58. ],
  59. ];
  60. public static $celdaFuente12 = [
  61. 'font' => [
  62. 'bold' => true,
  63. 'size' => 10,
  64. 'color' => ['argb' => 'FF000000'],
  65. ],
  66. 'alignment' => [
  67. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  68. 'vertical' => Alignment::VERTICAL_CENTER
  69. ],
  70. ];
  71. public static $celdaVerticalF12 = [
  72. 'font' => [
  73. 'bold' => true,
  74. 'size' => 10,
  75. 'color' => ['argb' => 'FF000000'],
  76. ],
  77. 'alignment' => [
  78. 'textRotation' => 90,
  79. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  80. 'vertical' => Alignment::VERTICAL_CENTER
  81. ],
  82. 'fill' => [
  83. 'fillType' => Fill::FILL_SOLID,
  84. 'startColor' => ['argb' => "FFE7E7E7"]
  85. ],
  86. ];
  87. public function encabezadoHoja($logo)
  88. {
  89. $celdas = [
  90. "A1" => [
  91. "valor" => $logo,
  92. "combinar" => "G4",
  93. ],
  94. ];
  95. foreach ($celdas as $coordenada => $valor) {
  96. $this->agregarCelda($coordenada, $valor);
  97. }
  98. $this->renglonActual = 6;
  99. return $this;
  100. }
  101. public function actionExcelObraEmpleado($idObra)
  102. {
  103. $json = intval($this->req->get("json", ""));
  104. $obra = Obra::findOne($idObra);
  105. $request = Yii::$app->request;
  106. $query = ObraEmpleado::find()
  107. ->joinWith('empleado')
  108. ->andWhere(['{{ObraEmpleado}}.[[eliminado]]' => null])->andWhere(['{{ObraEmpleado}}.[[idObra]]' => $idObra]);
  109. $query->orderBy(['creado' => SORT_DESC]);
  110. if ($json) {
  111. \Yii::$app->getResponse()->format = Response::FORMAT_JSON;
  112. return $json;
  113. }
  114. $BASEPATH = \Yii::getAlias('@app') . "/web";
  115. $logo = $BASEPATH . '/img/logos/edesarrollos-unicolor-azul.png';
  116. $this->encabezadoHoja($this->logo($logo, 'A1'));
  117. $fechaInicio = (new DateTime($obra->fechaInicio))->format('d-m-Y');
  118. $fechaFin = (new DateTime($obra->fechaFinal))->format('d-m-Y');
  119. $estiloEncabezado = array_merge(self::$celdaVertical, self::$bordes);
  120. $encabezadoTabla = [
  121. "A6" => [
  122. "valor" => "Empleados relacionado a la obra: $obra->nombre",
  123. "combinar" => "D6",
  124. "estilo" => $estiloEncabezado
  125. ],
  126. "A7" => [
  127. "valor" => "Fecha Inicial: $fechaInicio ",
  128. "combinar" => "B7",
  129. "estilo" => $estiloEncabezado
  130. ],
  131. "C7" => [
  132. "valor" => "Fecha Final: $fechaFin",
  133. "combinar" => "D7",
  134. "estilo" => $estiloEncabezado
  135. ]
  136. ];
  137. $this->agregarRenglones($encabezadoTabla);
  138. $encabezadoTabla = [
  139. "A9" => [
  140. "valor" => "Nombre",
  141. "estilo" => $estiloEncabezado,
  142. "combinar" => "D9",
  143. ],
  144. ];
  145. $r = 10;
  146. $this->agregarRenglones($encabezadoTabla);
  147. $estiloCeldaIzquierda = array_merge(self::$bordes, self::$celdaFuente12);
  148. $estiloCondicional = $this->estiloCondicional();
  149. foreach ($query->each() as $obraEmpleado) {
  150. $empleado = $obraEmpleado->empleado;
  151. $renglones = [
  152. "A{$r}" => [
  153. "valor" => $empleado->nombre,
  154. "estilo" => $estiloCeldaIzquierda,
  155. "combinar" => "D{$r}",
  156. ],
  157. ];
  158. $this->agregarRenglones($renglones);
  159. $r += 1;
  160. }
  161. $anchoColumnas = [
  162. "A" => ["ancho" => 20],
  163. "B" => ["ancho" => 20],
  164. "C" => ["ancho" => 20],
  165. "D" => ["ancho" => 20],
  166. ];
  167. $this->anchoColumnas($anchoColumnas);
  168. $documento = $this->obtenerHojaDeCalculo();
  169. $this->crear($documento, "Empleados_" . date("YmdHis"), self::TIPO_EXCEL);
  170. \Yii::$app->end();
  171. }
  172. }