ObraHerramientaController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 excel\web\Controller;
  15. use PhpOffice\PhpSpreadsheet\Style\Fill;
  16. use yii\web\Response;
  17. class ObraHerramientaController extends Controller {
  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 actionExcelObraHerramienta($idObra) {
  102. $json = intval($this->req->get("json", ""));
  103. $obra = Obra::findOne($idObra);
  104. $request = Yii::$app->request;
  105. $query= ObraHerramienta::find()
  106. ->joinWith('herramienta')
  107. ->andWhere(['{{ObraHerramienta}}.[[eliminado]]'=>null])->andWhere(['{{ObraHerramienta}}.[[idObra]]'=>$idObra]);
  108. $query->orderBy(['creado' => SORT_DESC]);
  109. if ($json) {
  110. \Yii::$app->getResponse()->format = Response::FORMAT_JSON;
  111. return $json;
  112. }
  113. $BASEPATH = \Yii::getAlias('@app') . "/web";
  114. $logo = $BASEPATH . '/img/logos/edesarrollos-unicolor-azul.png';
  115. $this->encabezadoHoja($this->logo($logo, 'A1'));
  116. $fechaInicio = (new DateTime($obra->fechaInicio))->format('d-m-Y');
  117. $fechaFin = (new DateTime($obra->fechaFinal))->format('d-m-Y');
  118. $estiloEncabezado = array_merge(self::$celdaVertical, self::$bordes);
  119. $encabezadoTabla = [
  120. "A6" => [
  121. "valor" => "Herramientas relacionadas a la obra: $obra->nombre",
  122. "combinar" => "D6",
  123. "estilo" => $estiloEncabezado
  124. ],
  125. "A7" => [
  126. "valor" => "Fecha Inicial: $fechaInicio ",
  127. "combinar" => "B7",
  128. "estilo" => $estiloEncabezado
  129. ],
  130. "C7" => [
  131. "valor" => "Fecha Final: $fechaFin",
  132. "combinar" => "D7",
  133. "estilo" => $estiloEncabezado
  134. ]
  135. ];
  136. $this->agregarRenglones($encabezadoTabla);
  137. $encabezadoTabla = [
  138. "A9" => [
  139. "valor" => "Nombre",
  140. "estilo" => $estiloEncabezado,
  141. "combinar" => "B9",
  142. ],
  143. "C9" => [
  144. "valor" => "Cantidad",
  145. "estilo" => $estiloEncabezado,
  146. "combinar" => "D9",
  147. ],
  148. ];
  149. $r = 10;
  150. $this->agregarRenglones($encabezadoTabla);
  151. $estiloCeldaIzquierda = array_merge(self::$bordes, self::$celdaFuente12);
  152. $estiloCondicional = $this->estiloCondicional();
  153. foreach ($query->each() as $obraHerramienta) {
  154. $herramienta = $obraHerramienta->herramienta;
  155. $renglones = [
  156. "A{$r}" => [
  157. "valor" => $herramienta->nombre,
  158. "estilo" => $estiloCeldaIzquierda,
  159. "combinar" => "B{$r}",
  160. ],
  161. "C{$r}" => [
  162. "valor" => $obraHerramienta->cantidad,
  163. "estilo" => $estiloCeldaIzquierda,
  164. "combinar" => "D{$r}",
  165. "formato" => "#,##0",
  166. "estiloCondicional" => $estiloCondicional
  167. ],
  168. ];
  169. $this->agregarRenglones($renglones);
  170. $r += 1;
  171. }
  172. $anchoColumnas = [
  173. "A" => ["ancho" => 20],
  174. "B" => ["ancho" => 20],
  175. "C" => ["ancho" => 20],
  176. "D" => ["ancho" => 20],
  177. ];
  178. $this->anchoColumnas($anchoColumnas);
  179. $documento = $this->obtenerHojaDeCalculo();
  180. $this->crear($documento, "Herramienta_" . date("YmdHis"), self::TIPO_EXCEL);
  181. \Yii::$app->end();
  182. }
  183. }