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.

60 lines
1.7 KiB
PHTML

2 years ago
<?php
namespace App\Http\Controllers;
2 years ago
use App\Models\Expense;
use App\Models\User;
2 years ago
use Illuminate\Http\Request;
2 years ago
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
2 years ago
class ExpenseController extends Controller
{
public function index(Request $request)
2 years ago
{
$isAdmin = Auth::user()->role === User::ROLE_CASIER;
$today = Carbon::now();
$query = Expense::query();
if ($isAdmin) {
$query->orderBy('date_expense', 'DESC');
}
if (!$isAdmin) {
$query->where('isIncome', 0)->orderBy('created_at', 'DESC');
}
if ($request->q) {
$query->where('name', 'like', '%'.$request->q.'%')
->orWhere('description', 'like', '%'.$request->q.'%')
->orWhere('job_number', 'like', '%'.$request->q.'%')
->orWhere('amount', 'like', '%'.$request->q.'%');
}
$endDate = Carbon::now()->toDateString();
$startDate = $today->subDays(30)->toDateString();
2 years ago
if ($request->startDate != null && $request->endDate != null) {
$startDate = Carbon::parse($request->startDate)->toDateString();
$endDate = Carbon::parse($request->endDate)->toDateString();
2 years ago
}
$query->whereDate('date_expense', '<=', $endDate)
->whereDate('date_expense', '>=', $startDate);
2 years ago
$limit = $request->limit ? $request->limit : 10;
return inertia('Expense/Index', [
'expenses' => $query->paginate($limit),
'_startDate' => $startDate,
'_endDate' => $endDate,
2 years ago
'_limit' => $limit
]);
}
public function destroy(Expense $expense)
{
$expense->delete();
}
2 years ago
}