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.

Item.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Document\DocumentItem;
  4. use App\Models\Setting\Category;
  5. use App\Models\Setting\Discount;
  6. use App\Models\Setting\Tax;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Wallo\FilamentCompanies\FilamentCompanies;
  12. class Item extends Model
  13. {
  14. use HasFactory;
  15. protected $table = 'items';
  16. protected $fillable = [
  17. 'company_id',
  18. 'type',
  19. 'name',
  20. 'sku',
  21. 'description',
  22. 'sale_price',
  23. 'purchase_price',
  24. 'quantity',
  25. 'category_id',
  26. 'tax_id',
  27. 'discount_id',
  28. 'enabled',
  29. 'created_by',
  30. 'updated_by',
  31. ];
  32. protected $casts = [
  33. 'enabled' => 'boolean',
  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 updatedBy(): BelongsTo
  44. {
  45. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  46. }
  47. public function category(): BelongsTo
  48. {
  49. return $this->belongsTo(Category::class, 'category_id')->withDefault([
  50. 'name' => 'General',
  51. ]);
  52. }
  53. public function tax(): BelongsTo
  54. {
  55. return $this->belongsTo(Tax::class, 'tax_id');
  56. }
  57. public function discount(): BelongsTo
  58. {
  59. return $this->belongsTo(Discount::class, 'discount_id');
  60. }
  61. public function document_items(): HasMany
  62. {
  63. return $this->hasMany(DocumentItem::class);
  64. }
  65. public function bill_items(): HasMany
  66. {
  67. return $this->document_items()->where('type', 'bill');
  68. }
  69. public function invoice_items(): HasMany
  70. {
  71. return $this->document_items()->where('type', 'invoice');
  72. }
  73. }