fix bug periode

This commit is contained in:
Jagad R R
2025-05-05 19:40:38 +07:00
parent af21c0877d
commit 35ad132607
9 changed files with 270 additions and 72 deletions
@@ -21,6 +21,7 @@ use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Schema;
use App\Models\Kategori;
use App\Models\BudgetControlLog;
use App\Helpers\PeriodeHelper;
class ReportController extends Controller
{
@@ -621,53 +622,60 @@ class ReportController extends Controller
]);
}
public function generateJurnal() {
public function generateJurnal()
{
$params = request()->all();
$kategoris = Kategori::all();
$kategori_bca = Kategori::where('name', 'Cash in Bank BCA')->first();
$cabang = Cabang::with('cost')->where('code', $params['cabang'])->first();
$cabang = Cabang::with('cost')->where('code', $params['cabang'])->firstOrFail();
$year = $params['year'];
$month = $params['month'];
$month = $params['month']; // format: angka atau teks bulan (pastikan valid)
// get last data
$budget = BudgetControlLog::where('cabang_id', $cabang->id)->where('periode_year', $year)->where('periode_month', $month)->orderBy('created_at', 'desc')->first();
// Hitung range periode (11 bulan_ini - 10 bulan_depan)
[$startDate, $endDate] = PeriodeHelper::getPeriodeRange($month, $year);
// Ambil data terakhir dari log budget
$budget = BudgetControlLog::where('cabang_id', $cabang->id)
->where('periode_year', $year)
->where('periode_month', $month)
->orderBy('created_at', 'desc')
->first();
$userIds = UserHasCabang::where('cabang_id', $cabang->id)->pluck('user_id');
// Fetch all relevant forms in one batch
// Fetch semua forms berdasarkan periode range
$forms = [
'Up Country' => FormUpCountry::whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year)
->whereIn('status', ['Approved', 'Closed'])
->whereBetween('tanggal', [$startDate, $endDate])
->whereIn('status', ['Approved', 'On Progress'])
->get(),
'Vehicle Running Cost' => FormVehicleRunningCost::whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year)
->whereIn('status', ['Approved', 'Closed'])
->whereBetween('tanggal', [$startDate, $endDate])
->whereIn('status', ['Approved', 'On Progress'])
->get(),
'Entertainment' => FormEntertaimentPresentation::whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year)
->whereIn('status', ['Approved', 'Closed'])
->whereBetween('tanggal', [$startDate, $endDate])
->whereIn('status', ['Approved', 'On Progress'])
->get(),
'Meeting / Seminar' => FormMeetingSeminar::whereIn('user_id', $userIds)
->whereMonth('created_at', '=', date('m', strtotime($month)))
->whereYear('created_at', '=', $year)
->whereIn('status', ['Approved', 'Closed'])
->whereBetween('created_at', [$startDate, $endDate])
->whereIn('status', ['Approved', 'On Progress'])
->get(),
];
// Fetch 'Others' forms by kategori
// Forms "Others" dikelompokkan berdasarkan kategori
$otherForms = FormOthers::whereIn('user_id', $userIds)
->whereMonth('tanggal', '=', date('m', strtotime($month)))
->whereYear('tanggal', '=', $year)
->whereIn('status', ['Approved', 'Closed'])
->whereBetween('tanggal', [$startDate, $endDate])
->whereIn('status', ['Approved', 'On Progress'])
->get();
// Precompute totals for each category
// Total nominal per kategori
$nominals = [];
foreach ($kategoris as $item) {
if (isset($forms[$item->name])) {
@@ -687,7 +695,7 @@ class ReportController extends Controller
'month' => $month,
'kategori_bca' => $kategori_bca,
'nominals' => $nominals,
'budget' => $budget
'budget' => $budget,
]);
}
}