CreditProduct.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Models;
  3. use Hidehalo\Nanoid\Client;
  4. use Illuminate\Database\Eloquent\Model;
  5. use NumberFormatter;
  6. use Spatie\Activitylog\Traits\LogsActivity;
  7. use App\Models\Configuration;
  8. class CreditProduct extends Model
  9. {
  10. use LogsActivity;
  11. /**
  12. * @var bool
  13. */
  14. public $incrementing = false;
  15. /**
  16. * @var string[]
  17. */
  18. protected $fillable = [
  19. "type",
  20. "price",
  21. "description",
  22. "display",
  23. "currency_code",
  24. "quantity",
  25. "disabled",
  26. ];
  27. public static function boot()
  28. {
  29. parent::boot();
  30. static::creating(function (CreditProduct $creditProduct) {
  31. $client = new Client();
  32. $creditProduct->{$creditProduct->getKeyName()} = $client->generateId($size = 21);
  33. });
  34. }
  35. /**
  36. * @param mixed $value
  37. * @param string $locale
  38. *
  39. * @return float
  40. */
  41. public function formatToCurrency($value,$locale = 'en_US')
  42. {
  43. $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
  44. return $formatter->formatCurrency($value, $this->currency_code);
  45. }
  46. /**
  47. * @description Returns the tax in % taken from the Configuration
  48. *
  49. * @return int
  50. */
  51. public function getTaxPercent()
  52. {
  53. $tax = Configuration::getValueByKey("SALES_TAX");
  54. return $tax < 0 ? 0 : $tax;
  55. }
  56. /**
  57. * @description Returns the tax as Number
  58. *
  59. * @return float
  60. */
  61. public function getTaxValue()
  62. {
  63. return $this->price*$this->getTaxPercent()/100;
  64. }
  65. /**
  66. * @description Returns the full price of a Product including tax
  67. *
  68. * @return float
  69. */
  70. public function getTotalPrice()
  71. {
  72. return $this->price+($this->getTaxValue());
  73. }
  74. }