feature/RVRNEXT-6-transactions #36
160
src/PersistentData/Model/Transaction.php
Normal file
160
src/PersistentData/Model/Transaction.php
Normal file
@ -0,0 +1,160 @@
|
||||
<?php namespace RVR\PersistentData\Model;
|
||||
|
||||
use DateTime;
|
||||
use SokoWeb\PersistentData\Model\Model;
|
||||
|
||||
class Transaction extends Model
|
||||
{
|
||||
protected static string $table = 'transactions';
|
||||
|
||||
protected static array $fields = ['community_id', 'currency_id', 'payer_user_id', 'payee_user_id', 'description', 'sum', 'time'];
|
||||
|
||||
protected static array $relations = [
|
||||
'community' => Community::class,
|
||||
'currency' => Currency::class,
|
||||
'payer_user' => User::class,
|
||||
'payee_user' => User::class
|
||||
];
|
||||
|
||||
private ?Community $community = null;
|
||||
|
||||
private int $communityId;
|
||||
|
||||
private ?Currency $currency = null;
|
||||
|
||||
private int $currencyId;
|
||||
|
||||
private ?User $payerUser = null;
|
||||
|
||||
private int $payerUserId;
|
||||
|
||||
private ?User $payeeUser = null;
|
||||
|
||||
private ?int $payeeUserId = null;
|
||||
|
||||
private string $description = '';
|
||||
|
||||
private float $sum = 0.0;
|
||||
|
||||
private DateTime $time;
|
||||
|
||||
public function setCommunity(Community $community): void
|
||||
{
|
||||
$this->community = $community;
|
||||
}
|
||||
|
||||
public function setCommunityId(int $communityId): void
|
||||
{
|
||||
$this->communityId = $communityId;
|
||||
}
|
||||
|
||||
public function setCurrency(Currency $currency): void
|
||||
{
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
public function setCurrencyId(int $currencyId): void
|
||||
{
|
||||
$this->currencyId = $currencyId;
|
||||
}
|
||||
|
||||
public function setPayerUser(User $payerUser): void
|
||||
{
|
||||
$this->payerUser = $payerUser;
|
||||
}
|
||||
|
||||
public function setPayerUserId(int $payerUserId): void
|
||||
{
|
||||
$this->payerUserId = $payerUserId;
|
||||
}
|
||||
|
||||
public function setPayeeUser(?User $payeeUser): void
|
||||
{
|
||||
$this->payeeUser = $payeeUser;
|
||||
}
|
||||
|
||||
public function setPayeeUserId(?int $payeeUserId): void
|
||||
{
|
||||
$this->payeeUserId = $payeeUserId;
|
||||
}
|
||||
|
||||
public function setDescription(string $description): void
|
||||
{
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
public function setSum(float $sum): void
|
||||
{
|
||||
$this->sum = $sum;
|
||||
}
|
||||
|
||||
public function setTimeDate(DateTime $time): void
|
||||
{
|
||||
$this->time = $time;
|
||||
}
|
||||
|
||||
public function setTime(string $time): void
|
||||
{
|
||||
$this->time = new DateTime($time);
|
||||
}
|
||||
|
||||
public function getCommunity(): ?Community
|
||||
{
|
||||
return $this->community;
|
||||
}
|
||||
|
||||
public function getCommunityId(): int
|
||||
{
|
||||
return $this->communityId;
|
||||
}
|
||||
|
||||
public function getCurrency(): ?Currency
|
||||
{
|
||||
return $this->currency;
|
||||
}
|
||||
|
||||
public function getCurrencyId(): int
|
||||
{
|
||||
return $this->currencyId;
|
||||
}
|
||||
|
||||
public function getPayerUser(): ?User
|
||||
{
|
||||
return $this->payerUser;
|
||||
}
|
||||
|
||||
public function getPayerUserId(): int
|
||||
{
|
||||
return $this->payerUserId;
|
||||
}
|
||||
|
||||
public function getPayeeUser(): ?User
|
||||
{
|
||||
return $this->payeeUser;
|
||||
}
|
||||
|
||||
public function getPayeeUserId(): ?int
|
||||
{
|
||||
return $this->payeeUserId;
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function getSum(): float
|
||||
{
|
||||
return $this->sum;
|
||||
}
|
||||
|
||||
public function getTimeDate(): DateTime
|
||||
{
|
||||
return $this->time;
|
||||
}
|
||||
|
||||
public function getTime(): string
|
||||
{
|
||||
return $this->time->format('Y-m-d H:i:s');
|
||||
}
|
||||
}
|
44
src/Repository/TransactionRepository.php
Normal file
44
src/Repository/TransactionRepository.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php namespace RVR\Repository;
|
||||
|
||||
use Container;
|
||||
use Generator;
|
||||
use RVR\PersistentData\Model\Community;
|
||||
use RVR\PersistentData\Model\Transaction;
|
||||
use SokoWeb\Database\Query\Select;
|
||||
|
||||
class TransactionRepository
|
||||
{
|
||||
public function getById(int $id): ?Transaction
|
||||
{
|
||||
return Container::$persistentDataManager->selectFromDbById($id, Transaction::class);
|
||||
}
|
||||
|
||||
public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator
|
||||
{
|
||||
$select = $this->selectAllByCommunity($community);
|
||||
|
||||
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations);
|
||||
}
|
||||
|
||||
public function countAllByCommunity(Community $community): int
|
||||
{
|
||||
return $this->selectAllByCommunity($community)->count();
|
||||
}
|
||||
|
||||
public function getPagedByCommunity(Community $community, int $start, int $limit, bool $useRelations = false, array $withRelations = []): Generator
|
||||
{
|
||||
$select = new Select(Container::$dbConnection);
|
||||
$select->where('community_id', '=', $community->getId());
|
||||
$select->orderBy('time', 'DESC');
|
||||
$select->limit($limit, $start);
|
||||
|
||||
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations);
|
||||
}
|
||||
|
||||
private function selectAllByCommunity(Community $community)
|
||||
{
|
||||
$select = new Select(Container::$dbConnection, Transaction::getTable());
|
||||
$select->where('community_id', '=', $community->getId());
|
||||
return $select;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user