improved some query

This commit is contained in:
Jagad R R
2025-01-15 22:29:07 +07:00
parent 5aa8304a48
commit 91e4a51f03
14 changed files with 551 additions and 372 deletions
+44 -1
View File
@@ -19,7 +19,7 @@ class BudgetHelper
public static function getAvailableBudget($cabang_id)
{
// Get the budget control data
$budget = BudgetControl::where('cabang_id', $cabang_id)->orderBy('created_at', 'desc')->with(['user', 'cabang'])->get();
$budget = BudgetControl::where('cabang_id', $cabang_id)->orderBy('created_at', 'desc')->with(['user.cabang'])->get();
// Initialize an empty array to hold the used budget sums
$usedBudget = 0;
@@ -41,6 +41,49 @@ class BudgetHelper
return $availableBudget;
}
public static function getNominalByFormType($userIds, $type, $type_id, $month, $year)
{
if ($type == 'Up Country') {
$form = FormUpCountry::select('approved_total')
->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year)
->whereIn('status', ['Approved', 'Closed'])
->get();
} elseif ($type == 'Vehicle Running Cost') {
$form = FormVehicleRunningCost::select('approved_total')
->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year)
->whereIn('status', ['Approved', 'Closed'])
->get();
} elseif ($type == 'Entertainment') {
$form = FormEntertaimentPresentation::select('approved_total')
->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year)
->whereIn('status', ['Approved', 'Closed'])
->get();
} elseif ($type == 'Meeting / Seminar') {
$form = FormMeetingSeminar::select('approved_total')
->whereIn('user_id', $userIds)
->whereMonth('created_at', '=', date('m', strtotime($month)))
->whereYear('created_at', '=', $year)
->whereIn('status', ['Approved', 'Closed'])
->get();
} else {
$form = FormOthers::select('approved_total')
->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year)
->where('kategori_id', $type_id)
->whereIn('status', ['Approved', 'Closed'])
->get();
}
return $form->sum('approved_total');
}
public static function getClosingDate($cabang_id)
{
$budget = BudgetControl::where('cabang_id', $cabang_id)->orderBy('created_at', 'desc')->first();
+129 -109
View File
@@ -2,6 +2,7 @@
namespace App\Helpers;
use App\Models\Admin;
use App\Models\FormEntertaimentPresentation;
use App\Models\FormMeetingSeminar;
use App\Models\FormOthers;
@@ -13,72 +14,77 @@ use App\Models\UserHasCabang;
class FormHelper
{
public static function getFormsByUserIds($userIds, $month, $year) {
// Eager load 'user.region' and 'user.cabang'
$users = Admin::with(['region', 'cabang'])
->whereIn('id', $userIds)
->get();
$formUpCountryQuery = FormUpCountry::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Up Country' as type")
)->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Up Country' as type")
)->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
$formVehicleRunningCostQuery = FormVehicleRunningCost::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Vehicle Running Cost' as type")
)->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Vehicle Running Cost' as type")
)->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
$formEntertaimentPresentationQuery = FormEntertaimentPresentation::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Entertainment Presentation' as type")
)->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Entertainment Presentation' as type")
)->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
$formMeetingSeminarQuery = FormMeetingSeminar::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Meeting Seminar' as type")
)->whereIn('user_id', $userIds)
->whereMonth('created_at', '=', date('m', strtotime($month)))
->whereYear('created_at', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Meeting Seminar' as type")
)->whereIn('user_id', $userIds)
->whereMonth('created_at', '=', date('m', strtotime($month)))
->whereYear('created_at', '=', $year);
$formOthersQuery = FormOthers::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Others' as type")
)->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Others' as type")
)->whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
// Use union before fetching the data
// Use union to combine queries
$forms = $formUpCountryQuery
->union($formVehicleRunningCostQuery)
->union($formEntertaimentPresentationQuery)
@@ -86,71 +92,80 @@ class FormHelper
->union($formOthersQuery)
->get();
return $forms;
// Return forms with eager loaded user details
return $forms->map(function ($form) use ($users) {
$user = $users->firstWhere('id', $form->user_id);
$form->user = $user;
return $form;
});
}
public static function getAllForms($month, $year) {
// Get all users with eager loading for 'region' and 'cabang'
$users = Admin::with(['region', 'cabang'])
->get();
$formUpCountryQuery = FormUpCountry::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Up Country' as type")
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Up Country' as type")
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
$formVehicleRunningCostQuery = FormVehicleRunningCost::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Vehicle Running Cost' as type")
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Vehicle Running Cost' as type")
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
$formEntertaimentPresentationQuery = FormEntertaimentPresentation::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Entertainment Presentation' as type")
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Entertainment Presentation' as type")
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
$formMeetingSeminarQuery = FormMeetingSeminar::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Meeting Seminar' as type")
)->whereMonth('created_at', '=', date('m', strtotime($month)))
->whereYear('created_at', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Meeting Seminar' as type")
)->whereMonth('created_at', '=', date('m', strtotime($month)))
->whereYear('created_at', '=', $year);
$formOthersQuery = FormOthers::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Others' as type")
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Others' as type")
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year);
// Use union before fetching the data
// Combine queries using union
$forms = $formUpCountryQuery
->union($formVehicleRunningCostQuery)
->union($formEntertaimentPresentationQuery)
@@ -158,7 +173,12 @@ class FormHelper
->union($formOthersQuery)
->get();
return $forms;
// Map users to the forms with eager loaded data
return $forms->map(function ($form) use ($users) {
$user = $users->firstWhere('id', $form->user_id);
$form->user = $user;
return $form;
});
}
public static function getNominalByFormType($cabang_id, $type, $type_id, $month, $year) {
@@ -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(),
]
);
}
+199 -203
View File
@@ -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
]);
}
}
@@ -37,6 +37,7 @@ class FormEntertainmentPresentationController extends Controller
->where('tanggal', '<=', date('Y-m-' . env('CLOSING_DATE')))
->whereRaw('DAY(CURDATE()) <= ' . env('CLOSING_DATE'))
->orderBy('tanggal', 'desc')
->with('user')
->get();
$availableBudget = null;
@@ -37,6 +37,7 @@ class FormMeetingSeminarController extends Controller
->where('created_at', '<=', date('Y-m-' . env('CLOSING_DATE')))
->whereRaw('DAY(CURDATE()) <= ' . env('CLOSING_DATE'))
->orderBy('created_at', 'desc')
->with('user')
->get();
$availableBudget = null;
@@ -37,6 +37,7 @@ class FormOtherController extends Controller
->where('tanggal', '<=', date('Y-m-' . env('CLOSING_DATE')))
->whereRaw('DAY(CURDATE()) <= ' . env('CLOSING_DATE'))
->orderBy('tanggal', 'desc')
->with(['user', 'kategori'])
->get();
$availableBudget = null;
@@ -37,6 +37,7 @@ class FormUpCountryController extends Controller
->where('tanggal', '<=', date('Y-m-' . env('CLOSING_DATE')))
->whereRaw('DAY(CURDATE()) <= ' . env('CLOSING_DATE'))
->orderBy('tanggal', 'desc')
->with(['rayon', 'user'])
->get();
$availableBudget = null;
@@ -37,6 +37,7 @@ class FormVehicleController extends Controller
->where('tanggal', '<=', date('Y-m-' . env('CLOSING_DATE')))
->whereRaw('DAY(CURDATE()) <= ' . env('CLOSING_DATE'))
->orderBy('tanggal', 'desc')
->with('user')
->get();
$availableBudget = null;