Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Client.php 10KB

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