<?php
namespace App\Entity\Order;
use App\Entity\PaymentMethod\PaymentMethod;
use App\Entity\ProduitCarrier;
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;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Doctrine\ORM\Event\PrePersistEventArgs;
use App\Entity\Promo\Promo;
use App\Entity\Address\{BillingAddress, ShippingAddress};
use App\Entity\Customer;
use App\Entity\Carrier\Carrier;
use App\EventListener\OrderListener;
use App\Entity\Produit\Produit;
#[ORM\Entity(repositoryClass: OrderRepository::class)]
#[ORM\Table(name: '`order`')]
#[ORM\HasLifecycleCallbacks]
class Order
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'datetime')]
private $createdAt;
#[ORM\Column(type: 'datetime')]
private $updatedAt;
#[ORM\Column(type: 'integer')]
private $state;
#[ORM\Column(type: 'decimal', precision: 10, scale: 2, nullable:true)]
private $totalPriceHt;
#[ORM\Column(type: 'decimal', precision: 10, scale: 2, nullable:true)]
private $totalVat;
#[ORM\Column(type: 'decimal', precision: 10, scale: 2, nullable:true)]
private $totalPrice;
#[ORM\ManyToOne(targetEntity: Customer::class, inversedBy: 'orders')]
private $customer;
#[ORM\ManyToOne(targetEntity: BillingAddress::class, inversedBy: 'ordersBilling', cascade: ['persist'])]
private $billingAddress;
#[ORM\ManyToOne(targetEntity: ShippingAddress::class, inversedBy: 'ordersShipping', cascade: ['persist'])]
private $shippingAddress;
#[ORM\OneToMany(mappedBy: 'orderentity', targetEntity: OrderLine::class, orphanRemoval: true , cascade: ["persist", "remove"])]
private Collection $orderLines;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $discount = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $discountType = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalWithoutDiscount = null;
#[ORM\Column(type: 'string', length: 128, unique: true, nullable:true)]
private ?string $numOrder = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
private ?Promo $promo = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $datePayment = null;
#[ORM\Column(nullable: true)]
private ?bool $acceptCommunication = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
private ?Carrier $carrier = null;
#[ORM\ManyToOne(inversedBy: 'orders')]
private ?PaymentMethod $paymentMethod = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalCarrierWithTaxes = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalCarrierWithoutTaxes = null;
#[ORM\Column(length: 128, unique: true, nullable:true)]
private $slug;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalDiscountHt = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalDiscountTtc = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalPriceTtc = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalPriceTtcFinal = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalPriceHtFinal = null;
#[ORM\Column(nullable: true)]
private ?int $typeCommand = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $email = null;
#[ORM\Column(nullable: true)]
private ?int $number = null;
#[ORM\OneToMany(mappedBy: 'ord', targetEntity: Payment::class)]
private Collection $payments;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $amountDue = null;
const STATE_PAID = 0 ;
const STATE_VALIDATE = 1 ;
const STATE_NO_PAID = 2 ;
const STATE_REFUSED = 3 ;
const STATE_CANCELED = 4 ;
const STATE_REFUND = 5;
const STATE_CART = 6;
const STATE_PREP = 7;
const STATE_SEND = 8;
const DISCOUNT_TYPE_PERCENTAGE = 1;
const DISCOUNT_TYPE_EURO = 2;
const TYPE_COMMAND = 0;
const TYPE_DEVIS = 1;
const DISCOUNT_TYPE_CHOICES = [
'percentage' => self::DISCOUNT_TYPE_PERCENTAGE,
'euro' => self::DISCOUNT_TYPE_EURO,
];
public function __construct()
{
$this->orderLines = new ArrayCollection();
$this->payments = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getState(): ?int
{
return $this->state;
}
public function setState(int $state): self
{
$this->state = $state;
return $this;
}
public function getTotalPriceHt(): ?string
{
return $this->totalPriceHt;
}
public function setTotalPriceHt(string $totalPriceHt): self
{
$this->totalPriceHt = $totalPriceHt;
return $this;
}
public function getTotalVat(): ?string
{
return $this->totalVat;
}
public function setTotalVat(string $totalVat): self
{
$this->totalVat = $totalVat;
return $this;
}
public function getTotalPrice(): ?string
{
return $this->totalPrice;
}
public function setTotalPrice(string $totalPrice): self
{
$this->totalPrice = $totalPrice;
return $this;
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
public function setCustomer(?Customer $customer): self
{
$this->customer = $customer;
return $this;
}
public function getBillingAddress(): ?BillingAddress
{
return $this->billingAddress;
}
public function setBillingAddress(?BillingAddress $billingAddress): self
{
$this->billingAddress = $billingAddress;
return $this;
}
public function getShippingAddress(): ?ShippingAddress
{
return $this->shippingAddress;
}
public function setShippingAddress(?ShippingAddress $shippingAddress): self
{
$this->shippingAddress = $shippingAddress;
return $this;
}
/**
* @return Collection<int, OrderLine>
*/
public function getOrderLines(): Collection
{
return $this->orderLines;
}
public function clearOrderLines(){
$this->orderLines = new ArrayCollection();
return $this;
}
public function addOrderLine(OrderLine $orderLine): self
{
if (!$this->orderLines->contains($orderLine)) {
$this->orderLines->add($orderLine);
$orderLine->setOrderentity($this);
}
return $this;
}
public function removeOrderLine(OrderLine $orderLine): self
{
if ($this->orderLines->removeElement($orderLine)) {
// set the owning side to null (unless already changed)
if ($orderLine->getOrderentity() === $this) {
$orderLine->setOrderentity(null);
}
}
return $this;
}
public function getStateText(){
if($this->getState() == '') return '';
return $this->getStates()[$this->getState()];
}
public static function getStatesChoice(){
return array_flip([
self::STATE_PAID => 'state.paid',
self::STATE_REFUSED => 'state.refused',
self::STATE_VALIDATE => 'state.validate',
self::STATE_NO_PAID => 'state.nopaid',
self::STATE_CANCELED => 'state.canceled',
self::STATE_REFUND => 'state.refund',
self::STATE_PREP => 'state.prep',
self::STATE_SEND=> 'state.send',
]);
}
public static function getStates(){
return ([
self::STATE_PAID => 'state.paid',
self::STATE_REFUSED => 'state.refused',
self::STATE_VALIDATE => 'state.validate',
self::STATE_NO_PAID => 'state.nopaid',
self::STATE_CANCELED => 'state.canceled',
self::STATE_REFUND => 'state.refund',
self::STATE_PREP => 'state.prep',
self::STATE_SEND=> 'state.send'
]);
}
public function getTypeCommandText(){
return $this->getNumOrder();
//if($this->getTypeCommand() == '') return '';
//return $this->getTypeCommand() == 0 ? "ORD ".$this->getNumOrder() : "DEV ".$this->getNumOrder();
}
public static function getTypeCommandChoice(){
return array_flip([
self::TYPE_COMMAND => 'Commande',
self::TYPE_DEVIS => 'Devis',
]);
}
public static function getTypesCommand(){
return ([
self::TYPE_COMMAND => 'typeCommand.command',
self::TYPE_DEVIS => 'typeCommand.devis',
]);
}
public function getDiscount()
{
return $this->discount;
}
public function setDiscount($discount): self
{
$this->discount = $discount;
return $this;
}
public function getDiscountType(): ?string
{
return $this->discountType;
}
public function setDiscountType(?string $discountType): self
{
$this->discountType = $discountType;
return $this;
}
public function getTotalWithoutDiscount(): ?string
{
return $this->totalWithoutDiscount;
}
public function setTotalWithoutDiscount(string $totalWithoutDiscount): self
{
$this->totalWithoutDiscount = $totalWithoutDiscount;
return $this;
}
public function getNumOrder(): ?string
{
return $this->numOrder;
}
public function setNumOrder(?string $numOrder): self
{
$this->numOrder = $numOrder;
return $this;
}
public function getPromo(): ?Promo
{
return $this->promo;
}
public function setPromo(?Promo $promo): self
{
$this->promo = $promo;
return $this;
}
public function getDatePayment(): ?\DateTimeInterface
{
return $this->datePayment;
}
public function setDatePayment(?\DateTimeInterface $datePayment): self
{
$this->datePayment = $datePayment;
return $this;
}
public function isAcceptCommunication(): ?bool
{
return $this->acceptCommunication;
}
public function setAcceptCommunication(?bool $acceptCommunication): self
{
$this->acceptCommunication = $acceptCommunication;
return $this;
}
public function getCarrier(): ?Carrier
{
return $this->carrier;
}
public function setCarrier(?Carrier $carrier): static
{
$this->carrier = $carrier;
return $this;
}
public function getPaymentMethod(): ?PaymentMethod
{
return $this->paymentMethod;
}
public function setPaymentMethod(?PaymentMethod $paymentMethod): static
{
$this->paymentMethod = $paymentMethod;
return $this;
}
public function getTotalCarrierWithTaxes(): ?string
{
return $this->totalCarrierWithTaxes;
}
public function setTotalCarrierWithTaxes(?string $totalCarrierWithTaxes): static
{
$this->totalCarrierWithTaxes = $totalCarrierWithTaxes;
return $this;
}
public function getTotalCarrierWithoutTaxes(): ?string
{
return $this->totalCarrierWithoutTaxes;
}
public function setTotalCarrierWithoutTaxes(string $totalCarrierWithoutTaxes): static
{
$this->totalCarrierWithoutTaxes = $totalCarrierWithoutTaxes;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function updateTotaux()
{
$totalHt = 0;
$totalTtc = 0;
$carrierHt = 0;
$carrierTtc = 0;
$carrier = $this->getCarrier();
if(!empty($this->getOrderLines()->toArray())){
foreach($this->getOrderLines()->toArray() as $line){
$line->updateTotalLine();
$totalHt += $line->getTotalLineHt();
$totalTtc += $line->getTotalLineTtc();
if(!empty($carrier) and !$carrier->isIsFree()){
$carrier_produit = $line->getProduit()->getProduitCarriers()->toArray();
if(!empty($carrier_produit) and array_key_exists($carrier->getId(), $carrier_produit)){
$carrierHt += $carrier_produit[$carrier->getId()]->getPriceHt();
$carrierTtc += $carrier_produit[$carrier->getId()]->getPriceTtc();
}
}
}
}
$totalTtc = $totalTtc + $carrierTtc;
$totalHt = $totalHt + $carrierHt;
$this->setTotalWithoutDiscount($totalTtc);
$totalDiscountHt = 0;
$totalDiscountTtc = 0;
//CORRIGER
if(!empty($this->getDiscountType())){
$discount = $this->getDiscount();
if($this->getDiscountType() == Order::DISCOUNT_TYPE_EURO){
$totalDiscountTtc = $discount;
$totalDiscountHt = $discount - ($discount * 0.21);
}else if($this->getDiscountType() == Order::DISCOUNT_TYPE_PERCENTAGE){
$dicountFix = ($discount/100);
$totalDiscountTtc = $totalTtc * $dicountFix;
$totalDiscountHt = $totalHt * $dicountFix;
}
}else if($this->getPromo() and !empty($this->getPromo()->getDiscount())){
$discount = $this->getPromo()->getDiscount();
$dicountFix = ($discount/100);
$totalDiscountTtc = $totalTtc * $dicountFix;
$totalDiscountHt = $totalHt * $dicountFix;
}
$this->setTotalCarrierWithoutTaxes($carrierHt);
$this->setTotalCarrierWithTaxes($carrierTtc);
$this->setTotalPriceHt($totalHt);
$this->setTotalPriceTtc($totalTtc);
$this->setTotalDiscountHt($totalDiscountHt);
$this->setTotalDiscountTtc($totalDiscountTtc);
$this->setTotalPriceTtcFinal($totalTtc-$totalDiscountTtc);
$this->setTotalPriceHtFinal($totalHt-$totalDiscountHt);
$this->setTotalVat($totalTtc- $totalHt);
}
public function getTotalProductsTtc(){
$total = 0;
foreach($this->getOrderLines()->toArray() as $line){
$total += $line->getTotalTtcProduct();
}
return $total;
}
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
public function getSlug()
{
return $this->slug;
}
public function getTotalDiscountHt(): ?string
{
return $this->totalDiscountHt;
}
public function setTotalDiscountHt(?string $totalDiscountHt): static
{
$this->totalDiscountHt = $totalDiscountHt;
return $this;
}
public function getTotalDiscountTtc(): ?string
{
return $this->totalDiscountTtc;
}
public function setTotalDiscountTtc(?string $totalDiscountTtc): static
{
$this->totalDiscountTtc = $totalDiscountTtc;
return $this;
}
public function getTotalPriceTtc(): ?string
{
return $this->totalPriceTtc;
}
public function setTotalPriceTtc(?string $totalPriceTtc): static
{
$this->totalPriceTtc = $totalPriceTtc;
return $this;
}
public function getTotalPriceTtcFinal(): ?string
{
return $this->totalPriceTtcFinal;
}
public function setTotalPriceTtcFinal(?string $totalPriceTtcFinal): static
{
$this->totalPriceTtcFinal = $totalPriceTtcFinal;
return $this;
}
public function getTotalPriceHtFinal(): ?string
{
return $this->totalPriceHtFinal;
}
public function setTotalPriceHtFinal(?string $totalPriceHtFinal): static
{
$this->totalPriceHtFinal = $totalPriceHtFinal;
return $this;
}
public function getTypeCommand(): ?int
{
return $this->typeCommand;
}
public function setTypeCommand(?int $typeCommand): static
{
$this->typeCommand = $typeCommand;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): static
{
$this->email = $email;
return $this;
}
public function getNumber(): ?int
{
return $this->number;
}
public function setNumber(?int $number): static
{
$this->number = $number;
return $this;
}
/**
* @return Collection<int, Payment>
*/
public function getPayments(): Collection
{
return $this->payments;
}
public function addPayment(Payment $payment): static
{
if (!$this->payments->contains($payment)) {
$this->payments->add($payment);
$payment->setOrd($this);
}
return $this;
}
public function removePayment(Payment $payment): static
{
if ($this->payments->removeElement($payment)) {
// set the owning side to null (unless already changed)
if ($payment->getOrd() === $this) {
$payment->setOrd(null);
}
}
return $this;
}
public function getAmountDue(): ?string
{
return $this->amountDue;
}
public function setAmountDue(?string $amountDue): static
{
$this->amountDue = $amountDue;
return $this;
}
}