diff --git a/app/Http/Controllers/BayaranController.php b/app/Http/Controllers/BayaranController.php index 613a2f4..d951121 100644 --- a/app/Http/Controllers/BayaranController.php +++ b/app/Http/Controllers/BayaranController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers; use App\Http\Requests\PaymentRequest; +use App\Models\CompanyStatus; +use App\Models\PersonalCarts; use App\Models\TransactionOnlineStatus; use App\Services\BillplzService; use Carbon\Carbon; @@ -18,21 +20,57 @@ class BayaranController extends Controller { public function PembayaranAction(PaymentRequest $request) { + + $exist_carts = $this->checkCarts(); + $exist_carts = optional($exist_carts)->toArray(); + + if($exist_carts){ + /* Do Have Carts. */ + $list_pkb = json_decode($exist_carts['list_pkb'],true); + $list_koop = json_decode($exist_carts['list_koop'],true); + + if($exist_carts['status_pkb'] == 'PENDING') $list_pkb = ($list_pkb) ? $this->setUpahTawarruq($list_pkb) : []; else $list_pkb = []; + if($exist_carts['status_koop'] == 'PENDING') $list_koop = ($list_koop) ? $this->setUpahTawarruq($list_koop) : []; else $list_koop = []; + + }else{ + /* Doesn't Have Carts. First Timer */ + $request->validated(); $request->validated(); dd($request->all()); dd($request->getPembiayaanFromNorujukan()); - $arraybayar = $this->setUpahTawarruq($request->getPembiayaanFromNorujukan()); + $request->validated(); + dd($request->all()); + dd($request->getPembiayaanFromNorujukan()); + $arraybayar = $this->setUpahTawarruq($request->getPembiayaanFromNorujukan()); - return view('includes.bayaran.pembayaransag', compact('arraybayar')); + $list_pkb = array_filter(array_map(function ($x){ + return ($x['tag_syarikat'] == CompanyStatus::COM_PKB) ? $x : null; + },$arraybayar)); + + $list_koop = array_filter(array_map(function ($x){ + return ($x['tag_syarikat'] == CompanyStatus::COM_KOOPPKB) ? $x : null; + },$arraybayar)); + + $personal_carts = new PersonalCarts(); + $personal_carts->ic_no = Auth::user()->icno; + $personal_carts->list_koop = json_encode($list_koop); + $personal_carts->list_pkb = json_encode($list_pkb); + $personal_carts->status_pkb = ($list_pkb) ? 'PENDING' : 'NONE'; + $personal_carts->status_koop = ($list_koop) ? 'PENDING' : 'NONE'; + $personal_carts->user_id = Auth::user()->id; + $personal_carts->save(); + } + + return view('includes.bayaran.pembayaransag', compact('list_pkb','list_koop')); } public function PembayaranEngine(Request $request, BillplzService $billplzService) { ['email' => $customerEmail, 'notel' => $customerPhoneNumber, 'name' => $customerName, 'icno' => $customerIcno] = auth()->user()->toArray(); - + // dd($request->all()); $bayaranArrahn = array_filter($request->txtjenisbayaran, fn($x) => $x == 'arrahn'); $bayaranOthers = array_filter($request->txtjenisbayaran, fn($x) => $x != 'arrahn'); - + // dd('test'); $bayaranArrahnArray = array_map(function ($x) use ($customerName, $customerIcno, $request) { return [ 'customer_name' => $customerName, @@ -63,7 +101,8 @@ class BayaranController extends Controller return Money::MYR($sum())->add($billPlzTransactionFee); }; - $bill = $billplzService->createBill([$customerEmail, $customerPhoneNumber, $customerName, $getPaymentSum()]); + $company = strtoupper($request->txtcompany); + $bill = $billplzService->createBill([$customerEmail, $customerPhoneNumber, $customerName, $getPaymentSum()],$company); $isRecordTransactionSuccess = $this->recordTransaction($paymentArray, $bill); @@ -320,4 +359,28 @@ class BayaranController extends Controller return array_map($getUpahTawarruqPerPembiayaan, $pembiayaan); } + + public function checkCarts(){ + $exist_carts = PersonalCarts::where('ic_no', Auth::user()->icno) + ->where('user_id', Auth::user()->id) + ->where(function ($query) { + $query->where('status_pkb', 'PENDING') + ->orWhere('status_koop', 'PENDING'); + }) + ->first(); + + return $exist_carts; + } + + public function removeCarts(){ + $exist_carts = PersonalCarts::where('ic_no', Auth::user()->icno) + ->where('user_id', Auth::user()->id) + ->first(); + + if ($exist_carts) { + $exist_carts->delete(); + } + + return $exist_carts; + } } diff --git a/app/Http/Requests/PaymentRequest.php b/app/Http/Requests/PaymentRequest.php index 9638f70..76c830b 100644 --- a/app/Http/Requests/PaymentRequest.php +++ b/app/Http/Requests/PaymentRequest.php @@ -3,6 +3,7 @@ namespace App\Http\Requests; use App\Http\Controllers\InformationController; +use App\Models\CompanyStatus; use App\Rules\PaymentRules\CawanganPermitUjrahPayment; use App\Rules\PaymentRules\NorujukanBukanFasa2; use App\Rules\PaymentRules\NorujukanContractNotExceed12Month; @@ -54,17 +55,28 @@ class PaymentRequest extends FormRequest public function getPembiayaanFromNorujukan() { $norujukanList = json_decode($this->totalSagInCartInput); - - dd($norujukanList,$this->test); + $syarikatList = json_decode($this->listSyarikatinCartInput); + $filterByStatus = function ($status) use ($norujukanList, $syarikatList) { + return array_filter( + array_map(function ($sag, $index) use ($syarikatList, $status) { + if (isset($syarikatList[$index]) && $syarikatList[$index] == $status) { + $HttpRequest = $status == CompanyStatus::COM_PKB ? Http::arrahn() : Http::arrahn_koop(); + $array_temp = $HttpRequest->get('/api/gadai/norujukan/' . $sag)->json()[0][0] ?? null; + $array_temp['tag_syarikat'] = $status; + return $array_temp; + } + return null; + }, $norujukanList, array_keys($norujukanList)) + ); + }; - // $list_pkb = array_filter($fn($x) => ); - // $list_koop = array_filter($fn($x) => ); + $list_pkb = $filterByStatus(CompanyStatus::COM_PKB); + $list_koop = $filterByStatus(CompanyStatus::COM_KOOPPKB); - $pkb = array_map(fn($x) => Http::arrahn()->get('/api/gadai/norujukan/' . $x)->json()[0][0],$norujukanList); - $koop = array_map(fn($x) => Http::arrahn()->get('/api/gadai/norujukan/' . $x)->json()[0][0],$norujukanList); - - return array_map(fn($x) => Http::arrahn()->get('/api/gadai/norujukan/' . $x)->json()[0][0], $norujukanList); + $array_merged_list = array_merge($list_pkb,$list_koop); + return $array_merged_list; + // return array_map(fn($x) => Http::arrahn()->get('/api/gadai/norujukan/' . $x)->json()[0][0], $norujukanList); } public function getPembiayaanAkanDilelong() diff --git a/app/Models/PersonalCarts.php b/app/Models/PersonalCarts.php new file mode 100644 index 0000000..5059d78 --- /dev/null +++ b/app/Models/PersonalCarts.php @@ -0,0 +1,35 @@ + 'array', + 'list_koop' => 'array', + ]; + + protected $guarded = [ + 'id', + 'ic_no', + 'list_pkb', + 'list_koop', + 'status_pkb', + 'status_koop', + 'user_id', + 'created_at', + 'updated_at' + ]; + + public $timestamps = true; +} diff --git a/app/Services/BillplzService.php b/app/Services/BillplzService.php index ae853f9..3aca74b 100644 --- a/app/Services/BillplzService.php +++ b/app/Services/BillplzService.php @@ -17,21 +17,9 @@ class BillplzService public function __construct() { - $appEnvironment = config('app.env'); - $billplzConfig = $appEnvironment == 'production' ? config('services.billplz.production') : config('services.billplz.sandbox'); - - $client = Client::make($billplzConfig['api_key'], $billplzConfig['x_signature']); - $client->useVersion('v4'); - - if ($appEnvironment != 'production') { - $client->useSandbox(); - } - - $this->callbackUrl = route('pay.callback'); - $this->redirectUrl = ['redirect_url' => route('pay.paymentStatus')]; - $this->paymentTitle = "Pembayaran Ansuran"; - $this->config = $billplzConfig; - $this->client = $client; + $this->setupBillplzClient(); + $this->setupUrls(); + $this->paymentTitle = config('services.billplz.paymentTitle'); } public function getCollections() @@ -39,8 +27,10 @@ class BillplzService return $this->client->collection()->all()->toArray(); } - public function createBill(array $data) + public function createBill(array $data,string $company = 'PKB') { + $this->setupBillplzClient($company); + $composed_data = [ $this->config['collection_id'], ...$data, @@ -252,4 +242,34 @@ class BillplzService { return array_map(fn($x) => $this->client->bill()->get($x)->toArray(), $billIds); } + + protected function setupBillplzClient($company = 'PKB') + { + $appEnvironment = config('app.env'); + + $billplzEnvironments = [ + 'PKB' => ($appEnvironment === 'production') ? config('services.billplz.production') : config('services.billplz.sandbox'), + 'KOPKB' => ($appEnvironment === 'production') ? config('services.billplz.production_kopkb') : config('services.billplz.sandbox_kopkb'), + ]; + + $billplzConfig = $appEnvironment === 'production' + ? ($billplzEnvironments[$company] ?? config('services.billplz.production')) + : ($billplzEnvironments[$company] ?? config('services.billplz.sandbox')); + + $client = Client::make($billplzConfig['api_key'], $billplzConfig['x_signature']); + $client->useVersion('v4'); + + if ($appEnvironment !== 'production') { + $client->useSandbox(); + } + + $this->config = $billplzConfig; + $this->client = $client; + } + + protected function setupUrls() + { + $this->callbackUrl = route('pay.callback'); + $this->redirectUrl = ['redirect_url' => route('pay.paymentStatus')]; + } } diff --git a/config/services.php b/config/services.php index 6995833..4af2e26 100644 --- a/config/services.php +++ b/config/services.php @@ -31,20 +31,26 @@ return [ ], 'billplz' => [ + 'paymentTitle' => 'Pembayaran Ansuran', 'production' => [ 'api_key' => env('BILLPLZ_PRODUCTION_API_KEY', 'f3e82fe1-a422-4660-97f0-65cc59ff6c47'), 'x_signature' => env('BILLPLZ_PRODUCTION_X_SIGNATURE', 'S-alZUbzmD2_TF3vmrRqgehQ'), 'collection_id' => env('BILLPLZ_PRODUCTION_COLLECTION_ID', '5aosv2dg'), ], + 'production_kopkb' => [ + 'api_key' => env('BILLPLZ_PRODUCTION_API_KEY', 'f3e82fe1-a422-4660-97f0-65cc59ff6c47'), + 'x_signature' => env('BILLPLZ_PRODUCTION_X_SIGNATURE', 'S-alZUbzmD2_TF3vmrRqgehQ'), + 'collection_id' => env('BILLPLZ_PRODUCTION_COLLECTION_ID', '5aosv2dg'), + ], 'sandbox' => [ 'api_key' => env('BILLPLZ_SANDBOX_API_KEY', 'abecfd0c-98d8-4635-abc2-2fcba8c60c11'), 'x_signature' => env('BILLPLZ_SANDBOX_X_SIGNATURE', 'S-GWr7z03XjAhqPsFfu_H5nQ'), - 'collection_id' => env('BILLPLZ_PRODUCTION_COLLECTION_ID', 'z0r4kjhi'), + 'collection_id' => env('BILLPLZ_SANDBOX_COLLECTION_ID', 'z0r4kjhi'), ], 'sandbox_kopkb' => [ 'api_key' => env('BILLPLZ_SANDBOX_KOPKB_API_KEY', 'abecfd0c-98d8-4635-abc2-2fcba8c60c11'), 'x_signature' => env('BILLPLZ_SANDBOX_KOPKB_X_SIGNATURE', 'S-GWr7z03XjAhqPsFfu_H5nQ'), - 'collection_id' => env('BILLPLZ_PRODUCTION_KOPKB_COLLECTION_ID', 'z0r4kjhi'), + 'collection_id' => env('BILLPLZ_SANDBOX_KOPKB_COLLECTION_ID', 'z0r4kjhi'), ], ], diff --git a/database/migrations/2024_12_07_012607_create_carts_table.php b/database/migrations/2024_12_07_012607_create_carts_table.php new file mode 100644 index 0000000..3ee51e4 --- /dev/null +++ b/database/migrations/2024_12_07_012607_create_carts_table.php @@ -0,0 +1,40 @@ +id(); + $table->string('ic_no'); + $table->json('list_pkb'); + $table->json('list_koop'); + $table->enum('status_pkb', ['PENDING', 'PAID','NONE']); + $table->enum('status_koop', ['PENDING', 'PAID','NONE']); + $table->softDeletes(); + $table->unsignedBigInteger('user_id'); + $table->timestamps(); + + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('personal_carts'); + } +} diff --git a/public/js/miscellaneous/cart.js b/public/js/miscellaneous/cart.js index d17232e..c4bd78e 100644 --- a/public/js/miscellaneous/cart.js +++ b/public/js/miscellaneous/cart.js @@ -17,16 +17,36 @@ radios.forEach((radio) => { let btn_payment = document.getElementById('bayarsbmt'); let content1 = document.getElementById('divpkb'); let content2 = document.getElementById('divkopkb'); - + let whole_total = document.getElementById('spanjumlah'); + let inputs = document.querySelectorAll(`#div${this.value} input[type="number"]`); + let values = Array.from(inputs).map(input => parseFloat(input.value) || 0).reduce((sum,value) => sum + value, 0); + if (this.checked) { if(this.value == 'kopkb'){ + let inputsdisabled = document.querySelectorAll(`#divpkb input:not([type="radio"])`); + inputsdisabled.forEach(input => { + input.disabled = true; + }); + inputs.forEach(input =>{ + input.disabled = false; + }); content1.classList.add('blurred'); content2.classList.remove('blurred'); btn_payment.disabled = false; + whole_total.innerHTML = values + 1; + }else{ + let inputsdisabled = document.querySelectorAll(`#divkopkb input:not([type="radio"])`); + inputsdisabled.forEach(input => { + input.disabled = true; + }); + inputs.forEach(input =>{ + input.disabled = false; + }); content2.classList.add('blurred'); content1.classList.remove('blurred'); btn_payment.disabled = false; + whole_total.innerHTML = values + 1; } } diff --git a/resources/views/includes/bayaran/pembayaransag.blade.php b/resources/views/includes/bayaran/pembayaransag.blade.php index 42b858e..aabab50 100644 --- a/resources/views/includes/bayaran/pembayaransag.blade.php +++ b/resources/views/includes/bayaran/pembayaransag.blade.php @@ -34,6 +34,7 @@
+ @if($list_pkb)
@@ -56,7 +57,7 @@ @php $counter = 1;$jenisbayaran = 'tawarruq'; $totalupahasal = 1; $errorcount = 0; @endphp - @foreach($arraybayar as $index=>$bayar) + @foreach($list_pkb as $index=>$bayar) @if($bayar['hargajualan'] != '') @@ -94,6 +95,7 @@ + @@ -109,29 +111,13 @@
- {{--
-
- -
- -
- RM1.00 -
-
-
-
- -
- -
- RM{{ number_format($totalupahasal,2) }} -
-
--}} *Jangan biarkan kotak bayaran ditinggalkan kosong, Sila masukkan amaun!
+ @endif + @if($list_koop)
@@ -155,7 +141,7 @@ @php $counter = 1;$jenisbayaran = 'tawarruq'; $totalupahasal = 1; $errorcount = 0; @endphp - @foreach($arraybayar as $index=>$bayar) + @foreach($list_koop as $index=>$bayar) @if($bayar['hargajualan'] != '') @@ -193,6 +179,8 @@ + + @@ -212,6 +200,7 @@ *Jangan biarkan kotak bayaran ditinggalkan kosong, Sila masukkan amaun!
+ @endif
@@ -241,7 +230,7 @@
- RM{{ number_format($totalupahasal,2) }} + RM1.00
diff --git a/resources/views/includes/home.blade.php b/resources/views/includes/home.blade.php index 68eb105..36aeaa0 100644 --- a/resources/views/includes/home.blade.php +++ b/resources/views/includes/home.blade.php @@ -138,26 +138,32 @@ let totalSagInCart = []; let totaltagSyarikat = []; - let updateSagInCart = function(norujukan){ - + let updateSagInCart = function(norujukan,syarikat){ + console.log(norujukan,syarikat); let isNorujukanIsAlreadyInCart = totalSagInCart.includes(norujukan); if(isNorujukanIsAlreadyInCart){ + let index = totalSagInCart.indexOf(norujukan); totalSagInCart = totalSagInCart.filter(x => x != norujukan); - } + totaltagSyarikat.splice(index, 1); + } if(! isNorujukanIsAlreadyInCart){ totalSagInCart.push(norujukan); + totaltagSyarikat.push(syarikat); } + console.log("total:" + totalSagInCart); + console.log("totalsyarikat:" + totaltagSyarikat); }; // Add event listener to checkboxes to update total count customer wants to pay $('#main-table').on('input', 'input[type="checkbox"][name="chkboxsag[]"]', function(el) { let norujukan = el.target.dataset.norujukan; + let tagsyarikat = el.target.dataset.syarikat; let isNorujukanIsChecked = el.target.checked; - updateSagInCart(norujukan); + updateSagInCart(norujukan,tagsyarikat); document.getElementById('totalsag').innerHTML = totalSagInCart.length; }); @@ -172,7 +178,51 @@ totalSagInCartInput.value = JSON.stringify(totalSagInCart); event.target.appendChild(totalSagInCartInput); - event.target.submit(); + let listSyarikatinCartInput = document.createElement('input'); + listSyarikatinCartInput.type = 'hidden'; + listSyarikatinCartInput.name = 'listSyarikatinCartInput'; + listSyarikatinCartInput.value = JSON.stringify(totaltagSyarikat); + event.target.appendChild(listSyarikatinCartInput); + + let check_carts = fetch('/showcarts-available') + .then(response => { + console.log(response); + if (!response.ok) { + throw new Error('HTTP error ' + response.status); + } + return response.json(); + }) + .then(data => console.log(data)) + .catch(error => console.error(error)); + + if(check_carts){ + Swal.fire({ + title: "Troli Anda Masih Ada Lagi?", + showDenyButton: true, + showCancelButton: false, + confirmButtonText: "Pergi Ke Troli", + denyButtonText: `Buang Troli` + }).then((result) => { + /* Read more about isConfirmed, isDenied below */ + if (result.isDenied) { + let remove_carts = fetch('/deletecarts') + .then(response => { + console.log(response); + if (!response.ok) { + throw new Error('HTTP error ' + response.status); + } + return response.json(); + }) + .then(data => console.log(data)) + .catch(error => console.error(error)); + + } + event.target.submit(); + }); + console.log(check_carts); + }else{ + event.target.submit(); + } }); diff --git a/resources/views/includes/partials/table-pembiayaan.blade.php b/resources/views/includes/partials/table-pembiayaan.blade.php index c1af081..f82c398 100644 --- a/resources/views/includes/partials/table-pembiayaan.blade.php +++ b/resources/views/includes/partials/table-pembiayaan.blade.php @@ -16,7 +16,7 @@ disabled_payment ? 'disabled' : '' }} /> + data-syarikat="{{ $data->tag_syarikat }}" {{ $data->disabled_payment ? 'disabled' : '' }} /> diff --git a/routes/web.php b/routes/web.php index 3529cc4..29bd693 100644 --- a/routes/web.php +++ b/routes/web.php @@ -125,6 +125,8 @@ Route::get('/requesticfe', 'App\Http\Controllers\TacController@requestKadPengena Route::get('/requesttac', 'App\Http\Controllers\TacController@TACrequest')->name('requesttac'); Route::get('/requesttacfe', 'App\Http\Controllers\TacController@TACrequestfe')->name('requesttacfe'); +Route::get('/showcarts-available','App\Http\Controllers\BayaranController@checkCarts')->name('checkcarts'); +Route::get('/deletecarts','App\Http\Controllers\BayaranController@removeCarts')->name('removecarts'); Route::get('download/manualpengguna',['as'=>'miscellaneous.manualpengguna', function(){ if (Storage::disk('miscellaneous')->exists('Manual_Pengguna.pdf')) {