You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Models\Document\DocumentItem;
  4. use App\Models\Item;
  5. use App\Scopes\CurrentCompanyScope;
  6. use App\Traits\Blamable;
  7. use App\Traits\CompanyOwned;
  8. use Database\Factories\DiscountFactory;
  9. use Illuminate\Database\Eloquent\Factories\Factory;
  10. use Illuminate\Database\Eloquent\Factories\HasFactory;
  11. use Illuminate\Database\Eloquent\Model;
  12. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  13. use Illuminate\Database\Eloquent\Relations\HasMany;
  14. use Illuminate\Database\Eloquent\Relations\HasOne;
  15. use Wallo\FilamentCompanies\FilamentCompanies;
  16. class Discount extends Model
  17. {
  18. use Blamable, CompanyOwned, HasFactory;
  19. protected $table = 'discounts';
  20. protected $fillable = [
  21. 'company_id',
  22. 'name',
  23. 'description',
  24. 'rate',
  25. 'computation',
  26. 'type',
  27. 'scope',
  28. 'start_date',
  29. 'end_date',
  30. 'enabled',
  31. 'created_by',
  32. 'updated_by',
  33. ];
  34. protected $casts = [
  35. 'enabled' => 'boolean',
  36. 'start_date' => 'datetime',
  37. 'end_date' => 'datetime',
  38. ];
  39. protected static function booted(): void
  40. {
  41. static::addGlobalScope(new CurrentCompanyScope);
  42. }
  43. public function company(): BelongsTo
  44. {
  45. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  46. }
  47. public function defaultSalesDiscount(): HasOne
  48. {
  49. return $this->hasOne(DefaultSetting::class, 'sales_discount_id');
  50. }
  51. public function defaultPurchaseDiscount(): HasOne
  52. {
  53. return $this->hasOne(DefaultSetting::class, 'purchase_discount_id');
  54. }
  55. public function createdBy(): BelongsTo
  56. {
  57. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  58. }
  59. public function updatedBy(): BelongsTo
  60. {
  61. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  62. }
  63. public function items(): HasMany
  64. {
  65. return $this->hasMany(Item::class);
  66. }
  67. public function document_items(): HasMany
  68. {
  69. return $this->hasMany(DocumentItem::class);
  70. }
  71. public function bill_items(): HasMany
  72. {
  73. return $this->document_items()->where('type', 'bill');
  74. }
  75. public function invoice_items(): HasMany
  76. {
  77. return $this->document_items()->where('type', 'invoice');
  78. }
  79. public static function getComputationTypes(): array
  80. {
  81. return [
  82. 'percentage' => 'Percentage',
  83. 'fixed' => 'Fixed',
  84. ];
  85. }
  86. public static function getDiscountTypes(): array
  87. {
  88. return [
  89. 'sales' => 'Sales',
  90. 'purchase' => 'Purchase',
  91. 'none' => 'None',
  92. ];
  93. }
  94. public static function getDiscountScopes(): array
  95. {
  96. return [
  97. 'product' => 'Product',
  98. 'service' => 'Service',
  99. ];
  100. }
  101. protected static function newFactory(): Factory
  102. {
  103. return DiscountFactory::new();
  104. }
  105. }