Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ jobs:
with:
folder: php
project: ${{ github.event.repository.name }}
secrets: inherit
secrets:
DOC_TOKEN: ${{ secrets.DOC_TOKEN }}

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ $statementBLL->withdrawFunds($statement);

// Add 50 USD hold to the account
$statement = new StatementDTO($accountId, 50);
$statementId = $statementBLL->reserveFundsForDeposit($statement);
$reserve = $statementBLL->reserveFundsForDeposit($statement);

// Accept the hold
$statementBLL->acceptFundsById($statementId);
$statementBLL->acceptFundsById($reserve->getStatementId());
```

## Installation
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ext-pdo": "*",
"ext-openssl": "*",
"php": ">=8.1 <8.4",
"byjg/micro-orm": "^5.0"
"byjg/micro-orm": "5.0.x-dev"
},
"autoload": {
"psr-4": {
Expand Down
Binary file removed composer.phar
Binary file not shown.
2 changes: 2 additions & 0 deletions db/migrations/down/00002-delete-laststatementid.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table account
drop column laststatementid;
5 changes: 5 additions & 0 deletions db/migrations/down/00003-delete-constraint.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE statement
DROP CONSTRAINT statement_chk_value_nonnegative;

ALTER TABLE account
DROP CONSTRAINT account_chk_value_nonnegative;
8 changes: 8 additions & 0 deletions db/migrations/up/00003-add-laststatementid.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
alter table account
add last_uuid binary(16) null;

alter table statement
add uuid binary(16) null,
algorithm = instant;

create unique index idx_statement_uuid on statement (uuid);
8 changes: 8 additions & 0 deletions db/migrations/up/00004-add-constraint.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER TABLE statement
ADD CONSTRAINT statement_chk_value_nonnegative
CHECK (netbalance >= 0);

ALTER TABLE account
ADD CONSTRAINT account_chk_value_nonnegative
CHECK (netbalance >= 0);

47 changes: 17 additions & 30 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,21 @@ To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
colors="true"
testdox="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
stopOnFailure="false">


<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
<ini name="error_reporting" value="E_ALL" />
</php>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" colors="true" testdox="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" convertDeprecationsToExceptions="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<php>
<ini name="display_errors" value="On"/>
<ini name="display_startup_errors" value="On"/>
<ini name="error_reporting" value="E_ALL"/>
</php>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
112 changes: 57 additions & 55 deletions src/Bll/AccountBLL.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,50 +183,60 @@ public function overrideBalance(
string $description = "Reset Balance"
): ?int
{

$model = $this->accountRepository->getById($accountId);
$account = $this->accountRepository->getById($accountId);

if (empty($model)) {
throw new AccountException('Id da conta não existe. Não é possível fechar a conta');
if (empty($account)) {
throw new AccountException('Account Id doesnt exists');
}

// Get total value reserved
$unclearedValues = 0;
$qtd = 0;
$object = $this->statementBLL->getUnclearedStatements($model->getAccountId());
foreach ($object as $stmt) {
$qtd++;
$unclearedValues += $stmt->getAmount();
}
$dto = StatementDTO::createEmpty();
$dto->setUuid($dto->calculateUuid($this->accountRepository->getDbDriver()));;

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"
);
}
$this->accountRepository->getDbDriver()->beginTransaction();
try {
// Get total value reserved
$unclearedValues = 0;
$qtd = 0;
$object = $this->statementBLL->getUnclearedStatements($account->getAccountId());
foreach ($object as $stmt) {
$qtd++;
$unclearedValues += $stmt->getAmount();
}

// 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);
if ($newBalance - $unclearedValues < $newMinValue) {
throw new StatementException(
"Can't override balance because there is $qtd pending statements with the amount of $unclearedValues"
);
}

// Update object Account
$account->setGrossBalance($newBalance);
$account->setNetBalance($newBalance - $unclearedValues);
$account->setUnCleared($unclearedValues);
$account->setPrice($newPrice);
$account->setMinValue($newMinValue);
$account->setLastUuid($dto->getUuid());
$this->accountRepository->save($account);

// Create new Statement
$statement = new StatementEntity();
$statement->setAmount($newBalance);
$statement->setAccountId($account->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($account->getAccountTypeId());
$statement->setUuid($dto->getUuid());
$this->statementBLL->getRepository()->save($statement);
$this->accountRepository->getDbDriver()->commitTransaction();
} catch (\Exception $ex) {
$this->accountRepository->getDbDriver()->rollbackTransaction();
throw $ex;
}

return $statement->getStatementId();
}
Expand Down Expand Up @@ -254,30 +264,26 @@ public function closeAccount(int $accountId): ?int
* @param int $accountId
* @param float $balance
* @param string $description
* @return int|null
* @return StatementEntity
* @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
public function partialBalance(int $accountId, float $balance, string $description = "Partial Balance"): StatementEntity
{
$account = $this->getById($accountId);

$amount = $balance - $account->getNetBalance();

if ($amount >= 0) {
$statementId = $this->statementBLL->addFunds(StatementDTO::create($accountId, $amount)->setDescription($description));
$statement = $this->statementBLL->addFunds(StatementDTO::create($accountId, $amount)->setDescription($description));
} else {
$statementId = $this->statementBLL->withdrawFunds(StatementDTO::create($accountId, abs($amount))->setDescription($description));
$statement = $this->statementBLL->withdrawFunds(StatementDTO::create($accountId, abs($amount))->setDescription($description));
}

return $statementId;
return $statement;
}

/**
Expand All @@ -288,11 +294,7 @@ public function partialBalance(int $accountId, float $balance, string $descripti
* @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
Expand All @@ -315,10 +317,10 @@ public function transferFunds(int $accountSource, int $accountTarget, float $amo
$statementTargetDTO->setReferenceId($refSource);
$statementTargetDTO->setDescription('Transfer from account id ' . $accountSource);

$statementSourceId = $this->statementBLL->withdrawFunds($statementSourceDTO);
$statementTargetId = $this->statementBLL->addFunds($statementTargetDTO);
$statementSource = $this->statementBLL->withdrawFunds($statementSourceDTO);
$statementTarget = $this->statementBLL->addFunds($statementTargetDTO);

return [ $statementSourceId, $statementTargetId ];
return [ $statementSource, $statementTarget ];
}

public function getRepository(): AccountRepository
Expand Down
4 changes: 2 additions & 2 deletions src/Bll/AccountTypeBLL.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public function __construct(AccountTypeRepository $accountTypeRepository)
* Se o ID não for passado, então devolve todos os AccountTypes.
*
* @param string $accountTypeId Opcional. Se não for passado obtém todos
* @return AccountTypeEntity|AccountTypeEntity[]
* @return AccountTypeEntity|AccountTypeEntity[]|null
Comment thread
byjg marked this conversation as resolved.
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
*/
public function getById(string $accountTypeId): array|AccountTypeEntity
public function getById(string $accountTypeId): array|AccountTypeEntity|null
{
return $this->accountTypeRepository->getById($accountTypeId);
}
Expand Down
Loading