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) { if (!$companyModule->wallets()->first()) { $object = new WalletObject($companyModule->id, 1, $this->generatesWalletCode->execute()); $this->createsWallet->execute($object, $companyModule); } /** @var Wallet $wallet */ $wallet = $companyModule->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->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; } }