Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Vendor.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Models\Common;
  3. use App\Concerns\Blamable;
  4. use App\Concerns\CompanyOwned;
  5. use App\Enums\Common\ContractorType;
  6. use App\Enums\Common\VendorType;
  7. use App\Models\Accounting\Bill;
  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\MorphOne;
  14. class Vendor extends Model
  15. {
  16. use Blamable;
  17. use CompanyOwned;
  18. use HasFactory;
  19. protected $table = 'vendors';
  20. protected $fillable = [
  21. 'company_id',
  22. 'name',
  23. 'type',
  24. 'contractor_type',
  25. 'ssn',
  26. 'ein',
  27. 'currency_code',
  28. 'account_number',
  29. 'website',
  30. 'notes',
  31. 'created_by',
  32. 'updated_by',
  33. ];
  34. protected $casts = [
  35. 'type' => VendorType::class,
  36. 'contractor_type' => ContractorType::class,
  37. 'ssn' => 'encrypted',
  38. 'ein' => 'encrypted',
  39. ];
  40. public static function createWithRelations(array $data): self
  41. {
  42. /** @var Vendor $vendor */
  43. $vendor = self::create($data);
  44. if (isset($data['contact'], $data['contact']['first_name'])) {
  45. $vendor->contact()->create([
  46. 'is_primary' => true,
  47. 'first_name' => $data['contact']['first_name'],
  48. 'last_name' => $data['contact']['last_name'],
  49. 'email' => $data['contact']['email'],
  50. 'phones' => $data['contact']['phones'] ?? [],
  51. ]);
  52. }
  53. if (isset($data['address'], $data['address']['type'], $data['address']['address_line_1'])) {
  54. $vendor->address()->create([
  55. 'type' => $data['address']['type'],
  56. 'address_line_1' => $data['address']['address_line_1'],
  57. 'address_line_2' => $data['address']['address_line_2'] ?? null,
  58. 'country_code' => $data['address']['country_code'] ?? null,
  59. 'state_id' => $data['address']['state_id'] ?? null,
  60. 'city' => $data['address']['city'] ?? null,
  61. 'postal_code' => $data['address']['postal_code'] ?? null,
  62. ]);
  63. }
  64. return $vendor;
  65. }
  66. public function updateWithRelations(array $data): self
  67. {
  68. $this->update($data);
  69. if (isset($data['contact'], $data['contact']['first_name'])) {
  70. $this->contact()->updateOrCreate(
  71. ['is_primary' => true],
  72. [
  73. 'first_name' => $data['contact']['first_name'],
  74. 'last_name' => $data['contact']['last_name'],
  75. 'email' => $data['contact']['email'],
  76. 'phones' => $data['contact']['phones'] ?? [],
  77. ]
  78. );
  79. }
  80. if (isset($data['address'], $data['address']['type'], $data['address']['address_line_1'])) {
  81. $this->address()->updateOrCreate(
  82. ['type' => $data['address']['type']],
  83. [
  84. 'address_line_1' => $data['address']['address_line_1'],
  85. 'address_line_2' => $data['address']['address_line_2'] ?? null,
  86. 'country_code' => $data['address']['country_code'] ?? null,
  87. 'state_id' => $data['address']['state_id'] ?? null,
  88. 'city' => $data['address']['city'] ?? null,
  89. 'postal_code' => $data['address']['postal_code'] ?? null,
  90. ]
  91. );
  92. }
  93. return $this;
  94. }
  95. public function bills(): HasMany
  96. {
  97. return $this->hasMany(Bill::class);
  98. }
  99. public function currency(): BelongsTo
  100. {
  101. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  102. }
  103. public function address(): MorphOne
  104. {
  105. return $this->morphOne(Address::class, 'addressable');
  106. }
  107. public function contact(): MorphOne
  108. {
  109. return $this->morphOne(Contact::class, 'contactable');
  110. }
  111. }