improved some query
This commit is contained in:
+129
-109
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Models\Admin;
|
||||
use App\Models\FormEntertaimentPresentation;
|
||||
use App\Models\FormMeetingSeminar;
|
||||
use App\Models\FormOthers;
|
||||
@@ -13,72 +14,77 @@ use App\Models\UserHasCabang;
|
||||
class FormHelper
|
||||
{
|
||||
public static function getFormsByUserIds($userIds, $month, $year) {
|
||||
// Eager load 'user.region' and 'user.cabang'
|
||||
$users = Admin::with(['region', 'cabang'])
|
||||
->whereIn('id', $userIds)
|
||||
->get();
|
||||
|
||||
$formUpCountryQuery = FormUpCountry::select(
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Up Country' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Up Country' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
|
||||
$formVehicleRunningCostQuery = FormVehicleRunningCost::select(
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Vehicle Running Cost' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Vehicle Running Cost' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
|
||||
$formEntertaimentPresentationQuery = FormEntertaimentPresentation::select(
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Entertainment Presentation' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Entertainment Presentation' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
|
||||
$formMeetingSeminarQuery = FormMeetingSeminar::select(
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Meeting Seminar' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('created_at', '=', date('m', strtotime($month)))
|
||||
->whereYear('created_at', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Meeting Seminar' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('created_at', '=', date('m', strtotime($month)))
|
||||
->whereYear('created_at', '=', $year);
|
||||
|
||||
$formOthersQuery = FormOthers::select(
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Others' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Others' as type")
|
||||
)->whereIn('user_id', $userIds)
|
||||
->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
|
||||
// Use union before fetching the data
|
||||
// Use union to combine queries
|
||||
$forms = $formUpCountryQuery
|
||||
->union($formVehicleRunningCostQuery)
|
||||
->union($formEntertaimentPresentationQuery)
|
||||
@@ -86,71 +92,80 @@ class FormHelper
|
||||
->union($formOthersQuery)
|
||||
->get();
|
||||
|
||||
return $forms;
|
||||
// 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;
|
||||
});
|
||||
}
|
||||
|
||||
public static function getAllForms($month, $year) {
|
||||
// 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")
|
||||
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Up Country' as type")
|
||||
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
|
||||
$formVehicleRunningCostQuery = FormVehicleRunningCost::select(
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Vehicle Running Cost' as type")
|
||||
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Vehicle Running Cost' as type")
|
||||
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
|
||||
$formEntertaimentPresentationQuery = FormEntertaimentPresentation::select(
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Entertainment Presentation' as type")
|
||||
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Entertainment Presentation' as type")
|
||||
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
|
||||
$formMeetingSeminarQuery = FormMeetingSeminar::select(
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Meeting Seminar' as type")
|
||||
)->whereMonth('created_at', '=', date('m', strtotime($month)))
|
||||
->whereYear('created_at', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Meeting Seminar' as type")
|
||||
)->whereMonth('created_at', '=', date('m', strtotime($month)))
|
||||
->whereYear('created_at', '=', $year);
|
||||
|
||||
$formOthersQuery = FormOthers::select(
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Others' as type")
|
||||
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
'expense_number',
|
||||
'user_id',
|
||||
'total',
|
||||
'approved_total',
|
||||
'status',
|
||||
'account_number',
|
||||
'created_at',
|
||||
DB::raw("'Others' as type")
|
||||
)->whereMonth('tanggal', '=', date('m', strtotime($month)))
|
||||
->whereYear('tanggal', '=', $year);
|
||||
|
||||
// Use union before fetching the data
|
||||
// Combine queries using union
|
||||
$forms = $formUpCountryQuery
|
||||
->union($formVehicleRunningCostQuery)
|
||||
->union($formEntertaimentPresentationQuery)
|
||||
@@ -158,7 +173,12 @@ class FormHelper
|
||||
->union($formOthersQuery)
|
||||
->get();
|
||||
|
||||
return $forms;
|
||||
// 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 getNominalByFormType($cabang_id, $type, $type_id, $month, $year) {
|
||||
|
||||
Reference in New Issue
Block a user