Merge branch 'ujrah_master' of https://bitbucket.org/arrahn-pkb/api_arrahn into api-laporan-ujrah
This commit is contained in:
@@ -3,14 +3,12 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Cawangan;
|
||||
use App\CawanganDetail;
|
||||
use App\Customer;
|
||||
use App\Gadai;
|
||||
use App\Marhun;
|
||||
use DateTime;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Services\Reports\GadaianService as ReportGadaianService;
|
||||
use App\Helper;
|
||||
|
||||
|
||||
class ReportGadaiController extends Controller
|
||||
{
|
||||
@@ -27,7 +25,7 @@ class ReportGadaiController extends Controller
|
||||
$startDate = $request->startDate;
|
||||
$endDate = $request->endDate;
|
||||
|
||||
$active_cawangan_db_connection = getActiveCawanganDbConnection();
|
||||
$active_cawangan_db_connection = Helper::getActiveCawanganDbConnection();
|
||||
|
||||
$active_cawangan_db_name = array_map(function ($db_connection) {
|
||||
return config('database.connections' . '.' . $db_connection . '.' . 'database');
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Gadai;
|
||||
use App\Marhun;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Helper;
|
||||
|
||||
class GadaianService
|
||||
{
|
||||
@@ -82,8 +83,8 @@ class GadaianService
|
||||
|
||||
$data = [
|
||||
'query' => [
|
||||
'startDate' => formatDate($this->startDate),
|
||||
'endDate' => formatDate($this->endDate),
|
||||
'startDate' => Helper::formatDate($this->startDate),
|
||||
'endDate' => Helper::formatDate($this->endDate),
|
||||
],
|
||||
'cawangan_detail' => $cawangan_detail,
|
||||
'gadaian' => $total_gadaian,
|
||||
|
||||
+12
-15
@@ -1,28 +1,25 @@
|
||||
<?php
|
||||
namespace App;
|
||||
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use DateTime;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
if (!function_exists('formatNumber')) {
|
||||
function formatNumber ($amount) {
|
||||
class Helper
|
||||
{
|
||||
|
||||
public static function formatNumber($amount)
|
||||
{
|
||||
$formattedAmount = number_format($amount, 2, '.', ',');
|
||||
|
||||
return $formattedAmount;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static function formatDate($date_string)
|
||||
{
|
||||
return (new DateTime($date_string))->format("d-m-Y");
|
||||
}
|
||||
|
||||
if (!function_exists('formatDate')) {
|
||||
function formatDate ($date_string) {
|
||||
return (new DateTime($date_string))->format("d-m-Y");
|
||||
};
|
||||
}
|
||||
|
||||
if (!function_exists('getActiveCawanganDbConnection')) {
|
||||
|
||||
function getActiveCawanganDbConnection()
|
||||
public static function getActiveCawanganDbConnection()
|
||||
{
|
||||
|
||||
$all_active_db_keys = array_filter(array_keys(config('database.connections')), function ($connection) {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
$all_active_db_keys = array_filter(array_keys(config('database.connections')), function ($connection) {
|
||||
try {
|
||||
DB::connection($connection)->getPdo();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
$non_cawangan_database = ['lelong', 'online_arrahn', 'kaunterpkb'];
|
||||
|
||||
$all_cawangan_db = array_filter($all_active_db_keys, function($db_key) use($non_cawangan_database){
|
||||
$isNotCawangan = in_array(config('database.connections.' . $db_key . '.database'), $non_cawangan_database);
|
||||
|
||||
if($isNotCawangan){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return $all_cawangan_db;
|
||||
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
use App\Cawangan;
|
||||
use App\CawanganDetail;
|
||||
use App\Customer;
|
||||
use App\Gadai;
|
||||
use App\Marhun;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Helper;
|
||||
|
||||
// test variable
|
||||
$request = new Request();
|
||||
$request->all();
|
||||
$request->merge([
|
||||
'startDate' => '2023-8-21',
|
||||
'endDate' => '2023-10-22',
|
||||
'kod_cawangan' => 'KB2',
|
||||
]);
|
||||
// test variable
|
||||
|
||||
function getCawanganDetail($cawangan_connection)
|
||||
{
|
||||
$cawangan_detail = CawanganDetail::on($cawangan_connection)->get()
|
||||
->map(function ($detail) {
|
||||
return [
|
||||
'nama' => $detail['namacaw'],
|
||||
'code' => $detail['kodcaw'] . ' Ar-Rahn ' . $detail['cawangan'],
|
||||
'alamat' => $detail['alamat1'],
|
||||
'waktu_operasi' => $detail['alamat4'],
|
||||
];
|
||||
});
|
||||
|
||||
return $cawangan_detail;
|
||||
|
||||
}
|
||||
|
||||
function getTotalSummaryGadaian(Builder $query_total_gadaian)
|
||||
{
|
||||
$formatNumber = function ($amount) {
|
||||
$formattedAmount = number_format($amount, 2, '.', ',');
|
||||
|
||||
return $formattedAmount;
|
||||
};
|
||||
|
||||
$total_summary_gadaian = (clone $query_total_gadaian)
|
||||
->selectRaw('sum(berat) as total_berat, sum(nilaimarhun) as total_nilai_marhun, sum(pinjaman) as total_pembiayaan')
|
||||
->get()
|
||||
->map(function ($x) use ($formatNumber) {
|
||||
return [
|
||||
'total_berat' => $formatNumber($x['total_berat']),
|
||||
'total_nilai_marhun' => $formatNumber($x['total_nilai_marhun']),
|
||||
'total_pembiayaan' => $formatNumber($x['total_pembiayaan']),
|
||||
];
|
||||
});
|
||||
|
||||
return $total_summary_gadaian;
|
||||
}
|
||||
|
||||
function getTotalSummaryGadaianGroupByTeller(Builder $query_total_gadaian)
|
||||
{
|
||||
|
||||
$total_summary_gadaian_groupByTeller = (clone $query_total_gadaian)
|
||||
->selectRaw('sum(berat) as total_berat, sum(nilaimarhun) as total_nilai_marhun, sum(pinjaman) as total_pembiayaan')
|
||||
->groupBy('teller')
|
||||
->get();
|
||||
return $total_summary_gadaian_groupByTeller;
|
||||
}
|
||||
|
||||
function getTotalGadaian(Builder $query_total_gadaian)
|
||||
{
|
||||
$total_gadaian = (clone $query_total_gadaian)
|
||||
->select('t_gadai', 'norujukan', 'nokp', 'semakbarcode', 'masa', 'berat', 'nilaimarhun', 'pinjaman', 'upah12', 'percentpinj', 'teller', 'nokp')
|
||||
->orderBy('teller')
|
||||
->orderBy('sirig')
|
||||
->get();
|
||||
return $total_gadaian;
|
||||
}
|
||||
|
||||
function getCustomerInfo($total_gadaian)
|
||||
{
|
||||
|
||||
$extract_nokp = $total_gadaian->pluck('nokp');
|
||||
|
||||
$customer_info = Customer::on('mysql7')
|
||||
->select(['nama', 'alamat1', 'kpbaru', 'kplama'])
|
||||
->where(function ($query) use ($extract_nokp) {
|
||||
$query->whereIn('kpbaru', $extract_nokp)
|
||||
->orWhereIn('kplama', $extract_nokp);
|
||||
})
|
||||
->get();
|
||||
|
||||
$customer_info = collect($extract_nokp)->map(function ($nokp) use ($customer_info) {
|
||||
return $customer_info->where('kpbaru', $nokp)->first() ?? $customer_info->where('kplama', $nokp)->first();
|
||||
})->toArray();
|
||||
|
||||
return $customer_info;
|
||||
}
|
||||
|
||||
function getMarhunGadaian($total_gadaian, $cawangan_connection)
|
||||
{
|
||||
|
||||
$extract_norujukan = $total_gadaian->pluck('norujukan');
|
||||
|
||||
$marhun_gadaian = Marhun::on($cawangan_connection)
|
||||
->select('norujukan', 'bil', 'marhun')
|
||||
->whereIn('norujukan', $extract_norujukan)
|
||||
->get()
|
||||
->groupBy('norujukan')
|
||||
->toArray();
|
||||
|
||||
$marhun_gadaian = array_map(function ($gadaian) {
|
||||
return array_map(function ($norujukan) {
|
||||
return $norujukan['bil'] . ' ' . $norujukan['marhun'];
|
||||
}, $gadaian);
|
||||
}, $marhun_gadaian);
|
||||
|
||||
$marhun_gadaian = array_map(function ($gadaian) {
|
||||
return implode(',', $gadaian);
|
||||
}, $marhun_gadaian);
|
||||
|
||||
return $marhun_gadaian;
|
||||
}
|
||||
|
||||
function getReportGadaian(Request $request)
|
||||
{
|
||||
$cawangan_connection = Cawangan::where('kodcaw', $request->kod_cawangan)->first()->mysql;
|
||||
|
||||
$query_total_gadaian = Gadai::on($cawangan_connection);
|
||||
|
||||
$query_total_gadaian->when($request->filled('startDate') and $request->filled('endDate'), function ($q) use ($request) {
|
||||
return $q->where('t_gadai', '>=', $request->startDate)
|
||||
->where('t_gadai', '<=', $request->endDate);
|
||||
});
|
||||
|
||||
$total_gadaian = getTotalGadaian($query_total_gadaian);
|
||||
$total_summary_gadaian_teller = getTotalSummaryGadaianGroupByTeller($query_total_gadaian);
|
||||
$total_summary_gadaian = getTotalSummaryGadaian($query_total_gadaian);
|
||||
$customer_info = getCustomerInfo($total_gadaian);
|
||||
$marhun_gadaian = getMarhunGadaian($total_gadaian, $cawangan_connection);
|
||||
$cawangan_detail = getCawanganDetail($cawangan_connection);
|
||||
|
||||
$total_gadaian = $total_gadaian->zip($customer_info, $marhun_gadaian)->map(function ($data) {
|
||||
|
||||
[$gadai, $customer_info, $marhun_gadaian] = $data;
|
||||
|
||||
return [
|
||||
'no_rujukan' => $gadai['norujukan'],
|
||||
'nama' => $customer_info['nama'],
|
||||
'alamat' => $customer_info['alamat1'],
|
||||
'no_kp' => $gadai['nokp'],
|
||||
'masa' => $gadai['masa'],
|
||||
'marhun' => $marhun_gadaian,
|
||||
'berat' => Helper::formatNumber($gadai['berat']),
|
||||
'nilai_marhun' => Helper::formatNumber($gadai['nilaimarhun']),
|
||||
'pembiayaan' => Helper::formatNumber($gadai['pinjaman']),
|
||||
'peratusan_margin_pinjaman' => $gadai['percentpinj'],
|
||||
'teller' => $gadai['teller'],
|
||||
];
|
||||
|
||||
});
|
||||
|
||||
$total_gadaian = $total_gadaian
|
||||
->groupBy('teller')
|
||||
->zip($total_summary_gadaian_teller)
|
||||
->map(function ($data) {
|
||||
[$gadaian, $summary] = $data;
|
||||
|
||||
return [$gadaian, $summary];
|
||||
});
|
||||
|
||||
$data = [
|
||||
'query' => [
|
||||
'startDate' => Helper::formatDate($request->startDate),
|
||||
'endDate' => Helper::formatDate($request->endDate),
|
||||
],
|
||||
'cawangan_detail' => $cawangan_detail,
|
||||
'gadaian' => $total_gadaian,
|
||||
'summary_gadaian' => $total_summary_gadaian,
|
||||
];
|
||||
|
||||
return json_encode($data);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
use App\Cawangan;
|
||||
use App\Customer;
|
||||
use App\Gadai;
|
||||
use App\Helper;
|
||||
|
||||
$tarikh = '2023-11-01';
|
||||
|
||||
$active_cawangan_db_connection = Helper::getActiveCawanganDbConnection();
|
||||
|
||||
$active_cawangan_db = array_map(function($connection){
|
||||
return config('database.connections.' . $connection)['database'];
|
||||
}, $active_cawangan_db_connection);
|
||||
|
||||
$active_cawangan_name = Cawangan::whereIn('db_name', $active_cawangan_db)->pluck('details_caw')->toArray();
|
||||
|
||||
$gadai_data = array_map(function ($db_connection) use ($tarikh) {
|
||||
$gadai = Gadai::on($db_connection)->select('t_gadai', 'norujukan', 'nokp', 'semakbarcode', 'masa', 'berat', 'nilaimarhun', 'pinjaman', 'upah12', 'percentpinj', 'teller', 'nokp')
|
||||
->where('t_gadai', '=', $tarikh)
|
||||
->orderBy('teller')
|
||||
->orderBy('sirig')
|
||||
->get();
|
||||
|
||||
return $gadai->toArray();
|
||||
}, $active_cawangan_db_connection);
|
||||
|
||||
$gadai_data_with_key = array_combine($active_cawangan_name, $gadai_data);
|
||||
|
||||
$overall_data_gadai = array_map(function ($gadaian_cawangan) {
|
||||
return array_map(function ($gadai) {
|
||||
|
||||
$customer_info = Customer::on('mysql7')->where('kplama', '=', $gadai['nokp'])->orWhere('kpbaru', '=', $gadai['nokp'])->first()->toArray();
|
||||
|
||||
return [
|
||||
"t_gadai" => $gadai['t_gadai'],
|
||||
"norujukan" => $gadai['norujukan'],
|
||||
"nama" => $customer_info['nama'],
|
||||
"nokp" => $gadai['nokp'],
|
||||
"semakbarcode" => $gadai['semakbarcode'],
|
||||
"masa" => $gadai['masa'],
|
||||
"berat" => $gadai['berat'],
|
||||
"nilaimarhun" => $gadai['nilaimarhun'],
|
||||
"pinjaman" => $gadai['pinjaman'],
|
||||
"upah12" => $gadai['upah12'],
|
||||
"percentpinj" => $gadai['percentpinj'],
|
||||
"alamat1" => $customer_info['alamat1'],
|
||||
"teller" => $gadai['teller'],
|
||||
];
|
||||
|
||||
}, $gadaian_cawangan);
|
||||
|
||||
}, $gadai_data_with_key);
|
||||
|
||||
dump($overall_data_gadai);
|
||||
return $overall_data_gadai;
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use App\Cawangan;
|
||||
use App\Gadai;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Helper;
|
||||
|
||||
$tarikh = '2023-11-01';
|
||||
$active_cawangan_db_connection = Helper::getActiveCawanganDbConnection();
|
||||
|
||||
$active_cawangan_db_name = array_map(function($db_connection){
|
||||
return config('database.connections' . '.' . $db_connection. '.' . 'database' );
|
||||
}, $active_cawangan_db_connection);
|
||||
|
||||
$active_cawangan_detail = Cawangan::select('kodcaw', 'details_caw')->whereIn('db_name', $active_cawangan_db_name)->get()->toArray();
|
||||
|
||||
$summary_gadaian_data = array_map(function ($db_connection) use ($tarikh) {
|
||||
$gadai = Gadai::on($db_connection)
|
||||
->select(DB::raw('count(norujukan) as total_gadaian, sum(berat) as sum_berat, sum(nilaimarhun) as sum_nilai_marhun, sum(pinjaman) as sum_pinjaman'))
|
||||
->where('t_gadai', '=', $tarikh)
|
||||
->get();
|
||||
|
||||
return $gadai->toArray();
|
||||
}, $active_cawangan_db_connection);
|
||||
|
||||
$response = array_map(function($cawangan_detail, $summary_gadaian_data){
|
||||
return array_merge($cawangan_detail, $summary_gadaian_data[0]);
|
||||
}, $active_cawangan_detail, $summary_gadaian_data);
|
||||
|
||||
return json_encode($response);
|
||||
Reference in New Issue
Block a user