added database transaction in hapus tebus and gadai and put auto tawarruq in V1

This commit is contained in:
Hafifie PKB
2024-11-25 12:02:46 +08:00
parent 438908a9ce
commit f986e0a515
2 changed files with 558 additions and 179 deletions
+236 -179
View File
@@ -35,7 +35,7 @@ use Illuminate\Http\Request;
use App\Services\MasterServices;
use App\Support\OnePercent;
use App\Services\Transaction\AnsuranServices as AnsServ;
use Log;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
@@ -2017,159 +2017,188 @@ class GadaiController extends Controller
$audit->updated_at = $current;
$audit->save();
if($gadai[0]['historygadaian'] != "0" && $gadai[0]['historygadaian'] != '')
{
if($tebus['tagterima'] == 1)
try{
DB::connection($cawangan['mysql'])->beginTransaction();
DB::connection('mysql7')->beginTransaction();
if($gadai[0]['historygadaian'] != "0" && $gadai[0]['historygadaian'] != '')
{
if($tebus['jenistebus'] == 'P')
if($tebus['tagterima'] == 1)
{
if($tebus['jenistebus'] == 'P')
{
$totalkeluar = $keluar - ($tebus['addpinjaman'] + $tebus['pinjaman']);
$totalmasuk = $masuk - ($tebus['bakiupah'] + $tebus['pinjaman']);
$totalkeluar = $keluar - ($tebus['addpinjaman'] + $tebus['pinjaman']);
$totalmasuk = $masuk - ($tebus['bakiupah'] + $tebus['pinjaman']);
// $bakiakhir = $bakiawal - $totalkeluar + $totalmasuk + $pendahuluan - $serahan;
$bakiakhir = $baki - $tebus['bakiupah'] + $tebus['addpinjaman'];
// $bakiakhir = $bakiawal - $totalkeluar + $totalmasuk + $pendahuluan - $serahan;
$bakiakhir = $baki - $tebus['bakiupah'] + $tebus['addpinjaman'];
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $totalkeluar,
'masuk' => $totalmasuk,
'baki' => $bakiakhir
));
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $totalkeluar,
'masuk' => $totalmasuk,
'baki' => $bakiakhir
));
}else{
$totalmasuk = $masuk - ($tebus['pinjaman']+$tebus['bakiupah']);
$totalkeluar = $keluar - (($tebus['pinjaman'] + $tebus['bakiupah']) - $tebus['bakipjm']);
// $bakiakhir = $bakiawal - $totalkeluar + $totalmasuk + $pendahuluan - $serahan;
if($tebus['jenistebus'] == 'S')
$bakiakhir = $baki - $tebus['bakipjm'];
else
$bakiakhir = $bakiawal - $totalkeluar + $totalmasuk + $pendahuluan - $serahan;
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $totalkeluar,
'masuk' => $totalmasuk,
'baki' => $bakiakhir
));
}
}else{
$totalmasuk = $masuk - ($tebus['pinjaman']+$tebus['bakiupah']);
$totalkeluar = $keluar - (($tebus['pinjaman'] + $tebus['bakiupah']) - $tebus['bakipjm']);
// $bakiakhir = $bakiawal - $totalkeluar + $totalmasuk + $pendahuluan - $serahan;
$bakiakhir = $baki;
if($tebus['jenistebus'] == 'S')
$bakiakhir = $baki - $tebus['bakipjm'];
else
$bakiakhir = $bakiawal - $totalkeluar + $totalmasuk + $pendahuluan - $serahan;
if($tebus['jenistebus'] == 'P')
{
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $totalkeluar,
'masuk' => $totalmasuk,
'baki' => $bakiakhir
));
$bakiakhir = $bakiakhir - $tebus['bakiupah'] + $tebus['addpinjaman'];
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $keluar,
'masuk' => $masuk,
'baki' => $bakiakhir
));
}else{
$totalmasuk = $masuk;
$totalkeluar = $keluar;
if($tebus['jenistebus'] == 'S')
$bakiakhir = ($bakiakhir - $tebus['bakipjm']);
else
$bakiakhir = $bakiawal - $totalkeluar + $totalmasuk + $pendahuluan - $serahan;
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $keluar,
'masuk' => $masuk,
'baki' => $bakiakhir
));
}
}
}else{
$bakiakhir = $baki;
if($tebus['jenistebus'] == 'P')
Gadai::on($cawangan['mysql'])->where('norujukan','=',$gadai[0]['historygadaian'])->update(['sttebus' => '']);
$saved_historygadaian = $gadai[0]['historygadaian'];
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$gadai[0]['historygadaian'])->orderBy('id', 'desc')->limit(1)->delete();
Transaction::on('mysql7')->where('norujukan','=',$gadai[0]['historygadaian'])->orderBy('id', 'desc')->limit(1)->delete();
Tebus::on($cawangan['mysql'])->where('norujukan','=',$gadai[0]['historygadaian'])->delete();
Marhun::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
$gadaiDouble = Gadai::on($cawangan['mysql'])->where([['historygadaian',$gadai[0]['historygadaian']],['norujukan','<>',$gadai[0]['historygadaian']]])->pluck('norujukan');
if($gadaiDouble){
$isGadaianTawarruq = $saved_historygadaian !== '0' || $saved_historygadaian !== null || $saved_historygadaian !== '';
if($isGadaianTawarruq){
$this->batalKomuditiTawarruq($norujukan);
Gadai::on($cawangan['mysql'])->where('norujukan', $norujukan)->delete();
Marhun::on($cawangan['mysql'])->where('norujukan', $norujukan)->delete();
} else {
Gadai::on($cawangan['mysql'])->whereIn('norujukan',$gadaiDouble)->delete();
Marhun::on($cawangan['mysql'])->whereIn('norujukan',$gadaiDouble)->delete();
$this->batalKomuditi($gadai[0]['historygadaian'],$cawangan['mysql']);
}
}
// return response('Deleted Successfully', 200);
}
else
{
if($gadai[0]['semakbarcode'] == 'Lulus')
{
$totalkeluar = $keluar - $gadai[0]['pinjaman'];
$bakiakhir = $bakiakhir - $tebus['bakiupah'] + $tebus['addpinjaman'];
$bakiakhir = $baki;
$bakiakhir += $gadai[0]['pinjaman'];
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $keluar,
'masuk' => $masuk,
'baki' => $bakiakhir
'keluar' => $totalkeluar,
'masuk' => $masuk,
'baki' => $bakiakhir
));
}else{
$totalmasuk = $masuk;
$totalkeluar = $keluar;
$bakiakhir = $baki;
if($tebus['jenistebus'] == 'S')
$bakiakhir = ($bakiakhir - $tebus['bakipjm']);
else
$bakiakhir = $bakiawal - $totalkeluar + $totalmasuk + $pendahuluan - $serahan;
$bakiakhir += $gadai[0]['pinjaman'];
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $keluar,
'masuk' => $masuk,
'baki' => $bakiakhir
'keluar' => $keluar,
'masuk' => $masuk,
'baki' => $bakiakhir
));
}
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
Marhun::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
Transaction::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
// return response('Deleted Successfully', 200);
}
$array_response = [
'result' => 'success',
'data' => 1
];
Gadai::on($cawangan['mysql'])->where('norujukan','=',$gadai[0]['historygadaian'])->update(['sttebus' => '']);
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()
];
$saved_historygadaian = $gadai[0]['historygadaian'];
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$gadai[0]['historygadaian'])->orderBy('id', 'desc')->limit(1)->delete();
Transaction::on('mysql7')->where('norujukan','=',$gadai[0]['historygadaian'])->orderBy('id', 'desc')->limit(1)->delete();
Tebus::on($cawangan['mysql'])->where('norujukan','=',$gadai[0]['historygadaian'])->delete();
Marhun::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
$gadaiDouble = Gadai::on($cawangan['mysql'])->where([['historygadaian',$gadai[0]['historygadaian']],['norujukan','<>',$gadai[0]['historygadaian']]])->pluck('norujukan');
if($gadaiDouble){
$isGadaianTawarruq = $saved_historygadaian !== '0' || $saved_historygadaian !== null || $saved_historygadaian !== '';
if($isGadaianTawarruq){
$this->batalKomuditiTawarruq($norujukan);
Gadai::on($cawangan['mysql'])->where('norujukan', $norujukan)->delete();
Marhun::on($cawangan['mysql'])->where('norujukan', $norujukan)->delete();
} else {
Gadai::on($cawangan['mysql'])->whereIn('norujukan',$gadaiDouble)->delete();
Marhun::on($cawangan['mysql'])->whereIn('norujukan',$gadaiDouble)->delete();
$this->batalKomuditi($gadai[0]['historygadaian'],$cawangan['mysql']);
}
}
return response('Deleted Successfully', 200);
}
else
{
if($gadai[0]['semakbarcode'] == 'Lulus')
{
$totalkeluar = $keluar - $gadai[0]['pinjaman'];
$bakiakhir = $baki;
$bakiakhir += $gadai[0]['pinjaman'];
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $totalkeluar,
'masuk' => $masuk,
'baki' => $bakiakhir
));
}else{
$bakiakhir = $baki;
$bakiakhir += $gadai[0]['pinjaman'];
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $keluar,
'masuk' => $masuk,
'baki' => $bakiakhir
));
}
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
Marhun::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
Transaction::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
return response('Deleted Successfully', 200);
Log::error('Transaction failed', [
'exception' => $e->getMessage(),
'line' => $e->getLine(),
'file' => $e->getFile(),
]);
return response()->json($array_response);
}
}
@@ -2202,6 +2231,7 @@ class GadaiController extends Controller
$transaksi_asal = KomuditiTransaksi::on('mysql7')->whereIn('norujukan','=',$gadaiDouble)->sum('unit_komuditi');
$unit_current = $komuditi_current['unit_komuditi'];
$update_unit = $unit_current + $transaksi_asal['unit_komuditi'];
@@ -2239,82 +2269,109 @@ class GadaiController extends Controller
$bakiakhir = $baki;
$totalkeluar = 0;
if($gadai){
return response('Bukan-Tebus', 200);
}else{
$audit = new Audit();
$audit->users = $request->kodlaluan;
$audit->actions = 'Hapus Tebus';
$audit->norujukan = $norujukan;
$audit->old_data = $gadai;
$audit->kodcaw = $kodcaw;
$audit->created_at = $current;
$audit->updated_at = $current;
$audit->save();
if($tebus[0]['tagterima'] == 1)
{
if($tebus[0]['jenistebus'] == 'T')
{
$totalmasuk = $masuk - $tebus[0]['bakipjm'];
// $bakiakhir = $bakiawal-$keluar+$totalmasuk+$pendahuluan-$serahan;
$bakiakhir = $baki - $tebus[0]['bakipjm'];
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $keluar,
'masuk' => $totalmasuk,
'baki' => $bakiakhir
));
Tebus::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
InOutMar::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
Transaction::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->update(['sttebus' => '']);
return response('Deleted Successfully', 200);
}
else
return response('Bukan-Tebus', 200);
try{
if($gadai){
return response('Bukan-Tebus', 200);
}else{
DB::connection($cawangan['mysql'])->beginTransaction();
DB::connection('mysql7')->beginTransaction();
if($tebus[0]['jenistebus'] == 'T')
$audit = new Audit();
$audit->users = $request->kodlaluan;
$audit->actions = 'Hapus Tebus';
$audit->norujukan = $norujukan;
$audit->old_data = $gadai;
$audit->kodcaw = $kodcaw;
$audit->created_at = $current;
$audit->updated_at = $current;
$audit->save();
if($tebus[0]['tagterima'] == 1)
{
if($tebus[0]['jenistebus'] == 'T')
{
$totalmasuk = $masuk - $tebus[0]['bakipjm'];
$bakiakhir = $bakiakhir - $tebus[0]['bakipjm'];
// $bakiakhir = $bakiawal-$keluar+$totalmasuk+$pendahuluan-$serahan;
$bakiakhir = $baki - $tebus[0]['bakipjm'];
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $keluar,
'masuk' => $masuk,
'baki' => $bakiakhir
));
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $keluar,
'masuk' => $totalmasuk,
'baki' => $bakiakhir
));
Tebus::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
InOutMar::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
Transaction::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->update(['sttebus' => '']);
DB::connection($cawangan['mysql'])->commit();
DB::connection('mysql7')->commit();
return response('Deleted Successfully', 200);
}
else
return response('Bukan-Tebus', 200);
}else{
if($tebus[0]['jenistebus'] == 'T')
{
$bakiakhir = $bakiakhir - $tebus[0]['bakipjm'];
$updatetunai=Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
update(array(
'keluar' => $keluar,
'masuk' => $masuk,
'baki' => $bakiakhir
));
Tebus::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
Tebus::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
InOutMar::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
InOutMar::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->delete();
TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
Transaction::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
Transaction::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id', 'desc')->limit(1)->delete();
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->update(['sttebus' => '']);
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->update(['sttebus' => '']);
return response('Deleted Successfully', 200);
DB::connection($cawangan['mysql'])->commit();
DB::connection('mysql7')->commit();
return response('Deleted Successfully', 200);
}
else
return response('Bukan-Tebus', 200);
}
else
return response('Bukan-Tebus', 200);
}
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 senaraiLelong($kodcaw,Request $request){
+322
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;
@@ -771,4 +774,323 @@ 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','marhun','teller','jenistebus']);
$array_tebus_base = $request->only(['totalbayaran', 'bakiupah','jeniskeuntungan','bakipinjaman','jenistebus','teller','nowakil','duitpelanggan']);
$array_trans_base = $request->only(['pinjaman','teller']);
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['harga'] - $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;
$new_transaction = sprintf("%04d",$new_sirig);
$new_sag_number = strtoupper($cawangan['kodcaw']) . $current->year . $current->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_emas'],
'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['pinjaman'] / $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['pinjaman'],$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['pinjaman'],
'kadarupah' => $kadar_raw['kadarujrah'],
'marhun' => $array_trans['marhun'],
'percentpinj' => $margin_pinjaman,
'teller' => $array_trans['teller'],
'bakipjm' => $array_trans['pinjaman'],
'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,
));
Gadai::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->update(array(
'sttebus' => "T",
't_update' => $current->toDateString(),
'historygadaian' => $new_sag_number,
'tag_tebus' => $array_trans['jenistebus'],
));
}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['pinjaman'];
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());
}
}
}