<?php
namespace App\Entity;
use App\Repository\OrderRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=OrderRepository::class)
* @ORM\Table(name="`order`")
*/
class Order
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="orders")
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity=Shop::class, inversedBy="orders")
*/
private $shop;
/**
* @ORM\OneToMany(targetEntity=Cart::class, mappedBy="order")
*/
private $carts;
/**
* @ORM\Column(type="string", length=64, nullable=true)
*/
private $reference;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private $coupon;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $customReference;
/**
* @ORM\ManyToOne(targetEntity=Address::class, cascade={"persist"})
*/
private $deliveryAddress;
/**
* @ORM\ManyToOne(targetEntity=Address::class, cascade={"persist"})
*/
private $invoiceAddress;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $confirm;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $acceptTerms;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $totalProduct;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $deliveryHt;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $deliveryTotal;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $amountHt;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $amountTotal;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $discount;
/**
* @ORM\Column(type="decimal", precision=4, scale=2, nullable=true)
*/
private $vat;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $date;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $b2lId;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private $bak2Reference;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private $shippingNumber;
/**
* @ORM\ManyToOne(targetEntity=Status::class, inversedBy="orders")
*/
private $status;
/**
* @ORM\OneToOne(targetEntity=Imei::class, mappedBy="order", cascade={"persist", "remove"})
*/
private $imeis;
/**
* @ORM\OneToOne(targetEntity=Address::class, cascade={"persist", "remove"})
*/
private $tempAddress;
/**
* @ORM\Column(type="string", length=16, nullable=true)
*/
private $payment;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private $paymentReference;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $comment;
/**
* @ORM\ManyToMany(targetEntity=Option::class, mappedBy="order", cascade={"persist"})
*/
private $options;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $optionPrice;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $invoiceFilename;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $active;
/**
* @ORM\OneToMany(targetEntity=Sav::class, mappedBy="order")
*/
private $savs;
private $displayName;
public function __construct()
{
$this->carts = new ArrayCollection();
$this->status = new ArrayCollection();
$this->options = new ArrayCollection();
$this->savs = new ArrayCollection();
}
public function __toString() {
return $this->getReference();
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getShop(): ?Shop
{
if($this->shop==null) {
$this->shop=$this->getUser()->getShop();
}
return $this->shop;
}
public function setShop(?Shop $shop): self
{
$this->shop = $shop;
return $this;
}
/**
* @return Collection<int, Cart>
*/
public function getCarts(): Collection
{
return $this->carts;
}
public function addCart(Cart $cart): self
{
if (!$this->carts->contains($cart)) {
$this->carts[] = $cart;
$cart->setOrder($this);
}
return $this;
}
public function removeCart(Cart $cart): self
{
if ($this->carts->removeElement($cart)) {
// set the owning side to null (unless already changed)
if ($cart->getOrder() === $this) {
$cart->setOrder(null);
}
}
return $this;
}
public function nbProducts() {
$carts=$this->getCarts();
$qty=0;
foreach($carts as $cart) {
$qty+=$cart->getQty();
}
return $qty;
}
public function totalAmount() {
$carts=$this->getCarts();
$total=0;
foreach($carts as $cart) {
$total+=($cart->getQty() * $cart->getPrice());
}
return $total/100;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getCustomReference(): ?string
{
return $this->customReference;
}
public function setCustomReference(string $customReference): self
{
$this->customReference = $customReference;
return $this;
}
public function getDeliveryAddress(): ?Address
{
return $this->deliveryAddress;
}
public function setDeliveryAddress(?Address $deliveryAddress): self
{
$this->deliveryAddress = $deliveryAddress;
return $this;
}
public function getInvoiceAddress(): ?Address
{
return $this->invoiceAddress;
}
public function setInvoiceAddress(?Address $invoiceAddress): self
{
$this->invoiceAddress = $invoiceAddress;
return $this;
}
public function getConfirm(): ?bool
{
return $this->confirm;
}
public function setConfirm(?bool $confirm): self
{
$this->confirm = $confirm;
return $this;
}
public function getAcceptTerms(): ?bool
{
return $this->acceptTerms;
}
public function setAcceptTerms(?bool $acceptTerms): self
{
$this->acceptTerms = $acceptTerms;
return $this;
}
public function getTotalProduct(): ?int
{
return $this->totalProduct;
}
public function setTotalProduct(?int $totalProduct): self
{
$this->totalProduct = $totalProduct;
return $this;
}
public function getDeliveryHt(): ?int
{
return $this->deliveryHt;
}
public function setDeliveryHt(?int $deliveryHt): self
{
$this->deliveryHt = $deliveryHt;
return $this;
}
public function getDeliveryTotal(): ?int
{
return $this->deliveryTotal;
}
public function setDeliveryTotal(?int $deliveryTotal): self
{
$this->deliveryTotal = $deliveryTotal;
return $this;
}
public function getAmountHt(): ?int
{
return $this->amountHt;
}
public function setAmountHt(?int $amountHt): self
{
$this->amountHt = $amountHt;
return $this;
}
public function getAmountTotal(): ?int
{
return $this->amountTotal;
}
public function setAmountTotal(?int $amountTotal): self
{
$this->amountTotal = $amountTotal;
return $this;
}
public function getVat(): ?string
{
return $this->vat;
}
public function setVat(?string $vat): self
{
$this->vat = $vat;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getB2lId(): ?int
{
return $this->b2lId;
}
public function setB2lId(?int $b2lId): self
{
$this->b2lId = $b2lId;
return $this;
}
public function generateReference() {
return "B-".$this->getUser()->getId()."-".$this->getId();
/*$date=new \DateTime('now');
return 'P-'.$date->format('YmdHis')."-".rand(0,1000);
*/
}
public function getBak2Reference(): ?string
{
return $this->bak2Reference;
}
public function setBak2Reference(?string $bak2Reference): self
{
$this->bak2Reference = $bak2Reference;
return $this;
}
public function getShippingNumber(): ?string
{
return $this->shippingNumber;
}
public function setShippingNumber(?string $shippingNumber): self
{
$this->shippingNumber = $shippingNumber;
return $this;
}
public function getStatus(): ?Status
{
return $this->status;
}
public function setStatus(?Status $status): self
{
$this->status = $status;
return $this;
}
public function getImeis(): ?Imei
{
return $this->imeis;
}
public function setImeis(?Imei $imeis): self
{
// unset the owning side of the relation if necessary
if ($imeis === null && $this->imeis !== null) {
$this->imeis->setOrder(null);
}
// set the owning side of the relation if necessary
if ($imeis !== null && $imeis->getOrder() !== $this) {
$imeis->setOrder($this);
}
$this->imeis = $imeis;
return $this;
}
public function getTempAddress(): ?Address
{
return $this->tempAddress;
}
public function setTempAddress(?Address $tempAddress): self
{
$this->tempAddress = $tempAddress;
return $this;
}
public function getPayment(): ?string
{
return $this->payment;
}
public function setPayment(string $payment): self
{
$this->payment = $payment;
return $this;
}
public function getPaymentReference(): ?string
{
return $this->paymentReference;
}
public function setPaymentReference(string $paymentReference): self
{
$this->paymentReference = $paymentReference;
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(?string $comment): self
{
$this->comment = $comment;
return $this;
}
public function isConfirm(): ?bool
{
return $this->confirm;
}
public function getCoupon(): ?string
{
return $this->coupon;
}
public function setCoupon(?string $coupon): static
{
$this->coupon = $coupon;
return $this;
}
public function setDiscount($discount) {
$this->discount = $discount;
}
public function getDiscount()
{
return $this->discount;
}
public function applyCoupon() {
$coupon=$this->getCoupon();
$totalAmount=$this->getAmountTotal();
if($coupon) {
switch($coupon->getType()) {
case "percent":
default:
$this->setAmountTotal($totalAmount - ($totalAmount * $coupon->getValue()/100));
break;
}
}
}
public function isAcceptTerms(): ?bool
{
return $this->acceptTerms;
}
/**
* @return Collection<int, Option>
*/
public function getOptions(): Collection
{
return $this->options;
}
public function addOption(Option $option): static
{
if (!$this->options->contains($option)) {
$this->options->add($option);
$option->addOrder($this);
}
return $this;
}
public function removeOption(Option $option): static
{
if ($this->options->removeElement($option)) {
$option->removeOrder($this);
}
return $this;
}
public function hasOption($option): bool
{
foreach($this->options as $o) {
if($o->getId()==$option->getId()) {
return true;
}
}
return false;
}
public function getOptionPrice(): ?int
{
return $this->optionPrice;
}
public function setOptionPrice(?int $optionPrice): static
{
$this->optionPrice = $optionPrice;
return $this;
}
public function getInvoiceFilename(): ?string
{
return $this->invoiceFilename;
}
public function setInvoiceFilename(string $invoiceFilename): self
{
$this->invoiceFilename = $invoiceFilename;
return $this;
}
public function getActive():bool
{
return $this->active;
}
public function setActive(bool $active):self
{
$this->active = $active;
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function getDisplayName() {
return $this->getReference()." - ".$this->getDate()->format('d/m/Y');//. " (".$this->getStatus().")";
}
/**
* @return Collection<int, Sav>
*/
public function getSavs(): Collection
{
return $this->savs;
}
public function addSav(Sav $sav): static
{
if (!$this->savs->contains($sav)) {
$this->savs->add($sav);
$sav->setOrder($this);
}
return $this;
}
public function removeSav(Sav $sav): static
{
if ($this->savs->removeElement($sav)) {
// set the owning side to null (unless already changed)
if ($sav->getOrder() === $this) {
$sav->setOrder(null);
}
}
return $this;
}
}