12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
-
- namespace App\Models;
-
- use App\Models\Document\DocumentItem;
- use App\Models\Setting\Category;
- use App\Models\Setting\Discount;
- use App\Models\Setting\Tax;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Wallo\FilamentCompanies\FilamentCompanies;
-
- class Item extends Model
- {
- use HasFactory;
-
- protected $table = 'items';
-
- protected $fillable = [
- 'company_id',
- 'type',
- 'name',
- 'sku',
- 'description',
- 'sale_price',
- 'purchase_price',
- 'quantity',
- 'category_id',
- 'tax_id',
- 'discount_id',
- 'enabled',
- 'created_by',
- 'updated_by',
- ];
-
- protected $casts = [
- 'enabled' => 'boolean',
- ];
-
- public function company(): BelongsTo
- {
- return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
- }
-
- public function createdBy(): BelongsTo
- {
- return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
- }
-
- public function updatedBy(): BelongsTo
- {
- return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
- }
-
- public function category(): BelongsTo
- {
- return $this->belongsTo(Category::class, 'category_id')->withDefault([
- 'name' => 'General',
- ]);
- }
-
- public function tax(): BelongsTo
- {
- return $this->belongsTo(Tax::class, 'tax_id');
- }
-
- public function discount(): BelongsTo
- {
- return $this->belongsTo(Discount::class, 'discount_id');
- }
-
- public function document_items(): HasMany
- {
- return $this->hasMany(DocumentItem::class);
- }
-
- public function bill_items(): HasMany
- {
- return $this->document_items()->where('type', 'bill');
- }
-
- public function invoice_items(): HasMany
- {
- return $this->document_items()->where('type', 'invoice');
- }
- }
|