173 lines
5.6 KiB
PHP
173 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Reports;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Cawangan;
|
|
use App\Gadai;
|
|
use App\Customer;
|
|
use App\Tebus;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\Reports\GadaianService as ReportGadaianService;
|
|
use App\Helper;
|
|
use App\Services\Reports\RingkasanHarianService as ReportRingkasanHarianService;
|
|
|
|
|
|
class AnalisaController extends Controller
|
|
{
|
|
public function getLaporanCawangan(string $kod_cawangan, Request $request)
|
|
{
|
|
$reportGadaianService = new ReportGadaianService($kod_cawangan, $request);
|
|
return $reportGadaianService->getCawanganReport();
|
|
}
|
|
|
|
public function getSummaryLaporanPelangganBaru(Request $request)
|
|
{
|
|
$startDate = $request->startDate;
|
|
$endDate = $request->endDate;
|
|
|
|
// Get active branch DBs from helper
|
|
$active_cawangan_db_connection = Helper::getActiveCawanganDbConnection();
|
|
|
|
// Fetch branch details with mysql info
|
|
$cawangan_available = Cawangan::select('kodcaw', 'details_caw', 'mysql')
|
|
->whereIn('mysql', $active_cawangan_db_connection)
|
|
->get();
|
|
|
|
$active_cawangan_db_connection = array_column($cawangan_available->toArray(), 'mysql');
|
|
|
|
$active_cawangan_detail = $cawangan_available->map(function ($c) {
|
|
return [
|
|
'kodcaw' => $c->kodcaw,
|
|
'details_caw' => $c->details_caw,
|
|
];
|
|
})->values()->toArray();
|
|
|
|
// Iterate through each branch and get data
|
|
$response = array_map(function ($db_connection, $cawangan_detail) use ($request) {
|
|
// Pass the required parameters to the service constructor
|
|
$reportRingkasanHarianService = new ReportRingkasanHarianService($cawangan_detail['kodcaw'], $request);
|
|
|
|
// Get the customer count from the service
|
|
$bilangan_customer_baru = $reportRingkasanHarianService->getCustomerSummary(); // Call public method
|
|
|
|
// Return the response
|
|
return [
|
|
'kodcaw' => $cawangan_detail['kodcaw'],
|
|
'details_caw' => $cawangan_detail['details_caw'],
|
|
'total_pelanggan_baru' => $bilangan_customer_baru,
|
|
];
|
|
}, $active_cawangan_db_connection, $active_cawangan_detail);
|
|
|
|
return response()->json($response);
|
|
}
|
|
|
|
// public function getSummaryLaporanPelangganAktif(Request $request)
|
|
// {
|
|
// $startDate = $request->startDate;
|
|
// $endDate = $request->endDate;
|
|
|
|
// // Dapatkan senarai cawangan aktif
|
|
// $active_cawangan_db_connection = Helper::getActiveCawanganDbConnection();
|
|
|
|
// // Dapatkan maklumat cawangan
|
|
// $cawangan_available = Cawangan::select('kodcaw', 'details_caw', 'mysql')
|
|
// ->whereIn('mysql', $active_cawangan_db_connection)
|
|
// ->get();
|
|
|
|
// $active_cawangan_detail = $cawangan_available->map(function ($c) {
|
|
// return [
|
|
// 'kodcaw' => $c->kodcaw,
|
|
// 'details_caw' => $c->details_caw,
|
|
// 'mysql' => $c->mysql,
|
|
// ];
|
|
// })->toArray();
|
|
|
|
// $response = array_map(function ($cawangan) use ($startDate, $endDate) {
|
|
// $db_connection = $cawangan['mysql'];
|
|
|
|
// $total_aktif = Gadai::on($db_connection)
|
|
// ->where(function ($query) use ($startDate) {
|
|
// $query->where('sttebus', '')
|
|
// ->where('t_update', '>=', $startDate);
|
|
// })
|
|
// ->where('t_gadai', '<=', $endDate)
|
|
// ->distinct('nokp')
|
|
// ->count('nokp');
|
|
|
|
// return [
|
|
// 'kodcaw' => $cawangan['kodcaw'],
|
|
// 'details_caw' => $cawangan['details_caw'],
|
|
// 'total_pelanggan_aktif' => $total_aktif,
|
|
// ];
|
|
// }, $active_cawangan_detail);
|
|
|
|
|
|
// return response()->json($response);
|
|
// }
|
|
|
|
|
|
public function getSummaryLaporanPelangganAktif(Request $request)
|
|
{
|
|
$startDate = $request->startDate;
|
|
$endDate = $request->endDate;
|
|
|
|
$active_cawangan = Cawangan::select('kodcaw', 'details_caw', 'mysql')
|
|
->whereIn('mysql', Helper::getActiveCawanganDbConnection())
|
|
->get();
|
|
|
|
$response = $active_cawangan->map(function ($cawangan) use ($startDate, $endDate) {
|
|
$db = $cawangan->mysql;
|
|
|
|
// Ambil nokp dari Gadai
|
|
$nokp_gadai = DB::connection($db)->table('gadai')
|
|
->whereBetween('t_gadai', [$startDate, $endDate])
|
|
->pluck('nokp')
|
|
->toArray();
|
|
|
|
// Ambil nokp dari Tebus
|
|
$nokp_tebus = DB::connection($db)->table('tebus as t')
|
|
->join('gadai as g', 't.norujukan', '=', 'g.norujukan')
|
|
->whereBetween('t.t_tebus', [$startDate, $endDate])
|
|
->pluck('g.nokp')
|
|
->toArray();
|
|
|
|
// Gabung semua nokp
|
|
$semua_nokp = collect(array_merge($nokp_gadai, $nokp_tebus))
|
|
->filter(fn($item) => !empty($item))
|
|
->unique()
|
|
->values();
|
|
|
|
if ($semua_nokp->isEmpty()) {
|
|
// Jika array kosong, kita kembalikan hasil kosong
|
|
$pelanggan_aktif = collect();
|
|
} else {
|
|
$pelanggan_aktif = Customer::on($db)
|
|
->where(function ($query) use ($semua_nokp) {
|
|
$query->whereIn('kpbaru', $semua_nokp)
|
|
->orWhereIn('kplama', $semua_nokp);
|
|
})
|
|
->get(['nama', 'kpbaru', 'kplama'])
|
|
->toArray();;
|
|
}
|
|
|
|
return [
|
|
'kodcaw' => $cawangan->kodcaw,
|
|
'details_caw' => $cawangan->details_caw,
|
|
'total_pelanggan_aktif' => count($semua_nokp),
|
|
];
|
|
});
|
|
|
|
return response()->json($response);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|