FIX: Exportar Reporte de corte de caja
This commit is contained in:
parent
90f3550d64
commit
7486ee089a
114
app/Exports/CashCloseReportExport.php
Normal file
114
app/Exports/CashCloseReportExport.php
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exports;
|
||||||
|
|
||||||
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||||
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||||
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||||
|
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
|
use App\Models\SaleItem;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class CashCloseReportExport implements FromCollection, WithHeadings, WithStyles, WithTitle
|
||||||
|
{
|
||||||
|
protected $cashCloses;
|
||||||
|
protected $cashCloseIds;
|
||||||
|
|
||||||
|
public function __construct($cashCloses)
|
||||||
|
{
|
||||||
|
$this->cashCloses = $cashCloses;
|
||||||
|
$this->cashCloseIds = $cashCloses->pluck('id')->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function collection()
|
||||||
|
{
|
||||||
|
$data = collect();
|
||||||
|
|
||||||
|
// RESUMEN FINANCIERO
|
||||||
|
$totalIncome = $this->cashCloses->sum('income');
|
||||||
|
$totalExit = $this->cashCloses->sum('exit');
|
||||||
|
$totalCash = $this->cashCloses->sum('income_cash');
|
||||||
|
$totalCard = $this->cashCloses->sum('income_card');
|
||||||
|
$totalTransfer = $this->cashCloses->sum('income_transfer');
|
||||||
|
$balanceFinal = $this->cashCloses->sum('initial_balance') + $totalIncome - $totalExit;
|
||||||
|
|
||||||
|
$data->push(['RESUMEN FINANCIERO', '', '', '', '', '']);
|
||||||
|
$data->push(['Periodo Inicio', $this->cashCloses->last()?->opened_at, '', '', '', '']);
|
||||||
|
$data->push(['Periodo Fin', $this->cashCloses->first()?->closed_at, '', '', '', '']);
|
||||||
|
$data->push(['Total Ventas', $totalIncome, '', '', '', '']);
|
||||||
|
$data->push(['Efectivo', $totalCash, '', '', '', '']);
|
||||||
|
$data->push(['Tarjeta', $totalCard, '', '', '', '']);
|
||||||
|
$data->push(['Transferencia', $totalTransfer, '', '', '', '']);
|
||||||
|
$data->push(['Egresos', $totalExit, '', '', '', '']);
|
||||||
|
$data->push(['Balance Final', $balanceFinal, '', '', '', '']);
|
||||||
|
$data->push(['', '', '', '', '', '']); // Espacios
|
||||||
|
|
||||||
|
// ESTADÍSTICAS POR PAQUETE
|
||||||
|
$packageStats = DB::table('sale_items')
|
||||||
|
->join('sales', 'sale_items.sale_id', '=', 'sales.id')
|
||||||
|
->join('packages', 'sale_items.package_id', '=', 'packages.id')
|
||||||
|
->whereIn('sales.cash_close_id', $this->cashCloseIds)
|
||||||
|
->select(
|
||||||
|
'packages.name as paquete',
|
||||||
|
DB::raw('COUNT(*) as total_vendidos'),
|
||||||
|
DB::raw('SUM(packages.price) as total_ingresos')
|
||||||
|
)
|
||||||
|
->groupBy('packages.id', 'packages.name')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$data->push(['VENTAS POR PAQUETE', '', '', '', '', '']);
|
||||||
|
$data->push(['Paquete', 'Total Vendidos', 'Total Ingresos', '', '', '']);
|
||||||
|
foreach ($packageStats as $stat) {
|
||||||
|
$data->push([$stat->paquete, $stat->total_vendidos, $stat->total_ingresos, '', '', '']);
|
||||||
|
}
|
||||||
|
$data->push(['', '', '', '', '', '']); // Espacios
|
||||||
|
|
||||||
|
// VENTAS DETALLADAS
|
||||||
|
$detailedSales = SaleItem::whereHas('sale', function ($query) {
|
||||||
|
$query->whereIn('cash_close_id', $this->cashCloseIds);
|
||||||
|
})
|
||||||
|
->with([
|
||||||
|
'sale.client:id,name,paternal,maternal',
|
||||||
|
'sale:id,client_id,payment_method',
|
||||||
|
'simCard:id,iccid,msisdn',
|
||||||
|
'package:id,name,price'
|
||||||
|
])
|
||||||
|
->orderBy('id', 'asc')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$data->push(['VENTAS DETALLADAS', '', '', '', '', '']);
|
||||||
|
$data->push(['Nombre Comprador', 'ID SIM', 'Número Asignado', 'Paquete', 'Costo', 'Medio de Pago']);
|
||||||
|
|
||||||
|
foreach ($detailedSales as $item) {
|
||||||
|
$data->push([
|
||||||
|
$item->sale->client->full_name,
|
||||||
|
$item->simCard->iccid,
|
||||||
|
$item->simCard->msisdn,
|
||||||
|
$item->package->name,
|
||||||
|
$item->package->price,
|
||||||
|
ucfirst($item->sale->payment_method)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function headings(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function title(): string
|
||||||
|
{
|
||||||
|
return 'Reporte Corte de Caja';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function styles(Worksheet $sheet)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
1 => ['font' => ['bold' => true, 'size' => 14]],
|
||||||
|
11 => ['font' => ['bold' => true, 'size' => 14]],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,7 +9,8 @@
|
|||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Notsoweb\ApiResponse\Enums\ApiResponse;
|
use Notsoweb\ApiResponse\Enums\ApiResponse;
|
||||||
use phpseclib3\Crypt\RC2;
|
use App\Exports\CashCloseReportExport;
|
||||||
|
use Maatwebsite\Excel\Facades\Excel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -210,11 +211,11 @@ public function exportReport(Request $request)
|
|||||||
$query = CashClose::with('user:id,name')->withCount('sales');
|
$query = CashClose::with('user:id,name')->withCount('sales');
|
||||||
|
|
||||||
if ($request->has('start_date') && $request->has('end_date')) {
|
if ($request->has('start_date') && $request->has('end_date')) {
|
||||||
$query->whereBetween('close_at', [$request->start_date, $request->end_date]);
|
$query->whereBetween('closed_at', [$request->start_date, $request->end_date]);
|
||||||
} elseif ($request->has('start_date')) {
|
} elseif ($request->has('start_date')) {
|
||||||
$query->whereDate('close_at', '>=', $request->start_date);
|
$query->whereDate('closed_at', '>=', $request->start_date);
|
||||||
} elseif ($request->has('end_date')) {
|
} elseif ($request->has('end_date')) {
|
||||||
$query->whereDate('close_at', '<=', $request->end_date);
|
$query->whereDate('closed_at', '<=', $request->end_date);
|
||||||
} else {
|
} else {
|
||||||
$query->closed()->orderBy('id', 'desc')->limit(1);
|
$query->closed()->orderBy('id', 'desc')->limit(1);
|
||||||
}
|
}
|
||||||
@ -227,70 +228,11 @@ public function exportReport(Request $request)
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$cashCloseIds = $cashCloses->pluck('id')->toArray();
|
$filename = 'reporte_corte_caja_' . date('Y-m-d_His') . '.xlsx';
|
||||||
|
|
||||||
// Obtener ventas detalladas
|
return Excel::download(
|
||||||
$detailedSales = SaleItem::whereHas('sale', function ($query) use ($cashCloseIds) {
|
new CashCloseReportExport($cashCloses),
|
||||||
$query->whereIn('cash_close_id', $cashCloseIds);
|
$filename
|
||||||
})
|
);
|
||||||
->with([
|
|
||||||
'sale.client:id,name,paternal,maternal',
|
|
||||||
'sale:id,client_id,payment_method',
|
|
||||||
'simCard:id,iccid,msisdn',
|
|
||||||
'package:id,name,price'
|
|
||||||
])
|
|
||||||
->orderBy('id', 'asc')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
// Calcular totales
|
|
||||||
$totalIncome = $cashCloses->sum('income');
|
|
||||||
$totalExit = $cashCloses->sum('exit');
|
|
||||||
$totalCash = $cashCloses->sum('income_cash');
|
|
||||||
$totalCard = $cashCloses->sum('income_card');
|
|
||||||
$totalTransfer = $cashCloses->sum('income_transfer');
|
|
||||||
|
|
||||||
// Crear el CSV
|
|
||||||
$filename = 'reporte_corte_caja_' . date('Y-m-d_His') . '.csv';
|
|
||||||
|
|
||||||
$headers = [
|
|
||||||
'Content-Type' => 'text/csv; charset=UTF-8',
|
|
||||||
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
|
|
||||||
];
|
|
||||||
|
|
||||||
$callback = function () use ($cashCloses, $detailedSales, $totalIncome, $totalExit, $totalCash, $totalCard, $totalTransfer) {
|
|
||||||
$file = fopen('php://output', 'w');
|
|
||||||
|
|
||||||
fprintf($file, chr(0xEF) . chr(0xBB) . chr(0xBF));
|
|
||||||
|
|
||||||
// RESUMEN FINANCIERO
|
|
||||||
fputcsv($file, ['RESUMEN FINANCIERO', '']);
|
|
||||||
fputcsv($file, ['Periodo Inicio', $cashCloses->last()?->opened_at]);
|
|
||||||
fputcsv($file, ['Periodo Fin', $cashCloses->first()?->closed_at]);
|
|
||||||
fputcsv($file, ['Total Ventas', number_format($totalIncome, 2)]);
|
|
||||||
fputcsv($file, ['Efectivo', number_format($totalCash, 2)]);
|
|
||||||
fputcsv($file, ['Tarjeta', number_format($totalCard, 2)]);
|
|
||||||
fputcsv($file, ['Transferencia', number_format($totalTransfer, 2)]);
|
|
||||||
fputcsv($file, ['Egresos', number_format($totalExit, 2)]);
|
|
||||||
fputcsv($file, []);
|
|
||||||
|
|
||||||
// VENTAS DETALLADAS
|
|
||||||
fputcsv($file, ['VENTAS DETALLADAS']);
|
|
||||||
fputcsv($file, ['Nombre Comprador', 'ID SIM', 'Número Asignado', 'Paquete', 'Costo', 'Medio de Pago']);
|
|
||||||
|
|
||||||
foreach ($detailedSales as $item) {
|
|
||||||
fputcsv($file, [
|
|
||||||
$item->sale->client->full_name,
|
|
||||||
"'" . $item->simCard->iccid . "'",
|
|
||||||
"'" . $item->simCard->msisdn . "'",
|
|
||||||
$item->package->name,
|
|
||||||
number_format($item->package->price, 2),
|
|
||||||
ucfirst($item->sale->payment_method)
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose($file);
|
|
||||||
};
|
|
||||||
|
|
||||||
return response()->stream($callback, 200, $headers);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user