<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Wits\PaymentBundle\Entity\PayableInterface;
use Wits\PaymentBundle\Entity\Payment;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="order_pack")
* @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
* @ORM\EntityListeners({"App\Listener\OrderListener"})
*/
class Order implements PayableInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\CreditPack")
* @ORM\JoinColumn(nullable=true)
*/
private $pack;
/**
* when this order is issued in a "pay as you go" process, this collection will contain the related credit costs
* @ORM\OneToMany(targetEntity=CreditCost::class, mappedBy="order")
*/
private Collection $creditCosts;
/**
* @ORM\OneToMany(targetEntity=StoreIndexation::class, mappedBy="order", cascade={"persist", "remove"})
*/
private Collection $storeIndexations;
/**
* @ORM\OneToOne(targetEntity="Wits\PaymentBundle\Entity\Payment")
* @ORM\JoinColumn(nullable=true)
*/
private ?Payment $payment = null;
/**
* @ORM\OneToMany(targetEntity="Wits\PaymentBundle\Entity\Payment", mappedBy="order")
*/
private Collection $payments;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*/
private $user;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Invoice", inversedBy="order", cascade={"persist", "remove"})
*/
private $invoice;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Freelancer", inversedBy="orders")
* @ORM\JoinColumn(nullable=true)
*/
private $freelancer;
/**
* @ORM\ManyToOne(targetEntity=CentralPurchasing::class, inversedBy="orders")
*/
private $centralPurchasing;
public function __construct()
{
$this->creditCosts = new ArrayCollection();
$this->storeIndexations = new ArrayCollection();
$this->payments = new ArrayCollection();
}
public function getLabel(): string
{
return 'Commande #' . $this->id;
}
/**
* Get the description of the order (pack, store indexation, etc...)
* @return string
*/
public function getDescription(): string
{
if ($this->getPack()) {
return $this->getPack()->getName();
} elseif ($this->hasStoreIndexation()) {
return 'Indexation de centre (' . $this->getStoreIndexations()->count() . ' centre(s) )';
} elseif ($this->hasCreditCost()) {
return 'Pay as you go (' . $this->getCreditCosts()->count() . ' contact(s) )';
}
return '-';
}
public function getId(): ?int
{
return $this->id;
}
public function getPack(): ?CreditPack
{
return $this->pack;
}
public function setPack(?CreditPack $pack): self
{
$this->pack = $pack;
return $this;
}
public function hasPack(): bool
{
return !is_null($this->pack);
}
public function setPayment(Payment $payment)
{
$this->payment = $payment;
$this->addPayment($payment);
}
public function getPayment(): ?Payment
{
return $this->payment;
}
public function isLastPaymentConfirmed(): bool
{
return !is_null($this->payment) && $this->payment->isConfirmed();
}
public function getLineItems(): array
{
return [$this->pack];
}
/**
* @return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* @param mixed $user
*/
public function setUser($user): void
{
$this->user = $user;
}
public function getInvoice(): ?Invoice
{
return $this->invoice;
}
public function setInvoice(?Invoice $invoice): self
{
$this->invoice = $invoice;
return $this;
}
public function getFreelancer(): ?Freelancer
{
return $this->freelancer;
}
public function setFreelancer(?Freelancer $freelancer): self
{
$this->freelancer = $freelancer;
return $this;
}
/**
* @return mixed
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* @param mixed $createdAt
*/
public function setCreatedAt($createdAt): void
{
$this->createdAt = $createdAt;
}
public function getCentralPurchasing(): ?CentralPurchasing
{
return $this->centralPurchasing;
}
public function setCentralPurchasing(?CentralPurchasing $centralPurchasing): self
{
$this->centralPurchasing = $centralPurchasing;
return $this;
}
/**
* return the owner of the order: freelancer or centralPurchasing
* @return |null
*/
public function getOwner()
{
if ($this->freelancer) {
return $this->freelancer;
} elseif ($this->centralPurchasing) {
return $this->centralPurchasing;
}
return null;
}
public function getCreditCosts(): Collection
{
return $this->creditCosts;
}
public function addCreditCost(CreditCost $creditCost): void
{
$creditCost->setOrder($this);
$this->creditCosts->add($creditCost);
}
public function hasCreditCost(): bool
{
return $this->creditCosts->count() > 0;
}
public function getStoreIndexations(): Collection
{
return $this->storeIndexations;
}
public function addStoreIndexation(StoreIndexation $storeIndexation): void
{
$storeIndexation->setOrder($this);
$this->storeIndexations->add($storeIndexation);
}
public function hasStoreIndexation(): bool
{
return $this->storeIndexations->count() > 0;
}
/**
* @Assert\Callback()
*/
public function validate(ExecutionContextInterface $context, $payload)
{
// if anything but single, situation date must be filled
if (
(is_null($this->getCentralPurchasing()) && is_null($this->getFreelancer())) ||
(!is_null($this->getCentralPurchasing()) && !is_null($this->getFreelancer()))
) {
$context->buildViolation('Vous devez spécifier un indépendant OU une central d\'achat')
->atPath('centralPurchasing')
->addViolation();
}
}
public function isPaid(): bool
{
return $this->payment && $this->payment->isConfirmed();
}
public function getPayments(): Collection
{
return $this->payments;
}
public function addPayment(Payment $payment): void
{
$payment->setOrder($this);
$this->payments->add($payment);
}
public function hasPayment(): bool
{
return $this->payments->count() > 0;
}
}