<?phpnamespace App\Entity;use DateInterval;use DateTime;use Exception;use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Validator\Constraints as Assert;use Symfony\Component\Validator\Context\ExecutionContextInterface;/** * @ORM\Entity * @ORM\Table( * name="terms_of_payment_row", * options={"collate"="utf8_swedish_ci"} * ) * @ORM\HasLifecycleCallbacks */class TermsOfPaymentRow implements EntityInterface{ /** * @ORM\Id * @ORM\Column( * type="integer" * ) * @ORM\GeneratedValue( * strategy="AUTO" * ) * * @var int|null */ protected ?int $id = null; /** * @ORM\Column( * type="integer", * nullable=true * ) * @Assert\Range( * min=0 * ) * * @var int|null */ protected ?int $days = null; /** * @ORM\Column( * type="string", * length=1000 * ) * @Assert\NotBlank * * @var string */ protected string $description; /** * @ORM\Column( * type="datetime", * nullable=true * ) * * @var DateTime|null */ protected ?DateTime $dueDate = null; /** * @ORM\Column( * type="integer", * nullable=true * ) * @Assert\Range( * min=0, * max=100 * ) * * @var int|null */ protected ?int $percentage = null; /** * @param string $description */ public function __construct(string $description) { $this->description = $description; } public function __clone() { $this->id = null; } /** * @return int|null */ public function getDays(): ?int { return $this->days; } /** * @return string */ public function getDescription(): string { return $this->description; } /** * @return DateTime|null */ public function getDueDate(): ?DateTime { return $this->dueDate; } /** * @return int|null */ public function getId(): ?int { return $this->id; } /** * @param string $deliveryDateStr * * @return DateTime * * @throws Exception */ public function getPaymentDate(string $deliveryDateStr): DateTime { if ($this->dueDate !== null) { return $this->dueDate; } else { return (new DateTime($deliveryDateStr))->add( new DateInterval('P' . $this->days . 'D') ); } } /** * @param int $totalValue * * @return int */ public function getPaymentValue(int $totalValue): int { return round($totalValue * $this->percentage / 100); } /** * @return int|null */ public function getPercentage(): ?int { return $this->percentage; } /** * @param int|null $days */ public function setDays(?int $days) { $this->days = $days; } /** * @param string $description */ public function setDescription(string $description) { $this->description = $description; } /** * @param DateTime|null $dueDate */ public function setDueDate(?DateTime $dueDate) { $this->dueDate = $dueDate; } /** * @param int|null $percentage */ public function setPercentage(?int $percentage) { $this->percentage = $percentage; } /** * @Assert\Callback * * @param ExecutionContextInterface $context */ public function validate(ExecutionContextInterface $context) { if ($this->dueDate !== null && $this->days !== null) { $context->buildViolation('error.either_due_date_or_days') ->setTranslationDomain('TermsOfPaymentRow') ->atPath('days') ->addViolation() ; } }}