diff --git a/app/Http/Controllers/PayableUjrahBranchController.php b/app/Http/Controllers/PayableUjrahBranchController.php new file mode 100644 index 0000000..505242b --- /dev/null +++ b/app/Http/Controllers/PayableUjrahBranchController.php @@ -0,0 +1,121 @@ +get()->toJson(); + + return json_encode($payableUjrahBranch); + } + + public function create(Request $request) + { + $validated_data = $request->validate([ + 'details_caw' => 'required', + 'kodcaw' => 'required', + ]); + + $created_branch = PayableUjrahBranch::create($validated_data); + + return response(json_encode($created_branch), 200); + } + + public function syncStatus(Request $request) + { + + $getDetailCawanganFromKodCaw = function($kodcaw){ + $cawangan_details = Cawangan::all(['kodcaw', 'details_caw']); + return $cawangan_details->where('kodcaw', $kodcaw)->first()->details_caw; + }; + + // payable ujrah updates + $previous_payable_ujrah_branch = PayableUjrahBranch::select('kodcaw as branch', 'gadaian_ujrah_payable')->get(); + + $payable_ujrah_branch = collect($request->gadai_ujrah_payable_branches); + + $payable_ujrah_branch_updates = $this->findDiff($previous_payable_ujrah_branch, $payable_ujrah_branch, 'branch'); + + $payable_ujrah_branch_updates->each(function ($item, $key) { + PayableUjrahBranch::where('kodcaw', $key)->update(['gadaian_ujrah_payable' => $item['gadaian_ujrah_payable']]); + }); + + $generateUpdateMessagePayableUjrahBranch = function($updateData) use($getDetailCawanganFromKodCaw){ + + return $updateData->map(function($item, $index) use($getDetailCawanganFromKodCaw) { + $cawanganName = $getDetailCawanganFromKodCaw($index); + + $gadaian_ujrah_payable_note = $item['gadaian_ujrah_payable'] === 0 ? 'gadaian ujrah payment disabled' : 'gadaian ujrah payment enabled'; + + return "$cawanganName $gadaian_ujrah_payable_note"; + }); + + }; + + + // tawarruq version updates + $previous_branch_tawarruq_version = Cawangan::select('kodcaw as branch', 'version as tawarruq_version')->get(); + + $branch_tawarruq_version = collect($request->branch_tawarruq_version); + + $arrahn_master_db_updates = $this->findDiff($previous_branch_tawarruq_version, $branch_tawarruq_version, 'branch'); + + $arrahn_master_db_updates->each(function ($item, $key) { + Cawangan::where('kodcaw', $key)->update(['version' => $item['tawarruq_version']]); + }); + + $generateUpdateMessageTawarruqVersion = function($updateData) use($getDetailCawanganFromKodCaw){ + + return $updateData->map(function($item, $index) use($getDetailCawanganFromKodCaw) { + $cawanganName = $getDetailCawanganFromKodCaw($index); + + $tawarruq_version = $item['tawarruq_version']; + return "$cawanganName change to use tawarruq version : $tawarruq_version"; + }); + + }; + + return response()->json([ + 'status' => 'success', + 'updates_done' => [ + 'updated_branch_tawarruq_version' => $generateUpdateMessageTawarruqVersion($arrahn_master_db_updates), + 'updated_payable_ujrah' => $generateUpdateMessagePayableUjrahBranch($payable_ujrah_branch_updates), + ] + ]); + } + + + + public function delete() + { + } + + // SUPPORTING METHODS + public function findDiff(Collection $collectionA, Collection $collectionB, $compare_key = 'id') + { + // Filter and find differences + $differences = $collectionA->mapWithKeys(function ($itemA) use ($collectionB, $compare_key) { + $itemB = $collectionB->firstWhere($compare_key, $itemA[$compare_key]); + + if ($itemB) { + $diff = collect($itemB)->diffAssoc($itemA); + if ($diff->isNotEmpty()) { + return [$itemA[$compare_key] => $diff]; + } + } else { + return [$itemA[$compare_key] => collect($itemA)]; + } + + return []; + })->filter(); + + return $differences; + } +} diff --git a/app/PayableUjrahBranch.php b/app/PayableUjrahBranch.php new file mode 100644 index 0000000..c2ce802 --- /dev/null +++ b/app/PayableUjrahBranch.php @@ -0,0 +1,20 @@ +belongsTo(Cawangan::class, 'kodcaw', 'kodcaw'); + } +} diff --git a/database/migrations/2024_05_16_093658_add_payable_ujrah_branches_table.php b/database/migrations/2024_05_16_093658_add_payable_ujrah_branches_table.php new file mode 100644 index 0000000..3b1ffa5 --- /dev/null +++ b/database/migrations/2024_05_16_093658_add_payable_ujrah_branches_table.php @@ -0,0 +1,187 @@ +bigIncrements('id'); + $table->string('details_caw'); + $table->string('kodcaw'); + $table->boolean('gadaian_ujrah_payable')->default(false); + $table->timestamps(); + }); + + $this->getBranches()->each(function($branch) { + PayableUjrahBranch::create([ + 'details_caw' => $branch['details_caw'], + 'kodcaw' => $branch['kodcaw'], + ]); + }); + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('payable_ujrah_branches'); + } + + public function getBranches() + { + $branches = [ + [ + "details_caw" => "Kota Bharu 2", + "kodcaw" => "KB2" + ], + [ + "details_caw" => "Wakaf Che Yeh", + "kodcaw" => "WCY" + ], + [ + "details_caw" => "1 Stop", + "kodcaw" => "1STOP" + ], + [ + "details_caw" => "Jelawat", + "kodcaw" => "020301" + ], + [ + "details_caw" => "Wakaf Bharu", + "kodcaw" => "WB" + ], + [ + "details_caw" => "Kok Lanas", + "kodcaw" => "ALS" + ], + [ + "details_caw" => "Pasir Puteh", + "kodcaw" => "020306" + ], + [ + "details_caw" => "Machang", + "kodcaw" => "ARM" + ], + [ + "details_caw" => "Pasir Mas", + "kodcaw" => "APM" + ], + [ + "details_caw" => "Gua Musang", + "kodcaw" => "010311" + ], + [ + "details_caw" => "Guchil", + "kodcaw" => "GC" + ], + [ + "details_caw" => "Jeli", + "kodcaw" => "JLI" + ], + [ + "details_caw" => "Rantau Panjang", + "kodcaw" => "ARP" + ], + [ + "details_caw" => "Kuantan", + "kodcaw" => "010621" + ], + [ + "details_caw" => "Pasir Gudang", + "kodcaw" => "010136" + ], + [ + "details_caw" => "Skudai", + "kodcaw" => "011235" + ], + [ + "details_caw" => "Putatan", + "kodcaw" => "011227" + ], + [ + "details_caw" => "Bentong", + "kodcaw" => "010620" + ], + [ + "details_caw" => "Raub", + "kodcaw" => "010617" + ], + [ + "details_caw" => "Kangar", + "kodcaw" => "010915" + ], + [ + "details_caw" => "Sandakan", + "kodcaw" => "011228" + ], + [ + "details_caw" => "Sungai Petani", + "kodcaw" => "010219" + ], + [ + "details_caw" => "Tawau", + "kodcaw" => "011229" + ], + [ + "details_caw" => "Api-Api", + "kodcaw" => "011230" + ], + [ + "details_caw" => "Kajang", + "kodcaw" => "011013" + ], + [ + "details_caw" => "Raja Laut", + "kodcaw" => "011412" + ], + [ + "details_caw" => "Beaufort", + "kodcaw" => "011231" + ], + [ + "details_caw" => "Kota Belud", + "kodcaw" => "011232" + ], + [ + "details_caw" => "Tuaran", + "kodcaw" => "011234" + ], + [ + "details_caw" => "Keningau", + "kodcaw" => "011233" + ], + [ + "details_caw" => "Tanah Merah", + "kodcaw" => "020303" + ], + [ + "details_caw" => "Temerloh", + "kodcaw" => "010618" + ], + [ + "details_caw" => "Semporna", + "kodcaw" => "011237" + ], + [ + "details_caw" => "Kota Bharu 1", + "kodcaw" => "KB1" + ] + ]; + + return collect($branches); + } +} diff --git a/routes/web.php b/routes/web.php index c05a331..572babb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -31,6 +31,15 @@ $router->group(['namespace' => '\Rap2hpoutre\LaravelLogViewer'], function() use // }); $router->group(['prefix'=>'api'], function () use ($router){ + + $router->group([ + 'prefix' => 'payable-ujrah-branches', + ], function($router){ + $router->get('/', 'PayableUjrahBranchController@index'); + $router->post('/', 'PayableUjrahBranchController@create'); + $router->post('/sync-status', 'PayableUjrahBranchController@syncStatus'); + }); + //Customer API $router->get('customers',['uses'=>'CustomerController@showAllCustomers']); $router->get('customers/cawangan/{kodcaw}',['uses'=>'CustomerController@showAllCustomersByCawangan']); @@ -126,7 +135,7 @@ $router->group(['prefix'=>'api'], function () use ($router){ $router->get('tebus/laporanjenistawarruqujrah/{kodcaw}',['uses'=>'TebusController@getLaporanTebusPengasinganUjrah']); //Ansuran Ujrah (Laporan Range Date) - $router->get('ansuranujrah/laporan/{kodcaw}',['uses'=>'GadaiController@getLaporanAnsuranUjrah']); + $router->get('ansuranujrah/laporan/{kodcaw}',['uses'=>'GadaiController@getLaporanAnsuranUjrah']); @@ -831,6 +840,7 @@ $router->group(['prefix'=>'admin'], function () use ($router){ $router->get('ig/bayaran_penghutang',['uses'=>'AdminPageController@SenaraiBayaranHutang']); }); + $router->group(['prefix'=>'lelong'], function () use ($router){ $router->get('upahtawarruq/{kodcaw}/{norujukan}/{jeniskeuntungan}',['uses'=>'TebusController@upahTawarruq']); $router->get('upahrebettawarruq/{kodcaw}/{norujukan}/{jeniskeuntungan}',['uses'=>'TebusController@upahRebetTawarruq']);