<?php
declare(strict_types=1);
namespace App\Entity\Order;
use App\Entity\Boutique;
use App\Entity\Giftcard;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\Order as BaseOrder;
use Sylius\Component\Core\Model\Adjustment as BaseAdjustment;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Order\Model\AdjustmentInterface as BaseAdjustmentInterface;
use App\Service\FideliumService;
/**
* @ORM\Entity
* @ORM\Table(name="sylius_order")
*/
class Order extends BaseOrder
{
/** @ORM\Column(type="float", nullable=true) */
private $fideliumpoints;
public function getFideliumpoints(): ?float
{
return $this->fideliumpoints;
}
public function setFideliumpoints(float $fideliumpoints): void
{
$this->fideliumpoints = $fideliumpoints;
}
/**
* @ORM\ManyToMany(targetEntity=Giftcard::class, inversedBy="orders")
*/
private $giftCards;
/**
* @ORM\ManyToOne(targetEntity=Boutique::class, inversedBy="orders")
*/
private $boutique;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $invoice_number;
public function __construct()
{
parent::__construct();
$this->giftCards = new ArrayCollection();
}
/*public function hasFidelium ()
{
$ajustements = $this->getFideliumAdjustments ();
if (
array_key_exists("Fidelium", $ajustements)
and
!empty($ajustements["Fidelium"])
)
return true;
else
return false;
}*/
public function getTotalWithOutAdjustments()
{
$fidelium_Adjustment_amount = 0;
$fidelium_Adjustments = $this->getFidelium ();
if ($fidelium_Adjustments && array_key_exists("amount", $fidelium_Adjustments))
$fidelium_Adjustment_amount = $fidelium_Adjustments["amount"];
$amount = $this->getItemsTotal() - (int)$fidelium_Adjustment_amount;
return $amount;
}
public function getFidelium ()
{
$ajustements = $this->getFideliumAdjustments ();
if (
array_key_exists("Fidelium", $ajustements)
and
!empty($ajustements["Fidelium"])
and
array_key_exists("amount", $ajustements["Fidelium"])
and
array_key_exists("points", $ajustements["Fidelium"])
and
(int)$ajustements["Fidelium"]["amount"] != 0
)
{
$ajustements["points"] = (int)$ajustements["Fidelium"]["points"];
return $ajustements["Fidelium"];
}
else
return false;
}
public function getFideliumAdjustments ()
{
$points = 0;
$total = 0;
$k=0;
$res = array();
$res["promo"] = array("amount" => 0);
$res["Fidelium"] = array("amount" => 0, "points" =>0);
$adjs = $this->getAdjustmentsRecursively(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT);
foreach ($adjs as $key => $ad){
//$details = $ad->getDetails();
$label = $ad->getLabel();
if ($label=="Fidelium")
{
if ($k=0)
{
$res["Fidelium"]["amount"] = 0;
}
$res["Fidelium"]["amount"] = $res["Fidelium"]["amount"] + $ad->getAmount();
//$res["Fidelium"]["points"] = $points;
$total = $total - $ad->getAmount();
$k++;
}
else
{
$res["promo"]["amount"] = $res["promo"]["amount"] + $ad->getAmount();
}
}
if ($res["Fidelium"]["amount"] != 0)
{
$customer = $this->getCustomer();
if (isset($customer))
{
$cardnumber = $customer->getFideliumcard();
if ($cardnumber && $cardnumber!="")
{
$fs = new FideliumService ($cardnumber);
$points = $fs->correspondanceMontantsPoints ( $res["Fidelium"]["amount"]);
$res["Fidelium"]["points"] = $points ;
}
}
}
return $res;
}
public function getDispoProduct ()
{
$DatedispofromMin = null;
$DatedispotoMax = null;
foreach ($this->getItems() as $item){
$product = $item->getProduct();
$Datedispofrom = $product->getDatedispofrom();
$Datedispoto = $product->getDatedispoto();
if (isset($Datedispofrom))
{
if (!isset($DatedispofromMin))
$DatedispofromMin = $Datedispofrom;
elseif ($Datedispofrom > $DatedispofromMin)
$DatedispofromMin = $Datedispofrom;
}
if (isset($Datedispoto))
{
if (!isset($DatedispotoMax))
$DatedispotoMax = $Datedispoto;
elseif ($Datedispoto < $DatedispotoMax)
$DatedispotoMax = $Datedispoto;
}
}
$DatedispofromMin_str = "";
$DatedispotoMax_str = "";
if (isset($DatedispofromMin))
$DatedispofromMin_str = $DatedispofromMin->format('Y-m-d');
if (isset($DatedispotoMax))
$DatedispotoMax_str = $DatedispotoMax->format('Y-m-d');
return '{"from": "' . $DatedispofromMin_str . '","to": "' . $DatedispotoMax_str . '"}';
}
public function getTaxes ()
{
$res = array();
$taxes = $this->getAdjustmentsRecursively(AdjustmentInterface::TAX_ADJUSTMENT);
foreach ($taxes as $key => $taxe){
$details = $taxe->getDetails();
$taxRateName = $details["taxRateName"];
if (array_key_exists($taxRateName, $res ))
$res[$taxRateName] = $res[$taxRateName] + $taxe->getAmount();
else
$res[$taxRateName] = $taxe->getAmount();
}
return $res;
}
public function hasGiftCards(): bool
{
return !$this->giftCards->isEmpty();
}
/**
* @return Collection|Giftcard[]
*/
public function getGiftCards(): Collection
{
return $this->giftCards;
}
public function addGiftCard(Giftcard $giftCard): self
{
if (!$this->giftCards->contains($giftCard)) {
$this->giftCards[] = $giftCard;
}
return $this;
}
public function removeGiftCard(Giftcard $giftCard): self
{
$this->giftCards->removeElement($giftCard);
return $this;
}
public function getBoutique(): ?Boutique
{
return $this->boutique;
}
public function setBoutique(?Boutique $boutique): self
{
$this->boutique = $boutique;
return $this;
}
public function getInvoiceNumber(): ?string
{
return $this->invoice_number;
}
public function setInvoiceNumber(?string $invoice_number): self
{
$this->invoice_number = $invoice_number;
return $this;
}
}