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(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -27,88 +27,75 @@ class ReportController extends Controller
|
||||
public function upcountry()
|
||||
{
|
||||
$role = auth()->user()->getRoleNames()[0];
|
||||
$cabangs = [];
|
||||
$regions = [];
|
||||
$forms = null;
|
||||
$cabangs = collect(); // Default empty collection
|
||||
$regions = collect(); // Default empty collection
|
||||
$formsQuery = FormUpCountry::with([
|
||||
'user.region',
|
||||
'user.cabang',
|
||||
'rayon'
|
||||
]);
|
||||
|
||||
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
||||
$region_id = auth()->user()->getMyCabangAndRegionId()['region'];
|
||||
|
||||
if ($region_id) {
|
||||
$users = UserHasCabang::whereHas('cabang', function ($query) use ($region_id) {
|
||||
$query->where('region_id', $region_id);
|
||||
})->get();
|
||||
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('region_id', $region_id)->get();
|
||||
$regions = Region::where('id', $region_id)->get();
|
||||
$forms = FormUpCountry::whereIn('user_id', $userIds)->get();
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', $cabangs->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Area Manager Cabang') {
|
||||
} elseif ($role == 'Area Manager Cabang') {
|
||||
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'];
|
||||
|
||||
if ($cabang_id) {
|
||||
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('id', $cabang_id)->get();
|
||||
$regions = Region::where('id', $cabangs[0]->region_id)->get();
|
||||
$forms = FormUpCountry::whereIn('user_id', $userIds)->get();
|
||||
$regions = Region::where('id', $cabangs->first()->region_id)->get();
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang_id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Medical Representatif') {
|
||||
$userId = auth()->user()->id;
|
||||
|
||||
$cabangs = null;
|
||||
$regions = null;
|
||||
$forms = FormUpCountry::where('user_id', $userId)->get();
|
||||
} else {
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
$forms = FormUpCountry::all();
|
||||
}
|
||||
|
||||
// get all params in url
|
||||
// Apply filters from URL params
|
||||
$params = request()->all();
|
||||
if ($params) {
|
||||
// Filter by region
|
||||
if (isset($params['region'])) {
|
||||
$region = Region::where('code', $params['region'])->first();
|
||||
if ($region) {
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by cabang
|
||||
if (isset($params['cabang'])) {
|
||||
$cabang = Cabang::where('code', $params['cabang'])->first();
|
||||
if ($cabang) {
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by date range
|
||||
if (isset($params['start_date']) || isset($params['end_date'])) {
|
||||
$startDate = $params['start_date'] ?? null;
|
||||
$endDate = $params['end_date'] ?? null;
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$forms = $forms->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
} elseif ($startDate) {
|
||||
$forms = $forms->where('tanggal', '>=', $startDate);
|
||||
} elseif ($endDate) {
|
||||
$forms = $forms->where('tanggal', '<=', $endDate);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by status
|
||||
if (isset($params['status'])) {
|
||||
$forms = $forms->where('status', $params['status']);
|
||||
if (isset($params['region'])) {
|
||||
$region = Region::where('code', $params['region'])->first();
|
||||
if ($region) {
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($params['cabang'])) {
|
||||
$cabang = Cabang::where('code', $params['cabang'])->first();
|
||||
if ($cabang) {
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($params['start_date']) || isset($params['end_date'])) {
|
||||
$startDate = $params['start_date'] ?? null;
|
||||
$endDate = $params['end_date'] ?? null;
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$formsQuery->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
} elseif ($startDate) {
|
||||
$formsQuery->where('tanggal', '>=', $startDate);
|
||||
} elseif ($endDate) {
|
||||
$formsQuery->where('tanggal', '<=', $endDate);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($params['status'])) {
|
||||
$formsQuery->where('status', $params['status']);
|
||||
}
|
||||
|
||||
// Fetch forms after applying all filters
|
||||
$forms = $formsQuery->get();
|
||||
|
||||
return view('backend.pages.report.upcountry', [
|
||||
'pageInfo' => [
|
||||
@@ -120,51 +107,43 @@ class ReportController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function vehicle()
|
||||
{
|
||||
$role = auth()->user()->getRoleNames()[0];
|
||||
$cabangs = [];
|
||||
$regions = [];
|
||||
$forms = null;
|
||||
$cabangs = collect(); // Default empty collection
|
||||
$regions = collect(); // Default empty collection
|
||||
$formsQuery = FormVehicleRunningCost::with([
|
||||
'user.region', // Eager load 'region' within 'user'
|
||||
'user.cabang', // Eager load 'cabang' within 'user'
|
||||
]);
|
||||
|
||||
// Apply role-based query logic
|
||||
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
||||
$region_id = auth()->user()->getMyCabangAndRegionId()['region'];
|
||||
|
||||
if ($region_id) {
|
||||
$users = UserHasCabang::whereHas('cabang', function ($query) use ($region_id) {
|
||||
$query->where('region_id', $region_id);
|
||||
})->get();
|
||||
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('region_id', $region_id)->get();
|
||||
$regions = Region::where('id', $region_id)->get();
|
||||
$forms = FormVehicleRunningCost::whereIn('user_id', $userIds)->get();
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', $cabangs->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Area Manager Cabang') {
|
||||
} elseif ($role == 'Area Manager Cabang') {
|
||||
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'];
|
||||
|
||||
if ($cabang_id) {
|
||||
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('id', $cabang_id)->get();
|
||||
$regions = Region::where('id', $cabangs[0]->region_id)->get();
|
||||
$forms = FormVehicleRunningCost::whereIn('user_id', $userIds)->get();
|
||||
$regions = Region::where('id', $cabangs->first()->region_id)->get();
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang_id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Medical Representatif') {
|
||||
$userId = auth()->user()->id;
|
||||
|
||||
$cabangs = null;
|
||||
$regions = null;
|
||||
$forms = FormVehicleRunningCost::where('user_id', $userId)->get();
|
||||
} else {
|
||||
// Default behavior for other roles (if no specific role is matched)
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
$forms = FormVehicleRunningCost::all();
|
||||
}
|
||||
|
||||
// get all params in url
|
||||
// Get all params in URL for filtering
|
||||
$params = request()->all();
|
||||
|
||||
if ($params) {
|
||||
@@ -172,8 +151,8 @@ class ReportController extends Controller
|
||||
if (isset($params['region'])) {
|
||||
$region = Region::where('code', $params['region'])->first();
|
||||
if ($region) {
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,8 +160,8 @@ class ReportController extends Controller
|
||||
if (isset($params['cabang'])) {
|
||||
$cabang = Cabang::where('code', $params['cabang'])->first();
|
||||
if ($cabang) {
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,20 +171,23 @@ class ReportController extends Controller
|
||||
$endDate = $params['end_date'] ?? null;
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$forms = $forms->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
$formsQuery->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
} elseif ($startDate) {
|
||||
$forms = $forms->where('tanggal', '>=', $startDate);
|
||||
$formsQuery->where('tanggal', '>=', $startDate);
|
||||
} elseif ($endDate) {
|
||||
$forms = $forms->where('tanggal', '<=', $endDate);
|
||||
$formsQuery->where('tanggal', '<=', $endDate);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by status
|
||||
if (isset($params['status'])) {
|
||||
$forms = $forms->where('status', $params['status']);
|
||||
$formsQuery->where('status', $params['status']);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch forms after applying all filters
|
||||
$forms = $formsQuery->get();
|
||||
|
||||
return view('backend.pages.report.vehicle', [
|
||||
'pageInfo' => [
|
||||
'title' => 'Generate Reports For Form Vehicle Running Cost',
|
||||
@@ -219,48 +201,35 @@ class ReportController extends Controller
|
||||
public function entertainment()
|
||||
{
|
||||
$role = auth()->user()->getRoleNames()[0];
|
||||
$cabangs = [];
|
||||
$regions = [];
|
||||
$forms = null;
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
$formsQuery = FormEntertaimentPresentation::with([
|
||||
'user.region', // Eager load 'region' within 'user'
|
||||
'user.cabang', // Eager load 'cabang' within 'user'
|
||||
]);
|
||||
|
||||
// Apply role-based query logic
|
||||
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
||||
$region_id = auth()->user()->getMyCabangAndRegionId()['region'];
|
||||
|
||||
if ($region_id) {
|
||||
$users = UserHasCabang::whereHas('cabang', function ($query) use ($region_id) {
|
||||
$query->where('region_id', $region_id);
|
||||
})->get();
|
||||
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('region_id', $region_id)->get();
|
||||
$regions = Region::where('id', $region_id)->get();
|
||||
$forms = FormEntertaimentPresentation::whereIn('user_id', $userIds)->get();
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', $cabangs->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Area Manager Cabang') {
|
||||
} elseif ($role == 'Area Manager Cabang') {
|
||||
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'];
|
||||
|
||||
if ($cabang_id) {
|
||||
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('id', $cabang_id)->get();
|
||||
$regions = Region::where('id', $cabangs[0]->region_id)->get();
|
||||
$forms = FormEntertaimentPresentation::whereIn('user_id', $userIds)->get();
|
||||
$regions = Region::where('id', $cabangs->first()->region_id)->get();
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang_id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Medical Representatif') {
|
||||
$userId = auth()->user()->id;
|
||||
|
||||
$cabangs = null;
|
||||
$regions = null;
|
||||
$forms = FormEntertaimentPresentation::where('user_id', $userId)->get();
|
||||
} else {
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
$forms = FormEntertaimentPresentation::all();
|
||||
}
|
||||
|
||||
// get all params in url
|
||||
// Get all params in URL for filtering
|
||||
$params = request()->all();
|
||||
|
||||
if ($params) {
|
||||
@@ -268,8 +237,8 @@ class ReportController extends Controller
|
||||
if (isset($params['region'])) {
|
||||
$region = Region::where('code', $params['region'])->first();
|
||||
if ($region) {
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,8 +246,8 @@ class ReportController extends Controller
|
||||
if (isset($params['cabang'])) {
|
||||
$cabang = Cabang::where('code', $params['cabang'])->first();
|
||||
if ($cabang) {
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,20 +257,23 @@ class ReportController extends Controller
|
||||
$endDate = $params['end_date'] ?? null;
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$forms = $forms->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
$formsQuery->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
} elseif ($startDate) {
|
||||
$forms = $forms->where('tanggal', '>=', $startDate);
|
||||
$formsQuery->where('tanggal', '>=', $startDate);
|
||||
} elseif ($endDate) {
|
||||
$forms = $forms->where('tanggal', '<=', $endDate);
|
||||
$formsQuery->where('tanggal', '<=', $endDate);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by status
|
||||
if (isset($params['status'])) {
|
||||
$forms = $forms->where('status', $params['status']);
|
||||
$formsQuery->where('status', $params['status']);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch forms after applying all filters
|
||||
$forms = $formsQuery->get();
|
||||
|
||||
return view('backend.pages.report.entertainment', [
|
||||
'pageInfo' => [
|
||||
'title' => 'Generate Reports For Form Entertainment & Presentation',
|
||||
@@ -315,48 +287,37 @@ class ReportController extends Controller
|
||||
public function meeting()
|
||||
{
|
||||
$role = auth()->user()->getRoleNames()[0];
|
||||
$cabangs = [];
|
||||
$regions = [];
|
||||
$forms = null;
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
|
||||
// Initialize the query for FormMeetingSeminar with eager loading
|
||||
$formsQuery = FormMeetingSeminar::with([
|
||||
'user.region', // Eager load 'region' within 'user'
|
||||
'user.cabang', // Eager load 'cabang' within 'user'
|
||||
]);
|
||||
|
||||
// Apply role-based query logic
|
||||
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
||||
$region_id = auth()->user()->getMyCabangAndRegionId()['region'];
|
||||
|
||||
if ($region_id) {
|
||||
$users = UserHasCabang::whereHas('cabang', function ($query) use ($region_id) {
|
||||
$query->where('region_id', $region_id);
|
||||
})->get();
|
||||
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('region_id', $region_id)->get();
|
||||
$regions = Region::where('id', $region_id)->get();
|
||||
$forms = FormMeetingSeminar::whereIn('user_id', $userIds)->get();
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', $cabangs->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Area Manager Cabang') {
|
||||
} elseif ($role == 'Area Manager Cabang') {
|
||||
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'];
|
||||
|
||||
if ($cabang_id) {
|
||||
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('id', $cabang_id)->get();
|
||||
$regions = Region::where('id', $cabangs[0]->region_id)->get();
|
||||
$forms = FormMeetingSeminar::whereIn('user_id', $userIds)->get();
|
||||
$regions = Region::where('id', $cabangs->first()->region_id)->get();
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang_id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Medical Representatif') {
|
||||
$userId = auth()->user()->id;
|
||||
|
||||
$cabangs = null;
|
||||
$regions = null;
|
||||
$forms = FormMeetingSeminar::where('user_id', $userId)->get();
|
||||
} else {
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
$forms = FormMeetingSeminar::all();
|
||||
}
|
||||
|
||||
// get all params in url
|
||||
// Get all params in URL for filtering
|
||||
$params = request()->all();
|
||||
|
||||
if ($params) {
|
||||
@@ -364,8 +325,8 @@ class ReportController extends Controller
|
||||
if (isset($params['region'])) {
|
||||
$region = Region::where('code', $params['region'])->first();
|
||||
if ($region) {
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -373,8 +334,8 @@ class ReportController extends Controller
|
||||
if (isset($params['cabang'])) {
|
||||
$cabang = Cabang::where('code', $params['cabang'])->first();
|
||||
if ($cabang) {
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -384,20 +345,23 @@ class ReportController extends Controller
|
||||
$endDate = $params['end_date'] ?? null;
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$forms = $forms->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
$formsQuery->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
} elseif ($startDate) {
|
||||
$forms = $forms->where('tanggal', '>=', $startDate);
|
||||
$formsQuery->where('tanggal', '>=', $startDate);
|
||||
} elseif ($endDate) {
|
||||
$forms = $forms->where('tanggal', '<=', $endDate);
|
||||
$formsQuery->where('tanggal', '<=', $endDate);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by status
|
||||
if (isset($params['status'])) {
|
||||
$forms = $forms->where('status', $params['status']);
|
||||
$formsQuery->where('status', $params['status']);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch forms after applying all filters
|
||||
$forms = $formsQuery->get();
|
||||
|
||||
return view('backend.pages.report.meeting', [
|
||||
'pageInfo' => [
|
||||
'title' => 'Generate Reports For Form Meeting & Seminar',
|
||||
@@ -411,48 +375,38 @@ class ReportController extends Controller
|
||||
public function others()
|
||||
{
|
||||
$role = auth()->user()->getRoleNames()[0];
|
||||
$cabangs = [];
|
||||
$regions = [];
|
||||
$forms = null;
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
|
||||
// Initialize the query for FormOthers with eager loading
|
||||
$formsQuery = FormOthers::with([
|
||||
'user.region', // Eager load 'region' within 'user'
|
||||
'user.cabang', // Eager load 'cabang' within 'user'
|
||||
'kategori'
|
||||
]);
|
||||
|
||||
// Apply role-based query logic
|
||||
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
||||
$region_id = auth()->user()->getMyCabangAndRegionId()['region'];
|
||||
|
||||
if ($region_id) {
|
||||
$users = UserHasCabang::whereHas('cabang', function ($query) use ($region_id) {
|
||||
$query->where('region_id', $region_id);
|
||||
})->get();
|
||||
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('region_id', $region_id)->get();
|
||||
$regions = Region::where('id', $region_id)->get();
|
||||
$forms = FormOthers::whereIn('user_id', $userIds)->get();
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', $cabangs->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Area Manager Cabang') {
|
||||
} elseif ($role == 'Area Manager Cabang') {
|
||||
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'];
|
||||
|
||||
if ($cabang_id) {
|
||||
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
|
||||
$userIds = $users->pluck('user_id')->toArray();
|
||||
|
||||
$cabangs = Cabang::where('id', $cabang_id)->get();
|
||||
$regions = Region::where('id', $cabangs[0]->region_id)->get();
|
||||
$forms = FormOthers::whereIn('user_id', $userIds)->get();
|
||||
$regions = Region::where('id', $cabangs->first()->region_id)->get();
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang_id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
} else if ($role == 'Medical Representatif') {
|
||||
$userId = auth()->user()->id;
|
||||
|
||||
$cabangs = null;
|
||||
$regions = null;
|
||||
$forms = FormOthers::where('user_id', $userId)->get();
|
||||
} else {
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
$forms = FormOthers::all();
|
||||
}
|
||||
|
||||
// get all params in url
|
||||
// Get all params in URL for filtering
|
||||
$params = request()->all();
|
||||
|
||||
if ($params) {
|
||||
@@ -460,8 +414,8 @@ class ReportController extends Controller
|
||||
if (isset($params['region'])) {
|
||||
$region = Region::where('code', $params['region'])->first();
|
||||
if ($region) {
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
$userIds = UserHasCabang::whereIn('cabang_id', Cabang::where('region_id', $region->id)->pluck('id'))->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,8 +423,8 @@ class ReportController extends Controller
|
||||
if (isset($params['cabang'])) {
|
||||
$cabang = Cabang::where('code', $params['cabang'])->first();
|
||||
if ($cabang) {
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id')->toArray();
|
||||
$forms = $forms->whereIn('user_id', $userIds);
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id');
|
||||
$formsQuery->whereIn('user_id', $userIds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -480,20 +434,23 @@ class ReportController extends Controller
|
||||
$endDate = $params['end_date'] ?? null;
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$forms = $forms->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
$formsQuery->whereBetween('tanggal', [$startDate, $endDate]);
|
||||
} elseif ($startDate) {
|
||||
$forms = $forms->where('tanggal', '>=', $startDate);
|
||||
$formsQuery->where('tanggal', '>=', $startDate);
|
||||
} elseif ($endDate) {
|
||||
$forms = $forms->where('tanggal', '<=', $endDate);
|
||||
$formsQuery->where('tanggal', '<=', $endDate);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by status
|
||||
if (isset($params['status'])) {
|
||||
$forms = $forms->where('status', $params['status']);
|
||||
$formsQuery->where('status', $params['status']);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch forms after applying all filters
|
||||
$forms = $formsQuery->get();
|
||||
|
||||
return view('backend.pages.report.others', [
|
||||
'pageInfo' => [
|
||||
'title' => 'Generate Reports For Form Others',
|
||||
@@ -538,10 +495,6 @@ class ReportController extends Controller
|
||||
$regions = Region::where('id', $cabangs[0]->region_id)->get();
|
||||
$forms = FormHelper::getFormsByUserIds($userIds, $month, $year)->sortByDesc('created_at');
|
||||
}
|
||||
} elseif ($role == 'Medical Representatif') {
|
||||
$cabangs = null;
|
||||
$regions = null;
|
||||
$forms = FormHelper::getFormsByUserIds([$userId], $month, $year)->sortByDesc('created_at');
|
||||
} else {
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
@@ -674,11 +627,53 @@ class ReportController extends Controller
|
||||
$kategoris = Kategori::all();
|
||||
$kategori_bca = Kategori::where('name', 'Cash in Bank BCA')->first();
|
||||
$cabang = Cabang::with('cost')->where('code', $params['cabang'])->first();
|
||||
|
||||
|
||||
$year = $params['year'];
|
||||
$month = $params['month'];
|
||||
|
||||
$budget = BudgetControlLog::where('cabang_id', $cabang->id)->where('periode_year', $year)->where('periode_month', $month)->orderBy('created_at', 'desc')->first();
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id');
|
||||
|
||||
// Fetch all relevant forms in one batch
|
||||
$forms = [
|
||||
'Up Country' => FormUpCountry::whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year)
|
||||
->whereIn('status', ['Approved', 'Closed'])
|
||||
->get(),
|
||||
'Vehicle Running Cost' => FormVehicleRunningCost::whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year)
|
||||
->whereIn('status', ['Approved', 'Closed'])
|
||||
->get(),
|
||||
'Entertainment' => FormEntertaimentPresentation::whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year)
|
||||
->whereIn('status', ['Approved', 'Closed'])
|
||||
->get(),
|
||||
'Meeting / Seminar' => FormMeetingSeminar::whereIn('user_id', $userIds)
|
||||
->whereMonth('created_at', '=', date('m', strtotime($month)))
|
||||
->whereYear('created_at', '=', $year)
|
||||
->whereIn('status', ['Approved', 'Closed'])
|
||||
->get(),
|
||||
];
|
||||
|
||||
// Fetch 'Others' forms by kategori
|
||||
$otherForms = FormOthers::whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year)
|
||||
->whereIn('status', ['Approved', 'Closed'])
|
||||
->get();
|
||||
|
||||
// Precompute totals for each category
|
||||
$nominals = [];
|
||||
foreach ($kategoris as $item) {
|
||||
if (isset($forms[$item->name])) {
|
||||
$nominals[$item->id] = $forms[$item->name]->sum('approved_total');
|
||||
} else {
|
||||
$nominals[$item->id] = $otherForms->where('kategori_id', $item->id)->sum('approved_total');
|
||||
}
|
||||
}
|
||||
|
||||
return view('backend.pages.report.generate_jurnal', [
|
||||
'pageInfo' => [
|
||||
@@ -688,8 +683,9 @@ class ReportController extends Controller
|
||||
'cabang' => $cabang,
|
||||
'year' => $year,
|
||||
'month' => $month,
|
||||
'budget' => $budget,
|
||||
'kategori_bca' => $kategori_bca
|
||||
'kategori_bca' => $kategori_bca,
|
||||
'nominals' => $nominals,
|
||||
'budget' => $budget
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user