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.

144 lines
4.3 KiB
PHTML

2 years ago
<?php
namespace App\Http\Controllers;
2 years ago
use App\Exports\ExpensesExport;
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
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
2 years ago
class ExpenseController extends Controller
{
public function index(Request $request)
2 years ago
{
$isAdmin = Auth::user()->role == User::ROLE_CASIER;
2 years ago
$today = Carbon::now();
$query = Expense::query();
if ($isAdmin) {
$query->orderBy('date_expense', 'DESC');
}
if (!$isAdmin) {
$query->where('isIncome', 0)->orderBy('created_at', 'DESC');
}
if (Auth::user()->role == User::ROLE_MANAJEMEN) {
$query->where('is_paid', Expense::IS_PAID_DRAFT);
}
2 years ago
if ($request->q != null) {
2 years ago
$query->where('name', 'like', '%'.$request->q.'%')
->orWhere('description', 'like', '%'.$request->q.'%')
2 years ago
->orWhere('job_number', 'like', '%'.$request->q.'%');
2 years ago
}
$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;
2 years ago
2 years ago
return inertia('Expense/Index', [
'expenses' => $query->paginate($limit),
'_startDate' => $startDate,
'_endDate' => $endDate,
2 years ago
'_limit' => $limit,
'_q' => $request->q
]);
}
public function store(Request $request)
{
$request->validate([
'description' => ['required'],
'date_expense' => ['required', 'date'],
'amount' => ['required', 'numeric'],
2 years ago
'is_paid' => ['required', 'in:0,1,2,3,4'],
2 years ago
'isIncome' => ['required', 'in:0,1'],
]);
2 years ago
$is_paid = $request->is_paid;
2 years ago
if ($request->isIncome === 0) {
$request->validate([
'name' => ['required'],
'job_number' => ['required'],
]);
2 years ago
} else {
$is_paid = Expense::IS_PAID_PAID;
2 years ago
}
Expense::create([
'name' => $request->name,
'description' => $request->description,
'job_number' => $request->job_number,
'date_expense' => Carbon::parse($request->date_expense)->toDateString(),
'amount' => $request->amount,
2 years ago
'is_paid' => $is_paid,
2 years ago
'isIncome' => $request->isIncome,
]);
return redirect()->route('expenses.index');
}
public function update(Request $request, Expense $expense)
{
$request->validate([
'description' => ['required'],
'date_expense' => ['required', 'date'],
'amount' => ['required', 'numeric'],
2 years ago
'is_paid' => ['required', 'in:0,1,2,3,4'],
2 years ago
'isIncome' => ['required', 'in:0,1'],
2 years ago
]);
2 years ago
2 years ago
$is_paid = $request->is_paid;
2 years ago
if ($request->isIncome === 0) {
$request->validate([
'name' => ['required'],
'job_number' => ['required'],
]);
2 years ago
} else {
$is_paid = Expense::IS_PAID_PAID;
2 years ago
}
$expense->update([
'name' => $request->name,
'description' => $request->description,
'job_number' => $request->job_number,
'date_expense' => Carbon::parse($request->date_expense)->toDateString(),
'amount' => $request->amount,
2 years ago
'is_paid' => $is_paid,
2 years ago
'isIncome' => $request->isIncome,
]);
return redirect()->route('expenses.index');
}
public function decision(Expense $expense, $status)
{
$expense->update(['is_paid' => $status]);
return redirect()->route('expenses.index');
2 years ago
}
public function destroy(Expense $expense)
{
$expense->delete();
}
2 years ago
public function export()
{
return Excel::download(new ExpensesExport, 'expenses.xlsx');
}
2 years ago
}