您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Discount.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public function company(): BelongsTo
  40. {
  41. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  42. }
  43. public function defaultSalesDiscount(): HasOne
  44. {
  45. return $this->hasOne(DefaultSetting::class, 'sales_discount_id');
  46. }
  47. public function defaultPurchaseDiscount(): HasOne
  48. {
  49. return $this->hasOne(DefaultSetting::class, 'purchase_discount_id');
  50. }
  51. public function createdBy(): BelongsTo
  52. {
  53. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  54. }
  55. public function updatedBy(): BelongsTo
  56. {
  57. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  58. }
  59. public function items(): HasMany
  60. {
  61. return $this->hasMany(Item::class);
  62. }
  63. public function document_items(): HasMany
  64. {
  65. return $this->hasMany(DocumentItem::class);
  66. }
  67. public function bill_items(): HasMany
  68. {
  69. return $this->document_items()->where('type', 'bill');
  70. }
  71. public function invoice_items(): HasMany
  72. {
  73. return $this->document_items()->where('type', 'invoice');
  74. }
  75. public static function getComputationTypes(): array
  76. {
  77. return [
  78. 'percentage' => 'Percentage',
  79. 'fixed' => 'Fixed',
  80. ];
  81. }
  82. public static function getDiscountTypes(): array
  83. {
  84. return [
  85. 'sales' => 'Sales',
  86. 'purchase' => 'Purchase',
  87. 'none' => 'None',
  88. ];
  89. }
  90. public static function getDiscountScopes(): array
  91. {
  92. return [
  93. 'product' => 'Product',
  94. 'service' => 'Service',
  95. ];
  96. }
  97. protected static function newFactory(): Factory
  98. {
  99. return DiscountFactory::new();
  100. }
  101. }