update revisi
This commit is contained in:
@@ -10,12 +10,7 @@ use App\Models\BudgetControl;
|
||||
use App\Models\BudgetControlLog;
|
||||
use App\Models\Admin;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\FormUpCountry;
|
||||
use App\Models\FormMeetingSeminar;
|
||||
use App\Models\FormOthers;
|
||||
use App\Models\FormEntertaimentPresentation;
|
||||
use App\Models\FormVehicleRunningCost;
|
||||
use App\Models\UserHasCabang;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class BudgetControlController extends Controller
|
||||
{
|
||||
@@ -47,10 +42,23 @@ class BudgetControlController extends Controller
|
||||
$budget = BudgetControl::with(['receiver', 'sender', 'cabang'])->get();
|
||||
}
|
||||
|
||||
// Calculate usedBudget and availableBudget for each cabang
|
||||
$budget->map(function ($b) {
|
||||
$b->availableBudget = BudgetHelper::getAvailableBudget($b->cabang_id);
|
||||
// Tentukan periode berdasarkan start dan closing date
|
||||
$startDay = (int) env('STARTING_DATE', 11);
|
||||
$closingDay = (int) env('CLOSING_DATE', 10);
|
||||
$now = Carbon::now();
|
||||
|
||||
if ($now->day >= $startDay) {
|
||||
$startDate = Carbon::create($now->year, $now->month, $startDay)->startOfDay();
|
||||
} else {
|
||||
$startDate = Carbon::create($now->copy()->subMonth()->year, $now->copy()->subMonth()->month, $startDay)->startOfDay();
|
||||
}
|
||||
|
||||
$periodeMonth = $startDate->month;
|
||||
$periodeYear = $startDate->year;
|
||||
|
||||
// Hitung available budget berdasarkan periode
|
||||
$budget->map(function ($b) use ($periodeMonth, $periodeYear) {
|
||||
$b->availableBudget = BudgetHelper::getAvailableBudget($b->cabang_id, $periodeMonth, $periodeYear);
|
||||
return $b;
|
||||
});
|
||||
|
||||
|
||||
@@ -471,8 +471,8 @@ class ReportController extends Controller
|
||||
|
||||
$availableBudget = 0;
|
||||
|
||||
$month = date('F');
|
||||
$year = date('Y');
|
||||
$month = strtolower(date('F')); // convert to lowercase for DB compatibility
|
||||
$year = (int) date('Y');
|
||||
|
||||
$region_id = auth()->user()->getMyCabangAndRegionId()['region'] ?? null;
|
||||
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'] ?? null;
|
||||
@@ -520,27 +520,27 @@ class ReportController extends Controller
|
||||
$cabang = Cabang::where('code', $params['cabang'])->first();
|
||||
if ($cabang) {
|
||||
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id')->toArray();
|
||||
$forms = isset($forms) ? $forms->whereIn('user_id', $userIds) : FormHelper::getFormsByUserIds($userIds); // Ensure $forms is updated
|
||||
$availableBudget = BudgetHelper::getAvailableBudget($cabang->id);
|
||||
$forms = isset($forms)
|
||||
? $forms->whereIn('user_id', $userIds)
|
||||
: FormHelper::getFormsByUserIds($userIds, $month, $year);
|
||||
|
||||
// Ensure lowercase month
|
||||
$availableBudget = BudgetHelper::getAvailableBudget($cabang->id, strtolower($month), (int) $year);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by date range
|
||||
if (isset($params['month']) || isset($params['year'])) {
|
||||
$month = $params['month'] ?? date('F');
|
||||
$year = $params['year'] ?? date('Y');
|
||||
$month = strtolower($params['month'] ?? date('F'));
|
||||
$year = (int) ($params['year'] ?? date('Y'));
|
||||
|
||||
$startOfMonth = Carbon::parse("first day of $month $year")->startOfDay();
|
||||
$endOfMonth = Carbon::parse("last day of $month $year")->endOfDay();
|
||||
|
||||
if (isset($forms)) {
|
||||
// Determine the date column to filter by
|
||||
$dateColumn = Schema::hasColumn('forms', 'tanggal') ? 'tanggal' : 'created_at';
|
||||
|
||||
// Further filter existing forms by the determined date column
|
||||
$forms = $forms->whereBetween($dateColumn, [$startOfMonth, $endOfMonth]);
|
||||
} else {
|
||||
// Rebuild $forms using FormHelper and filter by role
|
||||
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
||||
if ($region_id) {
|
||||
$users = UserHasCabang::whereHas('cabang', function ($query) use ($region_id) {
|
||||
@@ -566,7 +566,7 @@ class ReportController extends Controller
|
||||
} else {
|
||||
$cabangs = Cabang::all();
|
||||
$regions = Region::all();
|
||||
$forms = FormHelper::getAllForms($month, $year)->sortByDesc('tanggal')->sortByDesc('created_at');
|
||||
$forms = FormHelper::getAllForms($month, $year)->sortByDesc('tanggal')->sortByDesc('created_at');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user