您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Client.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Models\Common;
  3. use App\Concerns\Blamable;
  4. use App\Concerns\CompanyOwned;
  5. use App\Enums\Accounting\DocumentType;
  6. use App\Enums\Common\AddressType;
  7. use App\Models\Accounting\Document;
  8. use App\Models\Setting\Currency;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  12. use Illuminate\Database\Eloquent\Relations\HasMany;
  13. use Illuminate\Database\Eloquent\Relations\MorphMany;
  14. use Illuminate\Database\Eloquent\Relations\MorphOne;
  15. class Client extends Model
  16. {
  17. use Blamable;
  18. use CompanyOwned;
  19. use HasFactory;
  20. protected $table = 'clients';
  21. protected $fillable = [
  22. 'company_id',
  23. 'name',
  24. 'currency_code',
  25. 'account_number',
  26. 'website',
  27. 'notes',
  28. 'created_by',
  29. 'updated_by',
  30. ];
  31. public function contacts(): MorphMany
  32. {
  33. return $this->morphMany(Contact::class, 'contactable');
  34. }
  35. public function primaryContact(): MorphOne
  36. {
  37. return $this->morphOne(Contact::class, 'contactable')
  38. ->where('is_primary', true);
  39. }
  40. public function secondaryContacts(): MorphMany
  41. {
  42. return $this->morphMany(Contact::class, 'contactable')
  43. ->where('is_primary', false);
  44. }
  45. public function currency(): BelongsTo
  46. {
  47. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  48. }
  49. public function addresses(): MorphMany
  50. {
  51. return $this->morphMany(Address::class, 'addressable');
  52. }
  53. public function billingAddress(): MorphOne
  54. {
  55. return $this->morphOne(Address::class, 'addressable')
  56. ->where('type', AddressType::Billing);
  57. }
  58. public function shippingAddress(): MorphOne
  59. {
  60. return $this->morphOne(Address::class, 'addressable')
  61. ->where('type', AddressType::Shipping);
  62. }
  63. public function invoices(): HasMany
  64. {
  65. return $this->hasMany(Document::class, 'client_id')
  66. ->where('type', DocumentType::Invoice);
  67. }
  68. }