2025-01-09 14:17:28 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use App\Models\BudgetControl;
|
|
|
|
|
use App\Models\FormUpCountry;
|
|
|
|
|
use App\Models\FormMeetingSeminar;
|
|
|
|
|
use App\Models\FormOthers;
|
|
|
|
|
use App\Models\FormEntertaimentPresentation;
|
|
|
|
|
use App\Models\FormVehicleRunningCost;
|
2025-01-11 13:31:44 +07:00
|
|
|
use App\Helpers\FormHelper;
|
|
|
|
|
use App\Models\UserHasCabang;
|
2025-01-09 14:17:28 +07:00
|
|
|
|
|
|
|
|
class BudgetHelper
|
|
|
|
|
{
|
2025-01-11 13:31:44 +07:00
|
|
|
public static function getAvailableBudget($cabang_id)
|
2025-01-09 14:17:28 +07:00
|
|
|
{
|
|
|
|
|
// Get the budget control data
|
2025-01-11 13:31:44 +07:00
|
|
|
$budget = BudgetControl::where('cabang_id', $cabang_id)->orderBy('created_at', 'desc')->with(['user', 'cabang'])->get();
|
2025-01-09 14:17:28 +07:00
|
|
|
|
|
|
|
|
// Initialize an empty array to hold the used budget sums
|
2025-01-11 13:31:44 +07:00
|
|
|
$usedBudget = 0;
|
2025-01-09 14:17:28 +07:00
|
|
|
|
|
|
|
|
foreach ($budget as $b) {
|
|
|
|
|
// Get the month and year from the budget
|
|
|
|
|
$month = $b->periode_month;
|
|
|
|
|
$year = $b->periode_year;
|
|
|
|
|
|
|
|
|
|
// Add the used budget per model for the specified month and year to the usedBudget array
|
2025-01-11 13:31:44 +07:00
|
|
|
$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');
|
2025-01-09 14:17:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$availableBudget = $budget->sum('amount') - $usedBudget;
|
|
|
|
|
|
|
|
|
|
return $availableBudget;
|
|
|
|
|
}
|
2025-01-12 13:34:35 +07:00
|
|
|
|
|
|
|
|
public static function getClosingDate($cabang_id)
|
|
|
|
|
{
|
|
|
|
|
$budget = BudgetControl::where('cabang_id', $cabang_id)->orderBy('created_at', 'desc')->first();
|
|
|
|
|
|
|
|
|
|
return $budget->closing_at;
|
|
|
|
|
}
|
2025-01-09 14:17:28 +07:00
|
|
|
}
|