Controller.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. namespace excel\web;
  3. use common\rest\JsonController;
  4. use yii\web\Controller as WebController;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  7. use PhpOffice\PhpSpreadsheet\IOFactory;
  8. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  9. use PhpOffice\PhpSpreadsheet\Style\Border;
  10. use PhpOffice\PhpSpreadsheet\Style\Color;
  11. use PhpOffice\PhpSpreadsheet\Style\Conditional;
  12. use PhpOffice\PhpSpreadsheet\Style\Fill;
  13. use yii\filters\auth\CompositeAuth;
  14. use yii\filters\auth\QueryParamAuth;
  15. use yii\web\Response;
  16. use Yii;
  17. class Controller extends WebController {
  18. public $app = null;
  19. public $req = null;
  20. public $res = null;
  21. const MIMETYPE_EXCEL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  22. const TIPO_EXCEL = "excel";
  23. const TIPO_PDF = "pdf";
  24. const COLOR_NEGRO = "FF000000";
  25. const COLOR_AZUL = "FF1A95E8";
  26. const COLOR_GRIS = "FF959595";
  27. public $renglonActual = 1;
  28. public $renglonPrevio = 0;
  29. protected $spreadsheet;
  30. protected $activeSheet;
  31. public static $estiloCeldaNormal = [
  32. 'alignment' => [
  33. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  34. ],
  35. ];
  36. public static $estiloCeldaCentrada = [
  37. 'font' => [
  38. 'bold' => true,
  39. 'size' => 16,
  40. 'color' => ['argb' => 'FF000000'],
  41. ],
  42. 'alignment' => [
  43. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  44. 'vertical' => Alignment::VERTICAL_CENTER,
  45. ],
  46. ];
  47. public static $estiloCeldaIzquierda = [
  48. 'font' => [
  49. 'bold' => true,
  50. 'size' => 16,
  51. 'color' => ['argb' => 'FF000000'],
  52. ],
  53. 'alignment' => [
  54. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  55. 'vertical' => Alignment::VERTICAL_CENTER,
  56. ],
  57. ];
  58. public static $bordes = [
  59. 'borders' => [
  60. 'left' => [
  61. 'borderStyle' => Border::BORDER_THIN,
  62. 'color' => ['argb' => 'FF000000'],
  63. ],
  64. 'right' => [
  65. 'borderStyle' => Border::BORDER_THIN,
  66. 'color' => ['argb' => 'FF000000'],
  67. ],
  68. 'top' => [
  69. 'borderStyle' => Border::BORDER_THIN,
  70. 'color' => ['argb' => 'FF000000'],
  71. ],
  72. 'bottom' => [
  73. 'borderStyle' => Border::BORDER_THIN,
  74. 'color' => ['argb' => 'FF000000'],
  75. ]
  76. ],
  77. ];
  78. public static $bordesPrimario = [
  79. 'borders' => [
  80. 'left' => [
  81. 'borderStyle' => Border::BORDER_THIN,
  82. 'color' => ['argb' => 'FF750d44'],
  83. ],
  84. 'right' => [
  85. 'borderStyle' => Border::BORDER_THIN,
  86. 'color' => ['argb' => 'FF750d44'],
  87. ],
  88. 'top' => [
  89. 'borderStyle' => Border::BORDER_THIN,
  90. 'color' => ['argb' => 'FF750d44'],
  91. ],
  92. 'bottom' => [
  93. 'borderStyle' => Border::BORDER_THIN,
  94. 'color' => ['argb' => 'FF750d44'],
  95. ]
  96. ],
  97. ];
  98. public static $bordesWhite = [
  99. 'borders' => [
  100. 'left' => [
  101. 'borderStyle' => Border::BORDER_THIN,
  102. 'color' => ['argb' => 'FFFFFFFF'],
  103. ],
  104. 'right' => [
  105. 'borderStyle' => Border::BORDER_THIN,
  106. 'color' => ['argb' => 'FFFFFFFF'],
  107. ],
  108. 'top' => [
  109. 'borderStyle' => Border::BORDER_THIN,
  110. 'color' => ['argb' => 'FFFFFFFF'],
  111. ],
  112. 'bottom' => [
  113. 'borderStyle' => Border::BORDER_THIN,
  114. 'color' => ['argb' => 'FFFFFFFF'],
  115. ]
  116. ],
  117. ];
  118. public static $bordesGray = [
  119. 'borders' => [
  120. 'left' => [
  121. 'borderStyle' => Border::BORDER_THIN,
  122. 'color' => ['argb' => 'FFE2E2E2'],
  123. ],
  124. 'right' => [
  125. 'borderStyle' => Border::BORDER_THIN,
  126. 'color' => ['argb' => 'FFE2E2E2'],
  127. ],
  128. 'top' => [
  129. 'borderStyle' => Border::BORDER_THIN,
  130. 'color' => ['argb' => 'FFE2E2E2'],
  131. ],
  132. 'bottom' => [
  133. 'borderStyle' => Border::BORDER_THIN,
  134. 'color' => ['argb' => 'FFE2E2E2'],
  135. ]
  136. ],
  137. ];
  138. public static $estiloEncabezado = [
  139. 'font' => [
  140. 'bold' => true,
  141. 'size' => 9
  142. ],
  143. 'alignment' => [
  144. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  145. 'vertical' => Alignment::VERTICAL_CENTER,
  146. ],
  147. 'fill' => [
  148. 'fillType' => Fill::FILL_SOLID,
  149. 'startColor' => ['argb' => self::COLOR_GRIS]
  150. ],
  151. 'font' => [
  152. 'bold' => true,
  153. 'color' => ['argb' => "AAFFFFFF"],
  154. ],
  155. 'borders' => [
  156. 'left' => [
  157. 'borderStyle' => Border::BORDER_THICK,
  158. 'color' => ['argb' => 'FFFFFFFF'],
  159. ],
  160. 'right' => [
  161. 'borderStyle' => Border::BORDER_THICK,
  162. 'color' => ['argb' => 'FFFFFFFF'],
  163. ]
  164. ],
  165. ];
  166. public static $estiloTitulo = [
  167. 'font' => [
  168. 'bold' => true,
  169. 'size' => 13,
  170. 'color' => ['argb' => self::COLOR_AZUL],
  171. ],
  172. 'alignment' => [
  173. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  174. 'vertical' => Alignment::VERTICAL_CENTER,
  175. ],
  176. ];
  177. public static $CELDA_VERDE = [
  178. 'alignment' => [
  179. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  180. ],
  181. 'fill' => [
  182. 'fillType' => Fill::FILL_SOLID,
  183. 'startColor' => ['argb' => 'FFD8E4BD']
  184. ],
  185. 'borders' => [
  186. 'left' => [
  187. 'borderStyle' => Border::BORDER_THICK,
  188. 'color' => ['argb' => 'FFFFFFFF'],
  189. ],
  190. 'right' => [
  191. 'borderStyle' => Border::BORDER_THICK,
  192. 'color' => ['argb' => 'FFFFFFFF'],
  193. ],
  194. 'bottom' => [
  195. 'borderStyle' => Border::BORDER_DASHED,
  196. ],
  197. ],
  198. ];
  199. public static $CELDA_AMARILLA = [
  200. 'alignment' => [
  201. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  202. ],
  203. 'fill' => [
  204. 'fillType' => Fill::FILL_SOLID,
  205. 'startColor' => ['argb' => 'FFFFE597']
  206. ],
  207. 'borders' => [
  208. 'left' => [
  209. 'borderStyle' => Border::BORDER_THICK,
  210. 'color' => ['argb' => 'FFFFFFFF'],
  211. ],
  212. 'right' => [
  213. 'borderStyle' => Border::BORDER_THICK,
  214. 'color' => ['argb' => 'FFFFFFFF'],
  215. ],
  216. 'bottom' => [
  217. 'borderStyle' => Border::BORDER_DASHED,
  218. ],
  219. ],
  220. ];
  221. public static $CELDA_ROJA = [
  222. 'alignment' => [
  223. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  224. ],
  225. 'fill' => [
  226. 'fillType' => Fill::FILL_SOLID,
  227. 'startColor' => ['argb' => 'FFE6B8B7']
  228. ],
  229. 'borders' => [
  230. 'left' => [
  231. 'borderStyle' => Border::BORDER_THICK,
  232. 'color' => ['argb' => 'FFFFFFFF'],
  233. ],
  234. 'right' => [
  235. 'borderStyle' => Border::BORDER_THICK,
  236. 'color' => ['argb' => 'FFFFFFFF'],
  237. ],
  238. 'bottom' => [
  239. 'borderStyle' => Border::BORDER_DASHED,
  240. ],
  241. ],
  242. ];
  243. public static $CELDA_PRIMARIA = [
  244. 'font' => [
  245. 'bold' => true,
  246. 'size' => 16,
  247. 'color' => ['argb' => 'FFFFFFFF'],
  248. ],
  249. 'alignment' => [
  250. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  251. ],
  252. 'fill' => [
  253. 'fillType' => Fill::FILL_SOLID,
  254. 'startColor' => ['argb' => 'FF660034']
  255. ],
  256. ];
  257. public static $CELDA_SECUNDARIA = [
  258. 'font' => [
  259. 'bold' => true,
  260. 'size' => 16,
  261. 'color' => ['argb' => 'FFFFFFFF'],
  262. ],
  263. 'alignment' => [
  264. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  265. ],
  266. 'fill' => [
  267. 'fillType' => Fill::FILL_SOLID,
  268. 'startColor' => ['argb' => 'FF990033']
  269. ],
  270. ];
  271. public static $TITULO_01 = [
  272. 'font' => [
  273. 'bold' => true,
  274. 'size' => 14,
  275. 'color' => ['argb' => 'FFFFFFFF'],
  276. ],
  277. ];
  278. public static $TITULO_02 = [
  279. 'font' => [
  280. 'bold' => true,
  281. 'size' => 14,
  282. 'color' => ['argb' => 'FF000000'],
  283. ],
  284. ];
  285. public static $DESCR_01 = [
  286. 'alignment' => [
  287. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  288. ],
  289. 'font' => [
  290. 'bold' => false,
  291. 'size' => 12,
  292. 'color' => ['argb' => 'FF000000'],
  293. ],
  294. ];
  295. public static $CELDA_GRIS_01 = [
  296. 'font' => [
  297. 'bold' => true,
  298. 'size' => 14,
  299. 'color' => ['argb' => 'FFFFFFFF'],
  300. ],
  301. 'fill' => [
  302. 'fillType' => Fill::FILL_SOLID,
  303. 'startColor' => ['argb' => 'FF808080']
  304. ],
  305. ];
  306. public static $CELDA_GRIS_02 = [
  307. 'fill' => [
  308. 'fillType' => Fill::FILL_SOLID,
  309. 'startColor' => ['argb' => 'FFA6A6A6']
  310. ],
  311. ];
  312. public static $CELDA_GRIS_03 = [
  313. 'fill' => [
  314. 'fillType' => Fill::FILL_SOLID,
  315. 'startColor' => ['argb' => 'FFD9D9D9']
  316. ],
  317. ];
  318. /* public function behaviors() {
  319. $behavior = parent::behaviors();
  320. $behavior["authenticator"] = [
  321. "class" => CompositeAuth::className(),
  322. "authMethods" => [
  323. QueryParamAuth::className(),
  324. ]
  325. ];
  326. return $behavior;
  327. } */
  328. public function beforeAction($action) {
  329. parent::beforeAction($action);
  330. $this->app = Yii::$app;
  331. $this->req = $this->app->getRequest();
  332. $this->res = $this->app->getResponse();
  333. $this->spreadsheet = new Spreadsheet();
  334. $this->activeSheet = $this->spreadsheet->getActiveSheet();
  335. return true;
  336. }
  337. public function obtenerHojaDeCalculo() {
  338. return $this->spreadsheet;
  339. }
  340. public function nuevaHoja($indice = null, $titulo = null) {
  341. $this->activeSheet = $this->spreadsheet->createSheet($indice);
  342. if ($titulo !== null) {
  343. $this->activeSheet->setTitle($titulo);
  344. }
  345. }
  346. public function logo($logo, $coordenada = "I1", $nombre = 'Logo', $descripcion = 'Logo', $x = 27, $y = 8, $heigth = 75) {
  347. $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
  348. $drawing->setName($nombre);
  349. $drawing->setDescription($descripcion);
  350. $drawing->setPath($logo);
  351. $drawing->setCoordinates($coordenada);
  352. $drawing->setOffsetX($x);
  353. $drawing->setOffsetY($y);
  354. $drawing->setHeight($heigth);
  355. $drawing->setWorksheet($this->activeSheet);
  356. }
  357. public function titulo($inicio = 1, $titulo, $columnaInicio = "A", $columnaFinal = "E") {
  358. if ($inicio < $this->renglonActual) {
  359. $inicio = $this->renglonActual + 1;
  360. }
  361. $renglones = [
  362. "{$columnaInicio}{$inicio}" => [
  363. "valor" => "{$titulo}",
  364. "combinar" => "{$columnaFinal}{$inicio}",
  365. "estilo" => [
  366. 'alignment' => [
  367. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  368. ],
  369. 'font' => [
  370. 'bold' => true,
  371. 'color' => ['argb' => self::COLOR_AZUL],
  372. // 'size' => 15,
  373. // 'name' => 'Verdana'
  374. ]
  375. ]
  376. ]
  377. ];
  378. foreach ($renglones as $coordenada => $valor) {
  379. $this->agregarCelda($coordenada, $valor);
  380. }
  381. $this->renglonActual = $inicio + 1;
  382. return $this;
  383. }
  384. public function agregarRenglones($renglones) {
  385. foreach ($renglones as $coordenada => $valor) {
  386. $this->agregarCelda($coordenada, $valor);
  387. }
  388. return $this;
  389. }
  390. public function agregarEncabezado($titulos) {
  391. if (count($titulos) <= 0) {
  392. return $this;
  393. }
  394. foreach ($titulos as $k => $v) {
  395. $this->agregarCelda($k, [
  396. "valor" => "$v",
  397. "estilo" => self::$estiloEncabezado
  398. ]);
  399. }
  400. return $this;
  401. }
  402. public function agregarCelda($coordenada, $valor) {
  403. if (isset($valor["valor"])) {
  404. $this->activeSheet
  405. ->setCellValue($coordenada, $valor["valor"]);
  406. if (isset($valor["combinar"])) {
  407. $coordenada = "{$coordenada}:{$valor["combinar"]}";
  408. $this->activeSheet
  409. ->mergeCells($coordenada);
  410. }
  411. }
  412. if (isset($valor["estilo"])) {
  413. $this->activeSheet
  414. ->getStyle($coordenada)
  415. ->applyFromArray($valor["estilo"]);
  416. }
  417. if (isset($valor["formato"])) {
  418. $this->activeSheet
  419. ->getStyle($coordenada)
  420. ->getNumberFormat()
  421. ->setFormatCode($valor["formato"]);
  422. }
  423. if (isset($valor["wrap"])) {
  424. $this->activeSheet
  425. ->getStyle($coordenada)
  426. ->getAlignment()
  427. ->setWrapText($valor["wrap"] || false);
  428. }
  429. }
  430. # Después de agregar toda la información elegir el ancho de las columnas
  431. public function anchoColumnas($columnas) {
  432. foreach ($columnas as $columna => $c) {
  433. if (isset($c["auto"]) && $c["auto"]) {
  434. $this->activeSheet
  435. ->getColumnDimension($columna)
  436. ->setAutoSize(true);
  437. } elseif (isset($c["ancho"]) && $c["ancho"] > 0) {
  438. $this->activeSheet->getColumnDimension($columna)->setWidth($c["ancho"]);
  439. }
  440. }
  441. return $this;
  442. }
  443. # Generar el archivo excel
  444. public function generar($filename = null) {
  445. $writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx');
  446. try {
  447. ob_start();
  448. $writer->save("php://output");
  449. $documento = ob_get_contents();
  450. ob_clean();
  451. return $documento;
  452. } catch (\Exception $exception) {
  453. return null;
  454. }
  455. }
  456. # Descarga el archivo en formato excel o pdf
  457. public function crear($hojaCalculo, $archivo = null, $tipo = self::TIPO_EXCEL) {
  458. if (!$tipo) {
  459. $tipo = \Yii::$app->getRequest()->get("tipo", "excel");
  460. }
  461. $tipo_writer = 'Xlsx';
  462. $extension = '.xlsx';
  463. if ($tipo === self::TIPO_PDF) {
  464. $tipo_writer = 'Mpdf';
  465. $extension = '.pdf';
  466. }
  467. $writer = IOFactory::createWriter($hojaCalculo, $tipo_writer);
  468. $archivo .= $extension;
  469. try {
  470. ob_start();
  471. $writer->save("php://output");
  472. $content = ob_get_contents();
  473. ob_clean();
  474. \Yii::$app->getResponse()->sendContentAsFile($content, $archivo);
  475. } catch (\Exception $e) {
  476. echo $e->getMessage();
  477. }
  478. }
  479. public function estiloCondicional() {
  480. $condicionalRojo = new Conditional();
  481. $condicionalRojo->setConditionType(Conditional::CONDITION_CELLIS)
  482. ->setOperatorType(Conditional::OPERATOR_LESSTHAN)
  483. ->addCondition('0');
  484. $colorRojo = "FFFEC7CF";
  485. $condicionalRojo->getStyle()->getFont()->setBold(true)->getColor()->setARGB(Color::COLOR_BLACK);
  486. $condicionalRojo->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  487. $condicionalRojo->getStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
  488. $condicionalRojo->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
  489. $condicionalRojo->getStyle()->getFill()->getStartColor()->setARGB($colorRojo);
  490. $condicionalRojo->getStyle()->getFill()->getEndColor()->setARGB($colorRojo);
  491. $condicionalRojo->getStyle()->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FF000000');
  492. $condicionalRojo->getStyle()->getBorders()->getBottom()->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FF000000');
  493. $condicionalRojo->getStyle()->getBorders()->getLeft()->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FF000000');
  494. $condicionalRojo->getStyle()->getBorders()->getRight()->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FF000000');
  495. $condicionalVerde = new Conditional();
  496. $condicionalVerde->setConditionType(Conditional::CONDITION_CELLIS)
  497. ->setOperatorType(Conditional::OPERATOR_GREATERTHAN)
  498. ->addCondition('0');
  499. $colorVerde = "FFC6EFCD";
  500. $condicionalVerde->getStyle()->getFont()->setBold(true)->getColor()->setARGB(Color::COLOR_BLACK);
  501. $condicionalVerde->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  502. $condicionalVerde->getStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
  503. $condicionalVerde->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
  504. $condicionalVerde->getStyle()->getFill()->getStartColor()->setARGB($colorVerde);
  505. $condicionalVerde->getStyle()->getFill()->getEndColor()->setARGB($colorVerde);
  506. $condicionalVerde->getStyle()->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FF000000');
  507. $condicionalVerde->getStyle()->getBorders()->getBottom()->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FF000000');
  508. $condicionalVerde->getStyle()->getBorders()->getLeft()->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FF000000');
  509. $condicionalVerde->getStyle()->getBorders()->getRight()->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FF000000');
  510. return [$condicionalRojo, $condicionalVerde];
  511. }
  512. }