Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Models\Document;
  3. use App\Models\Item;
  4. use App\Models\Setting\Discount;
  5. use App\Models\Setting\Tax;
  6. use App\Traits\Blamable;
  7. use App\Traits\CompanyOwned;
  8. use Database\Factories\DocumentItemFactory;
  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 Wallo\FilamentCompanies\FilamentCompanies;
  14. class DocumentItem extends Model
  15. {
  16. use Blamable, CompanyOwned, HasFactory;
  17. protected $table = 'document_items';
  18. protected $fillable = [
  19. 'company_id',
  20. 'document_id',
  21. 'item_id',
  22. 'type',
  23. 'name',
  24. 'description',
  25. 'quantity',
  26. 'price',
  27. 'tax_id',
  28. 'discount_id',
  29. 'total',
  30. 'created_by',
  31. 'updated_by',
  32. ];
  33. protected $casts = [
  34. 'quantity' => 'decimal:2',
  35. 'price' => 'decimal:4',
  36. 'total' => 'decimal:4',
  37. ];
  38. public function company(): BelongsTo
  39. {
  40. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  41. }
  42. public function createdBy(): BelongsTo
  43. {
  44. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  45. }
  46. public function updatedBy(): BelongsTo
  47. {
  48. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  49. }
  50. public function document(): BelongsTo
  51. {
  52. return $this->belongsTo(Document::class);
  53. }
  54. public function item(): BelongsTo
  55. {
  56. return $this->belongsTo(Item::class);
  57. }
  58. public function tax(): BelongsTo
  59. {
  60. return $this->belongsTo(Tax::class);
  61. }
  62. public function discount(): BelongsTo
  63. {
  64. return $this->belongsTo(Discount::class);
  65. }
  66. public function scopeBill($query)
  67. {
  68. return $query->where('type', 'bill');
  69. }
  70. public function scopeInvoice($query)
  71. {
  72. return $query->where('type', 'invoice');
  73. }
  74. /**
  75. * Calculate and return the net price (price - discount + tax)
  76. */
  77. public function getNetPriceAttribute()
  78. {
  79. $discountAmount = $this->discount ? ($this->price * $this->discount->rate / 100) : 0;
  80. $taxAmount = $this->tax ? ($this->price * $this->tax->rate / 100) : 0;
  81. return $this->price - $discountAmount + $taxAmount;
  82. }
  83. protected static function newFactory(): Factory
  84. {
  85. return DocumentItemFactory::new();
  86. }
  87. }