update revisi

This commit is contained in:
Jagad R R
2025-05-25 15:14:23 +07:00
parent f0c7c3e3e4
commit f0546fa86f
12 changed files with 619 additions and 415 deletions
+21 -16
View File
@@ -13,33 +13,38 @@ use App\Models\FormEntertaimentPresentation;
use App\Models\FormVehicleRunningCost;
use App\Helpers\FormHelper;
use App\Models\UserHasCabang;
use Illuminate\Support\Carbon;
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();
public static function getAvailableBudget($cabang_id, $periode_month, $periode_year)
{
// Validasi & normalisasi
$periode_month = strtolower($periode_month);
$periode_year = (int) $periode_year;
// Ambil budget sesuai cabang dan periode
$budget = BudgetControl::where('cabang_id', $cabang_id)
->where('periode_month', $periode_month)
->where('periode_year', $periode_year)
->with(['user.cabang'])
->get();
// Initialize an empty array to hold the used budget sums
$usedBudget = 0;
foreach ($budget as $b) {
// Get the month and year from the budget
$month = $b->periode_month;
$year = $b->periode_year;
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
$userIds = $users->pluck('user_id')->toArray();
// Add the used budget per model for the specified month and year to the usedBudget array
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
$userIds = $users->pluck('user_id')->toArray();
$forms = FormHelper::getFormsByUserIds($userIds, $month, $year)->sortByDesc('tanggal')->sortByDesc('created_at');
$usedBudget += $forms->sum('approved_total');
}
$forms = FormHelper::getFormsByUserIds($userIds, $periode_month, $periode_year)
->sortByDesc('tanggal')
->sortByDesc('created_at');
$usedBudget += $forms->sum('approved_total');
$availableBudget = $budget->sum('amount') - $usedBudget;
return $availableBudget;
}
}
public static function getNominalByFormType($userIds, $type, $type_id, $month, $year)
{