src/Submarine/OrdersBundle/Entity/Order.php line 39

Open in your IDE?
  1. <?php
  2. /**
  3.  * (c) itmedia.by <info@itmedia.by>
  4.  */
  5. namespace Submarine\OrdersBundle\Entity;
  6. use Submarine\OrdersBundle\Entity\OrderB24Data;
  7. use Postroyka\AppBundle\Entity\Promocode;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Ramsey\Uuid\Uuid;
  11. use Ramsey\Uuid\UuidInterface;
  12. use Submarine\CartBundle\Cart\CartInterface;
  13. use Submarine\CartBundle\Cart\OptionInterface;
  14. use Submarine\CartBundle\Entity\Option;
  15. use Submarine\CartBundle\Exception\CartEmptyException;
  16. use Submarine\CartBundle\Exception\CartExpiredException;
  17. use Submarine\CoreBundle\Entity\SubmarineEntityInterface;
  18. use Submarine\OrdersBundle\Order\OrderDeliveryInterface;
  19. use Submarine\OrdersBundle\Order\OrderInterface;
  20. use Submarine\OrdersBundle\Order\OrderItemInterface;
  21. use Submarine\OrdersBundle\Order\OrderPaymentInterface;
  22. use Submarine\PagesBundle\Entity\Page;
  23. use Submarine\UsersBundle\Entity\User;
  24. use Symfony\Component\Security\Core\User\UserInterface;
  25. use Symfony\Component\Validator\Constraints as Assert;
  26. /**
  27.  * @ORM\Entity(repositoryClass="Submarine\OrdersBundle\Repository\OrderRepository")
  28.  *
  29.  * @ORM\Table(name="submarine_orders", indexes={
  30.  *          @ORM\Index(name="created_at", columns={"created_at"})
  31.  *      })
  32.  *
  33.  * @ORM\HasLifecycleCallbacks()
  34.  */
  35. class Order implements SubmarineEntityInterfaceOrderInterfaceOrderDeliveryInterfaceOrderPaymentInterface
  36. {
  37.     const NAME_PHONE_OPTION "phone";
  38.     const NAME_EMAIL_OPTION "email";
  39.     /**
  40.      * @var int
  41.      *
  42.      * @ORM\Id()
  43.      * @ORM\Column(name="id", type="integer", nullable=false, unique=true)
  44.      * @ORM\GeneratedValue(strategy="AUTO")
  45.      */
  46.     private $id;
  47.     /**
  48.      * @var UuidInterface|null
  49.      *
  50.      * @ORM\Column(name="uuid", type="uuid", nullable=true)
  51.      */
  52.     private $uuid;
  53.     /**
  54.      * @var Status
  55.      *
  56.      * @ORM\ManyToOne(targetEntity="Submarine\OrdersBundle\Entity\Status")
  57.      * @ORM\JoinColumn(name="status_is", referencedColumnName="id")
  58.      */
  59.     private $status;
  60.     // ------------------ Order Info -------------------
  61.     /**
  62.      * @var OrderItem[]|ArrayCollection
  63.      *
  64.      * @ORM\OneToMany(targetEntity="Submarine\OrdersBundle\Entity\OrderItem", mappedBy="order", cascade={"ALL"})
  65.      */
  66.     private $items;
  67.     /**
  68.      * @var UserInterface|null
  69.      *
  70.      * @ORM\ManyToOne(targetEntity="Submarine\UsersBundle\Entity\User", inversedBy="orders")
  71.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  72.      */
  73.     private $user;
  74.     /**
  75.      * Общая стоимость, с учетом скидок
  76.      * @var float
  77.      *
  78.      * @ORM\Column(name="total", type="decimal", precision=10, scale=2, nullable=true)
  79.      */
  80.     private $total 0;
  81.     /**
  82.      * Общая скидка
  83.      * @var float
  84.      *
  85.      * @ORM\Column(name="total_discount", type="decimal", precision=10, scale=2, nullable=true)
  86.      */
  87.     private $totalDiscount 0;
  88.     /**
  89.      * Дополнительные параметры, такие как адрес доставки, имя пользователя и т.д.
  90.      *
  91.      * @var ArrayCollection|OptionInterface[]
  92.      *
  93.      * @ORM\Column(name="options", type="array", nullable=false)
  94.      */
  95.     private $options;
  96.     // ---------------- Order Shipping & payment -------------
  97.     /**
  98.      * Заказ оплачен?
  99.      *
  100.      * @var bool
  101.      *
  102.      * @ORM\Column(name="is_paid", type="boolean", nullable=true)
  103.      */
  104.     private $paid false;
  105.     /**
  106.      * Способ оплаты
  107.      *
  108.      * @ORM\Column(name="payment_method", type="string", nullable=true)
  109.      */
  110.     private $paymentMethod;
  111.     /**
  112.      * Комиссия за оплату
  113.      *
  114.      * @var float
  115.      *
  116.      * @ORM\Column(name="payment_method_price", type="decimal", precision=10, scale=2, nullable=true)
  117.      */
  118.     private $paymentMethodPrice 0;
  119.     /**
  120.      * Способ доставки
  121.      *
  122.      * @ORM\Column(name="delivery_method", type="string", nullable=true)
  123.      */
  124.     private $deliveryMethod;
  125.     /**
  126.      * Дополнительная стоимость за доставку
  127.      *
  128.      * @var float
  129.      *
  130.      * @ORM\Column(name="delivery_method_price", type="decimal", precision=10, scale=2, nullable=true)
  131.      */
  132.     private $deliveryMethodPrice 0;
  133.     // ---------------------- Descriptions -------------------
  134.     /**
  135.      * @var string
  136.      *
  137.      * @ORM\Column(name="description", type="text", nullable=true)
  138.      */
  139.     private $description;
  140.     /**
  141.      * Заметки, недоступны клиенту
  142.      *
  143.      * @var string
  144.      *
  145.      * @ORM\Column(name="notes", type="text", nullable=true)
  146.      */
  147.     private $notes;
  148.     /**
  149.      * @var \DateTime
  150.      *
  151.      * @ORM\Column(name="created_at", type="datetime", nullable=false)
  152.      */
  153.     private $createdAt;
  154.     /**
  155.      * @var \DateTime
  156.      *
  157.      * @ORM\Column(name="updated_at", type="datetime", nullable=false)
  158.      */
  159.     private $updatedAt;
  160.     /**
  161.      * @ORM\OneToOne(targetEntity="Submarine\OrdersBundle\Entity\CardPayment", mappedBy="relatedOrder", cascade={"persist", "remove"})
  162.      */
  163.     private $cardPayment;
  164.     /**
  165.      * IP пользователя
  166.      *
  167.      * @var string
  168.      *
  169.      * @ORM\Column(name="userIp", type="string", nullable=false)
  170.      */
  171.     private $userIp;
  172.     /**
  173.      * @var Promocode|null
  174.      *
  175.      * @ORM\ManyToOne(targetEntity="Postroyka\AppBundle\Entity\Promocode", inversedBy="orders")
  176.      * @ORM\JoinColumn(name="promocode_id", referencedColumnName="id")
  177.      */
  178.     private $promocode;
  179.     /**
  180.      * @var OrderB24Data
  181.      *
  182.      * @ORM\OneToOne(targetEntity="Submarine\OrdersBundle\Entity\OrderB24Data", mappedBy="order", cascade={"persist", "remove"})
  183.      */
  184.     private $b24Data;
  185.     public function __construct()
  186.     {
  187.         $this->items = new ArrayCollection();
  188.         $this->options = new ArrayCollection();
  189.         $this->createdAt = new \DateTime();
  190.         $this->updatedAt = new \DateTime();
  191.         $this->userIp self::getIpUser();
  192.         $this->uuid Uuid::uuid4();
  193.         $this->b24Data = new OrderB24Data($this);
  194.     }
  195.     /**
  196.      * @return string
  197.      */
  198.     public static function entityName()
  199.     {
  200.         return __CLASS__;
  201.     }
  202.     /**
  203.      * @ORM\PreUpdate()
  204.      */
  205.     public function preUpdate()
  206.     {
  207.         $this->updatedAt = new \DateTime();
  208.     }
  209.     /**
  210.      * @param CartInterface $cart
  211.      *
  212.      * @throws CartEmptyException
  213.      * @throws CartExpiredException
  214.      */
  215.     public function handleCart(CartInterface $cart)
  216.     {
  217.         if ($cart->isEmpty()) {
  218.             throw new CartEmptyException();
  219.         }
  220.         if ($cart->isExpired()) {
  221.             throw new CartExpiredException();
  222.         }
  223.         foreach ($cart->getInStockItems() as $cartItem) {
  224.             $orderItem = new OrderItem();
  225.             $orderItem->handleCartItem($cartItem);
  226.             $this->addItem($orderItem);
  227.         }
  228.         $this->total $cart->getTotal();
  229.         $this->totalDiscount $cart->getTotalDiscount();
  230.         $this->description $cart->getDescription();
  231.         $promocode $cart->getPromocode();
  232.         if($promocode){
  233.             $this->promocode $promocode;
  234.             $promocode->addOrder($this);
  235.         }
  236.     }
  237.     /**
  238.      * @return int
  239.      */
  240.     public function getId()
  241.     {
  242.         return $this->id;
  243.     }
  244.     public function getUuid()
  245.     {
  246.         return $this->uuid;
  247.     }
  248.     /**
  249.      * @return Status|null
  250.      */
  251.     public function getStatus()
  252.     {
  253.         return $this->status;
  254.     }
  255.     /**
  256.      * @param Status $status
  257.      */
  258.     public function setStatus(Status $status null)
  259.     {
  260.         $this->status $status;
  261.     }
  262.     /**
  263.      * @return ArrayCollection|OrderItem[]
  264.      */
  265.     public function getItems()
  266.     {
  267.         return $this->items;
  268.     }
  269.     /**
  270.      * @param OrderItemInterface $item
  271.      */
  272.     public function addItem(OrderItemInterface $item)
  273.     {
  274.         $item->setOrder($this);
  275.         $this->items->add($item);
  276.     }
  277.     /**
  278.      * @return null|UserInterface
  279.      */
  280.     public function getUser()
  281.     {
  282.         return $this->user;
  283.     }
  284.     /**
  285.      * @param null|UserInterface|User $user
  286.      */
  287.     public function setUser(UserInterface $user null)
  288.     {
  289.         $this->user $user;
  290.     }
  291.     /**
  292.      * Общая стоимость заказа
  293.      * @return float
  294.      */
  295.     public function getTotal()
  296.     {
  297.         return $this->total;
  298.     }
  299.     /**
  300.      * @param float $total
  301.      */
  302.     public function setTotal(float|int $total): void
  303.     {
  304.         $this->total $total;
  305.     }
  306.     /**
  307.      * Итого к оплате
  308.      * @return float
  309.      */
  310.     public function getPayPrice()
  311.     {
  312.         return $this->getTotal() + $this->getDeliveryMethodPrice() + $this->getPaymentMethodPrice();
  313.     }
  314.     /**
  315.      * @return float
  316.      */
  317.     public function getTotalDiscount()
  318.     {
  319.         return $this->totalDiscount;
  320.     }
  321.     /**
  322.      * @param float $totalDiscount
  323.      */
  324.     public function setTotalDiscount(float|int $totalDiscount): void
  325.     {
  326.         $this->totalDiscount $totalDiscount;
  327.     }
  328.     /**
  329.      * @param OptionInterface $option
  330.      */
  331.     public function addOption(OptionInterface $option)
  332.     {
  333.         $this->options->set($option->getName(), $option);
  334.     }
  335.     /**
  336.      * @return ArrayCollection|OptionInterface[]
  337.      */
  338.     public function getOptions()
  339.     {
  340.         return $this->options;
  341.     }
  342.     /**
  343.      * @return string
  344.      */
  345.     public function getDescription()
  346.     {
  347.         return $this->description;
  348.     }
  349.     /**
  350.      * @param string $description
  351.      */
  352.     public function setDescription($description)
  353.     {
  354.         $this->description $description;
  355.     }
  356.     /**
  357.      * @return string
  358.      */
  359.     public function getNotes()
  360.     {
  361.         return $this->notes;
  362.     }
  363.     /**
  364.      * @param string $notes
  365.      */
  366.     public function setNotes($notes)
  367.     {
  368.         $this->notes $notes;
  369.     }
  370.     /**
  371.      * @return \DateTime
  372.      */
  373.     public function getCreatedAt()
  374.     {
  375.         return $this->createdAt;
  376.     }
  377.     /**
  378.      * @return \DateTime
  379.      */
  380.     public function getUpdatedAt()
  381.     {
  382.         return $this->updatedAt;
  383.     }
  384.     /**
  385.      * @param \DateTime $createdAt
  386.      */
  387.     public function setCreatedAt(\DateTime $createdAt): void
  388.     {
  389.         $this->createdAt $createdAt;
  390.     }
  391.     /**
  392.      * @param \DateTime $updatedAt
  393.      */
  394.     public function setUpdatedAt(\DateTime $updatedAt): void
  395.     {
  396.         $this->updatedAt $updatedAt;
  397.     }
  398.     /**
  399.      * @return string
  400.      */
  401.     public function getUserIp()
  402.     {
  403.         return $this->userIp;
  404.     }
  405.     /**
  406.      * @param string $userIp
  407.      */
  408.     public function setUserIp($userIp): void
  409.     {
  410.         $this->userIp $userIp;
  411.     }
  412.     // ---------------- Order Shipping & payment -------------
  413.     /**
  414.      * @return boolean
  415.      */
  416.     public function isPaid()
  417.     {
  418.         return $this->paid;
  419.     }
  420.     /**
  421.      * @param boolean $paid
  422.      */
  423.     public function setPaid($paid)
  424.     {
  425.         $this->paid = (bool)$paid;
  426.     }
  427.     /**
  428.      * @return string
  429.      */
  430.     public function getPaymentMethod()
  431.     {
  432.         return $this->paymentMethod;
  433.     }
  434.     /**
  435.      * @param string $paymentMethod
  436.      */
  437.     public function setPaymentMethod($paymentMethod)
  438.     {
  439.         $this->paymentMethod $paymentMethod;
  440.     }
  441.     /**
  442.      * @return float
  443.      */
  444.     public function getPaymentMethodPrice()
  445.     {
  446.         return $this->paymentMethodPrice;
  447.     }
  448.     /**
  449.      * @param float $paymentMethodPrice
  450.      */
  451.     public function setPaymentMethodPrice($paymentMethodPrice)
  452.     {
  453.         $this->paymentMethodPrice $paymentMethodPrice;
  454.     }
  455.     // ----------------- Delivery ---------------
  456.     /**
  457.      * {@inheritdoc}
  458.      */
  459.     public function getDeliveryMethod()
  460.     {
  461.         return $this->deliveryMethod;
  462.     }
  463.     /**
  464.      * {@inheritdoc}
  465.      */
  466.     public function setDeliveryMethod($methodName)
  467.     {
  468.         $this->deliveryMethod $methodName;
  469.     }
  470.     /**
  471.      * {@inheritdoc}
  472.      */
  473.     public function getDeliveryMethodPrice()
  474.     {
  475.         return $this->deliveryMethodPrice;
  476.     }
  477.     /**
  478.      * {@inheritdoc}
  479.      */
  480.     public function setDeliveryMethodPrice($price)
  481.     {
  482.         $this->deliveryMethodPrice $price;
  483.     }
  484.     public function getCardPayment(): ?CardPayment
  485.     {
  486.         return $this->cardPayment;
  487.     }
  488.     public function setCardPayment(CardPayment $cardPayment)
  489.     {
  490.         $this->cardPayment $cardPayment;
  491.         // set the owning side of the relation if necessary
  492.         if ($this !== $cardPayment->getRelatedOrder()) {
  493.             $cardPayment->setRelatedOrder($this);
  494.         }
  495.         return $this;
  496.     }
  497.     public function getOptionValue($name)
  498.     {
  499.         /** @var Option $option */
  500.         $option $this->options->get($name);
  501.         return $option $option->getValue() : null;
  502.     }
  503.     public function getEcommerceOptionString()
  504.     {
  505.         $allOptions = [];
  506.         foreach ($this->options as $option){
  507.             if(!$option->getValue()){
  508.                 continue;
  509.             }
  510.             $allOptions[] = $option->getName() . ": " html_entity_decode($option->getValue());
  511.         }
  512.         return implode(" | "$allOptions);
  513.     }
  514.     /**
  515.      * @return Promocode|null
  516.      */
  517.     public function getPromocode(): ?Promocode
  518.     {
  519.         return $this->promocode;
  520.     }
  521.     /**
  522.      * @param Promocode|null $promocode
  523.      */
  524.     public function setPromocode(?Promocode $promocode): void
  525.     {
  526.         $this->promocode $promocode;
  527.     }
  528.     /**
  529.      * @return OrderB24Data
  530.      */
  531.     public function getB24Data(): OrderB24Data
  532.     {
  533.         return $this->b24Data;
  534.     }
  535.     /**
  536.      * @param OrderB24Data $b24Data
  537.      */
  538.     public function setB24Data(OrderB24Data $b24Data): void
  539.     {
  540.         $this->b24Data $b24Data;
  541.     }
  542.     public function getCountAllItems()
  543.     {
  544.         $count 0;
  545.         foreach ($this->items as $item){
  546.             $count += $item->getQuantity();
  547.         }
  548.         return $count;
  549.     }
  550.     static function getIpUser(): string
  551.     {
  552.         return $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? "";
  553.     }
  554.     static function getNumEnding($number$endingArray = ["товар""товара""товаров"])
  555.     {
  556.         $number $number 100;
  557.         if($number >= 11 && $number <= 19){
  558.             return $endingArray[2];
  559.         }
  560.         switch ($number 10) {
  561.             case 1:
  562.                 return $endingArray[0];
  563.             case 2: case 3: case 4:
  564.                 return $endingArray[1];
  565.             default: return $endingArray[2];
  566.         }
  567.     }
  568.     public function isExistLongProduct()
  569.     {
  570.         foreach ($this->items as $item){
  571.             if($item->getLength() >= 4){
  572.                 return true;
  573.             }
  574.         }
  575.         return false;
  576.     }
  577.     public function getInStockItems()
  578.     {
  579.         return $this->items;
  580.     }
  581.     public function searchOrderItemByProductId(int $productIdfloat $lengthbool $isDefected) : OrderItem|false
  582.     {
  583.         $filteredItems $this->items->filter(function (OrderItem $orderItem) use ($productId$length$isDefected){
  584.            return $productId === $orderItem->getEntityId() && $orderItem->getLength() === $length && $orderItem->isDefected() === $isDefected;
  585.         });
  586.         return $filteredItems->first();
  587.     }
  588. }