improved some query
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use App\Helpers\BudgetHelper;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\BudgetControl;
|
||||
use App\Models\BudgetControlLog;
|
||||
@@ -23,8 +24,6 @@ class BudgetControlController extends Controller
|
||||
$this->checkAuthorization(auth()->user(), ['budget_control.view']);
|
||||
session()->put('redirect_url', route('budget_control'));
|
||||
|
||||
// Get the budget control data
|
||||
// $budget = BudgetControl::orderBy('created_at', 'desc')->with(['user', 'cabang'])->get();
|
||||
$role = auth()->user()->getRoleNames()[0];
|
||||
$budget = null;
|
||||
|
||||
@@ -32,72 +31,32 @@ class BudgetControlController extends Controller
|
||||
$region_id = auth()->user()->getMyCabangAndRegionId()['region'];
|
||||
|
||||
if ($region_id) {
|
||||
// Get budget controls where the region matches
|
||||
$budget = BudgetControl::whereHas('cabang', function ($query) use ($region_id) {
|
||||
$query->where('region_id', $region_id);
|
||||
})->get();
|
||||
})->with(['receiver', 'sender', 'cabang'])->get();
|
||||
}
|
||||
} elseif ($role == 'Area Manager Cabang') {
|
||||
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'];
|
||||
|
||||
if ($cabang_id) {
|
||||
// Get budget controls where the cabang matches
|
||||
$budget = BudgetControl::where('cabang_id', $cabang_id)->get();
|
||||
$budget = BudgetControl::where('cabang_id', $cabang_id)
|
||||
->with(['receiver', 'sender', 'cabang'])
|
||||
->get();
|
||||
}
|
||||
} else {
|
||||
// Get all budget controls for other roles
|
||||
$budget = BudgetControl::all();
|
||||
$budget = BudgetControl::with(['receiver', 'sender', 'cabang'])->get();
|
||||
}
|
||||
|
||||
// Initialize an empty array to hold the used budget sums
|
||||
$usedBudget = [];
|
||||
// Calculate usedBudget and availableBudget for each cabang
|
||||
$budget->map(function ($b) {
|
||||
$b->availableBudget = BudgetHelper::getAvailableBudget($b->cabang_id);
|
||||
|
||||
foreach ($budget as $b) {
|
||||
// Get the month and year from the budget
|
||||
$month = $b->periode_month;
|
||||
$year = $b->periode_year;
|
||||
return $b;
|
||||
});
|
||||
|
||||
// Add the used budget per model for the specified month and year to the usedBudget array
|
||||
$usedBudget[] = [
|
||||
'FormUpCountry' => FormUpCountry::whereIn('status', ['Approved', 'Closed'])
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year)
|
||||
->sum('approved_total'),
|
||||
|
||||
'FormMeetingSeminar' => FormMeetingSeminar::whereIn('status', ['Approved', 'Closed'])
|
||||
->whereMonth('created_at', '=', date('m', strtotime($month)))
|
||||
->whereYear('created_at', '=', $year)
|
||||
->sum('approved_total'),
|
||||
|
||||
'FormOthers' => FormOthers::whereIn('status', ['Approved', 'Closed'])
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year)
|
||||
->sum('approved_total'),
|
||||
|
||||
'FormEntertaimentPresentation' => FormEntertaimentPresentation::whereIn('status', ['Approved', 'Closed'])
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year)
|
||||
->sum('approved_total'),
|
||||
|
||||
'FormVehicleRunningCost' => FormVehicleRunningCost::whereIn('status', ['Approved', 'Closed'])
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year)
|
||||
->sum('approved_total')
|
||||
];
|
||||
}
|
||||
|
||||
$usedBudget = array_sum(array_map(function ($b) {
|
||||
return array_sum($b);
|
||||
}, $usedBudget));
|
||||
|
||||
return view(
|
||||
'backend.pages.budget_control.index',
|
||||
[
|
||||
'data' => $budget,
|
||||
'usedBudget' => $usedBudget,
|
||||
'availableBudget' => $budget->sum('amount') - $usedBudget,
|
||||
]
|
||||
);
|
||||
return view('backend.pages.budget_control.index', [
|
||||
'data' => $budget,
|
||||
]);
|
||||
}
|
||||
|
||||
public function log()
|
||||
@@ -108,7 +67,7 @@ class BudgetControlController extends Controller
|
||||
return view(
|
||||
'backend.pages.budget_control.log',
|
||||
[
|
||||
'data' => BudgetControlLog::orderBy('created_at', 'desc')->with(['user', 'cabang'])->get(),
|
||||
'data' => BudgetControlLog::orderBy('created_at', 'desc')->with(['receiver', 'sender', 'cabang'])->get(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user