From e94ef449282ca1c4d72dbbde76a28cc90f815ca0 Mon Sep 17 00:00:00 2001 From: Fairuz Date: Thu, 7 Oct 2021 01:34:11 +0800 Subject: [PATCH 1/6] Wallet operations --- .../CreateWalletTransactionProcessor.php | 76 +++++++++++++++++++ .../UpdateWalletTransactionProcessor.php | 76 +++++++++++++++++++ .../Services/CreatesWalletTransaction.php | 40 ++++++++++ .../ControllersLogic/CreateWalletLogic.php | 19 +++-- .../ControllersLogic/ListWalletLogic.php | 60 +++++++++++++++ .../ControllersLogic/TopUpWalletLogic.php | 70 +++++++++++++++++ .../UpdateStatusWalletLogic.php | 67 ++++++++++++++++ .../ControllersLogic/WithdrawWalletLogic.php | 70 +++++++++++++++++ .../DataTransferObjects/WalletObject.php | 12 ++- .../Wallets/Services/CreatesWallet.php | 12 +-- .../Modules/Wallets/Services/ListsWallet.php | 30 ++++++++ .../Wallets/Services/UpdatesWallet.php | 27 +++++++ .../Wallets/Standards/Rules/CanListWallet.php | 51 +++++++++++++ .../Standards/Rules/CanTopUpWallet.php | 51 +++++++++++++ .../Standards/Rules/CanWithdrawWallet.php | 51 +++++++++++++ .../Validators/ListWalletValidation.php | 30 ++++++++ .../Validators/TopUpWalletValidation.php | 30 ++++++++ .../Validators/WithdrawWalletValidation.php | 30 ++++++++ .../Constants/TransactionType.php | 2 + .../Wallets/ListWalletController.php | 14 ++++ .../Wallets/TopUpWalletController.php | 15 ++++ .../Wallets/UpdateStatusWalletController.php | 14 ++++ .../Wallets/WithdrawWalletController.php | 15 ++++ app/Models/Company.php | 8 ++ app/Models/Transaction.php | 27 ++++++- app/Models/Wallet.php | 28 +++++-- ..._082211_drop_wallet_transactions_table.php | 28 +++++++ ...1_10_02_082442_alter_wallet_company_id.php | 44 +++++++++++ ...02_083144_alter_transaction_booking_id.php | 45 +++++++++++ routes/api.php | 5 +- routes/wallet.php | 9 ++- 31 files changed, 1027 insertions(+), 29 deletions(-) create mode 100644 app/Classes/Modules/Transactions/Processors/CreateWalletTransactionProcessor.php create mode 100644 app/Classes/Modules/Transactions/Processors/UpdateWalletTransactionProcessor.php create mode 100644 app/Classes/Modules/Transactions/Services/CreatesWalletTransaction.php create mode 100644 app/Classes/Modules/Wallets/ControllersLogic/ListWalletLogic.php create mode 100644 app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php create mode 100644 app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php create mode 100644 app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php create mode 100644 app/Classes/Modules/Wallets/Services/ListsWallet.php create mode 100644 app/Classes/Modules/Wallets/Services/UpdatesWallet.php create mode 100644 app/Classes/Modules/Wallets/Standards/Rules/CanListWallet.php create mode 100644 app/Classes/Modules/Wallets/Standards/Rules/CanTopUpWallet.php create mode 100644 app/Classes/Modules/Wallets/Standards/Rules/CanWithdrawWallet.php create mode 100644 app/Classes/Modules/Wallets/Standards/Validators/ListWalletValidation.php create mode 100644 app/Classes/Modules/Wallets/Standards/Validators/TopUpWalletValidation.php create mode 100644 app/Classes/Modules/Wallets/Standards/Validators/WithdrawWalletValidation.php create mode 100644 app/Http/Controllers/Wallets/ListWalletController.php create mode 100644 app/Http/Controllers/Wallets/TopUpWalletController.php create mode 100644 app/Http/Controllers/Wallets/UpdateStatusWalletController.php create mode 100644 app/Http/Controllers/Wallets/WithdrawWalletController.php create mode 100644 database/migrations/2021_10_02_082211_drop_wallet_transactions_table.php create mode 100644 database/migrations/2021_10_02_082442_alter_wallet_company_id.php create mode 100644 database/migrations/2021_10_02_083144_alter_transaction_booking_id.php diff --git a/app/Classes/Modules/Transactions/Processors/CreateWalletTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateWalletTransactionProcessor.php new file mode 100644 index 00000000..4bde6135 --- /dev/null +++ b/app/Classes/Modules/Transactions/Processors/CreateWalletTransactionProcessor.php @@ -0,0 +1,76 @@ +generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsWalletTransaction = $createsWalletTransaction; + } + + + /** + * @param Booking $booking + * @return void + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function execute(Wallet $wallet, WalletObject $walletOject, int $transactionType) + { + + $billNumber = $this->generatesTransactionBillNumber->execute('WAL-'); + + $transaction_object = new TransactionObject( + $billNumber, + $transactionType, + $wallet->company->id, + 1, + 1, + PaymentMethodType::BA, + $walletOject->getAmount(), + $walletOject->getAmount(), + $wallet->currency_id, + $wallet->currency_id, + 0, + 0, + 0, + null, + ApprovalStatus::PENDING_SUBMISSION + ); + $walletTransaction = $this->createsWalletTransaction->execute($wallet, $transaction_object); + + return $walletTransaction; + } +} diff --git a/app/Classes/Modules/Transactions/Processors/UpdateWalletTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/UpdateWalletTransactionProcessor.php new file mode 100644 index 00000000..95f484a0 --- /dev/null +++ b/app/Classes/Modules/Transactions/Processors/UpdateWalletTransactionProcessor.php @@ -0,0 +1,76 @@ +fetchesTransaction = $fetchesTransaction; + $this->updatesTransactionStatus = $updatesTransactionStatus; + $this->updatesWallet = $updatesWallet; + } + + + /** + * @param Booking $booking + * @return void + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function execute(int $transactionId, int $transactionStatus) + { + + $transaction = $this->fetchesTransaction->execute(['id' => $transactionId]); + $wallet = $transaction->wallet; + + $walletOject = new WalletObject( $wallet->company->id, $wallet->currency_id, $wallet->code, $transaction->amount); + + $this->updatesTransactionStatus->execute($transaction, $transactionStatus); + + $this->updatesWallet->execute($wallet, $walletOject); + + return $wallet; + } +} diff --git a/app/Classes/Modules/Transactions/Services/CreatesWalletTransaction.php b/app/Classes/Modules/Transactions/Services/CreatesWalletTransaction.php new file mode 100644 index 00000000..7c3eacd6 --- /dev/null +++ b/app/Classes/Modules/Transactions/Services/CreatesWalletTransaction.php @@ -0,0 +1,40 @@ +bill_no = $object->getBillNo(); + $model->type = $object->getTransactionType(); + $model->issuer = $object->getIssuer(); + $model->receiver = $object->getReceiver(); + $model->recipient_bank_account_id = $object->getRecipientBankAccountId(); + $model->payment_method = $object->getPaymentMethod(); + $model->amount = $object->getAmount(); + $model->original_amount = $object->getOriginalAmount(); + $model->currency_id = $object->getCurrencyId(); + $model->original_currency_id = $object->getOriginalCurrencyId(); + $model->currency_rate = $object->getCurrencyRate(); + $model->tax = $object->getTax(); + $model->service_charge = $object->getServiceCharge(); + $model->expires_on = $object->getExpiresOn(); + $model->status = $object->getStatus(); + $model->booking_id = 0; + + return $this->handler($wallet->transactions(), $model); + + } +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php index efcff6b3..f0a16958 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php @@ -9,6 +9,7 @@ use App\Classes\Modules\Wallets\Services\CreatesWallet; use App\Classes\Modules\Wallets\Services\GeneratesWalletCode; use App\Classes\Modules\Wallets\Standards\Rules\CanCreateCompanyWallet; use App\Http\Resources\WalletResource; +use App\Classes\Modules\Companies\Services\FetchesCompany; use ErrorException; use Illuminate\Http\JsonResponse; @@ -38,14 +39,18 @@ class CreateWalletLogic extends AbstractControllerLogic /** @var CanCreateCompanyWallet */ private $canCreateCompanyWallet; + /** @var FetchesCompany */ + private $fetchesCompany; + /** * CreateWalletLogic constructor. * @param CreatesWallet $createsWallet * @param GeneratesWalletCode $generatesWalletCode * @param CanCreateCompanyWallet $canCreateCompanyWallet */ - public function __construct(CreatesWallet $createsWallet, GeneratesWalletCode $generatesWalletCode, CanCreateCompanyWallet $canCreateCompanyWallet) + public function __construct(CreatesWallet $createsWallet, GeneratesWalletCode $generatesWalletCode, CanCreateCompanyWallet $canCreateCompanyWallet, FetchesCompany $fetchesCompany) { + $this->fetchesCompany = $fetchesCompany; $this->createsWallet = $createsWallet; $this->generatesWalletCode = $generatesWalletCode; $this->canCreateCompanyWallet = $canCreateCompanyWallet; @@ -58,24 +63,22 @@ class CreateWalletLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { - try { DB::beginTransaction(); - $object = new WalletObject($request->input('currency_id'), $request->input('company_id'), $this->generatesWalletCode->execute()); + $object = new WalletObject($request->input('company_id'), $request->input('currency_id'), $this->generatesWalletCode->execute()); $this->canCreateCompanyWallet->passes($object); - $wallet = $this->createsWallet->execute($object); + $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); + + $wallet = $this->createsWallet->execute($object, $company); DB::commit(); return $this->resourceResponse(new WalletResource($wallet)); - } catch (\Exception $exception) { - throw new ErrorException($exception->getMessage(), $exception->getCode()); - } } -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/ListWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/ListWalletLogic.php new file mode 100644 index 00000000..cf6b86bb --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/ListWalletLogic.php @@ -0,0 +1,60 @@ + 'List Company Wallet', + 'message' => 'You have successfully list company wallet' + ]; + } + + /** @var ListWallet */ + private $listsWallet; + + /** @var CanCreateCompanyWallet */ + private $canListWallet; + + /** + * CreateWalletLogic constructor. + * @param CreatesWallet $createsWallet + * @param GeneratesWalletCode $generatesWalletCode + * @param CanCreateCompanyWallet $canCreateCompanyWallet + */ + public function __construct(CanListWallet $canListWallet, ListsWallet $listsWallet) + { + $this->canListWallet = $canListWallet; + $this->listsWallet = $listsWallet; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + //$this->canListWallet->passes(); + + $query = $this->listsWallet->execute($this->listsWallet->deserializeFilters($request->input('filters'))); + + return $this->collectionResponse(WalletResource::collection($query)); + } +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php new file mode 100644 index 00000000..817a9038 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php @@ -0,0 +1,70 @@ + 'TopUp into Company Wallet', + 'message' => 'You have successfully topup company wallet' + ]; + } + + /** @var ListWallet */ + private $fetchesWallet; + + /** @var CanCreateCompanyWallet */ + private $canListWallet; + + /** @var CreateInvoiceTransactionProcessor */ + private $createWalletTransactionProcessor; + + /** + * CreateWalletLogic constructor. + * @param CreatesWallet $createsWallet + * @param GeneratesWalletCode $generatesWalletCode + * @param CanCreateCompanyWallet $canCreateCompanyWallet + */ + public function __construct(FetchesWallet $fetchesWallet, CreateWalletTransactionProcessor $createWalletTransactionProcessor) + { + $this->fetchesWallet = $fetchesWallet; + $this->createWalletTransactionProcessor = $createWalletTransactionProcessor; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + //$this->canListWallet->passes(); + $wallet = $this->fetchesWallet->execute(['id' => $request->input('wallet_id')]); + + $walletOject = new WalletObject( $wallet->company->id, $wallet->currency_id, $wallet->code, $request->input('amount')); + $transaction = $this->createWalletTransactionProcessor->execute($wallet, $walletOject, TransactionType::TOP_UP); + + return $this->resourceResponse(new WalletResource($wallet)); + + } +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php new file mode 100644 index 00000000..a14051b7 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php @@ -0,0 +1,67 @@ + 'Update Status Transaction Company Wallet', + 'message' => 'You have successfully status transaction company wallet' + ]; + } + + /** @var ListWallet */ + private $fetchesWallet; + + /** @var CanCreateCompanyWallet */ + private $canListWallet; + + /** @var UpdateWalletTransactionProcessor */ + private $updateWalletTransactionProcessor; + + /** + * CreateWalletLogic constructor. + * @param CreatesWallet $createsWallet + * @param GeneratesWalletCode $generatesWalletCode + * @param CanCreateCompanyWallet $canCreateCompanyWallet + */ + public function __construct(FetchesWallet $fetchesWallet, UpdateWalletTransactionProcessor $updateWalletTransactionProcessor) + { + $this->fetchesWallet = $fetchesWallet; + $this->updateWalletTransactionProcessor = $updateWalletTransactionProcessor; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + $wallet = $this->updateWalletTransactionProcessor->execute($request->input('id'), $request->input('status')); + + return $this->resourceResponse(new WalletResource($wallet)); + + } +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php new file mode 100644 index 00000000..3a2bb21a --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php @@ -0,0 +1,70 @@ + 'Withdraw from Company Wallet', + 'message' => 'You have successfully withdraw company wallet' + ]; + } + + /** @var ListWallet */ + private $fetchesWallet; + + /** @var CanCreateCompanyWallet */ + private $canListWallet; + + /** @var CreateInvoiceTransactionProcessor */ + private $createWalletTransactionProcessor; + + /** + * CreateWalletLogic constructor. + * @param CreatesWallet $createsWallet + * @param GeneratesWalletCode $generatesWalletCode + * @param CanCreateCompanyWallet $canCreateCompanyWallet + */ + public function __construct(FetchesWallet $fetchesWallet, CreateWalletTransactionProcessor $createWalletTransactionProcessor) + { + $this->fetchesWallet = $fetchesWallet; + $this->createWalletTransactionProcessor = $createWalletTransactionProcessor; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + //$this->canListWallet->passes(); + $wallet = $this->fetchesWallet->execute(['id' => $request->input('wallet_id')]); + + $walletOject = new WalletObject( $wallet->company->id, $wallet->currency_id, $wallet->code, $request->input('amount')); + $transaction = $this->createWalletTransactionProcessor->execute($wallet, $walletOject, TransactionType::WITHDRAW); + + return $this->resourceResponse(new WalletResource($wallet)); + + } +} diff --git a/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php b/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php index 6f4fcd83..4bfae2f4 100644 --- a/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php +++ b/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php @@ -16,17 +16,20 @@ class WalletObject implements DataTransferObject /** @var int */ private $code; + private $amount; + /** * WalletObject constructor. * @param int $company_id * @param int $currency * @param int $code */ - public function __construct(int $company_id, int $currency, int $code) + public function __construct(int $company_id, int $currency, int $code, float $amount=0) { $this->company_id = $company_id; $this->currency_id = $currency; $this->code = $code; + $this->amount = $amount; } /** @@ -53,7 +56,10 @@ class WalletObject implements DataTransferObject return $this->code; } + public function getAmount(): float + { + return $this->amount; + } - -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Wallets/Services/CreatesWallet.php b/app/Classes/Modules/Wallets/Services/CreatesWallet.php index 720049cb..c114b9de 100644 --- a/app/Classes/Modules/Wallets/Services/CreatesWallet.php +++ b/app/Classes/Modules/Wallets/Services/CreatesWallet.php @@ -3,24 +3,26 @@ namespace App\Classes\Modules\Wallets\Services; use App\Classes\General\Eloquent\AbstractUpdateRecord; +use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord; use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; use App\Models\Wallet; +use App\Models\Company; -class CreatesWallet extends AbstractUpdateRecord +class CreatesWallet extends AbstractUpdateRelationshipRecord { /** * @param WalletObject $object * @return \Illuminate\Database\Eloquent\Model * @throws \App\Classes\Exceptions\MalformedRequestException */ - public function execute(WalletObject $object) { + public function execute(WalletObject $object, Company $company) { $model = new Wallet(); - $model->company_id = $object->getCompanyId(); + //$model->company_id = $object->getCompanyId(); $model->code = $object->getCode(); $model->currency_id = $object->getCurrency(); - return $this->handler($model); + return $this->handler($company->wallets(), $model); } -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Wallets/Services/ListsWallet.php b/app/Classes/Modules/Wallets/Services/ListsWallet.php new file mode 100644 index 00000000..c11e2063 --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/ListsWallet.php @@ -0,0 +1,30 @@ +repository = $repository; + } + + /** + * @return Builder + */ + public function getRepository(): Builder + { + return $this->repository->newQuery(); + } +} diff --git a/app/Classes/Modules/Wallets/Services/UpdatesWallet.php b/app/Classes/Modules/Wallets/Services/UpdatesWallet.php new file mode 100644 index 00000000..24ce2415 --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/UpdatesWallet.php @@ -0,0 +1,27 @@ +amount += $object->getAmount(); + $model->code = $object->getCode(); + $model->currency_id = $object->getCurrency(); + + + return $this->handler($model); + + } +} diff --git a/app/Classes/Modules/Wallets/Standards/Rules/CanListWallet.php b/app/Classes/Modules/Wallets/Standards/Rules/CanListWallet.php new file mode 100644 index 00000000..c87cb316 --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Rules/CanListWallet.php @@ -0,0 +1,51 @@ +listWalletValidation = $listWalletValidation; + } + + + /** + * @return bool + */ + protected function authorized(): bool + { + // TODO Set Authorization rules + return true; + } + + /** + * @param WalletObject $object + * @return bool + * @throws \App\Classes\Exceptions\RequestValidationException + */ + protected function validators($object): bool + { + return $this->listWalletValidation->validate($object); + } + + /** + * @param WalletTransactionObject $object + * @return bool + */ + protected function criteria($object): bool + { + return true; + } + +} diff --git a/app/Classes/Modules/Wallets/Standards/Rules/CanTopUpWallet.php b/app/Classes/Modules/Wallets/Standards/Rules/CanTopUpWallet.php new file mode 100644 index 00000000..9a005498 --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Rules/CanTopUpWallet.php @@ -0,0 +1,51 @@ +topUpWalletValidation = $topUpWalletValidation; + } + + + /** + * @return bool + */ + protected function authorized(): bool + { + // TODO Set Authorization rules + return true; + } + + /** + * @param WalletObject $object + * @return bool + * @throws \App\Classes\Exceptions\RequestValidationException + */ + protected function validators($object): bool + { + return $this->topUpWalletValidation->validate($object); + } + + /** + * @param WalletTransactionObject $object + * @return bool + */ + protected function criteria($object): bool + { + return true; + } + +} diff --git a/app/Classes/Modules/Wallets/Standards/Rules/CanWithdrawWallet.php b/app/Classes/Modules/Wallets/Standards/Rules/CanWithdrawWallet.php new file mode 100644 index 00000000..6fb4019d --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Rules/CanWithdrawWallet.php @@ -0,0 +1,51 @@ +witdrawWalletValidation = $witdrawWalletValidation; + } + + + /** + * @return bool + */ + protected function authorized(): bool + { + // TODO Set Authorization rules + return true; + } + + /** + * @param WalletObject $object + * @return bool + * @throws \App\Classes\Exceptions\RequestValidationException + */ + protected function validators($object): bool + { + return $this->witdrawWalletValidation->validate($object); + } + + /** + * @param WalletTransactionObject $object + * @return bool + */ + protected function criteria($object): bool + { + return true; + } + +} diff --git a/app/Classes/Modules/Wallets/Standards/Validators/ListWalletValidation.php b/app/Classes/Modules/Wallets/Standards/Validators/ListWalletValidation.php new file mode 100644 index 00000000..1f75aa54 --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Validators/ListWalletValidation.php @@ -0,0 +1,30 @@ +execute($request); + } +} diff --git a/app/Http/Controllers/Wallets/TopUpWalletController.php b/app/Http/Controllers/Wallets/TopUpWalletController.php new file mode 100644 index 00000000..aaa707bd --- /dev/null +++ b/app/Http/Controllers/Wallets/TopUpWalletController.php @@ -0,0 +1,15 @@ +execute($request); + } +} diff --git a/app/Http/Controllers/Wallets/UpdateStatusWalletController.php b/app/Http/Controllers/Wallets/UpdateStatusWalletController.php new file mode 100644 index 00000000..21eea924 --- /dev/null +++ b/app/Http/Controllers/Wallets/UpdateStatusWalletController.php @@ -0,0 +1,14 @@ +execute($request); + } +} diff --git a/app/Http/Controllers/Wallets/WithdrawWalletController.php b/app/Http/Controllers/Wallets/WithdrawWalletController.php new file mode 100644 index 00000000..0d125aa7 --- /dev/null +++ b/app/Http/Controllers/Wallets/WithdrawWalletController.php @@ -0,0 +1,15 @@ +execute($request); + } +} diff --git a/app/Models/Company.php b/app/Models/Company.php index c1a6a191..eba6677f 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -90,6 +90,14 @@ class Company extends AbstractModel implements Documentable return $this->HasMany(Booking::class, 'company_id'); } + /** + * @return MorphMany + */ + public function wallets(): morphMany + { + return $this->morphMany(Wallet::class, 'owner'); + } + /** * @return Builder */ diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 9d1ed5b9..c290ac4b 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -10,18 +10,41 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphMany; +use Illuminate\Database\Eloquent\Relations\MorphTo; class Transaction extends AbstractModel implements Documentable { protected $table = 'transactions'; + public function owner(): morphTo + { + return $this->morphTo(); + } + /** + * @return MorphMany + */ + public function booking(): morphMany + { + //return $this->BelongsTo( Booking::class, 'booking_id', 'id'); + return $this->morphMany(Booking::class, 'owner'); + } + + /** + * @return MorphMany + */ + public function wallets(): morphMany + { + return $this->morphMany(Wallet::class, 'owner'); + } + + /** * @return BelongsTo */ - public function booking(): BelongsTo + public function wallet(): BelongsTo { - return $this->BelongsTo( Booking::class, 'booking_id', 'id'); + return $this->BelongsTo(Wallet::class, 'owner_id', 'id'); } /** diff --git a/app/Models/Wallet.php b/app/Models/Wallet.php index f9808e9e..52d8f718 100644 --- a/app/Models/Wallet.php +++ b/app/Models/Wallet.php @@ -2,16 +2,34 @@ namespace App\Models; -use Illuminate\Database\Eloquent\Relations\HasOne; - +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\MorphMany; class Wallet extends AbstractModel { - protected $table = 'wallets'; + use SoftDeletes; - public function company(): HasOne + protected $table = 'wallets'; + /** + * @return \Illuminate\Database\Eloquent\Relations\MorphTo + */ + public function owner(): morphTo { - return $this->hasOne(Company::class, 'company_id', 'id'); + return $this->morphTo(); + } + public function company(): BelongsTo + { + return $this->BelongsTo(Company::class, 'owner_id', 'id'); } + /** + * @return morphMany + */ + public function transactions(): morphMany + { + return $this->morphMany(Transaction::class, 'owner'); + } } diff --git a/database/migrations/2021_10_02_082211_drop_wallet_transactions_table.php b/database/migrations/2021_10_02_082211_drop_wallet_transactions_table.php new file mode 100644 index 00000000..f16eeba3 --- /dev/null +++ b/database/migrations/2021_10_02_082211_drop_wallet_transactions_table.php @@ -0,0 +1,28 @@ +dropForeign('wallets_company_id_foreign'); + $table->dropColumn('company_id'); + }); + } + + if (!Schema::hasColumn('wallets', 'owner_id')) { + Schema::table('wallets', function (Blueprint $table) { + $table->morphs('owner'); + }); + + //In-case the model name lengthy + Schema::table('wallets', function (Blueprint $table) { + $table->string('owner_type', 250)->change(); + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/database/migrations/2021_10_02_083144_alter_transaction_booking_id.php b/database/migrations/2021_10_02_083144_alter_transaction_booking_id.php new file mode 100644 index 00000000..e95c1e58 --- /dev/null +++ b/database/migrations/2021_10_02_083144_alter_transaction_booking_id.php @@ -0,0 +1,45 @@ +morphs('owner'); + }); + + //In-case the model name lengthy + Schema::table('transactions', function (Blueprint $table) { + $table->string('owner_type', 250)->change(); + }); + + DB::statement("UPDATE transactions SET owner_type='App\\\\Models\\\\Booking', owner_id = booking_id"); + + Schema::table('transactions', function (Blueprint $table) { + $table->dropForeign('transactions_booking_id_foreign'); + //$table->dropColumn('booking_id'); + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/routes/api.php b/routes/api.php index 146d519a..426033a3 100644 --- a/routes/api.php +++ b/routes/api.php @@ -14,7 +14,7 @@ use Illuminate\Support\Facades\Route; */ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function () { - + require __DIR__ . '/account.php'; // Route::get('/rate/calculate', 'RateCalculateCurrencyController@convert')->name('calculate'); @@ -47,7 +47,8 @@ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function require __DIR__ . '/announcement.php'; -// require __DIR__ . '/wallet.php'; + require __DIR__ . '/wallet.php'; + // require __DIR__ . '/rate.php'; // require __DIR__ . '/receipt.php'; diff --git a/routes/wallet.php b/routes/wallet.php index 6b19a47c..13288fb2 100644 --- a/routes/wallet.php +++ b/routes/wallet.php @@ -3,8 +3,9 @@ use Illuminate\Support\Facades\Route; Route::group(['prefix' => 'wallets', 'namespace' => 'Wallets', 'as' => 'wallet.'], function () { - + Route::get('/', 'ListWalletController@list')->name('list'); Route::post('/create', 'CreateWalletController@create')->name('create'); - Route::post('/create-transaction/{id}', 'CreateWalletTransactionController@create')->name('create_transaction'); - -}); \ No newline at end of file + Route::post('/topup', 'TopUpWalletController@topUp')->name('topup'); + Route::post('/withdraw', 'WithdrawWalletController@withdraw')->name('withdraw'); + Route::post('/update-status', 'UpdateStatusWalletController@updateStatus')->name('update.status'); +}); From d56442e6ff9ca53387657071f74623795a0ce4af Mon Sep 17 00:00:00 2001 From: Fairuz Date: Thu, 7 Oct 2021 21:15:29 +0800 Subject: [PATCH 2/6] Wallet operations --- .../CreateWalletTransactionProcessor.php | 17 +--------- .../UpdateWalletTransactionProcessor.php | 32 +++++++------------ .../ControllersLogic/CreateWalletLogic.php | 18 +++-------- .../ControllersLogic/TopUpWalletLogic.php | 18 ++++++----- .../ControllersLogic/WithdrawWalletLogic.php | 18 ++++++----- .../Wallets/Services/UpdatesWallet.php | 2 +- 6 files changed, 39 insertions(+), 66 deletions(-) diff --git a/app/Classes/Modules/Transactions/Processors/CreateWalletTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateWalletTransactionProcessor.php index 4bde6135..7297c670 100644 --- a/app/Classes/Modules/Transactions/Processors/CreateWalletTransactionProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/CreateWalletTransactionProcessor.php @@ -2,30 +2,15 @@ namespace App\Classes\Modules\Transactions\Processors; -use App\Classes\Modules\Bookings\Services\CalculatesBookingPayableAmount; -use App\Classes\Modules\Bookings\Services\CalculatesBookingTransferredAmount; -use App\Classes\Modules\ServiceTypes\Services\FetchesServiceConfigurations; -use App\Classes\Modules\Transactions\Services\ListsTransactions; +use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; use App\Classes\Modules\Transactions\Services\CreatesWalletTransaction; use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; -use App\Classes\Modules\Bookings\Services\CalculatesBookingPaidAmount; -use App\Classes\Modules\Bookings\Services\CalculatesBookingCurrencyAverageRate; -use App\Classes\Modules\Companies\Services\FetchesCompany; -use App\Classes\Modules\Documents\Services\CreatesDocument; -use App\Classes\Modules\Documents\Services\CreatesFiles; -use App\Classes\Modules\Bookings\Services\UpdatesBookingStatus; -use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; - use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; -use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\SegmentConstants; use App\Classes\ValueObjects\Constants\TransactionType; use App\Classes\ValueObjects\Constants\PaymentMethodType; use App\Models\Wallet; -use App\Models\Document; -use App\Models\SegmentConstant; -use Meneses\LaravelMpdf\Facades\LaravelMpdf; class CreateWalletTransactionProcessor { diff --git a/app/Classes/Modules/Transactions/Processors/UpdateWalletTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/UpdateWalletTransactionProcessor.php index 95f484a0..7c7277f8 100644 --- a/app/Classes/Modules/Transactions/Processors/UpdateWalletTransactionProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/UpdateWalletTransactionProcessor.php @@ -2,35 +2,16 @@ namespace App\Classes\Modules\Transactions\Processors; -use App\Classes\Modules\Bookings\Services\CalculatesBookingPayableAmount; -use App\Classes\Modules\Bookings\Services\CalculatesBookingTransferredAmount; -use App\Classes\Modules\ServiceTypes\Services\FetchesServiceConfigurations; -use App\Classes\Modules\Transactions\Services\ListsTransactions; -use App\Classes\Modules\Transactions\Services\CreatesWalletTransaction; -use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; -use App\Classes\Modules\Bookings\Services\CalculatesBookingPaidAmount; -use App\Classes\Modules\Bookings\Services\CalculatesBookingCurrencyAverageRate; -use App\Classes\Modules\Companies\Services\FetchesCompany; -use App\Classes\Modules\Documents\Services\CreatesDocument; -use App\Classes\Modules\Documents\Services\CreatesFiles; -use App\Classes\Modules\Bookings\Services\UpdatesBookingStatus; - use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; use App\Classes\Modules\Transactions\Services\FetchesTransaction; use App\Classes\Modules\Wallets\Services\FetchesWallet; use App\Classes\Modules\Wallets\Services\UpdatesWallet; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; - use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; -use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject; use App\Classes\ValueObjects\Constants\ApprovalStatus; -use App\Classes\ValueObjects\Constants\SegmentConstants; use App\Classes\ValueObjects\Constants\TransactionType; use App\Classes\ValueObjects\Constants\PaymentMethodType; use App\Models\Wallet; -use App\Models\Document; -use App\Models\SegmentConstant; -use Meneses\LaravelMpdf\Facades\LaravelMpdf; class UpdateWalletTransactionProcessor { @@ -65,7 +46,18 @@ class UpdateWalletTransactionProcessor $transaction = $this->fetchesTransaction->execute(['id' => $transactionId]); $wallet = $transaction->wallet; - $walletOject = new WalletObject( $wallet->company->id, $wallet->currency_id, $wallet->code, $transaction->amount); + switch($transaction->type){ + case TransactionType::TOP_UP: + $updateWalletAmount = $transaction->amount + $wallet->amount; + break; + case TransactionType::WITHDRAW: + $updateWalletAmount = $wallet->amount - $transaction->amount; + break; + default: + break; + } + + $walletOject = new WalletObject( $wallet->company->id, $wallet->currency_id, $wallet->code, $updateWalletAmount); $this->updatesTransactionStatus->execute($transaction, $transactionStatus); diff --git a/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php index f0a16958..9a613b43 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php @@ -63,22 +63,14 @@ class CreateWalletLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { + $object = new WalletObject($request->input('company_id'), $request->input('currency_id'), $this->generatesWalletCode->execute()); - DB::beginTransaction(); + $this->canCreateCompanyWallet->passes($object); + $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); - $object = new WalletObject($request->input('company_id'), $request->input('currency_id'), $this->generatesWalletCode->execute()); - - $this->canCreateCompanyWallet->passes($object); - - $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); - - $wallet = $this->createsWallet->execute($object, $company); - - DB::commit(); - - return $this->resourceResponse(new WalletResource($wallet)); - + $wallet = $this->createsWallet->execute($object, $company); + return $this->resourceResponse(new WalletResource($wallet)); } } diff --git a/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php index 817a9038..8497ec32 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php @@ -9,7 +9,7 @@ use App\Classes\Modules\Wallets\Services\ListsWallet; use App\Classes\Modules\Wallets\Services\FetchesWallet; use App\Classes\ValueObjects\Constants\TransactionType; -use App\Classes\Modules\Wallets\Standards\Rules\CanListWallet; +use App\Classes\Modules\Wallets\Standards\Rules\CanTopUpWallet; use App\Http\Resources\WalletResource; use App\Classes\Modules\Transactions\Processors\CreateWalletTransactionProcessor; @@ -30,13 +30,13 @@ class TopUpWalletLogic extends AbstractControllerLogic ]; } - /** @var ListWallet */ + /** @var FetchesWallet */ private $fetchesWallet; - /** @var CanCreateCompanyWallet */ - private $canListWallet; + /** @var CanTopUpWallet */ + private $canTopUpWallet; - /** @var CreateInvoiceTransactionProcessor */ + /** @var CreateWalletTransactionProcessor */ private $createWalletTransactionProcessor; /** @@ -45,8 +45,9 @@ class TopUpWalletLogic extends AbstractControllerLogic * @param GeneratesWalletCode $generatesWalletCode * @param CanCreateCompanyWallet $canCreateCompanyWallet */ - public function __construct(FetchesWallet $fetchesWallet, CreateWalletTransactionProcessor $createWalletTransactionProcessor) + public function __construct(CanTopUpWallet $canTopUpWallet, FetchesWallet $fetchesWallet, CreateWalletTransactionProcessor $createWalletTransactionProcessor) { + $this->canTopUpWallet = $canTopUpWallet; $this->fetchesWallet = $fetchesWallet; $this->createWalletTransactionProcessor = $createWalletTransactionProcessor; } @@ -58,13 +59,14 @@ class TopUpWalletLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { - //$this->canListWallet->passes(); $wallet = $this->fetchesWallet->execute(['id' => $request->input('wallet_id')]); $walletOject = new WalletObject( $wallet->company->id, $wallet->currency_id, $wallet->code, $request->input('amount')); + + $this->canTopUpWallet->passes($walletOject); + $transaction = $this->createWalletTransactionProcessor->execute($wallet, $walletOject, TransactionType::TOP_UP); return $this->resourceResponse(new WalletResource($wallet)); - } } diff --git a/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php index 3a2bb21a..b1491ab7 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php @@ -9,7 +9,7 @@ use App\Classes\Modules\Wallets\Services\ListsWallet; use App\Classes\Modules\Wallets\Services\FetchesWallet; use App\Classes\ValueObjects\Constants\TransactionType; -use App\Classes\Modules\Wallets\Standards\Rules\CanListWallet; +use App\Classes\Modules\Wallets\Standards\Rules\CanWithdrawWallet; use App\Http\Resources\WalletResource; use App\Classes\Modules\Transactions\Processors\CreateWalletTransactionProcessor; @@ -30,13 +30,13 @@ class WithdrawWalletLogic extends AbstractControllerLogic ]; } - /** @var ListWallet */ + /** @var FetchesWallet */ private $fetchesWallet; - /** @var CanCreateCompanyWallet */ - private $canListWallet; + /** @var CanWithdrawWallet */ + private $canWithdrawWallet; - /** @var CreateInvoiceTransactionProcessor */ + /** @var CreateWalletTransactionProcessor */ private $createWalletTransactionProcessor; /** @@ -45,8 +45,9 @@ class WithdrawWalletLogic extends AbstractControllerLogic * @param GeneratesWalletCode $generatesWalletCode * @param CanCreateCompanyWallet $canCreateCompanyWallet */ - public function __construct(FetchesWallet $fetchesWallet, CreateWalletTransactionProcessor $createWalletTransactionProcessor) + public function __construct(CanWithdrawWallet $canWithdrawWallet, FetchesWallet $fetchesWallet, CreateWalletTransactionProcessor $createWalletTransactionProcessor) { + $this->canWithdrawWallet = $canWithdrawWallet; $this->fetchesWallet = $fetchesWallet; $this->createWalletTransactionProcessor = $createWalletTransactionProcessor; } @@ -58,13 +59,14 @@ class WithdrawWalletLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { - //$this->canListWallet->passes(); $wallet = $this->fetchesWallet->execute(['id' => $request->input('wallet_id')]); $walletOject = new WalletObject( $wallet->company->id, $wallet->currency_id, $wallet->code, $request->input('amount')); + + $this->canWithdrawWallet->passes($walletOject); + $transaction = $this->createWalletTransactionProcessor->execute($wallet, $walletOject, TransactionType::WITHDRAW); return $this->resourceResponse(new WalletResource($wallet)); - } } diff --git a/app/Classes/Modules/Wallets/Services/UpdatesWallet.php b/app/Classes/Modules/Wallets/Services/UpdatesWallet.php index 24ce2415..8b268fab 100644 --- a/app/Classes/Modules/Wallets/Services/UpdatesWallet.php +++ b/app/Classes/Modules/Wallets/Services/UpdatesWallet.php @@ -16,7 +16,7 @@ class UpdatesWallet extends AbstractUpdateRecord * @throws \App\Classes\Exceptions\MalformedRequestException */ public function execute(Wallet $model, WalletObject $object) { - $model->amount += $object->getAmount(); + $model->amount = $object->getAmount(); $model->code = $object->getCode(); $model->currency_id = $object->getCurrency(); From 004e56cd2ec9eb90c036ba610cddb70241d94ceb Mon Sep 17 00:00:00 2001 From: Fairuz Date: Fri, 8 Oct 2021 21:56:37 +0800 Subject: [PATCH 3/6] Wallet approval / reject --- .../Wallets/ControllersLogic/UpdateStatusWalletLogic.php | 4 +++- routes/wallet.php | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php index a14051b7..b051d3e3 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php @@ -59,7 +59,9 @@ class UpdateStatusWalletLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { - $wallet = $this->updateWalletTransactionProcessor->execute($request->input('id'), $request->input('status')); + $status = ($request->route('status')=='approve') ? 2 : 4; + + $wallet = $this->updateWalletTransactionProcessor->execute($request->route('transaction_id'), $status); return $this->resourceResponse(new WalletResource($wallet)); diff --git a/routes/wallet.php b/routes/wallet.php index 13288fb2..7ad2a479 100644 --- a/routes/wallet.php +++ b/routes/wallet.php @@ -7,5 +7,6 @@ Route::group(['prefix' => 'wallets', 'namespace' => 'Wallets', 'as' => 'wallet.' Route::post('/create', 'CreateWalletController@create')->name('create'); Route::post('/topup', 'TopUpWalletController@topUp')->name('topup'); Route::post('/withdraw', 'WithdrawWalletController@withdraw')->name('withdraw'); - Route::post('/update-status', 'UpdateStatusWalletController@updateStatus')->name('update.status'); + Route::put('/{transaction_id}/update-status/{status}', 'UpdateStatusWalletController@updateStatus')->where('status', 'approve|reject')->name('approval'); + }); From 2ffefc03bb7e67ab86ba17406621e1a66c84b932 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Fri, 22 Oct 2021 14:47:01 +0800 Subject: [PATCH 4/6] fix MorphMany transaction --- app/Classes/General/Interfaces/Transactionable.php | 13 +++++++++++++ .../ControllersLogic/CreateBookingPaymentLogic.php | 3 +-- .../CreateSupplierTransactionLogic.php | 4 ++-- .../Transactions/Services/CreatesTransaction.php | 7 +++---- app/Http/Resources/BookingResource.php | 7 ++++++- app/Models/Booking.php | 9 +++++---- app/Models/Transaction.php | 5 ++--- composer.json | 1 + ...21_10_02_083144_alter_transaction_booking_id.php | 2 +- 9 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 app/Classes/General/Interfaces/Transactionable.php diff --git a/app/Classes/General/Interfaces/Transactionable.php b/app/Classes/General/Interfaces/Transactionable.php new file mode 100644 index 00000000..174cb48d --- /dev/null +++ b/app/Classes/General/Interfaces/Transactionable.php @@ -0,0 +1,13 @@ +route('id')); $conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $request->input('amount'))), $booking->convertible_currency_id, $booking->service_id, $booking->fix_currency_id === 1 ? 0:1, PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')]); @@ -95,7 +94,7 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $configurations->getTax(), $configurations->getServiceCharge(), Carbon::now()->addMinutes($paymentAttemptLimit), ApprovalStatus::PENDING_SUBMISSION); $transaction = $this->createsTransaction->execute($booking, $object); - + return $this->resourceResponse(new TransactionResource($transaction)); } diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php index 01e18b40..fde2b235 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php @@ -101,7 +101,7 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic /** @var Transaction $payment */ $payment = $this->fetchesTransaction->execute(['id' => $payment['id']]); - + if($payment->status !== ApprovalStatus::APPROVED) continue; $this->updatesTransactionStatus->execute($payment, ApprovalStatus::COMPLETED); @@ -109,7 +109,7 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic $object = new TransactionObject($billNumber, TransactionType::BILL, $supplier->id, 1, $supplier->banks()->where('default', true)->first()->id, PaymentMethodType::CASH, $payment->original_amount * (1 / $rate), $payment->original_amount, 1, $payment->original_currency_id, - $rate, 0, 0, null, ApprovalStatus::APPROVED); + $rate, 0, 0, null, ApprovalStatus:: APPROVED); $transactions[] = $this->createsTransaction->execute($payment->booking, $object); } diff --git a/app/Classes/Modules/Transactions/Services/CreatesTransaction.php b/app/Classes/Modules/Transactions/Services/CreatesTransaction.php index ddebf0d5..fd5a403b 100644 --- a/app/Classes/Modules/Transactions/Services/CreatesTransaction.php +++ b/app/Classes/Modules/Transactions/Services/CreatesTransaction.php @@ -2,8 +2,8 @@ namespace App\Classes\Modules\Transactions\Services; -use App\Classes\General\Eloquent\AbstractUpdateRecord; use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord; +use App\Classes\General\Interfaces\Transactionable; use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; use App\Models\Booking; use App\Models\Transaction; @@ -15,7 +15,7 @@ class CreatesTransaction extends AbstractUpdateRelationshipRecord * @return \Illuminate\Database\Eloquent\Model * @throws \App\Classes\Exceptions\MalformedRequestException */ - public function execute(Booking $booking, TransactionObject $object) { + public function execute(Transactionable $transactionable, TransactionObject $object) { $model = new Transaction(); $model->bill_no = $object->getBillNo(); $model->type = $object->getTransactionType(); @@ -33,8 +33,7 @@ class CreatesTransaction extends AbstractUpdateRelationshipRecord $model->expires_on = $object->getExpiresOn(); $model->status = $object->getStatus(); - - return $this->handler($booking->transactions(), $model); + return $this->handler($transactionable->transactions(), $model); } } \ No newline at end of file diff --git a/app/Http/Resources/BookingResource.php b/app/Http/Resources/BookingResource.php index cc5f0dca..6333d46d 100644 --- a/app/Http/Resources/BookingResource.php +++ b/app/Http/Resources/BookingResource.php @@ -46,7 +46,12 @@ class BookingResource extends JsonResource 'created_at' => Carbon::parse($this->created_at)->format('d-m-Y'), $this->mergeWhen($this->relationLoaded('transactions'), [ 'purchase_order' => new TransactionResource($this->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first()), - 'payment_attempts' => TransactionResource::collection($this->transactions()->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION)->whereDate('expires_on', '>=', Carbon::now())->get()), + 'payment_attempts' => TransactionResource::collection( + $this->transactions() + ->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION) + ->whereDate('expires_on', '>=', Carbon::now()) + ->get() + ), 'expired_payment_attempts' => TransactionResource::collection($this->transactions()->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION)->whereDate('expires_on', '<', Carbon::now())->get()), 'payment_history' => TransactionResource::collection($this->transactions()->where(function($query){ $query->where(function($query){ diff --git a/app/Models/Booking.php b/app/Models/Booking.php index 33c33341..c3492241 100644 --- a/app/Models/Booking.php +++ b/app/Models/Booking.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Classes\General\Interfaces\Documentable; +use App\Classes\General\Interfaces\Transactionable; use App\Classes\ValueObjects\Constants\RoleTypes; use App\Scopes\CustomerBookingsScope; use Illuminate\Database\Eloquent\Builder; @@ -24,7 +25,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @property \App\Models\Currency convertible_currency_id * @property \App\Models\Currency conversion_currency_id */ -class Booking extends AbstractModel implements Documentable +class Booking extends AbstractModel implements Documentable, Transactionable { use SoftDeletes; @@ -89,11 +90,11 @@ class Booking extends AbstractModel implements Documentable } /** - * @return HasMany + * @return MorphMany */ - public function transactions(): HasMany + public function transactions(): MorphMany { - return $this->HasMany(Transaction::class, 'booking_id'); + return $this->MorphMany(Transaction::class, 'owner'); } protected static function booted() diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index c290ac4b..cad64361 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -25,10 +25,9 @@ class Transaction extends AbstractModel implements Documentable /** * @return MorphMany */ - public function booking(): morphMany + public function booking(): BelongsTo { - //return $this->BelongsTo( Booking::class, 'booking_id', 'id'); - return $this->morphMany(Booking::class, 'owner'); + return $this->BelongsTo(Booking::class, 'owner_id', 'id'); } /** diff --git a/composer.json b/composer.json index 7a3ac9b3..7715f0ca 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "ext-json": "^1.6", "barryvdh/laravel-dompdf": "^0.9.0", "carlos-meneses/laravel-mpdf": "^2.1", + "doctrine/dbal": "2.*", "fideloper/proxy": "^4.2", "fruitcake/laravel-cors": "^1.0", "guzzlehttp/guzzle": "^6.3", diff --git a/database/migrations/2021_10_02_083144_alter_transaction_booking_id.php b/database/migrations/2021_10_02_083144_alter_transaction_booking_id.php index e95c1e58..a37da4e4 100644 --- a/database/migrations/2021_10_02_083144_alter_transaction_booking_id.php +++ b/database/migrations/2021_10_02_083144_alter_transaction_booking_id.php @@ -28,7 +28,7 @@ class AlterTransactionBookingId extends Migration Schema::table('transactions', function (Blueprint $table) { $table->dropForeign('transactions_booking_id_foreign'); - //$table->dropColumn('booking_id'); + $table->dropColumn('booking_id'); }); } } From beed76c407e1367693dbe6c0f7f42b63b4e6a237 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Tue, 26 Oct 2021 22:04:19 +0800 Subject: [PATCH 5/6] company transactions owner_id --- app/Models/Company.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Company.php b/app/Models/Company.php index ab94c46e..7b8e4387 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -99,7 +99,7 @@ class Company extends AbstractModel implements Documentable */ public function transactions(): hasManyDeep { - return $this->hasManyDeep(Transaction::class, [Booking::class], ['company_id', 'booking_id'], ['id', 'id']); + return $this->hasManyDeep(Transaction::class, [Booking::class], ['company_id', 'owner_id'], ['id', 'id']); } /** From 70f95f511d98c7dff39d0443cd2df84da351443d Mon Sep 17 00:00:00 2001 From: glovetleong Date: Wed, 27 Oct 2021 14:22:32 +0800 Subject: [PATCH 6/6] transaction status downgrade --- .../ControllersLogic/CreatePaymentProofDocumentLogic.php | 2 +- .../ControllersLogic/CreateSupplierTransactionLogic.php | 2 +- app/Models/Transaction.php | 2 +- resources/views/pages/dashboards/admin.blade.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreatePaymentProofDocumentLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreatePaymentProofDocumentLogic.php index 111cd0db..81eb0c8a 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreatePaymentProofDocumentLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreatePaymentProofDocumentLogic.php @@ -83,7 +83,7 @@ class CreatePaymentProofDocumentLogic extends AbstractControllerLogic $this->createsFile->execute($document, $object); - $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::COMPLETED); + $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::APPROVED); $this->createInvoiceTransactionProcessor->execute($transaction->booking); diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php index fde2b235..f496ab4f 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php @@ -109,7 +109,7 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic $object = new TransactionObject($billNumber, TransactionType::BILL, $supplier->id, 1, $supplier->banks()->where('default', true)->first()->id, PaymentMethodType::CASH, $payment->original_amount * (1 / $rate), $payment->original_amount, 1, $payment->original_currency_id, - $rate, 0, 0, null, ApprovalStatus:: APPROVED); + $rate, 0, 0, null, ApprovalStatus::PENDING_VERIFICATION); $transactions[] = $this->createsTransaction->execute($payment->booking, $object); } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index e10e1781..f24fee8d 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -157,6 +157,6 @@ class Transaction extends AbstractModel implements Documentable */ public function scopeTransferred(Builder $query) { - return $query->whereIn('status', [ApprovalStatus::COMPLETED]); + return $query->whereIn('status', [ApprovalStatus::APPROVED]); } } diff --git a/resources/views/pages/dashboards/admin.blade.php b/resources/views/pages/dashboards/admin.blade.php index 7471027f..13e9e7ac 100644 --- a/resources/views/pages/dashboards/admin.blade.php +++ b/resources/views/pages/dashboards/admin.blade.php @@ -328,7 +328,7 @@
- +