diff --git a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php index 4f40da24..6367c2cc 100644 --- a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php +++ b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php @@ -22,8 +22,10 @@ class CreatesBillplzBill public function execute(string $name, string $email, string $description, float $amount, string $billNumber, ?string $bankCode = null, ?bool $wallet = null) { try{ // config('billplz.maybank') - $response = Http::withBasicAuth(config('billplz.api_key').':', '')->post(config('billplz.base_url').'/api/v3/bills', [ - 'collection_id' => config('billplz.collection_id'), + // $response = Http::withBasicAuth(config('billplz.api_key').':', '')->post(config('billplz.base_url').'/api/v3/bills', [ + $response = Http::withoutVerifying(config('billplz.api_key').':', '')->post(config('billplz.base_url').'/api/v3/bills', [ + // 'collection_id' => config('billplz.collection_id'), + 'collection_id' => $wallet ? config('billplz.wallet_collection_id') : config('billplz.collection_id'), 'name' => $name, 'email' => $email, 'description' => $description, diff --git a/app/Classes/Modules/Transactions/ControllersLogic/multipleInvoicesWithOnePaymentLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/multipleInvoicesWithOnePaymentLogic.php index 87b8531a..875e228f 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/multipleInvoicesWithOnePaymentLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/multipleInvoicesWithOnePaymentLogic.php @@ -49,6 +49,8 @@ use App\Classes\Modules\Wallets\Services\GeneratesWalletCode; use App\Classes\Modules\Wallets\Services\CreatesWallet; use APP\models\PackingList; +use App\Classes\Exceptions\MalformedRequestException; + class multipleInvoicesWithOnePaymentLogic extends AbstractControllerLogic diff --git a/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php new file mode 100644 index 00000000..9a613b43 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php @@ -0,0 +1,76 @@ + 'Created Company Wallet', + 'message' => 'You have successfully created a company wallet' + ]; + } + + /** @var CreatesWallet */ + private $createsWallet; + + /** @var GeneratesWalletCode */ + private $generatesWalletCode; + + /** @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, FetchesCompany $fetchesCompany) + { + $this->fetchesCompany = $fetchesCompany; + $this->createsWallet = $createsWallet; + $this->generatesWalletCode = $generatesWalletCode; + $this->canCreateCompanyWallet = $canCreateCompanyWallet; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + $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); + + return $this->resourceResponse(new WalletResource($wallet)); + } +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletTransactionLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletTransactionLogic.php new file mode 100644 index 00000000..6fe72a52 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletTransactionLogic.php @@ -0,0 +1,132 @@ + 'Created Wallet Transaction', + 'message' => 'You have successfully created a wallet transaction' + ]; + } + + /** @var CreatesWalletTransaction */ + private $createsWalletTransaction; + + /** @var GeneratesWalletTransactionBillNo */ + private $generatesWalletTransactionBillNo; + + /** @var CanCreateWalletTransaction */ + private $canCreateWalletTransaction; + + private $fetchesCurrency; + + private $rateCalculatesCurrency; + + private $fetchesWallet; + + /** + * CreateWalletLogic constructor. + * @param CreatesWalletTransaction $createsWalletTransaction + * @param GeneratesWalletTransactionBillNo $generatesWalletTransactionBillNo + * @param CanCreateWalletTransaction $canCreateWalletTransaction + * @param FetchesCurrency $fetchesCurrency + * @param RateCalculatesCurrency $rateCalculatesCurrency + * @param FetchesWallet $fetchesWallet + */ + public function __construct( + CreatesWalletTransaction $createsWalletTransaction, GeneratesWalletTransactionBillNo $generatesWalletTransactionBillNo, CanCreateWalletTransaction $canCreateWalletTransaction, + FetchesCurrency $fetchesCurrency,RateCalculatesCurrency $rateCalculatesCurrency, FetchesWallet $fetchesWallet + ) + { + $this->createsWalletTransaction = $createsWalletTransaction; + $this->generatesWalletTransactionBillNo = $generatesWalletTransactionBillNo; + $this->canCreateWalletTransaction = $canCreateWalletTransaction; + $this->fetchesCurrency = $fetchesCurrency; + $this->rateCalculatesCurrency = $rateCalculatesCurrency; + $this->fetchesWallet = $fetchesWallet; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + try { + + DB::beginTransaction(); + + $wallet = $this->fetchesWallet->execute(['id' => $request->route('id')]); + $conversion_currency = $this->fetchesCurrency->execute(['id' => $request->input('currency_id')]); + $convertable_currency = $this->fetchesCurrency->execute(['id' => $wallet->currency_id]);//MYR + + $convert_amount = $this->rateCalculatesCurrency->execute($conversion_currency, $convertable_currency, $request->input('amount')); + $convert_rate = $this->rateCalculatesCurrency->execute_rate($conversion_currency, $convertable_currency); + + + $object = new WalletTransactionObject( + $wallet->id, $this->generatesWalletTransactionBillNo->execute(),$request->input('trans_type'), + number_format( (float) $convert_amount, 5, '.', ''), $wallet->currency_id , number_format( (float) $request->input('amount'), 5, '.', ''), + $request->input('currency_id'),number_format( (float) $convert_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/ControllersLogic/CreditWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php new file mode 100644 index 00000000..8ed8aa08 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php @@ -0,0 +1,101 @@ + 'Credit into Company Wallet', + 'message' => 'You have successfully credit company wallet' + ]; + } + + + /** @var FetchesCompany */ + private $fetchesCompany; + + /** @var GeneratesWalletCode */ + private $generatesWalletCode; + + /** @var CreatesWallet */ + private $createsWallet; + + /** @var GeneratesTransactionBillNumber */ + private $generatesTransactionBillNumber; + + /** @var CreatesTransaction */ + private $createsTransaction; + + /** @var UpdatesWallet */ + private $updatesWallet; + + /** @var CreditWalletProcessor */ + private $creditWalletProcessor; + + /** + * CreateWalletLogic constructor. + * @param FetchesCompany $fetchesCompany + * @param GeneratesWalletCode $generatesWalletCode + * @param CreatesWallet $createsWallet + * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber + * @param CreatesTransaction $createsTransaction + * @param UpdatesWallet $updatesWallet + * @param CreditWalletProcessor $creditWalletProcessor + */ + public function __construct( + FetchesCompany $fetchesCompany, + GeneratesWalletCode $generatesWalletCode, + CreatesWallet $createsWallet, + GeneratesTransactionBillNumber $generatesTransactionBillNumber, + CreatesTransaction $createsTransaction, + UpdatesWallet $updatesWallet, + CreditWalletProcessor $creditWalletProcessor + ) + { + $this->fetchesCompany = $fetchesCompany; + $this->generatesWalletCode = $generatesWalletCode; + $this->createsWallet = $createsWallet; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransaction = $createsTransaction; + $this->updatesWallet = $updatesWallet; + $this->creditWalletProcessor = $creditWalletProcessor; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $amount = floatval(str_replace(',', '', $request->input('amount'))); + + $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); + + $reference = $request->input('reference'); + + $type = $request->input('transaction_type'); + + $wallet = $this->creditWalletProcessor->execute($company, $type, $amount, $reference); + + return $this->resourceResponse(new WalletResource($wallet)); + } +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php new file mode 100644 index 00000000..682065c0 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php @@ -0,0 +1,117 @@ + 'Debit into Company Wallet', + 'message' => 'You have successfully debit company wallet' + ]; + } + + + /** @var FetchesCompany */ + private $fetchesCompany; + + /** @var GeneratesTransactionBillNumber */ + private $generatesTransactionBillNumber; + + /** @var CreatesTransaction */ + private $createsTransaction; + + /** @var UpdatesWallet */ + private $updatesWallet; + + /** + * CreateWalletLogic constructor. + * @param FetchesCompany $fetchesCompany + * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber + * @param CreatesTransaction $createsTransaction + * @param UpdatesWallet $updatesWallet + */ + public function __construct( + FetchesCompany $fetchesCompany, + GeneratesTransactionBillNumber $generatesTransactionBillNumber, + CreatesTransaction $createsTransaction, + UpdatesWallet $updatesWallet + ) + { + $this->fetchesCompany = $fetchesCompany; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransaction = $createsTransaction; + $this->updatesWallet = $updatesWallet; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $amount = floatval(str_replace(',', '', $request->input('amount'))); + $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); + $reference = $request->input('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('DEBIT-NOTE-'); + + $transaction_object = new TransactionObject( + $billNumber, + TransactionType::DEBIT_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 = $wallet->amount - $transaction->amount; + + $walletOject = new WalletObject($wallet->company->id, $wallet->currency_id, $wallet->code, $updateWalletAmount); + + $wallet = $this->updatesWallet->execute($wallet, $walletOject); + + return $this->resourceResponse(new WalletResource($wallet)); + } +} 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..c60cbf54 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php @@ -0,0 +1,113 @@ + 'TopUp into Company Wallet', + 'message' => 'You have successfully created a topup request for company\'s wallet' + ]; + } + + + /** @var FetchesCompany */ + private $fetchesCompany; + + /** @var GeneratesWalletCode */ + private $generatesWalletCode; + + /** @var CreatesWallet */ + private $createsWallet; + + /** @var GeneratesTransactionBillNumber */ + private $generatesTransactionBillNumber; + + /** @var CreatesBillplzBill */ + private $createsBillplzBill; + + /** @var CreatesTransaction */ + private $createsTransaction; + + + /** + * TopUpWalletLogic constructor. + * @param FetchesCompany $fetchesCompany + * @param GeneratesWalletCode $generatesWalletCode + * @param CreatesWallet $createsWallet + * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber + * @param CreatesBillplzBill $createsBillplzBill + * @param CreatesTransaction $createsTransaction + */ + public function __construct(FetchesCompany $fetchesCompany, GeneratesWalletCode $generatesWalletCode, CreatesWallet $createsWallet, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesBillplzBill $createsBillplzBill, CreatesTransaction $createsTransaction) + { + $this->fetchesCompany = $fetchesCompany; + $this->generatesWalletCode = $generatesWalletCode; + $this->createsWallet = $createsWallet; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsBillplzBill = $createsBillplzBill; + $this->createsTransaction = $createsTransaction; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $amount = floatval(str_replace(',', '', $request->input('amount'))); + $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); + + /** @var Wallet $wallet */ + $wallet = $company->wallets()->first(); + + if (!$wallet) { + $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute()); + /** @var Wallet $wallet */ + $wallet = $this->createsWallet->execute($object, $company); + } + + $user = $company->employees()->first(); + $billNumber = $this->generatesTransactionBillNumber->execute('TOPUP-'); + + if($amount < 0) { + throw new MalformedRequestException('Top up credit value must be greater than zero.'); + } + + $billPlzBill = $this->createsBillplzBill->execute($company->name, $user->email, 'This payment is credit topup for company ref. ' . $company->reference, $amount, $billNumber, $request->input('bank_code'), true); + + $transaction_object = new TransactionObject($billNumber, TransactionType::TOP_UP, 1, $company->id, 1, PaymentMethodType::PAYMENT_GATEWAY, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, [], $billPlzBill->id); + + $transaction = $this->createsTransaction->execute($wallet, $transaction_object); + + return $this->resourceResponse(new WalletTransactionResource($transaction)); + } +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php new file mode 100644 index 00000000..b051d3e3 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/UpdateStatusWalletLogic.php @@ -0,0 +1,69 @@ + '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 + { + $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/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php new file mode 100644 index 00000000..b1491ab7 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/WithdrawWalletLogic.php @@ -0,0 +1,72 @@ + 'Withdraw from Company Wallet', + 'message' => 'You have successfully withdraw company wallet' + ]; + } + + /** @var FetchesWallet */ + private $fetchesWallet; + + /** @var CanWithdrawWallet */ + private $canWithdrawWallet; + + /** @var CreateWalletTransactionProcessor */ + private $createWalletTransactionProcessor; + + /** + * CreateWalletLogic constructor. + * @param CreatesWallet $createsWallet + * @param GeneratesWalletCode $generatesWalletCode + * @param CanCreateCompanyWallet $canCreateCompanyWallet + */ + public function __construct(CanWithdrawWallet $canWithdrawWallet, FetchesWallet $fetchesWallet, CreateWalletTransactionProcessor $createWalletTransactionProcessor) + { + $this->canWithdrawWallet = $canWithdrawWallet; + $this->fetchesWallet = $fetchesWallet; + $this->createWalletTransactionProcessor = $createWalletTransactionProcessor; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + $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/DataTransferObjects/WalletObject.php b/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php new file mode 100644 index 00000000..4bfae2f4 --- /dev/null +++ b/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php @@ -0,0 +1,65 @@ +company_id = $company_id; + $this->currency_id = $currency; + $this->code = $code; + $this->amount = $amount; + } + + /** + * @return int + */ + public function getCompanyId(): int + { + return $this->company_id; + } + + /** + * @return int + */ + public function getCurrency(): int + { + return $this->currency_id; + } + + /** + * @return int + */ + public function getCode(): int + { + return $this->code; + } + + public function getAmount(): float + { + return $this->amount; + } + + +} diff --git a/app/Classes/Modules/Wallets/DataTransferObjects/WalletTransactionObject.php b/app/Classes/Modules/Wallets/DataTransferObjects/WalletTransactionObject.php new file mode 100644 index 00000000..234c8f57 --- /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/Processors/CreditWalletProcessor.php b/app/Classes/Modules/Wallets/Processors/CreditWalletProcessor.php new file mode 100644 index 00000000..65f3b9b6 --- /dev/null +++ b/app/Classes/Modules/Wallets/Processors/CreditWalletProcessor.php @@ -0,0 +1,90 @@ +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()); + $this->createsWallet->execute($object, $company); + } + + /** @var Wallet $wallet */ + $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/Classes/Modules/Wallets/Services/ChecksIfWalletCodeExists.php b/app/Classes/Modules/Wallets/Services/ChecksIfWalletCodeExists.php new file mode 100644 index 00000000..4d00540a --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/ChecksIfWalletCodeExists.php @@ -0,0 +1,28 @@ +repository = $repository; + } + + public function execute(int $code): bool { + return $this->repository->where('code', $code)->exists(); + } + +} \ 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/CreatesWallet.php b/app/Classes/Modules/Wallets/Services/CreatesWallet.php new file mode 100644 index 00000000..2a927e6d --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/CreatesWallet.php @@ -0,0 +1,26 @@ +company_id = $object->getCompanyId(); + $model->code = $object->getCode(); + $model->currency_id = $object->getCurrency(); + + return $this->handler($company->wallets(), $model); + } +} diff --git a/app/Classes/Modules/Wallets/Services/CreatesWalletTransaction.php b/app/Classes/Modules/Wallets/Services/CreatesWalletTransaction.php new file mode 100644 index 00000000..0b40879c --- /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/FetchesWallet.php b/app/Classes/Modules/Wallets/Services/FetchesWallet.php new file mode 100644 index 00000000..c3542bb7 --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/FetchesWallet.php @@ -0,0 +1,34 @@ +repository = $repository; + } + + + /** + * @return Builder + */ + public function getRepository(): Builder + { + return $this->repository->newQuery(); + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Services/GeneratesWalletCode.php b/app/Classes/Modules/Wallets/Services/GeneratesWalletCode.php new file mode 100644 index 00000000..af2bda7e --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/GeneratesWalletCode.php @@ -0,0 +1,32 @@ +walletCodeExists = $walletCodeExists; + } + + + /** + * @return int + */ + public function execute(): int { + + $code = mt_rand(100000001, 999999999); + return !$this->walletCodeExists->execute($code) ? $code : self::execute(); + + } + +} \ 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/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..8b268fab --- /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/Services/UpdatesWalletBalance.php b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php new file mode 100644 index 00000000..a724cf24 --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/UpdatesWalletBalance.php @@ -0,0 +1,25 @@ +amount = $model->amount + $amount; + return $this->handler($model); + + } +} diff --git a/app/Classes/Modules/Wallets/Standards/Rules/CanCreateCompanyWallet.php b/app/Classes/Modules/Wallets/Standards/Rules/CanCreateCompanyWallet.php new file mode 100644 index 00000000..0f1dd065 --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Rules/CanCreateCompanyWallet.php @@ -0,0 +1,55 @@ +companyWalletValidation = $companyWalletValidation; + } + + + /** + * @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->companyWalletValidation->validate($object); + } + + + /** + * @param WalletObject $object + * @return bool + */ + protected function criteria($object): bool + { + return true; + } + +} \ 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..8beafb37 --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Rules/CanCreateWalletTransaction.php @@ -0,0 +1,51 @@ +walletTransactionValidation = $walletTransactionValidation; + } + + + /** + * @return bool + */ + protected function authorized(): bool + { + // TODO Set Authorization rules + return true; + } + + /** + * @param WalletTransactionObject $object + * @return bool + * @throws \App\Classes\Exceptions\RequestValidationException + */ + protected function validators($object): bool + { + return $this->walletTransactionValidation->validate($object); + } + + /** + * @param WalletTransactionObject $object + * @return bool + */ + protected function criteria($object): bool + { + return true; + } + +} \ No newline at end of file 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/CompanyWalletValidation.php b/app/Classes/Modules/Wallets/Standards/Validators/CompanyWalletValidation.php new file mode 100644 index 00000000..c924574d --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Validators/CompanyWalletValidation.php @@ -0,0 +1,40 @@ + $object->getCompanyId(), + 'currency_id' => $object->getCurrency(), + ]; + } + + /** + * @return array + */ + protected function rules(): array + { + return [ + 'company_id' => 'required', + 'currency_id' => 'required', + ]; + } + + /** + * @return array + */ + protected function messages(): array + { + return []; + } +} \ No newline at end of file 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 @@ + $object->getWalletId(), + 'currency_id' => $object->getCurrency(), + 'amount' => $object->getAmount(), + 'trans_type'=>$object->getTransType() + ]; + } + + /** + * @return array + */ + protected function rules(): array + { + return [ + 'wallet_id' => 'required', + 'currency_id' => 'required', + 'amount' => 'required', + 'trans_type'=>'required' + ]; + } + + /** + * @return array + */ + protected function messages(): array + { + return []; + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Standards/Validators/WithdrawWalletValidation.php b/app/Classes/Modules/Wallets/Standards/Validators/WithdrawWalletValidation.php new file mode 100644 index 00000000..78660228 --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Validators/WithdrawWalletValidation.php @@ -0,0 +1,30 @@ +logic($transactionsArray); + } + + public function getallivoiceid1( $transactionsArray) + { + + + // dd(json_decode($transactionsArray)); + $transactions = Transaction::whereIn('id', json_decode($transactionsArray))->where('type', TransactionType::SHIPPING_INVOICE)->get(); + $total = $transactions->sum('amount'); + $packingList =$transactions[1]->owner; + $order = $packingList->owner; + $companyModule = $order->companyModule; + $company = $companyModule->company; + $wallet = $company->wallets()->first(); + + if (!$wallet) { + $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute()); + /** @var Wallet $wallet */ + $wallet = $this->createsWallet->execute($object, $company); + } + + $billNumber = (App()->make(GeneratesTransactionBillNumber::class))->execute('TOPUP-'); + + if($total < 0) { + throw new MalformedRequestException('Top up credit value must be greater than zero.'); + } + $billPlzBill = (App()->make(CreatesBillplzBill::class))->execute($company->name, 'example@gmail.com', 'This payment is credit topup for company ref. ' . $company->reference, $total, $billNumber, 'bank code', true); + + $transaction_object = new TransactionObject($billNumber, TransactionType::TOP_UP, 1, $company->id, 1, PaymentMethodType::PAYMENT_GATEWAY, $total, $total, 1, 1, 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, [], 'test'); + + // $transaction = $this->createsTransaction->execute($wallet, $transaction_object); + + $transaction = (App()->make(CreatesTransaction::class))->execute($wallet, $transaction_object); + $result= (App()->make(UpdatesWalletBalance::class))->execute($wallet, $total); + + foreach ($transactions as $key => $value) { + multipleInvoicesWithOnePayment::create([ 'topUp_request_id' => $transaction->id, 'transaction_id' => $value->id ]); + + } + dd($transaction->id); + } + + +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 6305b971..87791e22 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -102,4 +102,9 @@ class Company extends AbstractModel implements Documentable, Contactable return $this->hasManyDeep(Package::class, [CompanyModule::class, Order::class, PackingList::class], ['company_id', 'company_module_id', ['owner_type', 'owner_id'], 'packing_list_id'], ['id', 'id', null, 'id']); } + public function wallets(): morphMany + { + return $this->morphMany(Wallet::class, 'owner'); + } + } diff --git a/app/Models/Wallet.php b/app/Models/Wallet.php new file mode 100644 index 00000000..8c3114fd --- /dev/null +++ b/app/Models/Wallet.php @@ -0,0 +1,32 @@ +morphTo(); + } + + /** + * @return morphMany + */ + public function transactions(): morphMany + { + return $this->morphMany(Transaction::class, 'owner'); + } +} diff --git a/database/migrations/2022_12_16_193724_create_multiple_invoice_one_payments_table.php b/database/migrations/2022_12_16_193724_create_multiple_invoice_one_payments_table.php new file mode 100644 index 00000000..cb5eac12 --- /dev/null +++ b/database/migrations/2022_12_16_193724_create_multiple_invoice_one_payments_table.php @@ -0,0 +1,35 @@ +id(); + $table->foreignId('topUp_request_id')->unsigned(); + $table->foreignId('transaction_id')->unsigned(); + $table->timestamps(); + $table->foreign('topUp_request_id')->references('id')->on('transactions'); + $table->foreign('transaction_id')->references('id')->on('transactions'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('multiple_invoice_one_payments'); + } +} diff --git a/database/migrations/2022_12_27_212614_create_companies_wallet_table.php b/database/migrations/2022_12_27_212614_create_companies_wallet_table.php new file mode 100644 index 00000000..925e8882 --- /dev/null +++ b/database/migrations/2022_12_27_212614_create_companies_wallet_table.php @@ -0,0 +1,41 @@ +id(); + + $table->foreignId('company_id')->unsigned(); + $table->string('code'); + $table->foreignId('currency_id')->unsigned(); + $table->decimal('amount', 20, 5)->default(0.00); + $table->softDeletes(); + $table->timestamps(); + + $table->foreign('company_id')->references('id')->on('companies'); + $table->foreign('currency_id')->references('id')->on('currencies'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('wallets'); + } +} + diff --git a/database/migrations/2022_12_27_212615_alter_wallet_company_id.php b/database/migrations/2022_12_27_212615_alter_wallet_company_id.php new file mode 100644 index 00000000..c1eba98a --- /dev/null +++ b/database/migrations/2022_12_27_212615_alter_wallet_company_id.php @@ -0,0 +1,44 @@ +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() + { + // + } +}