src/Entity/BasketItem.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\BasketItemRepository;
  4. use App\Service\ProductService;
  5. use Doctrine\ORM\EntityManager;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use JsonSerializable;
  9. #[ORM\Entity(repositoryClass: BasketItemRepository::class)]
  10. class BasketItem implements JsonSerializable
  11. {
  12. #[ORM\Id]
  13. #[ORM\GeneratedValue]
  14. #[ORM\Column(type: 'integer')]
  15. private $id;
  16. #[ORM\ManyToOne(targetEntity: 'Product', inversedBy: 'basketItems')]
  17. #[ORM\JoinColumn(name: 'id_product', referencedColumnName: 'id')]
  18. private $product;
  19. #[ORM\Column(type: 'integer')]
  20. private $quantity;
  21. public function getId(): ?int
  22. {
  23. return $this->id;
  24. }
  25. public function getQuantity(): ?int
  26. {
  27. return $this->quantity;
  28. }
  29. public function setQuantity(int $quantity): self
  30. {
  31. $this->quantity = $quantity;
  32. return $this;
  33. }
  34. public function getCost()
  35. {
  36. return $this->getProduct()->getCost() * $this->getQuantity();
  37. }
  38. public function getTva(){
  39. $cost = $this->getCost();
  40. return $this->getTvaWithCost($cost);
  41. }
  42. public function getTvaWithCost($cost){
  43. return ProductService::calculateTvaFromTTC($cost, $this->getProduct()->getTva());
  44. }
  45. public function getProduct(): ?Product
  46. {
  47. return $this->product;
  48. }
  49. public function setProduct(Product $product): self
  50. {
  51. $this->product = $product;
  52. return $this;
  53. }
  54. public function __construct($product, int $quantity){
  55. $this->product = $product;
  56. $this->quantity = $quantity;
  57. }
  58. public function jsonSerialize()
  59. {
  60. $vars = get_object_vars($this);
  61. return $vars;
  62. }
  63. public function refresh(EntityManagerInterface $entityManager){
  64. $productRepository = $entityManager->getRepository(Product::class);
  65. $product = $productRepository->find($this->getProduct()->getId());
  66. $this->setProduct($product);
  67. }
  68. public function getQtyPreOrder(){
  69. $qtePreCmd = 0;
  70. if($this->getProduct()->isPrecommande()){
  71. $qtePreCmd = $this->getQuantity() - $this->getProduct()->getProduitQteStock()->getQteStock();
  72. if( $qtePreCmd <= 0) $qtePreCmd = 0;
  73. }
  74. return $qtePreCmd;
  75. }
  76. }