up
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\FromArray;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||
use Maatwebsite\Excel\Concerns\WithColumnWidths;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||
|
||||
class PlantillaCompraExport implements FromArray, WithHeadings, WithStyles, WithColumnWidths, WithTitle
|
||||
{
|
||||
/**
|
||||
* Datos de ejemplo para la plantilla
|
||||
*/
|
||||
public function array(): array
|
||||
{
|
||||
return [
|
||||
['7501234567890', 'Balde 15 L', 10, 50000, 'Principal', 'Si codigo existe, usa ese producto'],
|
||||
['', 'Producto Nuevo Sin Codigo', 5, 75000, 'Principal', 'Si no hay codigo, busca por nombre'],
|
||||
['9999999999999', 'Escoba Verde', 20, 25000, 'Principal', 'Codigo nuevo - crea producto con ese nombre'],
|
||||
['', '', '', '', '', ''],
|
||||
['', 'INSTRUCCIONES:', '', '', '', ''],
|
||||
['', '1. Si tiene codigo: busca por codigo primero', '', '', '', ''],
|
||||
['', '2. Si no hay codigo: busca por nombre', '', '', '', ''],
|
||||
['', '3. Si no existe: crea nuevo producto', '', '', '', ''],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Encabezados de las columnas
|
||||
*/
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'Codigo de Barras',
|
||||
'Producto',
|
||||
'Cantidad',
|
||||
'Precio Unitario',
|
||||
'Bodega',
|
||||
'Observaciones',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Estilos de la hoja
|
||||
*/
|
||||
public function styles(Worksheet $sheet)
|
||||
{
|
||||
return [
|
||||
// Estilo para la fila de encabezados
|
||||
1 => [
|
||||
'font' => [
|
||||
'bold' => true,
|
||||
'size' => 12,
|
||||
'color' => ['rgb' => 'FFFFFF'],
|
||||
],
|
||||
'fill' => [
|
||||
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
|
||||
'startColor' => ['rgb' => '4472C4'],
|
||||
],
|
||||
'alignment' => [
|
||||
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
|
||||
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
|
||||
],
|
||||
],
|
||||
// Filas de datos
|
||||
'2:8' => [
|
||||
'alignment' => [
|
||||
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
|
||||
],
|
||||
],
|
||||
// Filas de instrucciones
|
||||
'5:8' => [
|
||||
'font' => [
|
||||
'italic' => true,
|
||||
'color' => ['rgb' => '666666'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ancho de las columnas
|
||||
*/
|
||||
public function columnWidths(): array
|
||||
{
|
||||
return [
|
||||
'A' => 20, // Codigo de Barras
|
||||
'B' => 25, // Producto
|
||||
'C' => 12, // Cantidad
|
||||
'D' => 18, // Precio Unitario
|
||||
'E' => 15, // Bodega
|
||||
'F' => 35, // Observaciones
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Título de la hoja
|
||||
*/
|
||||
public function title(): string
|
||||
{
|
||||
return 'Plantilla Compras';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use App\Models\Producto;
|
||||
use App\Models\Categoria;
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
|
||||
class ProductosExport implements WithMultipleSheets
|
||||
{
|
||||
/**
|
||||
* Retorna las hojas del Excel
|
||||
*/
|
||||
public function sheets(): array
|
||||
{
|
||||
return [
|
||||
new ProductosSheet(),
|
||||
new CategoriasReferenciaSheet(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hoja de productos
|
||||
*/
|
||||
class ProductosSheet implements
|
||||
\Maatwebsite\Excel\Concerns\FromCollection,
|
||||
\Maatwebsite\Excel\Concerns\WithHeadings,
|
||||
\Maatwebsite\Excel\Concerns\WithMapping,
|
||||
\Maatwebsite\Excel\Concerns\WithStyles,
|
||||
\Maatwebsite\Excel\Concerns\ShouldAutoSize,
|
||||
\Maatwebsite\Excel\Concerns\WithTitle
|
||||
{
|
||||
/**
|
||||
* Retorna la colección de productos a exportar
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
return Producto::with('categoria')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define los encabezados del Excel
|
||||
*/
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'ID',
|
||||
'Nombre',
|
||||
'Descripcion',
|
||||
'Codigo_de_Barras',
|
||||
'Categoria',
|
||||
'Precio_Compra',
|
||||
'Precio_Venta',
|
||||
'Stock',
|
||||
'Stock_Minimo',
|
||||
'Stock_Maximo',
|
||||
'Unidad_de_Medida',
|
||||
'Estado',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapea cada producto a una fila del Excel
|
||||
*/
|
||||
public function map($producto): array
|
||||
{
|
||||
return [
|
||||
$producto->id,
|
||||
$producto->nombre,
|
||||
$producto->descripcion,
|
||||
$producto->codigo_barras,
|
||||
$producto->categoria?->nombre ?? '',
|
||||
$producto->precio_compra,
|
||||
$producto->precio_venta,
|
||||
$producto->getStockEfectivo(),
|
||||
$producto->stock_minimo,
|
||||
$producto->stock_maximo,
|
||||
$producto->unidad_medida ?? 'unidad',
|
||||
$producto->estado ? 'Activo' : 'Inactivo',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Aplica estilos al Excel
|
||||
*/
|
||||
public function styles(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet)
|
||||
{
|
||||
return [
|
||||
// Estilo para la fila de encabezados
|
||||
1 => [
|
||||
'font' => ['bold' => true],
|
||||
'fill' => [
|
||||
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
|
||||
'startColor' => ['rgb' => '4F46E5']
|
||||
],
|
||||
'font' => [
|
||||
'bold' => true,
|
||||
'color' => ['rgb' => 'FFFFFF']
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Título de la hoja
|
||||
*/
|
||||
public function title(): string
|
||||
{
|
||||
return 'Productos';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hoja de referencia de categorías
|
||||
*/
|
||||
class CategoriasReferenciaSheet implements
|
||||
\Maatwebsite\Excel\Concerns\FromCollection,
|
||||
\Maatwebsite\Excel\Concerns\WithHeadings,
|
||||
\Maatwebsite\Excel\Concerns\WithMapping,
|
||||
\Maatwebsite\Excel\Concerns\WithStyles,
|
||||
\Maatwebsite\Excel\Concerns\ShouldAutoSize,
|
||||
\Maatwebsite\Excel\Concerns\WithTitle
|
||||
{
|
||||
/**
|
||||
* Retorna la colección de categorías
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
return Categoria::all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define los encabezados
|
||||
*/
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'ID',
|
||||
'Nombre_de_Categoria',
|
||||
'Descripcion',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapea cada categoría a una fila
|
||||
*/
|
||||
public function map($categoria): array
|
||||
{
|
||||
return [
|
||||
$categoria->id,
|
||||
$categoria->nombre,
|
||||
'Usa este nombre en la columna "Categoria" de la hoja Productos',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Aplica estilos
|
||||
*/
|
||||
public function styles(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet)
|
||||
{
|
||||
return [
|
||||
1 => [
|
||||
'font' => ['bold' => true],
|
||||
'fill' => [
|
||||
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
|
||||
'startColor' => ['rgb' => '10B981']
|
||||
],
|
||||
'font' => [
|
||||
'bold' => true,
|
||||
'color' => ['rgb' => 'FFFFFF']
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Título de la hoja
|
||||
*/
|
||||
public function title(): string
|
||||
{
|
||||
return 'Categorías Disponibles';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user