Merge branch 'production' of https://bitbucket.org/arrahn-pkb/api_arrahn into annur-rebate

This commit is contained in:
nurafrinaalimi16
2025-01-26 12:56:45 +08:00
41 changed files with 2928 additions and 531 deletions
+15 -1
View File
@@ -181,7 +181,14 @@ class OnePercent extends MasterServices
public function tebusUpdateAutoTawarruq($kodcaw,$norujukan,HttpRequest $request){
$tebus = new Penebusan($this->kodcaw,$this->cawangan_info);
$response = $tebus->tebusUpdateAutoTawarruq($this->kodcaw,$norujukan,$request);
$response = $tebus->transaksiPembiayaanSemula($this->kodcaw,$norujukan,$request);
return $response;
}
public function tebusUpdateTambahPinjaman($kodcaw,$norujukan,HttpRequest $request){
$tebus = new Penebusan($this->kodcaw,$this->cawangan_info);
$response = $tebus->transaksiTambahPembiayaan($this->kodcaw,$norujukan,$request);
return $response;
}
@@ -207,6 +214,13 @@ class OnePercent extends MasterServices
return $response;
}
public function tebusTransaksiUpdate($kodcaw,$norujukan,HttpRequest $request){
$tebus = new Penebusan($this->kodcaw,$this->cawangan_info);
$response = $tebus->tebusTransaksiUpdate($this->kodcaw,$norujukan,$request);
return $response;
}
/* End Penebusan */
public function test(){
+2 -1
View File
@@ -8,6 +8,7 @@ use App\Support\v1\GadaiV1 as Version1Gadai;
use App\Services\MasterServices;
use App\Support\v1\Penebusan;
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Support\Facades\Log;
class Tawarruq extends MasterServices
{
@@ -165,7 +166,7 @@ class Tawarruq extends MasterServices
public function tebusUpdateAutoTawarruq($kodcaw,$norujukan,HttpRequest $request){
$tebus = new Penebusan($this->kodcaw,$this->cawangan_info);
$response = $tebus->tebusUpdateAutoTawarruq($this->kodcaw,$norujukan,$request);
$response = $tebus->transaksiPembiayaanSemula($this->kodcaw,$norujukan,$request);
return $response;
}
+329 -3
View File
@@ -9,7 +9,10 @@ use App\KadarUpah;
use App\Support\v2\Calculation;
use Carbon\Carbon;
use App\Gadai;
use App\HargaEmas;
use App\InOutMar;
use App\KomuditiPurchase;
use App\KomuditiTransaksi;
use App\Marhun;
use App\Moratorium;
use App\Support\OnePercent;
@@ -188,7 +191,8 @@ class Penebusan
$bilang_bln = $tempoh - $gadai_data['tempohbayar'];
$tawarruqservices = new V1Calculation();
$keuntungan = $tawarruqservices->kiraanTawarruq($gadai_data['pinjaman'],$gadai_data['kadarupah']);
//$keuntungan = $tawarruqservices->kiraanTawarruq($gadai_data['pinjaman'],$gadai_data['kadarupah']);
$keuntungan = $tawarruqservices->kiraanTawarruq(($gadai_data['pinjaman'] !== null) ? $gadai_data['pinjaman'] : 0,($gadai_data['kadarupah'] !== null) ? $gadai_data['kadarupah'] : 0);
$upahsemasa_raw = $bilang_bln * $keuntungan;
$upah_semasa_round = number_format($upahsemasa_raw,2);
@@ -458,7 +462,7 @@ class Penebusan
//check duplicate
$data = Transaction::on('mysql7')->where('norujukan','=',$norujukan)->where('trans_type','=','T')->first();
if(!empty($data)){
return 'duplicate';
return response()->json('duplicate');
}
$gadai = Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->first();
@@ -601,7 +605,7 @@ class Penebusan
//check duplicate
$data = Transaction::on('mysql7')->where('norujukan','=',$norujukan)->where('trans_type','=','AT')->first();
if(!empty($data)){
return 'duplicate';
return response()->json('duplicate');
}
$update_gadai = Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->update(array(
@@ -771,4 +775,326 @@ class Penebusan
return $tempoh_gadai;
}
public function transaksiPembiayaanSemula($kodcaw,$norujukan,HttpRequest $request){
$cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
$gadai = Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->first();
$gadai = optional($gadai)->toArray();
//check duplicate
$data = Transaction::on('mysql7')->where('norujukan','=',$norujukan)->where('trans_type','=','AT')->first();
if(!empty($data)){
return 'duplicate';
}
if(!$gadai){
return 'gadai no-data';
}
$array_tebus = $request->only(['jeniskeuntungan', 'nopelanggan','jenistebus','addpinjaman','nowakil','marhun','teller','bakipinjaman','bakiupah']);
$array_trans = $request->only(['nopelanggan', 'noic','jenisAT','pinjaman','pinjamanbaru','marhun','teller','jenistebus']);
$array_tebus_base = $request->only(['totalbayaran', 'bakiupah','jeniskeuntungan','bakipinjaman','jenistebus','teller','nowakil','duitpelanggan']);
$array_trans_base = $request->only(['pinjaman','teller','pinjamanbaru']);
try{
DB::connection($cawangan['mysql'])->beginTransaction();
$this->saveTebus($kodcaw,$norujukan,$gadai,$cawangan,$array_tebus);
$this->createTransaction($cawangan,$kodcaw,$norujukan,$array_trans);
DB::connection('mysql7')->beginTransaction();
$this->saveTebusBase($kodcaw,$norujukan,$gadai,$array_tebus_base);
$this->createTransactionBase($norujukan,$kodcaw,$array_trans_base);
$array_response = [
'result' => 'success',
'data' => 1
];
DB::connection($cawangan['mysql'])->commit();
DB::connection('mysql7')->commit();
return response()->json($array_response);
}catch(\Exception $e){
DB::connection($cawangan['mysql'])->rollBack();
DB::connection('mysql7')->rollBack();
$array_response = [
'result' => 'error',
'data' => $e->getMessage()
];
Log::error('Transaction failed', [
'exception' => $e->getMessage(),
'line' => $e->getLine(),
'file' => $e->getFile(),
]);
return response()->json($array_response);
}
}
private function saveTebus($kodcaw,$norujukan,$gadai,$cawangan,$array_tebus){
$current = Carbon::now();
$rebet = 0;
$rebet = $this->rebetselepastawarruq($kodcaw,$norujukan);
$upah_rebet = $this->kiraUpahRebet($norujukan);
try{
Tebus::on($cawangan['mysql'])->create([
'kodcaw'=> $kodcaw,
'norujukan'=> $norujukan,
'nopelanggan' => $array_tebus['nopelanggan'],
'jenisbyr' => "TN",
'jenistebus' => $array_tebus['jenistebus'],
'glcode' => "0",
't_tebus' => $current->toDateString(),
'jbg' => $gadai['tempoh'],
'kadarupah' => $gadai['kadarupah'],
'nilaimarhun' => $gadai['nilaimarhun'],
'pinjaman' => $gadai['pinjaman'],
'ansuran' => 0.00,
'addpinjaman' => $array_tebus['addpinjaman'],
'bakipjm' => $array_tebus['bakipinjaman'],
'bakiupah' => $array_tebus['bakiupah'],
'terima' => "*",
'kodgl' => "0",
'nowakil' => $array_tebus['nowakil'],
'rebate' => $rebet,
'marhuntebus' => $array_tebus['marhun'],
'historygadaian' => $norujukan,
'tarikhbyrn' => $current->toDateString(),
'teller' => $array_tebus['teller'],
]);
}catch(\Exception $e){
throw new \Exception("Error in Tebus: " . $e->getMessage());
}
}
private function saveTebusBase($kodcaw,$norujukan,$gadai,$array_tebus_base){
$current = Carbon::now();
if($array_tebus_base['jenistebus'] == 'AT'){
$bayaran_pembiayaan = $array_tebus_base['totalbayaran'] - $array_tebus_base['bakiupah'];
}else{
$bayaran_pembiayaan = $array_tebus_base['bakipinjaman'];
}
$margin_semasa = ($gadai['bakipjm']/$gadai['nilaimarhun']) * 100;
$harga_tebus = $this->hargaTebusTawarruq($kodcaw,$norujukan,$array_tebus_base['jeniskeuntungan']);
$upah_rebet = $this->kiraUpahRebetSebenar($kodcaw,$norujukan);
$bayaran_pembiayaan = $array_tebus_base['totalbayaran'] - $array_tebus_base['bakiupah'];
$upah_rebet_sebenar = $harga_tebus - $gadai['bakipjm'];
try{
TransaksiKeuntungan::on('mysql7')->create([
'norujukan' => $norujukan,
'bayaran_keuntungan' => $array_tebus_base['bakiupah'],
'bayaran_pembiayaan' => $bayaran_pembiayaan,
'keuntungan_rebet' => $array_tebus_base['bakiupah'],
'tarikh_bayaran'=> $current,
'kodcaw'=> $kodcaw
]);
if($array_tebus_base['jenistebus'] == 'P'){
$duit_masuk = $array_tebus_base['bakiupah'];
}else if($array_tebus_base['jenistebus'] == 'AT'){
$duit_masuk = $array_tebus_base['totalbayaran'];
}else{
$duit_masuk = $harga_tebus['harga'];
}
$baki_hargajualan = ($array_tebus_base['jenistebus'] == 'P') ? $gadai['bakihargajualan'] = $array_tebus_base['bakiupah'] : $gadai['bakihargajualan'] - $array_tebus_base['bakipinjaman'];
Transaction::on('mysql7')->create([
'trans_type' => $array_tebus_base['jenistebus'],
'norujukan' => $norujukan,
'nokp' => $gadai['nokp'],
'teller' => $array_tebus_base['teller'],
'kodcaw' => $kodcaw,
'duit_masuk' => $array_tebus_base['totalbayaran'],
'created_at' => $current,
'bakiharga' => 0,
'duitpelanggan' => isset($array_tebus_base['duitpelanggan']) ? $array_tebus_base['duitpelanggan'] : 0,
'bakipelanggan' => isset($array_tebus_base['bakipelanggan']) ? $array_tebus_base['bakipelanggan'] : 0
]);
}catch(\Exception $e){
throw new \Exception("Error in TebusBase: " . $e->getMessage());
}
}
private function createTransaction($cawangan,$kodcaw,$norujukan,$array_trans){
$current = Carbon::now();
/* Create Gadai */
$gadaiTransaction = Gadai::on($cawangan['mysql'])->where('t_gadai','=',$current->toDateString())->orderBy('sirig', 'desc')->first();
$gadaiTransaction = optional($gadaiTransaction)->toArray();
$new_sirig = ($gadaiTransaction) ? $gadaiTransaction['sirig'] + 1 : 1;
$curent_year = substr($current->year,2,2);
$curent_month = sprintf("%02d", $current->month);
$current_day = sprintf("%02d", $current->day);
$new_transaction = sprintf("%04d",$new_sirig);
$new_sag_number = strtoupper($cawangan['kodcaw']) . $curent_year . $curent_month . $current_day . '-' .$new_transaction;
try{
Gadai::on($cawangan['mysql'])->create([
'kodcaw'=>$kodcaw,
'norujukan' => $new_sag_number,
'nopelanggan' => $array_trans['nopelanggan'],
'nokp' => $array_trans['noic'],
'sirig' => $new_sirig,
't_gadai' => $current->toDateString(),
'sttebus' => '',
'masa' =>$current->toTimeString(),
'tagbaru' => 1
]);
$marhun = Marhun::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->get();
foreach($marhun as $marhunloop){
$karatbaru = (strpos($marhunloop['karat'], '.') !== false) ? str_replace('-','.',$marhunloop['karat']) : $marhunloop['karat'];
$karatbaru = $marhunloop['karat'];
$harga_emas = HargaEmas::on('mysql7')->select('harga')->where('karat','=',$karatbaru)->first();
if(isset($array_trans['jenisAT'])){
$harga = ($array_trans['jenisAT'] == 'hargalama') ? $marhunloop['harga'] : $harga_emas['harga'];
}else{
$harga = $harga_emas['harga'];
}
$nilai_marhun = (floatval($harga) * floatval($marhunloop['berat']));
$marhunToCreate = [
'kodcaw'=> $kodcaw,
'norujukan'=> $new_sag_number,
't_gadai' => $current->toDateString(),
'bil' => $marhunloop['kuantiti'],
'marhun' => $marhunloop['marhun'],
'nota' => $marhunloop['nota'],
'tag_permata' => $marhunloop['tag_permata'] != null ? $marhunloop['tag_permata'] : 0,
'berat_batu_permata' => $marhunloop['berat_batu_permata'] != null ? $marhunloop['berat_batu_permata'] : '',
'karat' => $marhunloop['karat'],
'berat' => $marhunloop['berat'],
'harga' => $harga,
'n_marhun' => $nilai_marhun
];
if($marhunToCreate['berat_batu_permata'] == '') {
$marhunToCreate['berat_batu_permata'] = null;
};
Marhun::on($cawangan['mysql'])->create($marhunToCreate);
}
$total_nilai_marhun = Marhun::on($cawangan['mysql'])->where('norujukan',$new_sag_number)->sum('n_marhun');
$total_bil_marhun = Marhun::on($cawangan['mysql'])->where('norujukan',$new_sag_number)->sum('bil');
$total_berat_marhun = Marhun::on($cawangan['mysql'])->where('norujukan',$new_sag_number)->sum('berat');
$margin_pinjaman = ($array_trans['pinjamanbaru'] / $total_nilai_marhun);
$t_matang = Carbon::now();
$t_matang = $t_matang->addMonths(6)->subDays(1);
$t_kontrak = Carbon::now();
$t_kontrak = $t_kontrak->addMonths(12)->subDays(1);
$kadar_raw = KadarUpah::where('min','<=',$total_nilai_marhun)->where('max','>=',$total_nilai_marhun)->where('group_id',$cawangan['group_id'])->first();
$onepercentservices = new Calculation();
$keuntungan = $onepercentservices->kiraanKeuntunganSebenar($array_trans['pinjamanbaru'],$total_nilai_marhun,$kadar_raw['kadarujrah'],$margin_pinjaman);
$upah1 = $onepercentservices->getRoundedMethod($keuntungan['keuntungan']);
$upah6 = $onepercentservices->getRoundedMethod($upah1 * 6);
$upah12 = $onepercentservices->getRoundedMethod($upah1 * 12);
$ujrah = $onepercentservices->getRoundedMethod($keuntungan['ujrah']);
$onepercent = $onepercentservices->getRoundedMethod($keuntungan['onepercent']);
$akrual_ujrah = $onepercentservices->getRoundedMethod($keuntungan['ujrah'] * 0.5);
$akrual_onepercent = $onepercentservices->getRoundedMethod($keuntungan['onepercent'] * 0.5);
$hargajualan = $array_trans['pinjaman'] + $upah12;
Gadai::on($cawangan['mysql'])
->where('norujukan','=',$new_sag_number)
->update(array(
'nilaimarhun' => $total_nilai_marhun,
'berat'=> $total_berat_marhun,
'pinjaman' => $array_trans['pinjamanbaru'],
'kadarupah' => $kadar_raw['kadarujrah'],
'marhun' => $array_trans['marhun'],
'percentpinj' => $margin_pinjaman,
'teller' => $array_trans['teller'],
'bakipjm' => $array_trans['pinjamanbaru'],
'bakiupah' => $upah6,
'jbg' => 0.5,
't_update' => $current->toDateString(),
'cetak' => 0,
'upah12' => $upah12,
'hargajualan' => $hargajualan,
'bakihargajualan' => $hargajualan,
'tagserah' => 0,
't_matang' => $t_matang,
't_matang1' => $t_kontrak,
'tempoh' => 0.5,
'kuantiti' => $total_bil_marhun,
'tagkomuditi' => 0,
'upahdibayar' => 0.00,
'tempohbayar' => 0.00,
'lelong_tawarruq' => 1,
'kadarujrah' => $kadar_raw['kadarujrah'],
'ujrah' => $ujrah,
'onepercent' => $onepercent,
'margin_semasa' => $margin_pinjaman,
'tagbaru' => 1,
'tunggakanujrah' => $akrual_ujrah,
'tunggakanonepercent' => $akrual_onepercent,
'historygadaian' => $norujukan,
'tag_tebus' => $array_trans['jenistebus'],
));
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->update(array(
'sttebus' => "T",
't_update' => $current->toDateString(),
));
}catch(\Exception $e){
throw new \Exception("Error in Transaction: " . $e->getMessage());
}
}
private function createTransactionBase($norujukan,$kodcaw,$array_trans_base){
$current = Carbon::now();
$komuditi_current = KomuditiPurchase::on('mysql7')->where('date_purchase','=',$current->toDateString())->orderBy('created_at','desc')->first();
$unit_current = isset($komuditi_current['unit_komuditi']) ? $komuditi_current['unit_komuditi']: 0;
try{
$update_unit = $unit_current - $array_trans_base['pinjamanbaru'];
KomuditiPurchase::on('mysql7')->where('id','=',$komuditi_current['id'])->update([
'unit_komuditi' => $update_unit
]);
KomuditiTransaksi::on('mysql7')->create([
'unit_komuditi' => $update_unit,
'nosijil'=>$komuditi_current['no_sijil'],
'norujukan' => $norujukan,
'kodcaw' => $kodcaw,
'teller' => $array_trans_base['teller'],
'masa' => $current->toTimeString(),
'tarikh' => $current->toDateString()
]);
}catch(\Exception $e){
throw new \Exception("Error in TransactionBase: " . $e->getMessage());
}
}
}
+10 -4
View File
@@ -177,8 +177,11 @@ class GadaiV2
$bilang_bln = $tempoh - $gadai_data['tempohbayar'];
$upah = $onepercentservices->kiraanKeuntunganSebenar($gadai_data['pinjaman'],$gadai_data['nilaimarhun'],$gadai_data['kadarupah'],$gadai_data['percentpinj']);
$upahsemasa_raw = $bilang_bln * $upah['keuntungan'];
$upahsemasa_raw = $onepercentservices->getRoundedMethod($upahsemasa_raw);
//$upahsemasa_raw = $bilang_bln * $upah['keuntungan'];
$onepercent = $onepercentservices->getRoundedMethod($bilang_bln * $upah['onepercent']);
$ujrah = $onepercentservices->getRoundedMethod($bilang_bln * $upah['ujrah']);
$upahsemasa_raw = $onepercent + $ujrah;
$upahsemasa_raw = $onepercentservices->getRoundedMethod($upahsemasa_raw);
return $upahsemasa_raw;
}
@@ -203,7 +206,10 @@ class GadaiV2
$upah = $onepercentservices->kiraanKeuntunganRebet($gadai_data['bakipjm'],$gadai_data['nilaimarhun'],$gadai_data['kadarupah'],$gadai_data['margin_semasa']);
$upahrebet_raw = $bilang_bln * $upah['keuntungan'];
//$upahrebet_raw = $bilang_bln * $upah['keuntungan'];
$onepercent = $onepercentservices->getRoundedMethod($bilang_bln * $upah['onepercent']);
$ujrah = $onepercentservices->getRoundedMethod($bilang_bln * $upah['ujrah']);
$upahrebet_raw = $onepercent + $ujrah;
$upahrebet_raw = $onepercentservices->getRoundedMethod($upahrebet_raw);
return $upahrebet_raw;
@@ -240,7 +246,7 @@ class GadaiV2
$margin_semasa = ($gadai['bakipjm'] / $gadai['nilaimarhun']) * 100;
$keuntungan = $onepercent->kiraanKeuntunganSebenar($gadai['bakipjm'],$gadai['nilaimarhun'],$gadai['kadarupah'],$margin_semasa);
$keuntungan = $onepercent->kiraanKeuntunganRebet($gadai['bakipjm'],$gadai['nilaimarhun'],$gadai['kadarupah'],$gadai['margin_semasa']);
$ujrahasal = $onepercent->kiraanUjrahSebenar($gadai['nilaimarhun'],$gadai['kadarupah'],$gadai['percentpinj']);
$onepercentasal = $gadai['pinjaman'] * (1/100);
+434 -4
View File
@@ -9,8 +9,13 @@ use App\KadarUpah;
use App\Support\v2\Calculation;
use Carbon\Carbon;
use App\Gadai;
use App\HargaEmas;
use App\InOutMar;
use App\KomuditiPurchase;
use App\KomuditiTransaksi;
use App\Marhun;
use App\FailoverTransaction;
use App\FailoverTransactionFailed;
use App\Moratorium;
use App\Support\OnePercent;
use App\Tebus;
@@ -200,8 +205,9 @@ class Penebusan
$bilang_bln = $tempoh - $gadai_data['tempohbayar'];
$upah = $onepercentservices->kiraanKeuntunganSebenar($gadai_data['pinjaman'],$gadai_data['nilaimarhun'],$gadai_data['kadarupah'],$gadai_data['percentpinj']);
$upahsemasa_raw = $bilang_bln * $upah['keuntungan'];
$upahsemasa_raw = $onepercentservices->getRoundedMethod($upahsemasa_raw);
$onepercent = $onepercentservices->getRoundedMethod($bilang_bln * $upah['onepercent']);
$ujrah = $onepercentservices->getRoundedMethod($bilang_bln * $upah['ujrah']);
$upahsemasa_raw = $onepercentservices->getRoundedMethod($onepercent + $ujrah);
return $upahsemasa_raw;
}
@@ -483,7 +489,7 @@ class Penebusan
$data = Transaction::on('mysql7')->where('norujukan','=',$norujukan)->whereIn('trans_type', ['P', 'T', 'S'])->first();
if(!empty($data)){
return 'duplicate';
return response()->json('duplicate');
}
$ujrah=0;$onepercent=0;$rounded=0;
@@ -649,7 +655,7 @@ class Penebusan
$data = Transaction::on('mysql7')->where('norujukan','=',$norujukan)->where('trans_type','=','AT')->first();
if(!empty($data)){
return 'duplicate';
return response()->json('duplicate');
}
$ujrah=0;$onepercent=0;$rounded=0;
@@ -917,4 +923,428 @@ class Penebusan
}
public function transaksiPembiayaanSemula($kodcaw,$norujukan,HttpRequest $request){
$cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
$gadai = Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->first();
$gadai = optional($gadai)->toArray();
//check duplicate
$data = Transaction::on('mysql7')->where('norujukan','=',$norujukan)->where('trans_type','=','AT')->first();
if(!empty($data)){
return 'duplicate';
}
if(!$gadai){
return 'gadai no-data';
}
$array_tebus = $request->only(['jeniskeuntungan', 'nopelanggan','jenistebus','addpinjaman','nowakil','marhun','teller']);
$array_trans = $request->only(['nopelanggan', 'noic','jenisAT','pinjaman','pinjamanbaru','marhun','teller','jenistebus']);
$array_tebus_base = $request->only(['totalbayaran', 'bakiupah','jeniskeuntungan','bakipinjaman','jenistebus','teller','nowakil','duitpelanggan']);
$array_trans_base = $request->only(['pinjaman','teller','pinjamanbaru']);
try{
DB::connection($cawangan['mysql'])->beginTransaction();
$this->saveTebus($kodcaw,$norujukan,$gadai,$cawangan,$array_tebus);
$this->createTransaction($cawangan,$kodcaw,$norujukan,$array_trans);
DB::connection('mysql7')->beginTransaction();
$this->saveTebusBase($kodcaw,$norujukan,$gadai,$array_tebus_base);
$this->createTransactionBase($norujukan,$kodcaw,$array_trans_base);
$array_response = [
'result' => 'success',
'data' => 1
];
DB::connection($cawangan['mysql'])->commit();
DB::connection('mysql7')->commit();
return response()->json($array_response);
}catch(\Exception $e){
DB::connection($cawangan['mysql'])->rollBack();
DB::connection('mysql7')->rollBack();
$array_response = [
'result' => 'error',
'data' => $e->getMessage()
];
Log::error('Transaction failed', [
'exception' => $e->getMessage(),
'line' => $e->getLine(),
'file' => $e->getFile(),
]);
return response()->json($array_response);
}
}
public function transaksiTambahPembiayaan($kodcaw,$norujukan,HttpRequest $request){
$current = Carbon::now();
$current = new Carbon();
$cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
$gadai = Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->first();
$gadai = optional($gadai)->toArray();
//check duplicate
$data = Transaction::on('mysql7')->where('norujukan','=',$norujukan)->where('trans_type','=','AT')->first();
if(!empty($data)){
return 'duplicate';
}
$array_tebus = $request->only(['jeniskeuntungan', 'nopelanggan','jenistebus','addpinjaman','nowakil','marhun','teller']);
$array_trans = $request->only(['nopelanggan', 'noic','pinjaman','pinjamanbaru','marhun','teller','jenistebus']);
$array_tebus_base = $request->only(['totalbayaran', 'bakiupah','jeniskeuntungan','bakipinjaman','jenistebus','teller','nowakil','duitpelanggan']);
$array_trans_base = $request->only(['pinjaman','teller','pinjamanbaru']);
try{
DB::connection($cawangan['mysql'])->beginTransaction();
$this->saveTebus($kodcaw,$norujukan,$gadai,$cawangan,$array_tebus);
$this->createTransaction($cawangan,$kodcaw,$norujukan,$array_trans);
DB::connection('mysql7')->beginTransaction();
$this->saveTebusBase($kodcaw,$norujukan,$gadai,$array_tebus_base);
$this->createTransactionBase($norujukan,$kodcaw,$array_trans_base);
$array_response = [
'result' => 'success',
'data' => 1
];
DB::connection($cawangan['mysql'])->commit();
DB::connection('mysql7')->commit();
return response()->json($array_response);
}catch(\Exception $e){
DB::connection($cawangan['mysql'])->rollBack();
DB::connection('mysql7')->rollBack();
$array_response = [
'result' => 'error',
'data' => $e->getMessage()
];
Log::error('Transaction failed', [
'exception' => $e->getMessage(),
'line' => $e->getLine(),
'file' => $e->getFile(),
]);
return response()->json($array_response);
}
}
private function transaksiPenyelesaianSebahagian($kodcaw,$norujukan,HttpRequest $request){
$current = Carbon::now();
$current = new Carbon();
$cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
$gadai = Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->first();
try{
//check duplicate
$data = Transaction::on('mysql7')->where('norujukan','=',$norujukan)->where('trans_type','=','AT')->first();
if(!empty($data)){
return 'duplicate';
}
}catch(\Exception $e){
}
}
private function saveTebus($kodcaw,$norujukan,$gadai,$cawangan,$array_tebus){
$current = Carbon::now();
/*
Rebet Still looking for the calculation
*/
$rebet = 0;
$harga_tebus = $this->hargaTebusTawarruq($kodcaw,$norujukan,$array_tebus['jeniskeuntungan']);
$upah_tebus = $this->kiraupahTawarruq($norujukan);
$upah_rebet = $this->kiraUpahRebetSebenar($kodcaw,$norujukan);
$array_onepercent_rebet = $this->kiraupahUjrahOnePercentRebate($kodcaw,$norujukan);
try{
$onepercent_rebet = $array_onepercent_rebet['onepercent'] - $upah_rebet['onepercent'];
$ujrah_rebet = $array_onepercent_rebet['ujrah'] - $upah_rebet['ujrah'];
Tebus::on($cawangan['mysql'])->create([
'kodcaw'=> $kodcaw,
'norujukan'=> $norujukan,
'nopelanggan' => $array_tebus['nopelanggan'],
'jenisbyr' => "TN",
'jenistebus' => $array_tebus['jenistebus'],
'glcode' => "0",
't_tebus' => $current->toDateString(),
'jbg' => $gadai['tempoh'],
'kadarupah' => $gadai['kadarupah'],
'nilaimarhun' => $gadai['nilaimarhun'],
'pinjaman' => $gadai['pinjaman'],
'ansuran' => 0.00,
'addpinjaman' => $array_tebus['addpinjaman'],
'bakipjm' => $harga_tebus['harga'],
'bakiupah' => $upah_tebus,
'terima' => "*",
'kodgl' => "0",
'nowakil' => $array_tebus['nowakil'],
'rebate' => $rebet,
'marhuntebus' => $array_tebus['marhun'],
'historygadaian' => $norujukan,
'tarikhbyrn' => $current->toDateString(),
'teller' => $array_tebus['teller'],
'ujrah' => $ujrah_rebet,
'onepercent' => $onepercent_rebet,
]);
}catch(\Exception $e){
throw new \Exception("Error in Tebus: " . $e->getMessage());
}
}
private function saveTebusBase($kodcaw,$norujukan,$gadai,$array_tebus_base){
$current = Carbon::now();
if($array_tebus_base['jenistebus'] == 'AT'){
$bayaran_pembiayaan = $array_tebus_base['totalbayaran'] - $array_tebus_base['bakiupah'];
}else{
$bayaran_pembiayaan = $array_tebus_base['bakipinjaman'];
}
$margin_semasa = ($gadai['bakipjm']/$gadai['nilaimarhun']) * 100;
$harga_tebus = $this->hargaTebusTawarruq($kodcaw,$norujukan,$array_tebus_base['jeniskeuntungan']);
$upah_tebus = $this->kiraupahTawarruq($norujukan);
$array_onepercent = $this->kiraupahUjrahOnePercent($kodcaw,$norujukan);
$array_onepercent_rebet = $this->kiraupahUjrahOnePercentRebate($kodcaw,$norujukan);
$upah_rebet = $this->kiraUpahRebetSebenar($kodcaw,$norujukan);
$ujrah = $array_onepercent['ujrah'];
$onepercent = $array_onepercent['onepercent'];
$onepercent_rebet = $array_onepercent_rebet['onepercent'] - $upah_rebet['onepercent'];
$ujrah_rebet = $array_onepercent_rebet['ujrah'] - $upah_rebet['ujrah'];
$upah_rebet_sebenar = $harga_tebus['harga'] - $gadai['bakipjm'];
try{
TransaksiKeuntungan::on('mysql7')->create([
'norujukan' => $norujukan,
'bayaran_keuntungan' => $upah_tebus,
'bayaran_pembiayaan' => $bayaran_pembiayaan,
'keuntungan_rebet' => $upah_rebet_sebenar,
'tarikh_bayaran'=> $current,
'kodcaw'=> $kodcaw,
'ujrah' => ($array_tebus_base['jeniskeuntungan'] == 'onepercent') ? $ujrah : 0,
'ujrah_rebet' => ($array_tebus_base['jeniskeuntungan'] == 'onepercent') ? $ujrah_rebet : 0,
'onepercent' => ($array_tebus_base['jeniskeuntungan'] == 'onepercent') ? $onepercent : 0,
'onepercent_rebet' => ($array_tebus_base['jeniskeuntungan'] == 'onepercent') ? $onepercent_rebet : 0,
'margin_semasa' => ($array_tebus_base['jeniskeuntungan'] == 'onepercent') ? $margin_semasa : 0,
'tempoh' => $gadai['tempoh'],
'beza_tempoh' => ($gadai['tempoh'] - $gadai['tempohbayar']),
]);
if($array_tebus_base['jenistebus'] == 'P'){
$duit_masuk = $array_tebus_base['bakiupah'];
}else if($array_tebus_base['jenistebus'] == 'AT'){
$duit_masuk = $array_tebus_base['totalbayaran'];
}else{
$duit_masuk = $harga_tebus['harga'];
}
$baki_hargajualan = ($array_tebus_base['jenistebus'] == 'P') ? $gadai['bakihargajualan'] = $array_tebus_base['bakiupah'] : $gadai['bakihargajualan'] - $array_tebus_base['bakipinjaman'];
Transaction::on('mysql7')->create([
'trans_type' => $array_tebus_base['jenistebus'],
'norujukan' => $norujukan,
'nokp' => $gadai['nokp'],
'teller' => $array_tebus_base['teller'],
'kodcaw' => $kodcaw,
'duit_masuk' => $duit_masuk,
'created_at' => $current,
'bakiharga' => $baki_hargajualan,
'nowakil' => (int)$array_tebus_base['nowakil'],
'duitpelanggan' => isset($array_tebus_base['duitpelanggan']) ? $array_tebus_base['duitpelanggan'] : 0,
'bakipelanggan' => isset($array_tebus_base['bakipelanggan']) ? $array_tebus_base['bakipelanggan'] : 0
]);
}catch(\Exception $e){
throw new \Exception("Error in TebusBase: " . $e->getMessage());
}
}
private function createTransaction($cawangan,$kodcaw,$norujukan,$array_trans){
$current = Carbon::now();
/* Create Gadai */
$gadaiTransaction = Gadai::on($cawangan['mysql'])->where('t_gadai','=',$current->toDateString())->orderBy('sirig', 'desc')->first();
$gadaiTransaction = optional($gadaiTransaction)->toArray();
$new_sirig = ($gadaiTransaction) ? $gadaiTransaction['sirig'] + 1 : 1;
$curent_year = substr($current->year,2,2);
$curent_month = sprintf("%02d", $current->month);
$current_day = sprintf("%02d", $current->day);
$new_transaction = sprintf("%04d",$new_sirig);
$new_sag_number = strtoupper($cawangan['kodcaw']) . $curent_year . $curent_month . $current_day . '-' .$new_transaction;
try{
Gadai::on($cawangan['mysql'])->create([
'kodcaw'=>$kodcaw,
'norujukan' => $new_sag_number,
'nopelanggan' => $array_trans['nopelanggan'],
'nokp' => $array_trans['noic'],
'sirig' => $new_sirig,
't_gadai' => $current->toDateString(),
'sttebus' => '',
'masa' =>$current->toTimeString(),
'tagbaru' => 1
]);
$marhun = Marhun::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->get();
foreach($marhun as $marhunloop){
$karatbaru = (strpos($marhunloop['karat'], '.') !== false) ? str_replace('-','.',$marhunloop['karat']) : $marhunloop['karat'];
$karatbaru = $marhunloop['karat'];
$harga_emas = HargaEmas::on('mysql7')->select('harga')->where('karat','=',$karatbaru)->first();
if(isset($array_trans['jenisAT'])){
$harga = ($array_trans['jenisAT'] == 'hargalama') ? $marhunloop['harga'] : $harga_emas['harga'];
}else{
$harga = $harga_emas['harga'];
}
$nilai_marhun = (floatval($harga) * floatval($marhunloop['berat']));
$marhunToCreate = [
'kodcaw'=> $kodcaw,
'norujukan'=> $new_sag_number,
't_gadai' => $current->toDateString(),
'bil' => $marhunloop['kuantiti'],
'marhun' => $marhunloop['marhun'],
'nota' => $marhunloop['nota'],
'tag_permata' => $marhunloop['tag_permata'] != null ? $marhunloop['tag_permata'] : 0,
'berat_batu_permata' => $marhunloop['berat_batu_permata'] != null ? $marhunloop['berat_batu_permata'] : '',
'karat' => $marhunloop['karat'],
'berat' => $marhunloop['berat'],
'harga' => $harga,
'n_marhun' => $nilai_marhun
];
if($marhunToCreate['berat_batu_permata'] == '') {
$marhunToCreate['berat_batu_permata'] = null;
};
Marhun::on($cawangan['mysql'])->create($marhunToCreate);
}
$total_nilai_marhun = Marhun::on($cawangan['mysql'])->where('norujukan',$new_sag_number)->sum('n_marhun');
$total_bil_marhun = Marhun::on($cawangan['mysql'])->where('norujukan',$new_sag_number)->sum('bil');
$total_berat_marhun = Marhun::on($cawangan['mysql'])->where('norujukan',$new_sag_number)->sum('berat');
$margin_pinjaman = ($array_trans['pinjamanbaru'] / $total_nilai_marhun);
$t_matang = Carbon::now();
$t_matang = $t_matang->addMonths(6)->subDays(1);
$t_kontrak = Carbon::now();
$t_kontrak = $t_kontrak->addMonths(12)->subDays(1);
$kadar_raw = KadarUpah::where('min','<=',$total_nilai_marhun)->where('max','>=',$total_nilai_marhun)->where('group_id',$cawangan['group_id'])->first();
$onepercentservices = new Calculation();
$keuntungan = $onepercentservices->kiraanKeuntunganSebenar($array_trans['pinjamanbaru'],$total_nilai_marhun,$kadar_raw['kadarujrah'],$margin_pinjaman);
$upah1 = $onepercentservices->getRoundedMethod($keuntungan['keuntungan']);
$upah6 = $onepercentservices->getRoundedMethod($upah1 * 6);
$upah12 = $onepercentservices->getRoundedMethod($upah1 * 12);
$ujrah = $onepercentservices->getRoundedMethod($keuntungan['ujrah']);
$onepercent = $onepercentservices->getRoundedMethod($keuntungan['onepercent']);
$akrual_ujrah = $onepercentservices->getRoundedMethod($keuntungan['ujrah'] * 0.5);
$akrual_onepercent = $onepercentservices->getRoundedMethod($keuntungan['onepercent'] * 0.5);
$hargajualan = $array_trans['pinjamanbaru'] + $upah12;
Gadai::on($cawangan['mysql'])
->where('norujukan','=',$new_sag_number)
->update(array(
'nilaimarhun' => $total_nilai_marhun,
'berat'=> $total_berat_marhun,
'pinjaman' => $array_trans['pinjamanbaru'],
'kadarupah' => $kadar_raw['kadarujrah'],
'marhun' => $array_trans['marhun'],
'percentpinj' => $margin_pinjaman,
'teller' => $array_trans['teller'],
'bakipjm' => $array_trans['pinjamanbaru'],
'bakiupah' => $upah6,
'jbg' => 0.5,
't_update' => $current->toDateString(),
'cetak' => 0,
'upah12' => $upah12,
'hargajualan' => $hargajualan,
'bakihargajualan' => $hargajualan,
'tagserah' => 0,
't_matang' => $t_matang,
't_matang1' => $t_kontrak,
'tempoh' => 0.5,
'kuantiti' => $total_bil_marhun,
'tagkomuditi' => 0,
'upahdibayar' => 0.00,
'tempohbayar' => 0.00,
'lelong_tawarruq' => 1,
'kadarujrah' => $kadar_raw['kadarujrah'],
'ujrah' => $ujrah,
'onepercent' => $onepercent,
'margin_semasa' => $margin_pinjaman,
'tagbaru' => 1,
'tunggakanujrah' => $akrual_ujrah,
'tunggakanonepercent' => $akrual_onepercent,
'historygadaian' => $norujukan,
'tag_tebus' => $array_trans['jenistebus'],
));
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->update(array(
'sttebus' => "T",
't_update' => $current->toDateString(),
));
}catch(\Exception $e){
throw new \Exception("Error in Transaction: " . $e->getMessage());
}
}
private function createTransactionBase($norujukan,$kodcaw,$array_trans_base){
$current = Carbon::now();
$komuditi_current = KomuditiPurchase::on('mysql7')->where('date_purchase','=',$current->toDateString())->orderBy('created_at','desc')->first();
$unit_current = isset($komuditi_current['unit_komuditi']) ? $komuditi_current['unit_komuditi']: 0;
try{
$update_unit = $unit_current - $array_trans_base['pinjamanbaru'];
KomuditiPurchase::on('mysql7')->where('id','=',$komuditi_current['id'])->update([
'unit_komuditi' => $update_unit
]);
KomuditiTransaksi::on('mysql7')->create([
'unit_komuditi' => $update_unit,
'nosijil'=>$komuditi_current['no_sijil'],
'norujukan' => $norujukan,
'kodcaw' => $kodcaw,
'teller' => $array_trans_base['teller'],
'masa' => $current->toTimeString(),
'tarikh' => $current->toDateString()
]);
}catch(\Exception $e){
throw new \Exception("Error in TransactionBase: " . $e->getMessage());
}
}
}