Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DocumentItem.php 2.4KB

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