diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php index 5a7e76b4..93caf8d1 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php @@ -128,7 +128,7 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic $billNumber = $this->generatesTransactionBillNumber->execute('SPLR-'); // $constant = $this->fetchesConstant->execute(['service_charge' => $supplier->id]); $constant = SegmentConstant::where('reference', SegmentConstants::SERVICE_CHARGE)->where('detail->id', $supplier->id)->first(); - $serviceCharge = $this->calculatesTransactionServiceCharge->execute($payment->original_amount, $rate, $constant ? $constant->detail->service_charge->amount : 0.75); + $serviceCharge = $this->calculatesTransactionServiceCharge->execute($payment->original_amount, $rate, $constant ? $constant->detail->service_charge->amount : 0); $object = new TransactionObject($billNumber, TransactionType::BILL, $supplier->id, 1, $supplier->banks()->where('default', true)->first()->id, PaymentMethodType::CASH, diff --git a/app/Classes/Modules/Transactions/ControllersLogic/UpdateRefundTransactionStatusLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/UpdateRefundTransactionStatusLogic.php new file mode 100644 index 00000000..d760b05d --- /dev/null +++ b/app/Classes/Modules/Transactions/ControllersLogic/UpdateRefundTransactionStatusLogic.php @@ -0,0 +1,85 @@ + 'Updated Transaction', + 'message' => 'You have successfully updated a transaction' + ]; + } + + /** @var FetchesCompany */ + private $fetchesCompany; + + /** @var FetchesTransaction */ + private $fetchesTransaction; + + /** @var UpdatesTransactionStatus */ + private $updatesTransactionStatus; + + /** @var DeletesDocument */ + private $deletesDocument; + + /** @var CreditWalletProcessor */ + private $creditWalletProcessor; + + /** + * CreatePaymentVerificationDocumentLogic constructor. + * @param FetchesCompany $fetchesCompany + * @param FetchesTransaction $fetchesTransaction + * @param UpdatesTransactionStatus $updatesTransactionStatus + * @param DeletesDocument $deletesDocument + * @param CreditWalletProcessor $creditWalletProcessor + */ + public function __construct(FetchesCompany $fetchesCompany, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, DeletesDocument $deletesDocument, CreditWalletProcessor $creditWalletProcessor) + { + $this->fetchesCompany = $fetchesCompany; + $this->fetchesTransaction = $fetchesTransaction; + $this->updatesTransactionStatus = $updatesTransactionStatus; + $this->deletesDocument = $deletesDocument; + $this->creditWalletProcessor = $creditWalletProcessor; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]); + + $transaction = $this->updatesTransactionStatus->execute($transaction, $request->input('status')); + + $company = $this->fetchesCompany->execute(['id' => $transaction->booking->company_id]); + + $reference = 'Refund for booking reference '.$transaction->booking->marking; + + if ($transaction->status == ApprovalStatus::APPROVED) { + // add to credit wallet + $this->creditWalletProcessor->execute($company, $transaction->type, $transaction->amount, $reference); + } + + return $this->response([]); + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php index 1e7b3ceb..3bd908d0 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php @@ -2,25 +2,26 @@ namespace App\Classes\Modules\Wallets\ControllersLogic; -use App\Classes\General\Abstracts\AbstractControllerLogic; -use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; -use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; -use App\Classes\Modules\Companies\Services\FetchesCompany; -use App\Classes\Modules\Wallets\Services\CreatesWallet; -use App\Classes\Modules\Wallets\Services\GeneratesWalletCode; -use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; -use App\Classes\Modules\Transactions\Services\CreatesTransaction; -use App\Classes\Modules\Wallets\Services\UpdatesWallet; - -use App\Classes\ValueObjects\Constants\TransactionType; -use App\Classes\ValueObjects\Constants\PaymentMethodType; -use App\Classes\ValueObjects\Constants\ApprovalStatus; -use App\Http\Resources\WalletResource; - use ErrorException; -use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\DB; +use App\Http\Resources\WalletResource; +use App\Classes\ValueObjects\Constants\ApprovalStatus; +use App\Classes\Modules\Wallets\Services\CreatesWallet; +use App\Classes\Modules\Wallets\Services\UpdatesWallet; +use App\Classes\ValueObjects\Constants\TransactionType; + +use App\Classes\ValueObjects\Constants\PaymentMethodType; +use App\Classes\General\Abstracts\AbstractControllerLogic; +use App\Classes\Modules\Companies\Services\FetchesCompany; +use App\Classes\Modules\Wallets\Services\GeneratesWalletCode; + +use App\Classes\Modules\Transactions\Services\CreatesTransaction; +use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; +use App\Classes\Modules\Wallets\Processors\CreditWalletProcessor; +use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; +use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; class CreditWalletLogic extends AbstractControllerLogic { @@ -53,6 +54,9 @@ class CreditWalletLogic extends AbstractControllerLogic /** @var UpdatesWallet */ private $updatesWallet; + /** @var CreditWalletProcessor */ + private $creditWalletProcessor; + /** * CreateWalletLogic constructor. * @param FetchesCompany $fetchesCompany @@ -61,6 +65,7 @@ class CreditWalletLogic extends AbstractControllerLogic * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber * @param CreatesTransaction $createsTransaction * @param UpdatesWallet $updatesWallet + * @param CreditWalletProcessor $creditWalletProcessor */ public function __construct( FetchesCompany $fetchesCompany, @@ -68,7 +73,8 @@ class CreditWalletLogic extends AbstractControllerLogic CreatesWallet $createsWallet, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, - UpdatesWallet $updatesWallet + UpdatesWallet $updatesWallet, + CreditWalletProcessor $creditWalletProcessor ) { $this->fetchesCompany = $fetchesCompany; @@ -77,6 +83,7 @@ class CreditWalletLogic extends AbstractControllerLogic $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->createsTransaction = $createsTransaction; $this->updatesWallet = $updatesWallet; + $this->creditWalletProcessor = $creditWalletProcessor; } /** @@ -94,23 +101,7 @@ class CreditWalletLogic extends AbstractControllerLogic $type = $request->input('transaction_type'); - if (!$company->wallets()->first()) { - $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute()); - $this->createsWallet->execute($object, $company); - } - - $wallet = $company->wallets()->first(); - - $billNumber = $this->generatesTransactionBillNumber->execute($type === 2 ? 'DEBIT-NOTE-' : 'CREDIT-NOTE-'); - - $transaction_object = new TransactionObject($billNumber, $type === 2 ? TransactionType::DEBIT_NOTE : TransactionType::CREDIT_NOTE, 1, $wallet->company->id, 1, PaymentMethodType::CASH, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], $reference); - $transaction = $this->createsTransaction->execute($wallet, $transaction_object); - - $updateWalletAmount = $type === 2 ? ($wallet->amount - $transaction->amount) : ($wallet->amount + $transaction->amount); - - $walletObject = new WalletObject($wallet->company->id, $wallet->currency_id, $wallet->code, $updateWalletAmount); - - $wallet = $this->updatesWallet->execute($wallet, $walletObject); + $wallet = $this->creditWalletProcessor->execute($company, $type, $amount, $reference); return $this->resourceResponse(new WalletResource($wallet)); } diff --git a/app/Classes/Modules/Wallets/Processors/CreditWalletProcessor.php b/app/Classes/Modules/Wallets/Processors/CreditWalletProcessor.php new file mode 100644 index 00000000..2b303938 --- /dev/null +++ b/app/Classes/Modules/Wallets/Processors/CreditWalletProcessor.php @@ -0,0 +1,89 @@ +generatesWalletCode = $generatesWalletCode; + $this->createsWallet = $createsWallet; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransaction = $createsTransaction; + $this->updatesWallet = $updatesWallet; + } + + + /** + * @param Company $company + * @param int $transactionType + * @param float $amount + * @param string $reference + * @return \Illuminate\Database\Eloquent\Model + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function execute(Company $company, int $transactionType, float $amount, string $reference) + { + if (!$company->wallets()->first()) { + $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute()); + $wallet = $this->createsWallet->execute($object, $company); + } + + $wallet = $company->wallets()->first(); + + $billNumber = $this->generatesTransactionBillNumber->execute($transactionType === 2 ? 'DEBIT-NOTE-' : 'CREDIT-NOTE-'); + + $transaction_object = new TransactionObject($billNumber, $transactionType === 2 ? TransactionType::DEBIT_NOTE : TransactionType::CREDIT_NOTE, 1, $wallet->owner->id, 1, PaymentMethodType::CASH, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], $reference); + $transaction = $this->createsTransaction->execute($wallet, $transaction_object); + + $updateWalletAmount = $transactionType === 2 ? ($wallet->amount - $transaction->amount) : ($wallet->amount + $transaction->amount); + + $walletObject = new WalletObject($wallet->owner->id, $wallet->currency_id, $wallet->code, $updateWalletAmount); + + $wallet = $this->updatesWallet->execute($wallet, $walletObject); + + return $wallet; + } +} diff --git a/app/Http/Controllers/Transactions/UpdateRefundTransactionStatusController.php b/app/Http/Controllers/Transactions/UpdateRefundTransactionStatusController.php new file mode 100644 index 00000000..919fadd1 --- /dev/null +++ b/app/Http/Controllers/Transactions/UpdateRefundTransactionStatusController.php @@ -0,0 +1,14 @@ +execute($request); + } +} \ No newline at end of file diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index f89ae6ef..2806d782 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -54,14 +54,6 @@ class Transaction extends AbstractModel implements Documentable, Transactionable return $this->BelongsTo(Booking::class, 'owner_id', 'id'); } - /** - * @return MorphMany - */ - public function transactions(): MorphMany - { - return $this->MorphMany(Transaction::class, 'owner'); - } - /** * @return BelongsTo */ diff --git a/routes/transaction.php b/routes/transaction.php index 2012a045..c3402b47 100644 --- a/routes/transaction.php +++ b/routes/transaction.php @@ -11,6 +11,7 @@ Route::group(['prefix' => 'transactions', 'namespace' => 'Transactions', 'as' => route::post('{id}/bill/verification', 'CreatePaymentProofDocumentController@verify')->name('bill.verification'); route::post('{id}/bill/pay', 'CreatePaymentProofDocumentController@pay')->name('bill.pay'); Route::put('/{id}/bill/{status}', 'UpdatePaymentTransactionStatusController@update')->where('status', 'pending|complete')->name('bill.status'); + Route::put('/{id}/refund/status/update', 'UpdateRefundTransactionStatusController@update')->name('refund.status.update'); route::delete('{id}/bill/delete', 'DeletePaymentProofDocumentController@delete')->name('bill.delete');