Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DocumentItem.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. ];
  30. protected $casts = [
  31. 'quantity' => 'decimal:2',
  32. 'price' => 'decimal:4',
  33. 'total' => 'decimal:4',
  34. ];
  35. public function company(): BelongsTo
  36. {
  37. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  38. }
  39. public function createdBy(): BelongsTo
  40. {
  41. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  42. }
  43. public function document(): BelongsTo
  44. {
  45. return $this->belongsTo(Document::class);
  46. }
  47. public function item(): BelongsTo
  48. {
  49. return $this->belongsTo(Item::class);
  50. }
  51. public function tax(): BelongsTo
  52. {
  53. return $this->belongsTo(Tax::class);
  54. }
  55. public function discount(): BelongsTo
  56. {
  57. return $this->belongsTo(Discount::class);
  58. }
  59. public function scopeBill($query)
  60. {
  61. return $query->where('type', 'bill');
  62. }
  63. public function scopeInvoice($query)
  64. {
  65. return $query->where('type', 'invoice');
  66. }
  67. /**
  68. * Calculate and return the net price (price - discount + tax)
  69. */
  70. public function getNetPriceAttribute()
  71. {
  72. $discountAmount = $this->discount ? ($this->price * $this->discount->rate / 100) : 0;
  73. $taxAmount = $this->tax ? ($this->price * $this->tax->rate / 100) : 0;
  74. return $this->price - $discountAmount + $taxAmount;
  75. }
  76. protected static function newFactory(): Factory
  77. {
  78. return DocumentItemFactory::new();
  79. }
  80. }