src/NewShippingBundle/Entity/ShippingZonePrice.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\NewShippingBundle\Entity;
  3. use App\CurrencyBundle\Entity\Currency;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Doctrine\ORM\Mapping\UniqueConstraint;
  8. use PN\ServiceBundle\Interfaces\DateTimeInterface;
  9. use PN\ServiceBundle\Model\DateTimeTrait;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. /**
  13. * @ORM\HasLifecycleCallbacks
  14. * @ORM\Table("shipping_zone_price",
  15. * uniqueConstraints={
  16. * @UniqueConstraint(name="shipping_zone_unique", columns={"source_shipping_zone_id", "target_shipping_zone_id", "shipping_time_id"})
  17. * }
  18. * )
  19. * @ORM\Entity(repositoryClass="App\NewShippingBundle\Repository\ShippingZonePriceRepository")
  20. * @UniqueEntity({"sourceShippingZone", "targetShippingZone", "shippingTime"})
  21. */
  22. class ShippingZonePrice implements DateTimeInterface
  23. {
  24. use DateTimeTrait;
  25. const CALCULATOR_FIRST_KG_EXTRA_KG = 'first_kg_extra_kg';
  26. const CALCULATOR_WEIGHT_RATE = 'weight_rate';
  27. public static array $calculators = array(
  28. 'First Kg and Extra kg' => self::CALCULATOR_FIRST_KG_EXTRA_KG,
  29. 'Weight Rate' => self::CALCULATOR_WEIGHT_RATE,
  30. );
  31. /**
  32. * @var integer
  33. *
  34. * @ORM\Column(name="id", type="integer")
  35. * @ORM\Id
  36. * @ORM\GeneratedValue(strategy="AUTO")
  37. */
  38. private $id;
  39. /**
  40. * @Assert\NotBlank()
  41. * @ORM\ManyToOne(targetEntity="ShippingZone", inversedBy="sourceShippingZonePrices")
  42. */
  43. private ?ShippingZone $sourceShippingZone = null;
  44. /**
  45. * @Assert\NotBlank()
  46. * @ORM\ManyToOne(targetEntity="ShippingZone", inversedBy="targetShippingZonePrices")
  47. */
  48. private ?ShippingZone $targetShippingZone = null;
  49. /**
  50. * @ORM\ManyToOne(targetEntity="App\NewShippingBundle\Entity\ShippingTime", inversedBy="shippingZonePrices")
  51. */
  52. private ?ShippingTime $shippingTime = null;
  53. /**
  54. * @Assert\NotBlank()
  55. * @ORM\Column(name="calculator", type="string", length=45)
  56. */
  57. private ?string $calculator = null;
  58. /**
  59. * @ORM\Column(name="has_rates", type="boolean")
  60. */
  61. private bool $hasRates = false;
  62. /**
  63. * @ORM\Column(name="configuration", type="json")
  64. */
  65. private array $configuration = [];
  66. /**
  67. * @Assert\NotBlank()
  68. * @ORM\ManyToOne(targetEntity="App\CurrencyBundle\Entity\Currency")
  69. */
  70. private ?Currency $currency = null;
  71. /**
  72. * @ORM\ManyToOne(targetEntity="Courier")
  73. */
  74. private ?Courier $courier=null;
  75. /**
  76. * @ORM\OneToMany(targetEntity="ShippingZonePriceSpecificWeight", mappedBy="shippingZonePrice",cascade={"persist", "remove"}, orphanRemoval=true)
  77. * @ORM\OrderBy({"weight"="ASC"})
  78. */
  79. private Collection $specificWeights;
  80. public function __clone()
  81. {
  82. if ($this->id) {
  83. $this->id = null;
  84. $specificWeightsClone = new \Doctrine\Common\Collections\ArrayCollection();
  85. foreach ($this->getSpecificWeights() as $specificWeight) {
  86. $itemClone = clone $specificWeight;
  87. $itemClone->setShippingZonePrice($this);
  88. $specificWeightsClone->add($itemClone);
  89. }
  90. $this->specificWeights = $specificWeightsClone;
  91. }
  92. }
  93. public function __construct()
  94. {
  95. $this->specificWeights = new ArrayCollection();
  96. }
  97. /**
  98. * Now we tell doctrine that before we persist or update we call the updatedTimestamps() function.
  99. *
  100. * @ORM\PrePersist
  101. * @ORM\PreUpdate
  102. */
  103. public function updatedTimestamps(): void
  104. {
  105. $this->setModified(new \DateTime(date('Y-m-d H:i:s')));
  106. if ($this->getCreated() == null) {
  107. $this->setCreated(new \DateTime(date('Y-m-d H:i:s')));
  108. }
  109. }
  110. public function getCalculatorName(): string
  111. {
  112. return array_search($this->getCalculator(), self::$calculators);
  113. }
  114. public function getId(): ?int
  115. {
  116. return $this->id;
  117. }
  118. public function getCalculator(): ?string
  119. {
  120. return $this->calculator;
  121. }
  122. public function setCalculator(string $calculator): self
  123. {
  124. $this->calculator = $calculator;
  125. return $this;
  126. }
  127. public function getSourceShippingZone(): ?ShippingZone
  128. {
  129. return $this->sourceShippingZone;
  130. }
  131. public function setSourceShippingZone(?ShippingZone $sourceShippingZone): self
  132. {
  133. $this->sourceShippingZone = $sourceShippingZone;
  134. return $this;
  135. }
  136. public function getTargetShippingZone(): ?ShippingZone
  137. {
  138. return $this->targetShippingZone;
  139. }
  140. public function setTargetShippingZone(?ShippingZone $targetShippingZone): self
  141. {
  142. $this->targetShippingZone = $targetShippingZone;
  143. return $this;
  144. }
  145. public function getCurrency(): ?Currency
  146. {
  147. return $this->currency;
  148. }
  149. public function setCurrency(?Currency $currency): self
  150. {
  151. $this->currency = $currency;
  152. return $this;
  153. }
  154. public function getCourier(): ?Courier
  155. {
  156. return $this->courier;
  157. }
  158. public function setCourier(?Courier $courier): self
  159. {
  160. $this->courier = $courier;
  161. return $this;
  162. }
  163. /**
  164. * @return Collection|ShippingZonePriceSpecificWeight[]
  165. */
  166. public function getSpecificWeights(): Collection
  167. {
  168. return $this->specificWeights->filter(function ($specificWeight) {
  169. if ($specificWeight->getDeleted() == null) {
  170. return $specificWeight;
  171. }
  172. });
  173. }
  174. public function addSpecificWeight(ShippingZonePriceSpecificWeight $specificWeight): self
  175. {
  176. if (!$this->specificWeights->contains($specificWeight)) {
  177. $this->specificWeights[] = $specificWeight;
  178. $specificWeight->setShippingZonePrice($this);
  179. }
  180. return $this;
  181. }
  182. public function removeSpecificWeight(ShippingZonePriceSpecificWeight $specificWeight): self
  183. {
  184. if ($this->specificWeights->removeElement($specificWeight)) {
  185. // set the owning side to null (unless already changed)
  186. if ($specificWeight->getShippingZonePrice() === $this) {
  187. $specificWeight->setDeleted(new \DateTime());
  188. }
  189. }
  190. return $this;
  191. }
  192. public function getConfiguration(): ?array
  193. {
  194. if (!is_array($this->configuration)) {
  195. return [];
  196. }
  197. return $this->configuration;
  198. }
  199. public function setConfiguration(array $configuration): self
  200. {
  201. $this->configuration = $configuration;
  202. return $this;
  203. }
  204. public function getHasRates(): ?bool
  205. {
  206. return $this->hasRates;
  207. }
  208. public function setHasRates(bool $hasRates): self
  209. {
  210. $this->hasRates = $hasRates;
  211. return $this;
  212. }
  213. public function getShippingTime(): ?ShippingTime
  214. {
  215. return $this->shippingTime;
  216. }
  217. public function setShippingTime(?ShippingTime $shippingTime): self
  218. {
  219. $this->shippingTime = $shippingTime;
  220. return $this;
  221. }
  222. public function isHasRates(): ?bool
  223. {
  224. return $this->hasRates;
  225. }
  226. }