src/Entity/TermsOfPaymentRow.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateInterval;
  4. use DateTime;
  5. use Exception;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  9. /**
  10.  * @ORM\Entity
  11.  * @ORM\Table(
  12.  *     name="terms_of_payment_row",
  13.  *     options={"collate"="utf8_swedish_ci"}
  14.  * )
  15.  * @ORM\HasLifecycleCallbacks
  16.  */
  17. class TermsOfPaymentRow implements EntityInterface
  18. {
  19.     /**
  20.      * @ORM\Id
  21.      * @ORM\Column(
  22.      *     type="integer"
  23.      * )
  24.      * @ORM\GeneratedValue(
  25.      *     strategy="AUTO"
  26.      * )
  27.      *
  28.      * @var int|null
  29.      */
  30.     protected ?int $id null;
  31.     /**
  32.      * @ORM\Column(
  33.      *     type="integer",
  34.      *     nullable=true
  35.      * )
  36.      * @Assert\Range(
  37.      *     min=0
  38.      * )
  39.      *
  40.      * @var int|null
  41.      */
  42.     protected ?int $days null;
  43.     /**
  44.      * @ORM\Column(
  45.      *     type="string",
  46.      *     length=1000
  47.      * )
  48.      * @Assert\NotBlank
  49.      *
  50.      * @var string
  51.      */
  52.     protected string $description;
  53.     /**
  54.      * @ORM\Column(
  55.      *     type="datetime",
  56.      *     nullable=true
  57.      * )
  58.      *
  59.      * @var DateTime|null
  60.      */
  61.     protected ?DateTime $dueDate null;
  62.     /**
  63.      * @ORM\Column(
  64.      *     type="integer",
  65.      *     nullable=true
  66.      * )
  67.      * @Assert\Range(
  68.      *     min=0,
  69.      *     max=100
  70.      * )
  71.      *
  72.      * @var int|null
  73.      */
  74.     protected ?int $percentage null;
  75.     /**
  76.      * @param string $description
  77.      */
  78.     public function __construct(string $description)
  79.     {
  80.         $this->description $description;
  81.     }
  82.     public function __clone()
  83.     {
  84.         $this->id null;
  85.     }
  86.     /**
  87.      * @return int|null
  88.      */
  89.     public function getDays(): ?int
  90.     {
  91.         return $this->days;
  92.     }
  93.     /**
  94.      * @return string
  95.      */
  96.     public function getDescription(): string
  97.     {
  98.         return $this->description;
  99.     }
  100.     /**
  101.      * @return DateTime|null
  102.      */
  103.     public function getDueDate(): ?DateTime
  104.     {
  105.         return $this->dueDate;
  106.     }
  107.     /**
  108.      * @return int|null
  109.      */
  110.     public function getId(): ?int
  111.     {
  112.         return $this->id;
  113.     }
  114.     /**
  115.      * @param string $deliveryDateStr
  116.      *
  117.      * @return DateTime
  118.      *
  119.      * @throws Exception
  120.      */
  121.     public function getPaymentDate(string $deliveryDateStr): DateTime
  122.     {
  123.         if ($this->dueDate !== null) {
  124.             return $this->dueDate;
  125.         } else {
  126.             return (new DateTime($deliveryDateStr))->add(
  127.                 new DateInterval('P' $this->days 'D')
  128.             );
  129.         }
  130.     }
  131.     /**
  132.      * @param int $totalValue
  133.      *
  134.      * @return int
  135.      */
  136.     public function getPaymentValue(int $totalValue): int
  137.     {
  138.         return round($totalValue $this->percentage 100);
  139.     }
  140.     /**
  141.      * @return int|null
  142.      */
  143.     public function getPercentage(): ?int
  144.     {
  145.         return $this->percentage;
  146.     }
  147.     /**
  148.      * @param int|null $days
  149.      */
  150.     public function setDays(?int $days)
  151.     {
  152.         $this->days $days;
  153.     }
  154.     /**
  155.      * @param string $description
  156.      */
  157.     public function setDescription(string $description)
  158.     {
  159.         $this->description $description;
  160.     }
  161.     /**
  162.      * @param DateTime|null $dueDate
  163.      */
  164.     public function setDueDate(?DateTime $dueDate)
  165.     {
  166.         $this->dueDate $dueDate;
  167.     }
  168.     /**
  169.      * @param int|null $percentage
  170.      */
  171.     public function setPercentage(?int $percentage)
  172.     {
  173.         $this->percentage $percentage;
  174.     }
  175.     /**
  176.      * @Assert\Callback
  177.      *
  178.      * @param ExecutionContextInterface $context
  179.      */
  180.     public function validate(ExecutionContextInterface $context)
  181.     {
  182.         if ($this->dueDate !== null && $this->days !== null) {
  183.             $context->buildViolation('error.either_due_date_or_days')
  184.                 ->setTranslationDomain('TermsOfPaymentRow')
  185.                 ->atPath('days')
  186.                 ->addViolation()
  187.             ;
  188.         }
  189.     }
  190. }