<?php
namespace App\AtechNet\CoreBundle\Entity;
use App\AtechNet\CoreBundle\Enum\OrderErrorComment\OrderErrorCategories;
use App\AtechNet\CoreBundle\Enum\OrderErrorComment\OrderErrorTypes;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\Table;
use Symfony\Component\Validator\Constraints as Assert;
use function PHPUnit\Framework\isNan;
/**
* OrderComment
*
* @Table(name="order_comment")
* @Entity
* @HasLifecycleCallbacks
*/
class OrderComment extends AbstractSignedEntity
{
public const int TYPE_FOR_PREPARATION = 1;
public const int TYPE_FOR_PACKING = 2;
public const int TYPE_FOR_INSTALLATION = 3;
public const int TYPE_FOR_LABELS = 4;
public const int TYPE_ERROR = 5;
/**
* @return array
*/
public static function getTypeChoices(): array
{
return [
self::TYPE_FOR_PREPARATION => 'for-preparation',
self::TYPE_FOR_PACKING => 'for-packing',
self::TYPE_FOR_INSTALLATION => 'for-installation',
self::TYPE_FOR_LABELS => 'for-labels',
self::TYPE_ERROR => 'error',
];
}
/**
* @var Order
*
* @Assert\NotNull
*
* @ManyToOne(targetEntity="Order", inversedBy="comments")
* @JoinColumn(name="order_id", referencedColumnName="id")
*/
private $order;
/**
* @var int
*
* @Column(name="type", type="integer", nullable=true)
*/
private $type;
/**
* @var string
*
* @Column(name="text", type="text", nullable=true)
*/
private $text;
/**
* @var User
*
* @ManyToOne(targetEntity="User")
* @JoinColumn(name="done_by", referencedColumnName="id", nullable=true)
*/
private $doneBy;
/**
* @var \DateTime
*
* @Column(name="done_time", type="datetime", nullable=true)
*/
private $doneTime;
/**
* @param User $user
*
* @return bool
*/
public function isDeletableByUser(User $user)
{
return !is_null($this->getCreatedBy()) && $this->getCreatedBy()->getId() == $user->getId();
}
public function isDone()
{
return $this->doneTime != null;
}
/**
* @return Order
*/
public function getOrder()
{
return $this->order;
}
/**
* @param Order $order
*/
public function setOrder($order)
{
$this->order = $order;
}
/**
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* @param string $text
*/
public function setText($text)
{
$this->text = $text;
}
/**
* @return User
*/
public function getDoneBy()
{
return $this->doneBy;
}
/**
* @param User $doneBy
*/
public function setDoneBy($doneBy)
{
$this->doneBy = $doneBy;
}
/**
* @return \DateTime
*/
public function getDoneTime()
{
return $this->doneTime;
}
/**
* @param \DateTime $doneTime
*/
public function setDoneTime($doneTime)
{
$this->doneTime = $doneTime;
}
/**
* @return int|null
*/
public function getType(): ?int
{
return $this->type;
}
/**
* @param int|null $type
*/
public function setType(?int $type)
{
if (!isset(self::getTypeChoices()[$type])) {
throw new \InvalidArgumentException("Invalid comment type value " . $type);
}
$this->type = $type;
}
/**
* @param string $rawText
*
* @return string
*/
public function convertAndSetErrorTypeCommentToIntegers(string $rawText): string
{
preg_match('/\{([^}]+)\}/', $rawText, $categoryMatch);
$category = $categoryMatch[1] ?? null;
preg_match_all('/\[([^\]]+)\]/', $rawText, $itemMatches);
$items = $itemMatches[1] ?? [];
preg_match_all('/\|([^|]+)\|/', $rawText, $otherComments);
$otherComment = $otherComments[1][0] ?? null;
$dbString = '';
if (!is_null($category)) {
$dbString = "{" . OrderErrorCategories::fromDisplayName($category) . "}";
}
foreach ($items as $item) {
if (!is_null($otherComment) && $item == 'Other') {
$dbString .= "[" . OrderErrorTypes::fromDisplayName($item) . "] | " . trim($otherComment) . " |";
}
else {
$dbString .= "[" . OrderErrorTypes::fromDisplayName($item) . "]";
}
}
return $dbString == '' ? $rawText : $dbString;
}
/**
* @return string
*/
public function convertErrorTypeOrderToStrings(): string
{
$rawText = $this->getText();
preg_match('/\{([^}]+)\}/', $rawText, $categoryMatch);
$category = $categoryMatch[1] ?? null;
preg_match_all('/\[([^\]]+)\]/', $rawText, $itemMatches);
$items = $itemMatches[1] ?? [];
preg_match_all('/\|([^|]+)\|/', $rawText, $otherComments);
$otherComment = $otherComments[1][0] ?? null;
$uiString = '';
if (!is_null($category)) {
$uiString = "Category: {" . OrderErrorCategories::fromValueToDisplayName($category) . "}<br>";
}
foreach ($items as $item) {
if (!is_null($otherComment) && in_array($item, OrderErrorTypes::getAllOtherCategories())) {
$uiString .= "[" . OrderErrorTypes::fromValueToDisplayName($item) . "] | " . $otherComment . " |";
}
else {
$uiString .= "[" . OrderErrorTypes::fromValueToDisplayName($item) . "]";
}
}
return $uiString == '' ? $rawText : $uiString;
}
}