generatesWalletCode = $generatesWalletCode; $this->createsWallet = $createsWallet; $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->createsTransaction = $createsTransaction; $this->createsTransactionableTransaction = $createsTransactionableTransaction; $this->updatesWallet = $updatesWallet; $this->updatesWalletBalance = $updatesWalletBalance; } /** * @param CompanyModule $companyModule * @param int $transactionType * @param float $amount * @param string $reference * @return \Illuminate\Database\Eloquent\Model * @throws \App\Classes\Exceptions\MalformedRequestException */ public function execute(CompanyModule $companyModule, int $transactionType, float $amount, string $reference, ?int $currencyId = 1) { /** @var Wallet $wallet */ $wallet = $companyModule->wallets()->where('currency_id', $currencyId)->first(); if (!$wallet) { $object = new WalletObject($companyModule->id, $currencyId, $this->generatesWalletCode->execute()); $this->createsWallet->execute($object, $companyModule); $wallet = $companyModule->wallets()->where('currency_id', $currencyId)->first(); } $billNumberPrefix = null; $transaction_object_transaction_type = $transactionType; switch ($transactionType) { case TransactionType::PAYMENT: $billNumberPrefix = 'DEBIT-NOTE-'; $transaction_object_transaction_type = TransactionType::DEBIT_NOTE; break; case TransactionType::CIEF_COINS_CREDIT: $billNumberPrefix = 'COINS-CREDIT-'; // TODO-cief-coins: pls confirm the prefix break; case TransactionType::CIEF_COINS_DEBIT: $billNumberPrefix = 'COINS-DEBIT-'; // TODO-cief-coins: pls confirm the prefix break; default: $billNumberPrefix = 'CREDIT-NOTE-'; $transaction_object_transaction_type = TransactionType::CREDIT_NOTE; break; } $billNumber = $this->generatesTransactionBillNumber->execute($billNumberPrefix); $transaction_object = new TransactionObject($billNumber, $transaction_object_transaction_type, 1, $wallet->owner->id, 1, PaymentMethodType::CASH, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], $reference); $transaction = $this->createsTransactionableTransaction->execute($wallet, $transaction_object); $this->updatesWalletBalance->execute($wallet, $transaction->amount); // $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; } }