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 ;
2026-06-01 15:01:03 +07:00
use Illuminate\Support\Carbon ;
2025-01-11 13:31:44 +07:00
class FormHelper
{
2026-06-01 15:01:03 +07:00
/**
* Helper internal untuk menghitung rentang siklus tanggal
* Menghasilkan: Start (11), Max Input (10 bln depan), Closing (13 bln depan)
*/
private static function getCycleRange ( $month , $year )
{
// Pastikan di-cast ke (int) agar Carbon tidak error
$startDay = ( int ) env ( 'STARTING_DATE' , 11 );
$maxInputDay = ( int ) env ( 'INPUT_MAX_DATE' , 10 );
$closingDay = ( int ) env ( 'CLOSING_DATE' , 13 );
// Mengambil nomor bulan sebagai integer
$monthNumber = ( int ) date ( 'm' , strtotime ( $month . ' 1' ));
// Pastikan $year juga integer
$year = ( int ) $year ;
// Carbon::createFromDate membutuhkan integer
$startDate = \Illuminate\Support\Carbon :: createFromDate ( $year , $monthNumber , $startDay ) -> startOfDay ();
// Input maksimal dan Closing menggunakan method ->day() yang butuh integer
$maxInputDate = $startDate -> copy () -> addMonth () -> day ( $maxInputDay ) -> endOfDay ();
$closingDate = $startDate -> copy () -> addMonth () -> day ( $closingDay ) -> endOfDay ();
return [
'start' => $startDate -> toDateTimeString (),
'input_max' => $maxInputDate -> toDateTimeString (),
'closing' => $closingDate -> toDateTimeString (),
'display_label' => $startDate -> format ( 'd M' ) . ' - ' . $maxInputDate -> format ( 'd M Y' )
];
2025-01-11 13:31:44 +07:00
}
2026-06-01 15:01:03 +07:00
public static function getFormsByUserIds ( $userIds , $month , $year ) {
$dates = self :: getCycleRange ( $month , $year );
$startDate = $dates [ 'start' ];
$closingDate = $dates [ 'closing' ]; // Mengacu ke Closing Date (13)
$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 )
-> whereBetween ( 'tanggal' , [ $startDate , $closingDate ]);
$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 )
-> whereBetween ( 'tanggal' , [ $startDate , $closingDate ]);
$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 )
-> whereBetween ( 'tanggal' , [ $startDate , $closingDate ]);
$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 )
-> whereBetween ( 'created_at' , [ $startDate , $closingDate ]);
$formOthersQuery = FormOthers :: select (
'expense_number' , 'user_id' , 'total' , 'approved_total' , 'status' , 'account_number' , 'created_at' ,
DB :: raw ( " 'Others' as type " )
) -> whereIn ( 'user_id' , $userIds )
-> whereBetween ( 'tanggal' , [ $startDate , $closingDate ]);
$forms = $formUpCountryQuery
-> union ( $formVehicleRunningCostQuery )
-> union ( $formEntertaimentPresentationQuery )
-> union ( $formMeetingSeminarQuery )
-> union ( $formOthersQuery )
-> get ();
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 ) {
$dates = self :: getCycleRange ( $month , $year );
$startDate = $dates [ 'start' ];
$closingDate = $dates [ 'closing' ];
$users = Admin :: with ([ 'region' , 'cabang' ]) -> get ();
$commonSelect = [ 'expense_number' , 'user_id' , 'total' , 'approved_total' , 'status' , 'account_number' , 'created_at' ];
$queries = [
FormUpCountry :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Up Country' as type " )])) -> whereBetween ( 'tanggal' , [ $startDate , $closingDate ]),
FormVehicleRunningCost :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Vehicle Running Cost' as type " )])) -> whereBetween ( 'tanggal' , [ $startDate , $closingDate ]),
FormEntertaimentPresentation :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Entertainment Presentation' as type " )])) -> whereBetween ( 'tanggal' , [ $startDate , $closingDate ]),
FormMeetingSeminar :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Meeting Seminar' as type " )])) -> whereBetween ( 'created_at' , [ $startDate , $closingDate ]),
FormOthers :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Others' as type " )])) -> whereBetween ( 'tanggal' , [ $startDate , $closingDate ]),
];
$mainQuery = array_shift ( $queries );
foreach ( $queries as $q ) { $mainQuery -> union ( $q ); }
$forms = $mainQuery -> get ();
return $forms -> map ( function ( $form ) use ( $users ) {
$user = $users -> firstWhere ( 'id' , $form -> user_id );
$form -> user = $user ;
return $form ;
});
}
/**
* Dashboard Info: Menghasilkan label periode input (11-10)
*/
public static function getDashboardPeriodLabel ( $month , $year ) {
return self :: getCycleRange ( $month , $year )[ 'display_label' ];
}
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 ();
// Menggunakan logika cycle agar nominal yang muncul sinkron dengan cutoff
$dates = self :: getCycleRange ( $month , $year );
$start = $dates [ 'start' ];
$end = $dates [ 'closing' ];
$query = null ;
if ( $type == 'Up Country' ) {
$query = FormUpCountry :: whereIn ( 'user_id' , $userIds ) -> whereBetween ( 'tanggal' , [ $start , $end ]);
} else if ( $type == 'Vehicle Running Cost' ) {
$query = FormVehicleRunningCost :: whereIn ( 'user_id' , $userIds ) -> whereBetween ( 'tanggal' , [ $start , $end ]);
} else if ( $type == 'Entertainment' ) {
$query = FormEntertaimentPresentation :: whereIn ( 'user_id' , $userIds ) -> whereBetween ( 'tanggal' , [ $start , $end ]);
} else if ( $type == 'Meeting / Seminar' ) {
$query = FormMeetingSeminar :: whereIn ( 'user_id' , $userIds ) -> whereBetween ( 'created_at' , [ $start , $end ]);
} else {
$query = FormOthers :: whereIn ( 'user_id' , $userIds ) -> whereBetween ( 'tanggal' , [ $start , $end ]) -> where ( 'kategori_id' , $type_id );
}
return $query -> whereIn ( 'status' , [ 'Approved' , 'Closed' ]) -> sum ( 'approved_total' );
}
public static function getTotalAmount ( $userIds , $month , $year ) {
$dates = self :: getCycleRange ( $month , $year );
$startDate = $dates [ 'start' ];
$closingDate = $dates [ 'closing' ];
$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 ) -> whereBetween ( 'final_approved_at' , [ $startDate , $closingDate ]);
$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 ) -> whereBetween ( 'final_approved_at' , [ $startDate , $closingDate ]);
$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 ) -> whereBetween ( 'final_approved_at' , [ $startDate , $closingDate ]);
$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 ) -> whereBetween ( 'final_approved_at' , [ $startDate , $closingDate ]);
$formOthersQuery = FormOthers :: select ( 'expense_number' , 'user_id' , 'total' , 'approved_total' , 'status' , 'account_number' , 'created_at' , DB :: raw ( " 'Others' as type " ))
-> whereIn ( 'user_id' , $userIds ) -> whereBetween ( 'final_approved_at' , [ $startDate , $closingDate ]);
$forms = $formUpCountryQuery -> union ( $formVehicleRunningCostQuery ) -> union ( $formEntertaimentPresentationQuery ) -> union ( $formMeetingSeminarQuery ) -> union ( $formOthersQuery ) -> get ();
return $forms -> map ( function ( $form ) use ( $users ) {
$user = $users -> firstWhere ( 'id' , $form -> user_id );
$form -> user = $user ;
return $form ;
});
}
public static function getAllForms2 () {
// Fungsi ini tetap dipertahankan sesuai request (tanpa filter tanggal)
$users = Admin :: with ([ 'region' , 'cabang' ]) -> get ();
$commonSelect = [ 'expense_number' , 'user_id' , 'total' , 'approved_total' , 'status' , 'account_number' , 'created_at' ];
$forms = FormUpCountry :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Up Country' as type " )]))
-> union ( FormVehicleRunningCost :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Vehicle Running Cost' as type " )])))
-> union ( FormEntertaimentPresentation :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Entertainment Presentation' as type " )])))
-> union ( FormMeetingSeminar :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Meeting Seminar' as type " )])))
-> union ( FormOthers :: select ( array_merge ( $commonSelect , [ DB :: raw ( " 'Others' as type " )])))
-> get ();
return $forms -> map ( function ( $form ) use ( $users ) {
$user = $users -> firstWhere ( 'id' , $form -> user_id );
$form -> user = $user ;
return $form ;
});
}
}