src/ProductBundle/Entity/ProductPrice.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\ProductBundle\Entity;
  3. use App\CurrencyBundle\Entity\Currency;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. use PN\ServiceBundle\Interfaces\DateTimeInterface;
  7. use PN\ServiceBundle\Model\DateTimeTrait;
  8. use PN\ServiceBundle\Model\VirtualDeleteTrait;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. /**
  11. * @Gedmo\Loggable()
  12. * @ORM\HasLifecycleCallbacks
  13. * @ORM\Table(name="product_price")
  14. * @ORM\Entity(repositoryClass="App\ProductBundle\Repository\ProductPriceRepository")
  15. */
  16. class ProductPrice implements DateTimeInterface
  17. {
  18. use VirtualDeleteTrait,
  19. DateTimeTrait;
  20. /**
  21. * @ORM\Id
  22. * @ORM\Column(name="id", type="integer")
  23. * @ORM\GeneratedValue(strategy="AUTO")
  24. */
  25. private ?int $id = null;
  26. /**
  27. * @ORM\ManyToOne(targetEntity="App\ProductBundle\Entity\Product", inversedBy="prices", cascade={"remove"})
  28. */
  29. private ?Product $product = null;
  30. /**
  31. * @Gedmo\Versioned
  32. * @ORM\ManyToOne(targetEntity="App\CurrencyBundle\Entity\Currency")
  33. */
  34. private ?Currency $currency = null;
  35. /**
  36. * @Gedmo\Versioned
  37. * @ORM\Column(name="title", type="string", length=150, nullable=true)
  38. */
  39. private ?string $title = null;
  40. /**
  41. * @Gedmo\Versioned
  42. * @ORM\Column(name="stock", type="integer", options={"default" = 0}, nullable=true)
  43. */
  44. private ?int $stock = 0;
  45. /**
  46. * @Gedmo\Versioned
  47. * @Assert\NotBlank()
  48. * @ORM\Column(name="unit_price", type="float")
  49. */
  50. private ?float $unitPrice = null;
  51. /**
  52. * @Gedmo\Versioned
  53. * @Assert\NotBlank()
  54. * @ORM\Column(name="promotional_price", type="float", nullable=true)
  55. */
  56. private ?float $promotionalPrice = null;
  57. /**
  58. * @Gedmo\Versioned
  59. * @ORM\Column(name="weight", type="float", nullable=true)
  60. */
  61. private ?float $weight = null;
  62. /**
  63. * @Gedmo\Versioned
  64. * @ORM\Column(name="promotional_expiry_date", type="date", nullable=true)
  65. */
  66. private ?\DateTime $promotionalExpiryDate = null;
  67. /**
  68. * @Gedmo\Versioned
  69. * @ORM\Column(name="variant_option_ids", type="text", nullable=true)
  70. */
  71. private ?string $variantOptionIds = null;
  72. public function __clone()
  73. {
  74. if ($this->id) {
  75. $this->id = null;
  76. }
  77. }
  78. public function getObj(): array
  79. {
  80. return [
  81. "id" => $this->getId(),
  82. "title" => $this->getTitle(),
  83. "sellPrice" => $this->getSellPrice(),
  84. "promotionalPercentage" => $this->getPromotionalPercentage(),
  85. "stock" => $this->getStock(),
  86. "originalPrice" => $this->getUnitPrice(),
  87. ];
  88. }
  89. public function getPromotionalPercentage(): int
  90. {
  91. $promotionalExpiryDate = $this->getPromotionalExpiryDate();
  92. $currentDate = new \DateTime();
  93. if (
  94. !$promotionalExpiryDate instanceof \DateTimeInterface
  95. or $promotionalExpiryDate->format("Y-m-d") < $currentDate->format("Y-m-d")
  96. ) {
  97. return 0;
  98. }
  99. if ($this->getPromotionalPrice() > 0) {
  100. $discountPercentage = ($this->getPromotionalPrice() / $this->getUnitPrice()) * 100;
  101. return 100 - round($discountPercentage);
  102. }
  103. return 0;
  104. }
  105. public function getUnitPriceWithCommission(): float
  106. {
  107. $unitPrice = $this->getUnitPrice();
  108. $commissionPercentage = $this->getProduct()->getVendor()?->getCommissionPercentage() ?? 0;
  109. if ($commissionPercentage > 0) {
  110. $commission = ($unitPrice * $commissionPercentage) / 100;
  111. $unitPrice = $unitPrice + $commission;
  112. }
  113. return $unitPrice;
  114. }
  115. public function getSellPriceBeforeCommission(): ?float
  116. {
  117. $sellPrice = $this->getUnitPrice();
  118. $promotionalExpiryDate = $this->getPromotionalExpiryDate();
  119. if ($promotionalExpiryDate) {
  120. $currentDate = new \DateTime();
  121. if ($promotionalExpiryDate->format("Y-m-d") > $currentDate->format("Y-m-d")) {
  122. $sellPrice = $this->getPromotionalPrice();
  123. }
  124. }
  125. return $sellPrice;
  126. }
  127. public function getSellPrice(bool $withCommission = true): ?float
  128. {
  129. $sellPrice = $this->getUnitPrice();
  130. $promotionalExpiryDate = $this->getPromotionalExpiryDate();
  131. if ($promotionalExpiryDate) {
  132. $currentDate = new \DateTime();
  133. if ($promotionalExpiryDate->format("Y-m-d") > $currentDate->format("Y-m-d")) {
  134. $sellPrice = $this->getPromotionalPrice();
  135. }
  136. }
  137. if ($withCommission) {
  138. $commissionPercentage = $this->getProduct()->getVendor()?->getCommissionPercentage() ?? 0;
  139. if ($commissionPercentage > 0) {
  140. $commission = ($sellPrice * $commissionPercentage) / 100;
  141. $sellPrice = $sellPrice + $commission;
  142. }
  143. }
  144. return $sellPrice;
  145. }
  146. public function hasPromotion(): bool
  147. {
  148. if ($this->getPromotionalExpiryDate() != null) {
  149. $promotionalExpiryDate = $this->getPromotionalExpiryDate();
  150. $currentDate = new \DateTime();
  151. if ($promotionalExpiryDate->format("Y-m-d") > $currentDate->format("Y-m-d") and $this->getPromotionalPrice() > 0) {
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. public function getId(): ?int
  158. {
  159. return $this->id;
  160. }
  161. public function getTitle(): ?string
  162. {
  163. return $this->title;
  164. }
  165. public function setTitle(?string $title): self
  166. {
  167. $this->title = $title;
  168. return $this;
  169. }
  170. public function getStock(): ?int
  171. {
  172. return $this->stock;
  173. }
  174. public function setStock(?int $stock): self
  175. {
  176. $this->stock = $stock;
  177. return $this;
  178. }
  179. public function getUnitPrice(): ?float
  180. {
  181. return $this->unitPrice;
  182. }
  183. public function setUnitPrice(float $unitPrice): self
  184. {
  185. $this->unitPrice = $unitPrice;
  186. return $this;
  187. }
  188. public function getPromotionalPrice(): ?float
  189. {
  190. return $this->promotionalPrice;
  191. }
  192. public function setPromotionalPrice(?float $promotionalPrice): self
  193. {
  194. $this->promotionalPrice = $promotionalPrice;
  195. return $this;
  196. }
  197. public function getPromotionalExpiryDate(): ?\DateTimeInterface
  198. {
  199. return $this->promotionalExpiryDate;
  200. }
  201. public function setPromotionalExpiryDate(?\DateTimeInterface $promotionalExpiryDate): self
  202. {
  203. $this->promotionalExpiryDate = $promotionalExpiryDate;
  204. return $this;
  205. }
  206. public function getProduct(): ?Product
  207. {
  208. return $this->product;
  209. }
  210. public function setProduct(?Product $product): self
  211. {
  212. $this->product = $product;
  213. return $this;
  214. }
  215. public function getCurrency(): ?Currency
  216. {
  217. return $this->currency;
  218. }
  219. public function setCurrency(?Currency $currency): self
  220. {
  221. $this->currency = $currency;
  222. return $this;
  223. }
  224. public function getVariantOptionIds(): ?string
  225. {
  226. return $this->variantOptionIds;
  227. }
  228. public function setVariantOptionIds(?string $variantOptionIds): self
  229. {
  230. $this->variantOptionIds = $variantOptionIds;
  231. return $this;
  232. }
  233. public function getWeight(): ?float
  234. {
  235. return $this->weight;
  236. }
  237. public function setWeight(?float $weight): static
  238. {
  239. $this->weight = $weight;
  240. return $this;
  241. }
  242. }