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:----RyMO8zEkP7LRv4iXyyonuyK4aMq1ovAmnvDRC2jA2bkRoFfI5/XHRfdlrtq/42y2ipZdp9P5BEorq/LGV4fOiX31j2MPOMv2ShZNsmTiF+xRq9ANn3BWgQYqBG8K2U2lIGME06HYbD/C1KgYtH4AiyJIvsnUobPEmw5U0644lc01GllEdTEGSYuHvkJS2AS6sD9LZjVAidki3WN+Zxphfnosl+YdiXIcpz+o0exlIdEfPqY9OPrC7V0tOfPJ4VO+LU1V32hzbuxg/7hLuCHkStLVkTfc3jGi6D9lCavPQNfDBFi0nz5uoZAX60mEc6X8Ig3tfxzPV/gyvoobYRfMv7jb4YbSjPoKbvyIVKkii3jAF4MghmDw+t+7MtBh45XPzLSJ6+dVZMg7kujnF2953hgSBBNTwK4v1EnvW+NrgGn2FX0bNLrjQ13+/wzyLIg+vGMSkDR4Dsg5RniChvCbqET6IZ9xxySu0pmm53QijP2fo6p7Gilr7Hn1BCfHHFlAG+sBFTwkETTU3Jxrb3VyNLX6ljWRwLLK0s2CjcCKPxi8jvBNqpN3AaaeCddYH6MhOLplLZ5O/xh1yz2nq4PbuC6N6LEus6W+DhbrarFFlGDm+T2Tm3ok8KSZu+RWKj/JYx5J0cR+U9K5/Ep+IKr5vVpHSyUnUhOoVMQmeSx0coE=----ATTACHMENT:----MTE2NzgyNDk2MTEyNDU2MyA4NTQ0MjI3MDY3MjY3NDM4IDUwMDA2MDY1ODU5NjA1Mw==