ShopProduct.php 2.3 KB

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