HerramientaInventarioController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\modules\excel\controllers;
  3. use v1\models\HerramientaInventario;
  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\Herramienta;
  12. use Yii;
  13. use yii\web\Controller;
  14. class HerramientaInventarioController extends Controller {
  15. public function actionExcelHerramientaInventario() {
  16. $request = Yii::$app->request;
  17. $inicio = $request->get('inicio');
  18. $fin = $request->get('fin');
  19. $idHerramienta = $request->get('idHerramienta');
  20. $inicioDate = $inicio ? DateTime::createFromFormat('Y-m-d', $inicio)->format('Y-m-d 00:00:00') : null;
  21. $finDate = $fin ? DateTime::createFromFormat('Y-m-d', $fin)->format('Y-m-d 23:59:59') : null;
  22. $query = HerramientaInventario::find()->where(['eliminado' => null]);
  23. if ($inicioDate) {
  24. $query->andWhere(['>=', 'creado', $inicioDate]);
  25. }
  26. if ($finDate) {
  27. $query->andWhere(['<=', 'creado', $finDate]);
  28. }
  29. if ($idHerramienta) {
  30. $query->andWhere(['idHerramienta' => $idHerramienta]);
  31. }
  32. $query->orderBy(['creado' => SORT_DESC]);
  33. $spreadsheet = new Spreadsheet();
  34. $sheet = $spreadsheet->getActiveSheet();
  35. $sheet->setTitle('Inventario de Herramientas');
  36. $BASEPATH = \Yii::getAlias('@app') . "/web";
  37. $logo = $BASEPATH . '/img/logos/edesarrollos-unicolor-azul.png';
  38. $drawing = new Drawing();
  39. $drawing->setName('Logotipo');
  40. $drawing->setDescription('Logotipo de la empresa');
  41. $drawing->setPath($logo);
  42. $drawing->setHeight(70);
  43. $drawing->setCoordinates('A1');
  44. $drawing->setOffsetX(10);
  45. $drawing->setOffsetY(10);
  46. $drawing->setWorksheet($sheet);
  47. $sheet->mergeCells('A5:J5');
  48. $sheet->setCellValue('A5', 'Inventario de Herramientas');
  49. $sheet->getStyle('A5')->getFont()->setBold(true);
  50. $sheet->getStyle('A5')->getFont()->setSize(16);
  51. $sheet->getStyle('A5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  52. $sheet->setCellValue('A6', 'Nombre');
  53. $sheet->setCellValue('B6', 'Tipo');
  54. $sheet->setCellValue('C6', 'Costo');
  55. $sheet->setCellValue('D6', 'Serie');
  56. $sheet->setCellValue('E6', 'Fecha de Compra');
  57. $sheet->setCellValue('F6', 'Hora de Compra');
  58. $sheet->setCellValue('G6', 'Estatus');
  59. $sheet->setCellValue('H6', 'Descripción');
  60. $sheet->setCellValue('I6', 'Cantidad');
  61. $sheet->setCellValue('J6', 'Fecha de Ingreso');
  62. $sheet->setCellValue('K6', 'Hora de Ingreso');
  63. $headerStyle = [
  64. 'font' => ['bold' => true],
  65. 'alignment' => [
  66. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  67. 'vertical' => Alignment::VERTICAL_CENTER,
  68. 'wrapText' => true
  69. ],
  70. 'fill' => [
  71. 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
  72. 'startColor' => ['argb' => 'FFA0A0A0']
  73. ]
  74. ];
  75. $sheet->getStyle('A6:K6')->applyFromArray($headerStyle);
  76. $herramientas = Herramienta::find()
  77. ->select(['nombre', 'id'])
  78. ->indexBy('id')
  79. ->column();
  80. $row = 7;
  81. foreach ($query->each() as $inventario) {
  82. $herramientaInfo = 'N/A';
  83. if (isset($herramientas[$inventario->idHerramienta])) {
  84. $herramientaInfo = $herramientas[$inventario->idHerramienta];
  85. }
  86. $fechaCompra = (new DateTime($inventario->herramienta->fechaCompra, new DateTimeZone('UTC')))
  87. ->setTimezone(new DateTimeZone('America/Hermosillo'))
  88. ->format('d/m/Y');
  89. $horaCompra = (new DateTime($inventario->herramienta->fechaCompra, new DateTimeZone('UTC')))
  90. ->setTimezone(new DateTimeZone('America/Hermosillo'))
  91. ->format('H:i:s');
  92. $fechaIngreso = (new DateTime($inventario->fechaIngreso, new DateTimeZone('UTC')))
  93. ->setTimezone(new DateTimeZone('America/Hermosillo'))
  94. ->format('d/m/Y');
  95. $horaIngreso = (new DateTime($inventario->fechaIngreso, new DateTimeZone('UTC')))
  96. ->setTimezone(new DateTimeZone('America/Hermosillo'))
  97. ->format('H:i:s');
  98. $sheet->setCellValue('A' . $row, $inventario->herramienta->nombre);
  99. $sheet->setCellValue('B' . $row, $inventario->herramienta->tipoHerramienta->tipo);
  100. $sheet->setCellValue('C' . $row, $inventario->herramienta->costo);
  101. $sheet->getStyle('C' . $row)->getNumberFormat()->setFormatCode('$#,##0.00');
  102. $sheet->setCellValue('D' . $row, $inventario->herramienta->serie);
  103. $sheet->setCellValue('E' . $row, $fechaCompra);
  104. $sheet->setCellValue('F' . $row, $horaCompra);
  105. $sheet->setCellValue('G' . $row, $inventario->herramienta->estatus);
  106. $sheet->setCellValue('H' . $row, $inventario->herramienta->descripcion);
  107. $sheet->setCellValue('I' . $row, $inventario->cantidad);
  108. $sheet->setCellValue('J' . $row, $fechaIngreso);
  109. $sheet->setCellValue('K' . $row, $horaIngreso);
  110. $row++;
  111. }
  112. foreach (range('A', 'K') as $columnID) {
  113. $sheet->getColumnDimension($columnID)->setWidth(20);
  114. }
  115. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  116. try {
  117. ob_start();
  118. $writer->save("php://output");
  119. $documento = ob_get_contents();
  120. ob_clean();
  121. Yii::$app->getResponse()->sendContentAsFile($documento, "Inventario_Herramientas.xlsx");
  122. } catch (\Exception $exception) {
  123. return null;
  124. }
  125. }
  126. }