feat(sale): implement modifications filtering and validation for admin sales
This commit is contained in:
@@ -92,18 +92,24 @@ class AdminAppSaleService
|
||||
return $this->salesQuery($tenant, $filters)->get();
|
||||
}
|
||||
|
||||
/** @return LengthAwarePaginator<ValueChange> */
|
||||
public function modifications(Tenant $tenant): LengthAwarePaginator
|
||||
/**
|
||||
* @param array<string, mixed> $filters
|
||||
* @return LengthAwarePaginator<ValueChange>
|
||||
*/
|
||||
public function modifications(Tenant $tenant, array $filters = []): LengthAwarePaginator
|
||||
{
|
||||
return $this->modificationsQuery($tenant)
|
||||
return $this->modificationsQuery($tenant, $filters)
|
||||
->paginateFromRequest()
|
||||
->withQueryString();
|
||||
}
|
||||
|
||||
/** @return Collection<int, ValueChange> */
|
||||
public function modificationsForExport(Tenant $tenant): Collection
|
||||
/**
|
||||
* @param array<string, mixed> $filters
|
||||
* @return Collection<int, ValueChange>
|
||||
*/
|
||||
public function modificationsForExport(Tenant $tenant, array $filters = []): Collection
|
||||
{
|
||||
return $this->modificationsQuery($tenant)->get();
|
||||
return $this->modificationsQuery($tenant, $filters)->get();
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $filters */
|
||||
@@ -159,12 +165,50 @@ class AdminAppSaleService
|
||||
->when($sortBy !== 'id', fn (Builder $query): Builder => $query->orderByDesc('id'));
|
||||
}
|
||||
|
||||
/** @return Builder<ValueChange> */
|
||||
protected function modificationsQuery(Tenant $tenant): Builder
|
||||
/**
|
||||
* @param array<string, mixed> $filters
|
||||
* @return Builder<ValueChange>
|
||||
*/
|
||||
protected function modificationsQuery(Tenant $tenant, array $filters): Builder
|
||||
{
|
||||
return ValueChange::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('trackable_type', (new Purchase)->getMorphClass())
|
||||
->when($filters['q'] ?? null, function (Builder $query, string $search): void {
|
||||
$term = trim($search);
|
||||
|
||||
$query->where(function (Builder $query) use ($term): void {
|
||||
$query
|
||||
->where('trackable_id', 'like', "%{$term}%")
|
||||
->orWhereHasMorph(
|
||||
'trackable',
|
||||
[Purchase::class],
|
||||
function (Builder $sales) use ($term): void {
|
||||
$sales
|
||||
->where('nombre_apellido', 'like', "%{$term}%")
|
||||
->orWhere('created_at', 'like', "%{$term}%");
|
||||
},
|
||||
);
|
||||
});
|
||||
})
|
||||
->when(
|
||||
$filters['id'] ?? null,
|
||||
fn (Builder $query, int $id): Builder => $query->where('trackable_id', $id)
|
||||
)
|
||||
->when(
|
||||
$filters['sale_date'] ?? null,
|
||||
fn (Builder $query, string $date): Builder => $query->whereHasMorph(
|
||||
'trackable',
|
||||
[Purchase::class],
|
||||
fn (Builder $sales): Builder => $sales->whereDate('created_at', $date),
|
||||
)
|
||||
)
|
||||
->when(
|
||||
$filters['status'] ?? null,
|
||||
fn (Builder $query, string $status): Builder => $query
|
||||
->where('attribute', 'status')
|
||||
->whereIn('new_value', Purchase::realStatusesForAdminStatus($status))
|
||||
)
|
||||
->with(['trackable', 'user'])
|
||||
->orderByDesc('changed_at')
|
||||
->orderByDesc('id');
|
||||
|
||||
Reference in New Issue
Block a user