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-05-25 15:14:23 +07:00
|
|
|
use Illuminate\Support\Carbon;
|
2025-01-09 14:17:28 +07:00
|
|
|
|
|
|
|
|
class BudgetHelper
|
|
|
|
|
{
|
2025-05-25 15:14:23 +07:00
|
|
|
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();
|
2025-01-09 14:17:28 +07:00
|
|
|
|
2025-01-11 13:31:44 +07:00
|
|
|
$usedBudget = 0;
|
2025-01-09 14:17:28 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
|
|
|
|
|
$userIds = $users->pluck('user_id')->toArray();
|
|
|
|
|
|
2025-06-26 19:47:57 +07:00
|
|
|
$forms = FormHelper::getTotalAmount($userIds, $periode_month, $periode_year)
|
2025-05-25 15:14:23 +07:00
|
|
|
->sortByDesc('tanggal')
|
|
|
|
|
->sortByDesc('created_at');
|
2025-01-09 14:17:28 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
$usedBudget += $forms->sum('approved_total');
|
2025-01-09 14:17:28 +07:00
|
|
|
|
|
|
|
|
$availableBudget = $budget->sum('amount') - $usedBudget;
|
|
|
|
|
|
|
|
|
|
return $availableBudget;
|
2025-05-25 15:14:23 +07:00
|
|
|
}
|
2025-01-12 13:34:35 +07:00
|
|
|
|
2025-01-15 22:29:07 +07:00
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|