You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.2 KiB
PHTML

1 year ago
<?php
namespace App\Http\Controllers\Admin;
1 year ago
use App\Http\Controllers\Controller;
1 year ago
use App\Models\Location;
1 year ago
use App\Models\Sale;
use Illuminate\Http\Request;
1 year ago
use Illuminate\Support\Facades\DB;
1 year ago
class SaleController extends Controller
{
1 year ago
public function index(Request $request)
1 year ago
{
1 year ago
$customer_with_deposits = Sale::with('customer')
->whereHas('customer', function ($query) {
$query->whereHas('locationFavorites', function ($query) {
$query->whereIn('id', Location::all()->pluck('id')->toArray());
});
})
->whereBetween('created_at', [now()->startOfMonth(), now()->endOfMonth()])
->where('payed_with', Sale::PAYED_WITH_DEPOSIT)
->groupBy('customer_id')
->selectRaw('SUM(amount) as total, customer_id')
->orderBy('total', 'desc')
->limit(10)
->get();
$customer_with_paylaters = Sale::with('customer')
->whereBetween('created_at', [now()->startOfMonth(), now()->endOfMonth()])
->where('payed_with', Sale::PAYED_WITH_PAYLATER)
->groupBy('customer_id')
->selectRaw('SUM(amount) as total, customer_id')
->orderBy('total', 'desc')
->limit(10)
->get();
$query = Sale::with(['items', 'customer.level'])
1 year ago
->withCount(['items']);
1 year ago
if ($request->q != '') {
$query->where('code', 'ilike', "%$request->q%")
1 year ago
->orWhereHas('customer', function ($query) use ($request) {
$query->where('name', 'ilike', "%$request->q%")
->orWhere('fullname', 'ilike', "%$request->q%")
->orWhere('username', 'ilike', "%$request->q%");
1 year ago
});
}
1 year ago
1 year ago
if ($request->location_ids != '') {
$query->whereHas('customer', function ($q) use ($request) {
$q->whereHas('locationFavorites', function ($q) use ($request) {
$q->whereIn('location_id', $request->location_ids);
});
});
}
if ($request->payment != '') {
$query->where('payed_with', $request->payment);
} else {
$query->whereIn('payed_with', [Sale::PAYED_WITH_PAYLATER, Sale::PAYED_WITH_DEPOSIT]);
}
if ($request->startDate != '' && $request->endDate != '') {
$query->whereBetween(DB::raw('DATE(created_at)'), [$request->startDate, $request->endDate]);
}
$sortBy = 'created_at';
$sortRule = 'desc';
if ($request->sortBy != '' && $request->sortRule != '') {
$sortBy = $request->sortBy;
$sortRule = $request->sortRule;
}
$query->orderBy($sortBy, $sortRule);
1 year ago
return inertia('Sale/Index', [
'query' => $query->paginate(),
1 year ago
'customer_with_deposit' => $customer_with_deposits,
'customer_with_paylaters' => $customer_with_paylaters,
'_q' => $request->q,
'_sortBy' => $sortBy,
'_sortRule' => $sortRule,
1 year ago
]);
}
public function show(Sale $sale)
{
return inertia('Sale/Detail', [
'sale' => $sale->load(['items', 'customer.level']),
1 year ago
]);
}
}