src/Entity/Order/Order.php line 40

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Entity\Order;
  12. use App\Entity\Customer\Customer;
  13. use App\Entity\Geographical\Address;
  14. use App\Entity\IdentifiableTrait;
  15. use App\Entity\Printer\Printer;
  16. use App\OrderProductionStates;
  17. use App\OrderStates;
  18. use App\Repository\Order\OrderRepository;
  19. use Doctrine\Common\Collections\ArrayCollection;
  20. use Doctrine\Common\Collections\Collection;
  21. use Doctrine\ORM\Mapping as ORM;
  22. use Gedmo\Timestampable\Traits\TimestampableEntity;
  23. use Sylius\Component\Resource\Annotation\SyliusCrudRoutes;
  24. use Sylius\Component\Resource\Model\ResourceInterface;
  25. #[ORM\Entity(repositoryClass: OrderRepository::class)]
  26. #[ORM\Table(name: 'app_order')]
  27. #[SyliusCrudRoutes(alias: 'app.order', path: '/admin/orders', section: 'backend', templates: 'backend/order', grid: 'app_backend_order', only: ['index', 'show'], vars: [
  28.     'all' => [
  29.         'subheader' => 'app.ui.manage_your_orders',
  30.     ],
  31.     'index' => [
  32.         'icon' => 'cart',
  33.     ],
  34. ])]
  35. class Order implements ResourceInterface
  36. {
  37.     use IdentifiableTrait;
  38.     use TimestampableEntity;
  39.     #[ORM\ManyToOne(targetEntity: Customer::class, inversedBy: 'orders', cascade: ['persist'])]
  40.     #[ORM\JoinColumn(name: "customer_id", referencedColumnName: "id", onDelete: "CASCADE")]
  41.     private ?Customer $customer = null;
  42.     #[ORM\ManyToOne(targetEntity: Address::class, cascade:['persist', 'remove'])]
  43.     #[ORM\JoinColumn(name: "shipping_address_id", referencedColumnName: "id", onDelete: "CASCADE")]
  44.     private ?Address $shippingAddress = null;
  45.     #[ORM\Column(type: 'string')]
  46.     private string $state;
  47.     #[ORM\Column(type: 'datetime_immutable', nullable: true)]
  48.     private ?\DateTimeImmutable $checkoutCompletedAt = null;
  49.     #[ORM\Column(type: 'integer')]
  50.     private int $shippementAmount = 0;
  51.     #[ORM\Column(type: 'string', nullable: true)]
  52.     private ?string $number = null;
  53.     #[ORM\OneToMany(mappedBy: 'order', targetEntity: OrderItem::class, orphanRemoval: true, cascade:['persist', 'remove'])]
  54.     private Collection $orderItems;
  55.     #[ORM\Column(type: 'integer')]
  56.     private int $total = 0;
  57.     #[ORM\Column(length: 255)]
  58.     private ?string $productionState = null;
  59.     #[ORM\Column(nullable: true)]
  60.     private ?int $fujiId = null;
  61.     // Nullable malgré la contrainte NOT NULL déclarée par la migration Version20230531130423 :
  62.     // certaines lignes existantes ont NULL en base (ADD ... NOT NULL sur une table déjà peuplée,
  63.     // sans DEFAULT, peut laisser des lignes existantes Ã  NULL selon le sql_mode MySQL/MariaDB au
  64.     // moment de l'ALTER). Sans ce ?, l'hydratation Doctrine de ces lignes lève un TypeError
  65.     // ("Cannot assign null to property ... of type array"). getFujiMessages()/addFujiMessage()
  66.     // gèrent le null en interne, donc rien ne change côté appelants.
  67.     #[ORM\Column(type: 'json', nullable: true)]
  68.     private ?array $fujiMessages = [];
  69.     // Workflow imprimeurs internes (App\Processor\Order\PrinterUploadPhotoProcessor), actif seulement
  70.     // si le paramètre printer_workflow_enabled est true. Coexiste avec fujiId/fujiMessages ci-dessus,
  71.     // qui restent utilisés par le workflow Fuji par défaut.
  72.     #[ORM\ManyToOne(targetEntity: Printer::class, inversedBy: 'orders')]
  73.     #[ORM\JoinColumn(name: 'printer_id', referencedColumnName: 'id', onDelete: 'SET NULL')]
  74.     private ?Printer $printer = null;
  75.     #[ORM\Column(type: 'text', nullable: true)]
  76.     private ?string $adminComment = null;
  77.     /** @var Collection<int, OrderAdminImage> */
  78.     #[ORM\OneToMany(mappedBy: 'order', targetEntity: OrderAdminImage::class, orphanRemoval: true, cascade: ['persist', 'remove'])]
  79.     private Collection $adminImages;
  80.     #[ORM\Column(type: 'boolean')]
  81.     private bool $isFromCode = false;
  82.     /** Total in cents (€) for direct payment. null when paid with credits. */
  83.     #[ORM\Column(type: 'integer', nullable: true)]
  84.     private ?int $totalAmountCents = null;
  85.     /** credits | direct | complement */
  86.     #[ORM\Column(type: 'string', length: 20, options: ['default' => 'credits'])]
  87.     private string $paymentMode = 'credits';
  88.     public function __construct()
  89.     {
  90.         $this->state = OrderStates::STATE_CART;
  91.         $this->productionState = OrderProductionStates::STATE_AWAITING;
  92.         $this->orderItems = new ArrayCollection();
  93.         $this->adminImages = new ArrayCollection();
  94.     }
  95.     /**
  96.      * Get the value of customer
  97.      *
  98.      * @return ?Customer
  99.      */
  100.     public function getCustomer(): ?Customer
  101.     {
  102.         return $this->customer;
  103.     }
  104.     /**
  105.      * Set the value of customer
  106.      *
  107.      * @return self
  108.      */
  109.     public function setCustomer(?Customer $customer): self
  110.     {
  111.         $this->customer = $customer;
  112.         return $this;
  113.     }
  114.     /**
  115.      * Get the value of shippingAddress
  116.      *
  117.      * @return ?Address
  118.      */
  119.     public function getShippingAddress(): ?Address
  120.     {
  121.         return $this->shippingAddress;
  122.     }
  123.     /**
  124.      * Set the value of shippingAddress
  125.      *
  126.      * @return self
  127.      */
  128.     public function setShippingAddress(?Address $address): self
  129.     {
  130.         $this->shippingAddress = $address;
  131.         return $this;
  132.     }
  133.     /**
  134.      * Get the value of state
  135.      *
  136.      * @return string
  137.      */
  138.     public function getState(): string
  139.     {
  140.         return $this->state;
  141.     }
  142.     /**
  143.      * Set the value of state
  144.      *
  145.      * @return self
  146.      */
  147.     public function setState(string $state): self
  148.     {
  149.         $this->state = $state;
  150.         return $this;
  151.     }
  152.     /**
  153.      * Get the value of checkoutCompletedAt
  154.      *
  155.      * @return ?\DateTimeImmutable
  156.      */
  157.     public function getCheckoutCompletedAt(): ?\DateTimeImmutable
  158.     {
  159.         return $this->checkoutCompletedAt;
  160.     }
  161.     /**
  162.      * Set the value of checkoutCompletedAt
  163.      *
  164.      * @return self
  165.      */
  166.     public function setCheckoutCompletedAt(?\DateTimeImmutable $checkoutCompletedAt): self
  167.     {
  168.         $this->checkoutCompletedAt = $checkoutCompletedAt;
  169.         return $this;
  170.     }
  171.     /**
  172.      * Get the value of shippementAmount
  173.      *
  174.      * @return int
  175.      */
  176.     public function getShippementAmount(): int
  177.     {
  178.         return $this->shippementAmount;
  179.     }
  180.     /**
  181.      * Set the value of shippementAmount
  182.      *
  183.      * @return self
  184.      */
  185.     public function setShippementAmount(int $shippementAmount): self
  186.     {
  187.         $this->shippementAmount = $shippementAmount;
  188.         return $this;
  189.     }
  190.     /**
  191.      * Get the value of number
  192.      *
  193.      * @return ?string
  194.      */
  195.     public function getNumber(): ?string
  196.     {
  197.         return $this->number;
  198.     }
  199.     /**
  200.      * Set the value of number
  201.      *
  202.      * @return self
  203.      */
  204.     public function setNumber(?string $number): self
  205.     {
  206.         $this->number = $number;
  207.         return $this;
  208.     }
  209.     /**
  210.      * @return Collection<int, OrderItem>
  211.      */
  212.     public function getOrderItems(): Collection
  213.     {
  214.         return $this->orderItems;
  215.     }
  216.     public function addOrderItem(OrderItem $orderItem): self
  217.     {
  218.         if (!$this->orderItems->contains($orderItem)) {
  219.             $this->orderItems->add($orderItem);
  220.             $orderItem->setOrder($this);
  221.         }
  222.         return $this;
  223.     }
  224.     public function removeOrderItem(OrderItem $orderItem): self
  225.     {
  226.         if ($this->orderItems->removeElement($orderItem)) {
  227.             if ($orderItem->getOrder() === $this) {
  228.                 $orderItem->setOrder(null);
  229.             }
  230.         }
  231.         return $this;
  232.     }
  233.     /**
  234.      * Get the value of total
  235.      *
  236.      * @return int
  237.      */
  238.     public function getTotal(): int
  239.     {
  240.         return $this->total;
  241.     }
  242.     /**
  243.      * Set the value of total
  244.      *
  245.      * @return self
  246.      */
  247.     public function setTotal(int $total): self
  248.     {
  249.         $this->total = $total;
  250.         return $this;
  251.     }
  252.     public function getProductsCost(): int
  253.     {
  254.         $cost = 0;
  255.         foreach ($this->getOrderItems() as $orderItem) {
  256.             $cost += $orderItem->getTotalPrice();
  257.         }
  258.         return $cost;
  259.     }
  260.     public function getFirstItem(): ?OrderItem
  261.     {
  262.         $orderItem = $this->getOrderItems()->first();
  263.         return ($orderItem instanceof OrderItem) ? $orderItem : null;
  264.     }
  265.     public function getFirstItemName(): ?string
  266.     {
  267.         return ($this->getFirstItem()) ? $this->getFirstItem()->getAlbumName() : null;
  268.     }
  269.     public function getFirstItemQuantity(): int
  270.     {
  271.         return ($this->getFirstItem()) ? $this->getFirstItem()->getQuantity() : 0;
  272.     }
  273.     public function getFirstItemPhotosCount(): int
  274.     {
  275.         return ($this->getFirstItem()) ? $this->getFirstItem()->getPhotosCount() : 0;
  276.     }
  277.     public function getFirstItemAlbumSize(): int
  278.     {
  279.         return ($this->getFirstItem()) ? $this->getFirstItem()->getProduct()->getAlbumSize() : 0;
  280.     }
  281.     public function getFirstItemMattePaper(): ?bool
  282.     {
  283.         return ($this->getFirstItem()) ? $this->getFirstItem()->getMattePaper() : null;
  284.     }
  285.     public function getFirstItemWithFrame(): ?bool
  286.     {
  287.         return ($this->getFirstItem()) ? $this->getFirstItem()->getWithFrame() : null;
  288.     }
  289.     public function getProductionState(): ?string
  290.     {
  291.         return $this->productionState;
  292.     }
  293.     public function setProductionState(string $productionState): self
  294.     {
  295.         $this->productionState = $productionState;
  296.         return $this;
  297.     }
  298.     public function getFujiId(): ?int
  299.     {
  300.         return $this->fujiId;
  301.     }
  302.     public function setFujiId(?int $fujiId): self
  303.     {
  304.         $this->fujiId = $fujiId;
  305.         return $this;
  306.     }
  307.     public function getFujiMessages(): array
  308.     {
  309.         return $this->fujiMessages ?? [];
  310.     }
  311.     public function addFujiMessage(string $fujiMessage): self
  312.     {
  313.         $this->fujiMessages ??= [];
  314.         $this->fujiMessages[] = $fujiMessage;
  315.         return $this;
  316.     }
  317.     public function getPrinter(): ?Printer
  318.     {
  319.         return $this->printer;
  320.     }
  321.     public function setPrinter(?Printer $printer): self
  322.     {
  323.         $this->printer = $printer;
  324.         return $this;
  325.     }
  326.     public function getAdminComment(): ?string
  327.     {
  328.         return $this->adminComment;
  329.     }
  330.     public function setAdminComment(?string $adminComment): self
  331.     {
  332.         $this->adminComment = $adminComment;
  333.         return $this;
  334.     }
  335.     /** @return Collection<int, OrderAdminImage> */
  336.     public function getAdminImages(): Collection
  337.     {
  338.         return $this->adminImages;
  339.     }
  340.     public function addAdminImage(OrderAdminImage $adminImage): self
  341.     {
  342.         if (!$this->adminImages->contains($adminImage)) {
  343.             $this->adminImages->add($adminImage);
  344.             $adminImage->setOrder($this);
  345.         }
  346.         return $this;
  347.     }
  348.     public function removeAdminImage(OrderAdminImage $adminImage): self
  349.     {
  350.         if ($this->adminImages->removeElement($adminImage)) {
  351.             if ($adminImage->getOrder() === $this) {
  352.                 $adminImage->setOrder(null);
  353.             }
  354.         }
  355.         return $this;
  356.     }
  357.     public function isFromCode(): bool
  358.     {
  359.         return $this->isFromCode;
  360.     }
  361.     public function setIsFromCode(bool $isFromCode): self
  362.     {
  363.         $this->isFromCode = $isFromCode;
  364.         return $this;
  365.     }
  366.     public function getTotalAmountCents(): ?int
  367.     {
  368.         return $this->totalAmountCents;
  369.     }
  370.     public function setTotalAmountCents(?int $totalAmountCents): self
  371.     {
  372.         $this->totalAmountCents = $totalAmountCents;
  373.         return $this;
  374.     }
  375.     public function getPaymentMode(): string
  376.     {
  377.         return $this->paymentMode;
  378.     }
  379.     public function setPaymentMode(string $paymentMode): self
  380.     {
  381.         $this->paymentMode = $paymentMode;
  382.         return $this;
  383.     }
  384.     public function isDirectPayment(): bool
  385.     {
  386.         return $this->paymentMode === 'direct' || $this->paymentMode === 'complement';
  387.     }
  388. }