ShopProduct.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. public static function boot()
  35. {
  36. parent::boot();
  37. static::creating(function (ShopProduct $shopProduct) {
  38. $client = new Client();
  39. $shopProduct->{$shopProduct->getKeyName()} = $client->generateId($size = 21);
  40. });
  41. }
  42. /**
  43. * @param mixed $value
  44. * @param string $locale
  45. * @return float
  46. */
  47. public function formatToCurrency($value, $locale = 'en_US')
  48. {
  49. $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
  50. return $formatter->formatCurrency($value, $this->currency_code);
  51. }
  52. /**
  53. * @description Returns the tax in % taken from the Configuration
  54. *
  55. * @return int
  56. */
  57. public function getTaxPercent()
  58. {
  59. $tax = config('SETTINGS::PAYMENTS:SALES_TAX');
  60. return $tax < 0 ? 0 : $tax;
  61. }
  62. public function getPriceAfterDiscount()
  63. {
  64. return number_format($this->price - ($this->price * PartnerDiscount::getDiscount() / 100), 2);
  65. }
  66. /**
  67. * @description Returns the tax as Number
  68. *
  69. * @return float
  70. */
  71. public function getTaxValue()
  72. {
  73. return number_format($this->getPriceAfterDiscount() * $this->getTaxPercent() / 100, 2);
  74. }
  75. /**
  76. * @description Returns the full price of a Product including tax
  77. *
  78. * @return float
  79. */
  80. public function getTotalPrice()
  81. {
  82. return number_format($this->getPriceAfterDiscount() + $this->getTaxValue(), 2);
  83. }
  84. }