You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Lc\ShopBundle\Model;
  3. use Lc\ShopBundle\Context\ReductionInterface;
  4. use Lc\ShopBundle\Context\UnitInterface;
  5. trait PriceTrait
  6. {
  7. /**
  8. * @ORM\Column(type="float", nullable=true)
  9. */
  10. protected $price;
  11. /**
  12. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UnitInterface")
  13. * @ORM\JoinColumn(nullable=true)
  14. */
  15. protected $unit;
  16. /**
  17. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\TaxRateInterface")
  18. * @ORM\JoinColumn(nullable=true)
  19. */
  20. protected $taxRate;
  21. public function getPrice(): ?float
  22. {
  23. return $this->price;
  24. }
  25. public function getPriceInherited(): ?float
  26. {
  27. return $this->getPrice();
  28. }
  29. public function getUnitInherited(): ?Unit
  30. {
  31. return $this->getUnit();
  32. }
  33. public function getTaxRateInherited(): ?TaxRate
  34. {
  35. return $this->getTaxRate();
  36. }
  37. public function getPriceWithTax(): ?float
  38. {
  39. return $this->calculatePriceWithTax($this->getPriceInherited(), $this->getTaxRateInherited()->getValue());
  40. }
  41. public function getPriceByUnitRef(): ?float
  42. {
  43. return ($this->getPriceInherited() * $this->getUnitInherited()->getCoefficient()) / $this->getQuantityInherited();
  44. }
  45. public function getPriceByUnitRefWithTax(): ?float
  46. {
  47. return $this->calculatePriceWithTax($this->getPriceByUnitRef(), $this->getTaxRateInherited()->getValue());
  48. }
  49. public function getPriceWithTaxAndReduction(): ?float
  50. {
  51. if ($this->reductionCatalog) {
  52. return $this->getPriceWithTaxAndReductionCatalog($this->reductionCatalog);
  53. } else {
  54. return null;
  55. }
  56. }
  57. public function getPriceWithTaxAndReductionCatalog(ReductionInterface $reductionCatalog): ?float
  58. {
  59. if ($reductionCatalog->getUnit() == 'percent') {
  60. //Théoriquement que la réduction s'applique sur le prixHT ou le prixTTC le résultat est le même,j'ai laisser mon code au cas où ;)
  61. return $this->calculatePriceWithReductionPercent($this->getPriceWithTax(), $reductionCatalog->getValue());
  62. /*if ($reductionCatalog->getBehaviorTaxRate() == 'tax-excluded') {
  63. $priceReductionHT = $this->calculatePriceWithReductionPercent($this->getPriceInherited(), $reductionCatalog->getValue());
  64. return $this->calculatePriceWithTax($priceReductionHT, $this->getTaxRateInherited()->getValue());
  65. } else if ($reductionCatalog->getBehaviorTaxRate() == 'tax-included') {
  66. return $this->calculatePriceWithReductionPercent($this->getPriceWithTax(), $reductionCatalog->getValue());
  67. }*/
  68. }elseif ($reductionCatalog->getUnit() == 'amount') {
  69. if ($reductionCatalog->getBehaviorTaxRate() == 'tax-excluded') {
  70. $priceReductionHT = $this->calculatePriceWithReductionAmount($this->getPriceInherited(), $reductionCatalog->getValue());
  71. return $this->calculatePriceWithTax($priceReductionHT, $this->getTaxRateInherited()->getValue());
  72. }else if ($reductionCatalog->getBehaviorTaxRate() == 'tax-included') {
  73. return $this->calculatePriceWithReductionAmount($this->getPriceWithTax(), $reductionCatalog->getValue());
  74. }
  75. }
  76. }
  77. public function setPrice(float $price): self
  78. {
  79. $this->price = $price;
  80. return $this;
  81. }
  82. public function getUnit(): ?Unit
  83. {
  84. return $this->unit;
  85. }
  86. public function setUnit(Unit $unit): self
  87. {
  88. $this->unit = $unit;
  89. return $this;
  90. }
  91. public function getTaxRate(): ?TaxRate
  92. {
  93. return $this->taxRate;
  94. }
  95. public function setTaxRate(?TaxRate $taxRate): self
  96. {
  97. $this->taxRate = $taxRate;
  98. return $this;
  99. }
  100. private function calculatePriceWithTax($priceWithoutTax, $taxRateValue): ?float
  101. {
  102. $price = floatval($priceWithoutTax) * ($taxRateValue / 100 + 1);
  103. $price = round((($price * 100)) / 100, 2);
  104. return $price;
  105. }
  106. public function calculatePriceWithReductionPercent($price, $percentValue): ?float
  107. {
  108. $price = floatval($price) * (1 - $percentValue / 100);
  109. $price = round((($price * 100)) / 100, 2);
  110. return $price;
  111. }
  112. public function calculatePriceWithReductionAmount($price, $amountValue): ?float
  113. {
  114. $price = floatval($price) - $amountValue;
  115. $price = round((($price * 100)) / 100, 2);
  116. return $price;
  117. }
  118. }