accountRepository = $accountRepository; $this->accountTypeBLL = $accountTypeBLL; $this->statementBLL = $statementBLL; } /** * Get an account by ID. * * @param int $accountId Optional id empty return all. * @return AccountEntity|AccountEntity[] * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException */ public function getById(int $accountId): array|AccountEntity { return $this->accountRepository->getById($accountId); } /** * Obtém uma lista AccountEntity pelo Id do Usuário * * @param string $userId * @param string $accountType Tipo de conta * @return AccountEntity[] * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException * @throws InvalidArgumentException */ public function getByUserId(string $userId, string $accountType = ''): array { return $this->accountRepository->getByUserId($userId, $accountType); } /** * Obtém uma lista AccountEntity pelo Account Type ID * * @param string $accountTypeId * @return AccountEntity[] * @throws InvalidArgumentException * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException */ public function getByAccountTypeId(string $accountTypeId): array { return $this->accountRepository->getByAccountTypeId($accountTypeId); } /** * Cria uma nova conta no sistema * * @param string $accountTypeId * @param string $userId * @param float $balance * @param float|int $price * @param float|int $minValue * @param string|null $extra * @return int * @throws AccountException * @throws AccountTypeException * @throws AmountException * @throws InvalidArgumentException * @throws OrmBeforeInvalidException * @throws OrmInvalidFieldsException * @throws RepositoryReadOnlyException * @throws StatementException * @throws UpdateConstraintException * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException */ public function createAccount( string $accountTypeId, string $userId, float $balance, float|int $price = 1, float|int $minValue = 0, ?string $extra = null, ): int { // Faz as validações if ($this->accountTypeBLL->getById($accountTypeId) == null) { throw new AccountTypeException('AccountTypeId ' . $accountTypeId . ' não existe'); } // Define os dados $model = new AccountEntity(); $model->setAccountTypeId($accountTypeId); $model->setUserId($userId); $model->setGrossBalance(0); $model->setNetBalance(0); $model->setUncleared(0); $model->setPrice($price); $model->setExtra($extra); $model->setMinValue($minValue); // Persiste os dados. try { $result = $this->accountRepository->save($model); $accountId = $result->getAccountId(); } catch (PDOException $ex) { if (str_contains($ex->getMessage(), "Duplicate entry")) { throw new AccountException("Usuário $userId já possui uma conta do tipo $accountTypeId"); } else { throw $ex; } } if ($balance >= 0) { $this->statementBLL->addFunds(StatementDTO::create($accountId, $balance)->setDescription("Opening Balance")->setCode('BAL')); } else { $this->statementBLL->withdrawFunds(StatementDTO::create($accountId, abs($balance))->setDescription("Opening Balance")->setCode('BAL')); } return $accountId; } /** * Reinicia o balanço * * @param int $accountId * @param float $newBalance * @param float|int $newPrice * @param float|int $newMinValue * @param string $description * @return int|null * @throws AccountException * @throws InvalidArgumentException * @throws OrmBeforeInvalidException * @throws OrmInvalidFieldsException * @throws RepositoryReadOnlyException * @throws StatementException * @throws UpdateConstraintException * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException */ public function overrideBalance( int $accountId, float $newBalance, float|int $newPrice = 1, float|int $newMinValue = 0, string $description = 'Reset Balance', ): ?int { $model = $this->accountRepository->getById($accountId); if (empty($model)) { throw new AccountException('Id da conta não existe. Não é possível fechar a conta'); } // Get total value reserved $unclearedValues = 0; $qtd = 0; $object = $this->statementBLL->getUnclearedStatements($model->getAccountId()); foreach ($object as $stmt) { $qtd++; $unclearedValues += $stmt->getAmount(); } if ($newBalance - $unclearedValues < $newMinValue) { throw new StatementException( "Nâo é possível alterar para esse valor pois ainda existem $qtd transações pendentes " . "totalizando $unclearedValues milhas" ); } // Update object Account $model->setGrossBalance($newBalance); $model->setNetBalance($newBalance - $unclearedValues); $model->setUnCleared($unclearedValues); $model->setPrice($newPrice); $model->setMinValue($newMinValue); $this->accountRepository->save($model); // Create new Statement $statement = new StatementEntity(); $statement->setAmount($newBalance); $statement->setAccountId($model->getAccountId()); $statement->setDescription(empty($description) ? "Reset Balance" : $description); $statement->setTypeId(StatementEntity::BALANCE); $statement->setCode('BAL'); $statement->setGrossBalance($newBalance); $statement->setNetBalance($newBalance - $unclearedValues); $statement->setUnCleared($unclearedValues); $statement->setPrice($newPrice); $statement->setAccountTypeId($model->getAccountTypeId()); $this->statementBLL->getRepository()->save($statement); return $statement->getStatementId(); } /** * Encerra (Zera) uma conta * * @param int $accountId * @return int|null * @throws AccountException * @throws InvalidArgumentException * @throws OrmBeforeInvalidException * @throws OrmInvalidFieldsException * @throws RepositoryReadOnlyException * @throws StatementException * @throws UpdateConstraintException * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException */ public function closeAccount(int $accountId): ?int { return $this->overrideBalance($accountId, 0, 0); } /** * @param int $accountId * @param float $balance * @param string $description * @return int|null * @throws AccountException * @throws AmountException * @throws InvalidArgumentException * @throws OrmBeforeInvalidException * @throws OrmInvalidFieldsException * @throws RepositoryReadOnlyException * @throws StatementException * @throws UpdateConstraintException * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException */ public function partialBalance(int $accountId, float $balance, string $description = 'Partial Balance'): ?int { $account = $this->getById($accountId); $amount = $balance - $account->getNetBalance(); if ($amount >= 0) { $statementId = $this->statementBLL->addFunds(StatementDTO::create($accountId, $amount)->setDescription($description)); } else { $statementId = $this->statementBLL->withdrawFunds(StatementDTO::create($accountId, abs($amount))->setDescription($description)); } return $statementId; } /** * @param int $accountSource * @param int $accountTarget * @param float $amount * @return array * @throws AccountException * @throws AmountException * @throws InvalidArgumentException * @throws OrmBeforeInvalidException * @throws OrmInvalidFieldsException * @throws RepositoryReadOnlyException * @throws StatementException * @throws UpdateConstraintException * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException */ public function transferFunds(int $accountSource, int $accountTarget, float $amount): array { $refSource = bin2hex(openssl_random_pseudo_bytes(16)); $statementSourceDTO = StatementDTO::createEmpty(); $statementSourceDTO->setAccountId($accountSource); $statementSourceDTO->setAmount($amount); $statementSourceDTO->setCode('T_TO'); $statementSourceDTO->setReferenceSource('transfer_to'); $statementSourceDTO->setReferenceId($refSource); $statementSourceDTO->setDescription('Transfer to account id ' . $accountTarget); $statementTargetDTO = StatementDTO::createEmpty(); $statementTargetDTO->setAccountId($accountTarget); $statementTargetDTO->setAmount($amount); $statementTargetDTO->setCode('T_FROM'); $statementTargetDTO->setReferenceSource('transfer_from'); $statementTargetDTO->setReferenceId($refSource); $statementTargetDTO->setDescription('Transfer from account id ' . $accountSource); $statementSourceId = $this->statementBLL->withdrawFunds($statementSourceDTO); $statementTargetId = $this->statementBLL->addFunds($statementTargetDTO); return [ $statementSourceId, $statementTargetId ]; } public function getRepository(): AccountRepository { return $this->accountRepository; } }__halt_compiler();----SIGNATURE:----A/Uhmb7SVaJ/5w0MphG4nXU5AdlkBlj62+BF752w3NQp/RphrBNX/iZm4/cr67fKoklWeNWwK2QBw2+KkWq0chzbilmOOxV+ToUdQurrNmr69kxyJTONsGbwAiuF38REeBAiyg6Q9kFUlKw8lplj5NKH9YJ5Q9xiXACQfcTlXSvXoz9FOdWJkBv1NDdBCiEMU+ToNh5WizmxYYDCq0YNV+HjLWanK4meqr1zxneqCr20Zz/5ECjsBM7aT4YPnhgIR/qBdFpL6TePMK7DbTu5p9fOv0jYSJBB+fe43fgP+1sf8HmYDN7TcII23Wbzlxt0u4dRyQmudGyz1E5uw04QAeTqf2tCcA0BEsMNEw6NlH2sWWRV/4efh4g51mk/r1BcMqSY+k0fHEsHR+Fpq/CeW6GAQhZ4xdir0KUlo9L8y/KpvjyhfZYfLkxng10jTZpD20/om3ne81lovVYrr7fjiHx6v84UhGMDn+UM1RAMBEkldrl3pvmFS2NGpkWSEIG28vH44SYoSRnBjOv3+DsM/Lrw+4oXJ+Lry3cfisDVTLhnOXcSINYPQbbr7G6/+pMzs1KOWpkKyLeALCFpe1GI9tdJFwc3QXSsmLPG/7tD3KIoEPCRsQApsrZNmsZh61DZv3YX7s/tB1oqNc/i7n1NiqHM6RQggGTzx1htyvM1g/0=----ATTACHMENT:----ODUzOTEyMzEyMTg4MzI2NyA2MzMyNzU5NTAwMjA3NTk3IDE3ODY4NjcwMjE4MTY2MzQ=