src/Bidcoz/Bundle/CoreBundle/Entity/Auction/Auction.php line 31

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\CoreBundle\Entity\Auction;
  3. use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
  4. use Bidcoz\Bundle\CoreBundle\ObjectChange\ObjectChangeTrackable;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use JMS\Serializer\Annotation as Serialize;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  11. /**
  12.  * @ORM\Entity(repositoryClass="AuctionRepository")
  13.  * @ORM\Table(
  14.  *     name="auctions",
  15.  *      uniqueConstraints={
  16.  *          @ORM\UniqueConstraint(name="uniq_72d6e900f639f774", columns={"campaign_id", "type"})
  17.  *      }
  18.  * )
  19.  * @ORM\InheritanceType("SINGLE_TABLE")
  20.  * @ORM\DiscriminatorColumn(name="type", type="string", length=20)
  21.  * @ORM\DiscriminatorMap({
  22.  *      "auction" = "Auction",
  23.  *      "shop" = "Shop",
  24.  *      "fund-a-need" = "FundANeed",
  25.  * })
  26.  * @Serialize\ExclusionPolicy("all")
  27.  */
  28. class Auction implements ObjectChangeTrackable
  29. {
  30.     public const OVERTIME_NONE      'none';
  31.     public const OVERTIME_SINGLE    'single';
  32.     public const OVERTIME_UNLIMITED 'unlimited';
  33.     /**
  34.      * @ORM\Id
  35.      * @ORM\Column(name="id", type="integer")
  36.      * @ORM\GeneratedValue(strategy="AUTO")
  37.      * @Serialize\Expose
  38.      */
  39.     protected ?int $id null;
  40.     /**
  41.      * @ORM\OneToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\Campaign", inversedBy="auction")
  42.      * @ORM\JoinColumn(name="campaign_id", referencedColumnName="id", onDelete="SET NULL")
  43.      */
  44.     protected $campaign;
  45.     /**
  46.      * @ORM\Column(name="online_begin", type="datetime", nullable=true);
  47.      * @Serialize\Expose
  48.      */
  49.     protected $onlineBegin;
  50.     /**
  51.      * @ORM\Column(name="online_end", type="datetime", nullable=true);
  52.      * @Serialize\Expose
  53.      */
  54.     protected $onlineEnd;
  55.     /**
  56.      * @ORM\Column(name="onsite_begin", type="datetime", nullable=true);
  57.      * @Serialize\Expose
  58.      */
  59.     protected $onsiteBegin;
  60.     /**
  61.      * @ORM\Column(name="onsite_end", type="datetime", nullable=true);
  62.      * @Serialize\Expose
  63.      */
  64.     protected $onsiteEnd;
  65.     /**
  66.      * @ORM\Column(name="has_online", type="boolean")
  67.      */
  68.     protected $hasOnline true;
  69.     /**
  70.      * @ORM\Column(name="has_onsite", type="boolean")
  71.      */
  72.     protected $hasOnsite false;
  73.     /**
  74.      * @ORM\Column(name="online_ticket_required", type="boolean")
  75.      */
  76.     protected $onlineTicketRequired false;
  77.     /**
  78.      * @ORM\Column(name="onsite_ticket_required", type="boolean")
  79.      */
  80.     protected $onsiteTicketRequired false;
  81.     /**
  82.      * @ORM\Column(name="online_registration_required", type="boolean")
  83.      */
  84.     protected $onlineRegistrationRequired false;
  85.     /**
  86.      * @ORM\Column(name="onsite_registration_required", type="boolean")
  87.      */
  88.     protected $onsiteRegistrationRequired false;
  89.     /**
  90.      * @ORM\Column(name="location", type="text", nullable=true);
  91.      * @Serialize\Expose
  92.      */
  93.     protected $location;
  94.     /**
  95.      * @ORM\Column(name="location_link", type="boolean")
  96.      */
  97.     protected $locationLink true;
  98.     /**
  99.      * @ORM\Column(name="bid_increment", type="decimal", scale=2, precision=10, nullable=true)
  100.      * @Assert\Range(min=0)
  101.      */
  102.     protected $bidIncrement 5;
  103.     /**
  104.      * @ORM\Column(name="pickup_instructions", type="text", nullable=true)
  105.      */
  106.     protected $pickupInstructions;
  107.     /**
  108.      * @ORM\Column(name="bidding_rules", type="text", nullable=true);
  109.      */
  110.     protected $biddingRules;
  111.     /**
  112.      * @ORM\Column(name="overtime_bidding", type="string", length=10)
  113.      */
  114.     protected $overtimeBidding self::OVERTIME_NONE;
  115.     /**
  116.      * @var ItemCategory[]|Collection|null
  117.      * @ORM\OneToMany(targetEntity="ItemCategory", mappedBy="auction", cascade={"remove", "persist"})
  118.      */
  119.     protected ?Collection $itemCategories null;
  120.     /**
  121.      * @var Item[]|Collection|null
  122.      * @ORM\OneToMany(targetEntity="Item", mappedBy="auction", cascade={"remove", "persist"})
  123.      */
  124.     protected ?Collection $items null;
  125.     /**
  126.      * @ORM\Column(name="created_at", type="datetime")
  127.      * @Serialize\Expose
  128.      */
  129.     protected $createdAt;
  130.     /**
  131.      * @ORM\Column(name="show_auction_title", type="boolean", options={"default"= true})
  132.      */
  133.     protected $showAuctionTitle true;
  134.     /**
  135.      * @ORM\Column(name="show_qty_remaining", type="boolean", options={"default"= true})
  136.      */
  137.     protected $showQtyRemaining true;
  138.     /**
  139.      * @ORM\Column(name="are_you_sure_modal", type="boolean");
  140.      */
  141.     protected $areYouSureModal false;
  142.     public function __construct(Campaign $campaign)
  143.     {
  144.         $this->campaign $campaign;
  145.         $campaign->setAuction($this);
  146.         $this->itemCategories = new ArrayCollection();
  147.         $this->items          = new ArrayCollection();
  148.         $this->createdAt      = new \DateTime();
  149.     }
  150.     public function setId(?int $id)
  151.     {
  152.         $this->id $id;
  153.     }
  154.     public function getId()
  155.     {
  156.         return $this->id;
  157.     }
  158.     public function getCampaign()
  159.     {
  160.         return $this->campaign;
  161.     }
  162.     public function setCampaign(Campaign $campaign): void
  163.     {
  164.         $this->campaign $campaign;
  165.     }
  166.     public function setOnlineBegin(\DateTime $date null)
  167.     {
  168.         $this->onlineBegin $date;
  169.     }
  170.     public function getOnlineBegin()
  171.     {
  172.         return $this->onlineBegin;
  173.     }
  174.     public function setOnlineEnd(\DateTime $date null)
  175.     {
  176.         $this->onlineEnd $date;
  177.     }
  178.     public function getOnlineEnd()
  179.     {
  180.         return $this->onlineEnd;
  181.     }
  182.     public function isOver()
  183.     {
  184.         $now = new \DateTime();
  185.         if ($this->hasOnsite && $now $this->onsiteEnd) {
  186.             return false;
  187.         }
  188.         if ($this->hasOnline && $now $this->onlineEnd) {
  189.             return false;
  190.         }
  191.         return true;
  192.     }
  193.     public function getStatus(): string
  194.     {
  195.         $now            = new \DateTime();
  196.         $lastEndDate    $this->getLastEndDate();
  197.         $firstStartDate $this->getFirstStartDate();
  198.         if ($now $lastEndDate) {
  199.             return 'Past';
  200.         } elseif ($firstStartDate $now) {
  201.             return 'Future';
  202.         } else {
  203.             return 'Current';
  204.         }
  205.     }
  206.     public function getFirstStartDate()
  207.     {
  208.         $dates = [];
  209.         if ($this->hasOnline) {
  210.             $dates[] = $this->onlineBegin;
  211.         }
  212.         if ($this->hasOnsite) {
  213.             $dates[] = $this->onsiteBegin;
  214.         }
  215.         $dates array_filter($dates, function ($date) {
  216.             return !is_null($date);
  217.         });
  218.         if (=== count($dates)) {
  219.             return;
  220.         }
  221.         return min($dates);
  222.     }
  223.     public function getLastEndDate()
  224.     {
  225.         $dates = [];
  226.         if ($this->hasOnline) {
  227.             $dates[] = $this->onlineEnd;
  228.         }
  229.         if ($this->hasOnsite) {
  230.             $dates[] = $this->onsiteEnd;
  231.         }
  232.         $dates array_filter($dates, function ($date) {
  233.             return !is_null($date);
  234.         });
  235.         if (=== count($dates)) {
  236.             return;
  237.         }
  238.         return max($dates);
  239.     }
  240.     /**
  241.      * @Assert\Callback
  242.      */
  243.     public function ensureOnlineDatesValid(ExecutionContextInterface $context)
  244.     {
  245.         // Fields onlineBegin and onlineEnd are nullable, however they are checked when updating Auction that 'hasOnline'.
  246.         // Skip this check for Shop and Fund-a-Need entities that don't update these fields anywhere.
  247.         if ($this->hasOnline && Auction::class === get_class($this)) {
  248.             if (!$this->onlineBegin) {
  249.                 $context->buildViolation('Start Date required for Online Auction')
  250.                     ->atPath('onlineBegin')
  251.                     ->addViolation();
  252.             } elseif (!$this->onlineEnd) {
  253.                 $context->buildViolation('End Date required for Online Auction')
  254.                     ->atPath('onlineEnd')
  255.                     ->addViolation();
  256.             } elseif ($this->onlineBegin $this->onlineEnd) {
  257.                 $context->buildViolation('Start Date cannot be after End Date')
  258.                     ->atPath('onlineEnd')
  259.                     ->addViolation();
  260.             }
  261.         }
  262.     }
  263.     public function setOnsiteBegin(\DateTime $date null)
  264.     {
  265.         $this->onsiteBegin $date;
  266.     }
  267.     public function getOnsiteBegin()
  268.     {
  269.         return $this->onsiteBegin;
  270.     }
  271.     public function setOnsiteEnd(\DateTime $date null)
  272.     {
  273.         $this->onsiteEnd $date;
  274.     }
  275.     public function getOnsiteEnd()
  276.     {
  277.         return $this->onsiteEnd;
  278.     }
  279.     /**
  280.      * @Assert\Callback
  281.      */
  282.     public function ensureOnsiteDatesValid(ExecutionContextInterface $context)
  283.     {
  284.         if ($this->hasOnsite) {
  285.             if (!$this->onsiteBegin) {
  286.                 $context->buildViolation('Start Date required for Onsite Auction')
  287.                     ->atPath('onsiteBegin')
  288.                     ->addViolation();
  289.             } elseif (!$this->onsiteEnd) {
  290.                 $context->buildViolation('End Date required for Onsite Auction')
  291.                     ->atPath('onsiteEnd')
  292.                     ->addViolation();
  293.             } elseif ($this->onsiteBegin $this->onsiteEnd) {
  294.                 $context->buildViolation('Start Date cannot be after End Date')
  295.                     ->atPath('onsiteBegin')
  296.                     ->addViolation();
  297.             }
  298.         }
  299.     }
  300.     public function setHasOnline($hasOnline)
  301.     {
  302.         $this->hasOnline $hasOnline;
  303.     }
  304.     public function hasOnline()
  305.     {
  306.         return $this->hasOnline;
  307.     }
  308.     public function setHasOnsite($hasOnsite)
  309.     {
  310.         $this->hasOnsite $hasOnsite;
  311.     }
  312.     public function hasOnsite()
  313.     {
  314.         return $this->hasOnsite;
  315.     }
  316.     public function isAvailable(): bool
  317.     {
  318.         return ($this->hasOnsite() || $this->hasOnline()) && !$this->isOver();
  319.     }
  320.     public function setOnlineTicketRequired($onlineTicketRequired)
  321.     {
  322.         $this->onlineTicketRequired $onlineTicketRequired;
  323.     }
  324.     public function getOnlineTicketRequired()
  325.     {
  326.         return $this->onlineTicketRequired;
  327.     }
  328.     public function setOnsiteTicketRequired($onsiteTicketRequired)
  329.     {
  330.         $this->onsiteTicketRequired $onsiteTicketRequired;
  331.     }
  332.     public function getOnsiteTicketRequired()
  333.     {
  334.         return $this->onsiteTicketRequired;
  335.     }
  336.     public function setOnlineRegistrationRequired($onlineRegistrationRequired)
  337.     {
  338.         $this->onlineRegistrationRequired $onlineRegistrationRequired;
  339.     }
  340.     public function getOnlineRegistrationRequired()
  341.     {
  342.         return $this->onlineRegistrationRequired;
  343.     }
  344.     public function setOnsiteRegistrationRequired($onsiteRegistrationRequired)
  345.     {
  346.         $this->onsiteRegistrationRequired $onsiteRegistrationRequired;
  347.     }
  348.     public function getOnsiteRegistrationRequired()
  349.     {
  350.         return $this->onsiteRegistrationRequired;
  351.     }
  352.     public function setLocation($location)
  353.     {
  354.         $this->location $location;
  355.     }
  356.     public function getLocation()
  357.     {
  358.         return $this->location;
  359.     }
  360.     public function setLocationLink(bool $locationLink)
  361.     {
  362.         $this->locationLink $locationLink;
  363.     }
  364.     public function getLocationLink(): bool
  365.     {
  366.         return $this->locationLink;
  367.     }
  368.     public function setBidIncrement($bidIncrement)
  369.     {
  370.         $this->bidIncrement $bidIncrement;
  371.     }
  372.     public function getBidIncrement()
  373.     {
  374.         return $this->bidIncrement;
  375.     }
  376.     public function setPickupInstructions($pickupInstructions)
  377.     {
  378.         $this->pickupInstructions $pickupInstructions;
  379.     }
  380.     public function getPickupInstructions()
  381.     {
  382.         return $this->pickupInstructions;
  383.     }
  384.     public function setOvertimeBidding($overtimeBidding)
  385.     {
  386.         $this->overtimeBidding $overtimeBidding;
  387.     }
  388.     public function getOvertimeBidding()
  389.     {
  390.         return $this->overtimeBidding;
  391.     }
  392.     public static function getOvertimeBiddingChoices()
  393.     {
  394.         return [
  395.             'None'                      => self::OVERTIME_NONE,
  396.             'Single time extension'     => self::OVERTIME_SINGLE,
  397.             'Unlimited time extensions' => self::OVERTIME_UNLIMITED,
  398.         ];
  399.     }
  400.     public function isOvertimeBiddingSingleExtension()
  401.     {
  402.         return self::OVERTIME_SINGLE === $this->overtimeBidding;
  403.     }
  404.     public function isOvertimeBiddingUnlimitedExtension()
  405.     {
  406.         return self::OVERTIME_UNLIMITED === $this->overtimeBidding;
  407.     }
  408.     public function getVenueChoices()
  409.     {
  410.         $choices = [];
  411.         if ($this->hasOnline) {
  412.             $choices[AuctionVenue::ONLINE] = 'Online';
  413.         }
  414.         if ($this->hasOnsite) {
  415.             $choices[AuctionVenue::ONSITE] = 'Onsite';
  416.         }
  417.         if ($this->hasOnline && $this->hasOnsite) {
  418.             // TODO - Figure out this logic
  419.             // $choices[null] = 'Both (Online and Onsite)';
  420.         }
  421.         return $choices;
  422.     }
  423.     /**
  424.      * @return ItemCategory[]|Collection|null
  425.      */
  426.     public function getItemCategories(): ?Collection
  427.     {
  428.         return $this->itemCategories;
  429.     }
  430.     public function addItemCategory(ItemCategory $itemCategory): void
  431.     {
  432.         if (!$this->itemCategories->contains($itemCategory)) {
  433.             $this->itemCategories->add($itemCategory);
  434.             $itemCategory->setAuction($this);
  435.         }
  436.     }
  437.     public function removeItemCategory(ItemCategory $itemCategory): void
  438.     {
  439.         if ($this->itemCategories->contains($itemCategory)) {
  440.             $this->itemCategories->removeElement($itemCategory);
  441.             $itemCategory->setAuction(null);
  442.         }
  443.     }
  444.     public function getShowAuctionTitle(): bool
  445.     {
  446.         return $this->showAuctionTitle;
  447.     }
  448.     public function getShowQtyRemaining(): bool
  449.     {
  450.         return $this->showQtyRemaining;
  451.     }
  452.     public function setShowQtyRemaining($showQtyRemaining)
  453.     {
  454.         $this->showQtyRemaining $showQtyRemaining;
  455.     }
  456.     public function getObjectName()
  457.     {
  458.         return 'Auction';
  459.     }
  460.     public function setShopDisplayName(string $shopDisplayName)
  461.     {
  462.         $this->campaign->setShopDisplayName($shopDisplayName);
  463.     }
  464.     public function getShopDisplayName(): string
  465.     {
  466.         return $this->campaign->getShopDisplayName();
  467.     }
  468.     public function setFundANeedDisplayName(string $shopDisplayName)
  469.     {
  470.         $this->campaign->setFundANeedDisplayName($shopDisplayName);
  471.     }
  472.     public function getFundANeedDisplayName(): string
  473.     {
  474.         return $this->campaign->getFundANeedDisplayName();
  475.     }
  476.     /**
  477.      * @return Collection|Item[]|null
  478.      */
  479.     public function getItems(): ?Collection
  480.     {
  481.         return $this->items;
  482.     }
  483.     public function setItems(?Collection $items): void
  484.     {
  485.         $this->items $items;
  486.     }
  487.     public function addItem(Item $item): void
  488.     {
  489.         $this->items->add($item);
  490.         $item->setAuction($this);
  491.     }
  492.     public function removeItem(Item $item): void
  493.     {
  494.         $this->items->removeElement($item);
  495.         $item->setAuction(null);
  496.     }
  497.     public function setShopGoal(string $goal)
  498.     {
  499.         $this->campaign->setShopGoal($goal);
  500.     }
  501.     public function getShopGoal(): string
  502.     {
  503.         return $this->campaign->getShopGoal();
  504.     }
  505.     public function setFundANeedGoal(string $goal)
  506.     {
  507.         $this->campaign->setFundANeedGoal($goal);
  508.     }
  509.     public function getFundANeedGoal(): string
  510.     {
  511.         return $this->campaign->getFundANeedGoal();
  512.     }
  513.     public function getAreYouSureModal(): bool
  514.     {
  515.         return $this->areYouSureModal;
  516.     }
  517.     public function setAreYouSureModal(bool $areYouSureModal): void
  518.     {
  519.         $this->areYouSureModal $areYouSureModal;
  520.     }
  521.     public function getTrackedParams()
  522.     {
  523.         return [
  524.             'overtimeBidding' => 'Overtime bidding',
  525.             'onlineBegin'     => 'Online Begin',
  526.             'onlineEnd'       => 'Online End',
  527.             'onsiteBegin'     => 'Event Begin',
  528.             'onsiteEnd'       => 'Event End',
  529.             'hasOnline'       => 'Has Online',
  530.             'hasOnsite'       => 'Has Event',
  531.         ];
  532.     }
  533.     public function getRoutePath(): string
  534.     {
  535.         return 'auction';
  536.     }
  537. }