feat: Implement sale management features including controllers, services, resources, and routes; add PDF generation for sales and modifications
This commit is contained in:
68
app/Domains/Sale/Services/AdminAppSalePdfService.php
Normal file
68
app/Domains/Sale/Services/AdminAppSalePdfService.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sale\Services;
|
||||
|
||||
use App\Domains\Logging\Models\ValueChange;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Barryvdh\DomPDF\PDF as DomPdf;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class AdminAppSalePdfService
|
||||
{
|
||||
/** @param Collection<int, Purchase> $sales */
|
||||
public function downloadSales(Tenant $tenant, Collection $sales): Response
|
||||
{
|
||||
$pdf = Pdf::loadView('pdf.adminapp.sales', [
|
||||
'tenant' => $tenant,
|
||||
'sales' => $sales,
|
||||
'generatedAt' => now(),
|
||||
'confirmedSalesTotal' => number_format(
|
||||
(float) $sales->where('status', Purchase::STATUS_PAID)->sum('total'),
|
||||
2,
|
||||
'.',
|
||||
'',
|
||||
),
|
||||
])->setPaper('a4', 'landscape');
|
||||
|
||||
$this->addPageNumbers($pdf);
|
||||
|
||||
return $pdf->download(
|
||||
'ventas_'.$tenant->codigo.'_'.now()->format('Ymd_His').'.pdf'
|
||||
);
|
||||
}
|
||||
|
||||
/** @param Collection<int, ValueChange> $modifications */
|
||||
public function downloadModifications(Tenant $tenant, Collection $modifications): Response
|
||||
{
|
||||
$pdf = Pdf::loadView('pdf.adminapp.sale-modifications', [
|
||||
'tenant' => $tenant,
|
||||
'modifications' => $modifications,
|
||||
'generatedAt' => now(),
|
||||
])->setPaper('a4', 'landscape');
|
||||
|
||||
$this->addPageNumbers($pdf);
|
||||
|
||||
return $pdf->download(
|
||||
'historial_modificaciones_'.$tenant->codigo.'_'.now()->format('Ymd_His').'.pdf'
|
||||
);
|
||||
}
|
||||
|
||||
private function addPageNumbers(DomPdf $pdf): void
|
||||
{
|
||||
$pdf->render();
|
||||
$domPdf = $pdf->getDomPDF();
|
||||
$font = $domPdf->getFontMetrics()->getFont('DejaVu Sans');
|
||||
|
||||
$domPdf->getCanvas()->page_text(
|
||||
385,
|
||||
575,
|
||||
'Página {PAGE_NUM} de {PAGE_COUNT}',
|
||||
$font,
|
||||
7,
|
||||
[0.48, 0.52, 0.49],
|
||||
);
|
||||
}
|
||||
}
|
||||
120
app/Domains/Sale/Services/AdminAppSaleService.php
Normal file
120
app/Domains/Sale/Services/AdminAppSaleService.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sale\Services;
|
||||
|
||||
use App\Domains\Logging\Models\ValueChange;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class AdminAppSaleService
|
||||
{
|
||||
public function confirmedSalesTotal(Tenant $tenant): string
|
||||
{
|
||||
$total = Purchase::query()
|
||||
->where('tenant_codigo', $tenant->codigo)
|
||||
->where('status', Purchase::STATUS_PAID)
|
||||
->sum('total');
|
||||
|
||||
return number_format((float) $total, 2, '.', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{
|
||||
* q?: string|null,
|
||||
* id?: int|null,
|
||||
* sale_date?: string|null,
|
||||
* status?: string|null,
|
||||
* sort_by?: string,
|
||||
* sort_direction?: string
|
||||
* } $filters
|
||||
* @return LengthAwarePaginator<Purchase>
|
||||
*/
|
||||
public function sales(Tenant $tenant, array $filters = []): LengthAwarePaginator
|
||||
{
|
||||
return $this->salesQuery($tenant, $filters)
|
||||
->paginateFromRequest()
|
||||
->withQueryString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $filters
|
||||
* @return Collection<int, Purchase>
|
||||
*/
|
||||
public function salesForExport(Tenant $tenant, array $filters = []): Collection
|
||||
{
|
||||
return $this->salesQuery($tenant, $filters)->get();
|
||||
}
|
||||
|
||||
/** @return LengthAwarePaginator<ValueChange> */
|
||||
public function modifications(Tenant $tenant): LengthAwarePaginator
|
||||
{
|
||||
return $this->modificationsQuery($tenant)
|
||||
->paginateFromRequest()
|
||||
->withQueryString();
|
||||
}
|
||||
|
||||
/** @return Collection<int, ValueChange> */
|
||||
public function modificationsForExport(Tenant $tenant): Collection
|
||||
{
|
||||
return $this->modificationsQuery($tenant)->get();
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $filters */
|
||||
protected function salesQuery(Tenant $tenant, array $filters): Builder
|
||||
{
|
||||
$sortColumns = [
|
||||
'id' => 'id',
|
||||
'date' => 'created_at',
|
||||
'customer_name' => 'nombre_apellido',
|
||||
'quantity' => 'quantity',
|
||||
'status' => 'status',
|
||||
'total' => 'total',
|
||||
];
|
||||
$requestedSort = $filters['sort_by'] ?? 'date';
|
||||
$sortBy = array_key_exists($requestedSort, $sortColumns) ? $requestedSort : 'date';
|
||||
$requestedDirection = $filters['sort_direction'] ?? 'desc';
|
||||
$sortDirection = in_array($requestedDirection, ['asc', 'desc'], true)
|
||||
? $requestedDirection
|
||||
: 'desc';
|
||||
|
||||
return Purchase::query()
|
||||
->where('tenant_codigo', $tenant->codigo)
|
||||
->when($filters['q'] ?? null, function (Builder $query, string $search): void {
|
||||
$term = trim($search);
|
||||
|
||||
$query->where(function (Builder $query) use ($term): void {
|
||||
$query
|
||||
->where('id', 'like', "%{$term}%")
|
||||
->orWhere('nombre_apellido', 'like', "%{$term}%")
|
||||
->orWhere('created_at', 'like', "%{$term}%");
|
||||
});
|
||||
})
|
||||
->when($filters['id'] ?? null, fn (Builder $query, int $id): Builder => $query->whereKey($id))
|
||||
->when(
|
||||
$filters['sale_date'] ?? null,
|
||||
fn (Builder $query, string $date): Builder => $query->whereDate('created_at', $date)
|
||||
)
|
||||
->when(
|
||||
$filters['status'] ?? null,
|
||||
fn (Builder $query, string $status): Builder => $query->where('status', $status)
|
||||
)
|
||||
->withSum('items as quantity', 'cantidad')
|
||||
->withCount('tickets')
|
||||
->orderBy($sortColumns[$sortBy], $sortDirection)
|
||||
->when($sortBy !== 'id', fn (Builder $query): Builder => $query->orderByDesc('id'));
|
||||
}
|
||||
|
||||
/** @return Builder<ValueChange> */
|
||||
protected function modificationsQuery(Tenant $tenant): Builder
|
||||
{
|
||||
return ValueChange::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('trackable_type', (new Purchase)->getMorphClass())
|
||||
->with(['trackable', 'user'])
|
||||
->orderByDesc('changed_at')
|
||||
->orderByDesc('id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user