src/Entity/CompanyAuditNR.php line 12
<?php
namespace App\Entity;
use App\Repository\CompanyAuditNRRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CompanyAuditNRRepository::class)]
class CompanyAuditNR
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?LabelNR $labelNR = null;
#[ORM\OneToMany(mappedBy: 'companyAuditNR', targetEntity: CompanyAuditNRAxe::class, orphanRemoval: true, fetch: "EAGER")]
private Collection $companyAxes;
#[ORM\Column]
private ?bool $active = null;
#[ORM\Column(type: Types::SMALLINT)]
private ?int $type = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\ManyToOne(inversedBy: 'companyAuditNR')]
#[ORM\JoinColumn(nullable: false)]
private ?Company $company = null;
public function __construct()
{
$this->companyAxes = new ArrayCollection();
$this->active = true;
$this->createdAt = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getLabelNR(): ?LabelNR
{
return $this->labelNR;
}
public function setLabelNR(?LabelNR $labelNR): self
{
$this->labelNR = $labelNR;
return $this;
}
/**
* @return Collection<int, CompanyAuditNRAxe>
*/
public function getCompanyAxes(): Collection
{
return $this->companyAxes;
}
public function addCompanyAxe(CompanyAuditNRAxe $companyAxe): self
{
if (!$this->companyAxes->contains($companyAxe)) {
$this->companyAxes->add($companyAxe);
$companyAxe->setCompanyAuditNR($this);
}
return $this;
}
public function removeCompanyAxe(CompanyAuditNRAxe $companyAxe): self
{
if ($this->companyAxes->removeElement($companyAxe)) {
// set the owning side to null (unless already changed)
if ($companyAxe->getCompanyAuditNR() === $this) {
$companyAxe->setCompanyAuditNR(null);
}
}
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
public function getScore()
{
$score = 0;
$maxScore = 0;
$numberNone = 0;
foreach ($this->companyAxes as $key => $axe) {
foreach ($axe->getCompanyPAs() as $pa) {
foreach ($pa->getCompanyTIRs() as $key => $tir) {
if ($tir->getScore() === "none") {
$numberNone++;
$maxScore--;
} else {
$score += $tir->getScore();
}
$maxScore++;
}
}
}
if ($maxScore == $numberNone) {
return "none";
}
return [
"score" => $score,
"maxScore" => $maxScore,
"percentage" => round(($score / $maxScore) * 100, 2)
];
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getInfosAdvancement()
{
$infosAdvancement = [
"tir" => [
"evaluated" => 0,
"total" => 0,
"percentage" => 0,
],
"pa" => [
"finished" => 0,
"total" => 0,
"percentage" => 0,
],
"na" => 0,
];
foreach ($this->companyAxes as $key => $axe) {
foreach ($axe->getCompanyPAs() as $pa) {
$infosAdvancement["pa"]["total"]++;
if ($pa->isTerminated()) {
$infosAdvancement["pa"]["finished"]++;
}
foreach ($pa->getCompanyTIRs() as $tir) {
$infosAdvancement["tir"]["total"]++;
if ($tir->getEvaluation()) {
$infosAdvancement["tir"]["evaluated"]++;
}
if ($tir->getEvaluation() === "Non applicable") {
$infosAdvancement["na"]++;
}
}
}
}
$infosAdvancement["tir"]["percentage"] = round(($infosAdvancement["tir"]["evaluated"] / $infosAdvancement["tir"]["total"]) * 100);
$infosAdvancement["pa"]["percentage"] = round(($infosAdvancement["pa"]["finished"] / $infosAdvancement["pa"]["total"]) * 100);
return $infosAdvancement;
}
public function getInfosAmelioration()
{
$PAs = [];
foreach ($this->companyAxes as $axe) {
foreach ($axe->getCompanyPAs() as $pa) {
$score = $pa->getScore();
if ($score !== "none" && $score["percentage"] < 50) {
$PAs[] = [
"name" => $pa->getPA()->getName(),
"percentage" => $score["percentage"],
"color" => $axe->getAxe()->getColor(),
"code" => $axe->getCode()
];
}
}
}
usort($PAs, function ($a, $b) {
return $a["percentage"] <=> $b["percentage"];
});
return array_slice($PAs, 0, 5);
}
public function getInfosComparison($before)
{
$comparison = [
"scoreNow" => 0,
"scoreBefore" => 0,
"evolution" => 0,
"axes" => []
];
$comparison["scoreNow"] = $this->getScore()["percentage"];
$comparison["scoreBefore"] = $before->getScore()["percentage"];
$comparison["evolution"] = $comparison["scoreNow"] - $comparison["scoreBefore"];
foreach ($this->companyAxes as $key => $axe) {
$axeInfos = [
"name" => $axe->getAxe()->getName(),
"scoreNow" => 0,
"scoreBefore" => 0,
"evolution" => 0,
"color" => $axe->getAxe()->getColor(),
"code" => $axe->getCode()
];
$axeInfos["scoreNow"] = $axe->getScore()["percentage"];
$beforeAxeScore = null;
foreach ($before->getCompanyAxes() as $key => $beforeAxe) {
if ($beforeAxe->getAxe()->getId() === $axe->getAxe()->getId()) {
$beforeAxeScore = $beforeAxe->getScore()["percentage"];
break;
}
}
$axeInfos["scoreBefore"] = $beforeAxeScore;
$axeInfos["evolution"] = $axeInfos["scoreNow"] - $axeInfos["scoreBefore"];
$comparison["axes"][] = $axeInfos;
}
return $comparison;
}
}