Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Traits;
  4.  
  5. use Akaunting\Money\Money;
  6. use Akaunting\Money\Currency;
  7.  
  8. trait Currencies
  9. {
  10.  
  11. public function convert($amount, $code, $rate, $format = false)
  12. {
  13. $default = new Currency(setting('general.default_currency', 'USD'));
  14.  
  15. if ($format) {
  16. $money = Money::$code($amount, true)->convert($default, (double) $rate)->format();
  17. } else {
  18. $money = Money::$code($amount)->convert($default, (double) $rate)->getAmount();
  19. }
  20.  
  21. return $money;
  22. }
  23.  
  24. public function divide($amount, $code, $rate, $format = false)
  25. {
  26. if ($format) {
  27. $money = Money::$code($amount, true)->divide((double) $rate)->format();
  28. } else {
  29. $money = Money::$code($amount)->divide((double) $rate)->getAmount();
  30. }
  31.  
  32. return $money;
  33. }
  34.  
  35. public function reverseConvert($amount, $code, $rate, $format = false)
  36. {
  37. $default = setting('general.default_currency', 'USD');
  38.  
  39. $code = new Currency($code);
  40.  
  41. if ($format) {
  42. $money = Money::$default($amount, true)->convert($code, (double) $rate)->format();
  43. } else {
  44. $money = Money::$default($amount)->convert($code, (double) $rate)->getAmount();
  45. }
  46.  
  47. return $money;
  48. }
  49.  
  50. public function dynamicConvert($default, $amount, $code, $rate, $format = false)
  51. {
  52. $code = new Currency($code);
  53.  
  54. if ($format) {
  55. $money = Money::$default($amount, true)->convert($code, (double) $rate)->format();
  56. } else {
  57. $money = Money::$default($amount)->convert($code, (double) $rate)->getAmount();
  58. }
  59.  
  60. return $money;
  61. }
  62.  
  63. public function getConvertedAmount($format = false, $with_tax = true)
  64. {
  65. $amount = $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
  66.  
  67. return $this->convert($amount, $this->currency_code, $this->currency_rate, $format);
  68. }
  69.  
  70. public function getReverseConvertedAmount($format = false, $with_tax = true)
  71. {
  72. $amount = $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
  73.  
  74. return $this->reverseConvert($amount, $this->currency_code, $this->currency_rate, $format);
  75. }
  76.  
  77. public function getDynamicConvertedAmount($format = false, $with_tax = true)
  78. {
  79. $amount = $with_tax ? $this->amount : (isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount);
  80.  
  81. return $this->dynamicConvert($this->default_currency_code, $amount, $this->currency_code, $this->currency_rate, $format);
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement