<?php
namespace Customize\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Eccube\Entity\Member;
use Eccube\Entity\Tag;
if (! class_exists('\Customize\Entity\Auction')) {
/**
* オークションテーブル
*
* @ORM\Table(name="dtb_auction")
*
* @ORM\InheritanceType("SINGLE_TABLE")
*
* @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
*
* @ORM\HasLifecycleCallbacks()
*
* @ORM\Entity(repositoryClass="Customize\Repository\AuctionRepository")
*/
class Auction extends \Eccube\Entity\AbstractEntity
{
/**
* id
*
* @var int
*
* @ORM\Column(name="id", type="integer", options={"unsigned":true, "comment":"ID"})
*
* @ORM\Id
*
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* オークション名
*
* @var string|null
*
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* 下見開始日時
*
* @var \DateTime|null
*
* @ORM\Column(name="preview_start_date", type="datetimetz", nullable=true)
*/
private $preview_start_date;
/**
* オークション開始予定日時
*
* @var \DateTime|null
*
* @ORM\Column(name="schedule_start_date", type="datetimetz", nullable=true)
*/
private $schedule_start_date;
/**
* @var int
*
* @ORM\Column(name="negotiation_period_hour", type="datetimetz", nullable=true)
*/
private $negotiation_period_date;
/**
* オークション説明
*
* @var string|null
*
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* 競り開始日時
*
* @var \DateTime|null
*
* @ORM\Column(name="start_date", type="datetimetz", nullable=true)
*/
private $start_date;
/**
* 競り終了日時
*
* @var \DateTime|null
*
* @ORM\Column(name="end_date", type="datetimetz", nullable=true)
*/
private $end_date;
/**
* 作成日時
*
* @var \DateTime|null
*
* @ORM\Column(name="create_date", type="datetimetz", nullable=true)
*/
private $create_date;
/**
* 終了日時
*
* @var \DateTime|null
*
* @ORM\Column(name="update_date", type="datetimetz", nullable=true)
*/
private $update_date;
/**
* 公開日時
*
* @var \DateTime|null
*
* @ORM\Column(name="publish_date", type="datetimetz", nullable=true)
*/
private $publish_date;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Customize\Entity\AuctionImage", mappedBy="Auction", cascade={"remove"})
*
* @ORM\OrderBy({
* "sort_no"="ASC"
* })
*/
private $AuctionImage;
/**
* @var \Customize\Entity\Master\AuctionStatus
*
* @ORM\ManyToOne(targetEntity="Customize\Entity\Master\AuctionStatus")
*
* @ORM\JoinColumns({
*
* @ORM\JoinColumn(name="auction_status_id", referencedColumnName="id", nullable=true)
* })
*/
private $Status;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Customize\Entity\AuctionCategory", mappedBy="Auction", cascade={"persist","remove"})
*/
private $AuctionCategories;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Customize\Entity\AuctionTag", mappedBy="Auction", cascade={"remove"})
*/
private $AuctionTags;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Customize\Entity\CustomerFavoriteAuction", mappedBy="Auction")
*/
private $CustomerFavoriteAuctions;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Eccube\Entity\Product", mappedBy="Auction")
*/
private $Products;
/**
* @var \Eccube\Entity\Member
*
* @ORM\ManyToOne(targetEntity="Eccube\Entity\Member")
*
* @ORM\JoinColumns({
*
* @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
* })
*/
private $Creator;
/**
* 準備中説明文言
*
* @var string|null
*
* @ORM\Column(name="ready_message", type="text", nullable=true)
*/
private $ready_message;
/**
* 管理者用フラグ
*
* @var \DateTime|null
*
* @ORM\Column(name="is_admin", type="boolean", options={"default":false})
*/
private $is_admin = false;
/**
* Constructor
*/
public function __construct()
{
$this->AuctionCategories = new \Doctrine\Common\Collections\ArrayCollection();
$this->AuctionImage = new \Doctrine\Common\Collections\ArrayCollection();
$this->AuctionTags = new \Doctrine\Common\Collections\ArrayCollection();
}
public function copy()
{
$Categories = $this->getAuctionCategories();
$this->AuctionCategories = new ArrayCollection();
foreach ($Categories as $Category) {
$CopyCategory = clone $Category;
$this->addAuctionCategory($CopyCategory);
$CopyCategory->setAuction($this);
}
$Images = $this->getAuctionImage();
$this->AuctionImage = new ArrayCollection();
foreach ($Images as $Image) {
$CloneImage = clone $Image;
$this->addAuctionImage($CloneImage);
$CloneImage->setAuction($this);
}
$Tags = $this->getAuctionTags();
$this->AuctionTags = new ArrayCollection();
foreach ($Tags as $Tag) {
$CloneTag = clone $Tag;
$this->addAuctionTag($CloneTag);
$CloneTag->setAuction($this);
}
return $this;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return Auction
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @return \DateTime
*/
public function getPreviewStartDate()
{
return $this->preview_start_date;
}
/**
* @param string $previewStartDate
* @return Auction
*/
public function setPreviewStartDate($previewStartDate)
{
$this->preview_start_date = $previewStartDate;
return $this;
}
/**
* @return \DateTime
*/
public function getScheduleStartDate()
{
return $this->schedule_start_date;
}
/**
* @param \Datetime $scheduleStartDate
* @return Auction
*/
public function setScheduleStartDate($scheduleStartDate)
{
$this->schedule_start_date = $scheduleStartDate;
return $this;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
* @return Auction
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* @return string
*/
public function getReadyMessage()
{
return $this->ready_message;
}
/**
* @param string $readyMessage
* @return Auction
*/
public function setReadyMessage($readyMessage)
{
$this->ready_message = $readyMessage;
return $this;
}
/**
* @return \DateTime
*/
public function getStartDate()
{
return $this->start_date;
}
/**
* @param \Datetime $startDate
* @return Auction
*/
public function setStartDate($startDate)
{
$this->start_date = $startDate;
return $this;
}
/**
* @return \DateTime
*/
public function getEndDate()
{
return $this->end_date;
}
/**
* @param \Datetime $endDate
* @return Auction
*/
public function setEndDate($endDate)
{
$this->end_date = $endDate;
return $this;
}
/**
* Set createDate.
*
* @param \DateTime $createDate
* @return Auction
*/
public function setCreateDate($createDate)
{
$this->create_date = $createDate;
return $this;
}
/**
* Get createDate.
*
* @return \DateTime
*/
public function getCreateDate()
{
return $this->create_date;
}
/**
* Set updateDate.
*
* @param \DateTime $updateDate
* @return Auction
*/
public function setUpdateDate($updateDate)
{
$this->update_date = $updateDate;
return $this;
}
/**
* Get updateDate.
*
* @return \DateTime
*/
public function getUpdateDate()
{
return $this->update_date;
}
/**
* Set negotiationPeriodDate.
*
* @param \DateTime $negotiationPeriodDate
* @return Auction
*/
public function setNegotiationPeriodDate($negotiationPeriodDate)
{
$this->negotiation_period_date = $negotiationPeriodDate;
return $this;
}
/**
* Get negotiationPeriodDate.
*
* @return \DateTime|null
*/
public function getNegotiationPeriodDate()
{
return $this->negotiation_period_date;
}
/**
* Set updateDate.
*
* @param \DateTime $publishDate
* @return Auction
*/
public function setPublishDate($publishDate)
{
$this->publish_date = $publishDate;
return $this;
}
/**
* Get updateDate.
*
* @return \DateTime|null
*/
public function getPublishDate()
{
return $this->publish_date;
}
/**
* Add productImage.
*
*
* @return Auction
*/
public function addAuctionImage(AuctionImage $auctionImage)
{
$this->AuctionImage[] = $auctionImage;
return $this;
}
/**
* Remove auctionImage.
*
*
* @return bool TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeAuctionImage(AuctionImage $auctionImage)
{
return $this->AuctionImage->removeElement($auctionImage);
}
/**
* Get auctionImage.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAuctionImage()
{
return $this->AuctionImage;
}
public function getMainListImage()
{
$AuctionImages = $this->getAuctionImage();
return $AuctionImages->isEmpty() ? null : $AuctionImages[0];
}
public function getMainFileName()
{
if (count($this->AuctionImage) > 0) {
return $this->AuctionImage[0];
} else {
return null;
}
}
/**
* Set status.
*
*
* @return Auction
*/
public function setStatus(?Master\AuctionStatus $status = null)
{
$this->Status = $status;
return $this;
}
/**
* Get status.
*
* @return \Customize\Entity\Master\AuctionStatus|null
*/
public function getStatus()
{
return $this->Status;
}
/**
*
* @param boolean $isAdmin
*
* @return Auction
*/
public function setIsAdmin($isAdmin)
{
$this->is_admin = $isAdmin;
return $this;
}
/**
*
* @return boolean
*/
public function getIsAdmin()
{
return $this->is_admin;
}
/**
* Add auctionCategory.
*
*
* @return Auction
*/
public function addAuctionCategory(AuctionCategory $auctionCategory)
{
$this->AuctionCategories[] = $auctionCategory;
return $this;
}
/**
* Remove auctionCategory.
*
*
* @return bool TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeAuctionCategory(AuctionCategory $auctionCategory)
{
return $this->AuctionCategories->removeElement($auctionCategory);
}
/**
* Get auctionCategories.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAuctionCategories()
{
return $this->AuctionCategories;
}
/**
* Get auctionTags.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAuctionTags()
{
return $this->AuctionTags;
}
/**
* Add auctionTag.
*
*
* @return Auction
*/
public function addAuctionTag(AuctionTag $auctionTag)
{
$this->AuctionTags[] = $auctionTag;
return $this;
}
/**
* Remove productTag.
*
*
* @return bool TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeAuctionTag(AuctionTag $auctionTag)
{
return $this->AuctionTags->removeElement($auctionTag);
}
/**
* Get Tag
* フロント側タグsort_no順の配列を作成する
*
* @return []Tag
*/
public function getTags()
{
$tags = [];
if (! is_null($this->getAuctionTags())) {
foreach ($this->getAuctionTags() as $auctionTag) {
$tags[] = $auctionTag->getTag();
}
}
usort($tags, function (Tag $tag1, Tag $tag2) {
return $tag1->getSortNo() < $tag2->getSortNo();
});
return $tags;
}
/**
* Set creator.
*
*
* @return Product
*/
public function setCreator(?Member $creator = null)
{
$this->Creator = $creator;
return $this;
}
/**
* Get creator.
*
* @return \Eccube\Entity\Member|null
*/
public function getCreator()
{
return $this->Creator;
}
/**
* Get customerFavoriteAuctions.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCustomerFavoriteAuctions()
{
return $this->CustomerFavoriteAuctions;
}
/**
* Get products.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
$Products = [];
foreach ($this->Products as $product) {
if ($product->getStatus()->getName() == '公開') {
$Products[] = $product;
}
}
return new ArrayCollection($Products);
}
/**
* Get products.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProductsSortAuctionNo()
{
$Products = [];
foreach ($this->Products as $product) {
if ($product->getStatus()->getName() == '公開') {
$Products[] = $product;
}
}
// auction_noで昇順ソート
usort($Products, function($a, $b) {
return $a->getAuctionNo() <=> $b->getAuctionNo();
});
return new ArrayCollection($Products);
}
public function hasAuctionCategories()
{
foreach ($this->AuctionCategories as $AuctionCategory) {
if (! is_null($AuctionCategory)) {
return true;
}
}
return false;
}
public function getAuctionCategoryNames()
{
$categoryNames = [];
foreach ($this->AuctionCategories as $AuctionCategory) {
if (is_null($AuctionCategory)) {
break;
}
$categoryNames[] = $AuctionCategory->Category->getName();
}
return $categoryNames;
}
public function hasAuctionTags()
{
foreach ($this->AuctionTags as $AuctionTag) {
if (! is_null($AuctionTag)) {
return true;
}
}
return false;
}
public function getAuctionTagNames()
{
$tagNames = [];
foreach ($this->AuctionTags as $AuctionTag) {
if (is_null($AuctionTag)) {
break;
}
$tagNames[] = $AuctionTag->Tag->getName();
}
return $tagNames;
}
public function countCustomerFavoriteAuctions()
{
return count($this->CustomerFavoriteAuctions);
}
public function isFavorite($Customer)
{
$is_favorite = false;
if (is_null($Customer)) {
return $is_favorite;
}
foreach ($this->CustomerFavoriteAuctions as $favoriteAuction) {
$is_favorite = $favoriteAuction->checkFavoriteFromAuction($Customer);
if ($is_favorite) {
return $is_favorite;
}
}
return $is_favorite;
}
public function getDetailPath()
{
$detailPath = 'auction_detail';
$isStarting = $this->isStarting();
if ($isStarting) {
$detailPath = 'starting_auction_detail';
}
return $detailPath;
}
public function isBefore()
{
$now = new \DateTime();
return $this->preview_start_date > $now;
}
public function isPreview()
{
$now = new \DateTime();
return $this->preview_start_date <= $now && is_null($this->start_date);
}
public function isStarting()
{
if ($this->getStatus() == '非公開') {
return false;
}
$now = new \DateTime();
$after30min = $now->modify('+30 minutes');
return $this->schedule_start_date <= $after30min && is_null($this->end_date);
}
public function isStartingForAdmin()
{
if ($this->getStatus() == '非公開') {
return false;
}
return is_null($this->end_date);
}
public function isEnding()
{
if (is_null($this->start_date) || is_null($this->end_date)) {
return false;
}
return true;
}
/*
* 総落札数
*/
public function countTotalSuccessFulBid(): int
{
$countTotalSuccessFulBid = 0;
$products = $this->Products;
// priceがnullの商品は落札されていないため除外
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if (
$auctionResult->getStatusName() == '成約済み' ||
$auctionResult->getStatusName() == '交渉中' ||
$auctionResult->getStatusName() == '保留' && $auctionResult->getPrice() != null
) {
$countTotalSuccessFulBid++;
}
}
return $countTotalSuccessFulBid;
}
/*
* 成約数
*/
public function countNumberOfDeal()
{
$countNumberOfDeals = 0;
$products = $this->Products;
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if ($auctionResult->getStatusName() == '成約済み') {
$countNumberOfDeals++;
}
}
return $countNumberOfDeals;
}
/*
* 保留数
*/
public function countHold()
{
$countHold = 0;
$products = $this->Products;
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if ($auctionResult->getStatusName() == '保留') {
$countHold++;
}
}
return $countHold;
}
/*
* 不成約数
*/
public function countNotContract()
{
$countNoContract = 0;
$products = $this->Products;
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if ($auctionResult->getStatusName() == '次回出品' || $auctionResult->getStatusName() == '返品') {
$countNoContract++;
}
}
return $countNoContract;
}
/*
* 交渉中数
*/
public function countNowNegotiation()
{
$countNowNegotiation = 0;
$products = $this->Products;
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if ($auctionResult->getStatusName() == '交渉中') {
$countNowNegotiation++;
}
}
return $countNowNegotiation;
}
/*
* 次回出品数
*/
public function countNextSell()
{
$countNextSell = 0;
$products = $this->Products;
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if ($auctionResult->getStatusName() == '次回出品') {
$countNextSell++;
}
}
return $countNextSell;
}
public function isClosed()
{
return !is_null($this->start_date) && !is_null($this->end_date);
}
public function countProducts()
{
$displayProducts = [];
foreach ($this->Products as $Product) {
if ($Product->isEnable()) {
$displayProducts[] = $Product;
}
}
return count($displayProducts);
}
public function getCorrectionListProducts()
{
$correctionListProducts = [];
foreach ($this->Products as $product) {
if (is_null($product->getCorrectionList())) {
continue;
}
$correctionListProducts[] = $product;
}
return $correctionListProducts;
}
/**
* @return array
*/
public function getCanMakeOrderProductsByCustomer(AuctionResult $AuctionResult)
{
$productsByCustomer = [];
$excludedCustomers = [];
$products = $this->getProducts();
$beforeFlushProduct = $AuctionResult->getProduct();
// 落札確定APIで、最後の商品のオークション結果はDBにまだcommitされないためSET
foreach ($products as $product) {
if ($product->getId() == $beforeFlushProduct->getId()) {
$product->setAuctionResult($AuctionResult);
}
}
// 最初のループで保留商品がある顧客を特定
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
$customer = $auctionResult->getCustomer();
$productCustomer = $product->getCustomer();
if ($customer !== null && $productCustomer && $auctionResult->getStatusName() == '保留') {
$excludedCustomers[$customer->getId()] = true;
$excludedCustomers[$productCustomer->getId()] = true;
}
}
// 売りと買いでどちらも保留商品がない顧客の商品を配列に追加
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if (
$auctionResult->getStatusName() == '返品' ||
$auctionResult->getStatusName() == '次回出品'
) {
continue;
}
if ($auctionResult->getCustomer() == null) {
continue;
}
$successfulBidder = $auctionResult->getCustomer();
$seller = $product->getCustomer();
// 落札商品
if ($successfulBidder !== null && !isset($excludedCustomers[$successfulBidder->getId()])) {
$successfulBidderId = $successfulBidder->getId();
if (!isset($productsByCustomer[$successfulBidderId])) {
$productsByCustomer[$successfulBidderId] = [];
}
$productsByCustomer[$successfulBidderId]['successful_bid'][] = $product;
}
// 出品商品
if ($seller !== null && !isset($excludedCustomers[$seller->getId()])) {
$sellerId = $seller->getId();
if (!isset($productsByCustomer[$sellerId])) {
$productsByCustomer[$sellerId] = [];
}
$productsByCustomer[$sellerId]['sell'][] = $product;
}
}
return $productsByCustomer;
}
public function getOrderBySuccessfulBidCustomer(AuctionResult $auctionResult): array
{
$productsByCustomer = [];
$customer = $auctionResult->getCustomer();
$products = $this->getProducts();
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if ($auctionResult->getStatusName() == '返品' || $auctionResult->getStatusName() == '次回出品') {
continue;
}
$bidder = $auctionResult->getCustomer();
$seller = $product->getCustomer();
if ($bidder == null) {
continue;
}
// 落札商品
if ($bidder->getId() == $customer->getId()) {
$bidderId = $bidder->getId();
if (!isset($productsByCustomer[$bidderId])) {
$productsByCustomer[$bidderId] = [];
}
$productsByCustomer[$bidderId]['successful_bid'][] = $product;
}
// 出品商品
if ($seller->getId() == $customer->getId()) {
$sellerId = $seller->getId();
if (!isset($productsByCustomer[$sellerId])) {
$productsByCustomer[$sellerId] = [];
}
$productsByCustomer[$sellerId]['sell'][] = $product;
}
}
return $productsByCustomer;
}
public function getOrderBySellerCustomer(AuctionResult $auctionResult): array
{
$productsByCustomer = [];
$product = $auctionResult->getProduct();
$customer = $product->getCustomer();
$products = $this->getProducts();
foreach ($products as $product) {
$auctionResult = $product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if ($auctionResult->getStatusName() == '返品' || $auctionResult->getStatusName() == '次回出品') {
continue;
}
$bidder = $auctionResult->getCustomer();
$seller = $product->getCustomer();
if ($bidder == null) {
continue;
}
// 落札商品
if ($bidder->getId() == $customer->getId()) {
$bidderId = $bidder->getId();
if (!isset($productsByCustomer[$bidderId])) {
$productsByCustomer[$bidderId] = [];
}
$productsByCustomer[$bidderId]['successful_bid'][] = $product;
}
// 出品商品
if ($seller->getId() == $customer->getId()) {
$sellerId = $seller->getId();
if (!isset($productsByCustomer[$sellerId])) {
$productsByCustomer[$sellerId] = [];
}
$productsByCustomer[$sellerId]['sell'][] = $product;
}
}
return $productsByCustomer;
}
public function sumAgreementProductPrice(): int
{
$sum = 0;
foreach ($this->Products as $Product) {
$auctionResult = $Product->getAuctionResult();
if (!$auctionResult) {
continue;
}
if ($auctionResult->getStatusName() == '成約済み') {
$sum += $auctionResult->getPrice();
}
}
return $sum;
}
}
}