<?php
namespace App\Entity\Company;
use App\Entity\Accounting\OADA\CompanyAccount;
use App\Entity\Accounting\OADA\FiscalYear;
use App\Entity\Accounting\OADA\OADAAccount;
use App\Entity\Accounting\OADA\Transaction;
use App\Entity\ChangeRate;
use App\Entity\Department\Department;
use App\Entity\Department\Section;
use App\Entity\Docs\Finance\Payment\Transfer;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use App\Entity\Docs\Finance\Project;
use App\Entity\ParamsApp;
use App\Entity\User;
use App\Entity\Utils\Contact;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Utils\TimestampTrait;
use App\Entity\Utils\AccountVerification;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups as sGroup;
/**
* @ORM\HasLifecycleCallbacks()
* @ORM\Entity(repositoryClass="App\Repository\Company\CompanyRepository")
* @UniqueEntity(fields={"fullname"}, message="Ce nom existe déjà")
* @UniqueEntity(fields={"representativeEmail"}, message="Cet email existe déjà")
* @UniqueEntity(fields={"representativePhone"}, message="Ce numéro existe déjà")
*/
#[ApiResource(
normalizationContext:["groups" => ["company:seach"]]
)]
#[Get(
security:"is_granted('ROLE_ADMIN')"
)]
#[GetCollection(
security:"is_granted('ROLE_ADMIN')"
)]
#[Patch(
security:"is_granted('ROLE_ADMIN')"
)]
class Company
{
use TimestampTrait;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @sGroup({"company:seach","rmq:user", "app:params:get"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @sGroup({"company:seach","rmq:user", "app:params:get"})
*/
private $fullname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $abbreviation;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(4)
*/
private $creationDate;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\Email
*
*/
private $representativeEmail;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\Length(10)
*/
private $representativePhone;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Utils\AccountVerification", cascade={"persist", "remove"})
*/
private $accountVerification;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Company\Juridiction", cascade={"persist", "remove"})
*/
private $juridiction;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Company\Certification", cascade={"persist", "remove"})
*/
private $certification;
/**
* @ORM\Column(type="string", length=255)
*/
private $username;
/**
* @ORM\ManyToMany(targetEntity=Department::class, inversedBy="companies",cascade={"persist"})
*/
private $departments;
/**
* @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="company")
*/
private $users;
/**
* @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="companyOwner")
*/
private $owners;
/**
* @ORM\OneToMany(targetEntity=Department::class, mappedBy="company")
*/
private $departmentsAdded;
/**
* @ORM\OneToMany(targetEntity=Section::class, mappedBy="company")
*/
private $sectionsAdded;
/**
* @ORM\OneToMany(targetEntity=Contact::class, mappedBy="company")
*/
private $contacts;
/**
* @ORM\OneToMany(targetEntity=Project::class, mappedBy="company", cascade={"persist"})
*/
private $projects;
/**
* @ORM\OneToMany(targetEntity=Transfer::class, mappedBy="company")
*/
private $transfers;
/**
* @ORM\OneToMany(targetEntity=FiscalYear::class, mappedBy="company")
*/
private $fiscalYears;
/**
* @ORM\OneToMany(targetEntity=Transaction::class, mappedBy="company")
*/
private $transactionCheckout;
/**
* @ORM\OneToMany(targetEntity=CompanyAccount::class, mappedBy="company")
*/
private $companyAccounts;
/**
* @ORM\OneToMany(targetEntity=OADAAccount::class, mappedBy="company")
*/
private $oADAAccounts;
/**
* @ORM\OneToMany(targetEntity=ParamsApp::class, mappedBy="company")
*/
private $paramsApps;
/**
* @ORM\OneToMany(targetEntity=ChangeRate::class, mappedBy="company")
*/
private $changeRates;
public function __construct()
{
$this->departments = new ArrayCollection();
$this->users = new ArrayCollection();
$this->owners = new ArrayCollection();
$this->departmentsAdded = new ArrayCollection();
$this->sectionsAdded = new ArrayCollection();
$this->contacts = new ArrayCollection();
$this->username = base64_encode(uniqid());
$this->projects = new ArrayCollection();
$this->transfers = new ArrayCollection();
$this->fiscalYears = new ArrayCollection();
$this->transactionCheckout = new ArrayCollection();
$this->companyAccounts = new ArrayCollection();
$this->oADAAccounts = new ArrayCollection();
$this->paramsApps = new ArrayCollection();
$this->changeRates = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFullname(): ?string
{
return $this->fullname;
}
public function setFullname(string $fullname): self
{
$this->fullname = $fullname;
return $this;
}
public function getAbbreviation(): ?string
{
return $this->abbreviation;
}
public function setAbbreviation(?string $abbreviation): self
{
$this->abbreviation = $abbreviation;
return $this;
}
public function getCreationDate(): ?string
{
return $this->creationDate;
}
public function setCreationDate(string $creationDate): self
{
$this->creationDate = $creationDate;
return $this;
}
public function getRepresentativeEmail(): ?string
{
return $this->representativeEmail;
}
public function setRepresentativeEmail(string $representativeEmail): self
{
$this->representativeEmail = $representativeEmail;
return $this;
}
public function getRepresentativePhone(): ?string
{
return $this->representativePhone;
}
public function setRepresentativePhone(string $representativePhone): self
{
$this->representativePhone = $representativePhone;
return $this;
}
public function getAccountVerification(): ?AccountVerification
{
return $this->accountVerification;
}
public function setAccountVerification(?AccountVerification $accountVerification): self
{
$this->accountVerification = $accountVerification;
return $this;
}
public function getJuridiction(): ?Juridiction
{
return $this->juridiction;
}
public function setJuridiction(?Juridiction $juridiction): self
{
$this->juridiction = $juridiction;
return $this;
}
public function getCertification(): ?Certification
{
return $this->certification;
}
public function setCertification(?Certification $certification): self
{
$this->certification = $certification;
return $this;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @return Collection|Department[]
*/
public function getDepartments(): Collection
{
return $this->departments;
}
public function addDepartment(Department $department): self
{
if (!$this->departments->contains($department)) {
$this->departments[] = $department;
}
return $this;
}
public function removeDepartment(Department $department): self
{
if ($this->departments->contains($department)) {
$this->departments->removeElement($department);
}
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setCompany($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
// set the owning side to null (unless already changed)
if ($user->getCompany() === $this) {
$user->setCompany(null);
}
}
return $this;
}
/**
* @return Collection|User[]
*/
public function getOwners(): Collection
{
return $this->owners;
}
public function addOwner(User $owner): self
{
if (!$this->owners->contains($owner)) {
$this->owners[] = $owner;
$owner->setCompanyOwner($this);
}
return $this;
}
public function removeOwner(User $owner): self
{
if ($this->owners->contains($owner)) {
$this->owners->removeElement($owner);
// set the owning side to null (unless already changed)
if ($owner->getCompanyOwner() === $this) {
$owner->setCompanyOwner(null);
}
}
return $this;
}
/**
* @return Collection|Department[]
*/
public function getDepartmentsAdded(): Collection
{
return $this->departmentsAdded;
}
public function addDepartmentsAdded(Department $departmentsAdded): self
{
if (!$this->departmentsAdded->contains($departmentsAdded)) {
$this->departmentsAdded[] = $departmentsAdded;
$departmentsAdded->setCompany($this);
}
return $this;
}
public function removeDepartmentsAdded(Department $departmentsAdded): self
{
if ($this->departmentsAdded->contains($departmentsAdded)) {
$this->departmentsAdded->removeElement($departmentsAdded);
// set the owning side to null (unless already changed)
if ($departmentsAdded->getCompany() === $this) {
$departmentsAdded->setCompany(null);
}
}
return $this;
}
/**
* @return Collection|Section[]
*/
public function getSectionsAdded(): Collection
{
return $this->sectionsAdded;
}
public function addSectionsAdded(Section $sectionsAdded): self
{
if (!$this->sectionsAdded->contains($sectionsAdded)) {
$this->sectionsAdded[] = $sectionsAdded;
$sectionsAdded->setCompany($this);
}
return $this;
}
public function removeSectionsAdded(Section $sectionsAdded): self
{
if ($this->sectionsAdded->contains($sectionsAdded)) {
$this->sectionsAdded->removeElement($sectionsAdded);
// set the owning side to null (unless already changed)
if ($sectionsAdded->getCompany() === $this) {
$sectionsAdded->setCompany(null);
}
}
return $this;
}
/**
* @return Collection|Contact[]
*/
public function getContacts(): Collection
{
return $this->contacts;
}
public function addContact(Contact $contact): self
{
if (!$this->contacts->contains($contact)) {
$this->contacts[] = $contact;
$contact->setCompany($this);
}
return $this;
}
public function removeContact(Contact $contact): self
{
if ($this->contacts->contains($contact)) {
$this->contacts->removeElement($contact);
// set the owning side to null (unless already changed)
if ($contact->getCompany() === $this) {
$contact->setCompany(null);
}
}
return $this;
}
/**
* @return Collection|Project[]
*/
public function getProjects(): Collection
{
return $this->projects;
}
public function addProject(Project $project): self
{
if (!$this->projects->contains($project)) {
$this->projects[] = $project;
$project->setCompany($this);
}
return $this;
}
public function removeProject(Project $project): self
{
if ($this->projects->removeElement($project)) {
// set the owning side to null (unless already changed)
if ($project->getCompany() === $this) {
$project->setCompany(null);
}
}
return $this;
}
/**
* @return Transfer[]
*/
public function getTransfers(): Collection
{
return $this->transfers;
}
public function addTransfer(Transfer $transfer): self
{
if (!$this->transfers->contains($transfer)) {
$this->transfers[] = $transfer;
$transfer->setCompany($this);
}
return $this;
}
public function removeTransfer(Transfer $transfer): self
{
if ($this->transfers->removeElement($transfer)) {
// set the owning side to null (unless already changed)
if ($transfer->getCompany() === $this) {
$transfer->setCompany(null);
}
}
return $this;
}
/**
* @return float
*/
public function soldTransferUSD()
{
/**
* @var Transfer[]
*/
$transfers = $this->getTransfers();
$sum = 0;
foreach ($transfers as $key => $transfer) {
$sum += floatval($transfer->getCashoutUsd()) ;
}
return $sum;
}
/**
* @return float
*
*/
public function soldTransferCDF()
{
/**
* @var Transfer[]
*/
$transfers = $this->getTransfers();
$sum = 0;
foreach ($transfers as $key => $transfer) {
$sum += floatval($transfer->getCashoutCdf());
}
return $sum;
}
/**
*
*/
public function firstUser()
{
/**
* @var Department[]
*/
$departments = $this->getDepartments()->toArray();
foreach ($departments as $key => $department) {
$user = $department->firstUser();
if (!is_null($user)) {
return $user;
}
}
return null;
}
/**
* @return Collection<int, FiscalYear>
*/
public function getFiscalYears(): Collection
{
return $this->fiscalYears;
}
public function addFiscalYear(FiscalYear $fiscalYear): self
{
if (!$this->fiscalYears->contains($fiscalYear)) {
$this->fiscalYears[] = $fiscalYear;
$fiscalYear->setCompany($this);
}
return $this;
}
public function removeFiscalYear(FiscalYear $fiscalYear): self
{
if ($this->fiscalYears->removeElement($fiscalYear)) {
// set the owning side to null (unless already changed)
if ($fiscalYear->getCompany() === $this) {
$fiscalYear->setCompany(null);
}
}
return $this;
}
/**
* @return Collection<int, Transaction>
*/
public function getTransactionCheckout(): Collection
{
return $this->transactionCheckout;
}
public function addTransactionCheckout(Transaction $transactionCheckout): self
{
if (!$this->transactionCheckout->contains($transactionCheckout)) {
$this->transactionCheckout[] = $transactionCheckout;
$transactionCheckout->setCompany($this);
}
return $this;
}
public function removeTransactionCheckout(Transaction $transactionCheckout): self
{
if ($this->transactionCheckout->removeElement($transactionCheckout)) {
// set the owning side to null (unless already changed)
if ($transactionCheckout->getCompany() === $this) {
$transactionCheckout->setCompany(null);
}
}
return $this;
}
/**
* @return Collection<int, CompanyAccount>
*/
public function getCompanyAccounts(): Collection
{
return $this->companyAccounts;
}
public function addCompanyAccount(CompanyAccount $companyAccount): self
{
if (!$this->companyAccounts->contains($companyAccount)) {
$this->companyAccounts[] = $companyAccount;
$companyAccount->setCompany($this);
}
return $this;
}
public function removeCompanyAccount(CompanyAccount $companyAccount): self
{
if ($this->companyAccounts->removeElement($companyAccount)) {
// set the owning side to null (unless already changed)
if ($companyAccount->getCompany() === $this) {
$companyAccount->setCompany(null);
}
}
return $this;
}
/**
* @return Collection<int, OADAAccount>
*/
public function getOADAAccounts(): Collection
{
return $this->oADAAccounts;
}
public function addOADAAccount(OADAAccount $oADAAccount): self
{
if (!$this->oADAAccounts->contains($oADAAccount)) {
$this->oADAAccounts[] = $oADAAccount;
$oADAAccount->setCompany($this);
}
return $this;
}
public function removeOADAAccount(OADAAccount $oADAAccount): self
{
if ($this->oADAAccounts->removeElement($oADAAccount)) {
// set the owning side to null (unless already changed)
if ($oADAAccount->getCompany() === $this) {
$oADAAccount->setCompany(null);
}
}
return $this;
}
/**
* @return Collection<int, ParamsApp>
*/
public function getParamsApps(): Collection
{
return $this->paramsApps;
}
public function addParamsApp(ParamsApp $paramsApp): self
{
if (!$this->paramsApps->contains($paramsApp)) {
$this->paramsApps[] = $paramsApp;
$paramsApp->setCompany($this);
}
return $this;
}
public function removeParamsApp(ParamsApp $paramsApp): self
{
if ($this->paramsApps->removeElement($paramsApp)) {
// set the owning side to null (unless already changed)
if ($paramsApp->getCompany() === $this) {
$paramsApp->setCompany(null);
}
}
return $this;
}
/**
* @return Collection<int, ChangeRate>
*/
public function getChangeRates(): Collection
{
return $this->changeRates;
}
public function addChangeRate(ChangeRate $changeRate): self
{
if (!$this->changeRates->contains($changeRate)) {
$this->changeRates[] = $changeRate;
$changeRate->setCompany($this);
}
return $this;
}
public function removeChangeRate(ChangeRate $changeRate): self
{
if ($this->changeRates->removeElement($changeRate)) {
// set the owning side to null (unless already changed)
if ($changeRate->getCompany() === $this) {
$changeRate->setCompany(null);
}
}
return $this;
}
}