From f08a863e2c7b5bdf810681236a5c7ee32d04324a Mon Sep 17 00:00:00 2001 From: "weiken.intex" Date: Sun, 6 Dec 2020 02:14:22 +0800 Subject: [PATCH] create Wallet Transaction - integration --- .../CreateWalletTransactionLogic.php | 106 ++++++++++++++++++ .../WalletTransactionObject.php | 94 ++++++++++++++++ .../ChecksIfWalletTransactionBillNoExists.php | 22 ++++ .../Services/CreatesWalletTransaction.php | 30 +++++ .../GeneratesWalletTransactionBillNo.php | 27 +++++ .../Rules/CanCreateWalletTransaction.php | 50 +++++++++ .../WalletTransactionValidation.php | 40 +++++++ .../CreateWalletTransactionController.php | 15 +++ .../Resources/WalletTransactionResource.php | 29 +++++ app/Models/WalletTransaction.php | 27 +++++ ...212314_create_wallet_transaction_table.php | 45 ++++++++ routes/wallet.php | 1 + 12 files changed, 486 insertions(+) create mode 100644 app/Classes/Modules/Wallets/ControllersLogic/CreateWalletTransactionLogic.php create mode 100644 app/Classes/Modules/Wallets/DataTransferObjects/WalletTransactionObject.php create mode 100644 app/Classes/Modules/Wallets/Services/ChecksIfWalletTransactionBillNoExists.php create mode 100644 app/Classes/Modules/Wallets/Services/CreatesWalletTransaction.php create mode 100644 app/Classes/Modules/Wallets/Services/GeneratesWalletTransactionBillNo.php create mode 100644 app/Classes/Modules/Wallets/Standards/Rules/CanCreateWalletTransaction.php create mode 100644 app/Classes/Modules/Wallets/Standards/Validators/WalletTransactionValidation.php create mode 100644 app/Http/Controllers/Wallets/CreateWalletTransactionController.php create mode 100644 app/Http/Resources/WalletTransactionResource.php create mode 100644 app/Models/WalletTransaction.php create mode 100644 database/migrations/2020_11_30_212314_create_wallet_transaction_table.php diff --git a/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletTransactionLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletTransactionLogic.php new file mode 100644 index 00000000..7d6a642d --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletTransactionLogic.php @@ -0,0 +1,106 @@ + 'Created Wallet Transaction', + 'message' => 'You have successfully created a wallet transaction' + ]; + } + + /** @var CreatesWalletTransaction */ + private $createsWalletTransaction; + + /** @var GeneratesWalletTransactionBillNo */ + private $generatesWalletTransactionBillNo; + + /** @var CanCreateWalletTransaction */ + private $canCreateWalletTransaction; + + /** + * CreateWalletLogic constructor. + * @param CreatesWallet $createsWallet + * @param GeneratesWalletCode $generatesWalletCode + * @param CanCreateCompanyWallet $canCreateCompanyWallet + */ + public function __construct(CreatesWalletTransaction $createsWalletTransaction, GeneratesWalletTransactionBillNo $generatesWalletTransactionBillNo, CanCreateWalletTransaction $canCreateWalletTransaction) + { + $this->createsWalletTransaction = $createsWalletTransaction; + $this->generatesWalletTransactionBillNo = $generatesWalletTransactionBillNo; + $this->canCreateWalletTransaction = $canCreateWalletTransaction; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + try { + + DB::beginTransaction(); + + + $object = new WalletTransactionObject( + $request->route('id'), $this->generatesWalletTransactionBillNo->execute(),$request->input('trans_type'), + number_format( (float) $request->input('amount'), 5, '.', ''), $request->input('currency_id') , number_format( (float) $request->input('original_amount'), 5, '.', ''), + $request->input('original_currency_id'),number_format( (float) $request->input('currency_rate'), 5, '.', '') + ); + + $this->canCreateWalletTransaction->passes($object); + + $wallet_transaction = $this->createsWalletTransaction->execute($object); + + DB::commit(); + + return $this->resourceResponse(new WalletTransactionResource($wallet_transaction)); + + } catch (\Exception $exception) { + throw new ErrorException($exception->getMessage(), $exception->getCode()); + } + + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/DataTransferObjects/WalletTransactionObject.php b/app/Classes/Modules/Wallets/DataTransferObjects/WalletTransactionObject.php new file mode 100644 index 00000000..7fdfecbd --- /dev/null +++ b/app/Classes/Modules/Wallets/DataTransferObjects/WalletTransactionObject.php @@ -0,0 +1,94 @@ +wallet_id = $wallet_id; + $this->bill_no = $bill_no; + $this->trans_type = $trans_type; + $this->amount= $amount; + $this->currency_id = $currency_id; + $this->original_amount = $original_amount; + $this->original_currency_id = $original_currency_id; + $this->currency_rate = $currency_rate; + } + + /** + * @return int + */ + public function getWalletId(): int + { + return $this->wallet_id; + } + + /** + * @return int + */ + public function getBillNo(): int + { + return $this->bill_no; + } + + /** + * @return int + */ + public function getTransType(): int + { + return $this->trans_type; + } + + + public function getAmount(): float + { + return $this->amount; + } + + /** + * @return int + */ + public function getCurrency(): int + { + return $this->currency_id; + } + + + public function getOriginalAmount(): float + { + return $this->original_amount; + } + + public function getOriginalCurrency(): int + { + return $this->original_currency_id; + } + + public function getCurrencyRate(): float + { + return $this->currency_rate; + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Services/ChecksIfWalletTransactionBillNoExists.php b/app/Classes/Modules/Wallets/Services/ChecksIfWalletTransactionBillNoExists.php new file mode 100644 index 00000000..43df87d4 --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/ChecksIfWalletTransactionBillNoExists.php @@ -0,0 +1,22 @@ +repository = $repository; + } + + public function execute(int $bill_no): bool { + return $this->repository->where('bill_no', $bill_no)->exists(); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Services/CreatesWalletTransaction.php b/app/Classes/Modules/Wallets/Services/CreatesWalletTransaction.php new file mode 100644 index 00000000..06877e6a --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/CreatesWalletTransaction.php @@ -0,0 +1,30 @@ +wallet_id = $object->getWalletId(); + $model->bill_no = $object->getBillNo(); + $model->trans_type = $object->getTransType(); + $model->amount = $object->getAmount(); + $model->currency_id = $object->getCurrency(); + $model->original_amount = $object->getOriginalAmount(); + $model->original_currency_id = $object->getOriginalCurrency(); + $model->currency_rate = $object->getCurrencyRate(); + + return $this->handler($model); + + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Services/GeneratesWalletTransactionBillNo.php b/app/Classes/Modules/Wallets/Services/GeneratesWalletTransactionBillNo.php new file mode 100644 index 00000000..d3758d72 --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/GeneratesWalletTransactionBillNo.php @@ -0,0 +1,27 @@ +walletTransationBillNoExists = $walletTransationBillNoExists; + } + + + + public function execute(): int { + + $code = mt_rand(100000001, 999999999); + return !$this->walletTransationBillNoExists->execute($code) ? $code : self::execute(); + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Standards/Rules/CanCreateWalletTransaction.php b/app/Classes/Modules/Wallets/Standards/Rules/CanCreateWalletTransaction.php new file mode 100644 index 00000000..43f0118f --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Rules/CanCreateWalletTransaction.php @@ -0,0 +1,50 @@ +walletTransactionValidation = $walletTransactionValidation; + } + + + /** + * @return bool + */ + protected function authorized(): bool + { + // TODO Set Authorization rules + return true; + } + + /** + * @param CompanyWalletValidation $object + * @return bool + * @throws \App\Classes\Exceptions\RequestValidationException + */ + protected function validators($object): bool + { + return $this->walletTransactionValidation->validate($object); + } + + /** + * @param SegmentCompanyValidation $object + * @return bool + */ + protected function criteria($object): bool + { + return true; + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Standards/Validators/WalletTransactionValidation.php b/app/Classes/Modules/Wallets/Standards/Validators/WalletTransactionValidation.php new file mode 100644 index 00000000..14129c03 --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Validators/WalletTransactionValidation.php @@ -0,0 +1,40 @@ + $object->getWalletId(), + 'currency_id' => $object->getCurrency(), + 'original_currency_id' => $object->getOriginalCurrency(), + ]; + } + + /** + * @return array + */ + protected function rules(): array + { + return [ + 'wallet_id' => 'required', + 'currency_id' => 'required', + 'original_currency_id' => 'required', + ]; + } + + /** + * @return array + */ + protected function messages(): array + { + return []; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Wallets/CreateWalletTransactionController.php b/app/Http/Controllers/Wallets/CreateWalletTransactionController.php new file mode 100644 index 00000000..70b94a03 --- /dev/null +++ b/app/Http/Controllers/Wallets/CreateWalletTransactionController.php @@ -0,0 +1,15 @@ +execute($request); + } +} \ No newline at end of file diff --git a/app/Http/Resources/WalletTransactionResource.php b/app/Http/Resources/WalletTransactionResource.php new file mode 100644 index 00000000..1d0037fe --- /dev/null +++ b/app/Http/Resources/WalletTransactionResource.php @@ -0,0 +1,29 @@ + $this->id, + 'wallet_id' => (int) $this->wallet_id, + 'bill_no' => (int) $this->bill_no, + 'trans_type'=>(int) $this->trans_type, + 'amount' => (double) $this->amount, + 'currency_id' => (int) $this->currency_id, + 'original_amount' => (double) $this->original_amount, + 'original_currency_id' => (int) $this->original_currency_id, + 'currency_rate' => (double) $this->currency_rate + ]; + } +} diff --git a/app/Models/WalletTransaction.php b/app/Models/WalletTransaction.php new file mode 100644 index 00000000..efd5a878 --- /dev/null +++ b/app/Models/WalletTransaction.php @@ -0,0 +1,27 @@ +hasOne(Wallet::class, 'wallet_id', 'id'); + } + + public function currency(): HasOne + { + return $this->hasOne(Currency::class, 'currency_id', 'id'); + } + + public function original_currency(): HasOne + { + return $this->hasOne(Currency::class, 'original_currency_id', 'id'); + } + +} diff --git a/database/migrations/2020_11_30_212314_create_wallet_transaction_table.php b/database/migrations/2020_11_30_212314_create_wallet_transaction_table.php new file mode 100644 index 00000000..92818335 --- /dev/null +++ b/database/migrations/2020_11_30_212314_create_wallet_transaction_table.php @@ -0,0 +1,45 @@ +id(); + + $table->bigInteger('wallet_id')->unsigned(); + $table->bigInteger('bill_no')->unsigned(); + $table->integer('trans_type'); + $table->decimal('amount', 14, 5)->default(0.00); + $table->bigInteger('currency_id')->unsigned(); + $table->decimal('original_amount', 14, 5)->default(0.00); + $table->bigInteger('original_currency_id')->unsigned(); + $table->decimal('currency_rate', 14, 5)->default(0.00); + $table->timestamps(); + + $table->foreign('wallet_id')->references('id')->on('wallets')->onDelete('cascade'); + $table->foreign('currency_id')->references('id')->on('currencies')->onDelete('cascade'); + $table->foreign('original_currency_id')->references('id')->on('currencies')->onDelete('cascade'); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('wallet_transaction'); + } +} diff --git a/routes/wallet.php b/routes/wallet.php index 3c63ac88..6b19a47c 100644 --- a/routes/wallet.php +++ b/routes/wallet.php @@ -5,5 +5,6 @@ use Illuminate\Support\Facades\Route; Route::group(['prefix' => 'wallets', 'namespace' => 'Wallets', 'as' => 'wallet.'], function () { Route::post('/create', 'CreateWalletController@create')->name('create'); + Route::post('/create-transaction/{id}', 'CreateWalletTransactionController@create')->name('create_transaction'); }); \ No newline at end of file