Files
expense/app/Helpers/BudgetHelper.php
T
2025-01-15 22:29:07 +07:00

94 lines
3.1 KiB
PHP

<?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;
use App\Helpers\FormHelper;
use App\Models\UserHasCabang;
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();
// 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;
// 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');
}
$availableBudget = $budget->sum('amount') - $usedBudget;
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();
return $budget->closing_at;
}
}