<?php
namespace App\Entity;
use App\Enum\OrderEnum;
use App\Enum\PaymentEnum;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
/**
* @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
* @ORM\Table(name="`order`")
* @ORM\EntityListeners({
* "App\Listener\EntityListener",
* "App\Listener\OrderListener"
* })
*/
class Order
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $number;
/**
* @ORM\Column(type="string")
*/
private $hash;
/**
* @ORM\Column(type="datetime")
*/
private $createts;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $editts;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*/
private $editUser;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Client",cascade={"persist"}, inversedBy="orders")
* @ORM\JoinColumn(nullable=true)
*/
private $client;
/**
* @ORM\OneToOne(targetEntity=OrderAddress::class, cascade={"persist"})
*/
private $billingAddress;
/**
* @ORM\OneToOne(targetEntity=OrderAddress::class, cascade={"persist"})
*/
private $deliveryAddress;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Payment")
* @ORM\JoinColumn(nullable=false)
*/
private $payment;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Transport")
* @ORM\JoinColumn(nullable=false)
*/
private $transport;
/**
* @ORM\Column(type="string", length=3)
*/
private $currency;
/**
* @ORM\Column(type="string", length=2)
*/
private $locale;
/**
* @ORM\Column(type="float")
*/
private $priceSum;
/**
* @ORM\Column(type="float")
*/
private $priceSumNoTax;
/**
* @ORM\Column(type="float")
*/
private $pricePayment;
/**
* @ORM\Column(type="float")
*/
private $priceTransport;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $note;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $stornoNote;
/**
* @ORM\Column(type="integer", length=2)
*/
private $status;
/**
* @ORM\OneToMany(targetEntity="App\Entity\OrderItem",cascade={"all"}, mappedBy="order", orphanRemoval=true)
*/
private $orderItems;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Dealer", inversedBy="orders")
* @ORM\JoinColumn(nullable=false)
*/
private $dealer;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $onlineCounter = 0;
/**
* @ORM\Column(type="boolean")
*/
private $paid = false;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $paidts;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $sentts;
/**
* čas přijetí do ticu
* @ORM\Column(type="datetime", nullable=true)
*/
private $ticts;
/**
* @ORM\Column(type="boolean")
*/
private $needSendEmail = false;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $sendedEmailts;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $exportts;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Domain")
* @ORM\JoinColumn(nullable=true)
*/
private $domain;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $stornots;
/**
* @ORM\OneToMany(targetEntity=OrderLog::class,cascade={"all"}, mappedBy="order", orphanRemoval=true)
* @ORM\OrderBy({ "createts" = "DESC"})
*/
private $logs;
/**
* @ORM\OneToMany(targetEntity=Reclamation::class, mappedBy="order", orphanRemoval=true)
*/
private $reclamations;
private $linkGateway;
/**
* @Serializer\Groups({"excalibur"})
* @Serializer\VirtualProperty
* @Serializer\Inline
*/
public function excalibur(){
$polozky = [];
foreach ($this->getOrderItems() as $orderItem) {
$polozky[]=[
'CenaSDPH' => $orderItem->getPrice(),
'Nazev'=> $orderItem->getName(),
'IDKarty'=> $orderItem->getVariant()->getCode(),
'Mnozstvi'=> $orderItem->getCount(),
'SazbaDPH'=> $orderItem->getTax(),
'Typ'=> 'item'
];
}
return [
'IdObjednavky' => $this->getNumber(),
'Stav' => 0,
'Prijato' => $this->getCreatets()->format('Y-m-d H:i:s'),
'Mena' => $this->getCurrency(),
'Cislo' => $this->getNumber(),
'FakCisloZak' => '',
'FakJmeno' => $this->getBillingAddress()->getFullName(),
'FakEmail' => $this->getBillingAddress()->getEmail(),
'FakTelefon' => $this->getBillingAddress()->getPhone(),
'FakUlice' => $this->getBillingAddress()->getStreet(),
'FakCisloOrient' => $this->getBillingAddress()->getStreetNumber(),
'FakCisloPopis' => '',
'FakMisto' => $this->getBillingAddress()->getCity(),
'FakPSC' => $this->getBillingAddress()->getZip(),
'FakStat' => strtoupper($this->getBillingAddress()->getCountry()),
'DodCisloZak' => '',
'DodJmeno' => $this->getDeliveryAddress() !== null?$this->getDeliveryAddress()->getFullName():'',
'DodEmail' => $this->getDeliveryAddress() !== null?$this->getDeliveryAddress()->getEmail():'',
'DodTelefon' => $this->getDeliveryAddress() !== null?$this->getDeliveryAddress()->getPhone():'',
'DodUlice' => $this->getDeliveryAddress() !== null?$this->getDeliveryAddress()->getStreet():'',
'DodCisloOrient' => $this->getDeliveryAddress() !== null?$this->getDeliveryAddress()->getStreetNumber():'',
'DodCisloPopis' => '',
'DodMisto' => $this->getDeliveryAddress() !== null?$this->getDeliveryAddress()->getCity():'',
'DodPSC' => $this->getDeliveryAddress() !== null?$this->getDeliveryAddress()->getZip():'',
'DodStat' => $this->getDeliveryAddress() !== null?strtoupper($this->getDeliveryAddress()->getCountry()):'',
'DodExternalID' => '',
'Poznamka' => $this->getNote(),
'DatumDodani' => '',
'CisloFaktury' => '',
'Paid' => $this->getPaid(),
'Polozky'=>$polozky,
'Doprava'=> [
'ZpDopravy' => $this->getTransport()->getCode(),
'Mnozstvi' => 1,
'Nazev' => $this->getTransport()->getName(),
'CenaSDPH' => 0,
'SazbaDPH' => 21
],
'Platba'=> [
'TypPlatby' => $this->getPayment()->getCode(),
'Mnozstvi' => 1,
'Nazev' => $this->getPayment()->getName(),
'CenaSDPH' => 0,
'SazbaDPH' => 21
]
];
}
public function __construct(Domain $domain, $currency)
{
$this->setDomain($domain);
$this->setLocale($domain->getLocale());
$this->setCurrency($currency);
$this->orderItems = new ArrayCollection();
$this->logs = new ArrayCollection();
$this->reclamations = new ArrayCollection();
}
public function getFormatDeliveryAddress( )
{
if($this->getDeliveryAddress() instanceof OrderAddress){
return $this->formatAddress($this->getDeliveryAddress());
}
return $this->formatAddress($this->getBillingAddress());
}
/**
* mustPaidBeforeSend - Vrátí bool zda je potřeba platba předem
*
* @return bool
*/
public function mustPaidBeforeSend( ): bool
{
return PaymentEnum::mustPaidBeforeSend($this->getPayment()->getType());
}
private function formatAddress( OrderAddress $address )
{
$out = (empty($address->getLastName())?$this->getFullName():$address->getFullName()).'<br>';
if($address->getisCompany() && !empty($address->getCompanyName())){
$out.= $address->getCompanyName().'<br>';
}
$out .= $address->getStreet().' '.$address->getStreetNumber().'<br>';
$out .= $address->getCity().'<br>';
$out .= $address->getZip().'<br>';
$out .= $address->getCountry().'<br>';
$out .= $address->getPhone();
if(!empty($address->getNote())){
$out.='<br><br>'.$address->getNote();
}
return $out;
}
/**
* initCart - Vytvoření objednávky z košíku po odeslání kroku 3
*
* @param Cart $cart
*
* @return $this
*/
public function initCart( Cart $cart)
{
$this->setClient($cart->getClient());
$this->setTransport($cart->getTransport());
$this->setPayment($cart->getPayment());
//$this->setLocale(\Locale::getDefault());
$this->setPriceTransport($cart->getTransportPrice());
$this->setPricePayment($cart->getPaymentPrice());
$this->setNote($cart->getNote());
$this->setDealer($cart->getDealer());
$this->setBillingAddress($cart->getBillingAddress()->convertToOrder());
if($cart->getHasDeliveryAddress()) {
$this->setDeliveryAddress( $cart->getDeliveryAddress()->convertToOrder() );
}
foreach ($cart->getCartItems() as $cartItem){
$item = new OrderItem();
$item->setCount($cartItem->getCount());
$item->setProduct($cartItem->getProduct());
$item->setVariant($cartItem->getVariant());
$item->setName(sprintf('%s - %s',$cartItem->getProduct()->getName(),$cartItem->getVariant()->getName()));
$item->setDealer($cartItem->getProduct()->getDealer());
$item->setCourse($cartItem->getProduct()->getDealer()->getCourse());
$pricelist = $cartItem->getProduct()->getPrice($this->getCurrency());
$item->setPrice($pricelist->getPrice());
$item->setTax($pricelist->getTax());
$item->setCurrency($this->getCurrency());
$this->addOrderItem($item);
}
if($this->mustPaidBeforeSend()){
$this->setStatus(OrderEnum::IN_PAYMENT);
}else if($this->getTransport()->getisPersonal()){
$this->setStatus(OrderEnum::TO_PICK);
}else {
$this->setStatus(OrderEnum::TO_SEND);
}
return $this;
}
public function setDefaultLocale( $locale )
{
foreach ( $this->getOrderItems() as $orderItem ) {
$orderItem->getProduct()->setDefaultLocale($locale);
}
$this->getTransport()->setDefaultLocale($locale);
$this->getPayment()->setDefaultLocale($locale);
}
public function getId(): ?int
{
return $this->id;
}
public function getNumber(): ?int
{
return $this->number;
}
public function setNumber(int $number): self
{
$this->number = $number;
return $this;
}
public function getCreatets(): ?\DateTimeInterface
{
return $this->createts;
}
public function setCreatets(\DateTimeInterface $createts): self
{
$this->createts = $createts;
return $this;
}
public function getEditts(): ?\DateTimeInterface
{
return $this->editts;
}
public function setEditts(?\DateTimeInterface $editts): self
{
$this->editts = $editts;
return $this;
}
public function getEditUser(): ?User
{
return $this->editUser;
}
public function setEditUser(?User $editUser): self
{
$this->editUser = $editUser;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
$this->client = $client;
return $this;
}
public function initClient(?Client $client): self
{
if($client instanceof Client) {
$this->setClient( $client );
/*$this->setClientFirstName( $client->getFirstName() );
$this->setClientLastName( $client->getLastName() );
$this->setClientEmail( $client->getEmail() );*/
if($client->getBillingAddress() instanceof ClientAddress){
$billing = $client->getBillingAddress();
}else{
$billing = $client->getClientAddresses()->first();
}
$billingAddress = new OrderAddress();
$billingAddress->init( $billing );
$this->setBillingAddress($billingAddress);
if($client->getDeliveryAddress() instanceof ClientAddress){
$delivery = $client->getDeliveryAddress();
}else{
$delivery = $billing;
}
$deliveryAddress = new OrderAddress();
$deliveryAddress->init( $delivery );
$this->setDeliveryAddress($deliveryAddress);
}
return $this;
}
public function getPayment(): Payment
{
return $this->payment;
}
public function setPayment(?Payment $payment): self
{
$this->payment = $payment;
return $this;
}
public function getTransport():Transport
{
return $this->transport;
}
public function setTransport(?Transport $transport): self
{
$this->transport = $transport;
return $this;
}
public function getCurrency(): ?string
{
return $this->currency;
}
public function setCurrency(string $currency): self
{
$this->currency = $currency;
return $this;
}
public function getPriceSum(): ?float
{
return $this->priceSum;
}
public function setPriceSum(float $priceSum): self
{
$this->priceSum = $priceSum;
return $this;
}
public function getPriceSumNoTax(): ?float
{
return $this->priceSumNoTax;
}
public function setPriceSumNoTax(float $priceSumNoTax): self
{
$this->priceSumNoTax = $priceSumNoTax;
return $this;
}
public function getPricePayment(): ?float
{
return $this->pricePayment;
}
public function setPricePayment(float $pricePayment): self
{
$this->pricePayment = $pricePayment;
return $this;
}
public function getPriceTransport(): ?float
{
return $this->priceTransport;
}
public function setPriceTransport(float $priceTransport): self
{
$this->priceTransport = $priceTransport;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
/**
* @return Collection|OrderItem[]
*/
public function getOrderItems(): Collection
{
return $this->orderItems;
}
public function addOrderItem(OrderItem $orderItem): self
{
if (!$this->orderItems->contains($orderItem)) {
$this->orderItems[] = $orderItem;
$orderItem->setOrder($this);
}
return $this;
}
public function removeOrderItem(OrderItem $orderItem): self
{
if ($this->orderItems->contains($orderItem)) {
$this->orderItems->removeElement($orderItem);
// set the owning side to null (unless already changed)
if ($orderItem->getOrder() === $this) {
$orderItem->setOrder(null);
}
}
return $this;
}
public function getCountOrderItems(): int
{
$sum = 0;
foreach ( $this->getOrderItems() as $orderItem ) {
$sum += $orderItem->getCount();
}
return $sum;
}
/**
* Get Note
*
* @return mixed
*/
public function getNote()
{
return $this->note;
}
/**
* Set Note
*
* @param mixed $note
*/
public function setNote( $note )
{
$this->note = $note;
}
/**
* Get Paid
*
* @return mixed
*/
public function getPaid()
{
return $this->paid;
}
/**
* Set Paid
*
* @param mixed $paid
*/
public function setPaid( $paid )
{
$this->paid = $paid;
}
/**
* Get Paidts
*
* @return mixed
*/
public function getPaidts()
{
return $this->paidts;
}
/**
* Set Paidts
*
* @param mixed $paidts
*/
public function setPaidts( $paidts )
{
$this->paidts = $paidts;
}
/**
* Get Sentts
*
* @return mixed
*/
public function getSentts()
{
return $this->sentts;
}
/**
* Set Sentts
*
* @param mixed $sentts
*/
public function setSentts( $sentts )
{
$this->sentts = $sentts;
}
/**
* Get BillingAddress
*
* @return mixed
*/
public function getBillingAddress(): ?OrderAddress
{
return $this->billingAddress;
}
/**
* Set BillingAddress
*
* @param mixed $billingAddress
*/
public function setBillingAddress( $billingAddress )
{
$this->billingAddress = $billingAddress;
}
/**
* Get DeliveryAddress
*
* @return mixed
*/
public function getDeliveryAddress()
{
return $this->deliveryAddress;
}
/**
* Set DeliveryAddress
*
* @param mixed $deliveryAddress
*/
public function setDeliveryAddress( $deliveryAddress )
{
$this->deliveryAddress = $deliveryAddress;
}
/**
* Get Domain
*
* @return mixed
*/
public function getDomain()
{
return $this->domain;
}
/**
* Set Domain
*
* @param mixed $domain
*/
public function setDomain( $domain )
{
$this->domain = $domain;
}
/**
* Get NeedSendEmail
*
* @return mixed
*/
public function getNeedSendEmail()
{
return $this->needSendEmail;
}
/**
* Set NeedSendEmail
*
* @param mixed $needSendEmail
*/
public function setNeedSendEmail( $needSendEmail )
{
$this->needSendEmail = $needSendEmail;
}
/**
* Get SendedEmailts
*
* @return mixed
*/
public function getSendedEmailts()
{
return $this->sendedEmailts;
}
/**
* Set SendedEmailts
*
* @param mixed $sendedEmailts
*/
public function setSendedEmailts( $sendedEmailts )
{
$this->sendedEmailts = $sendedEmailts;
}
/**
* Get Locale
*
* @return mixed
*/
public function getLocale()
{
return $this->locale;
}
/**
* Set Locale
*
* @param mixed $locale
*/
public function setLocale( $locale )
{
$this->locale = $locale;
}
/**
* Get Stornots
*
* @return mixed
*/
public function getStornots()
{
return $this->stornots;
}
/**
* Set Stornots
*
* @param mixed $stornots
*/
public function setStornots( $stornots )
{
$this->stornots = $stornots;
}
/**
* Get Dealer
*
* @return mixed
*/
public function getDealer(): Dealer
{
return $this->dealer;
}
/**
* Set Dealer
*
* @param mixed $dealer
*/
public function setDealer( $dealer )
{
$this->dealer = $dealer;
}
/**
* @return Collection|OrderLog[]
*/
public function getLogs(): Collection
{
return $this->logs;
}
public function addLog(OrderLog $log): self
{
if (!$this->logs->contains($log)) {
$this->logs[] = $log;
$log->setOrder2($this);
}
return $this;
}
public function removeLog(OrderLog $log): self
{
if ($this->logs->removeElement($log)) {
// set the owning side to null (unless already changed)
if ($log->getOrder2() === $this) {
$log->setOrder2(null);
}
}
return $this;
}
/**
* Get Exportts
*
* @return mixed
*/
public function getExportts()
{
return $this->exportts;
}
/**
* Set Exportts
*
* @param mixed $exportts
*/
public function setExportts( $exportts )
{
$this->exportts = $exportts;
}
/**
* Get OnlineCounter
*
* @return mixed
*/
public function getOnlineCounter()
{
return $this->onlineCounter;
}
/**
* Set OnlineCounter
*
* @param mixed $onlineCounter
*/
public function setOnlineCounter( $onlineCounter )
{
$this->onlineCounter = $onlineCounter;
}
/**
* @return mixed
*/
public function getTicts()
{
return $this->ticts;
}
/**
* @param mixed $ticts
*/
public function setTicts($ticts): void
{
$this->ticts = $ticts;
}
/**
* @return Collection|Reclamation[]
*/
public function getReclamations(): Collection
{
return $this->reclamations;
}
public function addReclamation(Reclamation $reclamation): self
{
if (!$this->reclamations->contains($reclamation)) {
$this->reclamations[] = $reclamation;
$reclamation->setOrder($this);
}
return $this;
}
public function removeReclamation(Reclamation $reclamation): self
{
if ($this->reclamations->removeElement($reclamation)) {
// set the owning side to null (unless already changed)
if ($reclamation->getOrder() === $this) {
$reclamation->setOrder(null);
}
}
return $this;
}/*
public function createHash( )
{
return base64_encode(sprintf('%s|%s|%s',$this->getBillingAddress()->getEmail(),$this->getNumber(),$this->getId()));
}*/
/**
* @return mixed
*/
public function getHash()
{
return $this->hash;
}
/**
* @param mixed $hash
*/
public function setHash($hash): void
{
$this->hash = $hash;
}
/**
* @return mixed
*/
public function getLinkGateway()
{
return $this->linkGateway;
}
/**
* @param mixed $linkGateway
*/
public function setLinkGateway($linkGateway): void
{
$this->linkGateway = $linkGateway;
}
/**
* @return mixed
*/
public function getStornoNote()
{
return $this->stornoNote;
}
/**
* @param mixed $stornoNote
*/
public function setStornoNote($stornoNote): void
{
$this->stornoNote = $stornoNote;
}
}