diff --git a/app/Http/Controllers/Reports/StokAuditController.php b/app/Http/Controllers/Reports/StokAuditController.php new file mode 100644 index 0000000..a3861a6 --- /dev/null +++ b/app/Http/Controllers/Reports/StokAuditController.php @@ -0,0 +1,90 @@ +get(); + $connection_active_cawangan = Helper::getActiveCawanganDbConnection(); + + return collect($cawangan_details->filter(fn($x) => in_array($x->mysql, $connection_active_cawangan))); + + } + + public function buildStokAuditModel($month) + { + // Create the class name dynamically based on the number + $className = 'App\StokAudit' . $month; + + // Ensure the class exists + if (!class_exists($className)) { + abort(404); + } + + $model = new $className(); + + return $model; + } + + public function getSummaryLaporanStokAudit(Request $request) + { + $month = $request->month; + $year = $request->year; + + $stokAudit = $this->buildStokAuditModel($month); + $connections = $this->getActiveCawanganConnections(); + + $total_audit = $connections->map(function ($connection) use ($stokAudit, $year) { + $data = $stokAudit->on($connection->mysql)->where('tahun', $year)->get(); + + return [ + 'cawangan' => $connection->details_caw, + 'jumlah_pinjaman' => $data->count(), + 'total_pinjaman' => $data->sum('pinjaman'), + 'total_berat' => $data->sum('berat'), + 'total_nilai_marhun' => $data->sum('nilaimarhun'), + 'total_pinjaman' => $data->sum('pinjaman'), + 'total_baki_pinjaman' => $data->sum('bakipjm'), + 'total_upah_belum_bayar' => $data->sum('upahblmbyar'), + 'total_utg_onepercent' => $data->sum('utgonepercent'), + 'total_ujrah' => $data->sum('ujrah'), + ]; + }); + + $getTotalAcrossCawangan = function($property) use($total_audit) { + $total = array_reduce($total_audit->toArray(), function($carry, $subarray) use($property) { + return $carry + $subarray[$property]; + }, 0); + + return $total; + }; + + $total_all_cawangan_audit = [ + 'cawangan' => 'Total Semua Cawangan', + 'jumlah_pinjaman' => $getTotalAcrossCawangan('jumlah_pinjaman'), + 'total_pinjaman' => $getTotalAcrossCawangan('total_pinjaman'), + 'total_berat' => $getTotalAcrossCawangan('total_berat'), + 'total_nilai_marhun' => $getTotalAcrossCawangan('total_nilai_marhun'), + 'total_baki_pinjaman' => $getTotalAcrossCawangan('total_baki_pinjaman'), + 'total_upah_belum_bayar' => $getTotalAcrossCawangan('total_upah_belum_bayar'), + 'total_utg_onepercent' => $getTotalAcrossCawangan('total_utg_onepercent'), + 'total_ujrah' => $getTotalAcrossCawangan('total_ujrah'), + ]; + + $total_audit->push($total_all_cawangan_audit); + + if (sizeof($total_audit) == 0) { + return response()->json('failed', 200); + } else { + return response()->json($total_audit, 200); + } + } + +} diff --git a/routes/report.php b/routes/report.php index 67353ed..bec618e 100644 --- a/routes/report.php +++ b/routes/report.php @@ -11,6 +11,7 @@ $router->group(['prefix' => 'api/reports'], function () use ($router) { $router->get('kawalan-stok/{kod_cawangan}', ['uses' => 'KawalanStokController@getLaporanCawangan']); $router->get('ringkasan-harian', ['uses' => 'RingkasanHarianController@getSummaryLaporanRingkasanHarian']); $router->get('ringkasan-harian/{kod_cawangan}', ['uses' => 'RingkasanHarianController@getLaporanCawangan']); + $router->get('stok-audit', ['uses' => 'StokAuditController@getSummaryLaporanStokAudit']); $router->get('onepercent/balance/{kod_cawangan}', ['uses' => 'OnePercentController@getKeuntunganImbangDuga']); $router->get('onepercent/ledger/{kod_cawangan}', ['uses' => 'OnePercentController@getKeuntunganLejerAm']);