Files
expense/app/Helpers/FormHelper.php
T

274 lines
8.1 KiB
PHP
Raw Normal View History

2025-01-11 13:31:44 +07:00
<?php
namespace App\Helpers;
2025-01-15 22:29:07 +07:00
use App\Models\Admin;
2025-01-11 13:31:44 +07:00
use App\Models\FormEntertaimentPresentation;
use App\Models\FormMeetingSeminar;
use App\Models\FormOthers;
use App\Models\FormUpCountry;
use App\Models\FormVehicleRunningCost;
use Illuminate\Support\Facades\DB;
2025-01-14 12:01:54 +07:00
use App\Models\UserHasCabang;
2025-01-11 13:31:44 +07:00
class FormHelper
{
public static function getFormsByUserIds($userIds, $month, $year) {
2025-01-19 19:17:09 +07:00
$startDate = date("$year-$month-" . env('STARTING_DATE'));
$closingDateNextMonth = date("$year-$month-", strtotime("+1 month", strtotime("$year-$month-01"))) . env('CLOSING_DATE');
2025-01-19 15:11:21 +07:00
2025-01-15 22:29:07 +07:00
$users = Admin::with(['region', 'cabang'])
->whereIn('id', $userIds)
->get();
2025-01-11 13:31:44 +07:00
$formUpCountryQuery = FormUpCountry::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Up Country' as type")
)->whereIn('user_id', $userIds)
2025-01-19 15:11:21 +07:00
->whereBetween('tanggal', [$startDate, $closingDateNextMonth]);
2025-01-11 13:31:44 +07:00
$formVehicleRunningCostQuery = FormVehicleRunningCost::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Vehicle Running Cost' as type")
)->whereIn('user_id', $userIds)
2025-01-19 15:11:21 +07:00
->whereBetween('tanggal', [$startDate, $closingDateNextMonth]);
2025-01-11 13:31:44 +07:00
$formEntertaimentPresentationQuery = FormEntertaimentPresentation::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Entertainment Presentation' as type")
)->whereIn('user_id', $userIds)
2025-01-19 15:11:21 +07:00
->whereBetween('tanggal', [$startDate, $closingDateNextMonth]);
2025-01-11 13:31:44 +07:00
$formMeetingSeminarQuery = FormMeetingSeminar::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Meeting Seminar' as type")
)->whereIn('user_id', $userIds)
2025-01-19 15:11:21 +07:00
->whereBetween('created_at', [$startDate, $closingDateNextMonth]);
2025-01-11 13:31:44 +07:00
$formOthersQuery = FormOthers::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Others' as type")
)->whereIn('user_id', $userIds)
2025-01-19 15:11:21 +07:00
->whereBetween('tanggal', [$startDate, $closingDateNextMonth]);
2025-01-15 22:29:07 +07:00
// Use union to combine queries
2025-01-11 13:31:44 +07:00
$forms = $formUpCountryQuery
->union($formVehicleRunningCostQuery)
->union($formEntertaimentPresentationQuery)
->union($formMeetingSeminarQuery)
->union($formOthersQuery)
->get();
2025-01-15 22:29:07 +07:00
// Return forms with eager loaded user details
return $forms->map(function ($form) use ($users) {
$user = $users->firstWhere('id', $form->user_id);
$form->user = $user;
return $form;
});
2025-01-11 13:31:44 +07:00
}
public static function getAllForms($month, $year) {
2025-01-19 19:17:09 +07:00
$startDate = date("$year-$month-" . env('STARTING_DATE'));
$closingDateNextMonth = date("$year-$month-", strtotime("+1 month", strtotime("$year-$month-01"))) . env('CLOSING_DATE');
2025-01-19 15:11:21 +07:00
2025-01-15 22:29:07 +07:00
$users = Admin::with(['region', 'cabang'])
->get();
2025-01-11 13:31:44 +07:00
$formUpCountryQuery = FormUpCountry::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Up Country' as type")
2025-01-19 15:11:21 +07:00
)->whereBetween('tanggal', [$startDate, $closingDateNextMonth]);
2025-01-11 13:31:44 +07:00
$formVehicleRunningCostQuery = FormVehicleRunningCost::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Vehicle Running Cost' as type")
2025-01-19 15:11:21 +07:00
)->whereBetween('tanggal', [$startDate, $closingDateNextMonth]);
2025-01-11 13:31:44 +07:00
$formEntertaimentPresentationQuery = FormEntertaimentPresentation::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Entertainment Presentation' as type")
2025-01-19 15:11:21 +07:00
)->whereBetween('tanggal', [$startDate, $closingDateNextMonth]);
2025-01-11 13:31:44 +07:00
$formMeetingSeminarQuery = FormMeetingSeminar::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Meeting Seminar' as type")
2025-01-19 15:11:21 +07:00
)->whereBetween('created_at', [$startDate, $closingDateNextMonth]);
2025-01-11 13:31:44 +07:00
$formOthersQuery = FormOthers::select(
2025-01-15 22:29:07 +07:00
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Others' as type")
2025-01-19 15:11:21 +07:00
)->whereBetween('tanggal', [$startDate, $closingDateNextMonth]);
2025-01-15 22:29:07 +07:00
// Combine queries using union
2025-01-19 14:57:01 +07:00
$forms = $formUpCountryQuery
->union($formVehicleRunningCostQuery)
->union($formEntertaimentPresentationQuery)
->union($formMeetingSeminarQuery)
->union($formOthersQuery)
->get();
// Map users to the forms with eager loaded data
return $forms->map(function ($form) use ($users) {
$user = $users->firstWhere('id', $form->user_id);
$form->user = $user;
return $form;
});
}
public static function getAllForms2() {
// Get all users with eager loading for 'region' and 'cabang'
$users = Admin::with(['region', 'cabang'])
->get();
$formUpCountryQuery = FormUpCountry::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Up Country' as type")
);
$formVehicleRunningCostQuery = FormVehicleRunningCost::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Vehicle Running Cost' as type")
);
$formEntertaimentPresentationQuery = FormEntertaimentPresentation::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Entertainment Presentation' as type")
);
$formMeetingSeminarQuery = FormMeetingSeminar::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Meeting Seminar' as type")
);
$formOthersQuery = FormOthers::select(
'expense_number',
'user_id',
'total',
'approved_total',
'status',
'account_number',
'created_at',
DB::raw("'Others' as type")
);
// Combine queries using union
2025-01-11 13:31:44 +07:00
$forms = $formUpCountryQuery
->union($formVehicleRunningCostQuery)
->union($formEntertaimentPresentationQuery)
->union($formMeetingSeminarQuery)
->union($formOthersQuery)
->get();
2025-01-15 22:29:07 +07:00
// Map users to the forms with eager loaded data
return $forms->map(function ($form) use ($users) {
$user = $users->firstWhere('id', $form->user_id);
$form->user = $user;
return $form;
});
2025-01-11 13:31:44 +07:00
}
2025-01-14 12:01:54 +07:00
public static function getNominalByFormType($cabang_id, $type, $type_id, $month, $year) {
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
$userIds = $users->pluck('user_id')->toArray();
if($type == 'Up Country') {
2025-01-14 14:15:32 +07:00
$form = FormUpCountry::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('tanggal', '=', date('m', strtotime($month)))->whereYear('tanggal', '=', $year)->where('status', ['Approved', 'Closed'])->get();
2025-01-14 12:01:54 +07:00
} else if($type == 'Vehicle Running Cost') {
2025-01-14 14:15:32 +07:00
$form = FormVehicleRunningCost::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('tanggal', '=', date('m', strtotime($month)))->whereYear('tanggal', '=', $year)->where('status', ['Approved', 'Closed'])->get();
2025-01-14 12:01:54 +07:00
} else if($type == 'Entertainment') {
2025-01-14 14:15:32 +07:00
$form = FormEntertaimentPresentation::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('tanggal', '=', date('m', strtotime($month)))->whereYear('tanggal', '=', $year)->where('status', ['Approved', 'Closed'])->get();
2025-01-14 12:01:54 +07:00
} else if($type == 'Meeting / Seminar') {
2025-01-14 14:15:32 +07:00
$form = FormMeetingSeminar::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('created_at', '=', date('m', strtotime($month)))->whereYear('created_at', '=', $year)->where('status', ['Approved', 'Closed'])->get();
2025-01-14 12:01:54 +07:00
} else {
2025-01-14 14:15:32 +07:00
$form = FormOthers::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('tanggal', '=', date('m', strtotime($month)))->whereYear('tanggal', '=', $year)->where('kategori_id', $type_id)->where('status', ['Approved', 'Closed'])->get();
2025-01-14 12:01:54 +07:00
}
$nominal = $form->sum('approved_total');
return $nominal;
}
2025-01-11 13:31:44 +07:00
}