123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
-
- namespace App\Models\Common;
-
- use App\Concerns\Blamable;
- use App\Concerns\CompanyOwned;
- use App\Enums\Accounting\DocumentType;
- use App\Enums\Common\AddressType;
- use App\Models\Accounting\Document;
- use App\Models\Setting\Currency;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\MorphMany;
- use Illuminate\Database\Eloquent\Relations\MorphOne;
-
- class Client extends Model
- {
- use Blamable;
- use CompanyOwned;
- use HasFactory;
-
- protected $table = 'clients';
-
- protected $fillable = [
- 'company_id',
- 'name',
- 'currency_code',
- 'account_number',
- 'website',
- 'notes',
- 'created_by',
- 'updated_by',
- ];
-
- public function contacts(): MorphMany
- {
- return $this->morphMany(Contact::class, 'contactable');
- }
-
- public function primaryContact(): MorphOne
- {
- return $this->morphOne(Contact::class, 'contactable')
- ->where('is_primary', true);
- }
-
- public function secondaryContacts(): MorphMany
- {
- return $this->morphMany(Contact::class, 'contactable')
- ->where('is_primary', false);
- }
-
- public function currency(): BelongsTo
- {
- return $this->belongsTo(Currency::class, 'currency_code', 'code');
- }
-
- public function addresses(): MorphMany
- {
- return $this->morphMany(Address::class, 'addressable');
- }
-
- public function billingAddress(): MorphOne
- {
- return $this->morphOne(Address::class, 'addressable')
- ->where('type', AddressType::Billing);
- }
-
- public function shippingAddress(): MorphOne
- {
- return $this->morphOne(Address::class, 'addressable')
- ->where('type', AddressType::Shipping);
- }
-
- public function invoices(): HasMany
- {
- return $this->hasMany(Document::class, 'client_id')
- ->where('type', DocumentType::Invoice);
- }
- }
|