<?php
namespace EADPlataforma\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use EADPlataforma\Validator\Constraints as EADAssert;
use EADPlataforma\Enum\CartEnum;
use EADPlataforma\Enum\ProductEnum;
use EADPlataforma\Util\NumberUtil;
use \DateTime;
/**
* Cart
*
* @ORM\Table(name="cart", indexes={
* @ORM\Index(name="fk_cart_user_id", columns={"user_id"}),
* @ORM\Index(name="fk_cart_product_id", columns={"product_id"}),
* @ORM\Index(name="fk_cart_product_offer_id", columns={"product_offer_id"}),
* @ORM\Index(name="fk_cart_product_coupon_id", columns={"product_coupon_id"}),
* @ORM\Index(name="fk_cart_product_suggestion_id", columns={"product_suggestion_id"})
* })
*
* @ORM\Entity(repositoryClass="EADPlataforma\Repository\CartRepository")
*/
class Cart
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @Assert\NotBlank(
* message = "Deleted not informed"
* )
*
* @Assert\Choice(
* choices = {
* CartEnum::ITEM_NO_DELETED,
* CartEnum::ITEM_ON_TRASH,
* CartEnum::ITEM_DELETED
* },
* message = "Delete Option Invalid"
* )
*
* @var int
*
* @ORM\Column(name="deleted", type="integer", nullable=false, options={"default"="0"})
*/
private $deleted = CartEnum::ITEM_NO_DELETED;
/**
* @Assert\NotBlank(
* message = "Hash not informed"
* )
*
* @var string
*
* @ORM\Column(name="hash_identify", type="string", length=80, nullable=false)
*/
private $hashIdentify;
/**
* @Assert\NotBlank(
* message = "Price not informed"
* )
*
* @Assert\GreaterThanOrEqual(
* value = CartEnum::MIN_VALUE_PRODUCT,
* message = "Price invalid"
* )
*
* @var string
*
* @ORM\Column(name="price", type="decimal", precision=10, scale=2, nullable=false, options={"default"="0.00"})
*/
private $price = '0.00';
/**
* @Assert\NotBlank(
* message = "Membership Fee not informed"
* )
*
* @var string
*
* @ORM\Column(name="membership_fee", type="decimal", precision=10, scale=2, nullable=false, options={"default"="0.00"})
*/
private $membershipFee = '0.00';
/**
* @Assert\NotBlank(
* message = "Type not informed"
* )
*
* @Assert\Choice(
* choices = { CartEnum::CART, CartEnum::CHECKOUT },
* message = "Type Invalid"
* )
*
* @var int
*
* @ORM\Column(name="type", type="integer", nullable=false, options={"default"="1"})
*/
private $type = CartEnum::CART;
/**
* @Assert\NotBlank(
* message = "Able not informed"
* )
*
* @Assert\Choice(
* choices = { CartEnum::YES, CartEnum::NO },
* message = "Able Invalid"
* )
*
* @var int
*
* @ORM\Column(name="able", type="integer", nullable=false, options={"default"="1"})
*/
private $able = CartEnum::YES;
/**
* @Assert\NotBlank(
* message = "Product Type not informed"
* )
*
* @Assert\Choice(
* choices = {
* ProductEnum::COURSE,
* ProductEnum::SUBSCRIPTION,
* ProductEnum::COMBO,
* ProductEnum::CERTIFICATE,
* ProductEnum::PERIOD,
* ProductEnum::SUPPORT
* },
* message = "Type Invalid"
* )
*
* @var int
*
* @ORM\Column(name="product_type", type="integer", nullable=false)
*/
private $productType;
/**
* @Assert\Json(
* message = "Utms Url is an invalid Json."
* )
*
* @var string|null
*
* @ORM\Column(name="utms_url", type="text", length=0, nullable=true)
*/
private $utmsUrl;
/**
* @Assert\NotBlank(
* message = "Date not informed"
* )
*
* @EADAssert\DateTimeEAD(
* message = "Date Invalid"
* )
*
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime", nullable=false, options={"default"="CURRENT_TIMESTAMP"})
*/
private $date;
/**
* @var string|null
*
* @ORM\Column(name="error_message", type="text", length=0, nullable=true)
*/
private $errorMessage;
/**
* @var int
*
* @ORM\Column(name="pipedrive_deal_id", type="integer", nullable=true)
*/
private $pipedriveDeal;
/**
* @Assert\NotBlank(
* message = "Product not informed"
* )
*
* @Assert\Valid
*
* @var \Product
*
* @ORM\ManyToOne(targetEntity="Product")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
* })
*/
private $product;
/**
* @Assert\NotBlank(
* message = "Product Offer not informed"
* )
*
* @Assert\Valid
*
* @var \ProductOffer
*
* @ORM\ManyToOne(targetEntity="ProductOffer")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="product_offer_id", referencedColumnName="id", nullable=false)
* })
*/
private $productOffer;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Course", inversedBy="cart")
* @ORM\JoinTable(name="cart_x_course",
* joinColumns={
* @ORM\JoinColumn(name="cart_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="course_id", referencedColumnName="id")
* }
* )
*/
private $course;
/**
* @Assert\Valid
*
* @var \ProductCoupon
*
* @ORM\ManyToOne(targetEntity="ProductCoupon")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="product_coupon_id", referencedColumnName="id")
* })
*/
private $productCoupon;
/**
* @Assert\Valid
*
* @var \ProductCoupon
*
* @ORM\ManyToOne(targetEntity="ProductSuggestion")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="product_suggestion_id", referencedColumnName="id")
* })
*/
private $productSuggestion;
/**
* @Assert\Valid
*
* @var \User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
* })
*/
private $user;
/**
* Constructor
*/
public function __construct()
{
$this->date = new DateTime();
$this->course = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getHashIdentify(): ?string
{
return $this->hashIdentify;
}
public function setHashIdentify(?string $hashIdentify): self
{
$this->hashIdentify = $hashIdentify;
return $this;
}
public function getPrice($cents = false)
{
if($cents){
return NumberUtil::numberToCentsStatic($this->price);
}
return $this->price;
}
public function setPrice($price): self
{
$this->price = $price;
return $this;
}
public function getMembershipFee(?bool $cents = false)
{
if($cents){
return NumberUtil::numberToCentsStatic($this->membershipFee);
}
return $this->membershipFee;
}
public function setMembershipFee($membershipFee): self
{
$this->membershipFee = $membershipFee;
return $this;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
public function getAble(): ?int
{
return $this->able;
}
public function setAble(int $able): self
{
$this->able = $able;
return $this;
}
public function getProductType(): ?int
{
return $this->productType;
}
public function setProductType(int $productType): self
{
$this->productType = $productType;
return $this;
}
public function getUtmsUrl(): ?string
{
return $this->utmsUrl;
}
public function setUtmsUrl(?string $utmsUrl): self
{
$this->utmsUrl = $utmsUrl;
return $this;
}
public function getDate($dateFormat = 'Y-m-d H:i:s')
{
if(!empty($this->date)){
return $this->date->format($dateFormat);
}
return $this->date;
}
public function setDate($date): self
{
if(!empty($date)){
$date = DateTime::createFromFormat('Y-m-d H:i:s', $date);
}
$this->date = $date;
return $this;
}
public function getErrorMessage(): ?string
{
return $this->errorMessage;
}
public function setErrorMessage(?string $errorMessage): self
{
$this->errorMessage = $errorMessage;
return $this;
}
public function getPipedriveDeal(): ?int
{
return $this->pipedriveDeal;
}
public function setPipedriveDeal(?int $pipedriveDeal): self
{
$this->pipedriveDeal = $pipedriveDeal;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
public function getProductOffer(): ?ProductOffer
{
return $this->productOffer;
}
public function setProductOffer(?ProductOffer $productOffer): self
{
$this->productOffer = $productOffer;
return $this;
}
/**
* @return Collection|Course[]
*/
public function getCourse(): ?Collection
{
return $this->course;
}
public function addCourse(Course $course): self
{
if (!$this->course->contains($course)) {
$this->course[] = $course;
}
return $this;
}
public function removeCourse(Course $course): self
{
if ($this->course->contains($course)) {
$this->course->removeElement($course);
}
return $this;
}
public function removeAllCourse(): self
{
$this->course = new \Doctrine\Common\Collections\ArrayCollection();
return $this;
}
public function getProductCoupon(): ?ProductCoupon
{
return $this->productCoupon;
}
public function setProductCoupon(?ProductCoupon $productCoupon): self
{
$this->productCoupon = $productCoupon;
return $this;
}
public function getProductSuggestion(): ?ProductSuggestion
{
return $this->productSuggestion;
}
public function setProductSuggestion(?ProductSuggestion $productSuggestion): self
{
$this->productSuggestion = $productSuggestion;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function stringPaymentMethod($method)
{
$string = '';
switch ($method) {
case CartEnum::PAYMENT_CARD:
$string = 'Credit Card';
break;
case CartEnum::PAYMENT_BILL:
$string = 'Bill';
case CartEnum::PAYMENT_PIX:
$string = 'Pix';
break;
}
return $string;
}
public function isOnTrash(): bool
{
return ($this->deleted == CartEnum::ITEM_ON_TRASH);
}
public function isDeleted(): bool
{
return ($this->deleted == CartEnum::ITEM_DELETED);
}
public function restore(): self
{
$this->deleted = CartEnum::ITEM_NO_DELETED;
return $this;
}
public function trash(): self
{
$this->deleted = CartEnum::ITEM_ON_TRASH;
return $this;
}
public function delete(): self
{
$this->deleted = CartEnum::ITEM_DELETED;
return $this;
}
public function toReturn(){
$arrCourse = [];
foreach ($this->course as $key => $course) {
$arrCourse[] = $course->getId();
}
$data = [
"id" => $this->id,
"deleted" => $this->deleted,
"hashIdentify" => $this->hashIdentify,
"price" => $this->price,
"membershipFee" => $this->membershipFee,
"type" => $this->type,
"able" => $this->able,
"productType" => $this->productType,
"utmsUrl" => $this->utmsUrl,
"date" => $this->getDate(),
"errorMessage" => $this->errorMessage,
"pipedriveDeal" => $this->pipedriveDeal,
"product" => ( $this->product ? $this->product->getId() : null ),
"productOffer" => ( $this->productOffer ? $this->productOffer->getId() : null ),
"course" => $arrCourse,
"productCoupon" => ( $this->productCoupon ? $this->productCoupon->getId() : null ),
"productSuggestion" => (
$this->productSuggestion ? $this->productSuggestion->getId() : null
),
"user" => ( $this->user ? $this->user->getId() : null ),
];
return $data;
}
}