<?php
namespace App\Entity;
use App\Repository\TreasuryCashRegisterRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass=TreasuryCashRegisterRepository::class)
* Cette entite represente le releves (caisse journaliere)
* @UniqueEntity(fields={"code"}, message="Cet élt existe déjà")
*/
class TreasuryCashRegister
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Establishment::class, inversedBy="treasuryCashRegisters")
* @ORM\JoinColumn(nullable=false)
*/
private $establishment;
/**
* @ORM\ManyToOne(targetEntity=TreasuryCheckout::class, inversedBy="treasuryCashRegisters")
* @ORM\JoinColumn(nullable=false)
*/
private $checkout;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $code;
/**
* @ORM\Column(type="boolean")
*/
private $is_closed;
/**
* @ORM\Column(type="date_immutable")
*/
private $create_at;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $created_at;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $updated_at;
/**
* @ORM\Column(type="integer")
*/
private $created_by;
/**
* @ORM\Column(type="integer")
*/
private $updated_by;
/**
* @ORM\OneToMany(targetEntity=TreasuryCashMovement::class, mappedBy="cashRegister")
*/
private $treasuryCashMovements;
/**
* @ORM\OneToMany(targetEntity=AccountingStudentRegistrationFeeShedulPayment::class, mappedBy="cashRegister")
*/
private $accountingStudentRegistrationFeeShedulPayments;
/**
* @ORM\OneToMany(targetEntity=AccountingExpense::class, mappedBy="cashRegister")
*/
private $accountingExpenses;
/**
* @ORM\OneToMany(targetEntity=AccountingStudentRegistrationPayment::class, mappedBy="cashRegister")
* @ORM\OrderBy({"id" = "DESC"})
*/
private $accountingStudentRegistrationPayments;
/**
* @ORM\ManyToOne(targetEntity=SchoolYear::class, inversedBy="treasuryCashRegisters")
* @ORM\JoinColumn(nullable=false)
*/
private $schoolYear;
/**
* @ORM\OneToMany(targetEntity=TreasuryPayment::class, mappedBy="cashRegister")
*/
private $treasuryPayments;
/**
* @ORM\OneToMany(targetEntity=AccountingCreditLine::class, mappedBy="payment_cash_register")
*/
private $accountingCreditLines;
/**
* @ORM\Column(type="boolean")
*/
private $is_founder_valedated;
/**
* @ORM\Column(type="boolean")
*/
private $is_founder_rejected;
public function __construct()
{
$this->treasuryCashMovements = new ArrayCollection();
$this->accountingStudentRegistrationFeeShedulPayments = new ArrayCollection();
$this->accountingExpenses = new ArrayCollection();
$this->accountingStudentRegistrationPayments = new ArrayCollection();
$this->create_at = new DateTimeImmutable();
$this->is_closed = false;
$this->is_founder_valedated = false;
$this->is_founder_rejected = false;
$this->treasuryPayments = new ArrayCollection();
$this->accountingCreditLines = new ArrayCollection();
}
public function __toString()
{
return $this->code;
}
public function getId(): ?int
{
return $this->id;
}
public function getEstablishment(): ?Establishment
{
return $this->establishment;
}
public function setEstablishment(?Establishment $establishment): self
{
$this->establishment = $establishment;
return $this;
}
public function getCheckout(): ?TreasuryCheckout
{
return $this->checkout;
}
public function setCheckout(?TreasuryCheckout $checkout): self
{
$this->checkout = $checkout;
return $this;
}
/**
* @return Collection|TreasuryCashMovement[]
*/
public function getTreasuryCashMovements(): Collection
{
return $this->treasuryCashMovements;
}
public function addTreasuryCashMovement(TreasuryCashMovement $treasuryCashMovement): self
{
if (!$this->treasuryCashMovements->contains($treasuryCashMovement)) {
$this->treasuryCashMovements[] = $treasuryCashMovement;
$treasuryCashMovement->setCashRegister($this);
}
return $this;
}
public function removeTreasuryCashMovement(TreasuryCashMovement $treasuryCashMovement): self
{
if ($this->treasuryCashMovements->removeElement($treasuryCashMovement)) {
// set the owning side to null (unless already changed)
if ($treasuryCashMovement->getCashRegister() === $this) {
$treasuryCashMovement->setCashRegister(null);
}
}
return $this;
}
/**
* @return Collection|AccountingStudentRegistrationFeeShedulPayment[]
*/
public function getAccountingStudentRegistrationFeeShedulPayments(): Collection
{
return $this->accountingStudentRegistrationFeeShedulPayments;
}
public function addAccountingStudentRegistrationFeeShedulPayment(AccountingStudentRegistrationFeeShedulPayment $accountingStudentRegistrationFeeShedulPayment): self
{
if (!$this->accountingStudentRegistrationFeeShedulPayments->contains($accountingStudentRegistrationFeeShedulPayment)) {
$this->accountingStudentRegistrationFeeShedulPayments[] = $accountingStudentRegistrationFeeShedulPayment;
$accountingStudentRegistrationFeeShedulPayment->setCashRegister($this);
}
return $this;
}
public function removeAccountingStudentRegistrationFeeShedulPayment(AccountingStudentRegistrationFeeShedulPayment $accountingStudentRegistrationFeeShedulPayment): self
{
if ($this->accountingStudentRegistrationFeeShedulPayments->removeElement($accountingStudentRegistrationFeeShedulPayment)) {
// set the owning side to null (unless already changed)
if ($accountingStudentRegistrationFeeShedulPayment->getCashRegister() === $this) {
$accountingStudentRegistrationFeeShedulPayment->setCashRegister(null);
}
}
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeImmutable $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updated_at;
}
public function setUpdatedAt(\DateTimeImmutable $updated_at): self
{
$this->updated_at = $updated_at;
return $this;
}
public function getCreatedBy(): ?int
{
return $this->created_by;
}
public function setCreatedBy(int $created_by): self
{
$this->created_by = $created_by;
return $this;
}
public function getUpdatedBy(): ?int
{
return $this->updated_by;
}
public function setUpdatedBy(int $updated_by): self
{
$this->updated_by = $updated_by;
return $this;
}
/**
* @return Collection|AccountingExpense[]
*/
public function getAccountingExpenses(): Collection
{
return $this->accountingExpenses;
}
public function addAccountingExpense(AccountingExpense $accountingExpense): self
{
if (!$this->accountingExpenses->contains($accountingExpense)) {
$this->accountingExpenses[] = $accountingExpense;
$accountingExpense->setCashRegister($this);
}
return $this;
}
public function removeAccountingExpense(AccountingExpense $accountingExpense): self
{
if ($this->accountingExpenses->removeElement($accountingExpense)) {
// set the owning side to null (unless already changed)
if ($accountingExpense->getCashRegister() === $this) {
$accountingExpense->setCashRegister(null);
}
}
return $this;
}
/**
* @return Collection|AccountingStudentRegistrationPayment[]
*/
public function getAccountingStudentRegistrationPayments(): Collection
{
return $this->accountingStudentRegistrationPayments;
}
public function addAccountingStudentRegistrationPayment(AccountingStudentRegistrationPayment $accountingStudentRegistrationPayment): self
{
if (!$this->accountingStudentRegistrationPayments->contains($accountingStudentRegistrationPayment)) {
$this->accountingStudentRegistrationPayments[] = $accountingStudentRegistrationPayment;
$accountingStudentRegistrationPayment->setCashRegister($this);
}
return $this;
}
public function removeAccountingStudentRegistrationPayment(AccountingStudentRegistrationPayment $accountingStudentRegistrationPayment): self
{
if ($this->accountingStudentRegistrationPayments->removeElement($accountingStudentRegistrationPayment)) {
// set the owning side to null (unless already changed)
if ($accountingStudentRegistrationPayment->getCashRegister() === $this) {
$accountingStudentRegistrationPayment->setCashRegister(null);
}
}
return $this;
}
public function getCreateAt(): ?\DateTimeImmutable
{
return $this->create_at;
}
public function setCreateAt(\DateTimeImmutable $create_at): self
{
$this->create_at = $create_at;
return $this;
}
public function getCashedIn(){
$cashedIn = 0;
foreach ($this->getAccountingStudentRegistrationPayments() as $key => $payment) {
$cashedIn = $cashedIn + $payment->getAmount();
}
return $cashedIn;
}
public function getSpent(){
$spent = 0;
foreach ($this->getAccountingExpenses() as $key => $expense) {
if ($expense->getIsPaid()) {
$spent = $spent + $expense->getAmount();
}
}
return $spent;
}
public function getPaid(){
$paid = 0;
foreach ($this->getTreasuryPayments() as $key => $treasuryPayment) {
if ($this->getIsFounderValedated() && !$treasuryPayment->getIsFounderRejected()) {
$paid = $paid + $treasuryPayment->getAmount();
}
}
return $paid;
}
public function getAllPaid(){
$paid = 0;
foreach ($this->getTreasuryPayments() as $key => $treasuryPayment) {
//if ($this->getIsFounderValedated()) {
$paid = $paid + $treasuryPayment->getAmount();
//}
}
return $paid;
}
public function getAprovedPayment(){
$paid = 0;
foreach ($this->getTreasuryPayments() as $key => $treasuryPayment) {
if ($treasuryPayment->getIsFounderValedated()) {
$paid = $paid + $treasuryPayment->getAmount();
}
}
return $paid;
}
public function getNotAprovedPayment(){
$paid = 0;
foreach ($this->getTreasuryPayments() as $key => $treasuryPayment) {
if (!$treasuryPayment->getIsFounderValedated()) {
$paid = $paid + $treasuryPayment->getAmount();
}
}
return $paid;
}
public function toForward(TreasuryCashRegisterRepository $treasuryCashRegisterRepository){
return ($this->getCashedIn() + $this->getPostponed($treasuryCashRegisterRepository)) - ($this->getSpent() + $this->getPaid());
}
public function getPostPoned(TreasuryCashRegisterRepository $treasuryCashRegisterRepository){
$amount = 0;
$treasuryCashRegisters = $treasuryCashRegisterRepository->createQueryBuilder('entity')
->andWhere('entity.create_at < :endAt')
->setParameter('endAt', $this->getCreateAt())
->andWhere('entity.checkout = :checkout')
->setParameter('checkout', $this->getCheckout())
->orderBy('entity.id', 'ASC')
->getQuery()
->getResult();
$cashedIn = 0;
$spent = 0;
$paid = 0;
foreach ($treasuryCashRegisters as $key => $treasuryCashRegister) {
$paid = $paid + $treasuryCashRegister->getPaid();
$spent = $spent + $treasuryCashRegister->getSpent();
$cashedIn = $cashedIn + $treasuryCashRegister->getCashedIn();
}
$amount = $cashedIn - ($spent + $paid);
return $amount;
}
public function getSchoolYear(): ?SchoolYear
{
return $this->schoolYear;
}
public function setSchoolYear(?SchoolYear $schoolYear): self
{
$this->schoolYear = $schoolYear;
return $this;
}
public function getIsClosed(): ?bool
{
return $this->is_closed;
}
public function setIsClosed(bool $is_closed): self
{
$this->is_closed = $is_closed;
return $this;
}
/**
* @return Collection|TreasuryPayment[]
*/
public function getTreasuryPayments(): Collection
{
return $this->treasuryPayments;
}
public function addTreasuryPayment(TreasuryPayment $treasuryPayment): self
{
if (!$this->treasuryPayments->contains($treasuryPayment)) {
$this->treasuryPayments[] = $treasuryPayment;
$treasuryPayment->setCashRegister($this);
}
return $this;
}
public function removeTreasuryPayment(TreasuryPayment $treasuryPayment): self
{
if ($this->treasuryPayments->removeElement($treasuryPayment)) {
// set the owning side to null (unless already changed)
if ($treasuryPayment->getCashRegister() === $this) {
$treasuryPayment->setCashRegister(null);
}
}
return $this;
}
/**
* @return Collection|AccountingCreditLine[]
*/
public function getAccountingCreditLines(): Collection
{
return $this->accountingCreditLines;
}
public function addAccountingCreditLine(AccountingCreditLine $accountingCreditLine): self
{
if (!$this->accountingCreditLines->contains($accountingCreditLine)) {
$this->accountingCreditLines[] = $accountingCreditLine;
$accountingCreditLine->setPaymentCashRegister($this);
}
return $this;
}
public function removeAccountingCreditLine(AccountingCreditLine $accountingCreditLine): self
{
if ($this->accountingCreditLines->removeElement($accountingCreditLine)) {
// set the owning side to null (unless already changed)
if ($accountingCreditLine->getPaymentCashRegister() === $this) {
$accountingCreditLine->setPaymentCashRegister(null);
}
}
return $this;
}
public function getIsFounderValedated(): ?bool
{
return $this->is_founder_valedated;
}
public function setIsFounderValedated(bool $is_founder_valedated): self
{
$this->is_founder_valedated = $is_founder_valedated;
return $this;
}
public function getIsFounderRejected(): ?bool
{
return $this->is_founder_rejected;
}
public function setIsFounderRejected(bool $is_founder_rejected): self
{
$this->is_founder_rejected = $is_founder_rejected;
return $this;
}
}