選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Client.php 1.9KB

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