From 3a7259a64c9fa2cb5fec34443bf4c1b21a463349 Mon Sep 17 00:00:00 2001 From: hafifierahman Date: Fri, 1 Nov 2024 09:28:48 +0800 Subject: [PATCH] added function for cashdeposit and cash withdrawals --- app/Http/Controllers/TunaiController.php | 27 ++++ app/Services/CashFlow/TellerServices.php | 193 +++++++++++++++++++---- routes/web.php | 1 + 3 files changed, 193 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/TunaiController.php b/app/Http/Controllers/TunaiController.php index aa93a4c..1e36f79 100644 --- a/app/Http/Controllers/TunaiController.php +++ b/app/Http/Controllers/TunaiController.php @@ -291,6 +291,33 @@ class TunaiController extends Controller return response()->json($addtunai); } + public function updateRequestTellerDeno($kodcaw,Request $request){ + + $requestteller = RequestTeller::on('mysql7')->where([['no','=',$request->no],['kodcaw','=',$kodcaw],['kodlaluan','=',$request->kodlaluan],['update','=',0]])->update(array( + 'update' => 1, + 'bd1' => $request->bd1, + 'bd5' => $request->bd5, + 'bd10' => $request->bd10, + 'bd20' => $request->bd20, + 'bd50' => $request->bd50, + 'bd100' => $request->bd100, + 'txtring1' => $request->txtring1, + 'txtring5' => $request->txtring5, + 'txtring10' => $request->txtring10, + 'txtring20' => $request->txtring20, + 'txtring50' => $request->txtring50, + 'txtring100' => $request->txtring100, + 'txtring1000' => $request->txtring1000, + 'txtsen1' => $request->txtsen1, + 'txtsen5' => $request->txtsen5, + 'txtsen10' => $request->txtsen10, + 'txtsen20' => $request->txtsen20, + 'txtsen50' => $request->txtsen50, + ) ); + + return response()->json($requestteller); + } + public function getRequestTeller($kodcaw) { diff --git a/app/Services/CashFlow/TellerServices.php b/app/Services/CashFlow/TellerServices.php index 067594b..99cc56e 100644 --- a/app/Services/CashFlow/TellerServices.php +++ b/app/Services/CashFlow/TellerServices.php @@ -96,7 +96,7 @@ class TellerServices return 'success'; } - private function getDenoTotal(Request $request){ + private function getDenoTotal(Request $request,$operator){ $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$this->cawangan)->first(); $current = Carbon::now(); $current = new Carbon(); @@ -109,18 +109,18 @@ class TellerServices Log::error("Empty Denominasi Data"); } - $deno['txtsen1'] -= intval($request->ambilansen1); - $deno['txtsen5'] -= intval($request->ambilansen5); - $deno['txtsen10'] -= intval($request->ambilansen10); - $deno['txtsen20'] -= intval($request->ambilansen20); - $deno['txtsen50'] -= intval($request->ambilansen50); + $deno['txtsen1'] += ($operator == 'add') ? intval($request->ambilansen1) : -intval($request->ambilansen1); + $deno['txtsen5'] += ($operator == 'add') ? intval($request->ambilansen5) : -intval($request->ambilansen5); + $deno['txtsen10'] += ($operator == 'add') ? intval($request->ambilansen10) : -intval($request->ambilansen10); + $deno['txtsen20'] += ($operator == 'add') ? intval($request->ambilansen20) : -intval($request->ambilansen20); + $deno['txtsen50'] += ($operator == 'add') ? intval($request->ambilansen50) : -intval($request->ambilansen50); - $deno['txtring1'] -= intval($request->ambilanring1); - $deno['txtring5'] -= intval($request->ambilanring5); - $deno['txtring10'] -= intval($request->ambilanring10); - $deno['txtring20'] -= intval($request->ambilanring20); - $deno['txtring50'] -= intval($request->ambilanring50); - $deno['txtring100'] -= intval($request->ambilanring100); + $deno['txtring1'] += ($operator == 'add') ? intval($request->ambilanring1) : -intval($request->ambilanring1); + $deno['txtring5'] += ($operator == 'add') ? intval($request->ambilanring5) : -intval($request->ambilanring5); + $deno['txtring10'] += ($operator == 'add') ? intval($request->ambilanring10) : -intval($request->ambilanring10); + $deno['txtring20'] += ($operator == 'add') ? intval($request->ambilanring20) : -intval($request->ambilanring20); + $deno['txtring50'] += ($operator == 'add') ? intval($request->ambilanring50) : -intval($request->ambilanring50); + $deno['txtring100'] += ($operator == 'add') ? intval($request->ambilanring100) : -intval($request->ambilanring100); $k01 = ($deno['txtsen1'] * 0.01); $k05 = ($deno['txtsen5'] * 0.05); @@ -162,17 +162,17 @@ class TellerServices $current = Carbon::now(); $current = new Carbon(); + // Start transaction + DB::connection($cawangan['mysql'])->beginTransaction(); + DB::connection('mysql7')->beginTransaction(); + try { - // Start transaction - DB::connection($cawangan['mysql'])->beginTransaction(); - DB::connection('mysql7')->beginTransaction(); - /* Addtunai Transaction */ $response_addtunai = $this->checkAddTunai($request); if($response_addtunai !== 'success'){ - Log::error("Unsuccessful Tunai"); + // Log::error("Unsuccessful Tunai"); Log::debug($response_addtunai); throw new Exception('Unsuccessful Tunai'); } @@ -196,7 +196,6 @@ class TellerServices Log::debug($this->cawangan); Log::debug($current->toDateString()); Log::debug($request->teller); - Log::error("Empty Tunai Data"); throw new Exception("Empty Tunai Data"); } @@ -216,7 +215,6 @@ class TellerServices $vault = optional($vault->first())->toArray(); if(empty($vault)){ - Log::error("Empty Vault Data"); throw new Exception("Empty Vault Data"); } @@ -227,11 +225,11 @@ class TellerServices 'keluar' => $total_vault_deposit, 'bakiakhir' => $remaining_vault, 'bakieod' => $remaining_vault - ) ); + )); /* Denominasi Transaction */ - $response_deno = $this->getDenoTotal($request); + $response_deno = $this->getDenoTotal($request,'subtract'); if($response_deno == 'empty') throw new Exception("Empty Denominasi Data"); @@ -268,9 +266,27 @@ class TellerServices ) ); /* RequestTeller Transaction */ - RequestTeller::on('mysql7')->where([['no','=',$request->no],['kodcaw','=',$this->cawangan],['kodlaluan','=',$request->teller],['update','=',0]])->update(array( - 'update' => 1 - )); + $requestteller = RequestTeller::on('mysql7')->where([['no','=',$request->no],['kodcaw','=',$this->cawangan],['kodlaluan','=',$request->teller],['update','=',0]])->update(array( + 'update' => 1, + 'bd1' => 0, + 'bd5' => 0, + 'bd10' => 0, + 'bd20' => 0, + 'bd50' => 0, + 'bd100' => 0, + 'txtring1' => ($request->ambilanring1) ? $request->ambilanring1 : 0, + 'txtring5' => ($request->ambilanring5) ? $request->ambilanring5 : 0, + 'txtring10' => ($request->ambilanring10) ? $request->ambilanring10 : 0, + 'txtring20' => ($request->ambilanring20) ? $request->ambilanring20 : 0, + 'txtring50' => ($request->ambilanring50) ? $request->ambilanring50 : 0, + 'txtring100' => ($request->ambilanring100) ? $request->ambilanring100 : 0, + 'txtring1000' => 0, + 'txtsen1' => ($request->ambilansen1) ? $request->ambilansen1 : 0, + 'txtsen5' => ($request->ambilansen5) ? $request->ambilansen5 : 0, + 'txtsen10' => ($request->ambilansen10) ? $request->ambilansen10 : 0, + 'txtsen20' => ($request->ambilansen20) ? $request->ambilansen20 : 0, + 'txtsen50' => ($request->ambilansen50) ? $request->ambilansen50 : 0, + ) ); // Commit transactions DB::connection($cawangan['mysql'])->commit(); @@ -281,16 +297,137 @@ class TellerServices } catch (\Exception $e) { DB::connection($cawangan['mysql'])->rollBack(); DB::connection('mysql7')->rollBack(); - + Log::error("Transaction failed: " . $e->getMessage()); - // Handle the exception or re-throw - throw $e; + return 'error'; } } - public function CashWithdrawals(){ + public function CashWithdrawals(Request $request){ + $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$this->cawangan)->first(); + $current = Carbon::now(); + $current = new Carbon(); + // Start transaction + DB::connection($cawangan['mysql'])->beginTransaction(); + DB::connection('mysql7')->beginTransaction(); + + try { + + /* Addtunai Transaction */ + $response_addtunai = $this->checkAddTunai($request); + + if($response_addtunai !== 'success'){ + // Log::error("Unsuccessful Tunai"); + Log::debug($response_addtunai); + throw new Exception('Unsuccessful Tunai'); + } + + AddTunai::on($cawangan['mysql'])->create([ + 'tarikh' => $current->toDateString(), + 'kodcaw'=> $this->cawangan, + 'kodlaluan'=>$request->teller, + 'tambah'=>$request->amt, + 'kurang'=> 0, + 'nota'=>$request->nota, + 'created_at'=>$current + ]); + + /* Tunai Transaction */ + $tunai = Tunai::on($cawangan['mysql']) + ->where([['kodcaw','=',$this->cawangan],['tarikh','=',$current->toDateString()],['kodlaluan','=',$request->teller]]); + $tunai = optional($tunai->first())->toArray(); + + if(empty($tunai)){ + Log::debug($this->cawangan); + Log::debug($current->toDateString()); + Log::debug($request->teller); + throw new Exception("Empty Tunai Data"); + } + + $total_teller_deposit = $tunai['kurang'] + $request->amt; + $remaining_teller_cashflow = $tunai['baki'] + $request->amt; + + Tunai::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->teller] ])-> + update(array( + 'kurang' => $total_teller_deposit, + 'baki' => $remaining_teller_cashflow + )); + + /* Vault Transaction */ + $vault = Vault::on($cawangan['mysql']) + ->where('kodcaw','=',$this->cawangan) + ->where('tarikh','=',$current->toDateString()); + $vault = optional($vault->first())->toArray(); + + if(empty($vault)){ + throw new Exception("Empty Vault Data"); + } + + $total_vault_deposit = $vault['masuk'] + $request->amt; + $remaining_vault = $vault['bakiawal'] - $vault['keluar'] + $total_vault_deposit + $vault['pendahuluan'] - $vault['serahan'] - $vault['totpanjar']; + + Vault::on($cawangan['mysql'])->where([['tarikh','=',$current->toDateString()],['kodcaw','=', $this->cawangan]])->update(array( + 'masuk' => $total_vault_deposit, + 'bakiakhir' => $remaining_vault, + 'bakieod' => $remaining_vault + )); + + /* Denominasi Transaction */ + $response_deno = $this->getDenoTotal($request,'add'); + + if($response_deno == 'empty') + throw new Exception("Empty Denominasi Data"); + + $deno = $response_deno['deno']; + $total = $response_deno['total']; + $cash = $response_deno['cash']; + + Denominasi::on($cawangan['mysql'])->where([['tarikh','=',$current->toDateString()]])->update(array( + 'txtring1'=> $deno['txtring1'], + 'k1'=> $cash['kertas1'], + 'txtring5'=> $deno['txtring5'], + 'k5'=> $cash['kertas5'], + 'txtsen1'=> $deno['txtsen1'], + 'k01'=> $cash['syiling1'], + 'txtsen5'=> $deno['txtsen5'], + 'k05'=> $cash['syiling5'], + 'txtsen10'=> $deno['txtsen10'], + 'k010'=> $cash['syiling10'], + 'txtsen20'=> $deno['txtsen20'], + 'k020'=> $cash['syiling20'], + 'txtsen50'=> $deno['txtsen50'], + 'k050'=> $cash['syiling50'], + 'txtring10'=> $deno['txtring10'], + 'k10'=> $cash['kertas10'], + 'txtring20'=> $deno['txtring20'], + 'k20'=> $cash['kertas20'], + 'txtring50'=> $deno['txtring50'], + 'k50'=> $cash['kertas50'], + 'txtring100'=> $deno['txtring100'], + 'k100'=> $cash['kertas100'], + 'txtjumlah'=> $total, + 'jumlahkeping' => $total + ) ); + + /* RequestTeller Transaction */ + $requestteller = RequestTeller::on('mysql7')->where([['no','=',$request->no],['kodcaw','=',$this->cawangan],['kodlaluan','=',$request->teller],['update','=',0]])->update(array( + 'update' => 1, + ) ); + + // Commit transactions + DB::connection($cawangan['mysql'])->commit(); + DB::connection('mysql7')->commit(); + + return 'success'; + }catch(\Exception $e){ + DB::connection($cawangan['mysql'])->rollBack(); + DB::connection('mysql7')->rollBack(); + Log::error("Transaction failed: " . $e->getMessage()); + + return 'error'; + } } } diff --git a/routes/web.php b/routes/web.php index ad8c8d1..d91c904 100644 --- a/routes/web.php +++ b/routes/web.php @@ -383,6 +383,7 @@ $router->group(['prefix'=>'api'], function () use ($router){ $router->get('requestTeller/getlaporanberkala/{kodcaw}',['uses' => 'TunaiController@getRequestTellerLaporanSerahanBerkala']); $router->get('requestTeller/get/{kodcaw}/date',['uses' => 'TunaiController@getRequestTellerByDate']); $router->put('requestTeller/update/{kodcaw}',['uses' => 'TunaiController@updateRequestTeller']); + $router->put('requestTeller/updatedeno/{kodcaw}',['uses' => 'TunaiController@updateRequestTellerDeno']); $router->put('requestTeller/batal/{kodcaw}',['uses' => 'TunaiController@batalRequestTeller']); $router->get('requestTeller/checkproses',['uses'=>'TunaiController@CheckProsesReqTeller']);