src/Entity/Cart.php line 27

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Collections\Collection;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use EADPlataforma\Validator\Constraints as EADAssert;
  7. use EADPlataforma\Enum\CartEnum;
  8. use EADPlataforma\Enum\ProductEnum;
  9. use EADPlataforma\Util\NumberUtil;
  10. use \DateTime;
  11. /**
  12.  * Cart
  13.  *
  14.  * @ORM\Table(name="cart", indexes={
  15.  *      @ORM\Index(name="fk_cart_user_id", columns={"user_id"}), 
  16.  *      @ORM\Index(name="fk_cart_product_id", columns={"product_id"}),
  17.  *      @ORM\Index(name="fk_cart_product_offer_id", columns={"product_offer_id"}),
  18.  *      @ORM\Index(name="fk_cart_product_coupon_id", columns={"product_coupon_id"}),
  19.  *      @ORM\Index(name="fk_cart_product_suggestion_id", columns={"product_suggestion_id"})
  20.  * })
  21.  *
  22.  * @ORM\Entity(repositoryClass="EADPlataforma\Repository\CartRepository")
  23.  */
  24. class Cart
  25. {
  26.     /**
  27.      * @var int
  28.      *
  29.      * @ORM\Column(name="id", type="integer", nullable=false)
  30.      * @ORM\Id
  31.      * @ORM\GeneratedValue(strategy="IDENTITY")
  32.      */
  33.     private $id;
  34.     /**
  35.      * @Assert\NotBlank(
  36.      *      message = "Deleted not informed"
  37.      * )
  38.      *
  39.      * @Assert\Choice(
  40.      *      choices = { 
  41.      *                      CartEnum::ITEM_NO_DELETED, 
  42.      *                      CartEnum::ITEM_ON_TRASH,
  43.      *                      CartEnum::ITEM_DELETED
  44.      *                },
  45.      *      message = "Delete Option Invalid"
  46.      * )
  47.      *
  48.      * @var int
  49.      *
  50.      * @ORM\Column(name="deleted", type="integer", nullable=false, options={"default"="0"})
  51.      */
  52.     private $deleted CartEnum::ITEM_NO_DELETED;
  53.     /**
  54.      * @Assert\NotBlank(
  55.      *      message = "Hash not informed"
  56.      * )
  57.      *
  58.      * @var string
  59.      *
  60.      * @ORM\Column(name="hash_identify", type="string", length=80, nullable=false)
  61.      */
  62.     private $hashIdentify;
  63.     /**
  64.      * @Assert\NotBlank(
  65.      *      message = "Price not informed"
  66.      * )
  67.      *
  68.      * @Assert\GreaterThanOrEqual(
  69.      *     value = CartEnum::MIN_VALUE_PRODUCT,
  70.      *     message = "Price invalid"
  71.      * )
  72.      *
  73.      * @var string
  74.      *
  75.      * @ORM\Column(name="price", type="decimal", precision=10, scale=2, nullable=false, options={"default"="0.00"})
  76.      */
  77.     private $price '0.00';
  78.     /**
  79.      * @Assert\NotBlank(
  80.      *      message = "Membership Fee not informed"
  81.      * )
  82.      *
  83.      * @var string
  84.      *
  85.      * @ORM\Column(name="membership_fee", type="decimal", precision=10, scale=2, nullable=false, options={"default"="0.00"})
  86.      */
  87.     private $membershipFee '0.00';
  88.     /**
  89.      * @Assert\NotBlank(
  90.      *      message = "Type not informed"
  91.      * )
  92.      *
  93.      * @Assert\Choice(
  94.      *      choices = { CartEnum::CART, CartEnum::CHECKOUT },
  95.      *      message = "Type Invalid"
  96.      * )
  97.      *
  98.      * @var int
  99.      *
  100.      * @ORM\Column(name="type", type="integer", nullable=false, options={"default"="1"})
  101.      */
  102.     private $type CartEnum::CART;
  103.     /**
  104.      * @Assert\NotBlank(
  105.      *      message = "Able not informed"
  106.      * )
  107.      *
  108.      * @Assert\Choice(
  109.      *      choices = { CartEnum::YES, CartEnum::NO },
  110.      *      message = "Able Invalid"
  111.      * )
  112.      *
  113.      * @var int
  114.      *
  115.      * @ORM\Column(name="able", type="integer", nullable=false, options={"default"="1"})
  116.      */
  117.     private $able CartEnum::YES;
  118.     /**
  119.      * @Assert\NotBlank(
  120.      *      message = "Product Type not informed"
  121.      * )
  122.      *
  123.      * @Assert\Choice(
  124.      *      choices = { 
  125.      *                      ProductEnum::COURSE,
  126.      *                      ProductEnum::SUBSCRIPTION,
  127.      *                      ProductEnum::COMBO,
  128.      *                      ProductEnum::CERTIFICATE,
  129.      *                      ProductEnum::PERIOD,
  130.      *                      ProductEnum::SUPPORT
  131.      *                },
  132.      *      message = "Type Invalid"
  133.      * )
  134.      *
  135.      * @var int
  136.      *
  137.      * @ORM\Column(name="product_type", type="integer", nullable=false)
  138.      */
  139.     private $productType;
  140.     /**
  141.      * @Assert\Json(
  142.      *     message = "Utms Url is an invalid Json."
  143.      * )
  144.      *
  145.      * @var string|null
  146.      *
  147.      * @ORM\Column(name="utms_url", type="text", length=0, nullable=true)
  148.      */
  149.     private $utmsUrl;
  150.     /**
  151.      * @Assert\NotBlank(
  152.      *      message = "Date not informed"
  153.      * )
  154.      *
  155.      * @EADAssert\DateTimeEAD(
  156.      *      message = "Date Invalid"
  157.      * )
  158.      *
  159.      * @var \DateTime
  160.      *
  161.      * @ORM\Column(name="date", type="datetime", nullable=false, options={"default"="CURRENT_TIMESTAMP"})
  162.      */
  163.     private $date;
  164.     /**
  165.      * @var string|null
  166.      *
  167.      * @ORM\Column(name="error_message", type="text", length=0, nullable=true)
  168.      */
  169.     private $errorMessage;
  170.     /**
  171.      * @var int
  172.      *
  173.      * @ORM\Column(name="pipedrive_deal_id", type="integer", nullable=true)
  174.      */
  175.     private $pipedriveDeal;
  176.     /**
  177.      * @Assert\NotBlank(
  178.      *      message = "Product not informed"
  179.      * )
  180.      *
  181.      * @Assert\Valid
  182.      *
  183.      * @var \Product
  184.      *
  185.      * @ORM\ManyToOne(targetEntity="Product")
  186.      * @ORM\JoinColumns({
  187.      *   @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
  188.      * })
  189.      */
  190.     private $product;
  191.     /**
  192.      * @Assert\NotBlank(
  193.      *      message = "Product Offer not informed"
  194.      * )
  195.      *
  196.      * @Assert\Valid
  197.      *
  198.      * @var \ProductOffer
  199.      *
  200.      * @ORM\ManyToOne(targetEntity="ProductOffer")
  201.      * @ORM\JoinColumns({
  202.      *   @ORM\JoinColumn(name="product_offer_id", referencedColumnName="id", nullable=false)
  203.      * })
  204.      */
  205.     private $productOffer;
  206.     /**
  207.      * @var \Doctrine\Common\Collections\Collection
  208.      *
  209.      * @ORM\ManyToMany(targetEntity="Course", inversedBy="cart")
  210.      * @ORM\JoinTable(name="cart_x_course",
  211.      *   joinColumns={
  212.      *     @ORM\JoinColumn(name="cart_id", referencedColumnName="id")
  213.      *   },
  214.      *   inverseJoinColumns={
  215.      *     @ORM\JoinColumn(name="course_id", referencedColumnName="id")
  216.      *   }
  217.      * )
  218.      */
  219.     private $course;
  220.     /**
  221.      * @Assert\Valid
  222.      *
  223.      * @var \ProductCoupon
  224.      *
  225.      * @ORM\ManyToOne(targetEntity="ProductCoupon")
  226.      * @ORM\JoinColumns({
  227.      *   @ORM\JoinColumn(name="product_coupon_id", referencedColumnName="id")
  228.      * })
  229.      */
  230.     private $productCoupon;
  231.     /**
  232.      * @Assert\Valid
  233.      *
  234.      * @var \ProductCoupon
  235.      *
  236.      * @ORM\ManyToOne(targetEntity="ProductSuggestion")
  237.      * @ORM\JoinColumns({
  238.      *   @ORM\JoinColumn(name="product_suggestion_id", referencedColumnName="id")
  239.      * })
  240.      */
  241.     private $productSuggestion;
  242.     /**
  243.      * @Assert\Valid
  244.      *
  245.      * @var \User
  246.      *
  247.      * @ORM\ManyToOne(targetEntity="User")
  248.      * @ORM\JoinColumns({
  249.      *   @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
  250.      * })
  251.      */
  252.     private $user;
  253.     /**
  254.      * Constructor
  255.      */
  256.     public function __construct()
  257.     {
  258.         $this->date = new DateTime();
  259.         $this->course = new \Doctrine\Common\Collections\ArrayCollection();
  260.     }
  261.     public function getId(): ?int
  262.     {
  263.         return $this->id;
  264.     }
  265.     
  266.     public function getHashIdentify(): ?string
  267.     {
  268.         return $this->hashIdentify;
  269.     }
  270.     public function setHashIdentify(?string $hashIdentify): self
  271.     {
  272.         $this->hashIdentify $hashIdentify;
  273.         return $this;
  274.     }
  275.     public function getPrice($cents false)
  276.     {
  277.         if($cents){
  278.             return NumberUtil::numberToCentsStatic($this->price);
  279.         }
  280.         return $this->price;
  281.     }
  282.     public function setPrice($price): self
  283.     {
  284.         $this->price $price;
  285.         return $this;
  286.     }
  287.     public function getMembershipFee(?bool $cents false)
  288.     {
  289.         if($cents){
  290.             return NumberUtil::numberToCentsStatic($this->membershipFee);
  291.         }
  292.         return $this->membershipFee;
  293.     }
  294.     public function setMembershipFee($membershipFee): self
  295.     {
  296.         $this->membershipFee $membershipFee;
  297.         return $this;
  298.     }
  299.     public function getType(): ?int
  300.     {
  301.         return $this->type;
  302.     }
  303.     public function setType(int $type): self
  304.     {
  305.         $this->type $type;
  306.         return $this;
  307.     }
  308.     public function getAble(): ?int
  309.     {
  310.         return $this->able;
  311.     }
  312.     public function setAble(int $able): self
  313.     {
  314.         $this->able $able;
  315.         return $this;
  316.     }
  317.     public function getProductType(): ?int
  318.     {
  319.         return $this->productType;
  320.     }
  321.     public function setProductType(int $productType): self
  322.     {
  323.         $this->productType $productType;
  324.         return $this;
  325.     }
  326.     public function getUtmsUrl(): ?string
  327.     {
  328.         return $this->utmsUrl;
  329.     }
  330.     public function setUtmsUrl(?string $utmsUrl): self
  331.     {
  332.         $this->utmsUrl $utmsUrl;
  333.         return $this;
  334.     }
  335.     public function getDate($dateFormat 'Y-m-d H:i:s')
  336.     {
  337.         if(!empty($this->date)){
  338.             return $this->date->format($dateFormat);
  339.         }
  340.         return $this->date;
  341.     }
  342.     public function setDate($date): self
  343.     {
  344.         if(!empty($date)){
  345.             $date DateTime::createFromFormat('Y-m-d H:i:s'$date);
  346.         }
  347.         
  348.         $this->date $date;
  349.         return $this;
  350.     }
  351.     public function getErrorMessage(): ?string
  352.     {
  353.         return $this->errorMessage;
  354.     }
  355.     public function setErrorMessage(?string $errorMessage): self
  356.     {
  357.         $this->errorMessage $errorMessage;
  358.         return $this;
  359.     }
  360.     public function getPipedriveDeal(): ?int
  361.     {
  362.         return $this->pipedriveDeal;
  363.     }
  364.     public function setPipedriveDeal(?int $pipedriveDeal): self
  365.     {
  366.         $this->pipedriveDeal $pipedriveDeal;
  367.         return $this;
  368.     }
  369.     public function getProduct(): ?Product
  370.     {
  371.         return $this->product;
  372.     }
  373.     public function setProduct(?Product $product): self
  374.     {
  375.         $this->product $product;
  376.         return $this;
  377.     }
  378.     public function getProductOffer(): ?ProductOffer
  379.     {
  380.         return $this->productOffer;
  381.     }
  382.     public function setProductOffer(?ProductOffer $productOffer): self
  383.     {
  384.         $this->productOffer $productOffer;
  385.         return $this;
  386.     }
  387.     /**
  388.      * @return Collection|Course[]
  389.      */
  390.     public function getCourse(): ?Collection
  391.     {
  392.         return $this->course;
  393.     }
  394.     public function addCourse(Course $course): self
  395.     {
  396.         if (!$this->course->contains($course)) {
  397.             $this->course[] = $course;
  398.         }
  399.         return $this;
  400.     }
  401.     public function removeCourse(Course $course): self
  402.     {
  403.         if ($this->course->contains($course)) {
  404.             $this->course->removeElement($course);
  405.         }
  406.         return $this;
  407.     }
  408.     public function removeAllCourse(): self
  409.     {
  410.         $this->course = new \Doctrine\Common\Collections\ArrayCollection();
  411.         return $this;
  412.     }
  413.     public function getProductCoupon(): ?ProductCoupon
  414.     {
  415.         return $this->productCoupon;
  416.     }
  417.     public function setProductCoupon(?ProductCoupon $productCoupon): self
  418.     {
  419.         $this->productCoupon $productCoupon;
  420.         return $this;
  421.     }
  422.     public function getProductSuggestion(): ?ProductSuggestion
  423.     {
  424.         return $this->productSuggestion;
  425.     }
  426.     public function setProductSuggestion(?ProductSuggestion $productSuggestion): self
  427.     {
  428.         $this->productSuggestion $productSuggestion;
  429.         return $this;
  430.     }
  431.     
  432.     public function getUser(): ?User
  433.     {
  434.         return $this->user;
  435.     }
  436.     public function setUser(?User $user): self
  437.     {
  438.         $this->user $user;
  439.         return $this;
  440.     }
  441.     public function stringPaymentMethod($method)
  442.     {
  443.         $string '';
  444.         switch ($method) {
  445.             case CartEnum::PAYMENT_CARD:
  446.                 $string 'Credit Card';
  447.             break;
  448.             case CartEnum::PAYMENT_BILL:
  449.                 $string 'Bill';
  450.             case CartEnum::PAYMENT_PIX:
  451.                 $string 'Pix';
  452.             break;
  453.             
  454.         }
  455.         return $string;
  456.     }
  457.     public function isOnTrash(): bool
  458.     {
  459.         return ($this->deleted == CartEnum::ITEM_ON_TRASH);
  460.     }
  461.     public function isDeleted(): bool
  462.     {
  463.         return ($this->deleted == CartEnum::ITEM_DELETED);
  464.     }
  465.     public function restore(): self
  466.     {
  467.         $this->deleted CartEnum::ITEM_NO_DELETED;
  468.         return $this;
  469.     }
  470.     public function trash(): self
  471.     {
  472.         $this->deleted CartEnum::ITEM_ON_TRASH;
  473.         return $this;
  474.     }
  475.     public function delete(): self
  476.     {
  477.         $this->deleted CartEnum::ITEM_DELETED;
  478.         return $this;
  479.     }
  480.     
  481.     public function toReturn(){
  482.         $arrCourse = [];
  483.         foreach ($this->course as $key => $course) {
  484.             $arrCourse[] = $course->getId();
  485.         }
  486.         $data = [
  487.             "id" => $this->id,
  488.             "deleted" => $this->deleted,
  489.             "hashIdentify" => $this->hashIdentify,
  490.             "price" => $this->price,
  491.             "membershipFee" => $this->membershipFee,
  492.             "type" => $this->type,
  493.             "able" => $this->able,
  494.             "productType" => $this->productType,
  495.             "utmsUrl" => $this->utmsUrl,
  496.             "date" => $this->getDate(),
  497.             "errorMessage" => $this->errorMessage,
  498.             "pipedriveDeal" => $this->pipedriveDeal,
  499.             "product" => ( $this->product $this->product->getId() : null ),
  500.             "productOffer" => ( $this->productOffer $this->productOffer->getId() : null ),
  501.             "course" => $arrCourse,
  502.             "productCoupon" => ( $this->productCoupon $this->productCoupon->getId() : null ),
  503.             "productSuggestion" => (
  504.                 $this->productSuggestion $this->productSuggestion->getId() : null 
  505.             ),
  506.             "user" => ( $this->user $this->user->getId() : null ),
  507.         ];
  508.         return $data;
  509.     }
  510. }