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.

143 lines
4.8 KiB
PHTML

<?php
namespace App\Http\Controllers;
3 years ago
use DB;
use Illuminate\Http\Request;
3 years ago
use App\Models\Transaction;
use App\Models\Category;
3 years ago
use App\Models\Budget;
class ExpenseController extends Controller
{
3 years ago
public function index(Request $request)
{
3 years ago
$query = Transaction::with(['category:name,id']);
if ($request->q != null) {
$query->where('description', 'like', '%'.$request->q.'%');
}
3 years ago
$startDate = now()->startOfMonth()->toDateString();
$endDate = now()->endOfMonth()->toDateString();
3 years ago
if ($request->startDate != null && $request->endDate != null) {
3 years ago
$startDate = $request->startDate;
$endDate = $request->endDate;
3 years ago
}
3 years ago
$query->whereBetween('date', [$startDate, $endDate]);
3 years ago
return inertia('Transaction', [
3 years ago
'transactions' => $query->orderBy('date', 'desc')->paginate(10),
'_search' => $request->q,
3 years ago
'_startDate' => $startDate,
'_endDate' => $endDate
3 years ago
]);
}
3 years ago
3 years ago
public function store(Request $request)
3 years ago
{
3 years ago
$request->validate([
'category_id' => 'nullable|required_if:is_income,0|exists:categories,id',
'date' => 'required|date',
'amount' => 'required|numeric|max:999999999|min:1',
'description' => 'nullable|string',
'is_income' => 'required|in:0,1', // 0 expense, 1 income
'income_type' => 'required|in:0,1' // 0 cash out , 1 cash in
]);
DB::beginTransaction();
$transaction = Transaction::create($request->input());
3 years ago
if ($request->is_income == 0) {
$budget = $transaction->category->budgets()->where('end_date', null)->first();
3 years ago
$budget->update(['total_used' => $budget->total_used + $request->amount]);
if ($request->income_type == 1) {
$budget->update([
'remain' => $budget->remain + $request->amount
]);
} else {
$budget->update([
'remain' => $budget->remain - $request->amount
]);
}
3 years ago
}
DB::commit();
return redirect()->route('transactions');
3 years ago
}
3 years ago
public function update(Request $request, Transaction $transaction)
3 years ago
{
3 years ago
$request->validate([
'category_id' => 'nullable|required_if:is_income,0|exists:categories,id',
'date' => 'required|date',
'amount' => 'required|numeric|max:999999999|min:1',
'description' => 'nullable|string',
'is_income' => 'required|in:0,1', // 0 expense, 1 income
'income_type' => 'required|in:0,1' // 0 cash out , 1 cash in
]);
DB::beginTransaction();
3 years ago
if ($transaction->is_income == 0) {
3 years ago
$budget = $transaction->category->budgets()->where('end_date', null)->first();
3 years ago
$budget->update(['total_used' => $budget->total_used - $transaction->amount]);
if ($transaction->income_type == 1) {
$budget->update([
'remain' => $budget->remain - $transaction->amount
]);
} else {
$budget->update([
'remain' => $budget->remain + $transaction->amount
]);
}
3 years ago
}
$transaction->update($request->input());
3 years ago
if ($transaction->is_income == 0) {
3 years ago
$budget = Budget::where([
['end_date', '=', null],
['category_id', '=', $transaction->category_id]
])->first();
3 years ago
$budget->update(['total_used' => $budget->total_used + $request->amount]);
if ($request->income_type == 1) {
$budget->update([
'remain' => $budget->remain + $request->amount
]);
} else {
$budget->update([
'remain' => $budget->remain - $request->amount
]);
}
3 years ago
}
DB::commit();
return redirect()->route('transactions');
3 years ago
}
3 years ago
public function destroy(Transaction $transaction)
3 years ago
{
3 years ago
DB::beginTransaction();
3 years ago
if ($transaction->is_income == 0 && $transaction->category->deleted_at == null) {
3 years ago
$budget = $transaction->category->budgets()->where('end_date', null)->first();
3 years ago
$budget->update(['total_used' => $budget->total_used - $transaction->amount]);
if ($transaction->income_type == 1) {
$budget->update([
'remain' => $budget->remain - $transaction->amount
]);
} else {
$budget->update([
'remain' => $budget->remain + $transaction->amount
]);
}
3 years ago
}
$transaction->delete();
DB::commit();
return redirect()->route('transactions');
3 years ago
}
}