ShopProduct.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $discountRate = PartnerDiscount::getDiscount() / 100;
  71. $discountedPrice = $this->price * (1 - $discountRate);
  72. return round($discountedPrice, 2);
  73. }
  74. /**
  75. * @description Returns the tax as Number
  76. *
  77. * @return float
  78. */
  79. public function getTaxValue()
  80. {
  81. $taxValue = $this->getPriceAfterDiscount() * $this->getTaxPercent() / 100;
  82. return round($taxValue, 2);
  83. }
  84. /**
  85. * @description Returns the full price of a Product including tax
  86. *
  87. * @return float
  88. */
  89. public function getTotalPrice()
  90. {
  91. $total = $this->getPriceAfterDiscount() + $this->getTaxValue();
  92. return round($total, 2);
  93. }
  94. }