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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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\Estimate;
  7. use App\Models\Accounting\Invoice;
  8. use App\Models\Accounting\RecurringInvoice;
  9. use App\Models\Accounting\Transaction;
  10. use App\Models\Setting\Currency;
  11. use Illuminate\Database\Eloquent\Factories\HasFactory;
  12. use Illuminate\Database\Eloquent\Model;
  13. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  14. use Illuminate\Database\Eloquent\Relations\HasMany;
  15. use Illuminate\Database\Eloquent\Relations\MorphMany;
  16. use Illuminate\Database\Eloquent\Relations\MorphOne;
  17. class Client extends Model
  18. {
  19. use Blamable;
  20. use CompanyOwned;
  21. use HasFactory;
  22. protected $table = 'clients';
  23. protected $fillable = [
  24. 'company_id',
  25. 'name',
  26. 'currency_code',
  27. 'account_number',
  28. 'website',
  29. 'notes',
  30. 'created_by',
  31. 'updated_by',
  32. ];
  33. public static function createWithRelations(array $data): self
  34. {
  35. /** @var Client $client */
  36. $client = self::create($data);
  37. if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
  38. $client->primaryContact()->create([
  39. 'is_primary' => true,
  40. 'first_name' => $data['primaryContact']['first_name'],
  41. 'last_name' => $data['primaryContact']['last_name'],
  42. 'email' => $data['primaryContact']['email'],
  43. 'phones' => $data['primaryContact']['phones'] ?? [],
  44. ]);
  45. }
  46. if (isset($data['secondaryContacts'])) {
  47. foreach ($data['secondaryContacts'] as $contactData) {
  48. if (isset($contactData['first_name'])) {
  49. $client->secondaryContacts()->create([
  50. 'is_primary' => false,
  51. 'first_name' => $contactData['first_name'],
  52. 'last_name' => $contactData['last_name'],
  53. 'email' => $contactData['email'],
  54. 'phones' => $contactData['phones'] ?? [],
  55. ]);
  56. }
  57. }
  58. }
  59. if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
  60. $client->billingAddress()->create([
  61. 'type' => AddressType::Billing,
  62. 'address_line_1' => $data['billingAddress']['address_line_1'],
  63. 'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
  64. 'country_code' => $data['billingAddress']['country_code'] ?? null,
  65. 'state_id' => $data['billingAddress']['state_id'] ?? null,
  66. 'city' => $data['billingAddress']['city'] ?? null,
  67. 'postal_code' => $data['billingAddress']['postal_code'] ?? null,
  68. ]);
  69. }
  70. if (isset($data['shippingAddress'])) {
  71. $shippingData = $data['shippingAddress'];
  72. $shippingAddress = [
  73. 'type' => AddressType::Shipping,
  74. 'recipient' => $shippingData['recipient'] ?? null,
  75. 'phone' => $shippingData['phone'] ?? null,
  76. 'notes' => $shippingData['notes'] ?? null,
  77. ];
  78. if ($shippingData['same_as_billing'] ?? false) {
  79. $billingAddress = $client->billingAddress;
  80. if ($billingAddress) {
  81. $shippingAddress = [
  82. ...$shippingAddress,
  83. 'parent_address_id' => $billingAddress->id,
  84. 'address_line_1' => $billingAddress->address_line_1,
  85. 'address_line_2' => $billingAddress->address_line_2,
  86. 'country_code' => $billingAddress->country_code,
  87. 'state_id' => $billingAddress->state_id,
  88. 'city' => $billingAddress->city,
  89. 'postal_code' => $billingAddress->postal_code,
  90. ];
  91. $client->shippingAddress()->create($shippingAddress);
  92. }
  93. } elseif (isset($shippingData['address_line_1'])) {
  94. $shippingAddress = [
  95. ...$shippingAddress,
  96. 'address_line_1' => $shippingData['address_line_1'],
  97. 'address_line_2' => $shippingData['address_line_2'] ?? null,
  98. 'country_code' => $shippingData['country_code'] ?? null,
  99. 'state_id' => $shippingData['state_id'] ?? null,
  100. 'city' => $shippingData['city'] ?? null,
  101. 'postal_code' => $shippingData['postal_code'] ?? null,
  102. ];
  103. $client->shippingAddress()->create($shippingAddress);
  104. }
  105. }
  106. return $client;
  107. }
  108. public function updateWithRelations(array $data): self
  109. {
  110. $this->update($data);
  111. if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
  112. $this->primaryContact()->updateOrCreate(
  113. ['is_primary' => true],
  114. [
  115. 'first_name' => $data['primaryContact']['first_name'],
  116. 'last_name' => $data['primaryContact']['last_name'],
  117. 'email' => $data['primaryContact']['email'],
  118. 'phones' => $data['primaryContact']['phones'] ?? [],
  119. ]
  120. );
  121. }
  122. if (isset($data['secondaryContacts'])) {
  123. // Delete removed contacts
  124. $existingIds = collect($data['secondaryContacts'])->pluck('id')->filter()->all();
  125. $this->secondaryContacts()->whereNotIn('id', $existingIds)->delete();
  126. // Update or create contacts
  127. foreach ($data['secondaryContacts'] as $contactData) {
  128. if (isset($contactData['first_name'])) {
  129. $this->secondaryContacts()->updateOrCreate(
  130. ['id' => $contactData['id'] ?? null],
  131. [
  132. 'is_primary' => false,
  133. 'first_name' => $contactData['first_name'],
  134. 'last_name' => $contactData['last_name'],
  135. 'email' => $contactData['email'],
  136. 'phones' => $contactData['phones'] ?? [],
  137. ]
  138. );
  139. }
  140. }
  141. }
  142. if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
  143. $this->billingAddress()->updateOrCreate(
  144. ['type' => AddressType::Billing],
  145. [
  146. 'address_line_1' => $data['billingAddress']['address_line_1'],
  147. 'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
  148. 'country_code' => $data['billingAddress']['country_code'] ?? null,
  149. 'state_id' => $data['billingAddress']['state_id'] ?? null,
  150. 'city' => $data['billingAddress']['city'] ?? null,
  151. 'postal_code' => $data['billingAddress']['postal_code'] ?? null,
  152. ]
  153. );
  154. }
  155. if (isset($data['shippingAddress'])) {
  156. $shippingData = $data['shippingAddress'];
  157. if ($shippingData['same_as_billing'] ?? false) {
  158. $billingAddress = $this->billingAddress;
  159. if ($billingAddress) {
  160. $shippingAddress = [
  161. 'type' => AddressType::Shipping,
  162. 'recipient' => $shippingData['recipient'] ?? null,
  163. 'phone' => $shippingData['phone'] ?? null,
  164. 'notes' => $shippingData['notes'] ?? null,
  165. 'parent_address_id' => $billingAddress->id,
  166. 'address_line_1' => $billingAddress->address_line_1,
  167. 'address_line_2' => $billingAddress->address_line_2,
  168. 'country_code' => $billingAddress->country_code,
  169. 'state_id' => $billingAddress->state_id,
  170. 'city' => $billingAddress->city,
  171. 'postal_code' => $billingAddress->postal_code,
  172. ];
  173. $this->shippingAddress()->updateOrCreate(
  174. ['type' => AddressType::Shipping],
  175. $shippingAddress
  176. );
  177. }
  178. } elseif (isset($shippingData['address_line_1'])) {
  179. $shippingAddress = [
  180. 'type' => AddressType::Shipping,
  181. 'recipient' => $shippingData['recipient'] ?? null,
  182. 'phone' => $shippingData['phone'] ?? null,
  183. 'notes' => $shippingData['notes'] ?? null,
  184. 'parent_address_id' => null,
  185. 'address_line_1' => $shippingData['address_line_1'],
  186. 'address_line_2' => $shippingData['address_line_2'] ?? null,
  187. 'country_code' => $shippingData['country_code'] ?? null,
  188. 'state_id' => $shippingData['state_id'] ?? null,
  189. 'city' => $shippingData['city'] ?? null,
  190. 'postal_code' => $shippingData['postal_code'] ?? null,
  191. ];
  192. $this->shippingAddress()->updateOrCreate(
  193. ['type' => AddressType::Shipping],
  194. $shippingAddress
  195. );
  196. }
  197. }
  198. return $this;
  199. }
  200. public function transactions(): MorphMany
  201. {
  202. return $this->morphMany(Transaction::class, 'payeeable');
  203. }
  204. public function contacts(): MorphMany
  205. {
  206. return $this->morphMany(Contact::class, 'contactable');
  207. }
  208. public function primaryContact(): MorphOne
  209. {
  210. return $this->morphOne(Contact::class, 'contactable')
  211. ->where('is_primary', true);
  212. }
  213. public function secondaryContacts(): MorphMany
  214. {
  215. return $this->morphMany(Contact::class, 'contactable')
  216. ->where('is_primary', false);
  217. }
  218. public function currency(): BelongsTo
  219. {
  220. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  221. }
  222. public function addresses(): MorphMany
  223. {
  224. return $this->morphMany(Address::class, 'addressable');
  225. }
  226. public function billingAddress(): MorphOne
  227. {
  228. return $this->morphOne(Address::class, 'addressable')
  229. ->where('type', AddressType::Billing);
  230. }
  231. public function shippingAddress(): MorphOne
  232. {
  233. return $this->morphOne(Address::class, 'addressable')
  234. ->where('type', AddressType::Shipping);
  235. }
  236. public function estimates(): HasMany
  237. {
  238. return $this->hasMany(Estimate::class);
  239. }
  240. public function invoices(): HasMany
  241. {
  242. return $this->hasMany(Invoice::class);
  243. }
  244. public function recurringInvoices(): HasMany
  245. {
  246. return $this->hasMany(RecurringInvoice::class);
  247. }
  248. }