46 lines
1.1KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. class Price
  4. {
  5. protected $price ;
  6. protected $taxRate ;
  7. public function __construct($price, $taxRate)
  8. {
  9. $this->price = $price ;
  10. $this->taxRate = $taxRate ;
  11. }
  12. public function withTax()
  13. {
  14. return self::getPriceWithTax(
  15. $this->price,
  16. $this->taxRate
  17. ) ;
  18. }
  19. public function withoutTax()
  20. {
  21. return $this->price ;
  22. }
  23. /* Static */
  24. public static function priceTwoDecimals($number)
  25. {
  26. return number_format(( ($number * 100)) / 100, 2) ;
  27. }
  28. public static function getPriceWithoutTax($priceWithTax, $taxRate)
  29. {
  30. return floatval($priceWithTax) / ($taxRate + 1);
  31. }
  32. public static function getPriceWithTax($priceWithoutTax, $taxRate)
  33. {
  34. return self::priceTwoDecimals(floatval($priceWithoutTax) * ($taxRate + 1)) ;
  35. }
  36. }