<?php
namespace App\Entity;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* Account
*
* @ORM\Table(name="account")
* @ORM\Entity(repositoryClass="App\Repository\AccountRepository")
*/
class Account //extends OrmEntity
{
const debit = 1;
const credit = 0;
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
* @Groups("Moviment")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="code", type="integer")
* @Groups("Moviment")
*/
private $code;
/**
* @var string
*
* @ORM\Column(name="description", type="string", nullable=true, length=255)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100)
*/
private $name;
/**
*
* @ORM\Column(name="natureza",nullable=true, type="boolean")
*/
private $natureza;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Account", inversedBy="account")
*/
private $account;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\AccountClass", inversedBy="accountClass")
*/
private $accountClass;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Account", mappedBy="account")
* @ORM\OrderBy({"code" = "ASC"})
*/
private $accounts;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Moviment", mappedBy="account")
*/
private $moviments;
/**
* @var string
* @Groups("Moviment")
*/
private $fullCode;
/**
* @var string
*/
private $amount;
private $credit;
private $debit;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="user")
*/
private $user;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
/**
* @var bool
*
* @ORM\Column(name="is_active",nullable=true, type="boolean")
*/
private $isActive;
private $initialCredit;
private $finalCredit;
/**
* Account constructor.
* @param bool $isActive
* @throws \Exception
*/
public function __construct()
{
$this->isActive = true;
//parent::__construct();
}
/**
* @return \Ramsey\Uuid\UuidInterface
*/
public function getId()
{
return $this->id;
}
/**
* @return Account
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Set code
*
* @param integer $code
*
* @return Account
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return int
*/
public function getCode()
{
return $this->code;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name)
{
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getFullCode()
{
if (is_null($this->getAccount()))
$this->fullCode = is_null($this->getAccountClass()) ? '' : $this->getAccountClass()->getCode() . $this->getCode();
else
$this->fullCode = $this->getAccount()->getFullCode() . '.' . $this->getCode();
return $this->fullCode;
}
/**
* @param mixed $fullCode
*/
public function setFullCode()
{
$this->fullCode = $this->getAccount()->getFullCode() . '.' . $this->getCode();
}
/**
* Set description
*
* @param string $description
*
* @return Account
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set natureza
*
* @param integer $natureza
*
* @return Account
*/
public function setNatureza($natureza)
{
$this->natureza = $natureza;
return $this;
}
/**
* Get natureza
*
*/
public function getNatureza()
{
return $this->natureza;
}
/**
* Set account
*
* @param Account $account
*
* @return Account
*/
public function setAccount($account)
{
$this->account = $account;
return $this;
}
/**
* @return Account
*/
public function getAccount()
{
return $this->account;
}
/**
* @return AccountClass
*/
public function getAccountClass()
{
return $this->accountClass;
}
/**
* @param AccountClass $accountClass
*/
public function setAccountClass($accountClass)
{
$this->accountClass = $accountClass;
}
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* @param mixed $user
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* @param \DateTime $date
*/
public function setDate($date)
{
$this->date = $date;
}
/**
* @return Account[]
*/
public function getAccounts()
{
return $this->accounts;
}
/**
* @param Account[] $accounts
* @return Account
*/
public function setAccounts($accounts)
{
$this->accounts = $accounts;
return $this;
}
/**
* @return float
*/
public function getAmount()
{
/*$this->amount = $this->getDebit()-$this->getCredit();
return $this->amount;*/
$amount = 0;
if ($this->getFullCode() == '61')
$t = 0;
if (!empty($this->getMoviments()))
foreach ($this->getMoviments() as $moviment) {
if ($moviment->getTransactionNature())
$amount -= $moviment->getAmount();
else
$amount += $moviment->getAmount();
}
foreach ($this->getAccounts() as $account)
$amount += $account->getAmount();
$this->amount = $amount;
return $this->amount;
}
public $amounti;
/**
* @return float
*/
public function getAmountFromTo($from = null, $to = null)
{
/*$this->amount = $this->getDebit()-$this->getCredit();
return $this->amount;*/
$amount = 0;
if ($this->getFullCode() == '61')
$t = 0;
$moviments = [];
foreach ($this->getMoviments() as $moviment)
$moviments[] = $moviment;
$moviments = array_filter($moviments,
function ($e) use (&$from, &$to) {
return ($e->getTransaction()->getTransactionDate() >= $from or is_null($from)) and (is_null($to) or $e->getTransaction()->getTransactionDate() <= $to);
}
);
foreach ($moviments as $moviment) {
if ($moviment->getTransactionNature())
$amount -= $moviment->getAmount();
else
$amount += $moviment->getAmount();
}
foreach ($this->getAccounts() as $account)
$amount += $account->getAmountFromTo($from, $to);
$this->amount = $amount;
$this->amounti = $amount;
return $this->amounti;
}
/**
* @return mixed
*/
public function getDebit()
{
$amount = 0;
$moviments = $this->getMoviments();
if (count($moviments) > 1)
foreach ($moviments as $moviment) {
if (!$moviment->getTransactionNature())
$amount += $moviment->getAmount();
}
elseif (count($this->getAccounts()) > 0)
foreach ($this->getAccounts() as $account)
$amount += $account->getDebit();
$this->debit = $amount;
return $this->debit;
}
/**
* @return mixed
*/
public function getCredit()
{
$amount = 0;
$moviments = $this->getMoviments();
if (count($moviments) > 1)
foreach ($moviments as $moviment) {
if ($moviment->getTransactionNature())
$amount += $moviment->getAmount();
}
elseif (count($this->getAccounts()) > 0)
foreach ($this->getAccounts() as $account)
$amount += $account->getDebit();
$this->credit = $amount;
return $this->credit;
}
/**
* @return Moviment[]
*/
public function getMoviments()
{
return (is_null($this->moviments) ? [] : $this->moviments);
}
/**
* @param $moviments
* @return $this
*/
public function setMoviments($moviments)
{
$this->moviments = $moviments;
return $this;
}
public function build($from, $to)
{
return $this;
}
/**
* @return mixed
*/
public function getInitialCredit()
{
return $this->initialCredit;
}
/**
* @param mixed $initialCredit
*/
public function setInitialCredit($initialCredit)
{
$this->initialCredit = $initialCredit;
}
/**
* @return mixed
*/
public function getFinalCredit()
{
return $this->getAmount();
}
/**
* @param mixed $finalCredit
*/
public function setFinalCredit($finalCredit)
{
$this->finalCredit = $finalCredit;
}
/**
* @return bool
*/
public function isActive()
{
return $this->isActive;
}
/**
* @param bool $isActive
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
}
}