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.

107 lines
4.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use DB;
use Illuminate\Http\Request;
use App\Models\Transaction;
use App\Models\Category;
class ExpenseController extends Controller
{
public function index()
{
return inertia('Transaction', [
'transactions' => Transaction::with(['category:name,id'])->orderBy('date', 'desc')->paginate(10),
'categories' => Category::all()
]);
}
public function store(Request $request)
{
$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());
if ($request->is_income == 0 && $request->income_type == 0) { //total expense nambah
$budget = $transaction->category->budgets()->whereDate('end_date', now()->endOfMonth()->toDateString())->first();
$budget->update(['total_used' => $budget->total_used + $request->amount]);
}
if ($request->is_income == 0 && $request->income_type == 1) { //total remain nambah
$budget = $transaction->category->budgets()->whereDate('end_date', now()->endOfMonth()->toDateString())->first();
$budget->update(['remain' => $budget->remain + $request->amount]);
}
DB::commit();
return redirect()->route('transactions');
}
public function update(Request $request, Transaction $transaction)
{
$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();
// return when it create
if ($transaction->is_income == 0 && $transaction->income_type == 0) { //total expense nambah
$budget = $transaction->category->budgets()->whereDate('end_date', now()->endOfMonth()->toDateString())->first();
$budget->update(['total_used' => $budget->total_used - $transaction->amount]);
}
if ($transaction->is_income == 0 && $transaction->income_type == 1) { //total remain nambah
$budget = $transaction->category->budgets()->whereDate('end_date', now()->endOfMonth()->toDateString())->first();
$budget->update(['remain' => $budget->remain - $transaction->amount]);
}
$transaction->update($request->input());
// add new
if ($request->is_income == 0 && $request->income_type == 0) { //total expense nambah
$budget = $transaction->category->budgets()->whereDate('end_date', now()->endOfMonth()->toDateString())->first();
$budget->update(['total_used' => $budget->total_used + $request->amount]);
}
if ($request->is_income == 0 && $request->income_type == 1) { //total remain nambah
$budget = $transaction->category->budgets()->whereDate('end_date', now()->endOfMonth()->toDateString())->first();
$budget->update(['remain' => $budget->remain + $request->amount]);
}
DB::commit();
return redirect()->route('transactions');
}
public function destroy(Transaction $transaction)
{
DB::beginTransaction();
// return when it create
if ($transaction->is_income == 0 && $transaction->income_type == 0) { //total expense nambah
$budget = $transaction->category->budgets()->whereDate('end_date', now()->endOfMonth()->toDateString())->first();
$budget->update(['total_used' => $budget->total_used - $transaction->amount]);
}
if ($transaction->is_income == 0 && $transaction->income_type == 1) { //total remain nambah
$budget = $transaction->category->budgets()->whereDate('end_date', now()->endOfMonth()->toDateString())->first();
$budget->update(['remain' => $budget->remain - $transaction->amount]);
}
$transaction->delete();
DB::commit();
return redirect()->route('transactions');
}
}