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.

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